923a60
From ac8fd4f713c1861e8a62fd811b2e79acbee5db31 Mon Sep 17 00:00:00 2001
923a60
From: Michal Sekletar <msekleta@redhat.com>
923a60
Date: Wed, 10 Jan 2018 17:22:12 +0100
923a60
Subject: [PATCH] dbus: propagate errors from bus_init_system() and
923a60
 bus_init_api()
923a60
923a60
The aim of this change is to make sure that we properly log about all
923a60
D-Bus connection problems. After all, we only ever attempt to get on the
923a60
bus if dbus-daemon is around, so any failure in the process should be
923a60
treated as an error.
923a60
923a60
bus_init_system() is only called from bus_init() and in
923a60
bus_init() we have a bool flag which governs whether we should attempt
923a60
to connect to the system bus or not.
923a60
Hence if we are in bus_init_system() then it is clear we got called from
923a60
a context where connection to the bus is actually required and therefore
923a60
shouldn't be treated as the "best effort" type of operation. Same
923a60
applies to bus_init_api().
923a60
923a60
We make use of those error codes in bus_init() and log high level
923a60
message that informs admin about what is going on (and is easy to spot
923a60
and makes sense to an end user).
923a60
923a60
Also "retrying later" bit is actually a lie. We won't retry unless we
923a60
are explicitly told to reconnect via SIGUSR1 or re-executed. This is
923a60
because bus_init() is always called from the context where dbus-daemon
923a60
is already around and hence bus_init() won't be called again from
923a60
unit_notify().
923a60
923a60
Fixes #7782
923a60
923a60
(cherry picked from commit dc7118ba094415d8de3812881cc5cbe2e3cac73e)
923a60
923a60
Resolves: #1541061
923a60
---
923a60
 src/core/dbus.c | 46 +++++++++++++++++-----------------------------
923a60
 1 file changed, 17 insertions(+), 29 deletions(-)
923a60
923a60
diff --git a/src/core/dbus.c b/src/core/dbus.c
923a60
index 0061211faa..d551eab016 100644
923a60
--- a/src/core/dbus.c
923a60
+++ b/src/core/dbus.c
923a60
@@ -811,27 +811,21 @@ static int bus_init_api(Manager *m) {
923a60
                 else
923a60
                         r = sd_bus_open_user(&bus;;
923a60
 
923a60
-                if (r < 0) {
923a60
-                        log_debug("Failed to connect to API bus, retrying later...");
923a60
-                        return 0;
923a60
-                }
923a60
+                if (r < 0)
923a60
+                        return log_error_errno(r, "Failed to connect to API bus: %m");
923a60
 
923a60
                 r = sd_bus_attach_event(bus, m->event, SD_EVENT_PRIORITY_NORMAL);
923a60
-                if (r < 0) {
923a60
-                        log_error_errno(r, "Failed to attach API bus to event loop: %m");
923a60
-                        return 0;
923a60
-                }
923a60
+                if (r < 0)
923a60
+                        return log_error_errno(r, "Failed to attach API bus to event loop: %m");
923a60
 
923a60
                 r = bus_setup_disconnected_match(m, bus);
923a60
                 if (r < 0)
923a60
-                        return 0;
923a60
+                        return r;
923a60
         }
923a60
 
923a60
         r = bus_setup_api(m, bus);
923a60
-        if (r < 0) {
923a60
-                log_error_errno(r, "Failed to set up API bus: %m");
923a60
-                return 0;
923a60
-        }
923a60
+        if (r < 0)
923a60
+                return log_error_errno(r, "Failed to set up API bus: %m");
923a60
 
923a60
         m->api_bus = bus;
923a60
         bus = NULL;
923a60
@@ -880,26 +874,20 @@ static int bus_init_system(Manager *m) {
923a60
         }
923a60
 
923a60
         r = sd_bus_open_system(&bus;;
923a60
-        if (r < 0) {
923a60
-                log_debug("Failed to connect to system bus, retrying later...");
923a60
-                return 0;
923a60
-        }
923a60
+        if (r < 0)
923a60
+                return log_error_errno(r, "Failed to connect to system bus: %m");
923a60
 
923a60
         r = bus_setup_disconnected_match(m, bus);
923a60
         if (r < 0)
923a60
-                return 0;
923a60
+                return r;
923a60
 
923a60
         r = sd_bus_attach_event(bus, m->event, SD_EVENT_PRIORITY_NORMAL);
923a60
-        if (r < 0) {
923a60
-                log_error_errno(r, "Failed to attach system bus to event loop: %m");
923a60
-                return 0;
923a60
-        }
923a60
+        if (r < 0)
923a60
+                return log_error_errno(r, "Failed to attach system bus to event loop: %m");
923a60
 
923a60
         r = bus_setup_system(m, bus);
923a60
-        if (r < 0) {
923a60
-                log_error_errno(r, "Failed to set up system bus: %m");
923a60
-                return 0;
923a60
-        }
923a60
+        if (r < 0)
923a60
+                return log_error_errno(r, "Failed to set up system bus: %m");
923a60
 
923a60
         m->system_bus = bus;
923a60
         bus = NULL;
923a60
@@ -984,16 +972,16 @@ int bus_init(Manager *m, bool try_bus_connect) {
923a60
         if (try_bus_connect) {
923a60
                 r = bus_init_system(m);
923a60
                 if (r < 0)
923a60
-                        return r;
923a60
+                        return log_error_errno(r, "Failed to initialize D-Bus connection: %m");
923a60
 
923a60
                 r = bus_init_api(m);
923a60
                 if (r < 0)
923a60
-                        return r;
923a60
+                        return log_error_errno(r, "Error occured during D-Bus APIs initialization: %m");
923a60
         }
923a60
 
923a60
         r = bus_init_private(m);
923a60
         if (r < 0)
923a60
-                return r;
923a60
+                return log_error_errno(r, "Failed to create private D-Bus server: %m");
923a60
 
923a60
         return 0;
923a60
 }