chiark / gitweb /
Send clients a rights-changed message when their rights change.
[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 "disorder-server.h"
25
26 /** @brief Go into background
27  * @param tag Message tag, or NULL
28  * @param fac Logging facility
29  * @param pidfile Where to store PID, or NULL
30  *
31  * Become a daemon.  stdout/stderr are lost and DisOrder's logging is
32  * redirected to syslog.  It is assumed that there are no FDs beyond 2
33  * that need closing.
34  */
35 void daemonize(const char *tag, int fac, const char *pidfile) {
36   pid_t pid, r;
37   int w, dn;
38   FILE *fp;
39
40   D(("daemonize tag=%s fac=%d pidfile=%s",
41      tag ? tag : "NULL", fac, pidfile ? pidfile : "NULL"));
42   /* make sure that FDs 0, 1, 2 all at least exist (and get a
43    * /dev/null) */
44   do {
45     if((dn = open("/dev/null", O_RDWR, 0)) < 0)
46       fatal(errno, "error opening /dev/null");
47   } while(dn < 3);
48   pid = xfork();
49   if(pid) {
50     /* Parent process.  Wait for the first child to finish, then
51      * return to the caller. */
52     exitfn = _exit;
53     while((r = waitpid(pid, &w, 0)) == -1 && errno == EINTR)
54       ;
55     if(r < 0) fatal(errno, "error calling waitpid");
56     if(w) error(0, "subprocess exited with wait status %#x", (unsigned)w);
57     _exit(0);
58   }
59   /* First child process.  This will be the session leader, and will
60    * be transient. */
61   D(("first child pid=%lu", (unsigned long)getpid()));
62   if(setsid() < 0) fatal(errno, "error calling setsid");
63   /* we'll log to syslog */
64   openlog(tag, LOG_PID, fac);
65   log_default = &log_syslog;
66   /* stdin/out/err we lose */
67   xdup2(dn, 0);
68   xdup2(dn, 1);
69   xdup2(dn, 2);
70   xclose(dn);
71   pid = xfork();
72   if(pid)
73     _exit(0);
74   /* second child.  Write a pidfile if someone wanted it. */
75   D(("second child pid=%lu", (unsigned long)getpid()));
76   if(pidfile) {
77     if(!(fp = fopen(pidfile, "w"))
78        || fprintf(fp, "%lu\n", (unsigned long)getpid()) < 0
79        || fclose(fp) < 0)
80       fatal(errno, "error creating %s", pidfile);
81   }
82 }
83
84 /*
85 Local Variables:
86 c-basic-offset:2
87 comment-column:40
88 End:
89 */