Fix reload handling

Make sure we have written the new PID before letting it take over.
This commit is contained in:
Jean Flach 2018-03-14 10:01:11 +01:00
parent 9b0fccfd80
commit a742e64e4d
2 changed files with 23 additions and 20 deletions

View File

@ -90,25 +90,7 @@ void Application::Stop(bool runtimeRemoved)
WSACleanup();
#endif /* _WIN32 */
// Getting a shutdown-signal when a restart is in progress usually
// means that the restart succeeded and the new process wants to take
// over. Write the PID of the new process to the pidfile before this
// process exits to keep systemd happy.
if (l_Restarting) {
try {
UpdatePidFile(GetPidPath(), m_ReloadProcess);
} catch (const std::exception&) {
/* abort restart */
Log(LogCritical, "Application", "Cannot update PID file. Aborting restart operation.");
return;
}
Log(LogDebug, "Application")
<< "Keeping pid '" << m_ReloadProcess << "' open.";
ClosePidFile(false);
} else
ClosePidFile(true);
ClosePidFile(true);
ObjectImpl<Application>::Stop(runtimeRemoved);
}
@ -741,6 +723,20 @@ void Application::SigUsr2Handler(int)
sd_notifyf(0, "MAINPID=%lu", (unsigned long) m_ReloadProcess);
#endif /* HAVE_SYSTEMD */
/* Write the PID of the new process to the pidfile before this
* process exits to keep systemd happy.
*/
Application::Ptr instance = GetInstance();
try {
instance->UpdatePidFile(GetPidPath(), m_ReloadProcess);
} catch (const std::exception&) {
/* abort restart */
Log(LogCritical, "Application", "Cannot update PID file. Aborting restart operation.");
return;
}
instance->ClosePidFile(false);
Exit(0);
}

View File

@ -262,10 +262,17 @@ int DaemonCommand::Run(const po::variables_map& vm, const std::vector<std::strin
Log(LogInformation, "cli", "Requesting to take over.");
int rc = kill(vm["reload-internal"].as<int>(), SIGUSR2);
if (rc) {
Log(LogCritical, "Application")
Log(LogCritical, "cli")
<< "Failed to send signal to \"" << vm["reload-internal"].as<int>() << "\" with " << strerror(errno);
return EXIT_FAILURE;
}
double start = Utility::GetTime();
while (kill(vm["reload-internal"].as<int>(), SIGCHLD) == 0)
Utility::Sleep(0.2);
Log(LogNotice, "cli")
<< "Waited for " << Utility::FormatDuration(Utility::GetTime() - start) << " on old process to exit.";
}
#endif /* _WIN32 */