Commit c52ae25d authored by Mark Andrews's avatar Mark Andrews
Browse files

1551. [port] Open "/dev/null" before calling chroot().

parent 2b50e0d8
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
......
......@@ -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);
......
......@@ -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);
......
......@@ -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;
}
}
......
......@@ -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);
......
......@@ -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;
}
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment