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 */
59 extern volatile int lastsig; /* last signal received */
62 #if defined(HAVE_WAITPID) /* the POSIX way */
65 while ((pid = waitpid(-1, &status, WNOHANG)) > 0)
66 continue; /* swallow 'em up. */
67 #elif defined(HAVE_WAIT3) /* the BSD way */
68 #if defined(HAVE_UNION_WAIT) && !defined(__FreeBSD__)
74 while ((pid = wait3(&status, WNOHANG, 0)) > 0)
75 continue; /* swallow 'em up. */
76 #else /* Zooks! Nothing to do but wait(), and hope we don't block... */
85 daemonize (const char *logfile, void (*termhook)(int))
86 /* detach from control TTY, become process group leader, catch SIGCHLD */
90 RETSIGTYPE sigchld_handler(int);
92 /* if we are started by init (process 1) via /etc/inittab we needn't
93 bother to detach from our process group context */
98 /* Ignore BSD terminal stop signals */
100 signal(SIGTTOU, SIG_IGN);
103 signal(SIGTTIN, SIG_IGN);
106 signal(SIGTSTP, SIG_IGN);
109 /* In case we were not started in the background, fork and let
110 the parent exit. Guarantees that the child is not a process
113 if ((childpid = fork()) < 0) {
114 report(stderr, "fork (%s)\n", strerror(errno));
117 else if (childpid > 0)
118 exit(0); /* parent */
121 /* Make ourselves the leader of a new process group with no
122 controlling terminal */
124 #if defined(HAVE_SETSID) /* POSIX */
125 /* POSIX makes this soooo easy to do */
127 report(stderr, "setsid (%s)\n", strerror(errno));
130 #elif defined(SIGTSTP) /* BSD */
131 /* change process group */
133 setpgrp(0, getpid());
135 /* lose controlling tty */
136 if ((fd = open("/dev/tty", O_RDWR)) >= 0) {
137 ioctl(fd, TIOCNOTTY, (char *) 0);
138 close(fd); /* not checking should be safe, there were no writes */
140 #else /* SVR3 and older */
141 /* change process group */
146 /* lose controlling tty */
147 signal(SIGHUP, SIG_IGN);
148 if ((childpid = fork()) < 0) {
149 report(stderr, "fork (%)\n", strerror(errno));
152 else if (childpid > 0) {
153 exit(0); /* parent */
159 /* Close any/all open file descriptors */
160 #if defined(HAVE_GETDTABLESIZE)
161 for (fd = getdtablesize()-1; fd >= 0; fd--)
162 #elif defined(NOFILE)
163 for (fd = NOFILE-1; fd >= 0; fd--)
164 #else /* make an educated guess */
165 for (fd = 19; fd >= 0; fd--)
168 close(fd); /* not checking this should be safe, no writes */
171 /* Reopen stdin descriptor on /dev/null */
172 if ((fd = open("/dev/null", O_RDWR)) < 0) { /* stdin */
173 report(stderr, "open: /dev/null (%s)\n", strerror(errno));
178 fd = open(logfile, O_CREAT|O_WRONLY|O_APPEND, 0666); /* stdout */
180 if (dup(fd) < 0) { /* stdout */
181 report(stderr, "dup (%s)\n", strerror(errno));
184 if (dup(fd) < 0) { /* stderr */
185 report(stderr, "dup (%s)\n", strerror(errno));
189 /* move to root directory, so we don't prevent filesystem unmounts */
192 /* set our umask to something reasonable (we hope) */
193 #if defined(DEF_UMASK)
199 /* set up to catch child process termination signals */
200 signal(SIGCHLD, sigchld_handler);
202 signal(SIGPWR, sigchld_handler);
209 /* is the given fd attached to a file? (used to control logging) */
213 if (fstat(fd, &stbuf))
215 else if (stbuf.st_mode & (S_IFREG | S_IFBLK))
220 /* daemon.c ends here */