2 Bacula® - The Network Backup Solution
4 Copyright (C) 2000-2011 Free Software Foundation Europe e.V.
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
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.
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
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.
29 * daemon.c by Kern Sibbald 2000
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"
36 * Initialize a daemon process completely detaching us from
37 * any terminal processes.
43 extern int debug_level;
48 #if !defined(HAVE_WIN32)
61 Dmsg0(900, "Enter daemon_start\n");
62 if ( (cpid = fork() ) < 0) {
64 Emsg1(M_ABORT, 0, _("Cannot fork to become daemon: ERR=%s\n"), be.bstrerror());
65 } else if (cpid > 0) {
66 exit(0); /* parent exits */
72 /* In the PRODUCTION system, we close ALL
73 * file descriptors except stdin, stdout, and stderr.
75 if (debug_level > 0) {
76 low_fd = 2; /* don't close debug output */
78 for (i=sysconf(_SC_OPEN_MAX)-1; i > low_fd; i--) {
82 /* Move to root directory. For debug we stay
83 * in current directory so dumps go there.
90 * Avoid creating files 666 but don't override any
91 * more restrictive mask set by the user.
99 * Make sure we have fd's 0, 1, 2 open
100 * If we don't do this one of our sockets may open
101 * there and if we then use stdout, it could
102 * send total garbage to our socket.
106 fd = open("/dev/null", O_RDONLY, 0644);
110 for(i=1; fd + i <= 2; i++) {
115 #endif /* HAVE_WIN32 */
116 Dmsg0(900, "Exit daemon_start\n");