2 * daemon.c -- turn a process into a daemon under POSIX, SYSV, BSD.
4 * For license terms, see the file COPYING in this directory.
13 #include <sys/types.h>
14 #ifdef HAVE_SYS_WAIT_H
19 #else /* !HAVE_FCNTL_H */
20 #ifdef HAVE_SYS_FCNTL_H
21 #include <sys/fcntl.h>
22 #endif /* HAVE_SYS_FCNTL_H */
23 #endif /* !HAVE_FCNTL_H */
24 #include <sys/stat.h> /* get umask(2) prototyped */
26 #if defined(HAVE_UNISTD_H)
30 #if defined(STDC_HEADERS)
38 #if !defined(HAVE_SETSID) && defined(SIGTSTP)
39 #if defined(HAVE_TERMIOS_H)
40 # include <termios.h> /* for TIOCNOTTY under Linux */
43 #if !defined(TIOCNOTTY) && defined(HAVE_SGTTY_H)
44 # include <sgtty.h> /* for TIOCNOTTY under NEXTSTEP */
46 #endif /* !defined(HAVE_SETSID) && defined(SIGTSTP) */
48 /* BSD portability hack */
49 #if !defined(SIGCHLD) && defined(SIGCLD)
50 #define SIGCHLD SIGCLD
53 #include "fetchmail.h"
57 sigchld_handler (int sig)
58 /* process SIGCHLD to obtain the exit code of the terminating process */
60 extern volatile int lastsig; /* last signal received */
63 #if defined(HAVE_WAITPID) /* the POSIX way */
66 while ((pid = waitpid(-1, &status, WNOHANG)) > 0)
67 continue; /* swallow 'em up. */
68 #elif defined(HAVE_WAIT3) /* the BSD way */
69 #if defined(HAVE_UNION_WAIT) && !defined(__FreeBSD__)
75 while ((pid = wait3(&status, WNOHANG, 0)) > 0)
76 continue; /* swallow 'em up. */
77 #else /* Zooks! Nothing to do but wait(), and hope we don't block... */
85 void deal_with_sigchld(void)
87 RETSIGTYPE sigchld_handler(int);
89 struct sigaction sa_new;
91 memset (&sa_new, 0, sizeof sa_new);
92 sigemptyset (&sa_new.sa_mask);
93 sa_new.sa_handler = SIG_IGN;
95 /* set up to catch child process termination signals */
96 sa_new.sa_handler = sigchld_handler;
97 sigaction (SIGCHLD, &sa_new, NULL);
99 sigaction (SIGPWR, &sa_new, NULL);
102 signal(SIGCHLD, sigchld_handler);
104 signal(SIGPWR, sigchld_handler);
106 #endif /* HAVE_SIGACTION */
110 daemonize (const char *logfile, void (*termhook)(int))
111 /* detach from control TTY, become process group leader, catch SIGCHLD */
115 #ifdef HAVE_SIGACTION
116 struct sigaction sa_new;
117 #endif /* HAVE_SIGACTION */
119 /* if we are started by init (process 1) via /etc/inittab we needn't
120 bother to detach from our process group context */
125 /* Ignore BSD terminal stop signals */
126 #ifdef HAVE_SIGACTION
127 memset (&sa_new, 0, sizeof sa_new);
128 sigemptyset (&sa_new.sa_mask);
129 sa_new.sa_handler = SIG_IGN;
130 #endif /* HAVE_SIGACTION */
132 #ifndef HAVE_SIGACTION
133 signal(SIGTTOU, SIG_IGN);
135 sigaction (SIGTTOU, &sa_new, NULL);
136 #endif /* HAVE_SIGACTION */
139 #ifndef HAVE_SIGACTION
140 signal(SIGTTIN, SIG_IGN);
142 sigaction (SIGTTIN, &sa_new, NULL);
143 #endif /* HAVE_SIGACTION */
146 #ifndef HAVE_SIGACTION
147 signal(SIGTSTP, SIG_IGN);
149 sigaction (SIGTSTP, &sa_new, NULL);
150 #endif /* HAVE_SIGACTION */
153 /* In case we were not started in the background, fork and let
154 the parent exit. Guarantees that the child is not a process
157 if ((childpid = fork()) < 0) {
158 report(stderr, "fork (%s)\n", strerror(errno));
161 else if (childpid > 0)
162 exit(0); /* parent */
165 /* Make ourselves the leader of a new process group with no
166 controlling terminal */
168 #if defined(HAVE_SETSID) /* POSIX */
169 /* POSIX makes this soooo easy to do */
171 report(stderr, "setsid (%s)\n", strerror(errno));
174 #elif defined(SIGTSTP) /* BSD */
175 /* change process group */
177 setpgrp(0, getpid());
179 /* lose controlling tty */
180 if ((fd = open("/dev/tty", O_RDWR)) >= 0) {
181 ioctl(fd, TIOCNOTTY, (char *) 0);
182 close(fd); /* not checking should be safe, there were no writes */
184 #else /* SVR3 and older */
185 /* change process group */
190 /* lose controlling tty */
191 #ifndef HAVE_SIGACTION
192 signal(SIGHUP, SIG_IGN);
194 sigaction (SIGHUP, &sa_new, NULL);
195 #endif /* HAVE_SIGACTION */
196 if ((childpid = fork()) < 0) {
197 report(stderr, "fork (%)\n", strerror(errno));
200 else if (childpid > 0) {
201 exit(0); /* parent */
207 /* Close any/all open file descriptors */
208 #if defined(HAVE_GETDTABLESIZE)
209 for (fd = getdtablesize()-1; fd >= 0; fd--)
210 #elif defined(NOFILE)
211 for (fd = NOFILE-1; fd >= 0; fd--)
212 #else /* make an educated guess */
213 for (fd = 19; fd >= 0; fd--)
216 close(fd); /* not checking this should be safe, no writes */
219 /* Reopen stdin descriptor on /dev/null */
220 if ((fd = open("/dev/null", O_RDWR)) < 0) { /* stdin */
221 report(stderr, "open: /dev/null (%s)\n", strerror(errno));
226 fd = open(logfile, O_CREAT|O_WRONLY|O_APPEND, 0666); /* stdout */
228 if (dup(fd) < 0) { /* stdout */
229 report(stderr, "dup (%s)\n", strerror(errno));
232 if (dup(fd) < 0) { /* stderr */
233 report(stderr, "dup (%s)\n", strerror(errno));
237 /* move to root directory, so we don't prevent filesystem unmounts */
240 /* set our umask to something reasonable (we hope) */
241 #if defined(DEF_UMASK)
253 /* is the given fd attached to a file? (used to control logging) */
258 * We'd like just to return 1 on (S_IFREG | S_IFBLK),
259 * but weirdly enough, Linux ptys seem to have S_IFBLK
260 * so this test would fail when run on an xterm.
262 if (isatty(fd) || fstat(fd, &stbuf))
264 else if (stbuf.st_mode & (S_IFREG))
269 /* daemon.c ends here */