]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/daemon.c
Minor changes
[bacula/bacula] / bacula / src / lib / daemon.c
1 /*
2  *  daemon.c by Kern Sibbald
3  *
4  *   Version $Id$
5  *
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"
10  *
11  * Initialize a daemon process completely detaching us from
12  * any terminal processes.
13  *
14  */
15
16 /*
17    Copyright (C) 2000-2004 Kern Sibbald and John Walker
18
19    This program is free software; you can redistribute it and/or
20    modify it under the terms of the GNU General Public License as
21    published by the Free Software Foundation; either version 2 of
22    the License, or (at your option) any later version.
23
24    This program is distributed in the hope that it will be useful,
25    but WITHOUT ANY WARRANTY; without even the implied warranty of
26    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27    General Public License for more details.
28
29    You should have received a copy of the GNU General Public
30    License along with this program; if not, write to the Free
31    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
32    MA 02111-1307, USA.
33
34  */
35
36
37 #include "bacula.h"
38 extern int debug_level;
39
40 void
41 daemon_start()
42 {
43 #if !defined(HAVE_CYGWIN) && !defined(HAVE_WIN32)
44    int i;
45    pid_t cpid;
46    mode_t oldmask;
47 #ifdef DEVELOPER
48    int low_fd = 2;
49 #else
50    int low_fd = -1;
51 #endif
52    /*
53     *  Become a daemon.
54     */
55
56    Dmsg0(900, "Enter daemon_start\n");
57    if ( (cpid = fork() ) < 0)
58       Emsg1(M_ABORT, 0, "Cannot fork to become daemon: %s\n", strerror(errno));
59    else if (cpid > 0)
60       exit(0);              /* parent exits */
61    /* Child continues */
62
63    setsid();
64
65    /* In the PRODUCTION system, we close ALL
66     * file descriptors except stdin, stdout, and stderr.
67     */
68    if (debug_level > 0) {
69       low_fd = 2;                     /* don't close debug output */
70    }
71    for (i=sysconf(_SC_OPEN_MAX)-1; i > low_fd; i--) {
72       close(i);
73    }
74
75    /* Move to root directory. For debug we stay
76     * in current directory so dumps go there.
77     */
78 #ifndef DEBUG
79    chdir("/");
80 #endif
81
82    /*
83     * Avoid creating files 666 but don't override any
84     * more restrictive mask set by the user.
85     */
86    oldmask = umask(026);
87    oldmask |= 026;
88    umask(oldmask);
89
90
91    /*
92     * Make sure we have fd's 0, 1, 2 open
93     *  If we don't do this one of our sockets may open
94     *  there and if we then use stdout, it could
95     *  send total garbage to our socket.
96     *
97     */
98    int fd;
99    fd = open("/dev/null", O_RDONLY, 0644);
100    if (fd > 2) {
101       close(fd);
102    } else {
103       for(i=1; fd + i <= 2; i++) {
104          dup2(fd, fd+i);
105       }
106    }
107
108 #endif /* HAVE_CYGWIN */
109    Dmsg0(900, "Exit daemon_start\n");
110 }