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 */
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... */
84 RETSIGTYPE null_signal_handler(int sig) { }
86 SIGHANDLERTYPE set_signal_handler(int sig, SIGHANDLERTYPE handler)
88 * This function is called by other parts of the program to
89 * setup the signal handler after a change to the signal context.
90 * This is done to improve robustness of the signal handling code.
91 * It has the same prototype as signal(2).
94 SIGHANDLERTYPE rethandler;
96 struct sigaction sa_new, sa_old;
98 memset (&sa_new, 0, sizeof sa_new);
99 sigemptyset (&sa_new.sa_mask);
100 sa_new.sa_handler = handler;
102 #ifdef SA_RESTART /* SunOS 4.1 portability hack */
103 /* system call should restart on all signals except SIGALRM */
105 sa_new.sa_flags |= SA_RESTART;
107 #ifdef SA_NOCLDSTOP /* SunOS 4.1 portability hack */
109 sa_new.sa_flags |= SA_NOCLDSTOP;
111 sigaction(sig, &sa_new, &sa_old);
112 rethandler = sa_old.sa_handler;
115 sigaction(SIGPWR, &sa_new, NULL);
117 #else /* HAVE_SIGACTION */
118 rethandler = signal(sig, handler);
121 signal(SIGPWR, handler);
123 /* system call should restart on all signals except SIGALRM */
124 siginterrupt(sig, sig == SIGALRM);
125 #endif /* HAVE_SIGACTION */
129 void deal_with_sigchld(void)
131 set_signal_handler(SIGCHLD, sigchld_handler);
135 daemonize (const char *logfile, void (*termhook)(int))
136 /* detach from control TTY, become process group leader, catch SIGCHLD */
141 /* if we are started by init (process 1) via /etc/inittab we needn't
142 bother to detach from our process group context */
147 /* Ignore BSD terminal stop signals */
149 set_signal_handler(SIGTTOU, SIG_IGN);
152 set_signal_handler(SIGTTIN, SIG_IGN);
155 set_signal_handler(SIGTSTP, SIG_IGN);
158 /* In case we were not started in the background, fork and let
159 the parent exit. Guarantees that the child is not a process
162 if ((childpid = fork()) < 0) {
163 report(stderr, "fork (%s)\n", strerror(errno));
166 else if (childpid > 0)
167 exit(0); /* parent */
170 /* Make ourselves the leader of a new process group with no
171 controlling terminal */
173 #if defined(HAVE_SETSID) /* POSIX */
174 /* POSIX makes this soooo easy to do */
176 report(stderr, "setsid (%s)\n", strerror(errno));
179 #elif defined(SIGTSTP) /* BSD */
180 /* change process group */
182 setpgrp(0, getpid());
184 /* lose controlling tty */
185 if ((fd = open("/dev/tty", O_RDWR)) >= 0) {
186 ioctl(fd, TIOCNOTTY, (char *) 0);
187 close(fd); /* not checking should be safe, there were no writes */
189 #else /* SVR3 and older */
190 /* change process group */
195 /* lose controlling tty */
196 set_signal_handler(SIGHUP, SIG_IGN);
197 if ((childpid = fork()) < 0) {
198 report(stderr, "fork (%s)\n", strerror(errno));
201 else if (childpid > 0) {
202 exit(0); /* parent */
208 /* Close any/all open file descriptors */
209 #if defined(HAVE_GETDTABLESIZE)
210 for (fd = getdtablesize()-1; fd >= 0; fd--)
211 #elif defined(NOFILE)
212 for (fd = NOFILE-1; fd >= 0; fd--)
213 #else /* make an educated guess */
214 for (fd = 19; fd >= 0; fd--)
217 close(fd); /* not checking this should be safe, no writes */
220 /* Reopen stdin descriptor on /dev/null */
221 if ((fd = open("/dev/null", O_RDWR)) < 0) { /* stdin */
222 report(stderr, "open: /dev/null (%s)\n", strerror(errno));
228 if ((fd = open(logfile, O_CREAT|O_WRONLY|O_APPEND, 0666)) < 0) { /* stdout */
229 report(stderr, "open %s (%s)\n", logfile, strerror(errno));
235 if (dup(fd) < 0) { /* stdout */
236 report(stderr, "dup (%s)\n", strerror(errno));
240 if (dup(fd) < 0) { /* stderr */
241 report(stderr, "dup (%s)\n", strerror(errno));
246 /* move to root directory, so we don't prevent filesystem unmounts */
250 /* set our umask to something reasonable (we hope) */
251 #if defined(DEF_UMASK)
262 flag is_a_file(int fd)
263 /* is the given fd attached to a file? (used to control logging) */
268 * We'd like just to return 1 on (S_IFREG | S_IFBLK),
269 * but weirdly enough, Linux ptys seem to have S_IFBLK
270 * so this test would fail when run on an xterm.
272 if (isatty(fd) || fstat(fd, &stbuf))
274 else if (stbuf.st_mode & (S_IFREG))
279 /* daemon.c ends here */