dryang / rpms / systemd

Forked from rpms/systemd a year ago
Clone
923a60
From 8946c35e525c2a14b12ed425f11af152e37d8583 Mon Sep 17 00:00:00 2001
923a60
From: Michal Sekletar <msekleta@redhat.com>
923a60
Date: Fri, 10 Apr 2015 15:56:52 +0200
923a60
Subject: [PATCH] bus-util: be more verbose if dbus job fails
923a60
923a60
Users might have hard time figuring out why exactly their systemctl request
923a60
failed. If dbus job fails try to figure out more details about failure by
923a60
examining Result property of the service.
923a60
923a60
https://bugzilla.redhat.com/show_bug.cgi?id=1016680
923a60
923a60
Cherry-picked from: d5cad22109749faffb7563e4b2a3a728486d47b5
923a60
Resolves: #1016680
923a60
---
923a60
 src/libsystemd/sd-bus/bus-util.c | 79 +++++++++++++++++++++++++++++---
923a60
 1 file changed, 72 insertions(+), 7 deletions(-)
923a60
923a60
diff --git a/src/libsystemd/sd-bus/bus-util.c b/src/libsystemd/sd-bus/bus-util.c
923a60
index b0a5a7592a..2e6d889620 100644
923a60
--- a/src/libsystemd/sd-bus/bus-util.c
923a60
+++ b/src/libsystemd/sd-bus/bus-util.c
923a60
@@ -30,6 +30,7 @@
923a60
 #include "path-util.h"
923a60
 #include "missing.h"
923a60
 #include "set.h"
923a60
+#include "unit-name.h"
923a60
 
923a60
 #include "sd-bus.h"
923a60
 #include "bus-error.h"
923a60
@@ -1690,6 +1691,68 @@ static int bus_process_wait(sd_bus *bus) {
923a60
         }
923a60
 }
923a60
 
923a60
+static int bus_job_get_service_result(BusWaitForJobs *d, char **result) {
923a60
+        _cleanup_free_ char *dbus_path = NULL;
923a60
+
923a60
+        assert(d);
923a60
+        assert(d->name);
923a60
+        assert(result);
923a60
+
923a60
+        dbus_path = unit_dbus_path_from_name(d->name);
923a60
+        if (!dbus_path)
923a60
+                return -ENOMEM;
923a60
+
923a60
+        return sd_bus_get_property_string(d->bus,
923a60
+                                          "org.freedesktop.systemd1",
923a60
+                                          dbus_path,
923a60
+                                          "org.freedesktop.systemd1.Service",
923a60
+                                          "Result",
923a60
+                                          NULL,
923a60
+                                          result);
923a60
+}
923a60
+
923a60
+static const struct {
923a60
+        const char *result, *explanation;
923a60
+} explanations [] = {
923a60
+        { "resources", "configured resource limit was exceeded" },
923a60
+        { "timeout", "timeout was exceeded" },
923a60
+        { "exit-code", "control process exited with error code" },
923a60
+        { "signal", "fatal signal was delivered to the control process" },
923a60
+        { "core-dump", "fatal signal was delivered to the control process. Core dumped" },
923a60
+        { "watchdog", "service failed to send watchdog ping" },
923a60
+        { "start-limit", "start of the service was attempted too often too quickly" }
923a60
+};
923a60
+
923a60
+static void log_job_error_with_service_result(const char* service, const char *result) {
923a60
+        unsigned i;
923a60
+        _cleanup_free_ char *service_shell_quoted = NULL;
923a60
+
923a60
+        assert(service);
923a60
+        assert(result);
923a60
+
923a60
+        service_shell_quoted = shell_maybe_quote(service);
923a60
+
923a60
+        for (i = 0; i < ELEMENTSOF(explanations); ++i)
923a60
+                if (streq(result, explanations[i].result))
923a60
+                        break;
923a60
+
923a60
+        if (i < ELEMENTSOF(explanations))
923a60
+                log_error("Job for %s failed because %s. See \"systemctl status %s\" and \"journalctl -xe\" for details.\n",
923a60
+                          service,
923a60
+                          explanations[i].explanation,
923a60
+                          strna(service_shell_quoted));
923a60
+        else
923a60
+                log_error("Job for %s failed. See \"systemctl status %s\" and \"journalctl -xe\" for details.\n",
923a60
+                          service,
923a60
+                          strna(service_shell_quoted));
923a60
+
923a60
+        /* For some results maybe additional explanation is required */
923a60
+        if (streq_ptr(result, "start-limit"))
923a60
+                log_info("To force a start please invoke \"systemctl reset-failed %s\" followed by \"systemctl start %s\" again.",
923a60
+                         strna(service_shell_quoted),
923a60
+                         strna(service_shell_quoted));
923a60
+}
923a60
+
923a60
 static int check_wait_response(BusWaitForJobs *d, bool quiet) {
923a60
         int r = 0;
923a60
 
923a60
@@ -1709,15 +1772,17 @@ static int check_wait_response(BusWaitForJobs *d, bool quiet) {
923a60
                 else if (streq(d->result, "unsupported"))
923a60
                         log_error("Operation on or unit type of %s not supported on this system.", strna(d->name));
923a60
                 else if (!streq(d->result, "done") && !streq(d->result, "skipped")) {
923a60
-                        _cleanup_free_ char *quoted = NULL;
923a60
+                        if (d->name) {
923a60
+                                int q;
923a60
+                                _cleanup_free_ char *result = NULL;
923a60
 
923a60
-                        if (d->name)
923a60
-                                quoted = shell_maybe_quote(d->name);
923a60
+                                q = bus_job_get_service_result(d, &result);
923a60
+                                if (q < 0)
923a60
+                                        log_debug_errno(q, "Failed to get Result property of service %s: %m", d->name);
923a60
 
923a60
-                        if (quoted)
923a60
-                                log_error("Job for %s failed. See 'systemctl status %s' and 'journalctl -xe' for details.", d->name, quoted);
923a60
-                        else
923a60
-                                log_error("Job failed. See 'journalctl -xe' for details.");
923a60
+                                log_job_error_with_service_result(d->name, result);
923a60
+                        } else
923a60
+                                log_error("Job failed. See \"journalctl -xe\" for details.");
923a60
                 }
923a60
         }
923a60