tree-wide: use device_in_subsystem() and device_is_devtype()

This commit is contained in:
Yu Watanabe
2023-12-23 03:52:21 +09:00
parent 3b34580d1a
commit fb53ee0a64
19 changed files with 123 additions and 260 deletions
+1 -5
View File
@@ -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);
+6 -13
View File
@@ -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;
+17 -34
View File
@@ -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) {
@@ -2609,7 +2596,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;
@@ -2630,10 +2617,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;
@@ -2644,7 +2627,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. */