]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
tree-wide: introduce bus_error_is_connection() and use it where applicable
authorYu Watanabe <watanabe.yu+github@gmail.com>
Fri, 6 Jun 2025 12:42:01 +0000 (21:42 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Fri, 6 Jun 2025 12:42:25 +0000 (21:42 +0900)
src/login/logind-dbus.c
src/machine/machined-dbus.c
src/run/run.c
src/shared/bus-util.c
src/shared/bus-util.h
src/update-utmp/update-utmp.c

index e13b09112824537002dc755807967f6f96855180..22dd11190a3a4d33d2ec97bb0e9fbaaac0e682d0 100644 (file)
@@ -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))
index 954f20e4557c4585f2b003cf2a93a9ebe4cd0615..d64959dab71b150c585f2a20ac6e193e5264fa0a 100644 (file)
@@ -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))
index 27e09cd2db6d2993ddf479bd25fabf10d6ca0a49..02c3d8a8e82ca73a1d635d501356eba07f941b1b 100644 (file)
@@ -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);
index 89d96a4045c70b2b5c283457e8d80187c4fd3fac..2c1f531c6a7ddd1a0090ec5a75dfea1e15b72548 100644 (file)
@@ -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;
index 5f160ffcafd190bb4a3d20a82a3bed4d26132799..ffd8bd6e347c0bb78ca9bebf3afc59b681ca6890 100644 (file)
@@ -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);
 
index 914c5ea3223d9294feba1f3e33274f4587f39a9c..78c693012e21d6021c2d1c08988c051130a18bf2 100644 (file)
@@ -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));