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