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 */
34 /* BSD portability hack */
35 #if !defined(SIGCHLD) && defined(SICHLD)
36 #define SIGCHLD SIGCLD
39 #include "fetchmail.h"
43 sigchld_handler (int sig)
44 /* process SIGCHLD to obtain the exit code of the terminating process */
48 #if defined(HAVE_UNION_WAIT)
54 #if defined(HAVE_WAIT3)
55 while ((pid = wait3(&status, WNOHANG, (struct rusage *) 0)) > 0)
56 ; /* swallow 'em up. */
57 #elif defined(HAVE_WAITPID)
58 while ((pid = waitpid(-1, &status, WNOHANG)) > 0)
59 ; /* swallow 'em up. */
60 #else /* Zooks! Nothing to do but wait(), and hope we don't block... */
67 daemonize (const char *logfile, void (*termhook)(int))
68 /* detach from control TTY, become process group leader, catch SIGCHLD */
72 RETSIGTYPE sigchld_handler();
74 /* if we are started by init (process 1) via /etc/inittab we needn't
75 bother to detach from our process group context */
80 /* Ignore BSD terminal stop signals */
82 signal(SIGTTOU, SIG_IGN);
85 signal(SIGTTIN, SIG_IGN);
88 signal(SIGTSTP, SIG_IGN);
91 /* In case we were not started in the background, fork and let
92 the parent exit. Guarantees that the child is not a process
95 if ((childpid = fork()) < 0) {
96 error(0, errno, "fork");
99 else if (childpid > 0)
100 exit(0); /* parent */
103 /* Make ourselves the leader of a new process group with no
104 controlling terminal */
106 #if defined(HAVE_SETSID) /* POSIX */
107 /* POSIX makes this soooo easy to do */
109 error(0, errno, "setsid");
112 #elif defined(SIGTSTP) /* BSD */
113 /* change process group */
114 setpgrp(0, getpid());
116 /* lose controlling tty */
117 if ((fd = open("/dev/tty", O_RDWR)) >= 0) {
118 ioctl(fd, TIOCNOTTY, (char *) 0);
121 #else /* SVR3 and older */
122 /* change process group */
125 /* lose controlling tty */
126 signal(SIGHUP, SIG_IGN);
127 if ((childpid = fork) < 0) {
128 error(0, errno, "fork");
131 else if (childpid > 0) {
132 exit(0); /* parent */
138 /* Close any/all open file descriptors */
139 #if defined(HAVE_GETDTABLESIZE)
140 for (fd = getdtablesize()-1; fd >= 0; fd--)
141 #elif defined(NOFILE)
142 for (fd = NOFILE-1; fd >= 0; fd--)
143 #else /* make an educated guess */
144 for (fd = 19; fd >= 0; fd--)
150 /* Reopen stdin descriptor on /dev/null */
151 if ((fd = open("/dev/null", O_RDWR)) < 0) { /* stdin */
152 error(0, errno, "open: /dev/null");
157 fd = open(logfile, O_CREAT|O_WRONLY|O_APPEND, 0777); /* stdout */
159 if (dup(fd) < 0) { /* stdout */
160 error(0, errno, "dup");
163 if (dup(fd) < 0) { /* stderr */
164 error(0, errno, "dup");
168 /* move to root directory, so we don't prevent filesystem unmounts */
171 /* set our umask to something reasonable (we hope) */
172 #if defined(DEF_UMASK)
178 /* set up to catch child process termination signals */
179 signal(SIGCHLD, sigchld_handler);
181 signal(SIGPWR, sigchld_handler);
187 /* daemon.c ends here */