]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/lib/daemon.c
Add support for fcntl(fd, F_CLOSEM) to close filedescriptors.
[bacula/bacula] / bacula / src / lib / daemon.c
index 9d3f4391b7927499c34a7d5d48ae35ffff0bbe09..72b3365caaa7cabba83aa51942e72f771df251c3 100644 (file)
@@ -1,7 +1,7 @@
 /*
    Bacula® - The Network Backup Solution
 
-   Copyright (C) 2000-2008 Free Software Foundation Europe e.V.
+   Copyright (C) 2000-2011 Free Software Foundation Europe e.V.
 
    The main author of Bacula is Kern Sibbald, with contributions from
    many others, a complete list can be found in the file AUTHORS.
@@ -28,8 +28,6 @@
 /*
  *  daemon.c by Kern Sibbald 2000
  *
- *   Version $Id$
- *
  *   This code is inspired by the Prentice Hall book
  *   "Unix Network Programming" by W. Richard Stevens
  *   and later updated from his book "Advanced Programming
@@ -49,6 +47,7 @@ daemon_start()
 {
 #if !defined(HAVE_WIN32)
    int i;
+   int fd;
    pid_t cpid;
    mode_t oldmask;
 #ifdef DEVELOPER
@@ -77,9 +76,28 @@ daemon_start()
    if (debug_level > 0) {
       low_fd = 2;                     /* don't close debug output */
    }
+
+#if defined(HAVE_FCNTL_F_CLOSEM)
+   /*
+    * fcntl(fd, F_CLOSEM) needs the minimum filedescriptor
+    * to close. the current code sets the last one to keep
+    * open. So increment it with 1 and use that as argument.
+    */
+   low_fd++;
+   fcntl(low_fd, F_CLOSEM);
+#elif defined(HAVE_CLOSEFROM)
+   /*
+    * closefrom needs the minimum filedescriptor to close.
+    * the current code sets the last one to keep open.
+    * So increment it with 1 and use that as argument.
+    */
+   low_fd++;
+   closefrom(low_fd);
+#else
    for (i=sysconf(_SC_OPEN_MAX)-1; i > low_fd; i--) {
       close(i);
    }
+#endif
 
    /* Move to root directory. For debug we stay
     * in current directory so dumps go there.
@@ -104,7 +122,6 @@ daemon_start()
     *  send total garbage to our socket.
     *
     */
-   int fd;
    fd = open("/dev/null", O_RDONLY, 0644);
    if (fd > 2) {
       close(fd);