]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
device-util: introduce device_get_seat() helper function
authorYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 13 May 2025 14:39:09 +0000 (23:39 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 13 May 2025 17:06:02 +0000 (02:06 +0900)
src/home/homed-home.c
src/libsystemd/sd-device/device-util.c
src/libsystemd/sd-device/device-util.h
src/login/logind-core.c
src/login/logind-session-dbus.c
src/login/sysfs-show.c
src/shared/devnode-acl.c
src/udev/udev-builtin-uaccess.c

index da5e304f1b1bdb0cdfceee768d6215ff1597ce0c..58a8ffff8334d1ca9d259c01897707a84a3fae22 100644 (file)
@@ -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);
index 67bcbf8af52c06e91a030e3f696efa6e714e3f79..5169c24d848a984c8825695c1217380d1351c9ea 100644 (file)
@@ -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,
index 008946f4de4f33df2bdcfa59c3412c8cf46059d4..5b899ecfcdf2ff79d2a0e2ad3fe8ab57eaf4f358 100644 (file)
@@ -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)
index 6d435c0ebe78c449ffd05bfa3b69d2ea7f33f598..0208c74233998531308e261d9740e18d495d5fc0 100644 (file)
@@ -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);
 
index f3fe877a2c16db27671c686382ffc9a06a86216e..50a965c5ff5ce782919f2466dfb0ca15c5e863be 100644 (file)
@@ -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);
index 43eb13ac83ecc012553eac78b02c69119f90cc55..d1f105b1e37922eeab16e7f831d825fb3a74a89b 100644 (file)
@@ -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;
index 81961c5b05dbcb32eb3c25e8ef30a38a808c9b64..945ad8db32060e43bd70e12c837ed5ca74289ca6 100644 (file)
@@ -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;
index 7fbd91a5c243e097d834525a510553d4d9fcf535..38863c0b10898488fe752b1ec223133a61763ed7 100644 (file)
@@ -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);