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>
16 #include <sys/stat.h> /* get umask(2) prototyped */
22 #include <termios.h> /* for TIOCNOTTY under Linux */
24 /* BSD portability hack */
25 #if !defined(SIGCHLD) && defined(SIGCLD)
26 #define SIGCHLD SIGCLD
29 #include "fetchmail.h"
33 sigchld_handler (int sig)
34 /* process SIGCHLD to obtain the exit code of the terminating process */
36 #if defined(HAVE_WAITPID) /* the POSIX way */
39 while (waitpid(-1, &status, WNOHANG) > 0)
40 continue; /* swallow 'em up. */
41 #elif defined(HAVE_WAIT3) /* the BSD way */
43 #if defined(HAVE_UNION_WAIT) && !defined(__FreeBSD__)
49 while ((pid = wait3(&status, WNOHANG, 0)) > 0)
50 continue; /* swallow 'em up. */
51 #else /* Zooks! Nothing to do but wait(), and hope we don't block... */
60 void null_signal_handler(int sig) { (void)sig; }
62 SIGHANDLERTYPE set_signal_handler(int sig, SIGHANDLERTYPE handler)
64 * This function is called by other parts of the program to
65 * setup the signal handler after a change to the signal context.
66 * This is done to improve robustness of the signal handling code.
67 * It has the same prototype as signal(2).
70 SIGHANDLERTYPE rethandler;
71 struct sigaction sa_new, sa_old;
73 memset (&sa_new, 0, sizeof sa_new);
74 sigemptyset (&sa_new.sa_mask);
75 sa_new.sa_handler = handler;
77 /* system call should restart on all signals except SIGALRM */
79 sa_new.sa_flags |= SA_RESTART;
81 sa_new.sa_flags |= SA_NOCLDSTOP;
82 sigaction(sig, &sa_new, &sa_old);
83 rethandler = sa_old.sa_handler;
86 sigaction(SIGPWR, &sa_new, NULL);
91 void deal_with_sigchld(void)
93 set_signal_handler(SIGCHLD, sigchld_handler);
97 daemonize (const char *logfile)
98 /* detach from control TTY, become process group leader, catch SIGCHLD */
103 /* if we are started by init (process 1) via /etc/inittab we needn't
104 bother to detach from our process group context */
109 /* Ignore BSD terminal stop signals */
111 set_signal_handler(SIGTTOU, SIG_IGN);
114 set_signal_handler(SIGTTIN, SIG_IGN);
117 set_signal_handler(SIGTSTP, SIG_IGN);
120 /* In case we were not started in the background, fork and let
121 the parent exit. Guarantees that the child is not a process
124 if ((childpid = fork()) < 0) {
125 report(stderr, "fork (%s)\n", strerror(errno));
128 else if (childpid > 0)
129 exit(0); /* parent */
132 /* Make ourselves the leader of a new process group with no
133 controlling terminal */
135 /* POSIX makes this soooo easy to do */
137 report(stderr, "setsid (%s)\n", strerror(errno));
145 /* Reopen stdin descriptor on /dev/null */
146 if (open("/dev/null", O_RDWR) < 0) { /* stdin */
147 report(stderr, "cannot open /dev/null: %s\n", strerror(errno));
153 if ((logfd = open(logfile, O_CREAT|O_WRONLY|O_APPEND, 0666)) < 0) { /* stdout */
154 report(stderr, "cannot open %s: %s\n", logfile, strerror(errno));
158 logfd = 0; /* else use /dev/null */
160 /* Close any/all open file descriptors */
161 #if defined(HAVE_GETDTABLESIZE)
162 fd = getdtablesize() - 1;
163 #elif defined(NOFILE)
165 #else /* make an educated guess */
170 close(fd); /* not checking this should be safe, no writes */
174 if (dup(logfd) < 0 /* stdout */
175 || ((logfd == 0 || logfd >= 3) && dup(logfd) < 0)) { /* stderr */
176 report(stderr, "dup (%s)\n", strerror(errno));
181 /* move to root directory, so we don't prevent filesystem unmounts */
185 /* set our umask to something reasonable (we hope) */
186 #if defined(DEF_UMASK)
197 flag is_a_file(int fd)
198 /* is the given fd attached to a file? (used to control logging) */
203 * We'd like just to return 1 on (S_IFREG | S_IFBLK),
204 * but weirdly enough, Linux ptys seem to have S_IFBLK
205 * so this test would fail when run on an xterm.
207 if (isatty(fd) || fstat(fd, &stbuf))
209 else if (stbuf.st_mode & (S_IFREG))
214 /* daemon.c ends here */