Adjust sd_notify()

refs #5230
This commit is contained in:
Alexander A. Klimov 2019-07-15 16:58:34 +02:00
parent 06b504f291
commit ac29b3b93a
2 changed files with 60 additions and 19 deletions

View File

@ -30,9 +30,6 @@
#else /* _WIN32 */ #else /* _WIN32 */
#include <signal.h> #include <signal.h>
#endif /* _WIN32 */ #endif /* _WIN32 */
#ifdef HAVE_SYSTEMD
#include <systemd/sd-daemon.h>
#endif /* HAVE_SYSTEMD */
using namespace icinga; using namespace icinga;
@ -295,20 +292,12 @@ void Application::SetArgV(char **argv)
*/ */
void Application::RunEventLoop() void Application::RunEventLoop()
{ {
#ifdef HAVE_SYSTEMD
sd_notify(0, "READY=1");
#endif /* HAVE_SYSTEMD */
double lastLoop = Utility::GetTime(); double lastLoop = Utility::GetTime();
while (!m_ShuttingDown) { while (!m_ShuttingDown) {
if (m_RequestRestart) { if (m_RequestRestart) {
m_RequestRestart = false; // we are now handling the request, once is enough 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 #ifdef _WIN32
// are we already restarting? ignore request if we already are // are we already restarting? ignore request if we already are
if (!l_Restarting) { if (!l_Restarting) {
@ -331,10 +320,6 @@ void Application::RunEventLoop()
double now = Utility::GetTime(); double now = Utility::GetTime();
double timeDiff = lastLoop - now; double timeDiff = lastLoop - now;
#ifdef HAVE_SYSTEMD
sd_notify(0, "WATCHDOG=1");
#endif /* HAVE_SYSTEMD */
if (std::fabs(timeDiff) > 15) { if (std::fabs(timeDiff) > 15) {
/* We made a significant jump in time. */ /* We made a significant jump in time. */
Log(LogInformation, "Application") 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..."); Log(LogInformation, "Application", "Shutting down...");
ConfigObject::StopObjects(); ConfigObject::StopObjects();

View File

@ -31,6 +31,10 @@
#include <unistd.h> #include <unistd.h>
#endif /* _WIN32 */ #endif /* _WIN32 */
#ifdef HAVE_SYSTEMD
#include <systemd/sd-daemon.h>
#endif /* HAVE_SYSTEMD */
using namespace icinga; using namespace icinga;
namespace po = boost::program_options; 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) static pid_t StartUnixWorker(const std::vector<std::string>& configs)
{ {
try { try {
@ -383,11 +401,18 @@ static pid_t StartUnixWorker(const std::vector<std::string>& configs)
(void)sigprocmask(SIG_UNBLOCK, &l_UnixWorkerSignals, nullptr); (void)sigprocmask(SIG_UNBLOCK, &l_UnixWorkerSignals, nullptr);
for (;;) { for (;;) {
#ifdef HAVE_SYSTEMD
NotifyWatchdog();
#endif /* HAVE_SYSTEMD */
switch (l_CurrentlyStartingUnixWorkerState.load()) { switch (l_CurrentlyStartingUnixWorkerState.load()) {
case UnixWorkerState::LoadedConfig: case UnixWorkerState::LoadedConfig:
break; break;
case UnixWorkerState::Failed: case UnixWorkerState::Failed:
while (waitpid(pid, nullptr, 0) == -1 && errno == EINTR) { while (waitpid(pid, nullptr, 0) == -1 && errno == EINTR) {
#ifdef HAVE_SYSTEMD
NotifyWatchdog();
#endif /* HAVE_SYSTEMD */
} }
pid = -1; pid = -1;
break; break;
@ -533,34 +558,69 @@ int DaemonCommand::Run(const po::variables_map& vm, const std::vector<std::strin
(void)kill(currentWorker, SIGUSR2); (void)kill(currentWorker, SIGUSR2);
#ifdef HAVE_SYSTEMD
sd_notify(0, "READY=1");
#endif /* HAVE_SYSTEMD */
bool requestedTermination = false; bool requestedTermination = false;
bool notifiedTermination = false;
for (;;) { for (;;) {
#ifdef HAVE_SYSTEMD
NotifyWatchdog();
#endif /* HAVE_SYSTEMD */
if (!requestedTermination) { if (!requestedTermination) {
int termSig = l_TermSignal.load(); int termSig = l_TermSignal.load();
if (termSig != -1) { if (termSig != -1) {
(void)kill(currentWorker, termSig); (void)kill(currentWorker, termSig);
requestedTermination = true; requestedTermination = true;
#ifdef HAVE_SYSTEMD
if (!notifiedTermination) {
notifiedTermination = true;
sd_notify(0, "STOPPING=1");
}
#endif /* HAVE_SYSTEMD */
} }
} }
if (l_RequestedReload.exchange(false)) { if (l_RequestedReload.exchange(false)) {
#ifdef HAVE_SYSTEMD
sd_notify(0, "RELOADING=1");
#endif /* HAVE_SYSTEMD */
pid_t nextWorker = StartUnixWorker(configs); pid_t nextWorker = StartUnixWorker(configs);
if (nextWorker != -1) { if (nextWorker != -1) {
(void)kill(currentWorker, SIGTERM); (void)kill(currentWorker, SIGTERM);
while (waitpid(currentWorker, nullptr, 0) == -1 && errno == EINTR) { while (waitpid(currentWorker, nullptr, 0) == -1 && errno == EINTR) {
#ifdef HAVE_SYSTEMD
NotifyWatchdog();
#endif /* HAVE_SYSTEMD */
} }
(void)kill(nextWorker, SIGUSR2); (void)kill(nextWorker, SIGUSR2);
currentWorker = nextWorker; currentWorker = nextWorker;
} }
#ifdef HAVE_SYSTEMD
sd_notify(0, "READY=1");
#endif /* HAVE_SYSTEMD */
} }
{ {
int status; int status;
if (waitpid(currentWorker, &status, WNOHANG) > 0) { 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); return WIFSIGNALED(status) ? 128 + WTERMSIG(status) : WEXITSTATUS(status);
} }
} }