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>
14 #include <sys/fcntl.h> /* Solaris 2.5 requires this */
16 #if defined(HAVE_UNISTD_H)
20 #if defined(STDC_HEADERS)
28 #if defined(HAVE_SGTTY_H)
29 # include <sgtty.h> /* for TIOCNOTTY under NEXTSTEP */
32 /* BSD portability hack */
33 #if !defined(SIGCHLD) && defined(SIGCLD)
34 #define SIGCHLD SIGCLD
37 #include "fetchmail.h"
41 sigchld_handler (int sig)
42 /* process SIGCHLD to obtain the exit code of the terminating process */
46 #if defined(HAVE_UNION_WAIT)
52 #if defined(HAVE_WAIT3)
54 while ((pid = wait3(&status, WNOHANG, (int *) 0)) > 0)
56 while ((pid = wait3(&status, WNOHANG, (struct rusage *) 0)) > 0)
58 ; /* swallow 'em up. */
59 #elif defined(HAVE_WAITPID)
60 while ((pid = waitpid(-1, &status, WNOHANG)) > 0)
61 ; /* swallow 'em up. */
62 #else /* Zooks! Nothing to do but wait(), and hope we don't block... */
69 daemonize (const char *logfile, void (*termhook)(int))
70 /* detach from control TTY, become process group leader, catch SIGCHLD */
74 RETSIGTYPE sigchld_handler();
76 /* if we are started by init (process 1) via /etc/inittab we needn't
77 bother to detach from our process group context */
82 /* Ignore BSD terminal stop signals */
84 signal(SIGTTOU, SIG_IGN);
87 signal(SIGTTIN, SIG_IGN);
90 signal(SIGTSTP, SIG_IGN);
93 /* In case we were not started in the background, fork and let
94 the parent exit. Guarantees that the child is not a process
97 if ((childpid = fork()) < 0) {
98 error(0, errno, "fork");
101 else if (childpid > 0)
102 exit(0); /* parent */
105 /* Make ourselves the leader of a new process group with no
106 controlling terminal */
108 #if defined(HAVE_SETSID) /* POSIX */
109 /* POSIX makes this soooo easy to do */
111 error(0, errno, "setsid");
114 #elif defined(SIGTSTP) /* BSD */
115 /* change process group */
116 setpgrp(0, getpid());
118 /* lose controlling tty */
119 if ((fd = open("/dev/tty", O_RDWR)) >= 0) {
120 ioctl(fd, TIOCNOTTY, (char *) 0);
123 #else /* SVR3 and older */
124 /* change process group */
127 /* lose controlling tty */
128 signal(SIGHUP, SIG_IGN);
129 if ((childpid = fork()) < 0) {
130 error(0, errno, "fork");
133 else if (childpid > 0) {
134 exit(0); /* parent */
140 /* Close any/all open file descriptors */
141 #if defined(HAVE_GETDTABLESIZE)
142 for (fd = getdtablesize()-1; fd >= 0; fd--)
143 #elif defined(NOFILE)
144 for (fd = NOFILE-1; fd >= 0; fd--)
145 #else /* make an educated guess */
146 for (fd = 19; fd >= 0; fd--)
152 /* Reopen stdin descriptor on /dev/null */
153 if ((fd = open("/dev/null", O_RDWR)) < 0) { /* stdin */
154 error(0, errno, "open: /dev/null");
159 fd = open(logfile, O_CREAT|O_WRONLY|O_APPEND, 0666); /* stdout */
161 if (dup(fd) < 0) { /* stdout */
162 error(0, errno, "dup");
165 if (dup(fd) < 0) { /* stderr */
166 error(0, errno, "dup");
170 /* move to root directory, so we don't prevent filesystem unmounts */
173 /* set our umask to something reasonable (we hope) */
174 #if defined(DEF_UMASK)
180 /* set up to catch child process termination signals */
181 signal(SIGCHLD, sigchld_handler);
183 signal(SIGPWR, sigchld_handler);
189 /* daemon.c ends here */