tree-wide: introduce bus_error_is_connection() and use it where applicable

This commit is contained in:
Yu Watanabe
2025-06-06 21:42:01 +09:00
parent f2a8cc943d
commit ac004f4cc9
6 changed files with 16 additions and 26 deletions

View 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))

View 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))

View 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);

View 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;

View 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);

View 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));