From ac004f4cc90112fec86dc966cb9ca435b95c9c81 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Fri, 6 Jun 2025 21:42:01 +0900 Subject: [PATCH] tree-wide: introduce bus_error_is_connection() and use it where applicable --- src/login/logind-dbus.c | 6 ++---- src/machine/machined-dbus.c | 6 ++---- src/run/run.c | 16 ++-------------- src/shared/bus-util.c | 7 +++++++ src/shared/bus-util.h | 2 ++ src/update-utmp/update-utmp.c | 5 +---- 6 files changed, 16 insertions(+), 26 deletions(-) diff --git a/src/login/logind-dbus.c b/src/login/logind-dbus.c index e13b0911282..22dd11190a3 100644 --- a/src/login/logind-dbus.c +++ b/src/login/logind-dbus.c @@ -4814,8 +4814,7 @@ int manager_unit_is_active(Manager *manager, const char *unit, sd_bus_error *ret if (r < 0) { /* systemd might have dropped off momentarily, let's * not make this an error */ - if (sd_bus_error_has_names(&error, SD_BUS_ERROR_NO_REPLY, - SD_BUS_ERROR_DISCONNECTED)) + if (bus_error_is_connection(&error)) return true; /* If the unit is already unloaded then it's not @@ -4853,8 +4852,7 @@ int manager_job_is_active(Manager *manager, const char *path, sd_bus_error *ret_ &reply, "s"); if (r < 0) { - if (sd_bus_error_has_names(&error, SD_BUS_ERROR_NO_REPLY, - SD_BUS_ERROR_DISCONNECTED)) + if (bus_error_is_connection(&error)) return true; if (sd_bus_error_has_name(&error, SD_BUS_ERROR_UNKNOWN_OBJECT)) diff --git a/src/machine/machined-dbus.c b/src/machine/machined-dbus.c index 954f20e4557..d64959dab71 100644 --- a/src/machine/machined-dbus.c +++ b/src/machine/machined-dbus.c @@ -1286,8 +1286,7 @@ int manager_unit_is_active(Manager *manager, const char *unit, sd_bus_error *ret &reply, "s"); if (r < 0) { - if (sd_bus_error_has_names(&error, SD_BUS_ERROR_NO_REPLY, - SD_BUS_ERROR_DISCONNECTED)) + if (bus_error_is_connection(&error)) return true; if (sd_bus_error_has_names(&error, BUS_ERROR_NO_SUCH_UNIT, @@ -1323,8 +1322,7 @@ int manager_job_is_active(Manager *manager, const char *path, sd_bus_error *rete &reply, "s"); if (r < 0) { - if (sd_bus_error_has_names(&error, SD_BUS_ERROR_NO_REPLY, - SD_BUS_ERROR_DISCONNECTED)) + if (bus_error_is_connection(&error)) return true; if (sd_bus_error_has_name(&error, SD_BUS_ERROR_UNKNOWN_OBJECT)) diff --git a/src/run/run.c b/src/run/run.c index 27e09cd2db6..02c3d8a8e82 100644 --- a/src/run/run.c +++ b/src/run/run.c @@ -1728,12 +1728,7 @@ static int run_context_reconnect(RunContext *c) { /* reply = */ NULL, NULL); if (r < 0) { /* Hmm, the service manager probably hasn't finished reexecution just yet? Try again later. */ - if (sd_bus_error_has_names(&error, - SD_BUS_ERROR_NO_REPLY, - SD_BUS_ERROR_DISCONNECTED, - SD_BUS_ERROR_TIMED_OUT, - SD_BUS_ERROR_SERVICE_UNKNOWN, - SD_BUS_ERROR_NAME_HAS_NO_OWNER)) + if (bus_error_is_connection(&error) || bus_error_is_unknown_service(&error)) goto retry_timer; if (sd_bus_error_has_name(&error, SD_BUS_ERROR_UNKNOWN_OBJECT)) @@ -1874,14 +1869,7 @@ static int run_context_update(RunContext *c) { if (r < 0) { /* If this is a connection error, then try to reconnect. This might be because the service * manager is being restarted. Handle this gracefully. */ - if (sd_bus_error_has_names( - &error, - SD_BUS_ERROR_NO_REPLY, - SD_BUS_ERROR_DISCONNECTED, - SD_BUS_ERROR_TIMED_OUT, - SD_BUS_ERROR_SERVICE_UNKNOWN, - SD_BUS_ERROR_NAME_HAS_NO_OWNER)) { - + if (bus_error_is_connection(&error) || bus_error_is_unknown_service(&error)) { log_info_errno(r, "Bus call failed due to connection problems. Trying to reconnect..."); /* Not propagating error, because we handled it already, by reconnecting. */ return run_context_reconnect(c); diff --git a/src/shared/bus-util.c b/src/shared/bus-util.c index 89d96a4045c..2c1f531c6a7 100644 --- a/src/shared/bus-util.c +++ b/src/shared/bus-util.c @@ -212,6 +212,13 @@ bool bus_error_is_unknown_service(const sd_bus_error *error) { BUS_ERROR_NO_SUCH_UNIT); } +bool bus_error_is_connection(const sd_bus_error *error) { + return sd_bus_error_has_names(error, + SD_BUS_ERROR_NO_REPLY, + SD_BUS_ERROR_DISCONNECTED, + SD_BUS_ERROR_TIMED_OUT); +} + int bus_check_peercred(sd_bus *c) { struct ucred ucred; int fd, r; diff --git a/src/shared/bus-util.h b/src/shared/bus-util.h index 5f160ffcafd..ffd8bd6e347 100644 --- a/src/shared/bus-util.h +++ b/src/shared/bus-util.h @@ -22,7 +22,9 @@ typedef bool (*check_idle_t)(void *userdata); int bus_event_loop_with_idle(sd_event *e, sd_bus *bus, const char *name, usec_t timeout, check_idle_t check_idle, void *userdata); int bus_name_has_owner(sd_bus *c, const char *name, sd_bus_error *error); + bool bus_error_is_unknown_service(const sd_bus_error *error); +bool bus_error_is_connection(const sd_bus_error *error); int bus_check_peercred(sd_bus *c); diff --git a/src/update-utmp/update-utmp.c b/src/update-utmp/update-utmp.c index 914c5ea3223..78c693012e2 100644 --- a/src/update-utmp/update-utmp.c +++ b/src/update-utmp/update-utmp.c @@ -115,10 +115,7 @@ static int get_current_runlevel(Context *c) { "ActiveState", &error, &state); - if ((r == -ENOTCONN || - sd_bus_error_has_names(&error, - SD_BUS_ERROR_NO_REPLY, - SD_BUS_ERROR_DISCONNECTED)) && + if ((r == -ENOTCONN || bus_error_is_connection(&error)) && n_attempts < MAX_ATTEMPTS) { log_debug_errno(r, "Failed to get state of %s, retrying after a slight delay: %s", e->special, bus_error_message(&error, r)); -- 2.47.3