21255d
From cacf7a619cdccd9a6da6c2fe7361eac121b9ea0b Mon Sep 17 00:00:00 2001
21255d
From: Lennart Poettering <lennart@poettering.net>
21255d
Date: Fri, 22 Mar 2019 20:58:13 +0100
21255d
Subject: [PATCH] systemctl: add new --show-transaction switch
21255d
21255d
This new switch uses the new method call EnqueueUnitJob() for enqueuing
21255d
a job and showing the jobs it enqueued.
21255d
21255d
Fixes: #2297
21255d
(cherry picked from commit 85d9b5981ba6b7ee3955f95fa6cf3bb8cdf3444d)
21255d
21255d
Resolves: #846319
21255d
---
21255d
 src/systemctl/systemctl.c | 183 ++++++++++++++++++++++++++------------
21255d
 1 file changed, 128 insertions(+), 55 deletions(-)
21255d
21255d
diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c
21255d
index e7a8fd559f..8bec798373 100644
21255d
--- a/src/systemctl/systemctl.c
21255d
+++ b/src/systemctl/systemctl.c
21255d
@@ -126,6 +126,7 @@ static bool arg_dry_run = false;
21255d
 static bool arg_quiet = false;
21255d
 static bool arg_full = false;
21255d
 static bool arg_recursive = false;
21255d
+static bool arg_show_transaction = false;
21255d
 static int arg_force = 0;
21255d
 static bool arg_ask_password = false;
21255d
 static bool arg_runtime = false;
21255d
@@ -734,7 +735,6 @@ static int get_unit_list_recursive(
21255d
                 *_machines = NULL;
21255d
 
21255d
         *_unit_infos = TAKE_PTR(unit_infos);
21255d
-
21255d
         *_replies = TAKE_PTR(replies);
21255d
 
21255d
         return c;
21255d
@@ -2688,25 +2688,26 @@ static int check_triggering_units(sd_bus *bus, const char *name) {
21255d
 }
21255d
 
21255d
 static const struct {
21255d
-        const char *verb;
21255d
-        const char *method;
21255d
+        const char *verb;      /* systemctl verb */
21255d
+        const char *method;    /* Name of the specific D-Bus method */
21255d
+        const char *job_type;  /* Job type when passing to the generic EnqueueUnitJob() method */
21255d
 } unit_actions[] = {
21255d
-        { "start",                 "StartUnit" },
21255d
-        { "stop",                  "StopUnit" },
21255d
-        { "condstop",              "StopUnit" },
21255d
-        { "reload",                "ReloadUnit" },
21255d
-        { "restart",               "RestartUnit" },
21255d
-        { "try-restart",           "TryRestartUnit" },
21255d
-        { "condrestart",           "TryRestartUnit" },
21255d
-        { "reload-or-restart",     "ReloadOrRestartUnit" },
21255d
-        { "try-reload-or-restart", "ReloadOrTryRestartUnit" },
21255d
-        { "reload-or-try-restart", "ReloadOrTryRestartUnit" },
21255d
-        { "condreload",            "ReloadOrTryRestartUnit" },
21255d
-        { "force-reload",          "ReloadOrTryRestartUnit" }
21255d
+        { "start",                 "StartUnit",              "start"                 },
21255d
+        { "stop",                  "StopUnit",               "stop"                  },
21255d
+        { "condstop",              "StopUnit",               "stop"                  }, /* legacy alias */
21255d
+        { "reload",                "ReloadUnit",             "reload"                },
21255d
+        { "restart",               "RestartUnit",            "restart"               },
21255d
+        { "try-restart",           "TryRestartUnit",         "try-restart"           },
21255d
+        { "condrestart",           "TryRestartUnit",         "try-restart"           }, /* legacy alias */
21255d
+        { "reload-or-restart",     "ReloadOrRestartUnit",    "reload-or-restart"     },
21255d
+        { "try-reload-or-restart", "ReloadOrTryRestartUnit", "reload-or-try-restart" },
21255d
+        { "reload-or-try-restart", "ReloadOrTryRestartUnit", "reload-or-try-restart" }, /* legacy alias */
21255d
+        { "condreload",            "ReloadOrTryRestartUnit", "reload-or-try-restart" }, /* legacy alias */
21255d
+        { "force-reload",          "ReloadOrTryRestartUnit", "reload-or-try-restart" }, /* legacy alias */
21255d
 };
21255d
 
21255d
 static const char *verb_to_method(const char *verb) {
21255d
-       uint i;
21255d
+       size_t i;
21255d
 
21255d
        for (i = 0; i < ELEMENTSOF(unit_actions); i++)
21255d
                 if (streq_ptr(unit_actions[i].verb, verb))
21255d
@@ -2715,14 +2716,14 @@ static const char *verb_to_method(const char *verb) {
21255d
        return "StartUnit";
21255d
 }
21255d
 
21255d
-static const char *method_to_verb(const char *method) {
21255d
-       uint i;
21255d
+static const char *verb_to_job_type(const char *verb) {
21255d
+       size_t i;
21255d
 
21255d
        for (i = 0; i < ELEMENTSOF(unit_actions); i++)
21255d
-                if (streq_ptr(unit_actions[i].method, method))
21255d
-                        return unit_actions[i].verb;
21255d
+                if (streq_ptr(unit_actions[i].verb, verb))
21255d
+                        return unit_actions[i].job_type;
21255d
 
21255d
-       return "n/a";
21255d
+       return "start";
21255d
 }
21255d
 
21255d
 typedef struct {
21255d
@@ -2805,7 +2806,8 @@ static int on_properties_changed(sd_bus_message *m, void *userdata, sd_bus_error
21255d
 
21255d
 static int start_unit_one(
21255d
                 sd_bus *bus,
21255d
-                const char *method,
21255d
+                const char *method,    /* When using classic per-job bus methods */
21255d
+                const char *job_type,  /* When using new-style EnqueueUnitJob() */
21255d
                 const char *name,
21255d
                 const char *mode,
21255d
                 sd_bus_error *error,
21255d
@@ -2814,6 +2816,7 @@ static int start_unit_one(
21255d
 
21255d
         _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
21255d
         const char *path;
21255d
+        bool done = false;
21255d
         int r;
21255d
 
21255d
         assert(method);
21255d
@@ -2859,44 +2862,81 @@ static int start_unit_one(
21255d
         log_debug("%s dbus call org.freedesktop.systemd1.Manager %s(%s, %s)",
21255d
                   arg_dry_run ? "Would execute" : "Executing",
21255d
                   method, name, mode);
21255d
+
21255d
         if (arg_dry_run)
21255d
                 return 0;
21255d
 
21255d
-        r = sd_bus_call_method(
21255d
-                        bus,
21255d
-                        "org.freedesktop.systemd1",
21255d
-                        "/org/freedesktop/systemd1",
21255d
-                        "org.freedesktop.systemd1.Manager",
21255d
-                        method,
21255d
-                        error,
21255d
-                        &reply,
21255d
-                        "ss", name, mode);
21255d
-        if (r < 0) {
21255d
-                const char *verb;
21255d
+        if (arg_show_transaction) {
21255d
+                _cleanup_(sd_bus_error_free) sd_bus_error enqueue_error = SD_BUS_ERROR_NULL;
21255d
 
21255d
-                /* There's always a fallback possible for legacy actions. */
21255d
-                if (arg_action != ACTION_SYSTEMCTL)
21255d
-                        return r;
21255d
+                /* Use the new, fancy EnqueueUnitJob() API if the user wants us to print the transaction */
21255d
+                r = sd_bus_call_method(
21255d
+                                bus,
21255d
+                                "org.freedesktop.systemd1",
21255d
+                                "/org/freedesktop/systemd1",
21255d
+                                "org.freedesktop.systemd1.Manager",
21255d
+                                "EnqueueUnitJob",
21255d
+                                &enqueue_error,
21255d
+                                &reply,
21255d
+                                "sss",
21255d
+                                name, job_type, mode);
21255d
+                if (r < 0) {
21255d
+                        if (!sd_bus_error_has_name(&enqueue_error, SD_BUS_ERROR_UNKNOWN_METHOD)) {
21255d
+                                (void) sd_bus_error_copy(error, &enqueue_error);
21255d
+                                sd_bus_error_free(&enqueue_error);
21255d
+                                goto fail;
21255d
+                        }
21255d
+
21255d
+                        /* Hmm, the API is not yet available. Let's use the classic API instead (see below). */
21255d
+                        log_notice("--show-transaction not supported by this service manager, proceeding without.");
21255d
+                } else {
21255d
+                        const char *u, *jt;
21255d
+                        uint32_t id;
21255d
 
21255d
-                verb = method_to_verb(method);
21255d
+                        r = sd_bus_message_read(reply, "uosos", &id, &path, &u, NULL, &jt;;
21255d
+                        if (r < 0)
21255d
+                                return bus_log_parse_error(r);
21255d
 
21255d
-                log_error("Failed to %s %s: %s", verb, name, bus_error_message(error, r));
21255d
+                        log_info("Enqueued anchor job %" PRIu32 " %s/%s.", id, u, jt);
21255d
 
21255d
-                if (!sd_bus_error_has_name(error, BUS_ERROR_NO_SUCH_UNIT) &&
21255d
-                    !sd_bus_error_has_name(error, BUS_ERROR_UNIT_MASKED) &&
21255d
-                    !sd_bus_error_has_name(error, BUS_ERROR_JOB_TYPE_NOT_APPLICABLE))
21255d
-                        log_error("See %s logs and 'systemctl%s status%s %s' for details.",
21255d
-                                   arg_scope == UNIT_FILE_SYSTEM ? "system" : "user",
21255d
-                                   arg_scope == UNIT_FILE_SYSTEM ? "" : " --user",
21255d
-                                   name[0] == '-' ? " --" : "",
21255d
-                                   name);
21255d
+                        r = sd_bus_message_enter_container(reply, 'a', "(uosos)");
21255d
+                        if (r < 0)
21255d
+                                return bus_log_parse_error(r);
21255d
+                        for (;;) {
21255d
+                                r = sd_bus_message_read(reply, "(uosos)", &id, NULL, &u, NULL, &jt;;
21255d
+                                if (r < 0)
21255d
+                                        return bus_log_parse_error(r);
21255d
+                                if (r == 0)
21255d
+                                        break;
21255d
 
21255d
-                return r;
21255d
+                                log_info("Enqueued auxiliary job %" PRIu32 " %s/%s.", id, u, jt);
21255d
+                        }
21255d
+
21255d
+                        r = sd_bus_message_exit_container(reply);
21255d
+                        if (r < 0)
21255d
+                                return bus_log_parse_error(r);
21255d
+
21255d
+                        done = true;
21255d
+                }
21255d
         }
21255d
 
21255d
-        r = sd_bus_message_read(reply, "o", &path);
21255d
-        if (r < 0)
21255d
-                return bus_log_parse_error(r);
21255d
+        if (!done) {
21255d
+                r = sd_bus_call_method(
21255d
+                                bus,
21255d
+                                "org.freedesktop.systemd1",
21255d
+                                "/org/freedesktop/systemd1",
21255d
+                                "org.freedesktop.systemd1.Manager",
21255d
+                                method,
21255d
+                                error,
21255d
+                                &reply,
21255d
+                                "ss", name, mode);
21255d
+                if (r < 0)
21255d
+                        goto fail;
21255d
+
21255d
+                r = sd_bus_message_read(reply, "o", &path);
21255d
+                if (r < 0)
21255d
+                        return bus_log_parse_error(r);
21255d
+        }
21255d
 
21255d
         if (need_daemon_reload(bus, name) > 0)
21255d
                 warn_unit_file_changed(name);
21255d
@@ -2909,6 +2949,24 @@ static int start_unit_one(
21255d
         }
21255d
 
21255d
         return 0;
21255d
+
21255d
+fail:
21255d
+        /* There's always a fallback possible for legacy actions. */
21255d
+        if (arg_action != ACTION_SYSTEMCTL)
21255d
+                return r;
21255d
+
21255d
+        log_error_errno(r, "Failed to %s %s: %s", job_type, name, bus_error_message(error, r));
21255d
+
21255d
+        if (!sd_bus_error_has_name(error, BUS_ERROR_NO_SUCH_UNIT) &&
21255d
+            !sd_bus_error_has_name(error, BUS_ERROR_UNIT_MASKED) &&
21255d
+            !sd_bus_error_has_name(error, BUS_ERROR_JOB_TYPE_NOT_APPLICABLE))
21255d
+                log_error("See %s logs and 'systemctl%s status%s %s' for details.",
21255d
+                          arg_scope == UNIT_FILE_SYSTEM ? "system" : "user",
21255d
+                          arg_scope == UNIT_FILE_SYSTEM ? "" : " --user",
21255d
+                          name[0] == '-' ? " --" : "",
21255d
+                          name);
21255d
+
21255d
+        return r;
21255d
 }
21255d
 
21255d
 static int expand_names(sd_bus *bus, char **names, const char* suffix, char ***ret) {
21255d
@@ -2965,7 +3023,6 @@ static int expand_names(sd_bus *bus, char **names, const char* suffix, char ***r
21255d
         }
21255d
 
21255d
         *ret = TAKE_PTR(mangled);
21255d
-
21255d
         return 0;
21255d
 }
21255d
 
21255d
@@ -3024,7 +3081,7 @@ static const char** make_extra_args(const char *extra_args[static 4]) {
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
+        const char *method, *job_type, *mode, *one_name, *suffix = NULL;
21255d
         _cleanup_strv_free_ char **names = NULL;
21255d
         int r, ret = EXIT_SUCCESS;
21255d
         sd_bus *bus;
21255d
@@ -3050,27 +3107,34 @@ static int start_unit(int argc, char *argv[], void *userdata) {
21255d
                 action = verb_to_action(argv[0]);
21255d
 
21255d
                 if (action != _ACTION_INVALID) {
21255d
+                        /* A command in style "systemctl reboot", "systemctl poweroff", … */
21255d
                         method = "StartUnit";
21255d
+                        job_type = "start";
21255d
                         mode = action_table[action].mode;
21255d
                         one_name = action_table[action].target;
21255d
                 } else {
21255d
                         if (streq(argv[0], "isolate")) {
21255d
+                                /* A "systemctl isolate <unit1> <unit2> …" command */
21255d
                                 method = "StartUnit";
21255d
+                                job_type = "start";
21255d
                                 mode = "isolate";
21255d
-
21255d
                                 suffix = ".target";
21255d
                         } else {
21255d
+                                /* A command in style of "systemctl start <unit1> <unit2> …", "sysemctl stop <unit1> <unit2> …" and so on */
21255d
                                 method = verb_to_method(argv[0]);
21255d
+                                job_type = verb_to_job_type(argv[0]);
21255d
                                 mode = arg_job_mode;
21255d
                         }
21255d
                         one_name = NULL;
21255d
                 }
21255d
         } else {
21255d
+                /* A SysV legacy command such as "halt", "reboot", "poweroff", … */
21255d
                 assert(arg_action >= 0 && arg_action < _ACTION_MAX);
21255d
                 assert(action_table[arg_action].target);
21255d
                 assert(action_table[arg_action].mode);
21255d
 
21255d
                 method = "StartUnit";
21255d
+                job_type = "start";
21255d
                 mode = action_table[arg_action].mode;
21255d
                 one_name = action_table[arg_action].target;
21255d
         }
21255d
@@ -3105,9 +3169,11 @@ static int start_unit(int argc, char *argv[], void *userdata) {
21255d
                                 NULL);
21255d
                 if (r < 0)
21255d
                         return log_error_errno(r, "Failed to enable subscription: %m");
21255d
+
21255d
                 r = sd_event_default(&wait_context.event);
21255d
                 if (r < 0)
21255d
                         return log_error_errno(r, "Failed to allocate event loop: %m");
21255d
+
21255d
                 r = sd_bus_attach_event(bus, wait_context.event, 0);
21255d
                 if (r < 0)
21255d
                         return log_error_errno(r, "Failed to attach bus to event loop: %m");
21255d
@@ -3116,7 +3182,7 @@ static int start_unit(int argc, char *argv[], void *userdata) {
21255d
         STRV_FOREACH(name, names) {
21255d
                 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
21255d
 
21255d
-                r = start_unit_one(bus, method, *name, mode, &error, w, arg_wait ? &wait_context : NULL);
21255d
+                r = start_unit_one(bus, method, job_type, *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
@@ -7167,6 +7233,8 @@ static void systemctl_help(void) {
21255d
                "     --reverse        Show reverse dependencies with 'list-dependencies'\n"
21255d
                "     --job-mode=MODE  Specify how to deal with already queued jobs, when\n"
21255d
                "                      queueing a new job\n"
21255d
+               "  -T --show-transaction\n"
21255d
+               "                      When enqueuing a unit job, show full transaction\n"
21255d
                "     --show-types     When showing sockets, explicitly show their type\n"
21255d
                "     --value          When showing properties, only print the value\n"
21255d
                "  -i --ignore-inhibitors\n"
21255d
@@ -7482,6 +7550,7 @@ static int systemctl_parse_argv(int argc, char *argv[]) {
21255d
                 { "firmware-setup",      no_argument,       NULL, ARG_FIRMWARE_SETUP      },
21255d
                 { "now",                 no_argument,       NULL, ARG_NOW                 },
21255d
                 { "message",             required_argument, NULL, ARG_MESSAGE             },
21255d
+                { "show-transaction",    no_argument,       NULL, 'T'                     },
21255d
                 {}
21255d
         };
21255d
 
21255d
@@ -7494,7 +7563,7 @@ static int systemctl_parse_argv(int argc, char *argv[]) {
21255d
         /* we default to allowing interactive authorization only in systemctl (not in the legacy commands) */
21255d
         arg_ask_password = true;
21255d
 
21255d
-        while ((c = getopt_long(argc, argv, "ht:p:alqfs:H:M:n:o:ir", options, NULL)) >= 0)
21255d
+        while ((c = getopt_long(argc, argv, "ht:p:alqfs:H:M:n:o:iTr", options, NULL)) >= 0)
21255d
 
21255d
                 switch (c) {
21255d
 
21255d
@@ -7815,6 +7884,10 @@ static int systemctl_parse_argv(int argc, char *argv[]) {
21255d
                                 return log_oom();
21255d
                         break;
21255d
 
21255d
+                case 'T':
21255d
+                        arg_show_transaction = true;
21255d
+                        break;
21255d
+
21255d
                 case '?':
21255d
                         return -EINVAL;
21255d