Skip to content

Commit

Permalink
Allow PID file to be disabled at compile-time
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
chrisnovakovic committed Sep 17, 2023
1 parent 9292e28 commit 2ea7a6a
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 1 deletion.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions cmake/include/cmakeconfig.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

#cmakedefine SERVER_DEBUG 1

#cmakedefine ENABLE_PID_FILE 1
#cmakedefine ENABLE_SYSTEMD 1

#cmakedefine STUBBYCONFDIR "@STUBBYCONFDIR@"
Expand Down
6 changes: 5 additions & 1 deletion src/stubby.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@

#if defined(ENABLE_WINDOWS_SERVICE)
#include "windowsservice.h"
#else
#elif defined(ENABLE_PID_FILE)
#define STUBBYPIDFILE RUNSTATEDIR"/stubby.pid"
#endif

Expand Down Expand Up @@ -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 {
Expand All @@ -225,13 +226,15 @@ main(int argc, char **argv)
} while(0);
if (fh)
(void) fclose(fh);
#endif

pid = fork();
if (pid == -1) {
perror("Could not fork of stubby daemon\n");
r = GETDNS_RETURN_GENERIC_ERROR;

} else if (pid) {
#if defined(ENABLE_PID_FILE)
fh = fopen(STUBBYPIDFILE, "w");
if (fh) {
fprintf(fh, "%d", (int)pid);
Expand All @@ -242,6 +245,7 @@ main(int argc, char **argv)
strerror(errno));
exit(EXIT_FAILURE);
}
#endif
} else {
#ifdef SIGPIPE
(void)signal(SIGPIPE, SIG_IGN);
Expand Down

0 comments on commit 2ea7a6a

Please sign in to comment.