2 * daemon.c by Kern Sibbald
6 * this code is inspired by the Prentice Hall book
7 * "Unix Network Programming" by W. Richard Stevens
8 * and later updated from his book "Advanced Programming
9 * in the UNIX Environment"
11 * Initialize a daemon process completely detaching us from
12 * any terminal processes.
16 Bacula® - The Network Backup Solution
18 Copyright (C) 2000-2006 Free Software Foundation Europe e.V.
20 The main author of Bacula is Kern Sibbald, with contributions from
21 many others, a complete list can be found in the file AUTHORS.
22 This program is Free Software; you can redistribute it and/or
23 modify it under the terms of version two of the GNU General Public
24 License as published by the Free Software Foundation plus additions
25 that are listed in the file LICENSE.
27 This program is distributed in the hope that it will be useful, but
28 WITHOUT ANY WARRANTY; without even the implied warranty of
29 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
30 General Public License for more details.
32 You should have received a copy of the GNU General Public License
33 along with this program; if not, write to the Free Software
34 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
37 Bacula® is a registered trademark of John Walker.
38 The licensor of Bacula is the Free Software Foundation Europe
39 (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
40 Switzerland, email:ftf@fsfeurope.org.
45 extern int debug_level;
50 #if !defined(HAVE_WIN32)
63 Dmsg0(900, "Enter daemon_start\n");
64 if ( (cpid = fork() ) < 0) {
66 Emsg1(M_ABORT, 0, _("Cannot fork to become daemon: %s\n"), be.strerror());
67 } else if (cpid > 0) {
68 exit(0); /* parent exits */
74 /* In the PRODUCTION system, we close ALL
75 * file descriptors except stdin, stdout, and stderr.
77 if (debug_level > 0) {
78 low_fd = 2; /* don't close debug output */
80 for (i=sysconf(_SC_OPEN_MAX)-1; i > low_fd; i--) {
84 /* Move to root directory. For debug we stay
85 * in current directory so dumps go there.
92 * Avoid creating files 666 but don't override any
93 * more restrictive mask set by the user.
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.
108 fd = open("/dev/null", O_RDONLY, 0644);
112 for(i=1; fd + i <= 2; i++) {
117 #endif /* HAVE_WIN32 */
118 Dmsg0(900, "Exit daemon_start\n");