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 #endif /* HAVE_FCNTL_H */
17 #ifdef HAVE_SYS_FCNTL_H
18 #include <sys/fcntl.h>
19 #endif /* HAVE_SYS_FCNTL_H */
20 #include <sys/stat.h> /* get umask(2) prototyped */
22 #if defined(HAVE_UNISTD_H)
26 #if defined(STDC_HEADERS)
34 #if defined(HAVE_SGTTY_H)
35 # include <sgtty.h> /* for TIOCNOTTY under NEXTSTEP */
38 /* BSD portability hack */
39 #if !defined(SIGCHLD) && defined(SIGCLD)
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 #ifdef oldhpux /* HP-UX fixed this sometime between 9.01 and 10.20 */
60 while ((pid = wait3(&status, WNOHANG, (int *) 0)) > 0)
62 while ((pid = wait3(&status, WNOHANG, (struct rusage *) 0)) > 0)
64 ; /* swallow 'em up. */
65 #elif defined(HAVE_WAITPID)
66 while ((pid = waitpid(-1, &status, WNOHANG)) > 0)
67 ; /* swallow 'em up. */
68 #else /* Zooks! Nothing to do but wait(), and hope we don't block... */
75 daemonize (const char *logfile, void (*termhook)(int))
76 /* detach from control TTY, become process group leader, catch SIGCHLD */
80 RETSIGTYPE sigchld_handler();
82 /* if we are started by init (process 1) via /etc/inittab we needn't
83 bother to detach from our process group context */
88 /* Ignore BSD terminal stop signals */
90 signal(SIGTTOU, SIG_IGN);
93 signal(SIGTTIN, SIG_IGN);
96 signal(SIGTSTP, SIG_IGN);
99 /* In case we were not started in the background, fork and let
100 the parent exit. Guarantees that the child is not a process
103 if ((childpid = fork()) < 0) {
104 error(0, errno, "fork");
107 else if (childpid > 0)
108 exit(0); /* parent */
111 /* Make ourselves the leader of a new process group with no
112 controlling terminal */
114 #if defined(HAVE_SETSID) /* POSIX */
115 /* POSIX makes this soooo easy to do */
117 error(0, errno, "setsid");
120 #elif defined(SIGTSTP) /* BSD */
121 /* change process group */
122 setpgrp(0, getpid());
124 /* lose controlling tty */
125 if ((fd = open("/dev/tty", O_RDWR)) >= 0) {
126 ioctl(fd, TIOCNOTTY, (char *) 0);
129 #else /* SVR3 and older */
130 /* change process group */
133 /* lose controlling tty */
134 signal(SIGHUP, SIG_IGN);
135 if ((childpid = fork()) < 0) {
136 error(0, errno, "fork");
139 else if (childpid > 0) {
140 exit(0); /* parent */
146 /* Close any/all open file descriptors */
147 #if defined(HAVE_GETDTABLESIZE)
148 for (fd = getdtablesize()-1; fd >= 0; fd--)
149 #elif defined(NOFILE)
150 for (fd = NOFILE-1; fd >= 0; fd--)
151 #else /* make an educated guess */
152 for (fd = 19; fd >= 0; fd--)
158 /* Reopen stdin descriptor on /dev/null */
159 if ((fd = open("/dev/null", O_RDWR)) < 0) { /* stdin */
160 error(0, errno, "open: /dev/null");
165 fd = open(logfile, O_CREAT|O_WRONLY|O_APPEND, 0666); /* stdout */
167 if (dup(fd) < 0) { /* stdout */
168 error(0, errno, "dup");
171 if (dup(fd) < 0) { /* stderr */
172 error(0, errno, "dup");
176 /* move to root directory, so we don't prevent filesystem unmounts */
179 /* set our umask to something reasonable (we hope) */
180 #if defined(DEF_UMASK)
186 /* set up to catch child process termination signals */
187 signal(SIGCHLD, sigchld_handler);
189 signal(SIGPWR, sigchld_handler);
195 /* daemon.c ends here */