daandemeyer / rpms / systemd

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