]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
tree-wide: use device_in_subsystem() and device_is_devtype() 30591/head
authorYu Watanabe <watanabe.yu+github@gmail.com>
Thu, 21 Dec 2023 20:09:05 +0000 (05:09 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Fri, 22 Dec 2023 18:52:21 +0000 (03:52 +0900)
19 files changed:
src/backlight/backlight.c
src/dissect/dissect.c
src/libsystemd/sd-device/device-enumerator.c
src/libsystemd/sd-device/device-monitor.c
src/libsystemd/sd-device/sd-device.c
src/login/logind-core.c
src/login/logind-session-device.c
src/mount/mount-tool.c
src/network/networkctl.c
src/network/networkd-manager.c
src/network/networkd-wiphy.c
src/shared/blockdev-util.c
src/udev/test-udev-rule-runner.c
src/udev/udev-builtin-hwdb.c
src/udev/udev-builtin-net_id.c
src/udev/udev-builtin-path_id.c
src/udev/udev-builtin-usb_id.c
src/udev/udev-node.c
src/udev/udev-worker.c

index 5ac9f904a925e388c047c56a6a0eaa030d2c2ddf..039581b00c60c970f561e9c59fa6e206f5f3675b 100644 (file)
@@ -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;
-
-        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) {
@@ -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;
@@ -335,8 +317,6 @@ static int clamp_brightness(
                 unsigned *brightness) {
 
         unsigned new_brightness, min_brightness;
-        const char *subsystem;
-        int r;
 
         assert(device);
         assert(brightness);
@@ -346,11 +326,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;
@@ -401,18 +377,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, "
index f9f47cf7aacf670070486d6642d31d83f7863512..ddefd9faa4ba25a74e7935181670d6dca120e708 100644 (file)
@@ -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;
                         }
 
index 15c5c42ade757f634bcf10cd6b1527c70e17c689..499784aae2d17f252f3ba22c0ec5b51e04172fb6 100644 (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);
index bb4f9bd513e222e71a6308557598ac4ba1e99c9d..49ab56adcbbadc401748b65a8e7423b34a719524 100644 (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;
index 2fbc619a34d3cd4da057bd356c24acbe5eb21f08..5e44d35ae6ab444aaa6cfc4cad398834aa7f788c 100644 (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. */
index becc21b0de6936cc6a56ec88ea83b13553d6409b..26133ee6efceece88d6b716ff8b09ebb5aa814bf 100644 (file)
@@ -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)
index 44d8d525ee01c240ac4033dc0dae78374c129f76..bde2fe756b87041d578b29003115ae23120d5d4a 100644 (file)
@@ -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"
@@ -239,22 +240,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) {
index 2c276ef22a4f2b54e7aaa3d5909dc9053781b936..d8c37534d94e898f0ed052b1e5e850e1ecfb8cd0 100644 (file)
@@ -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;
         }
 
index cf9d17c8b2fe98c37a760fd3b623125f195126c9..c45b3c3c0618dd5a69e965efd3fd771dce67685f 100644 (file)
@@ -780,14 +780,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);
index c8df73d444249904f54bb0f32f916c1988a72dc8..e81905ad98a99deedde2a34a08e8199290b6c943 100644 (file)
@@ -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));
index 13f2d7202eb2b9c06d2d387f1f12068f9d2ab29b..441713fa0c876700e36a68b61b10cb704b5bd6c5 100644 (file)
@@ -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");
index 347cd787c3a21173114b1cc5f7a95c3652f646c5..0bc68e1721e215acec5dd257d343ee35e941caaa 100644 (file)
@@ -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) {
index 72296b3f0e69cbf0fc3b4edeabbe49cfbff53af9..6fbb7abbcbabf7f44e9732c61d2aee135002811e 100644 (file)
@@ -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;
index 19e07e734f1afea6e73cc9c80f56255d816149fa..4540c33d982e6ce7b69d80a14dbc2ccc3b4d3046 100644 (file)
@@ -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));
index 841e4615fcb1456d36a6e360e1fa75fefacfb614..2482436006250864b20603d20d654c7c96ba1228 100644 (file)
@@ -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. */
-                r = sd_device_get_subsystem(parent, &subsystem);
-                if (r < 0)
-                        return r;
-
-                if (!strv_contains(parent_subsystems, subsystem))
-                        return -ENODEV;
-        }
-
-        if (ret)
-                *ret = parent;
+        /* 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;
+                }
 
-        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";
index 467c9a6ad3981ece6d1d8c4ae09ddd1362d18cec..edd98fa7a71786c3ed8928b6d03f1f7a3c8562cd 100644 (file)
@@ -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);
index 8e83c9c3426615960c3308c110daf4a3505fea93..f5b8fe9e9adf4916010ed02fc82ff21bd000f740 100644 (file)
@@ -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;
index e12c26ce5a6c9b95a5aff49486d9527c8b5c1f7b..633fb2a31d7d7b8c66178679e5d867c2512e7128 100644 (file)
@@ -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) {
index 53722b21bd6dc156a3e91b5e066c6e0255c871d0..d9a525f4bd420eb1b7b95a36bb3af483477bb798 100644 (file)
@@ -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);