From: Yu Watanabe Date: Tue, 13 May 2025 14:39:09 +0000 (+0900) Subject: device-util: introduce device_get_seat() helper function X-Git-Tag: v258-rc1~625^2~5 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=2012d6d74e79ddf46686234eabc50edad619d4df;p=thirdparty%2Fsystemd.git device-util: introduce device_get_seat() helper function --- diff --git a/src/home/homed-home.c b/src/home/homed-home.c index da5e304f1b1..58a8ffff833 100644 --- a/src/home/homed-home.c +++ b/src/home/homed-home.c @@ -9,6 +9,7 @@ #include "build-path.h" #include "bus-common-errors.h" #include "bus-locator.h" +#include "device-util.h" #include "env-util.h" #include "errno-list.h" #include "errno-util.h" @@ -3206,10 +3207,8 @@ static int home_get_image_path_seat(Home *h, char **ret) { if (r < 0) return r; - r = sd_device_get_property_value(d, "ID_SEAT", &seat); - if (r == -ENOENT) /* no property means seat0 */ - seat = "seat0"; - else if (r < 0) + r = device_get_seat(d, &seat); + if (r < 0) return r; return strdup_to(ret, seat); diff --git a/src/libsystemd/sd-device/device-util.c b/src/libsystemd/sd-device/device-util.c index 67bcbf8af52..5169c24d848 100644 --- a/src/libsystemd/sd-device/device-util.c +++ b/src/libsystemd/sd-device/device-util.c @@ -191,6 +191,21 @@ int device_sysname_startswith_strv(sd_device *device, char * const *prefixes, co return !!suffix; } +int device_get_seat(sd_device *device, const char **ret) { + const char *seat = NULL; + int r; + + assert(device); + assert(ret); + + r = sd_device_get_property_value(device, "ID_SEAT", &seat); + if (r < 0 && r != -ENOENT) + return r; + + *ret = isempty(seat) ? "seat0" : seat; + return 0; +} + bool device_property_can_set(const char *property) { return property && !STR_IN_SET(property, diff --git a/src/libsystemd/sd-device/device-util.h b/src/libsystemd/sd-device/device-util.h index 008946f4de4..5b899ecfcdf 100644 --- a/src/libsystemd/sd-device/device-util.h +++ b/src/libsystemd/sd-device/device-util.h @@ -111,6 +111,8 @@ int device_is_devtype(sd_device *device, const char *devtype); int device_is_subsystem_devtype(sd_device *device, const char *subsystem, const char *devtype); +int device_get_seat(sd_device *device, const char **ret); + int device_sysname_startswith_strv(sd_device *device, char * const *prefixes, const char **ret_suffix); #define device_sysname_startswith(device, ...) \ device_sysname_startswith_strv(device, STRV_MAKE(__VA_ARGS__), NULL) diff --git a/src/login/logind-core.c b/src/login/logind-core.c index 6d435c0ebe7..0208c742339 100644 --- a/src/login/logind-core.c +++ b/src/login/logind-core.c @@ -289,8 +289,9 @@ int manager_process_seat_device(Manager *m, sd_device *d) { bool master; Seat *seat; - if (sd_device_get_property_value(d, "ID_SEAT", &sn) < 0 || isempty(sn)) - sn = "seat0"; + r = device_get_seat(d, &sn); + if (r < 0) + return r; if (!seat_name_is_valid(sn)) { log_device_warning(d, "Device with invalid seat name %s found, ignoring.", sn); @@ -352,8 +353,9 @@ int manager_process_button_device(Manager *m, sd_device *d) { if (r < 0) return r; - if (sd_device_get_property_value(d, "ID_SEAT", &sn) < 0 || isempty(sn)) - sn = "seat0"; + r = device_get_seat(d, &sn); + if (r < 0) + return r; button_set_seat(b, sn); diff --git a/src/login/logind-session-dbus.c b/src/login/logind-session-dbus.c index f3fe877a2c1..50a965c5ff5 100644 --- a/src/login/logind-session-dbus.c +++ b/src/login/logind-session-dbus.c @@ -9,6 +9,7 @@ #include "bus-label.h" #include "bus-polkit.h" #include "bus-util.h" +#include "device-util.h" #include "devnum-util.h" #include "fd-util.h" #include "format-util.h" @@ -705,7 +706,10 @@ static int method_set_brightness(sd_bus_message *message, void *userdata, sd_bus if (r < 0) return sd_bus_error_set_errnof(error, r, "Failed to open device %s:%s: %m", subsystem, name); - if (sd_device_get_property_value(d, "ID_SEAT", &seat) >= 0 && !streq_ptr(seat, s->seat->id)) + r = device_get_seat(d, &seat); + if (r < 0) + return sd_bus_error_set_errnof(error, r, "Failed to get seat of %s:%s: %m", subsystem, name); + if (!streq(seat, s->seat->id)) return sd_bus_error_setf(error, BUS_ERROR_NOT_YOUR_DEVICE, "Device %s:%s does not belong to your seat %s, refusing.", subsystem, name, s->seat->id); r = manager_write_brightness(s->manager, d, brightness, message); diff --git a/src/login/sysfs-show.c b/src/login/sysfs-show.c index 43eb13ac83e..d1f105b1e37 100644 --- a/src/login/sysfs-show.c +++ b/src/login/sysfs-show.c @@ -6,6 +6,7 @@ #include "alloc-util.h" #include "device-enumerator-private.h" +#include "device-util.h" #include "glyph-util.h" #include "path-util.h" #include "string-util.h" @@ -47,8 +48,9 @@ static int show_sysfs_one( !path_startswith(sysfs, sub)) return 0; - if (sd_device_get_property_value(dev_list[*i_dev], "ID_SEAT", &sn) < 0 || isempty(sn)) - sn = "seat0"; + r = device_get_seat(dev_list[*i_dev], &sn); + if (r < 0) + return r; /* Explicitly also check for tag 'seat' here */ if (!streq(seat, sn) || @@ -75,9 +77,9 @@ static int show_sysfs_one( !path_startswith(lookahead_sysfs, sysfs)) { const char *lookahead_sn; - if (sd_device_get_property_value(dev_list[lookahead], "ID_SEAT", &lookahead_sn) < 0 || - isempty(lookahead_sn)) - lookahead_sn = "seat0"; + r = device_get_seat(dev_list[lookahead], &lookahead_sn); + if (r < 0) + return r; if (streq(seat, lookahead_sn) && sd_device_has_current_tag(dev_list[lookahead], "seat") > 0) break; diff --git a/src/shared/devnode-acl.c b/src/shared/devnode-acl.c index 81961c5b05d..945ad8db320 100644 --- a/src/shared/devnode-acl.c +++ b/src/shared/devnode-acl.c @@ -170,8 +170,9 @@ int devnode_acl_all(const char *seat, if (sd_device_has_current_tag(d, "uaccess") <= 0) continue; - if (sd_device_get_property_value(d, "ID_SEAT", &sn) < 0 || isempty(sn)) - sn = "seat0"; + r = device_get_seat(d, &sn); + if (r < 0) + return r; if (!streq(seat, sn)) continue; diff --git a/src/udev/udev-builtin-uaccess.c b/src/udev/udev-builtin-uaccess.c index 7fbd91a5c24..38863c0b108 100644 --- a/src/udev/udev-builtin-uaccess.c +++ b/src/udev/udev-builtin-uaccess.c @@ -32,8 +32,9 @@ static int builtin_uaccess(UdevEvent *event, int argc, char *argv[]) { return log_device_error_errno(dev, r, "Failed to get device node: %m"); const char *seat; - if (sd_device_get_property_value(dev, "ID_SEAT", &seat) < 0) - seat = "seat0"; + r = device_get_seat(dev, &seat); + if (r < 0) + return log_device_error_errno(dev, r, "Failed to get seat: %m"); uid_t uid; r = sd_seat_get_active(seat, /* ret_session = */ NULL, &uid);