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(int);
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 */
131 setpgrp(0, getpid());
133 /* lose controlling tty */
134 if ((fd = open("/dev/tty", O_RDWR)) >= 0) {
135 ioctl(fd, TIOCNOTTY, (char *) 0);
138 #else /* SVR3 and older */
139 /* change process group */
144 /* lose controlling tty */
145 signal(SIGHUP, SIG_IGN);
146 if ((childpid = fork()) < 0) {
147 error(0, errno, "fork");
150 else if (childpid > 0) {
151 exit(0); /* parent */
157 /* Close any/all open file descriptors */
158 #if defined(HAVE_GETDTABLESIZE)
159 for (fd = getdtablesize()-1; fd >= 0; fd--)
160 #elif defined(NOFILE)
161 for (fd = NOFILE-1; fd >= 0; fd--)
162 #else /* make an educated guess */
163 for (fd = 19; fd >= 0; fd--)
169 /* Reopen stdin descriptor on /dev/null */
170 if ((fd = open("/dev/null", O_RDWR)) < 0) { /* stdin */
171 error(0, errno, "open: /dev/null");
176 fd = open(logfile, O_CREAT|O_WRONLY|O_APPEND, 0666); /* stdout */
178 if (dup(fd) < 0) { /* stdout */
179 error(0, errno, "dup");
182 if (dup(fd) < 0) { /* stderr */
183 error(0, errno, "dup");
187 /* move to root directory, so we don't prevent filesystem unmounts */
190 /* set our umask to something reasonable (we hope) */
191 #if defined(DEF_UMASK)
197 /* set up to catch child process termination signals */
198 signal(SIGCHLD, sigchld_handler);
200 signal(SIGPWR, sigchld_handler);
206 /* daemon.c ends here */