diff options
author | Jakob Schlyter <jakob@cvs.openbsd.org> | 2003-01-20 22:22:46 +0000 |
---|---|---|
committer | Jakob Schlyter <jakob@cvs.openbsd.org> | 2003-01-20 22:22:46 +0000 |
commit | 3611ae51a4fa38456ab27419723ebbbc59f3916c (patch) | |
tree | e841ae3507b024f1039e65a8a9577802b6fe0758 | |
parent | d086ffa01cf3a5e5971e50cd05805eb120f87bd9 (diff) |
write pid-file before chroot (also specifiable via -i on the command line).
-rw-r--r-- | usr.sbin/bind/bin/named/include/named/globals.h | 2 | ||||
-rw-r--r-- | usr.sbin/bind/bin/named/main.c | 11 | ||||
-rw-r--r-- | usr.sbin/bind/bin/named/server.c | 18 | ||||
-rw-r--r-- | usr.sbin/bind/bin/named/unix/include/named/os.h | 3 | ||||
-rw-r--r-- | usr.sbin/bind/bin/named/unix/os.c | 56 |
5 files changed, 66 insertions, 24 deletions
diff --git a/usr.sbin/bind/bin/named/include/named/globals.h b/usr.sbin/bind/bin/named/include/named/globals.h index 1f3a276cba6..c114c883a21 100644 --- a/usr.sbin/bind/bin/named/include/named/globals.h +++ b/usr.sbin/bind/bin/named/include/named/globals.h @@ -106,6 +106,8 @@ EXTERN const char * ns_g_defaultpidfile INIT(NS_LOCALSTATEDIR "/run/named.pid"); EXTERN const char * lwresd_g_defaultpidfile INIT(NS_LOCALSTATEDIR "/run/lwresd.pid"); +EXTERN const char * ns_g_pidfile INIT(NS_LOCALSTATEDIR + "/run/named.pid"); EXTERN const char * ns_g_username INIT("named"); #undef EXTERN diff --git a/usr.sbin/bind/bin/named/main.c b/usr.sbin/bind/bin/named/main.c index 7dc7719ee46..9d628beb6fc 100644 --- a/usr.sbin/bind/bin/named/main.c +++ b/usr.sbin/bind/bin/named/main.c @@ -228,7 +228,7 @@ usage(void) { fprintf(stderr, "usage: named [-c conffile] [-d debuglevel] " "[-f|-g] [-n number_of_cpus]\n" - " [-p port] [-s] [-t chrootdir] [-u username]\n"); + " [-p port] [-s] [-t chrootdir] [-u username] [-i pidfile]\n"); } static void @@ -325,9 +325,8 @@ parse_command_line(int argc, char *argv[]) { ns_g_foreground = ISC_TRUE; ns_g_logstderr = ISC_TRUE; break; - /* XXXBEW -i should be removed */ case 'i': - lwresd_g_defaultpidfile = isc_commandline_argument; + ns_g_pidfile = isc_commandline_argument; break; case 'l': ns_g_lwresdonly = ISC_TRUE; @@ -456,6 +455,12 @@ static void setup(void) { isc_result_t result; + /* + * Write pidfile before chroot if specified on the command line + */ + if (ns_g_pidfile != NULL) + ns_os_preopenpidfile(ns_g_pidfile); + /* * Get the user and group information before changing the root * directory, so the administrator does not need to keep a copy diff --git a/usr.sbin/bind/bin/named/server.c b/usr.sbin/bind/bin/named/server.c index bfe95abeb1a..00b25c963f9 100644 --- a/usr.sbin/bind/bin/named/server.c +++ b/usr.sbin/bind/bin/named/server.c @@ -2065,13 +2065,17 @@ load_configuration(const char *filename, ns_server_t *server, } } - obj = NULL; - if (ns_config_get(maps, "pid-file", &obj) == ISC_R_SUCCESS) - ns_os_writepidfile(cfg_obj_asstring(obj), first_time); - else if (ns_g_lwresdonly) - ns_os_writepidfile(lwresd_g_defaultpidfile, first_time); - else - ns_os_writepidfile(ns_g_defaultpidfile, first_time); + if (ns_g_pidfile != NULL) { + ns_os_writepidfile(ns_g_pidfile, first_time); + } else { + obj = NULL; + if (ns_config_get(maps, "pid-file", &obj) == ISC_R_SUCCESS) + ns_os_writepidfile(cfg_obj_asstring(obj), first_time); + else if (ns_g_lwresdonly) + ns_os_writepidfile(lwresd_g_defaultpidfile, first_time); + else + ns_os_writepidfile(ns_g_defaultpidfile, first_time); + } obj = NULL; result = ns_config_get(maps, "statistics-file", &obj); 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 acf6e2094fb..db427bc99d3 100644 --- a/usr.sbin/bind/bin/named/unix/include/named/os.h +++ b/usr.sbin/bind/bin/named/unix/include/named/os.h @@ -47,6 +47,9 @@ void ns_os_minprivs(void); void +ns_os_preopenpidfile(const char *filename); + +void ns_os_writepidfile(const char *filename, isc_boolean_t first_time); void diff --git a/usr.sbin/bind/bin/named/unix/os.c b/usr.sbin/bind/bin/named/unix/os.c index a6dfa76633d..e94088699f4 100644 --- a/usr.sbin/bind/bin/named/unix/os.c +++ b/usr.sbin/bind/bin/named/unix/os.c @@ -43,6 +43,8 @@ #include <named/os.h> static char *pidfile = NULL; +static int pidfilefd = -1; +static isc_boolean_t preopenpidfile = ISC_FALSE; static int devnullfd = -1; /* @@ -482,19 +484,13 @@ cleanup_pidfile(void) { pidfile = NULL; } -void -ns_os_writepidfile(const char *filename, isc_boolean_t first_time) { +static int +open_pidfile(const char *filename, isc_boolean_t first_time) { int fd; - FILE *lockfile; size_t len; - pid_t pid; char strbuf[ISC_STRERRORSIZE]; void (*report)(const char *, ...); - /* - * The caller must ensure any required synchronization. - */ - report = first_time ? ns_main_earlyfatal : ns_main_earlywarning; cleanup_pidfile(); @@ -504,7 +500,7 @@ ns_os_writepidfile(const char *filename, isc_boolean_t first_time) { if (pidfile == NULL) { isc__strerror(errno, strbuf, sizeof(strbuf)); (*report)("couldn't malloc '%s': %s", filename, strbuf); - return; + return -1; } /* This is safe. */ strcpy(pidfile, filename); @@ -515,15 +511,46 @@ ns_os_writepidfile(const char *filename, isc_boolean_t first_time) { (*report)("couldn't open pid file '%s': %s", filename, strbuf); free(pidfile); pidfile = NULL; - return; + return -1; } + + return fd; +} + +void +ns_os_preopenpidfile(const char *filename) { + pidfilefd = open_pidfile(filename, ISC_TRUE); + preopenpidfile = ISC_TRUE; +} + +void +ns_os_writepidfile(const char *filename, isc_boolean_t first_time) { + int fd; + FILE *lockfile; + pid_t pid; + char strbuf[ISC_STRERRORSIZE]; + void (*report)(const char *, ...); + + /* + * The caller must ensure any required synchronization. + */ + + report = first_time ? ns_main_earlyfatal : ns_main_earlywarning; + + if (preopenpidfile == ISC_TRUE) + fd = pidfilefd; + else + fd = open_pidfile(filename, first_time); + + if (fd < 0) return; + lockfile = fdopen(fd, "w"); if (lockfile == NULL) { isc__strerror(errno, strbuf, sizeof(strbuf)); (*report)("could not fdopen() pid file '%s': %s", filename, strbuf); (void)close(fd); - cleanup_pidfile(); + if (preopenpidfile == ISC_FALSE) cleanup_pidfile(); return; } #ifdef HAVE_LINUXTHREADS @@ -534,22 +561,23 @@ ns_os_writepidfile(const char *filename, isc_boolean_t first_time) { if (fprintf(lockfile, "%ld\n", (long)pid) < 0) { (*report)("fprintf() to pid file '%s' failed", filename); (void)fclose(lockfile); - cleanup_pidfile(); + if (preopenpidfile == ISC_FALSE) cleanup_pidfile(); return; } if (fflush(lockfile) == EOF) { (*report)("fflush() to pid file '%s' failed", filename); (void)fclose(lockfile); - cleanup_pidfile(); + if (preopenpidfile == ISC_FALSE) cleanup_pidfile(); return; } (void)fclose(lockfile); + if (preopenpidfile == ISC_TRUE) pidfilefd = -1; } void ns_os_shutdown(void) { closelog(); - cleanup_pidfile(); + if (preopenpidfile == ISC_FALSE) cleanup_pidfile(); } void |