From 12eb54d03f923b239d63dea49c13e6242e7fc8d0 Mon Sep 17 00:00:00 2001 From: Nicolas Williams Date: Sun, 4 Dec 2016 16:41:26 -0600 Subject: [PATCH] rk_pidfile() should call pidfile() if HAVE_PIDFILE This is necessary so we can use --detach in tests even on NetBSD and OpenBSD. --- lib/roken/detach.c | 1 + lib/roken/roken.h.in | 2 -- lib/roken/write_pid.c | 18 ++++++++++++++---- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/lib/roken/detach.c b/lib/roken/detach.c index 626e1e608..2b8bc924c 100644 --- a/lib/roken/detach.c +++ b/lib/roken/detach.c @@ -163,6 +163,7 @@ roken_detach_finish(const char *dir, int daemon_child_fd) ssize_t bytes; int fd; + pidfile(NULL); if (pipefds[1] == -1 && daemon_child_fd != -1) pipefds[1] = daemon_child_fd; if (pipefds[0] != -1) diff --git a/lib/roken/roken.h.in b/lib/roken/roken.h.in index e6b89277b..9add023b5 100644 --- a/lib/roken/roken.h.in +++ b/lib/roken/roken.h.in @@ -756,14 +756,12 @@ ROKEN_LIB_FUNCTION ssize_t ROKEN_LIB_CALL readv(int, const struct iovec *, int); #endif -#ifndef HAVE_PIDFILE #ifdef NO_PIDFILES #define pidfile(x) ((void) 0) #else #define pidfile rk_pidfile ROKEN_LIB_FUNCTION void ROKEN_LIB_CALL pidfile (const char*); #endif -#endif #ifndef HAVE_BSWAP64 #define bswap64 rk_bswap64 diff --git a/lib/roken/write_pid.c b/lib/roken/write_pid.c index 4b549f1b3..1be00c344 100644 --- a/lib/roken/write_pid.c +++ b/lib/roken/write_pid.c @@ -76,7 +76,6 @@ pid_file_delete(char **filename) } } -#ifndef HAVE_PIDFILE static char *pidfile_path; static void @@ -89,15 +88,26 @@ pidfile_cleanup(void) ROKEN_LIB_FUNCTION void ROKEN_LIB_CALL pidfile(const char *bname) { + /* + * If the OS has a pidfile(), call that, but still call + * pid_file_write(). Even if both want to write the same file, + * writing it twice will still work. + */ +#ifdef HAVE_PIDFILE +#undef pidfile + pidfile(bname); +#endif + if (pidfile_path != NULL) return; if (bname == NULL) bname = getprogname(); pidfile_path = pid_file_write(bname); #if defined(HAVE_ATEXIT) - atexit(pidfile_cleanup); + if (pidfile_path != NULL) + atexit(pidfile_cleanup); #elif defined(HAVE_ON_EXIT) - on_exit(pidfile_cleanup); + if (pidfile_path != NULL) + on_exit(pidfile_cleanup); #endif } -#endif