mirror of
https://github.com/systemd/systemd.git
synced 2024-12-23 21:35:11 +03:00
Merge pull request #30591 from yuwata/device-util
device-util: introduce device_in_subsystem() and device_is_devtype() helper functions
This commit is contained in:
commit
86b8b66675
@ -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);
|
||||
@ -98,34 +98,29 @@ 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;
|
||||
if (device_in_subsystem(parent, "drm")) {
|
||||
|
||||
r = sd_device_get_sysname(parent, &sysname);
|
||||
if (r < 0)
|
||||
return r;
|
||||
r = sd_device_get_sysname(parent, &s);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
if (streq(subsystem, "drm")) {
|
||||
const char *c;
|
||||
|
||||
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) {
|
||||
@ -133,7 +128,7 @@ static int find_pci_or_platform_parent(sd_device *device, sd_device **ret) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
} else if (streq(subsystem, "platform")) {
|
||||
} else if (device_in_subsystem(parent, "platform")) {
|
||||
*ret = parent;
|
||||
return 0;
|
||||
}
|
||||
@ -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;
|
||||
|
||||
@ -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)
|
||||
@ -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);
|
||||
@ -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");
|
||||
@ -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.
|
||||
@ -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;
|
||||
@ -347,8 +329,6 @@ static int clamp_brightness(
|
||||
unsigned *brightness) {
|
||||
|
||||
unsigned new_brightness, min_brightness;
|
||||
const char *subsystem;
|
||||
int r;
|
||||
|
||||
assert(device);
|
||||
assert(brightness);
|
||||
@ -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;
|
||||
@ -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, "
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
|
@ -402,8 +402,7 @@ 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);
|
||||
@ -411,20 +410,14 @@ static int check_subsystem_filter(sd_device_monitor *m, sd_device *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;
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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);
|
||||
|
@ -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;
|
||||
|
||||
@ -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);
|
||||
@ -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) {
|
||||
@ -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;
|
||||
@ -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;
|
||||
@ -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. */
|
||||
|
@ -1,23 +1,91 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
|
||||
#include "device-util.h"
|
||||
#include "mountpoint-util.h"
|
||||
#include "tests.h"
|
||||
|
||||
TEST(log_device_full) {
|
||||
_cleanup_(sd_device_unrefp) sd_device *dev = NULL;
|
||||
int r;
|
||||
|
||||
for (int level = LOG_ERR; level <= LOG_DEBUG; level++) {
|
||||
log_device_full(NULL, level, "test level=%d: %m", level);
|
||||
(void) sd_device_new_from_subsystem_sysname(&dev, "net", "lo");
|
||||
|
||||
r = log_device_full_errno(NULL, level, EUCLEAN, "test level=%d errno=EUCLEAN: %m", level);
|
||||
for (int level = LOG_ERR; level <= LOG_DEBUG; level++) {
|
||||
log_device_full(dev, level, "test level=%d: %m", level);
|
||||
|
||||
r = log_device_full_errno(dev, level, EUCLEAN, "test level=%d errno=EUCLEAN: %m", level);
|
||||
assert_se(r == -EUCLEAN);
|
||||
|
||||
r = log_device_full_errno(NULL, level, 0, "test level=%d errno=0: %m", level);
|
||||
r = log_device_full_errno(dev, level, 0, "test level=%d errno=0: %m", level);
|
||||
assert_se(r == 0);
|
||||
|
||||
r = log_device_full_errno(NULL, level, SYNTHETIC_ERRNO(ENODATA), "test level=%d errno=S(ENODATA): %m", level);
|
||||
r = log_device_full_errno(dev, level, SYNTHETIC_ERRNO(ENODATA), "test level=%d errno=S(ENODATA): %m", level);
|
||||
assert_se(r == -ENODATA);
|
||||
}
|
||||
}
|
||||
|
||||
DEFINE_TEST_MAIN(LOG_INFO);
|
||||
TEST(device_in_subsystem) {
|
||||
_cleanup_(sd_device_unrefp) sd_device *dev = NULL;
|
||||
int r;
|
||||
|
||||
r = sd_device_new_from_subsystem_sysname(&dev, "net", "lo");
|
||||
if (r == -ENODEV)
|
||||
return (void) log_tests_skipped("net/lo does not exist");
|
||||
assert_se(r >= 0);
|
||||
|
||||
assert_se(device_in_subsystem(dev, "net"));
|
||||
assert_se(!device_in_subsystem(dev, "disk"));
|
||||
assert_se(!device_in_subsystem(dev, "subsystem"));
|
||||
assert_se(!device_in_subsystem(dev, ""));
|
||||
assert_se(!device_in_subsystem(dev, NULL));
|
||||
|
||||
dev = sd_device_unref(dev);
|
||||
|
||||
assert_se(sd_device_new_from_syspath(&dev, "/sys/class/net") >= 0);
|
||||
assert_se(!device_in_subsystem(dev, "net"));
|
||||
assert_se(!device_in_subsystem(dev, "disk"));
|
||||
assert_se(device_in_subsystem(dev, "subsystem"));
|
||||
assert_se(!device_in_subsystem(dev, ""));
|
||||
assert_se(!device_in_subsystem(dev, NULL));
|
||||
|
||||
dev = sd_device_unref(dev);
|
||||
|
||||
assert_se(sd_device_new_from_syspath(&dev, "/sys/class") >= 0);
|
||||
assert_se(!device_in_subsystem(dev, "net"));
|
||||
assert_se(!device_in_subsystem(dev, "disk"));
|
||||
assert_se(!device_in_subsystem(dev, "subsystem"));
|
||||
assert_se(!device_in_subsystem(dev, ""));
|
||||
assert_se(device_in_subsystem(dev, NULL));
|
||||
}
|
||||
|
||||
TEST(device_is_devtype) {
|
||||
_cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
|
||||
_cleanup_(sd_device_unrefp) sd_device *dev = NULL;
|
||||
|
||||
assert_se(sd_device_enumerator_new(&e) >= 0);
|
||||
assert_se(sd_device_enumerator_add_match_subsystem(e, "disk", true) >= 0);
|
||||
|
||||
FOREACH_DEVICE(e, d) {
|
||||
const char *t;
|
||||
|
||||
assert_se(sd_device_get_devtype(d, &t) >= 0);
|
||||
assert_se(device_is_devtype(d, t));
|
||||
assert_se(!device_is_devtype(d, "hoge"));
|
||||
assert_se(!device_is_devtype(d, ""));
|
||||
assert_se(!device_is_devtype(d, NULL));
|
||||
}
|
||||
|
||||
assert_se(sd_device_new_from_syspath(&dev, "/sys/class/net") >= 0);
|
||||
assert_se(!device_is_devtype(dev, "hoge"));
|
||||
assert_se(!device_is_devtype(dev, ""));
|
||||
assert_se(device_is_devtype(dev, NULL));
|
||||
}
|
||||
|
||||
static int intro(void) {
|
||||
if (path_is_mount_point("/sys", NULL, 0) <= 0)
|
||||
return log_tests_skipped("/sys is not mounted");
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
DEFINE_TEST_MAIN_WITH_INTRO(LOG_INFO, intro);
|
||||
|
@ -588,7 +588,7 @@ static int manager_count_external_displays(Manager *m) {
|
||||
return r;
|
||||
|
||||
FOREACH_DEVICE(e, d) {
|
||||
const char *status, *enabled, *dash, *nn, *subsys;
|
||||
const char *status, *enabled, *dash, *nn;
|
||||
sd_device *p;
|
||||
|
||||
if (sd_device_get_parent(d, &p) < 0)
|
||||
@ -597,7 +597,7 @@ static int manager_count_external_displays(Manager *m) {
|
||||
/* If the parent shares the same subsystem as the
|
||||
* device we are looking at then it is a connector,
|
||||
* which is what we are interested in. */
|
||||
if (sd_device_get_subsystem(p, &subsys) < 0 || !streq(subsys, "drm"))
|
||||
if (!device_in_subsystem(p, "drm"))
|
||||
continue;
|
||||
|
||||
if (sd_device_get_sysname(d, &nn) < 0)
|
||||
|
@ -11,6 +11,7 @@
|
||||
#include "alloc-util.h"
|
||||
#include "bus-util.h"
|
||||
#include "daemon-util.h"
|
||||
#include "device-util.h"
|
||||
#include "fd-util.h"
|
||||
#include "logind-session-dbus.h"
|
||||
#include "logind-session-device.h"
|
||||
@ -238,22 +239,21 @@ static void session_device_stop(SessionDevice *sd) {
|
||||
}
|
||||
|
||||
static DeviceType detect_device_type(sd_device *dev) {
|
||||
const char *sysname, *subsystem;
|
||||
DeviceType type = DEVICE_TYPE_UNKNOWN;
|
||||
const char *sysname;
|
||||
|
||||
if (sd_device_get_sysname(dev, &sysname) < 0 ||
|
||||
sd_device_get_subsystem(dev, &subsystem) < 0)
|
||||
return type;
|
||||
if (sd_device_get_sysname(dev, &sysname) < 0)
|
||||
return DEVICE_TYPE_UNKNOWN;
|
||||
|
||||
if (streq(subsystem, "drm")) {
|
||||
if (device_in_subsystem(dev, "drm")) {
|
||||
if (startswith(sysname, "card"))
|
||||
type = DEVICE_TYPE_DRM;
|
||||
} else if (streq(subsystem, "input")) {
|
||||
return DEVICE_TYPE_DRM;
|
||||
|
||||
} else if (device_in_subsystem(dev, "input")) {
|
||||
if (startswith(sysname, "event"))
|
||||
type = DEVICE_TYPE_EVDEV;
|
||||
return DEVICE_TYPE_EVDEV;
|
||||
}
|
||||
|
||||
return type;
|
||||
return DEVICE_TYPE_UNKNOWN;
|
||||
}
|
||||
|
||||
static int session_device_verify(SessionDevice *sd) {
|
||||
|
@ -1267,7 +1267,7 @@ static int acquire_removable(sd_device *d) {
|
||||
if (sd_device_get_parent(d, &d) < 0)
|
||||
return 0;
|
||||
|
||||
if (sd_device_get_subsystem(d, &v) < 0 || !streq(v, "block"))
|
||||
if (!device_in_subsystem(d, "block"))
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -792,14 +792,14 @@ static void acquire_ether_link_info(int *fd, LinkInfo *link) {
|
||||
|
||||
static void acquire_wlan_link_info(LinkInfo *link) {
|
||||
_cleanup_(sd_netlink_unrefp) sd_netlink *genl = NULL;
|
||||
const char *type = NULL;
|
||||
int r, k = 0;
|
||||
|
||||
assert(link);
|
||||
|
||||
if (link->sd_device)
|
||||
(void) sd_device_get_devtype(link->sd_device, &type);
|
||||
if (!streq_ptr(type, "wlan"))
|
||||
if (!link->sd_device)
|
||||
return;
|
||||
|
||||
if (!device_is_devtype(link->sd_device, "wlan"))
|
||||
return;
|
||||
|
||||
r = sd_genl_socket_open(&genl);
|
||||
|
@ -164,7 +164,6 @@ static int manager_connect_bus(Manager *m) {
|
||||
static int manager_process_uevent(sd_device_monitor *monitor, sd_device *device, void *userdata) {
|
||||
Manager *m = ASSERT_PTR(userdata);
|
||||
sd_device_action_t action;
|
||||
const char *s;
|
||||
int r;
|
||||
|
||||
assert(device);
|
||||
@ -173,20 +172,12 @@ static int manager_process_uevent(sd_device_monitor *monitor, sd_device *device,
|
||||
if (r < 0)
|
||||
return log_device_warning_errno(device, r, "Failed to get udev action, ignoring: %m");
|
||||
|
||||
r = sd_device_get_subsystem(device, &s);
|
||||
if (r < 0)
|
||||
return log_device_warning_errno(device, r, "Failed to get subsystem, ignoring: %m");
|
||||
|
||||
if (streq(s, "net"))
|
||||
if (device_in_subsystem(device, "net"))
|
||||
r = manager_udev_process_link(m, device, action);
|
||||
else if (streq(s, "ieee80211"))
|
||||
else if (device_in_subsystem(device, "ieee80211"))
|
||||
r = manager_udev_process_wiphy(m, device, action);
|
||||
else if (streq(s, "rfkill"))
|
||||
else if (device_in_subsystem(device, "rfkill"))
|
||||
r = manager_udev_process_rfkill(m, device, action);
|
||||
else {
|
||||
log_device_debug(device, "Received device with unexpected subsystem \"%s\", ignoring.", s);
|
||||
return 0;
|
||||
}
|
||||
if (r < 0)
|
||||
log_device_warning_errno(device, r, "Failed to process \"%s\" uevent, ignoring: %m",
|
||||
device_action_to_string(action));
|
||||
|
@ -118,11 +118,7 @@ static int link_get_wiphy(Link *link, Wiphy **ret) {
|
||||
if (!link->dev)
|
||||
return -ENODEV;
|
||||
|
||||
r = sd_device_get_devtype(link->dev, &s);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
if (!streq_ptr(s, "wlan"))
|
||||
if (!device_is_devtype(link->dev, "wlan"))
|
||||
return -EOPNOTSUPP;
|
||||
|
||||
r = sd_device_new_child(&phy, link->dev, "phy80211");
|
||||
|
@ -56,23 +56,12 @@ static int fd_get_devnum(int fd, BlockDeviceLookupFlag flags, dev_t *ret) {
|
||||
}
|
||||
|
||||
int block_device_is_whole_disk(sd_device *dev) {
|
||||
const char *s;
|
||||
int r;
|
||||
|
||||
assert(dev);
|
||||
|
||||
r = sd_device_get_subsystem(dev, &s);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
if (!streq(s, "block"))
|
||||
if (!device_in_subsystem(dev, "block"))
|
||||
return -ENOTBLK;
|
||||
|
||||
r = sd_device_get_devtype(dev, &s);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
return streq(s, "disk");
|
||||
return device_is_devtype(dev, "disk");
|
||||
}
|
||||
|
||||
int block_device_get_whole_disk(sd_device *dev, sd_device **ret) {
|
||||
|
@ -12,6 +12,7 @@
|
||||
#include <unistd.h>
|
||||
|
||||
#include "device-private.h"
|
||||
#include "device-util.h"
|
||||
#include "fs-util.h"
|
||||
#include "log.h"
|
||||
#include "main-func.h"
|
||||
@ -147,10 +148,9 @@ static int run(int argc, char *argv[]) {
|
||||
|
||||
/* do what devtmpfs usually provides us */
|
||||
if (sd_device_get_devname(dev, &devname) >= 0) {
|
||||
const char *subsystem;
|
||||
mode_t mode = 0600;
|
||||
|
||||
if (sd_device_get_subsystem(dev, &subsystem) >= 0 && streq(subsystem, "block"))
|
||||
if (device_in_subsystem(dev, "block"))
|
||||
mode |= S_IFBLK;
|
||||
else
|
||||
mode |= S_IFCHR;
|
||||
|
@ -77,20 +77,15 @@ static int udev_builtin_hwdb_search(sd_device *dev, sd_device *srcdev,
|
||||
srcdev = dev;
|
||||
|
||||
for (sd_device *d = srcdev; d; ) {
|
||||
const char *dsubsys, *devtype, *modalias = NULL;
|
||||
|
||||
if (sd_device_get_subsystem(d, &dsubsys) < 0)
|
||||
goto next;
|
||||
const char *modalias = NULL;
|
||||
|
||||
/* look only at devices of a specific subsystem */
|
||||
if (subsystem && !streq(dsubsys, subsystem))
|
||||
if (subsystem && !device_in_subsystem(d, subsystem))
|
||||
goto next;
|
||||
|
||||
(void) sd_device_get_property_value(d, "MODALIAS", &modalias);
|
||||
|
||||
if (streq(dsubsys, "usb") &&
|
||||
sd_device_get_devtype(d, &devtype) >= 0 &&
|
||||
streq(devtype, "usb_device")) {
|
||||
if (device_in_subsystem(d, "usb") && device_is_devtype(d, "usb_device")) {
|
||||
/* if the usb_device does not have a modalias, compose one */
|
||||
if (!modalias)
|
||||
modalias = modalias_usb(d, s, sizeof(s));
|
||||
|
@ -49,12 +49,7 @@ static sd_device *device_skip_virtio(sd_device *dev) {
|
||||
* safely ignore any virtio buses. see
|
||||
* http://lists.linuxfoundation.org/pipermail/virtualization/2015-August/030331.html */
|
||||
while (dev) {
|
||||
const char *subsystem;
|
||||
|
||||
if (sd_device_get_subsystem(dev, &subsystem) < 0)
|
||||
break;
|
||||
|
||||
if (!streq(subsystem, "virtio"))
|
||||
if (!device_in_subsystem(dev, "virtio"))
|
||||
break;
|
||||
|
||||
if (sd_device_get_parent(dev, &dev) < 0)
|
||||
@ -86,22 +81,15 @@ static int get_matching_parent(
|
||||
return -ENODEV;
|
||||
}
|
||||
|
||||
if (!strv_isempty(parent_subsystems)) {
|
||||
const char *subsystem;
|
||||
/* check if our direct parent is in an expected subsystem. */
|
||||
STRV_FOREACH(s, parent_subsystems)
|
||||
if (device_in_subsystem(parent, *s)) {
|
||||
if (ret)
|
||||
*ret = parent;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* check if our direct parent is in an expected subsystem. */
|
||||
r = sd_device_get_subsystem(parent, &subsystem);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
if (!strv_contains(parent_subsystems, subsystem))
|
||||
return -ENODEV;
|
||||
}
|
||||
|
||||
if (ret)
|
||||
*ret = parent;
|
||||
|
||||
return 0;
|
||||
return -ENODEV;
|
||||
}
|
||||
|
||||
static int get_first_syspath_component(sd_device *dev, const char *prefix, char **ret) {
|
||||
@ -1272,15 +1260,9 @@ static int get_ifname_prefix(sd_device *dev, const char **ret) {
|
||||
/* handle only ARPHRD_ETHER, ARPHRD_SLIP and ARPHRD_INFINIBAND devices */
|
||||
switch (iftype) {
|
||||
case ARPHRD_ETHER: {
|
||||
const char *s = NULL;
|
||||
|
||||
r = sd_device_get_devtype(dev, &s);
|
||||
if (r < 0 && r != -ENOENT)
|
||||
return r;
|
||||
|
||||
if (streq_ptr(s, "wlan"))
|
||||
if (device_is_devtype(dev, "wlan"))
|
||||
*ret = "wl";
|
||||
else if (streq_ptr(s, "wwan"))
|
||||
else if (device_is_devtype(dev, "wwan"))
|
||||
*ret = "ww";
|
||||
else
|
||||
*ret = "en";
|
||||
|
@ -95,12 +95,7 @@ static sd_device *skip_subsystem(sd_device *dev, const char *subsys) {
|
||||
*/
|
||||
|
||||
for (parent = dev; ; ) {
|
||||
const char *subsystem;
|
||||
|
||||
if (sd_device_get_subsystem(parent, &subsystem) < 0)
|
||||
break;
|
||||
|
||||
if (!streq(subsystem, subsys))
|
||||
if (!device_in_subsystem(parent, subsys))
|
||||
break;
|
||||
|
||||
dev = parent;
|
||||
@ -417,10 +412,9 @@ static sd_device *handle_scsi_hyperv(sd_device *parent, char **path, size_t guid
|
||||
}
|
||||
|
||||
static sd_device *handle_scsi(sd_device *parent, char **path, char **compat_path, bool *supported_parent) {
|
||||
const char *devtype, *id, *name;
|
||||
const char *id, *name;
|
||||
|
||||
if (sd_device_get_devtype(parent, &devtype) < 0 ||
|
||||
!streq(devtype, "scsi_device"))
|
||||
if (!device_is_devtype(parent, "scsi_device"))
|
||||
return parent;
|
||||
|
||||
/* firewire */
|
||||
@ -532,12 +526,10 @@ static int get_usb_revision(sd_device *dev) {
|
||||
}
|
||||
|
||||
static sd_device *handle_usb(sd_device *parent, char **path) {
|
||||
const char *devtype, *str, *port;
|
||||
const char *str, *port;
|
||||
int r;
|
||||
|
||||
if (sd_device_get_devtype(parent, &devtype) < 0)
|
||||
return parent;
|
||||
if (!STR_IN_SET(devtype, "usb_interface", "usb_device"))
|
||||
if (!device_is_devtype(parent, "usb_interface") && !device_is_devtype(parent, "usb_device"))
|
||||
return parent;
|
||||
|
||||
if (sd_device_get_sysname(parent, &str) < 0)
|
||||
@ -715,105 +707,103 @@ static int builtin_path_id(UdevEvent *event, int argc, char *argv[], bool test)
|
||||
_cleanup_(sd_device_unrefp) sd_device *dev_other_branch = NULL;
|
||||
_cleanup_free_ char *path = NULL, *compat_path = NULL;
|
||||
bool supported_transport = false, supported_parent = false;
|
||||
const char *subsystem;
|
||||
int r;
|
||||
|
||||
/* walk up the chain of devices and compose path */
|
||||
for (sd_device *parent = dev; parent; ) {
|
||||
const char *subsys, *sysname;
|
||||
const char *sysname;
|
||||
|
||||
if (sd_device_get_subsystem(parent, &subsys) < 0 ||
|
||||
sd_device_get_sysname(parent, &sysname) < 0) {
|
||||
if (sd_device_get_sysname(parent, &sysname) < 0) {
|
||||
;
|
||||
} else if (streq(subsys, "scsi_tape")) {
|
||||
} else if (device_in_subsystem(parent, "scsi_tape")) {
|
||||
handle_scsi_tape(parent, &path);
|
||||
} else if (streq(subsys, "scsi")) {
|
||||
} else if (device_in_subsystem(parent, "scsi")) {
|
||||
parent = handle_scsi(parent, &path, &compat_path, &supported_parent);
|
||||
supported_transport = true;
|
||||
} else if (streq(subsys, "cciss")) {
|
||||
} else if (device_in_subsystem(parent, "cciss")) {
|
||||
parent = handle_cciss(parent, &path);
|
||||
supported_transport = true;
|
||||
} else if (streq(subsys, "usb")) {
|
||||
} else if (device_in_subsystem(parent, "usb")) {
|
||||
parent = handle_usb(parent, &path);
|
||||
supported_transport = true;
|
||||
} else if (streq(subsys, "bcma")) {
|
||||
} else if (device_in_subsystem(parent, "bcma")) {
|
||||
parent = handle_bcma(parent, &path);
|
||||
supported_transport = true;
|
||||
} else if (streq(subsys, "serio")) {
|
||||
} else if (device_in_subsystem(parent, "serio")) {
|
||||
const char *sysnum;
|
||||
|
||||
if (sd_device_get_sysnum(parent, &sysnum) >= 0 && sysnum) {
|
||||
path_prepend(&path, "serio-%s", sysnum);
|
||||
parent = skip_subsystem(parent, "serio");
|
||||
}
|
||||
} else if (streq(subsys, "pci")) {
|
||||
} else if (device_in_subsystem(parent, "pci")) {
|
||||
path_prepend(&path, "pci-%s", sysname);
|
||||
if (compat_path)
|
||||
path_prepend(&compat_path, "pci-%s", sysname);
|
||||
parent = skip_subsystem(parent, "pci");
|
||||
supported_parent = true;
|
||||
} else if (streq(subsys, "platform")) {
|
||||
} else if (device_in_subsystem(parent, "platform")) {
|
||||
path_prepend(&path, "platform-%s", sysname);
|
||||
if (compat_path)
|
||||
path_prepend(&compat_path, "platform-%s", sysname);
|
||||
parent = skip_subsystem(parent, "platform");
|
||||
supported_transport = true;
|
||||
supported_parent = true;
|
||||
} else if (streq(subsys, "amba")) {
|
||||
} else if (device_in_subsystem(parent, "amba")) {
|
||||
path_prepend(&path, "amba-%s", sysname);
|
||||
if (compat_path)
|
||||
path_prepend(&compat_path, "amba-%s", sysname);
|
||||
parent = skip_subsystem(parent, "amba");
|
||||
supported_transport = true;
|
||||
supported_parent = true;
|
||||
} else if (streq(subsys, "acpi")) {
|
||||
} else if (device_in_subsystem(parent, "acpi")) {
|
||||
path_prepend(&path, "acpi-%s", sysname);
|
||||
if (compat_path)
|
||||
path_prepend(&compat_path, "acpi-%s", sysname);
|
||||
parent = skip_subsystem(parent, "acpi");
|
||||
supported_parent = true;
|
||||
} else if (streq(subsys, "xen")) {
|
||||
} else if (device_in_subsystem(parent, "xen")) {
|
||||
path_prepend(&path, "xen-%s", sysname);
|
||||
if (compat_path)
|
||||
path_prepend(&compat_path, "xen-%s", sysname);
|
||||
parent = skip_subsystem(parent, "xen");
|
||||
supported_parent = true;
|
||||
} else if (streq(subsys, "virtio")) {
|
||||
} else if (device_in_subsystem(parent, "virtio")) {
|
||||
parent = skip_subsystem(parent, "virtio");
|
||||
supported_transport = true;
|
||||
} else if (streq(subsys, "scm")) {
|
||||
} else if (device_in_subsystem(parent, "scm")) {
|
||||
path_prepend(&path, "scm-%s", sysname);
|
||||
if (compat_path)
|
||||
path_prepend(&compat_path, "scm-%s", sysname);
|
||||
parent = skip_subsystem(parent, "scm");
|
||||
supported_transport = true;
|
||||
supported_parent = true;
|
||||
} else if (streq(subsys, "ccw")) {
|
||||
} else if (device_in_subsystem(parent, "ccw")) {
|
||||
path_prepend(&path, "ccw-%s", sysname);
|
||||
if (compat_path)
|
||||
path_prepend(&compat_path, "ccw-%s", sysname);
|
||||
parent = skip_subsystem(parent, "ccw");
|
||||
supported_transport = true;
|
||||
supported_parent = true;
|
||||
} else if (streq(subsys, "ccwgroup")) {
|
||||
} else if (device_in_subsystem(parent, "ccwgroup")) {
|
||||
path_prepend(&path, "ccwgroup-%s", sysname);
|
||||
if (compat_path)
|
||||
path_prepend(&compat_path, "ccwgroup-%s", sysname);
|
||||
parent = skip_subsystem(parent, "ccwgroup");
|
||||
supported_transport = true;
|
||||
supported_parent = true;
|
||||
} else if (streq(subsys, "ap")) {
|
||||
} else if (device_in_subsystem(parent, "ap")) {
|
||||
parent = handle_ap(parent, &path);
|
||||
supported_transport = true;
|
||||
supported_parent = true;
|
||||
} else if (streq(subsys, "iucv")) {
|
||||
} else if (device_in_subsystem(parent, "iucv")) {
|
||||
path_prepend(&path, "iucv-%s", sysname);
|
||||
if (compat_path)
|
||||
path_prepend(&compat_path, "iucv-%s", sysname);
|
||||
parent = skip_subsystem(parent, "iucv");
|
||||
supported_transport = true;
|
||||
supported_parent = true;
|
||||
} else if (STR_IN_SET(subsys, "nvme", "nvme-subsystem")) {
|
||||
} else if (device_in_subsystem(parent, "nvme") || device_in_subsystem(parent, "nvme-subsystem")) {
|
||||
const char *nsid;
|
||||
|
||||
if (sd_device_get_sysattr_value(dev, "nsid", &nsid) >= 0) {
|
||||
@ -821,7 +811,7 @@ static int builtin_path_id(UdevEvent *event, int argc, char *argv[], bool test)
|
||||
if (compat_path)
|
||||
path_prepend(&compat_path, "nvme-%s", nsid);
|
||||
|
||||
if (streq(subsys, "nvme-subsystem")) {
|
||||
if (device_in_subsystem(parent, "nvme-subsystem")) {
|
||||
r = find_real_nvme_parent(dev, &dev_other_branch);
|
||||
if (r < 0)
|
||||
return r;
|
||||
@ -833,7 +823,7 @@ static int builtin_path_id(UdevEvent *event, int argc, char *argv[], bool test)
|
||||
supported_parent = true;
|
||||
supported_transport = true;
|
||||
}
|
||||
} else if (streq(subsys, "spi")) {
|
||||
} else if (device_in_subsystem(parent, "spi")) {
|
||||
const char *sysnum;
|
||||
|
||||
if (sd_device_get_sysnum(parent, &sysnum) >= 0 && sysnum) {
|
||||
@ -864,9 +854,7 @@ static int builtin_path_id(UdevEvent *event, int argc, char *argv[], bool test)
|
||||
* devices do not expose their buses and do not provide a unique
|
||||
* and predictable name that way.
|
||||
*/
|
||||
if (sd_device_get_subsystem(dev, &subsystem) >= 0 &&
|
||||
streq(subsystem, "block") &&
|
||||
!supported_transport)
|
||||
if (device_in_subsystem(dev, "block") && !supported_transport)
|
||||
return -ENOENT;
|
||||
|
||||
add_id_with_usb_revision(dev, test, path);
|
||||
|
@ -248,7 +248,7 @@ static int builtin_usb_id(UdevEvent *event, int argc, char *argv[], bool test) {
|
||||
size_t l;
|
||||
char *s;
|
||||
|
||||
const char *syspath, *sysname, *devtype, *interface_syspath;
|
||||
const char *syspath, *sysname, *interface_syspath;
|
||||
int r;
|
||||
|
||||
r = sd_device_get_syspath(dev, &syspath);
|
||||
@ -260,7 +260,7 @@ static int builtin_usb_id(UdevEvent *event, int argc, char *argv[], bool test) {
|
||||
return r;
|
||||
|
||||
/* shortcut, if we are called directly for a "usb_device" type */
|
||||
if (sd_device_get_devtype(dev, &devtype) >= 0 && streq(devtype, "usb_device")) {
|
||||
if (device_is_devtype(dev, "usb_device")) {
|
||||
dev_if_packed_info(dev, packed_if_str, sizeof(packed_if_str));
|
||||
dev_usb = dev;
|
||||
goto fallback;
|
||||
|
@ -510,22 +510,17 @@ static int link_update(sd_device *dev, const char *slink, bool add) {
|
||||
}
|
||||
|
||||
static int device_get_devpath_by_devnum(sd_device *dev, char **ret) {
|
||||
const char *subsystem;
|
||||
dev_t devnum;
|
||||
int r;
|
||||
|
||||
assert(dev);
|
||||
assert(ret);
|
||||
|
||||
r = sd_device_get_subsystem(dev, &subsystem);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
r = sd_device_get_devnum(dev, &devnum);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
return device_path_make_major_minor(streq(subsystem, "block") ? S_IFBLK : S_IFCHR, devnum, ret);
|
||||
return device_path_make_major_minor(device_in_subsystem(dev, "block") ? S_IFBLK : S_IFCHR, devnum, ret);
|
||||
}
|
||||
|
||||
int udev_node_update(sd_device *dev, sd_device *dev_old) {
|
||||
|
@ -138,11 +138,7 @@ static int worker_mark_block_device_read_only(sd_device *dev) {
|
||||
if (!device_for_action(dev, SD_DEVICE_ADD))
|
||||
return 0;
|
||||
|
||||
r = sd_device_get_subsystem(dev, &val);
|
||||
if (r < 0)
|
||||
return log_device_debug_errno(dev, r, "Failed to get subsystem: %m");
|
||||
|
||||
if (!streq(val, "block"))
|
||||
if (!device_in_subsystem(dev, "block"))
|
||||
return 0;
|
||||
|
||||
r = sd_device_get_sysname(dev, &val);
|
||||
|
Loading…
Reference in New Issue
Block a user