daandemeyer / rpms / systemd

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