From 2ea7a6aa48a4b781eb08fdc485493f7b7e7e15d5 Mon Sep 17 00:00:00 2001 From: Chris Novakovic Date: Sun, 17 Sep 2023 00:15:36 +0100 Subject: [PATCH] Allow PID file to be disabled at compile-time Stubby unconditionally attempts to write a PID file under the run state directory when it starts, but this directory is not always writeable by non-root or non-system account users (e.g. on Slackware). To aid with running Stubby in environments where the PID file would otherwise be unwriteable or unnecessary, allow it to be disabled at compile-time with the `ENABLE_PID_FILE` option (which remains on by default). --- CMakeLists.txt | 1 + cmake/include/cmakeconfig.h.in | 1 + src/stubby.c | 6 +++++- 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 3202377..e08cc9c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -47,6 +47,7 @@ find_package(Libsystemd) if (Libsystemd_FOUND) option(ENABLE_SYSTEMD "Enable systemd support." ON) endif() +option(ENABLE_PID_FILE "Enable PID file support." ON) option(ENABLE_GETDNS_STATIC_LINK "Link GetDNS statically." ON) if (ENABLE_GETDNS_STATIC_LINK) set(GETDNS_STATIC ON) diff --git a/cmake/include/cmakeconfig.h.in b/cmake/include/cmakeconfig.h.in index 923098e..7b1a757 100644 --- a/cmake/include/cmakeconfig.h.in +++ b/cmake/include/cmakeconfig.h.in @@ -24,6 +24,7 @@ #cmakedefine SERVER_DEBUG 1 +#cmakedefine ENABLE_PID_FILE 1 #cmakedefine ENABLE_SYSTEMD 1 #cmakedefine STUBBYCONFDIR "@STUBBYCONFDIR@" diff --git a/src/stubby.c b/src/stubby.c index 3dd7b54..0a0baf4 100644 --- a/src/stubby.c +++ b/src/stubby.c @@ -50,7 +50,7 @@ #if defined(ENABLE_WINDOWS_SERVICE) #include "windowsservice.h" -#else +#elif defined(ENABLE_PID_FILE) #define STUBBYPIDFILE RUNSTATEDIR"/stubby.pid" #endif @@ -204,6 +204,7 @@ main(int argc, char **argv) #if !defined(STUBBY_ON_WINDOWS) if (!run_in_foreground) { pid_t pid; +#if defined(ENABLE_PID_FILE) char pid_str[1024], *endptr; FILE *fh = fopen(STUBBYPIDFILE, "r"); do { @@ -225,6 +226,7 @@ main(int argc, char **argv) } while(0); if (fh) (void) fclose(fh); +#endif pid = fork(); if (pid == -1) { @@ -232,6 +234,7 @@ main(int argc, char **argv) r = GETDNS_RETURN_GENERIC_ERROR; } else if (pid) { +#if defined(ENABLE_PID_FILE) fh = fopen(STUBBYPIDFILE, "w"); if (fh) { fprintf(fh, "%d", (int)pid); @@ -242,6 +245,7 @@ main(int argc, char **argv) strerror(errno)); exit(EXIT_FAILURE); } +#endif } else { #ifdef SIGPIPE (void)signal(SIGPIPE, SIG_IGN);