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>
16 #include <sys/fcntl.h> /* Solaris 2.5 requires this */
18 #if defined(HAVE_UNISTD_H)
22 #if defined(STDC_HEADERS)
30 #if defined(HAVE_SGTTY_H)
31 # include <sgtty.h> /* for TIOCNOTTY under NEXTSTEP */
38 /* BSD portability hack */
39 #if !defined(SIGCHLD) && defined(SICHLD)
40 #define SIGCHLD SIGCLD
43 #include "fetchmail.h"
47 sigchld_handler (int sig)
48 /* process SIGCHLD to obtain the exit code of the terminating process */
52 #if defined(HAVE_UNION_WAIT)
58 #if defined(HAVE_WAIT3)
59 while ((pid = wait3(&status, WNOHANG, (struct rusage *) 0)) > 0)
60 ; /* swallow 'em up. */
61 #elif defined(HAVE_WAITPID)
62 while ((pid = waitpid(-1, &status, WNOHANG)) > 0)
63 ; /* swallow 'em up. */
64 #else /* Zooks! Nothing to do but wait(), and hope we don't block... */
71 daemonize (const char *logfile, void (*termhook)(int))
72 /* detach from control TTY, become process group leader, catch SIGCHLD */
76 RETSIGTYPE sigchld_handler();
78 /* if we are started by init (process 1) via /etc/inittab we needn't
79 bother to detach from our process group context */
84 /* Ignore BSD terminal stop signals */
86 signal(SIGTTOU, SIG_IGN);
89 signal(SIGTTIN, SIG_IGN);
92 signal(SIGTSTP, SIG_IGN);
95 /* In case we were not started in the background, fork and let
96 the parent exit. Guarantees that the child is not a process
99 if ((childpid = fork()) < 0) {
100 error(0, errno, "fork");
103 else if (childpid > 0)
104 exit(0); /* parent */
107 /* Make ourselves the leader of a new process group with no
108 controlling terminal */
110 #if defined(HAVE_SETSID) /* POSIX */
111 /* POSIX makes this soooo easy to do */
113 error(0, errno, "setsid");
116 #elif defined(SIGTSTP) /* BSD */
117 /* change process group */
118 setpgrp(0, getpid());
120 /* lose controlling tty */
121 if ((fd = open("/dev/tty", O_RDWR)) >= 0) {
122 ioctl(fd, TIOCNOTTY, (char *) 0);
125 #else /* SVR3 and older */
126 /* change process group */
129 /* lose controlling tty */
130 signal(SIGHUP, SIG_IGN);
131 if ((childpid = fork) < 0) {
132 error(0, errno, "fork");
135 else if (childpid > 0) {
136 exit(0); /* parent */
142 /* Close any/all open file descriptors */
143 #if defined(HAVE_GETDTABLESIZE)
144 for (fd = getdtablesize()-1; fd >= 0; fd--)
145 #elif defined(NOFILE)
146 for (fd = NOFILE-1; fd >= 0; fd--)
147 #else /* make an educated guess */
148 for (fd = 19; fd >= 0; fd--)
154 /* Reopen stdin descriptor on /dev/null */
155 if ((fd = open("/dev/null", O_RDWR)) < 0) { /* stdin */
156 error(0, errno, "open: /dev/null");
161 fd = open(logfile, O_CREAT|O_WRONLY|O_APPEND, 0777); /* stdout */
163 if (dup(fd) < 0) { /* stdout */
164 error(0, errno, "dup");
167 if (dup(fd) < 0) { /* stderr */
168 error(0, errno, "dup");
172 /* move to root directory, so we don't prevent filesystem unmounts */
175 /* set our umask to something reasonable (we hope) */
176 #if defined(DEF_UMASK)
182 /* set up to catch child process termination signals */
183 signal(SIGCHLD, sigchld_handler);
185 signal(SIGPWR, sigchld_handler);
191 /* daemon.c ends here */