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 #else /* !HAVE_FCNTL_H */
17 #ifdef HAVE_SYS_FCNTL_H
18 #include <sys/fcntl.h>
19 #endif /* HAVE_SYS_FCNTL_H */
20 #endif /* !HAVE_FCNTL_H */
21 #include <sys/stat.h> /* get umask(2) prototyped */
23 #if defined(HAVE_UNISTD_H)
27 #if defined(STDC_HEADERS)
35 #if !defined(HAVE_SETSID) && defined(SIGTSTP)
36 #if defined(HAVE_TERMIOS_H)
37 # include <termios.h> /* for TIOCNOTTY under Linux */
40 #if !defined(TIOCNOTTY) && defined(HAVE_SGTTY_H)
41 # include <sgtty.h> /* for TIOCNOTTY under NEXTSTEP */
43 #endif /* !defined(HAVE_SETSID) && defined(SIGTSTP) */
45 /* BSD portability hack */
46 #if !defined(SIGCHLD) && defined(SIGCLD)
47 #define SIGCHLD SIGCLD
50 #include "fetchmail.h"
54 sigchld_handler (int sig)
55 /* process SIGCHLD to obtain the exit code of the terminating process */
59 #if defined(HAVE_WAITPID) /* the POSIX way */
62 while ((pid = waitpid(-1, &status, WNOHANG)) > 0)
63 continue; /* swallow 'em up. */
64 #elif defined(HAVE_WAIT3) /* the BSD way */
65 #if defined(HAVE_UNION_WAIT) && !defined(__FreeBSD__)
71 while ((pid = wait3(&status, WNOHANG, 0)) > 0)
72 continue; /* swallow 'em up. */
73 #else /* Zooks! Nothing to do but wait(), and hope we don't block... */
79 daemonize (const char *logfile, void (*termhook)(int))
80 /* detach from control TTY, become process group leader, catch SIGCHLD */
84 RETSIGTYPE sigchld_handler();
86 /* if we are started by init (process 1) via /etc/inittab we needn't
87 bother to detach from our process group context */
92 /* Ignore BSD terminal stop signals */
94 signal(SIGTTOU, SIG_IGN);
97 signal(SIGTTIN, SIG_IGN);
100 signal(SIGTSTP, SIG_IGN);
103 /* In case we were not started in the background, fork and let
104 the parent exit. Guarantees that the child is not a process
107 if ((childpid = fork()) < 0) {
108 error(0, errno, "fork");
111 else if (childpid > 0)
112 exit(0); /* parent */
115 /* Make ourselves the leader of a new process group with no
116 controlling terminal */
118 #if defined(HAVE_SETSID) /* POSIX */
119 /* POSIX makes this soooo easy to do */
121 error(0, errno, "setsid");
124 #elif defined(SIGTSTP) /* BSD */
125 /* change process group */
126 setpgrp(0, getpid());
128 /* lose controlling tty */
129 if ((fd = open("/dev/tty", O_RDWR)) >= 0) {
130 ioctl(fd, TIOCNOTTY, (char *) 0);
133 #else /* SVR3 and older */
134 /* change process group */
137 /* lose controlling tty */
138 signal(SIGHUP, SIG_IGN);
139 if ((childpid = fork()) < 0) {
140 error(0, errno, "fork");
143 else if (childpid > 0) {
144 exit(0); /* parent */
150 /* Close any/all open file descriptors */
151 #if defined(HAVE_GETDTABLESIZE)
152 for (fd = getdtablesize()-1; fd >= 0; fd--)
153 #elif defined(NOFILE)
154 for (fd = NOFILE-1; fd >= 0; fd--)
155 #else /* make an educated guess */
156 for (fd = 19; fd >= 0; fd--)
162 /* Reopen stdin descriptor on /dev/null */
163 if ((fd = open("/dev/null", O_RDWR)) < 0) { /* stdin */
164 error(0, errno, "open: /dev/null");
169 fd = open(logfile, O_CREAT|O_WRONLY|O_APPEND, 0666); /* stdout */
171 if (dup(fd) < 0) { /* stdout */
172 error(0, errno, "dup");
175 if (dup(fd) < 0) { /* stderr */
176 error(0, errno, "dup");
180 /* move to root directory, so we don't prevent filesystem unmounts */
183 /* set our umask to something reasonable (we hope) */
184 #if defined(DEF_UMASK)
190 /* set up to catch child process termination signals */
191 signal(SIGCHLD, sigchld_handler);
193 signal(SIGPWR, sigchld_handler);
199 /* daemon.c ends here */