Skip to content

Commit

Permalink
Merge pull request systemd#30591 from yuwata/device-util
Browse files Browse the repository at this point in the history
device-util: introduce device_in_subsystem() and device_is_devtype() helper functions
  • Loading branch information
bluca authored Jan 1, 2024
2 parents 9d75598 + fb53ee0 commit 86b8b66
Show file tree
Hide file tree
Showing 22 changed files with 217 additions and 265 deletions.
80 changes: 26 additions & 54 deletions src/backlight/backlight.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ static int has_multiple_graphics_cards(void) {
}

static int find_pci_or_platform_parent(sd_device *device, sd_device **ret) {
const char *subsystem, *sysname, *value;
sd_device *parent;
const char *s;
int r;

assert(device);
Expand All @@ -98,42 +98,37 @@ static int find_pci_or_platform_parent(sd_device *device, sd_device **ret) {
if (r < 0)
return r;

r = sd_device_get_subsystem(parent, &subsystem);
if (r < 0)
return r;

r = sd_device_get_sysname(parent, &sysname);
if (r < 0)
return r;
if (device_in_subsystem(parent, "drm")) {

if (streq(subsystem, "drm")) {
const char *c;
r = sd_device_get_sysname(parent, &s);
if (r < 0)
return r;

c = startswith(sysname, "card");
if (!c)
s = startswith(s, "card");
if (!s)
return -ENODATA;

c += strspn(c, DIGITS);
if (*c == '-' && !STARTSWITH_SET(c, "-LVDS-", "-Embedded DisplayPort-", "-eDP-"))
s += strspn(s, DIGITS);
if (*s == '-' && !STARTSWITH_SET(s, "-LVDS-", "-Embedded DisplayPort-", "-eDP-"))
/* A connector DRM device, let's ignore all but LVDS and eDP! */
return -EOPNOTSUPP;

} else if (streq(subsystem, "pci") &&
sd_device_get_sysattr_value(parent, "class", &value) >= 0) {
} else if (device_in_subsystem(parent, "pci") &&
sd_device_get_sysattr_value(parent, "class", &s)) {

unsigned long class;

r = safe_atolu(value, &class);
r = safe_atolu(s, &class);
if (r < 0)
return log_warning_errno(r, "Cannot parse PCI class '%s' of device %s:%s: %m",
value, subsystem, sysname);
return log_device_warning_errno(parent, r, "Cannot parse PCI class '%s': %m", s);

/* Graphics card */
if (class == PCI_CLASS_GRAPHICS_CARD) {
*ret = parent;
return 0;
}

} else if (streq(subsystem, "platform")) {
} else if (device_in_subsystem(parent, "platform")) {
*ret = parent;
return 0;
}
Expand Down Expand Up @@ -172,7 +167,7 @@ static int same_device(sd_device *a, sd_device *b) {

static int validate_device(sd_device *device) {
_cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *enumerate = NULL;
const char *v, *sysname, *subsystem;
const char *v, *sysname;
sd_device *parent;
int r;

Expand All @@ -191,11 +186,8 @@ static int validate_device(sd_device *device) {
if (r < 0)
return log_device_debug_errno(device, r, "Failed to get sysname: %m");

r = sd_device_get_subsystem(device, &subsystem);
if (r < 0)
return log_device_debug_errno(device, r, "Failed to get subsystem: %m");
if (!streq(subsystem, "backlight"))
return true;
if (!device_in_subsystem(device, "backlight"))
return true; /* We assume LED device is always valid. */

r = sd_device_get_sysattr_value(device, "type", &v);
if (r < 0)
Expand All @@ -207,15 +199,12 @@ static int validate_device(sd_device *device) {
if (r < 0)
return log_device_debug_errno(device, r, "Failed to find PCI or platform parent: %m");

r = sd_device_get_subsystem(parent, &subsystem);
if (r < 0)
return log_device_debug_errno(parent, r, "Failed to get subsystem: %m");

if (DEBUG_LOGGING) {
const char *s = NULL;
const char *s = NULL, *subsystem = NULL;

(void) sd_device_get_syspath(parent, &s);
log_device_debug(device, "Found %s parent device: %s", subsystem, strna(s));
(void) sd_device_get_subsystem(parent, &subsystem);
log_device_debug(device, "Found %s parent device: %s", strna(subsystem), strna(s));
}

r = sd_device_enumerator_new(&enumerate);
Expand All @@ -242,7 +231,7 @@ static int validate_device(sd_device *device) {
if (r < 0)
return log_debug_errno(r, "Failed to add sysattr match: %m");

if (streq(subsystem, "pci")) {
if (device_in_subsystem(parent, "pci")) {
r = has_multiple_graphics_cards();
if (r < 0)
return log_debug_errno(r, "Failed to check if the system has multiple graphics cards: %m");
Expand All @@ -260,7 +249,6 @@ static int validate_device(sd_device *device) {
}

FOREACH_DEVICE(enumerate, other) {
const char *other_subsystem;
sd_device *other_parent;

/* OK, so there's another backlight device, and it's a platform or firmware device.
Expand All @@ -285,13 +273,7 @@ static int validate_device(sd_device *device) {
return false;
}

r = sd_device_get_subsystem(other_parent, &other_subsystem);
if (r < 0) {
log_device_debug_errno(other_parent, r, "Failed to get subsystem, ignoring: %m");
continue;
}

if (streq(other_subsystem, "platform") && streq(subsystem, "pci")) {
if (device_in_subsystem(other_parent, "platform") && device_in_subsystem(parent, "pci")) {
/* The other is connected to the platform bus and we are a PCI device, that also means we are out. */
if (DEBUG_LOGGING) {
const char *other_sysname = NULL, *other_type = NULL;
Expand Down Expand Up @@ -347,8 +329,6 @@ static int clamp_brightness(
unsigned *brightness) {

unsigned new_brightness, min_brightness;
const char *subsystem;
int r;

assert(device);
assert(brightness);
Expand All @@ -358,11 +338,7 @@ static int clamp_brightness(
* avoids preserving an unreadably dim screen, which would otherwise force the user to disable
* state restoration. */

r = sd_device_get_subsystem(device, &subsystem);
if (r < 0)
return log_device_warning_errno(device, r, "Failed to get device subsystem: %m");

if (streq(subsystem, "backlight"))
if (device_in_subsystem(device, "backlight"))
min_brightness = MAX(1U, (unsigned) ((double) max_brightness * percent / 100));
else
min_brightness = 0;
Expand Down Expand Up @@ -413,18 +389,14 @@ static bool shall_clamp(sd_device *d, unsigned *ret) {
}

static int read_brightness(sd_device *device, unsigned max_brightness, unsigned *ret_brightness) {
const char *subsystem, *value;
const char *value;
unsigned brightness;
int r;

assert(device);
assert(ret_brightness);

r = sd_device_get_subsystem(device, &subsystem);
if (r < 0)
return log_device_debug_errno(device, r, "Failed to get subsystem: %m");

if (streq(subsystem, "backlight")) {
if (device_in_subsystem(device, "backlight")) {
r = sd_device_get_sysattr_value(device, "actual_brightness", &value);
if (r == -ENOENT) {
log_device_debug_errno(device, r, "Failed to read 'actual_brightness' attribute, "
Expand Down
17 changes: 2 additions & 15 deletions src/dissect/dissect.c
Original file line number Diff line number Diff line change
Expand Up @@ -1741,26 +1741,13 @@ static int action_detach(const char *path) {

FOREACH_DEVICE(e, d) {
_cleanup_(loop_device_unrefp) LoopDevice *entry_loop = NULL;
const char *name, *devtype;

r = sd_device_get_sysname(d, &name);
if (r < 0) {
log_warning_errno(r, "Failed to get enumerated device's sysname, skipping: %m");
continue;
}

r = sd_device_get_devtype(d, &devtype);
if (r < 0) {
log_warning_errno(r, "Failed to get devtype of '%s', skipping: %m", name);
continue;
}

if (!streq(devtype, "disk")) /* Filter out partition block devices */
if (!device_is_devtype(d, "disk")) /* Filter out partition block devices */
continue;

r = loop_device_open(d, O_RDONLY, LOCK_SH, &entry_loop);
if (r < 0) {
log_warning_errno(r, "Failed to open loopback block device '%s', skipping: %m", name);
log_device_warning_errno(d, r, "Failed to open loopback block device, skipping: %m");
continue;
}

Expand Down
6 changes: 1 addition & 5 deletions src/libsystemd/sd-device/device-enumerator.c
Original file line number Diff line number Diff line change
Expand Up @@ -370,12 +370,8 @@ static int enumerator_sort_devices(sd_device_enumerator *enumerator) {

HASHMAP_FOREACH_KEY(device, syspath, enumerator->devices_by_syspath) {
_cleanup_free_ char *p = NULL;
const char *subsys;

if (sd_device_get_subsystem(device, &subsys) < 0)
continue;

if (!streq(subsys, *prioritized_subsystem))
if (!device_in_subsystem(device, *prioritized_subsystem))
continue;

devices[n++] = sd_device_ref(device);
Expand Down
19 changes: 6 additions & 13 deletions src/libsystemd/sd-device/device-monitor.c
Original file line number Diff line number Diff line change
Expand Up @@ -402,29 +402,22 @@ static sd_device_monitor *device_monitor_free(sd_device_monitor *m) {
DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC(sd_device_monitor, sd_device_monitor, device_monitor_free);

static int check_subsystem_filter(sd_device_monitor *m, sd_device *device) {
const char *s, *subsystem, *d, *devtype = NULL;
int r;
const char *s, *d;

assert(m);
assert(device);

if (hashmap_isempty(m->subsystem_filter))
return true;

r = sd_device_get_subsystem(device, &subsystem);
if (r < 0)
return r;

r = sd_device_get_devtype(device, &devtype);
if (r < 0 && r != -ENOENT)
return r;

HASHMAP_FOREACH_KEY(d, s, m->subsystem_filter) {
if (!streq(s, subsystem))
if (!device_in_subsystem(device, s))
continue;

if (!d || streq_ptr(d, devtype))
return true;
if (d && !device_is_devtype(device, d))
continue;

return true;
}

return false;
Expand Down
18 changes: 18 additions & 0 deletions src/libsystemd/sd-device/device-util.c
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,21 @@ char** device_make_log_fields(sd_device *device) {

return TAKE_PTR(strv);
}

bool device_in_subsystem(sd_device *device, const char *subsystem) {
const char *s = NULL;

assert(device);

(void) sd_device_get_subsystem(device, &s);
return streq_ptr(s, subsystem);
}

bool device_is_devtype(sd_device *device, const char *devtype) {
const char *s = NULL;

assert(device);

(void) sd_device_get_devtype(device, &s);
return streq_ptr(s, devtype);
}
3 changes: 3 additions & 0 deletions src/libsystemd/sd-device/device-util.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,6 @@ static inline int devname_from_stat_rdev(const struct stat *st, char **ret) {
int device_open_from_devnum(mode_t mode, dev_t devnum, int flags, char **ret);

char** device_make_log_fields(sd_device *device);

bool device_in_subsystem(sd_device *device, const char *subsystem);
bool device_is_devtype(sd_device *device, const char *devtype);
51 changes: 17 additions & 34 deletions src/libsystemd/sd-device/sd-device.c
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ _public_ int sd_device_new_from_syspath(sd_device **ret, const char *syspath) {
int device_new_from_mode_and_devnum(sd_device **ret, mode_t mode, dev_t devnum) {
_cleanup_(sd_device_unrefp) sd_device *dev = NULL;
_cleanup_free_ char *syspath = NULL;
const char *t, *subsystem = NULL;
const char *t;
dev_t n;
int r;

Expand Down Expand Up @@ -314,10 +314,7 @@ int device_new_from_mode_and_devnum(sd_device **ret, mode_t mode, dev_t devnum)
if (n != devnum)
return -ENXIO;

r = sd_device_get_subsystem(dev, &subsystem);
if (r < 0 && r != -ENOENT)
return r;
if (streq_ptr(subsystem, "block") != !!S_ISBLK(mode))
if (device_in_subsystem(dev, "block") != !!S_ISBLK(mode))
return -ENXIO;

*ret = TAKE_PTR(dev);
Expand Down Expand Up @@ -1222,37 +1219,27 @@ _public_ int sd_device_get_devtype(sd_device *device, const char **devtype) {
return !!device->devtype;
}

_public_ int sd_device_get_parent_with_subsystem_devtype(sd_device *child, const char *subsystem, const char *devtype, sd_device **ret) {
sd_device *parent = NULL;
_public_ int sd_device_get_parent_with_subsystem_devtype(sd_device *device, const char *subsystem, const char *devtype, sd_device **ret) {
int r;

assert_return(child, -EINVAL);
assert_return(device, -EINVAL);
assert_return(subsystem, -EINVAL);

r = sd_device_get_parent(child, &parent);
while (r >= 0) {
const char *parent_subsystem = NULL;
for (;;) {
r = sd_device_get_parent(device, &device);
if (r < 0)
return r;

(void) sd_device_get_subsystem(parent, &parent_subsystem);
if (streq_ptr(parent_subsystem, subsystem)) {
const char *parent_devtype = NULL;
if (!device_in_subsystem(device, subsystem))
continue;

if (!devtype)
break;
if (devtype && !device_is_devtype(device, devtype))
continue;

(void) sd_device_get_devtype(parent, &parent_devtype);
if (streq_ptr(parent_devtype, devtype))
break;
}
r = sd_device_get_parent(parent, &parent);
if (ret)
*ret = device;
return 0;
}

if (r < 0)
return r;

if (ret)
*ret = parent;
return 0;
}

_public_ int sd_device_get_devnum(sd_device *device, dev_t *devnum) {
Expand Down Expand Up @@ -2591,7 +2578,7 @@ _public_ int sd_device_trigger_with_uuid(

_public_ int sd_device_open(sd_device *device, int flags) {
_cleanup_close_ int fd = -EBADF, fd2 = -EBADF;
const char *devname, *subsystem = NULL;
const char *devname;
uint64_t q, diskseq = 0;
struct stat st;
dev_t devnum;
Expand All @@ -2612,10 +2599,6 @@ _public_ int sd_device_open(sd_device *device, int flags) {
if (r < 0)
return r;

r = sd_device_get_subsystem(device, &subsystem);
if (r < 0 && r != -ENOENT)
return r;

fd = open(devname, FLAGS_SET(flags, O_PATH) ? flags : O_CLOEXEC|O_NOFOLLOW|O_PATH);
if (fd < 0)
return -errno;
Expand All @@ -2626,7 +2609,7 @@ _public_ int sd_device_open(sd_device *device, int flags) {
if (st.st_rdev != devnum)
return -ENXIO;

if (streq_ptr(subsystem, "block") ? !S_ISBLK(st.st_mode) : !S_ISCHR(st.st_mode))
if (device_in_subsystem(device, "block") ? !S_ISBLK(st.st_mode) : !S_ISCHR(st.st_mode))
return -ENXIO;

/* If flags has O_PATH, then we cannot check diskseq. Let's return earlier. */
Expand Down
Loading

0 comments on commit 86b8b66

Please sign in to comment.