From: Zbigniew Jędrzejewski-Szmek Date: Sat, 22 Aug 2020 16:48:03 +0000 (+0200) Subject: various: treat BUS_ERROR_NO_SUCH_UNIT the same as SD_BUS_ERROR_SERVICE_UNKNOWN X-Git-Tag: v247-rc1~376^2~2 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=73d3ac8e2440cda3b7f2310f329f0798de6c041c;p=thirdparty%2Fsystemd.git various: treat BUS_ERROR_NO_SUCH_UNIT the same as SD_BUS_ERROR_SERVICE_UNKNOWN We return BUS_ERROR_NO_SUCH_UNIT a.k.a. org.freedesktop.systemd1.NoSuchUnit in various places. In #16813: Aug 22 06:14:48 core sudo[2769199]: pam_systemd_home(sudo:account): Failed to query user record: Unit dbus-org.freedesktop.home1.service not found. Aug 22 06:14:48 core dbus-daemon[5311]: [system] Activation via systemd failed for unit 'dbus-org.freedesktop.home1.service': Unit dbus-org.freedesktop.home1.service not found. Aug 22 06:14:48 core dbus-daemon[5311]: [system] Activating via systemd: service name='org.freedesktop.home1' unit='dbus-org.freedesktop.home1.service' requested by ':1.6564' (uid=0 pid=2769199 comm="sudo su ") This particular error comes from bus_unit_validate_load_state() in pid1: case UNIT_NOT_FOUND: return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s not found.", u->id); It seems possible that we should return a different error, but it doesn't really matter: if we change pid1 to return a different error, we still need to handle BUS_ERROR_NO_SUCH_UNIT as in this patch to handle pid1 with current code. --- diff --git a/src/home/pam_systemd_home.c b/src/home/pam_systemd_home.c index 2c2c7a0819c..13b164ecfc0 100644 --- a/src/home/pam_systemd_home.c +++ b/src/home/pam_systemd_home.c @@ -7,6 +7,7 @@ #include "bus-common-errors.h" #include "bus-locator.h" +#include "bus-util.h" #include "errno-util.h" #include "fd-util.h" #include "home-util.h" @@ -153,8 +154,7 @@ static int acquire_user_record( r = bus_call_method(bus, bus_home_mgr, "GetUserRecordByName", &error, &reply, "s", username); if (r < 0) { - if (sd_bus_error_has_name(&error, SD_BUS_ERROR_SERVICE_UNKNOWN) || - sd_bus_error_has_name(&error, SD_BUS_ERROR_NAME_HAS_NO_OWNER)) { + if (bus_error_is_unknown_service(&error)) { pam_syslog(handle, LOG_DEBUG, "systemd-homed is not available: %s", bus_error_message(&error, r)); goto user_unknown; } diff --git a/src/shared/bus-polkit.c b/src/shared/bus-polkit.c index 9b0a4552be4..0722b1a118b 100644 --- a/src/shared/bus-polkit.c +++ b/src/shared/bus-polkit.c @@ -3,6 +3,7 @@ #include "bus-internal.h" #include "bus-message.h" #include "bus-polkit.h" +#include "bus-util.h" #include "strv.h" #include "user-util.h" @@ -123,7 +124,7 @@ int bus_test_polkit( r = sd_bus_call(call->bus, request, 0, ret_error, &reply); if (r < 0) { /* Treat no PK available as access denied */ - if (sd_bus_error_has_name(ret_error, SD_BUS_ERROR_SERVICE_UNKNOWN)) { + if (bus_error_is_unknown_service(ret_error)) { sd_bus_error_free(ret_error); return -EACCES; } @@ -296,8 +297,7 @@ int bus_verify_polkit_async( e = sd_bus_message_get_error(q->reply); /* Treat no PK available as access denied */ - if (sd_bus_error_has_name(e, SD_BUS_ERROR_SERVICE_UNKNOWN) || - sd_bus_error_has_name(e, SD_BUS_ERROR_NAME_HAS_NO_OWNER)) + if (bus_error_is_unknown_service(e)) return -EACCES; /* Copy error from polkit reply */ diff --git a/src/shared/bus-util.c b/src/shared/bus-util.c index 77c1c621827..9a306daa032 100644 --- a/src/shared/bus-util.c +++ b/src/shared/bus-util.c @@ -14,14 +14,13 @@ #include "sd-event.h" #include "sd-id128.h" -/* #include "alloc-util.h" */ +#include "bus-common-errors.h" #include "bus-internal.h" #include "bus-label.h" #include "bus-util.h" #include "path-util.h" #include "socket-util.h" #include "stdio-util.h" -/* #include "string-util.h" */ static int name_owner_change_callback(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) { sd_event *e = userdata; @@ -153,6 +152,13 @@ int bus_name_has_owner(sd_bus *c, const char *name, sd_bus_error *error) { return has_owner; } +bool bus_error_is_unknown_service(const sd_bus_error *error) { + return sd_bus_error_has_names(error, + SD_BUS_ERROR_SERVICE_UNKNOWN, + SD_BUS_ERROR_NAME_HAS_NO_OWNER, + BUS_ERROR_NO_SUCH_UNIT); +} + 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 d98e0040f3b..9b86f9526e0 100644 --- a/src/shared/bus-util.h +++ b/src/shared/bus-util.h @@ -28,6 +28,7 @@ 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); int bus_check_peercred(sd_bus *c); diff --git a/src/sleep/sleep.c b/src/sleep/sleep.c index 7029352ca5b..cf7e238ad4c 100644 --- a/src/sleep/sleep.c +++ b/src/sleep/sleep.c @@ -160,11 +160,8 @@ static int lock_all_homes(void) { r = sd_bus_call(bus, m, DEFAULT_TIMEOUT_USEC, &error, NULL); if (r < 0) { - if (sd_bus_error_has_name(&error, SD_BUS_ERROR_SERVICE_UNKNOWN) || - sd_bus_error_has_name(&error, SD_BUS_ERROR_NAME_HAS_NO_OWNER)) { - log_debug("systemd-homed is not running, skipping locking of home directories."); - return 0; - } + if (bus_error_is_unknown_service(&error)) + return log_debug("systemd-homed is not running, skipping locking of home directories."); return log_error_errno(r, "Failed to lock home directories: %s", bus_error_message(&error, r)); }