From 8333305aca426d31f1c771c290cf916335fa57c4 Mon Sep 17 00:00:00 2001 From: Davin McCall Date: Thu, 15 Feb 2018 18:32:19 +0000 Subject: [PATCH] On interrupting start of scripted service, run stop script. If a scripted service startup is interrupted via signal, run the stop script in case cleanup is required. --- src/proc-service.cc | 54 ++++++++++++++++++++++----------------------- 1 file changed, 26 insertions(+), 28 deletions(-) diff --git a/src/proc-service.cc b/src/proc-service.cc index 49a1cae..07ef7ee 100644 --- a/src/proc-service.cc +++ b/src/proc-service.cc @@ -299,43 +299,41 @@ void scripted_service::handle_exit_status(int exit_status) noexcept if (service_state == service_state_t::STOPPING) { // We might be running the stop script, or we might be running the start script and have issued // a cancel order via SIGINT: - if (did_exit && WEXITSTATUS(exit_status) == 0) { - if (interrupting_start) { - interrupting_start = false; - // launch stop script: - bring_down(); - } - else { - // We were running the stop script and finished successfully + if (interrupting_start) { + // We issued a start interrupt, so we expected this failure: + if (did_exit && WEXITSTATUS(exit_status) != 0) { + log(loglevel_t::INFO, "Service ", get_name(), " start cancelled; exit code ", + WEXITSTATUS(exit_status)); + // Assume that a command terminating normally requires no cleanup: stopped(); } - } - else { - if (interrupting_start) { - // We issued a start interrupt, so we expected this failure: - if (did_exit) { - log(loglevel_t::INFO, "Service ", get_name(), " start cancelled; exit code ", - WEXITSTATUS(exit_status)); - } - else if (was_signalled) { + else { + if (was_signalled) { log(loglevel_t::INFO, "Service ", get_name(), " start cancelled from signal ", WTERMSIG(exit_status)); } + // If the start script completed successfully, or was interrupted via our signal, + // we want to run the stop script to clean up: + bring_down(); } - else { - // ??? failed to stop! Let's log it as warning: - if (did_exit) { - log(loglevel_t::WARN, "Service ", get_name(), " stop command failed with exit code ", - WEXITSTATUS(exit_status)); - } - else if (was_signalled) { - log(loglevel_t::WARN, "Service ", get_name(), " stop command terminated due to signal ", - WTERMSIG(exit_status)); - } + interrupting_start = false; + } + else if (did_exit && WEXITSTATUS(exit_status) == 0) { + // We were running the stop script and finished successfully + stopped(); + } + else { + // ??? failed to stop! Let's log it as warning: + if (did_exit) { + log(loglevel_t::WARN, "Service ", get_name(), " stop command failed with exit code ", + WEXITSTATUS(exit_status)); + } + else if (was_signalled) { + log(loglevel_t::WARN, "Service ", get_name(), " stop command terminated due to signal ", + WTERMSIG(exit_status)); } // Even if the stop script failed, assume that service is now stopped, so that any dependencies // can be stopped. There's not really any other useful course of action here. - interrupting_start = false; stopped(); } services->process_queues(); -- 2.25.1