From: Mark Andrews Date: Wed, 7 Jan 2004 06:17:04 +0000 (+0000) Subject: 1551. [port] Open "/dev/null" before calling chroot(). X-Git-Tag: v9.2.4rc1~81^2~44 X-Git-Url: http://git.ipfire.org/gitweb/?a=commitdiff_plain;h=c52ae25dd70636c673d4a299859137a1c8ba611a;p=thirdparty%2Fbind9.git 1551. [port] Open "/dev/null" before calling chroot(). --- diff --git a/CHANGES b/CHANGES index e08bcc675c6..b0dd18a9603 100644 --- 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 diff --git a/bin/named/main.c b/bin/named/main.c index 88a28e809bc..c63b0d681c4 100644 --- a/bin/named/main.c +++ b/bin/named/main.c @@ -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 @@ -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); diff --git a/bin/named/unix/include/named/os.h b/bin/named/unix/include/named/os.h index ccf8ceb4375..0347a640091 100644 --- a/bin/named/unix/include/named/os.h +++ b/bin/named/unix/include/named/os.h @@ -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); diff --git a/bin/named/unix/os.c b/bin/named/unix/os.c index f5caf3a0b4c..3d4d63485f3 100644 --- a/bin/named/unix/os.c +++ b/bin/named/unix/os.c @@ -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 #include @@ -44,6 +44,7 @@ #include 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; } } diff --git a/bin/named/win32/include/named/os.h b/bin/named/win32/include/named/os.h index e0799a2616f..ae3b4dd6253 100644 --- a/bin/named/win32/include/named/os.h +++ b/bin/named/win32/include/named/os.h @@ -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); diff --git a/bin/named/win32/os.c b/bin/named/win32/os.c index 5fab422cd90..f395423b537 100644 --- a/bin/named/win32/os.c +++ b/bin/named/win32/os.c @@ -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 #include @@ -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; } }