2 Bacula® - The Network Backup Solution
4 Copyright (C) 2000-2014 Free Software Foundation Europe e.V.
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.
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.
14 Bacula® is a registered trademark of Kern Sibbald.
17 * daemon.c by Kern Sibbald 2000
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"
24 * Initialize a daemon process completely detaching us from
25 * any terminal processes.
35 #if !defined(HAVE_WIN32)
49 Dmsg0(900, "Enter daemon_start\n");
50 if ( (cpid = fork() ) < 0) {
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 */
60 /* In the PRODUCTION system, we close ALL
61 * file descriptors except stdin, stdout, and stderr.
63 if (debug_level > 0) {
64 low_fd = 2; /* don't close debug output */
67 #if defined(HAVE_FCNTL_F_CLOSEM)
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.
74 fcntl(low_fd, F_CLOSEM);
75 #elif defined(HAVE_CLOSEFROM)
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.
84 for (i=sysconf(_SC_OPEN_MAX)-1; i > low_fd; i--) {
89 /* Move to root directory. For debug we stay
90 * in current directory so dumps go there.
97 * Avoid creating files 666 but don't override any
98 * more restrictive mask set by the user.
100 oldmask = umask(026);
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.
112 fd = open("/dev/null", O_RDONLY, 0644);
116 for(i=1; fd + i <= 2; i++) {
121 #endif /* HAVE_WIN32 */
122 Dmsg0(900, "Exit daemon_start\n");