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 RETSIGTYPE null_signal_handler(int sig) { }
87 SIGHANDLERTYPE set_signal_handler(int sig, SIGHANDLERTYPE handler)
89 * This function is called by other parts of the program to
90 * setup the signal handler after a change to the signal context.
91 * This is done to improve robustness of the signal handling code.
92 * It has the same prototype as signal(2).
95 SIGHANDLERTYPE rethandler;
97 struct sigaction sa_new, sa_old;
99 memset (&sa_new, 0, sizeof sa_new);
100 sigemptyset (&sa_new.sa_mask);
101 sa_new.sa_handler = handler;
103 #ifdef SA_RESTART /* SunOS 4.1 portability hack */
104 /* system call should restart on all signals except SIGALRM */
106 sa_new.sa_flags |= SA_RESTART;
108 #ifdef SA_NOCLDSTOP /* SunOS 4.1 portability hack */
110 sa_new.sa_flags |= SA_NOCLDSTOP;
112 sigaction(sig, &sa_new, &sa_old);
113 rethandler = sa_old.sa_handler;
116 sigaction(SIGPWR, &sa_new, NULL);
118 #else /* HAVE_SIGACTION */
119 rethandler = signal(sig, handler);
122 signal(SIGPWR, handler);
124 /* system call should restart on all signals except SIGALRM */
125 siginterrupt(sig, sig == SIGALRM);
126 #endif /* HAVE_SIGACTION */
130 void deal_with_sigchld(void)
132 set_signal_handler(SIGCHLD, sigchld_handler);
136 daemonize (const char *logfile, void (*termhook)(int))
137 /* detach from control TTY, become process group leader, catch SIGCHLD */
142 /* if we are started by init (process 1) via /etc/inittab we needn't
143 bother to detach from our process group context */
148 /* Ignore BSD terminal stop signals */
150 set_signal_handler(SIGTTOU, SIG_IGN);
153 set_signal_handler(SIGTTIN, SIG_IGN);
156 set_signal_handler(SIGTSTP, SIG_IGN);
159 /* In case we were not started in the background, fork and let
160 the parent exit. Guarantees that the child is not a process
163 if ((childpid = fork()) < 0) {
164 report(stderr, "fork (%s)\n", strerror(errno));
167 else if (childpid > 0)
168 exit(0); /* parent */
171 /* Make ourselves the leader of a new process group with no
172 controlling terminal */
174 #if defined(HAVE_SETSID) /* POSIX */
175 /* POSIX makes this soooo easy to do */
177 report(stderr, "setsid (%s)\n", strerror(errno));
180 #elif defined(SIGTSTP) /* BSD */
181 /* change process group */
183 setpgrp(0, getpid());
185 /* lose controlling tty */
186 if ((fd = open("/dev/tty", O_RDWR)) >= 0) {
187 ioctl(fd, TIOCNOTTY, (char *) 0);
188 close(fd); /* not checking should be safe, there were no writes */
190 #else /* SVR3 and older */
191 /* change process group */
196 /* lose controlling tty */
197 set_signal_handler(SIGHUP, SIG_IGN);
198 if ((childpid = fork()) < 0) {
199 report(stderr, "fork (%s)\n", strerror(errno));
202 else if (childpid > 0) {
203 exit(0); /* parent */
209 /* Close any/all open file descriptors */
210 #if defined(HAVE_GETDTABLESIZE)
211 for (fd = getdtablesize()-1; fd >= 0; fd--)
212 #elif defined(NOFILE)
213 for (fd = NOFILE-1; fd >= 0; fd--)
214 #else /* make an educated guess */
215 for (fd = 19; fd >= 0; fd--)
218 close(fd); /* not checking this should be safe, no writes */
221 /* Reopen stdin descriptor on /dev/null */
222 if ((fd = open("/dev/null", O_RDWR)) < 0) { /* stdin */
223 report(stderr, "open: /dev/null (%s)\n", strerror(errno));
229 if ((fd = open(logfile, O_CREAT|O_WRONLY|O_APPEND, 0666)) < 0) { /* stdout */
230 report(stderr, "open %s (%s)\n", logfile, strerror(errno));
236 if (dup(fd) < 0) { /* stdout */
237 report(stderr, "dup (%s)\n", strerror(errno));
241 if (dup(fd) < 0) { /* stderr */
242 report(stderr, "dup (%s)\n", strerror(errno));
247 /* move to root directory, so we don't prevent filesystem unmounts */
251 /* set our umask to something reasonable (we hope) */
252 #if defined(DEF_UMASK)
264 /* is the given fd attached to a file? (used to control logging) */
269 * We'd like just to return 1 on (S_IFREG | S_IFBLK),
270 * but weirdly enough, Linux ptys seem to have S_IFBLK
271 * so this test would fail when run on an xterm.
273 if (isatty(fd) || fstat(fd, &stbuf))
275 else if (stbuf.st_mode & (S_IFREG))
280 /* daemon.c ends here */