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
reverse-scroll
nirenjan 2022-05-16 10:28:58 -07:00
parent 6a8dff0a17
commit 8c8f261c80
1 changed files with 30 additions and 32 deletions

View File

@ -105,6 +105,7 @@ static void start_daemon(bool foreground, const char *pid_file)
pid_file = X52D_PID_FILE;
}
if (!foreground) {
/* Check if there is an existing daemon process running */
pid_fd = fopen(pid_file, "r");
if (pid_fd != NULL) {
@ -124,7 +125,6 @@ static void start_daemon(bool foreground, const char *pid_file)
}
}
if (!foreground) {
/* Fork off the parent process */
pid = fork();
if (pid < 0) {
@ -163,7 +163,6 @@ 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");
@ -181,7 +180,6 @@ static void start_daemon(bool foreground, const char *pid_file)
exit(EXIT_FAILURE);
}
if (!foreground) {
/* Set new file permissions */
umask(0);