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_TERMIOS_H)
36 # include <termios.h> /* for TIOCNOTTY under Linux */
39 #if !defined(TIOCNOTTY) && defined(HAVE_SGTTY_H)
40 # include <sgtty.h> /* for TIOCNOTTY under NEXTSTEP */
43 /* BSD portability hack */
44 #if !defined(SIGCHLD) && defined(SIGCLD)
45 #define SIGCHLD SIGCLD
48 #include "fetchmail.h"
52 sigchld_handler (int sig)
53 /* process SIGCHLD to obtain the exit code of the terminating process */
57 #if defined(HAVE_UNION_WAIT)
63 #if defined(HAVE_WAIT3)
64 #ifdef oldhpux /* HP-UX fixed this sometime between 9.01 and 10.20 */
65 while ((pid = wait3(&status, WNOHANG, (int *) 0)) > 0)
67 while ((pid = wait3(&status, WNOHANG, (struct rusage *) 0)) > 0)
69 ; /* swallow 'em up. */
70 #elif defined(HAVE_WAITPID)
71 while ((pid = waitpid(-1, &status, WNOHANG)) > 0)
72 ; /* swallow 'em up. */
73 #else /* Zooks! Nothing to do but wait(), and hope we don't block... */
80 daemonize (const char *logfile, void (*termhook)(int))
81 /* detach from control TTY, become process group leader, catch SIGCHLD */
85 RETSIGTYPE sigchld_handler();
87 /* if we are started by init (process 1) via /etc/inittab we needn't
88 bother to detach from our process group context */
93 /* Ignore BSD terminal stop signals */
95 signal(SIGTTOU, SIG_IGN);
98 signal(SIGTTIN, SIG_IGN);
101 signal(SIGTSTP, SIG_IGN);
104 /* In case we were not started in the background, fork and let
105 the parent exit. Guarantees that the child is not a process
108 if ((childpid = fork()) < 0) {
109 error(0, errno, "fork");
112 else if (childpid > 0)
113 exit(0); /* parent */
116 /* Make ourselves the leader of a new process group with no
117 controlling terminal */
119 #if defined(HAVE_SETSID) /* POSIX */
120 /* POSIX makes this soooo easy to do */
122 error(0, errno, "setsid");
125 #elif defined(SIGTSTP) /* BSD */
126 /* change process group */
127 setpgrp(0, getpid());
129 /* lose controlling tty */
130 if ((fd = open("/dev/tty", O_RDWR)) >= 0) {
131 ioctl(fd, TIOCNOTTY, (char *) 0);
134 #else /* SVR3 and older */
135 /* change process group */
138 /* lose controlling tty */
139 signal(SIGHUP, SIG_IGN);
140 if ((childpid = fork()) < 0) {
141 error(0, errno, "fork");
144 else if (childpid > 0) {
145 exit(0); /* parent */
151 /* Close any/all open file descriptors */
152 #if defined(HAVE_GETDTABLESIZE)
153 for (fd = getdtablesize()-1; fd >= 0; fd--)
154 #elif defined(NOFILE)
155 for (fd = NOFILE-1; fd >= 0; fd--)
156 #else /* make an educated guess */
157 for (fd = 19; fd >= 0; fd--)
163 /* Reopen stdin descriptor on /dev/null */
164 if ((fd = open("/dev/null", O_RDWR)) < 0) { /* stdin */
165 error(0, errno, "open: /dev/null");
170 fd = open(logfile, O_CREAT|O_WRONLY|O_APPEND, 0666); /* stdout */
172 if (dup(fd) < 0) { /* stdout */
173 error(0, errno, "dup");
176 if (dup(fd) < 0) { /* stderr */
177 error(0, errno, "dup");
181 /* move to root directory, so we don't prevent filesystem unmounts */
184 /* set our umask to something reasonable (we hope) */
185 #if defined(DEF_UMASK)
191 /* set up to catch child process termination signals */
192 signal(SIGCHLD, sigchld_handler);
194 signal(SIGPWR, sigchld_handler);
200 /* daemon.c ends here */