8be66a
From 7569756d005d4f780fffd2504eb6f461982833f2 Mon Sep 17 00:00:00 2001
8be66a
From: Lennart Poettering <lennart@poettering.net>
8be66a
Date: Sat, 13 Oct 2018 14:38:46 +0200
8be66a
Subject: [PATCH] systemctl: clean up start_unit_one() error handling
8be66a
8be66a
Let's split exit code handling in two: "r" is only used for errno-style
8be66a
errors, and "ret" is used for exit() codes. Then, let's use EXIT_SUCCESS
8be66a
for checking whether the latter is already used.
8be66a
8be66a
This way it should always be clear what kind of error we are processing,
8be66a
and when we propaate one into the other.
8be66a
8be66a
Moreover this allows us to drop "q" form all inner loops, avoiding
8be66a
confusion when to use "q" and when "r" to store received errors.
8be66a
8be66a
Fixes: #9704
8be66a
(cherry picked from commit 0e8d9c0c4d7e71487c486f626c59853cfb031d16)
8be66a
8be66a
Related: #846319
8be66a
---
8be66a
 src/systemctl/systemctl.c | 32 +++++++++++++++-----------------
8be66a
 1 file changed, 15 insertions(+), 17 deletions(-)
8be66a
8be66a
diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c
8be66a
index 1929692480..4af9deb98d 100644
8be66a
--- a/src/systemctl/systemctl.c
8be66a
+++ b/src/systemctl/systemctl.c
8be66a
@@ -3004,12 +3004,12 @@ static enum action verb_to_action(const char *verb) {
8be66a
 
8be66a
 static int start_unit(int argc, char *argv[], void *userdata) {
8be66a
         _cleanup_(bus_wait_for_jobs_freep) BusWaitForJobs *w = NULL;
8be66a
+        _cleanup_(wait_context_free) WaitContext wait_context = {};
8be66a
         const char *method, *mode, *one_name, *suffix = NULL;
8be66a
         _cleanup_strv_free_ char **names = NULL;
8be66a
+        int r, ret = EXIT_SUCCESS;
8be66a
         sd_bus *bus;
8be66a
-        _cleanup_(wait_context_free) WaitContext wait_context = {};
8be66a
         char **name;
8be66a
-        int r = 0;
8be66a
 
8be66a
         if (arg_wait && !STR_IN_SET(argv[0], "start", "restart")) {
8be66a
                 log_error("--wait may only be used with the 'start' or 'restart' commands.");
8be66a
@@ -3096,16 +3096,15 @@ static int start_unit(int argc, char *argv[], void *userdata) {
8be66a
 
8be66a
         STRV_FOREACH(name, names) {
8be66a
                 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
8be66a
-                int q;
8be66a
 
8be66a
-                q = start_unit_one(bus, method, *name, mode, &error, w, arg_wait ? &wait_context : NULL);
8be66a
-                if (r >= 0 && q < 0)
8be66a
-                        r = translate_bus_error_to_exit_status(q, &error);
8be66a
+                r = start_unit_one(bus, method, *name, mode, &error, w, arg_wait ? &wait_context : NULL);
8be66a
+                if (ret == EXIT_SUCCESS && r < 0)
8be66a
+                        ret = translate_bus_error_to_exit_status(r, &error);
8be66a
         }
8be66a
 
8be66a
         if (!arg_no_block) {
8be66a
-                int q, arg_count = 0;
8be66a
                 const char* extra_args[4] = {};
8be66a
+                int arg_count = 0;
8be66a
 
8be66a
                 if (arg_scope != UNIT_FILE_SYSTEM)
8be66a
                         extra_args[arg_count++] = "--user";
8be66a
@@ -3119,9 +3118,9 @@ static int start_unit(int argc, char *argv[], void *userdata) {
8be66a
                         extra_args[arg_count++] = arg_host;
8be66a
                 }
8be66a
 
8be66a
-                q = bus_wait_for_jobs(w, arg_quiet, extra_args);
8be66a
-                if (q < 0)
8be66a
-                        return q;
8be66a
+                r = bus_wait_for_jobs(w, arg_quiet, extra_args);
8be66a
+                if (r < 0)
8be66a
+                        return r;
8be66a
 
8be66a
                 /* When stopping units, warn if they can still be triggered by
8be66a
                  * another active unit (socket, path, timer) */
8be66a
@@ -3130,16 +3129,15 @@ static int start_unit(int argc, char *argv[], void *userdata) {
8be66a
                                 check_triggering_units(bus, *name);
8be66a
         }
8be66a
 
8be66a
-        if (r >= 0 && arg_wait && !set_isempty(wait_context.unit_paths)) {
8be66a
-                int q;
8be66a
-                q = sd_event_loop(wait_context.event);
8be66a
-                if (q < 0)
8be66a
-                        return log_error_errno(q, "Failed to run event loop: %m");
8be66a
+        if (ret == EXIT_SUCCESS && arg_wait && !set_isempty(wait_context.unit_paths)) {
8be66a
+                r = sd_event_loop(wait_context.event);
8be66a
+                if (r < 0)
8be66a
+                        return log_error_errno(r, "Failed to run event loop: %m");
8be66a
                 if (wait_context.any_failed)
8be66a
-                        r = EXIT_FAILURE;
8be66a
+                        ret = EXIT_FAILURE;
8be66a
         }
8be66a
 
8be66a
-        return r;
8be66a
+        return ret;
8be66a
 }
8be66a
 
8be66a
 #if ENABLE_LOGIND