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