]> granicus.if.org Git - icinga2/commitdiff
Fix reload handling 6163/head
authorJean Flach <jean-marcel.flach@icinga.com>
Wed, 14 Mar 2018 09:01:11 +0000 (10:01 +0100)
committerJean Flach <jean-marcel.flach@icinga.com>
Wed, 14 Mar 2018 09:43:59 +0000 (10:43 +0100)
Make sure we have written the new PID before letting it take over.

lib/base/application.cpp
lib/cli/daemoncommand.cpp

index 6f0f9d6606a97b97f15403ae1c880ff2d70c1cfe..f28946abd8a2d4552eec720916bd580ae6788d4f 100644 (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);
 }
 
index 199fa67ff713c6b409268c5eaac9f01a28865456..5b34bc18dc475fe160bf80cccb7b75bcfca655b8 100644 (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 */