dryang / rpms / systemd

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