]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/daemon.c
Switch from GPLv2 to AGPLv3
[bacula/bacula] / bacula / src / lib / daemon.c
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2000-2008 Free Software Foundation Europe e.V.
5
6    The main author of Bacula is Kern Sibbald, with contributions from
7    many others, a complete list can be found in the file AUTHORS.
8    This program is Free Software; you can redistribute it and/or
9    modify it under the terms of version three of the GNU Affero General Public
10    License as published by the Free Software Foundation and included
11    in the file LICENSE.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU Affero General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23    Bacula® is a registered trademark of Kern Sibbald.
24    The licensor of Bacula is the Free Software Foundation Europe
25    (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
26    Switzerland, email:ftf@fsfeurope.org.
27 */
28 /*
29  *  daemon.c by Kern Sibbald 2000
30  *
31  *   Version $Id$
32  *
33  *   This code is inspired by the Prentice Hall book
34  *   "Unix Network Programming" by W. Richard Stevens
35  *   and later updated from his book "Advanced Programming
36  *   in the UNIX Environment"
37  *
38  * Initialize a daemon process completely detaching us from
39  * any terminal processes.
40  *
41  */
42
43
44 #include "bacula.h"
45 extern int debug_level;
46
47 void
48 daemon_start()
49 {
50 #if !defined(HAVE_WIN32)
51    int i;
52    pid_t cpid;
53    mode_t oldmask;
54 #ifdef DEVELOPER
55    int low_fd = 2;
56 #else
57    int low_fd = -1;
58 #endif
59    /*
60     *  Become a daemon.
61     */
62
63    Dmsg0(900, "Enter daemon_start\n");
64    if ( (cpid = fork() ) < 0) {
65       berrno be;
66       Emsg1(M_ABORT, 0, _("Cannot fork to become daemon: ERR=%s\n"), be.bstrerror());
67    } else if (cpid > 0) {
68       exit(0);              /* parent exits */
69    }
70    /* Child continues */
71
72    setsid();
73
74    /* In the PRODUCTION system, we close ALL
75     * file descriptors except stdin, stdout, and stderr.
76     */
77    if (debug_level > 0) {
78       low_fd = 2;                     /* don't close debug output */
79    }
80    for (i=sysconf(_SC_OPEN_MAX)-1; i > low_fd; i--) {
81       close(i);
82    }
83
84    /* Move to root directory. For debug we stay
85     * in current directory so dumps go there.
86     */
87 #ifndef DEBUG
88    chdir("/");
89 #endif
90
91    /*
92     * Avoid creating files 666 but don't override any
93     * more restrictive mask set by the user.
94     */
95    oldmask = umask(026);
96    oldmask |= 026;
97    umask(oldmask);
98
99
100    /*
101     * Make sure we have fd's 0, 1, 2 open
102     *  If we don't do this one of our sockets may open
103     *  there and if we then use stdout, it could
104     *  send total garbage to our socket.
105     *
106     */
107    int fd;
108    fd = open("/dev/null", O_RDONLY, 0644);
109    if (fd > 2) {
110       close(fd);
111    } else {
112       for(i=1; fd + i <= 2; i++) {
113          dup2(fd, fd+i);
114       }
115    }
116
117 #endif /* HAVE_WIN32 */
118    Dmsg0(900, "Exit daemon_start\n");
119 }