]> Pileus Git - ~andy/fetchmail/blob - daemon.c
Fix patches from HMH and others.
[~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 <string.h>
13 #include <sys/types.h>
14 #ifdef HAVE_SYS_WAIT_H
15 #include <sys/wait.h>
16 #endif
17 #ifdef HAVE_FCNTL_H
18 #include <fcntl.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 */
25
26 #if defined(HAVE_UNISTD_H)
27 #include <unistd.h>
28 #endif
29
30 #if defined(STDC_HEADERS)
31 #include <stdlib.h>
32 #endif
33
34 #if defined(QNX)
35 #include <unix.h>
36 #endif
37
38 #if !defined(HAVE_SETSID) && defined(SIGTSTP)
39 #if defined(HAVE_TERMIOS_H)
40 #  include <termios.h>          /* for TIOCNOTTY under Linux */
41 #endif
42
43 #if !defined(TIOCNOTTY) && defined(HAVE_SGTTY_H)
44 #  include <sgtty.h>            /* for TIOCNOTTY under NEXTSTEP */
45 #endif
46 #endif /* !defined(HAVE_SETSID) && defined(SIGTSTP) */
47
48 /* BSD portability hack */
49 #if !defined(SIGCHLD) && defined(SIGCLD)
50 #define SIGCHLD SIGCLD
51 #endif
52
53 #include "fetchmail.h"
54 #include "tunable.h"
55
56 RETSIGTYPE
57 sigchld_handler (int sig)
58 /* process SIGCHLD to obtain the exit code of the terminating process */
59 {
60     extern volatile int lastsig;                /* last signal received */
61     pid_t pid;
62
63 #if     defined(HAVE_WAITPID)                           /* the POSIX way */
64     int status;
65
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__)
70     union wait status;
71 #else
72     int status;
73 #endif
74
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... */
78     int status;
79
80     wait(&status);
81 #endif
82     lastsig = SIGCHLD;
83 }
84
85 /* 
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.
89  */
90 void deal_with_sigchld(void)
91 {
92   RETSIGTYPE sigchld_handler(int);
93 #ifdef HAVE_SIGACTION
94   struct sigaction sa_new;
95
96   memset (&sa_new, 0, sizeof sa_new);
97   sigemptyset (&sa_new.sa_mask);
98   /* sa_new.sa_handler = SIG_IGN;     pointless */
99
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;
104 #endif
105   sigaction (SIGCHLD, &sa_new, NULL);
106 #if defined(SIGPWR)
107   sigaction (SIGPWR, &sa_new, NULL);
108 #endif
109 #else
110   signal(SIGCHLD, sigchld_handler); 
111 #if defined(SIGPWR)
112   signal(SIGPWR, sigchld_handler); 
113 #endif
114 #endif /* HAVE_SIGACTION */
115 }
116
117 int
118 daemonize (const char *logfile, void (*termhook)(int))
119 /* detach from control TTY, become process group leader, catch SIGCHLD */
120 {
121   int fd;
122   pid_t childpid;
123 #ifdef HAVE_SIGACTION
124   struct sigaction sa_new;
125 #endif /* HAVE_SIGACTION */
126
127   /* if we are started by init (process 1) via /etc/inittab we needn't 
128      bother to detach from our process group context */
129
130   if (getppid() == 1) 
131     goto nottyDetach;
132
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;
140 #endif
141 #endif /* HAVE_SIGACTION */
142 #ifdef  SIGTTOU
143 #ifndef HAVE_SIGACTION
144   signal(SIGTTOU, SIG_IGN);
145 #else
146   sigaction (SIGTTOU, &sa_new, NULL);
147 #endif /* HAVE_SIGACTION */
148 #endif
149 #ifdef  SIGTTIN
150 #ifndef HAVE_SIGACTION
151   signal(SIGTTIN, SIG_IGN);
152 #else
153   sigaction (SIGTTIN, &sa_new, NULL);
154 #endif /* HAVE_SIGACTION */
155 #endif
156 #ifdef  SIGTSTP
157 #ifndef HAVE_SIGACTION
158   signal(SIGTSTP, SIG_IGN);
159 #else
160   sigaction (SIGTSTP, &sa_new, NULL);
161 #endif /* HAVE_SIGACTION */
162 #endif
163
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
166      group leader */
167
168   if ((childpid = fork()) < 0) {
169     report(stderr, "fork (%s)\n", strerror(errno));
170     return(PS_IOERR);
171   }
172   else if (childpid > 0) 
173     exit(0);  /* parent */
174
175   
176   /* Make ourselves the leader of a new process group with no
177      controlling terminal */
178
179 #if     defined(HAVE_SETSID)            /* POSIX */
180   /* POSIX makes this soooo easy to do */
181   if (setsid() < 0) {
182     report(stderr, "setsid (%s)\n", strerror(errno));
183     return(PS_IOERR);
184   }
185 #elif   defined(SIGTSTP)                /* BSD */
186   /* change process group */
187 #ifndef __EMX__
188   setpgrp(0, getpid());
189 #endif
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 */
194   }
195 #else                                   /* SVR3 and older */
196   /* change process group */
197 #ifndef __EMX__
198   setpgrp();
199 #endif
200   
201   /* lose controlling tty */
202 #ifndef HAVE_SIGACTION
203   signal(SIGHUP, SIG_IGN);
204 #else
205   sigaction (SIGHUP, &sa_new, NULL);
206 #endif /* HAVE_SIGACTION */
207   if ((childpid = fork()) < 0) {
208     report(stderr, "fork (%)\n", strerror(errno));
209     return(PS_IOERR);
210   }
211   else if (childpid > 0) {
212     exit(0);    /* parent */
213   }
214 #endif
215
216 nottyDetach:
217
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--)
225 #endif
226   {
227     close(fd);  /* not checking this should be safe, no writes */
228   }
229
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));
233     return(PS_IOERR);
234   }
235
236   if (logfile)
237     fd = open(logfile, O_CREAT|O_WRONLY|O_APPEND, 0666);        /* stdout */
238   else
239     if (dup(fd) < 0) {                          /* stdout */
240       report(stderr, "dup (%s)\n", strerror(errno));
241       return(PS_IOERR);
242     }
243   if (dup(fd) < 0) {                            /* stderr */
244     report(stderr, "dup (%s)\n", strerror(errno));
245     return(PS_IOERR);
246   }
247
248   /* move to root directory, so we don't prevent filesystem unmounts */
249   chdir("/");
250
251   /* set our umask to something reasonable (we hope) */
252 #if defined(DEF_UMASK)
253   umask(DEF_UMASK);
254 #else
255   umask(022);
256 #endif
257
258   deal_with_sigchld();
259
260   return(0);
261 }
262
263 flag isafile(int fd)
264 /* is the given fd attached to a file? (used to control logging) */
265 {
266     struct stat stbuf;
267
268     /*
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.
272      */
273     if (isatty(fd) || fstat(fd, &stbuf))
274         return(0);
275     else if (stbuf.st_mode & (S_IFREG))
276         return(1);
277     return(0);
278 }
279
280 /* daemon.c ends here */