summaryrefslogtreecommitdiff
path: root/usr.sbin/bind/bin/named/unix/os.c
diff options
context:
space:
mode:
Diffstat (limited to 'usr.sbin/bind/bin/named/unix/os.c')
-rw-r--r--usr.sbin/bind/bin/named/unix/os.c56
1 files changed, 42 insertions, 14 deletions
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