diff options
author | Daan De Meyer <daan.j.demeyer@gmail.com> | 2024-10-11 15:16:54 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-10-11 15:16:54 +0200 |
commit | 051441e559afa6c81a028da5d26b112d9a60b9d8 (patch) | |
tree | 2f3fbd41e39955a97f25dd66f09b4e4467bf113f /src | |
parent | Merge pull request #34718 from poettering/efi-smbios-tweak (diff) | |
parent | stdio-bridge: Use customized log message for forwarding bus (diff) | |
download | systemd-051441e559afa6c81a028da5d26b112d9a60b9d8.tar.xz systemd-051441e559afa6c81a028da5d26b112d9a60b9d8.zip |
Merge pull request #34686 from DaanDeMeyer/bus-fallback
Make sure bus_connect_transport_systemd() actually connects to the private manager bus
Diffstat (limited to 'src')
-rw-r--r-- | src/shared/bus-util.c | 19 | ||||
-rw-r--r-- | src/stdio-bridge/stdio-bridge.c | 4 | ||||
-rw-r--r-- | src/update-utmp/update-utmp.c | 45 |
3 files changed, 36 insertions, 32 deletions
diff --git a/src/shared/bus-util.c b/src/shared/bus-util.c index f4c4eed707..a196ba47f6 100644 --- a/src/shared/bus-util.c +++ b/src/shared/bus-util.c @@ -229,12 +229,6 @@ int bus_connect_system_systemd(sd_bus **ret_bus) { assert(ret_bus); - if (geteuid() != 0) - return sd_bus_default_system(ret_bus); - - /* If we are root then let's talk directly to the system - * instance, instead of going via the bus */ - r = sd_bus_new(&bus); if (r < 0) return r; @@ -245,7 +239,7 @@ int bus_connect_system_systemd(sd_bus **ret_bus) { r = sd_bus_start(bus); if (r < 0) - return sd_bus_default_system(ret_bus); + return r; r = bus_check_peercred(bus); if (r < 0) @@ -265,7 +259,7 @@ int bus_connect_user_systemd(sd_bus **ret_bus) { e = secure_getenv("XDG_RUNTIME_DIR"); if (!e) - return sd_bus_default_user(ret_bus); + return -ENXIO; ee = bus_address_escape(e); if (!ee) @@ -281,7 +275,7 @@ int bus_connect_user_systemd(sd_bus **ret_bus) { r = sd_bus_start(bus); if (r < 0) - return sd_bus_default_user(ret_bus); + return r; r = bus_check_peercred(bus); if (r < 0) @@ -521,8 +515,13 @@ int bus_connect_transport_systemd( /* Print a friendly message when the local system is actually not running systemd as PID 1. */ return log_error_errno(SYNTHETIC_ERRNO(EHOSTDOWN), "System has not been booted with systemd as init system (PID 1). Can't operate."); - return bus_connect_system_systemd(ret_bus); + if (geteuid() == 0) + /* If we are root then let's talk directly to the system + * instance, instead of going via the bus. */ + return bus_connect_system_systemd(ret_bus); + + return sd_bus_default_system(ret_bus); default: assert_not_reached(); } diff --git a/src/stdio-bridge/stdio-bridge.c b/src/stdio-bridge/stdio-bridge.c index d3629f5fb0..22570511cb 100644 --- a/src/stdio-bridge/stdio-bridge.c +++ b/src/stdio-bridge/stdio-bridge.c @@ -142,7 +142,7 @@ static int run(int argc, char *argv[]) { r = sd_bus_start(a); if (r < 0) - return log_error_errno(r, "Failed to start bus client: %m"); + return bus_log_connect_error(r, arg_transport, arg_runtime_scope); r = sd_bus_get_bus_id(a, &server_id); if (r < 0) @@ -170,7 +170,7 @@ static int run(int argc, char *argv[]) { r = sd_bus_start(b); if (r < 0) - return log_error_errno(r, "Failed to start bus client: %m"); + return log_error_errno(r, "Failed to start bus forwarding server: %m"); for (;;) { _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; diff --git a/src/update-utmp/update-utmp.c b/src/update-utmp/update-utmp.c index c376676e8d..7a8a53f7e8 100644 --- a/src/update-utmp/update-utmp.c +++ b/src/update-utmp/update-utmp.c @@ -82,6 +82,25 @@ static int get_current_runlevel(Context *c) { assert(c); for (unsigned n_attempts = 0;;) { + if (n_attempts++ > 0) { + /* systemd might have dropped off momentarily, let's not make this an error, + * and wait some random time. Let's pick a random time in the range 0ms…250ms, + * linearly scaled by the number of failed attempts. */ + c->bus = sd_bus_flush_close_unref(c->bus); + + usec_t usec = random_u64_range(UINT64_C(10) * USEC_PER_MSEC + + UINT64_C(240) * USEC_PER_MSEC * n_attempts/64); + (void) usleep_safe(usec); + + 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"); + continue; + } + if (r < 0) + return log_error_errno(r, "Failed to reconnect to system bus: %m"); + } + FOREACH_ELEMENT(e, table) { _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; _cleanup_free_ char *state = NULL, *path = NULL; @@ -102,18 +121,10 @@ static int get_current_runlevel(Context *c) { sd_bus_error_has_names(&error, SD_BUS_ERROR_NO_REPLY, SD_BUS_ERROR_DISCONNECTED)) && - ++n_attempts < 64) { - - /* systemd might have dropped off momentarily, let's not make this an error, - * and wait some random time. Let's pick a random time in the range 0ms…250ms, - * linearly scaled by the number of failed attempts. */ - - usec_t usec = random_u64_range(UINT64_C(10) * USEC_PER_MSEC + - UINT64_C(240) * USEC_PER_MSEC * n_attempts/64); - log_debug_errno(r, "Failed to get state of %s, retrying after %s: %s", - e->special, FORMAT_TIMESPAN(usec, USEC_PER_MSEC), bus_error_message(&error, r)); - (void) usleep_safe(usec); - goto reconnect; + n_attempts < 64) { + log_debug_errno(r, "Failed to get state of %s, retrying after a slight delay: %s", + e->special, bus_error_message(&error, r)); + break; } if (r < 0) return log_warning_errno(r, "Failed to get state of %s: %s", e->special, bus_error_message(&error, r)); @@ -121,14 +132,8 @@ static int get_current_runlevel(Context *c) { if (STR_IN_SET(state, "active", "reloading")) return e->runlevel; } - - return 0; - -reconnect: - c->bus = sd_bus_flush_close_unref(c->bus); - r = bus_connect_system_systemd(&c->bus); - if (r < 0) - return log_error_errno(r, "Failed to reconnect to system bus: %m"); + if (r >= 0) + return 0; } } |