chiark / gitweb /
update plumbing diagram
[disorder] / server / daemonize.c
CommitLineData
460b9539 1/*
2 * This file is part of DisOrder.
3 * Copyright (C) 2004, 2005 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
21#include <config.h>
22
23#include <fcntl.h>
24#include <unistd.h>
25#include <stdio.h>
26#include <errno.h>
27#include <sys/wait.h>
28#include <syslog.h>
29
30#include "daemonize.h"
31#include "syscalls.h"
32#include "log.h"
33
34void daemonize(const char *tag, int fac, const char *pidfile) {
35 pid_t pid, r;
36 int w, dn;
37 FILE *fp;
38
39 D(("daemonize tag=%s fac=%d pidfile=%s",
40 tag ? tag : "NULL", fac, pidfile ? pidfile : "NULL"));
41 /* make sure that FDs 0, 1, 2 all at least exist (and get a
42 * /dev/null) */
43 do {
44 if((dn = open("/dev/null", O_RDWR, 0)) < 0)
45 fatal(errno, "error opening /dev/null");
46 } while(dn < 3);
47 pid = xfork();
48 if(pid) {
49 /* Parent process. Wait for the first child to finish, then
50 * return to the caller. */
51 exitfn = _exit;
52 while((r = waitpid(pid, &w, 0)) == -1 && errno == EINTR)
53 ;
54 if(r < 0) fatal(errno, "error calling waitpid");
55 if(w) error(0, "subprocess exited with wait status %#x", (unsigned)w);
56 _exit(0);
57 }
58 /* First child process. This will be the session leader, and will
59 * be transient. */
60 D(("first child pid=%lu", (unsigned long)getpid()));
61 if(setsid() < 0) fatal(errno, "error calling setsid");
62 /* we'll log to syslog */
63 openlog(tag, LOG_PID, fac);
64 log_default = &log_syslog;
65 /* stdin/out/err we lose */
66 xdup2(dn, 0);
67 xdup2(dn, 1);
68 xdup2(dn, 2);
69 xclose(dn);
70 pid = xfork();
71 if(pid)
72 _exit(0);
73 /* second child. Write a pidfile if someone wanted it. */
74 D(("second child pid=%lu", (unsigned long)getpid()));
75 if(pidfile) {
76 if(!(fp = fopen(pidfile, "w"))
77 || fprintf(fp, "%lu\n", (unsigned long)getpid()) < 0
78 || fclose(fp) < 0)
79 fatal(errno, "error creating %s", pidfile);
80 }
81}
82
83/*
84Local Variables:
85c-basic-offset:2
86comment-column:40
87End:
88*/