]> Pileus Git - ~andy/fetchmail/blob - daemon.c
Improved configuration and compilation.
[~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 #ifdef hpux
54   while ((pid = wait3(&status, WNOHANG, (int *) 0)) > 0)
55 #else
56   while ((pid = wait3(&status, WNOHANG, (struct rusage *) 0)) > 0)
57 #endif
58     ; /* swallow 'em up. */
59 #elif   defined(HAVE_WAITPID)
60   while ((pid = waitpid(-1, &status, WNOHANG)) > 0)
61     ; /* swallow 'em up. */
62 #else   /* Zooks! Nothing to do but wait(), and hope we don't block... */
63   wait(&status);
64 #endif
65
66 }
67
68 int
69 daemonize (const char *logfile, void (*termhook)(int))
70 /* detach from control TTY, become process group leader, catch SIGCHLD */
71 {
72   int fd;
73   pid_t childpid;
74   RETSIGTYPE sigchld_handler();
75
76   /* if we are started by init (process 1) via /etc/inittab we needn't 
77      bother to detach from our process group context */
78
79   if (getppid() == 1) 
80     goto nottyDetach;
81
82   /* Ignore BSD terminal stop signals */
83 #ifdef  SIGTTOU
84   signal(SIGTTOU, SIG_IGN);
85 #endif
86 #ifdef  SIGTTIN
87   signal(SIGTTIN, SIG_IGN);
88 #endif
89 #ifdef  SIGTSTP
90   signal(SIGTSTP, SIG_IGN);
91 #endif
92
93   /* In case we were not started in the background, fork and let
94      the parent exit.  Guarantees that the child is not a process
95      group leader */
96
97   if ((childpid = fork()) < 0) {
98     error(0, errno, "fork");
99     return(PS_IOERR);
100   }
101   else if (childpid > 0) 
102     exit(0);  /* parent */
103
104   
105   /* Make ourselves the leader of a new process group with no
106      controlling terminal */
107
108 #if     defined(HAVE_SETSID)            /* POSIX */
109   /* POSIX makes this soooo easy to do */
110   if (setsid() < 0) {
111     error(0, errno, "setsid");
112     return(PS_IOERR);
113   }
114 #elif   defined(SIGTSTP)                /* BSD */
115   /* change process group */
116   setpgrp(0, getpid());
117
118   /* lose controlling tty */
119   if ((fd = open("/dev/tty", O_RDWR)) >= 0) {
120     ioctl(fd, TIOCNOTTY, (char *) 0);
121     close(fd);
122   }
123 #else                                   /* SVR3 and older */
124   /* change process group */
125   setpgrp();
126   
127   /* lose controlling tty */
128   signal(SIGHUP, SIG_IGN);
129   if ((childpid = fork()) < 0) {
130     error(0, errno, "fork");
131     return(PS_IOERR);
132   }
133   else if (childpid > 0) {
134     exit(0);    /* parent */
135   }
136 #endif
137
138 nottyDetach:
139
140   /* Close any/all open file descriptors */
141 #if     defined(HAVE_GETDTABLESIZE)
142   for (fd = getdtablesize()-1;  fd >= 0;  fd--)
143 #elif   defined(NOFILE)
144   for (fd = NOFILE-1;  fd >= 0;  fd--)
145 #else           /* make an educated guess */
146   for (fd = 19;  fd >= 0;  fd--)
147 #endif
148   {
149     close(fd);
150   }
151
152   /* Reopen stdin descriptor on /dev/null */
153   if ((fd = open("/dev/null", O_RDWR)) < 0) {   /* stdin */
154     error(0, errno, "open: /dev/null");
155     return(PS_IOERR);
156   }
157
158   if (logfile)
159     fd = open(logfile, O_CREAT|O_WRONLY|O_APPEND, 0666);        /* stdout */
160   else
161     if (dup(fd) < 0) {                          /* stdout */
162       error(0, errno, "dup");
163       return(PS_IOERR);
164     }
165   if (dup(fd) < 0) {                            /* stderr */
166     error(0, errno, "dup");
167     return(PS_IOERR);
168   }
169
170   /* move to root directory, so we don't prevent filesystem unmounts */
171   chdir("/");
172
173   /* set our umask to something reasonable (we hope) */
174 #if defined(DEF_UMASK)
175   umask(DEF_UMASK);
176 #else
177   umask(022);
178 #endif
179
180   /* set up to catch child process termination signals */ 
181   signal(SIGCHLD, sigchld_handler); 
182 #if defined(SIGPWR)
183   signal(SIGPWR, sigchld_handler); 
184 #endif
185
186   return(0);
187 }
188
189 /* daemon.c ends here */