]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/daemon.c
Real fix of bug #1897
[bacula/bacula] / bacula / src / lib / daemon.c
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2000-2011 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  *   This code is inspired by the Prentice Hall book
32  *   "Unix Network Programming" by W. Richard Stevens
33  *   and later updated from his book "Advanced Programming
34  *   in the UNIX Environment"
35  *
36  * Initialize a daemon process completely detaching us from
37  * any terminal processes.
38  *
39  */
40
41
42 #include "bacula.h"
43 extern int debug_level;
44
45 void
46 daemon_start()
47 {
48 #if !defined(HAVE_WIN32)
49    int i;
50    int fd;
51    pid_t cpid;
52    mode_t oldmask;
53 #ifdef DEVELOPER
54    int low_fd = 2;
55 #else
56    int low_fd = -1;
57 #endif
58    /*
59     *  Become a daemon.
60     */
61
62    Dmsg0(900, "Enter daemon_start\n");
63    if ( (cpid = fork() ) < 0) {
64       berrno be;
65       Emsg1(M_ABORT, 0, _("Cannot fork to become daemon: ERR=%s\n"), be.bstrerror());
66    } else if (cpid > 0) {
67       exit(0);              /* parent exits */
68    }
69    /* Child continues */
70
71    setsid();
72
73    /* In the PRODUCTION system, we close ALL
74     * file descriptors except stdin, stdout, and stderr.
75     */
76    if (debug_level > 0) {
77       low_fd = 2;                     /* don't close debug output */
78    }
79
80 #if defined(HAVE_FCNTL_F_CLOSEM)
81    /*
82     * fcntl(fd, F_CLOSEM) needs the minimum filedescriptor
83     * to close. the current code sets the last one to keep
84     * open. So increment it with 1 and use that as argument.
85     */
86    low_fd++;
87    fcntl(low_fd, F_CLOSEM);
88 #elif defined(HAVE_CLOSEFROM)
89    /*
90     * closefrom needs the minimum filedescriptor to close.
91     * the current code sets the last one to keep open.
92     * So increment it with 1 and use that as argument.
93     */
94    low_fd++;
95    closefrom(low_fd);
96 #else
97    for (i=sysconf(_SC_OPEN_MAX)-1; i > low_fd; i--) {
98       close(i);
99    }
100 #endif
101
102    /* Move to root directory. For debug we stay
103     * in current directory so dumps go there.
104     */
105 #ifndef DEBUG
106    chdir("/");
107 #endif
108
109    /*
110     * Avoid creating files 666 but don't override any
111     * more restrictive mask set by the user.
112     */
113    oldmask = umask(026);
114    oldmask |= 026;
115    umask(oldmask);
116
117
118    /*
119     * Make sure we have fd's 0, 1, 2 open
120     *  If we don't do this one of our sockets may open
121     *  there and if we then use stdout, it could
122     *  send total garbage to our socket.
123     *
124     */
125    fd = open("/dev/null", O_RDONLY, 0644);
126    if (fd > 2) {
127       close(fd);
128    } else {
129       for(i=1; fd + i <= 2; i++) {
130          dup2(fd, fd+i);
131       }
132    }
133
134 #endif /* HAVE_WIN32 */
135    Dmsg0(900, "Exit daemon_start\n");
136 }