]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/daemon.c
Backport from BEE
[bacula/bacula] / bacula / src / lib / daemon.c
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2000-2014 Free Software Foundation Europe e.V.
5
6    The main author of Bacula is Kern Sibbald, with contributions from many
7    others, a complete list can be found in the file AUTHORS.
8
9    You may use this file and others of this release according to the
10    license defined in the LICENSE file, which includes the Affero General
11    Public License, v3.0 ("AGPLv3") and some additional permissions and
12    terms pursuant to its AGPLv3 Section 7.
13
14    Bacula® is a registered trademark of Kern Sibbald.
15 */
16 /*
17  *  daemon.c by Kern Sibbald 2000
18  *
19  *   This code is inspired by the Prentice Hall book
20  *   "Unix Network Programming" by W. Richard Stevens
21  *   and later updated from his book "Advanced Programming
22  *   in the UNIX Environment"
23  *
24  * Initialize a daemon process completely detaching us from
25  * any terminal processes.
26  *
27  */
28
29
30 #include "bacula.h"
31
32 void
33 daemon_start()
34 {
35 #if !defined(HAVE_WIN32)
36    int i;
37    int fd;
38    pid_t cpid;
39    mode_t oldmask;
40 #ifdef DEVELOPER
41    int low_fd = 2;
42 #else
43    int low_fd = -1;
44 #endif
45    /*
46     *  Become a daemon.
47     */
48
49    Dmsg0(900, "Enter daemon_start\n");
50    if ( (cpid = fork() ) < 0) {
51       berrno be;
52       Emsg1(M_ABORT, 0, _("Cannot fork to become daemon: ERR=%s\n"), be.bstrerror());
53    } else if (cpid > 0) {
54       exit(0);              /* parent exits */
55    }
56    /* Child continues */
57
58    setsid();
59
60    /* In the PRODUCTION system, we close ALL
61     * file descriptors except stdin, stdout, and stderr.
62     */
63    if (debug_level > 0) {
64       low_fd = 2;                     /* don't close debug output */
65    }
66
67 #if defined(HAVE_FCNTL_F_CLOSEM)
68    /*
69     * fcntl(fd, F_CLOSEM) needs the minimum filedescriptor
70     * to close. the current code sets the last one to keep
71     * open. So increment it with 1 and use that as argument.
72     */
73    low_fd++;
74    fcntl(low_fd, F_CLOSEM);
75 #elif defined(HAVE_CLOSEFROM)
76    /*
77     * closefrom needs the minimum filedescriptor to close.
78     * the current code sets the last one to keep open.
79     * So increment it with 1 and use that as argument.
80     */
81    low_fd++;
82    closefrom(low_fd);
83 #else
84    for (i=sysconf(_SC_OPEN_MAX)-1; i > low_fd; i--) {
85       close(i);
86    }
87 #endif
88
89    /* Move to root directory. For debug we stay
90     * in current directory so dumps go there.
91     */
92 #ifndef DEBUG
93    chdir("/");
94 #endif
95
96    /*
97     * Avoid creating files 666 but don't override any
98     * more restrictive mask set by the user.
99     */
100    oldmask = umask(026);
101    oldmask |= 026;
102    umask(oldmask);
103
104
105    /*
106     * Make sure we have fd's 0, 1, 2 open
107     *  If we don't do this one of our sockets may open
108     *  there and if we then use stdout, it could
109     *  send total garbage to our socket.
110     *
111     */
112    fd = open("/dev/null", O_RDONLY, 0644);
113    if (fd > 2) {
114       close(fd);
115    } else {
116       for(i=1; fd + i <= 2; i++) {
117          dup2(fd, fd+i);
118       }
119    }
120
121 #endif /* HAVE_WIN32 */
122    Dmsg0(900, "Exit daemon_start\n");
123 }