]> Pileus Git - ~andy/fetchmail/blob - daemon.c
8e270237d00359c4f907cdfbcc0b286f6eab29d7
[~andy/fetchmail] / daemon.c
1 /*
2  * daemon.c -- turn a process into a daemon under POSIX, SYSV, BSD.
3  *
4  * For license terms, see the file COPYING in this directory.
5  */
6
7 #include <config.h>
8
9 #include <stdio.h>
10 #include <errno.h>
11 #include <signal.h>
12 #include <sys/types.h>
13 #include <sys/wait.h>
14 #include <sys/fcntl.h>  /* Solaris 2.5 requires this */
15
16 #if defined(HAVE_UNISTD_H)
17 #include <unistd.h>
18 #endif
19
20 #if defined(STDC_HEADERS)
21 #include <stdlib.h>
22 #endif
23
24 #if defined(QNX)
25 #include <unix.h>
26 #endif
27
28 #if defined(HAVE_SGTTY_H)
29 #  include <sgtty.h>            /* for TIOCNOTTY under NEXTSTEP */
30 #endif
31
32 /* BSD portability hack */
33 #if !defined(SIGCHLD) && defined(SIGCLD)
34 #define SIGCHLD SIGCLD
35 #endif
36
37 #include "fetchmail.h"
38 #include "tunable.h"
39
40 RETSIGTYPE
41 sigchld_handler (int sig)
42 /* process SIGCHLD to obtain the exit code of the terminating process */
43 {
44   pid_t pid;
45
46 #if defined(HAVE_UNION_WAIT)
47   union wait status;
48 #else
49   int status;
50 #endif
51
52 #if     defined(HAVE_WAIT3)
53   while ((pid = wait3(&status, WNOHANG, (struct rusage *) 0)) > 0)
54     ; /* swallow 'em up. */
55 #elif   defined(HAVE_WAITPID)
56   while ((pid = waitpid(-1, &status, WNOHANG)) > 0)
57     ; /* swallow 'em up. */
58 #else   /* Zooks! Nothing to do but wait(), and hope we don't block... */
59   wait(&status);
60 #endif
61
62 }
63
64 int
65 daemonize (const char *logfile, void (*termhook)(int))
66 /* detach from control TTY, become process group leader, catch SIGCHLD */
67 {
68   int fd;
69   pid_t childpid;
70   RETSIGTYPE sigchld_handler();
71
72   /* if we are started by init (process 1) via /etc/inittab we needn't 
73      bother to detach from our process group context */
74
75   if (getppid() == 1) 
76     goto nottyDetach;
77
78   /* Ignore BSD terminal stop signals */
79 #ifdef  SIGTTOU
80   signal(SIGTTOU, SIG_IGN);
81 #endif
82 #ifdef  SIGTTIN
83   signal(SIGTTIN, SIG_IGN);
84 #endif
85 #ifdef  SIGTSTP
86   signal(SIGTSTP, SIG_IGN);
87 #endif
88
89   /* In case we were not started in the background, fork and let
90      the parent exit.  Guarantees that the child is not a process
91      group leader */
92
93   if ((childpid = fork()) < 0) {
94     error(0, errno, "fork");
95     return(PS_IOERR);
96   }
97   else if (childpid > 0) 
98     exit(0);  /* parent */
99
100   
101   /* Make ourselves the leader of a new process group with no
102      controlling terminal */
103
104 #if     defined(HAVE_SETSID)            /* POSIX */
105   /* POSIX makes this soooo easy to do */
106   if (setsid() < 0) {
107     error(0, errno, "setsid");
108     return(PS_IOERR);
109   }
110 #elif   defined(SIGTSTP)                /* BSD */
111   /* change process group */
112   setpgrp(0, getpid());
113
114   /* lose controlling tty */
115   if ((fd = open("/dev/tty", O_RDWR)) >= 0) {
116     ioctl(fd, TIOCNOTTY, (char *) 0);
117     close(fd);
118   }
119 #else                                   /* SVR3 and older */
120   /* change process group */
121   setpgrp();
122   
123   /* lose controlling tty */
124   signal(SIGHUP, SIG_IGN);
125   if ((childpid = fork()) < 0) {
126     error(0, errno, "fork");
127     return(PS_IOERR);
128   }
129   else if (childpid > 0) {
130     exit(0);    /* parent */
131   }
132 #endif
133
134 nottyDetach:
135
136   /* Close any/all open file descriptors */
137 #if     defined(HAVE_GETDTABLESIZE)
138   for (fd = getdtablesize()-1;  fd >= 0;  fd--)
139 #elif   defined(NOFILE)
140   for (fd = NOFILE-1;  fd >= 0;  fd--)
141 #else           /* make an educated guess */
142   for (fd = 19;  fd >= 0;  fd--)
143 #endif
144   {
145     close(fd);
146   }
147
148   /* Reopen stdin descriptor on /dev/null */
149   if ((fd = open("/dev/null", O_RDWR)) < 0) {   /* stdin */
150     error(0, errno, "open: /dev/null");
151     return(PS_IOERR);
152   }
153
154   if (logfile)
155     fd = open(logfile, O_CREAT|O_WRONLY|O_APPEND, 0666);        /* stdout */
156   else
157     if (dup(fd) < 0) {                          /* stdout */
158       error(0, errno, "dup");
159       return(PS_IOERR);
160     }
161   if (dup(fd) < 0) {                            /* stderr */
162     error(0, errno, "dup");
163     return(PS_IOERR);
164   }
165
166   /* move to root directory, so we don't prevent filesystem unmounts */
167   chdir("/");
168
169   /* set our umask to something reasonable (we hope) */
170 #if defined(DEF_UMASK)
171   umask(DEF_UMASK);
172 #else
173   umask(022);
174 #endif
175
176   /* set up to catch child process termination signals */ 
177   signal(SIGCHLD, sigchld_handler); 
178 #if defined(SIGPWR)
179   signal(SIGPWR, sigchld_handler); 
180 #endif
181
182   return(0);
183 }
184
185 /* daemon.c ends here */