From 8c8f261c8042392a2c92070b2b5915a222662da4 Mon Sep 17 00:00:00 2001 From: nirenjan Date: Mon, 16 May 2022 10:28:58 -0700 Subject: [PATCH] Disable use of PID file when running in foreground When running in foreground, it is likely that it is running within systemd. In this case, a stale PID file is likely to have a PID that corresponds to a different process, which can still be kill'ed by the root user. This results in a false positive that the process is still running and causes the daemon to abort prematurely. Fixes #42 --- daemon/x52d_main.c | 62 ++++++++++++++++++++++------------------------ 1 file changed, 30 insertions(+), 32 deletions(-) diff --git a/daemon/x52d_main.c b/daemon/x52d_main.c index 4d4cdb7..128dbd0 100644 --- a/daemon/x52d_main.c +++ b/daemon/x52d_main.c @@ -105,26 +105,26 @@ static void start_daemon(bool foreground, const char *pid_file) pid_file = X52D_PID_FILE; } - /* Check if there is an existing daemon process running */ - pid_fd = fopen(pid_file, "r"); - if (pid_fd != NULL) { - int rc; + if (!foreground) { + /* Check if there is an existing daemon process running */ + pid_fd = fopen(pid_file, "r"); + if (pid_fd != NULL) { + int rc; - /* File exists, read the PID and check if it exists */ - rc = fscanf(pid_fd, "%u", &pid); - fclose(pid_fd); + /* File exists, read the PID and check if it exists */ + rc = fscanf(pid_fd, "%u", &pid); + fclose(pid_fd); - if (rc != 1) { - perror("fscanf"); - } else { - rc = kill(pid, 0); - if (rc == 0 || (rc < 0 && errno == EPERM)) { - PINELOG_FATAL(_("Daemon is already running as PID %u"), pid); + if (rc != 1) { + perror("fscanf"); + } else { + rc = kill(pid, 0); + if (rc == 0 || (rc < 0 && errno == EPERM)) { + PINELOG_FATAL(_("Daemon is already running as PID %u"), pid); + } } } - } - if (!foreground) { /* Fork off the parent process */ pid = fork(); if (pid < 0) { @@ -163,25 +163,23 @@ static void start_daemon(bool foreground, const char *pid_file) /* Terminate the parent */ exit(EXIT_SUCCESS); } - } - /* Write the PID to the pid_file */ - pid_fd = fopen(pid_file, "w"); - if (pid_fd == NULL) { - /* Unable to open PID file */ - perror("fopen"); - exit(EXIT_FAILURE); - } - if (fprintf(pid_fd, "%u\n", getpid()) < 0) { - perror("fprintf"); - exit(EXIT_FAILURE); - } - if (fclose(pid_fd) != 0) { - perror("fclose"); - exit(EXIT_FAILURE); - } + /* Write the PID to the pid_file */ + pid_fd = fopen(pid_file, "w"); + if (pid_fd == NULL) { + /* Unable to open PID file */ + perror("fopen"); + exit(EXIT_FAILURE); + } + if (fprintf(pid_fd, "%u\n", getpid()) < 0) { + perror("fprintf"); + exit(EXIT_FAILURE); + } + if (fclose(pid_fd) != 0) { + perror("fclose"); + exit(EXIT_FAILURE); + } - if (!foreground) { /* Set new file permissions */ umask(0);