From 39c06ea84187cda78fdce843482765cdf52537b3 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Wed, 18 Dec 2024 22:27:29 +0900 Subject: [PATCH] update-utmp: do not give up if the first attempt at connecting bus failed MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Otherwise, the program exits with failure if the first attempt in run() failed: ``` Dec 18 20:27:37 systemd-update-utmp[254]: Bus n/a: changing state UNSET → OPENING Dec 18 20:27:37 systemd-update-utmp[254]: sd-bus: starting bus by connecting to /run/systemd/private... Dec 18 20:27:37 systemd-update-utmp[254]: Bus n/a: changing state OPENING → CLOSED Dec 18 20:27:37 systemd-update-utmp[254]: Failed to get D-Bus connection: Connection refused ``` (cherry picked from commit 85d040dabd2cc67c89b7ed6157429b8f6f2240f4) --- src/update-utmp/update-utmp.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/update-utmp/update-utmp.c b/src/update-utmp/update-utmp.c index e40843cf35..a10e6d478a 100644 --- a/src/update-utmp/update-utmp.c +++ b/src/update-utmp/update-utmp.c @@ -53,6 +53,12 @@ static int get_startup_monotonic_time(Context *c, usec_t *ret) { assert(c); assert(ret); + if (!c->bus) { + r = bus_connect_system_systemd(&c->bus); + if (r < 0) + return log_warning_errno(r, "Failed to get D-Bus connection, ignoring: %m"); + } + r = bus_get_property_trivial( c->bus, bus_systemd_mgr, @@ -94,10 +100,13 @@ static int get_current_runlevel(Context *c) { UINT64_C(100) * USEC_PER_MSEC + random_u64_range(UINT64_C(1900) * USEC_PER_MSEC * n_attempts / MAX_ATTEMPTS); (void) usleep_safe(usec); + } + if (!c->bus) { r = bus_connect_system_systemd(&c->bus); if (r == -ECONNREFUSED && n_attempts < 64) { - log_debug_errno(r, "Failed to reconnect to system bus, retrying after a slight delay: %m"); + log_debug_errno(r, "Failed to %s to system bus, retrying after a slight delay: %m", + n_attempts <= 1 ? "connect" : "reconnect"); continue; } if (r < 0) @@ -251,7 +260,6 @@ static int run(int argc, char *argv[]) { .audit_fd = -EBADF, #endif }; - int r; log_setup(); @@ -264,9 +272,6 @@ static int run(int argc, char *argv[]) { log_full_errno(IN_SET(errno, EAFNOSUPPORT, EPROTONOSUPPORT) ? LOG_DEBUG : LOG_WARNING, errno, "Failed to connect to audit log, ignoring: %m"); #endif - r = bus_connect_system_systemd(&c.bus); - if (r < 0) - return log_error_errno(r, "Failed to get D-Bus connection: %m"); return dispatch_verb(argc, argv, verbs, &c); }