Merge pull request #34686 from DaanDeMeyer/bus-fallback

Make sure bus_connect_transport_systemd() actually connects to the private manager bus
This commit is contained in:
Daan De Meyer
2024-10-11 15:16:54 +02:00
committed by GitHub
4 changed files with 39 additions and 32 deletions
@@ -0,0 +1,3 @@
# SPDX-License-Identifier: LGPL-2.1-or-later
z! /home/testuser 700 testuser testuser
+9 -10
View File
@@ -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();
}
+2 -2
View File
@@ -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;
+25 -20
View File
@@ -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;
}
}