2 * daemon.c -- turn a process into a daemon under POSIX, SYSV, BSD.
4 * For license terms, see the file COPYING in this directory.
12 #include <sys/types.h>
13 #ifdef HAVE_SYS_WAIT_H
18 #else /* !HAVE_FCNTL_H */
19 #ifdef HAVE_SYS_FCNTL_H
20 #include <sys/fcntl.h>
21 #endif /* HAVE_SYS_FCNTL_H */
22 #endif /* !HAVE_FCNTL_H */
23 #include <sys/stat.h> /* get umask(2) prototyped */
25 #if defined(HAVE_UNISTD_H)
29 #if defined(STDC_HEADERS)
37 #if !defined(HAVE_SETSID) && defined(SIGTSTP)
38 #if defined(HAVE_TERMIOS_H)
39 # include <termios.h> /* for TIOCNOTTY under Linux */
42 #if !defined(TIOCNOTTY) && defined(HAVE_SGTTY_H)
43 # include <sgtty.h> /* for TIOCNOTTY under NEXTSTEP */
45 #endif /* !defined(HAVE_SETSID) && defined(SIGTSTP) */
47 /* BSD portability hack */
48 #if !defined(SIGCHLD) && defined(SIGCLD)
49 #define SIGCHLD SIGCLD
52 #include "fetchmail.h"
56 sigchld_handler (int sig)
57 /* process SIGCHLD to obtain the exit code of the terminating process */
61 #if defined(HAVE_WAITPID) /* the POSIX way */
64 while ((pid = waitpid(-1, &status, WNOHANG)) > 0)
65 continue; /* swallow 'em up. */
66 #elif defined(HAVE_WAIT3) /* the BSD way */
67 #if defined(HAVE_UNION_WAIT) && !defined(__FreeBSD__)
73 while ((pid = wait3(&status, WNOHANG, 0)) > 0)
74 continue; /* swallow 'em up. */
75 #else /* Zooks! Nothing to do but wait(), and hope we don't block... */
83 daemonize (const char *logfile, void (*termhook)(int))
84 /* detach from control TTY, become process group leader, catch SIGCHLD */
88 RETSIGTYPE sigchld_handler();
90 /* if we are started by init (process 1) via /etc/inittab we needn't
91 bother to detach from our process group context */
96 /* Ignore BSD terminal stop signals */
98 signal(SIGTTOU, SIG_IGN);
101 signal(SIGTTIN, SIG_IGN);
104 signal(SIGTSTP, SIG_IGN);
107 /* In case we were not started in the background, fork and let
108 the parent exit. Guarantees that the child is not a process
111 if ((childpid = fork()) < 0) {
112 error(0, errno, "fork");
115 else if (childpid > 0)
116 exit(0); /* parent */
119 /* Make ourselves the leader of a new process group with no
120 controlling terminal */
122 #if defined(HAVE_SETSID) /* POSIX */
123 /* POSIX makes this soooo easy to do */
125 error(0, errno, "setsid");
128 #elif defined(SIGTSTP) /* BSD */
129 /* change process group */
130 setpgrp(0, getpid());
132 /* lose controlling tty */
133 if ((fd = open("/dev/tty", O_RDWR)) >= 0) {
134 ioctl(fd, TIOCNOTTY, (char *) 0);
137 #else /* SVR3 and older */
138 /* change process group */
141 /* lose controlling tty */
142 signal(SIGHUP, SIG_IGN);
143 if ((childpid = fork()) < 0) {
144 error(0, errno, "fork");
147 else if (childpid > 0) {
148 exit(0); /* parent */
154 /* Close any/all open file descriptors */
155 #if defined(HAVE_GETDTABLESIZE)
156 for (fd = getdtablesize()-1; fd >= 0; fd--)
157 #elif defined(NOFILE)
158 for (fd = NOFILE-1; fd >= 0; fd--)
159 #else /* make an educated guess */
160 for (fd = 19; fd >= 0; fd--)
166 /* Reopen stdin descriptor on /dev/null */
167 if ((fd = open("/dev/null", O_RDWR)) < 0) { /* stdin */
168 error(0, errno, "open: /dev/null");
173 fd = open(logfile, O_CREAT|O_WRONLY|O_APPEND, 0666); /* stdout */
175 if (dup(fd) < 0) { /* stdout */
176 error(0, errno, "dup");
179 if (dup(fd) < 0) { /* stderr */
180 error(0, errno, "dup");
184 /* move to root directory, so we don't prevent filesystem unmounts */
187 /* set our umask to something reasonable (we hope) */
188 #if defined(DEF_UMASK)
194 /* set up to catch child process termination signals */
195 signal(SIGCHLD, sigchld_handler);
197 signal(SIGPWR, sigchld_handler);
203 /* daemon.c ends here */