From 8d63731b4807ca1cf964ba9e83a57da278900680 Mon Sep 17 00:00:00 2001 From: Jakob Schlyter Date: Mon, 20 Jan 2003 22:03:59 +0000 Subject: open /dev/null before chroot --- usr.sbin/bind/bin/named/main.c | 3 +- usr.sbin/bind/bin/named/unix/include/named/os.h | 6 +++ usr.sbin/bind/bin/named/unix/os.c | 40 +++++++++++++------- usr.sbin/bind/bin/named/win32/include/named/os.h | 6 +++ usr.sbin/bind/bin/named/win32/os.c | 47 ++++++++++++++---------- 5 files changed, 68 insertions(+), 34 deletions(-) (limited to 'usr.sbin/bind/bin') diff --git a/usr.sbin/bind/bin/named/main.c b/usr.sbin/bind/bin/named/main.c index c2c93ebd484..4d318f6db17 100644 --- a/usr.sbin/bind/bin/named/main.c +++ b/usr.sbin/bind/bin/named/main.c @@ -461,9 +461,10 @@ setup(void) { ns_os_inituserinfo(ns_g_username); /* - * Initialize time conversion information + * Initialize time conversion information and /dev/null */ ns_os_tzset(); + ns_os_opendevnull(); ns_os_chroot(ns_g_chrootdir); diff --git a/usr.sbin/bind/bin/named/unix/include/named/os.h b/usr.sbin/bind/bin/named/unix/include/named/os.h index 6f4e8d80043..acf6e2094fb 100644 --- a/usr.sbin/bind/bin/named/unix/include/named/os.h +++ b/usr.sbin/bind/bin/named/unix/include/named/os.h @@ -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); diff --git a/usr.sbin/bind/bin/named/unix/os.c b/usr.sbin/bind/bin/named/unix/os.c index 0affc4ca39a..a6dfa76633d 100644 --- a/usr.sbin/bind/bin/named/unix/os.c +++ b/usr.sbin/bind/bin/named/unix/os.c @@ -43,6 +43,7 @@ #include static char *pidfile = NULL; +static int devnullfd = -1; /* * If there's no , we don't care about @@ -288,7 +289,6 @@ ns_os_init(const char *progname) { void ns_os_daemonize(void) { pid_t pid; - int fd; char strbuf[ISC_STRERRORSIZE]; pid = fork(); @@ -322,21 +322,35 @@ 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) { - close(STDIN_FILENO); - (void)dup2(fd, STDIN_FILENO); - close(STDOUT_FILENO); - (void)dup2(fd, STDOUT_FILENO); - 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); +} + static isc_boolean_t all_digits(const char *s) { if (*s == '\0') diff --git a/usr.sbin/bind/bin/named/win32/include/named/os.h b/usr.sbin/bind/bin/named/win32/include/named/os.h index b0387f2d9d5..02f9a5bd150 100644 --- a/usr.sbin/bind/bin/named/win32/include/named/os.h +++ b/usr.sbin/bind/bin/named/win32/include/named/os.h @@ -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); diff --git a/usr.sbin/bind/bin/named/win32/os.c b/usr.sbin/bind/bin/named/win32/os.c index c574daaf433..bcb0cfe37c3 100644 --- a/usr.sbin/bind/bin/named/win32/os.c +++ b/usr.sbin/bind/bin/named/win32/os.c @@ -44,6 +44,7 @@ static char *pidfile = NULL; +static int devnullfd = -1; static BOOL Initialized = FALSE; @@ -85,33 +86,39 @@ 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); +} + void ns_os_chroot(const char *root) { } -- cgit v1.2.3