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)
53 while ((pid = wait3(&status, WNOHANG, (struct rusage *) 0)) > 0)
54 ; /* swallow 'em up. */
55 #elif defined(HAVE_WAITPID)
56 while ((pid = waitpid(-1, &status, WNOHANG)) > 0)
57 ; /* swallow 'em up. */
58 #else /* Zooks! Nothing to do but wait(), and hope we don't block... */
65 daemonize (const char *logfile, void (*termhook)(int))
66 /* detach from control TTY, become process group leader, catch SIGCHLD */
70 RETSIGTYPE sigchld_handler();
72 /* if we are started by init (process 1) via /etc/inittab we needn't
73 bother to detach from our process group context */
78 /* Ignore BSD terminal stop signals */
80 signal(SIGTTOU, SIG_IGN);
83 signal(SIGTTIN, SIG_IGN);
86 signal(SIGTSTP, SIG_IGN);
89 /* In case we were not started in the background, fork and let
90 the parent exit. Guarantees that the child is not a process
93 if ((childpid = fork()) < 0) {
94 error(0, errno, "fork");
97 else if (childpid > 0)
101 /* Make ourselves the leader of a new process group with no
102 controlling terminal */
104 #if defined(HAVE_SETSID) /* POSIX */
105 /* POSIX makes this soooo easy to do */
107 error(0, errno, "setsid");
110 #elif defined(SIGTSTP) /* BSD */
111 /* change process group */
112 setpgrp(0, getpid());
114 /* lose controlling tty */
115 if ((fd = open("/dev/tty", O_RDWR)) >= 0) {
116 ioctl(fd, TIOCNOTTY, (char *) 0);
119 #else /* SVR3 and older */
120 /* change process group */
123 /* lose controlling tty */
124 signal(SIGHUP, SIG_IGN);
125 if ((childpid = fork()) < 0) {
126 error(0, errno, "fork");
129 else if (childpid > 0) {
130 exit(0); /* parent */
136 /* Close any/all open file descriptors */
137 #if defined(HAVE_GETDTABLESIZE)
138 for (fd = getdtablesize()-1; fd >= 0; fd--)
139 #elif defined(NOFILE)
140 for (fd = NOFILE-1; fd >= 0; fd--)
141 #else /* make an educated guess */
142 for (fd = 19; fd >= 0; fd--)
148 /* Reopen stdin descriptor on /dev/null */
149 if ((fd = open("/dev/null", O_RDWR)) < 0) { /* stdin */
150 error(0, errno, "open: /dev/null");
155 fd = open(logfile, O_CREAT|O_WRONLY|O_APPEND, 0666); /* stdout */
157 if (dup(fd) < 0) { /* stdout */
158 error(0, errno, "dup");
161 if (dup(fd) < 0) { /* stderr */
162 error(0, errno, "dup");
166 /* move to root directory, so we don't prevent filesystem unmounts */
169 /* set our umask to something reasonable (we hope) */
170 #if defined(DEF_UMASK)
176 /* set up to catch child process termination signals */
177 signal(SIGCHLD, sigchld_handler);
179 signal(SIGPWR, sigchld_handler);
185 /* daemon.c ends here */