]> granicus.if.org Git - icinga2/commitdiff
Adjust sd_notify()
authorAlexander A. Klimov <alexander.klimov@icinga.com>
Mon, 15 Jul 2019 14:58:34 +0000 (16:58 +0200)
committerAlexander A. Klimov <alexander.klimov@icinga.com>
Mon, 15 Jul 2019 15:04:55 +0000 (17:04 +0200)
refs #5230

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

index 9aea52c7b8728249ced8de2033d855c001188184..873c399c9dddf4b403807941b99ef9ba2e0cdfb2 100644 (file)
@@ -30,9 +30,6 @@
 #else /* _WIN32 */
 #include <signal.h>
 #endif /* _WIN32 */
-#ifdef HAVE_SYSTEMD
-#include <systemd/sd-daemon.h>
-#endif /* HAVE_SYSTEMD */
 
 using namespace icinga;
 
@@ -295,20 +292,12 @@ void Application::SetArgV(char **argv)
  */
 void Application::RunEventLoop()
 {
-#ifdef HAVE_SYSTEMD
-       sd_notify(0, "READY=1");
-#endif /* HAVE_SYSTEMD */
-
        double lastLoop = Utility::GetTime();
 
        while (!m_ShuttingDown) {
                if (m_RequestRestart) {
                        m_RequestRestart = false;         // we are now handling the request, once is enough
 
-#ifdef HAVE_SYSTEMD
-                       sd_notify(0, "RELOADING=1");
-#endif /* HAVE_SYSTEMD */
-
 #ifdef _WIN32
                        // are we already restarting? ignore request if we already are
                        if (!l_Restarting) {
@@ -331,10 +320,6 @@ void Application::RunEventLoop()
                        double now = Utility::GetTime();
                        double timeDiff = lastLoop - now;
 
-#ifdef HAVE_SYSTEMD
-                       sd_notify(0, "WATCHDOG=1");
-#endif /* HAVE_SYSTEMD */
-
                        if (std::fabs(timeDiff) > 15) {
                                /* We made a significant jump in time. */
                                Log(LogInformation, "Application")
@@ -349,10 +334,6 @@ void Application::RunEventLoop()
                }
        }
 
-#ifdef HAVE_SYSTEMD
-       sd_notify(0, "STOPPING=1");
-#endif /* HAVE_SYSTEMD */
-
        Log(LogInformation, "Application", "Shutting down...");
 
        ConfigObject::StopObjects();
index 0f5ad15eb6bb4659859a453204fbd876f2d71c29..9eaeb8ea08dbc64058b073270e710d9d6b735b87 100644 (file)
 #include <unistd.h>
 #endif /* _WIN32 */
 
+#ifdef HAVE_SYSTEMD
+#include <systemd/sd-daemon.h>
+#endif /* HAVE_SYSTEMD */
+
 using namespace icinga;
 namespace po = boost::program_options;
 
@@ -319,6 +323,20 @@ static void WorkerSignalHandler(int num, siginfo_t *info, void*)
        }
 }
 
+#ifdef HAVE_SYSTEMD
+static std::atomic<double> l_LastNotifiedWatchdog (0);
+
+static void NotifyWatchdog()
+{
+       double now = Utility::GetTime();
+
+       if (now - l_LastNotifiedWatchdog.load() >= 2.5) {
+               sd_notify(0, "WATCHDOG=1");
+               l_LastNotifiedWatchdog.store(now);
+       }
+}
+#endif /* HAVE_SYSTEMD */
+
 static pid_t StartUnixWorker(const std::vector<std::string>& configs)
 {
        try {
@@ -383,11 +401,18 @@ static pid_t StartUnixWorker(const std::vector<std::string>& configs)
                        (void)sigprocmask(SIG_UNBLOCK, &l_UnixWorkerSignals, nullptr);
 
                        for (;;) {
+#ifdef HAVE_SYSTEMD
+                               NotifyWatchdog();
+#endif /* HAVE_SYSTEMD */
+
                                switch (l_CurrentlyStartingUnixWorkerState.load()) {
                                        case UnixWorkerState::LoadedConfig:
                                                break;
                                        case UnixWorkerState::Failed:
                                                while (waitpid(pid, nullptr, 0) == -1 && errno == EINTR) {
+#ifdef HAVE_SYSTEMD
+                                                       NotifyWatchdog();
+#endif /* HAVE_SYSTEMD */
                                                }
                                                pid = -1;
                                                break;
@@ -533,34 +558,69 @@ int DaemonCommand::Run(const po::variables_map& vm, const std::vector<std::strin
 
        (void)kill(currentWorker, SIGUSR2);
 
+#ifdef HAVE_SYSTEMD
+       sd_notify(0, "READY=1");
+#endif /* HAVE_SYSTEMD */
+
        bool requestedTermination = false;
+       bool notifiedTermination = false;
 
        for (;;) {
+#ifdef HAVE_SYSTEMD
+               NotifyWatchdog();
+#endif /* HAVE_SYSTEMD */
+
                if (!requestedTermination) {
                        int termSig = l_TermSignal.load();
                        if (termSig != -1) {
                                (void)kill(currentWorker, termSig);
                                requestedTermination = true;
+
+#ifdef HAVE_SYSTEMD
+                               if (!notifiedTermination) {
+                                       notifiedTermination = true;
+                                       sd_notify(0, "STOPPING=1");
+                               }
+#endif /* HAVE_SYSTEMD */
                        }
                }
 
                if (l_RequestedReload.exchange(false)) {
+#ifdef HAVE_SYSTEMD
+                       sd_notify(0, "RELOADING=1");
+#endif /* HAVE_SYSTEMD */
+
                        pid_t nextWorker = StartUnixWorker(configs);
 
                        if (nextWorker != -1) {
                                (void)kill(currentWorker, SIGTERM);
                                while (waitpid(currentWorker, nullptr, 0) == -1 && errno == EINTR) {
+#ifdef HAVE_SYSTEMD
+                                       NotifyWatchdog();
+#endif /* HAVE_SYSTEMD */
                                }
 
                                (void)kill(nextWorker, SIGUSR2);
 
                                currentWorker = nextWorker;
                        }
+
+#ifdef HAVE_SYSTEMD
+                       sd_notify(0, "READY=1");
+#endif /* HAVE_SYSTEMD */
+
                }
 
                {
                        int status;
                        if (waitpid(currentWorker, &status, WNOHANG) > 0) {
+#ifdef HAVE_SYSTEMD
+                               if (!notifiedTermination) {
+                                       notifiedTermination = true;
+                                       sd_notify(0, "STOPPING=1");
+                               }
+#endif /* HAVE_SYSTEMD */
+
                                return WIFSIGNALED(status) ? 128 + WTERMSIG(status) : WEXITSTATUS(status);
                        }
                }