From d57148540f5620243ebb2a44322ba104a5129692 Mon Sep 17 00:00:00 2001 From: Andrew den Exter Date: Thu, 19 Apr 2018 14:08:44 +1000 Subject: [PATCH] [mapplauncherd] Fix race when in writing the daemon PID file and exiting. Contributes to JB#41671 The process forks twice when daemonizing, we need to wait for the first fork to exit after it has written the PID of the second fork before exiting the original process otherwise systemd may try and read the file before it exists. --- src/launcherlib/daemon.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/launcherlib/daemon.cpp b/src/launcherlib/daemon.cpp index fc33992..c1dcaf6 100644 --- a/src/launcherlib/daemon.cpp +++ b/src/launcherlib/daemon.cpp @@ -522,7 +522,10 @@ void Daemon::daemonize() // If we got a good PID, then we can exit the parent process. if (pid > 0) { - exit(EXIT_SUCCESS); + // Wait for the child fork to exit to ensure the PID has been written before a caller + // is notified of the exit. + waitpid(pid, NULL, 0); + _exit(EXIT_SUCCESS); } // Fork off the parent process: second fork @@ -541,7 +544,7 @@ void Daemon::daemonize() fclose(pidFile); } - exit(EXIT_SUCCESS); + _exit(EXIT_SUCCESS); } // Change the file mode mask