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... */
86 * This function is called by other parts of the program to
87 * setup the sigchld handler after a change to the signal context.
88 * This is done to improve robustness of the signal handling code.
90 void deal_with_sigchld(void)
92 RETSIGTYPE sigchld_handler(int);
94 struct sigaction sa_new;
96 memset (&sa_new, 0, sizeof sa_new);
97 sigemptyset (&sa_new.sa_mask);
98 /* sa_new.sa_handler = SIG_IGN; pointless */
100 /* set up to catch child process termination signals */
101 sa_new.sa_handler = sigchld_handler;
102 #ifdef SA_RESTART /* SunOS 4.1 portability hack */
103 sa_new.sa_flags = SA_RESTART | SA_NOCLDSTOP;
105 sigaction (SIGCHLD, &sa_new, NULL);
107 sigaction (SIGPWR, &sa_new, NULL);
109 #else /* HAVE_SIGACTION */
110 signal(SIGCHLD, sigchld_handler);
112 signal(SIGPWR, sigchld_handler);
114 #endif /* HAVE_SIGACTION */
118 daemonize (const char *logfile, void (*termhook)(int))
119 /* detach from control TTY, become process group leader, catch SIGCHLD */
123 #ifdef HAVE_SIGACTION
124 struct sigaction sa_new;
125 #endif /* HAVE_SIGACTION */
127 /* if we are started by init (process 1) via /etc/inittab we needn't
128 bother to detach from our process group context */
133 /* Ignore BSD terminal stop signals */
134 #ifdef HAVE_SIGACTION
135 memset (&sa_new, 0, sizeof sa_new);
136 sigemptyset (&sa_new.sa_mask);
137 sa_new.sa_handler = SIG_IGN;
138 #ifdef SA_RESTART /* SunOS 4.1 portability hack */
139 sa_new.sa_flags = SA_RESTART;
141 #endif /* HAVE_SIGACTION */
143 #ifndef HAVE_SIGACTION
144 signal(SIGTTOU, SIG_IGN);
146 sigaction (SIGTTOU, &sa_new, NULL);
147 #endif /* HAVE_SIGACTION */
150 #ifndef HAVE_SIGACTION
151 signal(SIGTTIN, SIG_IGN);
153 sigaction (SIGTTIN, &sa_new, NULL);
154 #endif /* HAVE_SIGACTION */
157 #ifndef HAVE_SIGACTION
158 signal(SIGTSTP, SIG_IGN);
160 sigaction (SIGTSTP, &sa_new, NULL);
161 #endif /* HAVE_SIGACTION */
164 /* In case we were not started in the background, fork and let
165 the parent exit. Guarantees that the child is not a process
168 if ((childpid = fork()) < 0) {
169 report(stderr, "fork (%s)\n", strerror(errno));
172 else if (childpid > 0)
173 exit(0); /* parent */
176 /* Make ourselves the leader of a new process group with no
177 controlling terminal */
179 #if defined(HAVE_SETSID) /* POSIX */
180 /* POSIX makes this soooo easy to do */
182 report(stderr, "setsid (%s)\n", strerror(errno));
185 #elif defined(SIGTSTP) /* BSD */
186 /* change process group */
188 setpgrp(0, getpid());
190 /* lose controlling tty */
191 if ((fd = open("/dev/tty", O_RDWR)) >= 0) {
192 ioctl(fd, TIOCNOTTY, (char *) 0);
193 close(fd); /* not checking should be safe, there were no writes */
195 #else /* SVR3 and older */
196 /* change process group */
201 /* lose controlling tty */
202 #ifndef HAVE_SIGACTION
203 signal(SIGHUP, SIG_IGN);
205 sigaction (SIGHUP, &sa_new, NULL);
206 #endif /* HAVE_SIGACTION */
207 if ((childpid = fork()) < 0) {
208 report(stderr, "fork (%s)\n", strerror(errno));
211 else if (childpid > 0) {
212 exit(0); /* parent */
218 /* Close any/all open file descriptors */
219 #if defined(HAVE_GETDTABLESIZE)
220 for (fd = getdtablesize()-1; fd >= 0; fd--)
221 #elif defined(NOFILE)
222 for (fd = NOFILE-1; fd >= 0; fd--)
223 #else /* make an educated guess */
224 for (fd = 19; fd >= 0; fd--)
227 close(fd); /* not checking this should be safe, no writes */
230 /* Reopen stdin descriptor on /dev/null */
231 if ((fd = open("/dev/null", O_RDWR)) < 0) { /* stdin */
232 report(stderr, "open: /dev/null (%s)\n", strerror(errno));
238 if ((fd = open(logfile, O_CREAT|O_WRONLY|O_APPEND, 0666)) < 0) { /* stdout */
239 report(stderr, "open %s (%s)\n", logfile, strerror(errno));
245 if (dup(fd) < 0) { /* stdout */
246 report(stderr, "dup (%s)\n", strerror(errno));
250 if (dup(fd) < 0) { /* stderr */
251 report(stderr, "dup (%s)\n", strerror(errno));
255 /* move to root directory, so we don't prevent filesystem unmounts */
258 /* set our umask to something reasonable (we hope) */
259 #if defined(DEF_UMASK)
271 /* is the given fd attached to a file? (used to control logging) */
276 * We'd like just to return 1 on (S_IFREG | S_IFBLK),
277 * but weirdly enough, Linux ptys seem to have S_IFBLK
278 * so this test would fail when run on an xterm.
280 if (isatty(fd) || fstat(fd, &stbuf))
282 else if (stbuf.st_mode & (S_IFREG))
287 /* daemon.c ends here */