]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
1551. [port] Open "/dev/null" before calling chroot().
authorMark Andrews <marka@isc.org>
Wed, 7 Jan 2004 06:17:04 +0000 (06:17 +0000)
committerMark Andrews <marka@isc.org>
Wed, 7 Jan 2004 06:17:04 +0000 (06:17 +0000)
CHANGES
bin/named/main.c
bin/named/unix/include/named/os.h
bin/named/unix/os.c
bin/named/win32/include/named/os.h
bin/named/win32/os.c

diff --git a/CHANGES b/CHANGES
index e08bcc675c6f71613c818372b06287376efd7d63..b0dd18a96038b3388f18726fa1c8c40e99888127 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -1,3 +1,5 @@
+1551.  [port]          Open "/dev/null" before calling chroot().
+
 1550.  [port]          Call tzset(), if available, before calling chroot().
 
 1549.  [func]          named-checkzone can now write out the zone contents
index 88a28e809bc66a4467c340c0714b4334bfe9069f..c63b0d681c475ed131c85d5be408bbff74302fe2 100644 (file)
@@ -15,7 +15,7 @@
  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 
-/* $Id: main.c,v 1.132 2004/01/07 05:48:15 marka Exp $ */
+/* $Id: main.c,v 1.133 2004/01/07 06:17:04 marka Exp $ */
 
 #include <config.h>
 
@@ -542,6 +542,8 @@ setup(void) {
         */
        ns_os_tzset();
 
+       ns_os_opendevnull();
+
        ns_os_chroot(ns_g_chrootdir);
 
        /*
@@ -740,6 +742,8 @@ main(int argc, char *argv[]) {
 
        isc_app_finish();
 
+       ns_os_closedevnull();
+
        ns_os_shutdown();
 
        return (0);
index ccf8ceb437547d9a23afe53b4390a01ffc220815..0347a640091a7d9cfcfa54c2cf3e19cb283b489a 100644 (file)
@@ -15,7 +15,7 @@
  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 
-/* $Id: os.h,v 1.20 2004/01/07 05:48:15 marka Exp $ */
+/* $Id: os.h,v 1.21 2004/01/07 06:17:04 marka Exp $ */
 
 #ifndef NS_OS_H
 #define NS_OS_H 1
@@ -28,6 +28,12 @@ ns_os_init(const char *progname);
 void
 ns_os_daemonize(void);
 
+void
+ns_os_opendevnull(void);
+
+void
+ns_os_closedevnull(void);
+
 void
 ns_os_chroot(const char *root);
 
index f5caf3a0b4ccb42112bc963f0a4273cc0b8f8003..3d4d63485f39dafd04950e0ae405e2d308f2d14c 100644 (file)
@@ -15,7 +15,7 @@
  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 
-/* $Id: os.c,v 1.63 2004/01/07 05:48:15 marka Exp $ */
+/* $Id: os.c,v 1.64 2004/01/07 06:17:04 marka Exp $ */
 
 #include <config.h>
 #include <stdarg.h>
@@ -44,6 +44,7 @@
 #include <named/os.h>
 
 static char *pidfile = NULL;
+static int devnullfd = -1;
 
 #ifndef ISC_FACILITY
 #define ISC_FACILITY LOG_DAEMON
@@ -292,7 +293,6 @@ ns_os_init(const char *progname) {
 void
 ns_os_daemonize(void) {
        pid_t pid;
-       int fd;
        char strbuf[ISC_STRERRORSIZE];
 
        pid = fork();
@@ -326,18 +326,34 @@ ns_os_daemonize(void) {
         * and will end up closing the wrong FD.  This will be fixed eventually,
         * and these calls will be removed.
         */
-       fd = open("/dev/null", O_RDWR, 0);
-       if (fd != -1) {
-               (void)close(STDIN_FILENO);
-               (void)dup2(fd, STDIN_FILENO);
-               (void)close(STDOUT_FILENO);
-               (void)dup2(fd, STDOUT_FILENO);
-               (void)close(STDERR_FILENO);
-               (void)dup2(fd, STDERR_FILENO);
-               if (fd != STDIN_FILENO &&
-                   fd != STDOUT_FILENO &&
-                   fd != STDERR_FILENO)
-                       (void)close(fd);
+       if (devnullfd != -1) {
+               if (devnullfd != STDIN_FILENO) {
+                       (void)close(STDIN_FILENO);
+                       (void)dup2(devnullfd, STDIN_FILENO);
+               }
+               if (devnullfd != STDOUT_FILENO) {
+                       (void)close(STDOUT_FILENO);
+                       (void)dup2(devnullfd, STDOUT_FILENO);
+               }
+               if (devnullfd != STDERR_FILENO) {
+                       (void)close(STDERR_FILENO);
+                       (void)dup2(devnullfd, STDERR_FILENO);
+               }
+       }
+}
+
+void
+ns_os_opendevnull(void) {
+       devnullfd = open("/dev/null", O_RDWR, 0);
+}
+
+void
+ns_os_closedevnull(void) {
+       if (devnullfd != STDIN_FILENO &&
+           devnullfd != STDOUT_FILENO &&
+           devnullfd != STDERR_FILENO) {
+               close(devnullfd);
+               devnullfd = -1;
        }
 }
 
index e0799a2616f4ac8ae647253b5e8cb08f7dfaab7b..ae3b4dd625352eecd6b6fa6d4a6e008eeaf80f01 100644 (file)
@@ -15,7 +15,7 @@
  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 
-/* $Id: os.h,v 1.7 2004/01/07 05:48:15 marka Exp $ */
+/* $Id: os.h,v 1.8 2004/01/07 06:17:04 marka Exp $ */
 
 #ifndef NS_OS_H
 #define NS_OS_H 1
@@ -28,6 +28,12 @@ ns_os_init(const char *progname);
 void
 ns_os_daemonize(void);
 
+void
+ns_os_opendevnull(void);
+
+void
+ns_os_closedevnull(void);
+
 void
 ns_os_chroot(const char *root);
 
index 5fab422cd90a1f18661132ea5f56137bdfc1d509..f395423b537a6a440da78c222f819933e3d0d665 100644 (file)
@@ -15,7 +15,7 @@
  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 
-/* $Id: os.c,v 1.18 2004/01/07 05:48:15 marka Exp $ */
+/* $Id: os.c,v 1.19 2004/01/07 06:17:04 marka Exp $ */
 
 #include <config.h>
 #include <stdarg.h>
@@ -47,6 +47,7 @@
 
 
 static char *pidfile = NULL;
+static int devnullfd = -1;
 
 static BOOL Initialized = FALSE;
 
@@ -108,30 +109,38 @@ ns_os_init(const char *progname) {
 
 void
 ns_os_daemonize(void) {
-       int fd;
-
        /*
         * Try to set stdin, stdout, and stderr to /dev/null, but press
         * on even if it fails.
-        *
-        * XXXMLG The close() calls here are unneeded on all but NetBSD, but
-        * are harmless to include everywhere.  dup2() is supposed to close
-        * the FD if it is in use, but unproven-pthreads-0.16 is broken
-        * and will end up closing the wrong FD.  This will be fixed eventually,
-        * and these calls will be removed.
         */
-       fd = open("NUL", O_RDWR, 0);
-       if (fd != -1) {
-               close(_fileno(stdin));
-               (void)_dup2(fd, _fileno(stdin));
-               close(_fileno(stdout));
-               (void)_dup2(fd, _fileno(stdout));
-               close(_fileno(stderr));
-               (void)_dup2(fd, _fileno(stderr));
-               if (fd != _fileno(stdin) &&
-                   fd != _fileno(stdout) &&
-                   fd != _fileno(stderr))
-                       (void)close(fd);
+       if (devnullfd != -1) {
+               if (devnullfd != _fileno(stdin)) {
+                       close(_fileno(stdin));
+                       (void)_dup2(devnullfd, _fileno(stdin));
+               }
+               if (devnullfd != _fileno(stdout)) {
+                       close(_fileno(stdout));
+                       (void)_dup2(devnullfd, _fileno(stdout));
+               }
+               if (devnullfd != _fileno(stderr)) {
+                       close(_fileno(stderr));
+                       (void)_dup2(devnullfd, _fileno(stderr));
+               }
+       }
+}
+
+void
+ns_os_opendevnull(void) {
+       devnullfd = open("NUL", O_RDWR, 0);
+}
+
+void
+ns_os_closedevnull(void) {
+       if (devnullfd != _fileno(stdin) &&
+           devnullfd != _fileno(stdout) &&
+           devnullfd != _fileno(stderr)) {
+               close(devnullfd);
+               devnullfd = -1;
        }
 }