diff --git a/src/libsystemd/sd-device/device-util.c b/src/libsystemd/sd-device/device-util.c index 123629c3560..3cfdf736c12 100644 --- a/src/libsystemd/sd-device/device-util.c +++ b/src/libsystemd/sd-device/device-util.c @@ -131,13 +131,14 @@ 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; - +bool device_in_subsystems(sd_device *device, char * const *subsystems) { assert(device); - (void) sd_device_get_subsystem(device, &s); - return streq_ptr(s, subsystem); + const char *s; + if (sd_device_get_subsystem(device, &s) < 0) + return strv_isempty(subsystems); + + return strv_contains(subsystems, s); } bool device_is_devtype(sd_device *device, const char *devtype) { diff --git a/src/libsystemd/sd-device/device-util.h b/src/libsystemd/sd-device/device-util.h index 070e564a52c..c09b8425fe9 100644 --- a/src/libsystemd/sd-device/device-util.h +++ b/src/libsystemd/sd-device/device-util.h @@ -104,7 +104,10 @@ int device_open_from_devnum(mode_t mode, dev_t devnum, int flags, char **ret_dev char** device_make_log_fields(sd_device *device); -bool device_in_subsystem(sd_device *device, const char *subsystem); +bool device_in_subsystems(sd_device *device, char * const *subsystems); +static inline bool device_in_subsystem(sd_device *device, const char *subsystem) { + return device_in_subsystems(device, STRV_MAKE(subsystem)); +} bool device_is_devtype(sd_device *device, const char *devtype); static inline bool device_property_can_set(const char *property) { diff --git a/src/libsystemd/sd-device/test-device-util.c b/src/libsystemd/sd-device/test-device-util.c index 030a861e29d..a80bd83386a 100644 --- a/src/libsystemd/sd-device/test-device-util.c +++ b/src/libsystemd/sd-device/test-device-util.c @@ -28,6 +28,15 @@ TEST(device_in_subsystem) { ASSERT_FALSE(device_in_subsystem(dev, "")); ASSERT_FALSE(device_in_subsystem(dev, NULL)); + ASSERT_TRUE(device_in_subsystems(dev, STRV_MAKE("net"))); + ASSERT_TRUE(device_in_subsystems(dev, STRV_MAKE("", "net"))); + ASSERT_TRUE(device_in_subsystems(dev, STRV_MAKE("net", "disk"))); + ASSERT_FALSE(device_in_subsystems(dev, STRV_MAKE("disk", "subsystem"))); + ASSERT_FALSE(device_in_subsystems(dev, STRV_MAKE("disk", ""))); + ASSERT_FALSE(device_in_subsystems(dev, STRV_MAKE(""))); + ASSERT_FALSE(device_in_subsystems(dev, STRV_MAKE(NULL))); + ASSERT_FALSE(device_in_subsystems(dev, NULL)); + dev = sd_device_unref(dev); } @@ -38,6 +47,15 @@ TEST(device_in_subsystem) { ASSERT_FALSE(device_in_subsystem(dev, "")); ASSERT_FALSE(device_in_subsystem(dev, NULL)); + ASSERT_FALSE(device_in_subsystems(dev, STRV_MAKE("net"))); + ASSERT_FALSE(device_in_subsystems(dev, STRV_MAKE("", "net"))); + ASSERT_FALSE(device_in_subsystems(dev, STRV_MAKE("net", "disk"))); + ASSERT_TRUE(device_in_subsystems(dev, STRV_MAKE("disk", "subsystem"))); + ASSERT_FALSE(device_in_subsystems(dev, STRV_MAKE("disk", ""))); + ASSERT_FALSE(device_in_subsystems(dev, STRV_MAKE(""))); + ASSERT_FALSE(device_in_subsystems(dev, STRV_MAKE(NULL))); + ASSERT_FALSE(device_in_subsystems(dev, NULL)); + dev = sd_device_unref(dev); ASSERT_OK(sd_device_new_from_syspath(&dev, "/sys/class")); @@ -46,6 +64,15 @@ TEST(device_in_subsystem) { ASSERT_FALSE(device_in_subsystem(dev, "subsystem")); ASSERT_FALSE(device_in_subsystem(dev, "")); ASSERT_TRUE(device_in_subsystem(dev, NULL)); + + ASSERT_FALSE(device_in_subsystems(dev, STRV_MAKE("net"))); + ASSERT_FALSE(device_in_subsystems(dev, STRV_MAKE("", "net"))); + ASSERT_FALSE(device_in_subsystems(dev, STRV_MAKE("net", "disk"))); + ASSERT_FALSE(device_in_subsystems(dev, STRV_MAKE("disk", "subsystem"))); + ASSERT_FALSE(device_in_subsystems(dev, STRV_MAKE("disk", ""))); + ASSERT_FALSE(device_in_subsystems(dev, STRV_MAKE(""))); + ASSERT_TRUE(device_in_subsystems(dev, STRV_MAKE(NULL))); + ASSERT_TRUE(device_in_subsystems(dev, NULL)); } TEST(device_is_devtype) {