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