+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
* 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>
*/
ns_os_tzset();
+ ns_os_opendevnull();
+
ns_os_chroot(ns_g_chrootdir);
/*
isc_app_finish();
+ ns_os_closedevnull();
+
ns_os_shutdown();
return (0);
* 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
void
ns_os_daemonize(void);
+void
+ns_os_opendevnull(void);
+
+void
+ns_os_closedevnull(void);
+
void
ns_os_chroot(const char *root);
* 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>
#include <named/os.h>
static char *pidfile = NULL;
+static int devnullfd = -1;
#ifndef ISC_FACILITY
#define ISC_FACILITY LOG_DAEMON
void
ns_os_daemonize(void) {
pid_t pid;
- int fd;
char strbuf[ISC_STRERRORSIZE];
pid = fork();
* 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;
}
}
* 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
void
ns_os_daemonize(void);
+void
+ns_os_opendevnull(void);
+
+void
+ns_os_closedevnull(void);
+
void
ns_os_chroot(const char *root);
* 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>
static char *pidfile = NULL;
+static int devnullfd = -1;
static BOOL Initialized = FALSE;
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;
}
}