chiark / gitweb /
DisOrder 3.0
[disorder] / server / daemonize.c
1 /*
2  * This file is part of DisOrder.
3  * Copyright (C) 2004, 2005, 2007, 2008 Richard Kettlewell
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18  * USA
19  */
20 /** @file server/daemonize.c
21  * @brief Go into background
22  */
23
24 #include <config.h>
25
26 #include <fcntl.h>
27 #include <unistd.h>
28 #include <stdio.h>
29 #include <errno.h>
30 #include <sys/wait.h>
31 #include <syslog.h>
32
33 #include "daemonize.h"
34 #include "syscalls.h"
35 #include "log.h"
36
37 /** @brief Go into background
38  * @param tag Message tag, or NULL
39  * @param fac Logging facility
40  * @param pidfile Where to store PID, or NULL
41  *
42  * Become a daemon.  stdout/stderr are lost and DisOrder's logging is
43  * redirected to syslog.  It is assumed that there are no FDs beyond 2
44  * that need closing.
45  */
46 void daemonize(const char *tag, int fac, const char *pidfile) {
47   pid_t pid, r;
48   int w, dn;
49   FILE *fp;
50
51   D(("daemonize tag=%s fac=%d pidfile=%s",
52      tag ? tag : "NULL", fac, pidfile ? pidfile : "NULL"));
53   /* make sure that FDs 0, 1, 2 all at least exist (and get a
54    * /dev/null) */
55   do {
56     if((dn = open("/dev/null", O_RDWR, 0)) < 0)
57       fatal(errno, "error opening /dev/null");
58   } while(dn < 3);
59   pid = xfork();
60   if(pid) {
61     /* Parent process.  Wait for the first child to finish, then
62      * return to the caller. */
63     exitfn = _exit;
64     while((r = waitpid(pid, &w, 0)) == -1 && errno == EINTR)
65       ;
66     if(r < 0) fatal(errno, "error calling waitpid");
67     if(w) error(0, "subprocess exited with wait status %#x", (unsigned)w);
68     _exit(0);
69   }
70   /* First child process.  This will be the session leader, and will
71    * be transient. */
72   D(("first child pid=%lu", (unsigned long)getpid()));
73   if(setsid() < 0) fatal(errno, "error calling setsid");
74   /* we'll log to syslog */
75   openlog(tag, LOG_PID, fac);
76   log_default = &log_syslog;
77   /* stdin/out/err we lose */
78   xdup2(dn, 0);
79   xdup2(dn, 1);
80   xdup2(dn, 2);
81   xclose(dn);
82   pid = xfork();
83   if(pid)
84     _exit(0);
85   /* second child.  Write a pidfile if someone wanted it. */
86   D(("second child pid=%lu", (unsigned long)getpid()));
87   if(pidfile) {
88     if(!(fp = fopen(pidfile, "w"))
89        || fprintf(fp, "%lu\n", (unsigned long)getpid()) < 0
90        || fclose(fp) < 0)
91       fatal(errno, "error creating %s", pidfile);
92   }
93 }
94
95 /*
96 Local Variables:
97 c-basic-offset:2
98 comment-column:40
99 End:
100 */