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 */
#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();

View File

@ -31,6 +31,10 @@
#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);
}
}