From 7e3bfe3b4f81f06da7a623bc9a7643efff6ebab6 Mon Sep 17 00:00:00 2001 From: Arne Juul Date: Thu, 10 Sep 2020 09:45:44 +0000 Subject: run prepare_for_shutdown before sending any signals --- configd/src/apps/sentinel/config-handler.cpp | 5 +++++ configd/src/apps/sentinel/service.cpp | 29 +++++++++++++++------------- configd/src/apps/sentinel/service.h | 2 +- 3 files changed, 22 insertions(+), 14 deletions(-) (limited to 'configd') diff --git a/configd/src/apps/sentinel/config-handler.cpp b/configd/src/apps/sentinel/config-handler.cpp index d4600471904..15560daf435 100644 --- a/configd/src/apps/sentinel/config-handler.cpp +++ b/configd/src/apps/sentinel/config-handler.cpp @@ -57,6 +57,11 @@ ConfigHandler::~ConfigHandler() void ConfigHandler::terminateServices(bool catchable, bool printDebug) { + for (const auto & entry : _services) { + Service *service = entry.second.get(); + service->setAutomatic(false); + service->prepare_for_shutdown(); + } for (const auto & entry : _services) { Service *service = entry.second.get(); if (printDebug && service->isRunning()) { diff --git a/configd/src/apps/sentinel/service.cpp b/configd/src/apps/sentinel/service.cpp index fa5af578946..9c78894f1a7 100644 --- a/configd/src/apps/sentinel/service.cpp +++ b/configd/src/apps/sentinel/service.cpp @@ -87,14 +87,26 @@ Service::~Service() delete _config; } +void +Service::prepare_for_shutdown() +{ + auto cmd = _config->preShutdownCommand; + if (cmd.empty()) { + return; + } + if (_state == RUNNING) { + // only run this once, before signaling the service: + LOG(info, "prepare %s for shutdown: running %s", name().c_str(), cmd.c_str()); + runCommand(cmd); + } else { + LOG(info, "%s: not running, skipping preShutdownCommand(%s)", name().c_str(), cmd.c_str()); + } +} + int Service::terminate(bool catchable, bool dumpState) { if (isRunning()) { - if (_state == RUNNING) { - // only run this once, before signaling the service: - runPreShutdownCommand(); - } LOG(debug, "%s: terminate(%s)", name().c_str(), catchable ? "cleanly" : "NOW"); resetRestartPenalty(); kill(_pid, SIGCONT); // if it was stopped for some reason @@ -134,15 +146,6 @@ Service::terminate(bool catchable, bool dumpState) return 0; // Not running, so all is ok. } -void -Service::runPreShutdownCommand() -{ - if (_config->preShutdownCommand.length() > 0) { - LOG(debug, "%s: runPreShutdownCommand(%s)", name().c_str(), _config->preShutdownCommand.c_str()); - runCommand(_config->preShutdownCommand); - } -} - void Service::runCommand(const std::string & command) { diff --git a/configd/src/apps/sentinel/service.h b/configd/src/apps/sentinel/service.h index 03d0e5f0473..8ef9893a014 100644 --- a/configd/src/apps/sentinel/service.h +++ b/configd/src/apps/sentinel/service.h @@ -34,7 +34,6 @@ private: void runChild() __attribute__((noreturn)); void setState(ServiceState state); - void runPreShutdownCommand(); void runCommand(const std::string & command); const char *stateName(ServiceState state) const; @@ -52,6 +51,7 @@ public: StartMetrics &metrics); void reconfigure(const SentinelConfig::Service& config); int pid() const { return _pid; } + void prepare_for_shutdown(); int terminate(bool catchable, bool dumpState); int terminate() { return terminate(true, false); -- cgit v1.2.3