mirror of https://github.com/nirenjan/libx52.git
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 #42reverse-scroll
parent
6a8dff0a17
commit
8c8f261c80
|
@ -105,26 +105,26 @@ static void start_daemon(bool foreground, const char *pid_file)
|
||||||
pid_file = X52D_PID_FILE;
|
pid_file = X52D_PID_FILE;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Check if there is an existing daemon process running */
|
if (!foreground) {
|
||||||
pid_fd = fopen(pid_file, "r");
|
/* Check if there is an existing daemon process running */
|
||||||
if (pid_fd != NULL) {
|
pid_fd = fopen(pid_file, "r");
|
||||||
int rc;
|
if (pid_fd != NULL) {
|
||||||
|
int rc;
|
||||||
|
|
||||||
/* File exists, read the PID and check if it exists */
|
/* File exists, read the PID and check if it exists */
|
||||||
rc = fscanf(pid_fd, "%u", &pid);
|
rc = fscanf(pid_fd, "%u", &pid);
|
||||||
fclose(pid_fd);
|
fclose(pid_fd);
|
||||||
|
|
||||||
if (rc != 1) {
|
if (rc != 1) {
|
||||||
perror("fscanf");
|
perror("fscanf");
|
||||||
} else {
|
} else {
|
||||||
rc = kill(pid, 0);
|
rc = kill(pid, 0);
|
||||||
if (rc == 0 || (rc < 0 && errno == EPERM)) {
|
if (rc == 0 || (rc < 0 && errno == EPERM)) {
|
||||||
PINELOG_FATAL(_("Daemon is already running as PID %u"), pid);
|
PINELOG_FATAL(_("Daemon is already running as PID %u"), pid);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (!foreground) {
|
|
||||||
/* Fork off the parent process */
|
/* Fork off the parent process */
|
||||||
pid = fork();
|
pid = fork();
|
||||||
if (pid < 0) {
|
if (pid < 0) {
|
||||||
|
@ -163,25 +163,23 @@ static void start_daemon(bool foreground, const char *pid_file)
|
||||||
/* Terminate the parent */
|
/* Terminate the parent */
|
||||||
exit(EXIT_SUCCESS);
|
exit(EXIT_SUCCESS);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/* Write the PID to the pid_file */
|
/* Write the PID to the pid_file */
|
||||||
pid_fd = fopen(pid_file, "w");
|
pid_fd = fopen(pid_file, "w");
|
||||||
if (pid_fd == NULL) {
|
if (pid_fd == NULL) {
|
||||||
/* Unable to open PID file */
|
/* Unable to open PID file */
|
||||||
perror("fopen");
|
perror("fopen");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
if (fprintf(pid_fd, "%u\n", getpid()) < 0) {
|
if (fprintf(pid_fd, "%u\n", getpid()) < 0) {
|
||||||
perror("fprintf");
|
perror("fprintf");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
if (fclose(pid_fd) != 0) {
|
if (fclose(pid_fd) != 0) {
|
||||||
perror("fclose");
|
perror("fclose");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!foreground) {
|
|
||||||
/* Set new file permissions */
|
/* Set new file permissions */
|
||||||
umask(0);
|
umask(0);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue