From 2aa67e804d85f4d88153368ce65ce4df7b5390e6 Mon Sep 17 00:00:00 2001 From: Bob Halley Date: Wed, 26 Jan 2000 21:12:33 +0000 Subject: [PATCH] add ns_os_deamonize() --- bin/named/unix/include/named/os.h | 3 +++ bin/named/unix/os.c | 33 +++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/bin/named/unix/include/named/os.h b/bin/named/unix/include/named/os.h index b146bdec32..41de451cc0 100644 --- a/bin/named/unix/include/named/os.h +++ b/bin/named/unix/include/named/os.h @@ -23,6 +23,9 @@ isc_result_t ns_os_init(void); +isc_result_t +ns_os_daemonize(void); + void ns_os_shutdown(void); diff --git a/bin/named/unix/os.c b/bin/named/unix/os.c index 48451250f2..57b9206d95 100644 --- a/bin/named/unix/os.c +++ b/bin/named/unix/os.c @@ -25,6 +25,7 @@ #include #include #include +#include #include @@ -92,6 +93,38 @@ ns_os_init(void) { return (ISC_R_SUCCESS); } +isc_result_t +ns_os_daemonize(void) { + pid_t pid; + int fd; + + pid = fork(); + if (pid == -1) + return (ISC_R_FAILURE); + if (pid != 0) + _exit(0); + + /* + * We're the child. + */ + + if (setsid() == -1) + return (ISC_R_FAILURE); + + /* + * Try to set stdin, stdout, and stderr to /dev/null, but press + * on even if it fails. + */ + fd = open("/dev/null", O_RDWR, 0); + if (fd != -1) { + (void)dup2(fd, STDIN_FILENO); + (void)dup2(fd, STDOUT_FILENO); + (void)dup2(fd, STDERR_FILENO); + } + + return (ISC_R_SUCCESS); +} + void ns_os_shutdown(void) { closelog();