device-util: introduce device_get_seat() helper function

This commit is contained in:
Yu Watanabe
2025-05-13 23:39:09 +09:00
parent ab1bd9daed
commit 2012d6d74e
8 changed files with 44 additions and 18 deletions

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

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

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

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

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

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

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

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