On interrupting start of scripted service, run stop script.
authorDavin McCall <davmac@davmac.org>
Thu, 15 Feb 2018 18:32:19 +0000 (18:32 +0000)
committerDavin McCall <davmac@davmac.org>
Thu, 15 Feb 2018 19:03:51 +0000 (19:03 +0000)
If a scripted service startup is interrupted via signal, run the stop
script in case cleanup is required.

src/proc-service.cc

index 49a1caec590fda133bc6adbc836a5e124b0e262f..07ef7ee9f2dc38ab841819819411823b1ca610c2 100644 (file)
@@ -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();