Skip to content

Commit 34e0ea4

Browse files
committed
Write PID file in foreground mode when -P is explicitly given (#299)
When running with -E (foreground/non-daemon mode), the -P option was silently ignored because write_pid_file() was only called inside the DaemonMode code path. This caused no PID file to be created even when -P was explicitly passed on the command line, which is the typical setup for systemd service units using Type=simple. Fix by writing the PID file in non-daemon mode when -P is present, and tracking whether a PID file was written for proper cleanup on exit.
1 parent 717f654 commit 34e0ea4

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

core/sems.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,7 @@ int main(int argc, char* argv[])
347347
std::map<char,string> args;
348348
#ifndef DISABLE_DAEMON_MODE
349349
int fd[2] = {0,0};
350+
bool pid_file_written = false;
350351
#endif
351352

352353
progname = strrchr(argv[0], '/');
@@ -542,6 +543,7 @@ int main(int argc, char* argv[])
542543
if(write_pid_file()<0) {
543544
goto error;
544545
}
546+
pid_file_written = true;
545547

546548
#ifdef PROPAGATE_COREDUMP_SETTINGS
547549
if (have_limit) {
@@ -567,6 +569,11 @@ int main(int argc, char* argv[])
567569
strerror(errno));
568570
/* continue, leave it open */
569571
};
572+
} else if(args.find('P') != args.end()) {
573+
if(write_pid_file()<0) {
574+
goto error;
575+
}
576+
pid_file_written = true;
570577
}
571578

572579
#endif /* DISABLE_DAEMON_MODE */
@@ -669,7 +676,7 @@ int main(int argc, char* argv[])
669676
async_file_writer::instance()->join();
670677

671678
#ifndef DISABLE_DAEMON_MODE
672-
if (AmConfig::DaemonMode) {
679+
if (pid_file_written) {
673680
unlink(AmConfig::DaemonPidFile.c_str());
674681
}
675682
if(fd[1]){

0 commit comments

Comments
 (0)