if (r < 0)
return log_error_errno(r, "Failed to open device %s: %m", arg_drm_device_path);
- if (!device_in_subsystem(drm_dev, "drm"))
+ r = device_in_subsystem(drm_dev, "drm");
+ if (r < 0)
+ return log_error_errno(r, "Failed to check if the device is a DRM device: %m");
+ if (r == 0)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Cannot read EDID from a non-DRM device '%s'", arg_drm_device_path);
r = edid_parse(drm_dev, &smbios_fields[CHID_EDID_PANEL]);
if (r < 0)
return r;
- if (device_in_subsystem(device, "drm")) {
+ r = device_in_subsystem(device, "drm");
+ if (r < 0)
+ return r;
+ if (r > 0) {
const char *s;
- r = sd_device_get_sysname(device, &s);
+ r = device_sysname_startswith_strv(device, STRV_MAKE("card"), &s);
if (r < 0)
return r;
-
- s = startswith(s, "card");
- if (!s)
+ if (r == 0)
return -ENODATA;
s += strspn(s, DIGITS);
continue;
}
- if (device_in_subsystem(device, "pci")) {
+ r = device_in_subsystem(device, "pci");
+ if (r < 0)
+ return r;
+ if (r > 0) {
uint32_t class;
r = device_get_sysattr_u32(device, "class", &class);
continue;
}
- if (device_in_subsystem(device, "platform")) {
+ r = device_in_subsystem(device, "platform");
+ if (r < 0)
+ return r;
+ if (r > 0) {
*ret = device;
return 0;
}
if (r < 0)
return log_device_debug_errno(device, r, "Failed to get sysname: %m");
- if (!device_in_subsystem(device, "backlight"))
+ r = device_in_subsystem(device, "leds");
+ if (r < 0)
+ return log_device_debug_errno(device, r, "Failed to check if device is in backlight subsystem: %m");
+ if (r > 0)
return true; /* We assume LED device is always valid. */
r = sd_device_get_sysattr_value(device, "type", &v);
if (r < 0)
return log_debug_errno(r, "Failed to add sysattr match: %m");
- if (device_in_subsystem(parent, "pci")) {
+ if (device_in_subsystem(parent, "pci") > 0) {
r = has_multiple_graphics_cards();
if (r < 0)
return log_debug_errno(r, "Failed to check if the system has multiple graphics cards: %m");
return false;
}
- if (device_in_subsystem(other_parent, "platform") && device_in_subsystem(parent, "pci")) {
+ if (device_in_subsystem(other_parent, "platform") > 0 && device_in_subsystem(parent, "pci") > 0) {
/* 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;
unsigned *brightness) {
unsigned new_brightness, min_brightness;
+ int r;
assert(device);
assert(brightness);
* state restoration. */
min_brightness = (unsigned) ((double) max_brightness * percent / 100);
- if (device_in_subsystem(device, "backlight"))
+ r = device_in_subsystem(device, "backlight");
+ if (r < 0)
+ return r;
+ if (r > 0)
min_brightness = MAX(1U, min_brightness);
new_brightness = CLAMP(*brightness, min_brightness, max_brightness);
return 0;
}
-static bool shall_clamp(sd_device *device, unsigned *ret) {
+static int shall_clamp(sd_device *device, unsigned *ret) {
const char *property, *s;
unsigned default_percent;
int r;
assert(device);
assert(ret);
- if (device_in_subsystem(device, "backlight")) {
+ r = device_in_subsystem(device, "backlight");
+ if (r < 0)
+ return r;
+ if (r > 0) {
property = "ID_BACKLIGHT_CLAMP";
default_percent = 5;
} else {
if (validate_device(device) == 0)
return 0;
- clamp = shall_clamp(device, &percent);
+ r = shall_clamp(device, &percent);
+ if (r < 0)
+ return r;
+ clamp = r;
r = read_saved_brightness(device, &brightness);
if (r < 0) {
FOREACH_DEVICE(e, d) {
_cleanup_(loop_device_unrefp) LoopDevice *entry_loop = NULL;
- if (!device_is_devtype(d, "disk")) /* Filter out partition block devices */
+ r = device_is_devtype(d, "disk");
+ if (r < 0) {
+ log_device_warning_errno(d, r, "Failed to check if device is a whole disk, skipping: %m");
continue;
+ }
+ if (r == 0)
+ continue; /* Filter out partition block devices */
r = loop_device_open(d, O_RDONLY, LOCK_SH, &entry_loop);
if (r < 0) {
HASHMAP_FOREACH_KEY(device, syspath, enumerator->devices_by_syspath) {
_cleanup_free_ char *p = NULL;
- if (!device_in_subsystem(device, *prioritized_subsystem))
+ r = device_in_subsystem(device, *prioritized_subsystem);
+ if (r < 0)
+ return r;
+ if (r == 0)
continue;
devices[n++] = sd_device_ref(device);
static int check_subsystem_filter(sd_device_monitor *m, sd_device *device) {
const char *s, *d;
+ int r;
assert(m);
assert(device);
return true;
HASHMAP_FOREACH_KEY(d, s, m->subsystem_filter) {
- if (!device_in_subsystem(device, s))
- continue;
-
- if (d && !device_is_devtype(device, d))
- continue;
-
- return true;
+ r = device_is_subsystem_devtype(device, s, d);
+ if (r != 0)
+ return r;
}
return false;
return log_device_debug_errno(device, SYNTHETIC_ERRNO(EINVAL),
"sd-device: Device created from strv or nulstr lacks devpath, subsystem, action or seqnum.");
- if (device_in_subsystem(device, "drivers")) {
+ r = device_in_subsystem(device, "drivers");
+ if (r < 0)
+ return log_device_debug_errno(device, r, "sd-device: Failed to check if the device is a driver: %m");
+ if (r > 0) {
r = device_set_drivers_subsystem(device);
if (r < 0)
return log_device_debug_errno(device, r,
return TAKE_PTR(strv);
}
-bool device_in_subsystem(sd_device *device, const char *subsystem) {
- const char *s = NULL;
+int device_in_subsystem_strv(sd_device *device, char * const *subsystems) {
+ const char *s;
+ int r;
assert(device);
- (void) sd_device_get_subsystem(device, &s);
- return streq_ptr(s, subsystem);
+ r = sd_device_get_subsystem(device, &s);
+ if (r == -ENOENT)
+ return strv_isempty(subsystems);
+ if (r < 0)
+ return r;
+ return strv_contains(subsystems, s);
}
-bool device_is_devtype(sd_device *device, const char *devtype) {
- const char *s = NULL;
+int device_is_devtype(sd_device *device, const char *devtype) {
+ const char *s;
+ int r;
assert(device);
- (void) sd_device_get_devtype(device, &s);
+ r = sd_device_get_devtype(device, &s);
+ if (r == -ENOENT)
+ return !devtype;
+ if (r < 0)
+ return r;
return streq_ptr(s, devtype);
}
+int device_is_subsystem_devtype(sd_device *device, const char *subsystem, const char *devtype) {
+ int r;
+
+ assert(device);
+
+ r = device_in_subsystem(device, subsystem);
+ if (r <= 0)
+ return r;
+
+ if (!devtype)
+ return true;
+
+ return device_is_devtype(device, devtype);
+}
+
+int device_sysname_startswith_strv(sd_device *device, char * const *prefixes, const char **ret_suffix) {
+ const char *sysname;
+ int r;
+
+ assert(device);
+
+ r = sd_device_get_sysname(device, &sysname);
+ if (r < 0)
+ return r;
+
+ const char *suffix = startswith_strv(sysname, prefixes);
+ if (ret_suffix)
+ *ret_suffix = suffix;
+ return !!suffix;
+}
+
bool device_property_can_set(const char *property) {
return property &&
!STR_IN_SET(property,
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);
+int device_in_subsystem_strv(sd_device *device, char * const *subsystems);
+#define device_in_subsystem(device, ...) \
+ device_in_subsystem_strv(device, STRV_MAKE(__VA_ARGS__))
+
+int device_is_devtype(sd_device *device, const char *devtype);
+
+int device_is_subsystem_devtype(sd_device *device, const char *subsystem, const char *devtype);
+
+int device_sysname_startswith_strv(sd_device *device, char * const *prefixes, const char **ret_suffix);
+#define device_sysname_startswith(device, ...) \
+ device_sysname_startswith_strv(device, STRV_MAKE(__VA_ARGS__), NULL)
bool device_property_can_set(const char *property) _pure_;
if (n != devnum)
return -ENXIO;
- if (device_in_subsystem(dev, "block") != !!S_ISBLK(mode))
+ r = device_in_subsystem(dev, "block");
+ if (r < 0)
+ return r;
+ if (r > 0 ? !S_ISBLK(mode) : !S_ISCHR(mode))
return -ENXIO;
*ret = TAKE_PTR(dev);
return r;
/* Check if the found device really has the expected subsystem and sysname, for safety. */
- if (!device_in_subsystem(new_device, subsystem))
- return 0;
+ r = device_in_subsystem(new_device, subsystem);
+ if (r <= 0)
+ return r;
const char *new_driver_subsystem = NULL;
(void) sd_device_get_driver_subsystem(new_device, &new_driver_subsystem);
major, strna(minor));
}
- if (device_in_subsystem(device, "drivers")) {
+ r = device_in_subsystem(device, "drivers");
+ if (r < 0)
+ log_device_debug_errno(device, r, "Failed to check if the device is a driver, ignoring: %m");
+ if (r > 0) {
r = device_set_drivers_subsystem(device);
if (r < 0)
log_device_debug_errno(device, r,
}
_public_ int sd_device_get_driver_subsystem(sd_device *device, const char **ret) {
+ int r;
+
assert_return(device, -EINVAL);
- if (!device_in_subsystem(device, "drivers"))
+ r = device_in_subsystem(device, "drivers");
+ if (r < 0)
+ return r;
+ if (r == 0)
return -ENOENT;
assert(device->driver_subsystem);
if (r < 0)
return r;
- if (!device_in_subsystem(device, subsystem))
- continue;
-
- if (devtype && !device_is_devtype(device, devtype))
+ r = device_is_subsystem_devtype(device, subsystem, devtype);
+ if (r < 0)
+ return r;
+ if (r == 0)
continue;
if (ret)
int ifindex, r;
if (sd_device_get_devnum(device, &devnum) >= 0) {
+ r = device_in_subsystem(device, "block");
+ if (r < 0)
+ return r;
+ char t = r > 0 ? 'b' : 'c';
+
/* use dev_t — b259:131072, c254:0 */
- if (asprintf(&id, "%c" DEVNUM_FORMAT_STR,
- device_in_subsystem(device, "block") ? 'b' : 'c',
- DEVNUM_FORMAT_VAL(devnum)) < 0)
+ if (asprintf(&id, "%c" DEVNUM_FORMAT_STR, t, DEVNUM_FORMAT_VAL(devnum)) < 0)
return -ENOMEM;
+
} else if (sd_device_get_ifindex(device, &ifindex) >= 0) {
/* use netdev ifindex — n3 */
if (asprintf(&id, "n%u", (unsigned) ifindex) < 0)
return -ENOMEM;
+
} else {
_cleanup_free_ char *sysname = NULL;
if (r == O_DIRECTORY)
return -EINVAL;
- if (device_in_subsystem(device, "drivers"))
+ r = device_in_subsystem(device, "drivers");
+ if (r < 0)
+ return r;
+ if (r > 0)
/* the 'drivers' pseudo-subsystem is special, and needs the real
* subsystem encoded as well */
id = strjoin("+drivers:", ASSERT_PTR(device->driver_subsystem), ":", sysname);
if (st.st_rdev != devnum)
return -ENXIO;
- if (device_in_subsystem(device, "block") ? !S_ISBLK(st.st_mode) : !S_ISCHR(st.st_mode))
+ r = device_in_subsystem(device, "block");
+ if (r < 0)
+ return r;
+ if (r > 0 ? !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. */
TEST(log_device_full) {
_cleanup_(sd_device_unrefp) sd_device *dev = NULL;
- int r;
(void) sd_device_new_from_subsystem_sysname(&dev, "net", "lo");
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(dev, level, 0, "test level=%d errno=0: %m", level);
- assert_se(r == 0);
-
- r = log_device_full_errno(dev, level, SYNTHETIC_ERRNO(ENODATA), "test level=%d errno=S(ENODATA).", level);
- assert_se(r == -ENODATA);
+ ASSERT_EQ(log_device_full_errno(dev, level, EUCLEAN, "test level=%d errno=EUCLEAN: %m", level), -EUCLEAN);
+ ASSERT_EQ(log_device_full_errno(dev, level, 0, "test level=%d errno=0: %m", level), 0);
+ ASSERT_EQ(log_device_full_errno(dev, level, SYNTHETIC_ERRNO(ENODATA), "test level=%d errno=S(ENODATA).", level), -ENODATA);
}
}
-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);
+TEST(device_in_subsystem_devtype_sysname_startswith) {
+ _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
- 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));
+ ASSERT_OK(sd_device_enumerator_new(&e));
+ ASSERT_OK(sd_device_enumerator_allow_uninitialized(e));
+ ASSERT_OK(sd_device_enumerator_add_match_subsystem(e, "block", true));
- dev = sd_device_unref(dev);
+ FOREACH_DEVICE(e, d) {
+ ASSERT_OK_ZERO(device_in_subsystem(d, "net"));
+ ASSERT_OK_POSITIVE(device_in_subsystem(d, "block"));
+ ASSERT_OK_ZERO(device_in_subsystem(d, "subsystem"));
+ ASSERT_OK_ZERO(device_in_subsystem(d, "", "net"));
+ ASSERT_OK_POSITIVE(device_in_subsystem(d, "net", "block"));
+ ASSERT_OK_POSITIVE(device_in_subsystem(d, "block", "subsystem"));
+ ASSERT_OK_POSITIVE(device_in_subsystem(d, "block", ""));
+ ASSERT_OK_ZERO(device_in_subsystem(d, ""));
+ ASSERT_OK_ZERO(device_in_subsystem(d, NULL));
+
+ ASSERT_OK_ZERO(device_in_subsystem_strv(d, STRV_MAKE("net")));
+ ASSERT_OK_ZERO(device_in_subsystem_strv(d, STRV_MAKE("", "net")));
+ ASSERT_OK_POSITIVE(device_in_subsystem_strv(d, STRV_MAKE("net", "block")));
+ ASSERT_OK_POSITIVE(device_in_subsystem_strv(d, STRV_MAKE("block", "subsystem")));
+ ASSERT_OK_POSITIVE(device_in_subsystem_strv(d, STRV_MAKE("block", "")));
+ ASSERT_OK_ZERO(device_in_subsystem_strv(d, STRV_MAKE("")));
+ ASSERT_OK_ZERO(device_in_subsystem_strv(d, STRV_MAKE(NULL)));
+ ASSERT_OK_ZERO(device_in_subsystem_strv(d, NULL));
- 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));
-}
+ const char *t;
+ ASSERT_OK(sd_device_get_devtype(d, &t));
+ ASSERT_OK_POSITIVE(device_is_devtype(d, t));
+ ASSERT_OK_ZERO(device_is_devtype(d, "hoge"));
+ ASSERT_OK_ZERO(device_is_devtype(d, ""));
+ ASSERT_OK_ZERO(device_is_devtype(d, NULL));
+
+ ASSERT_OK_POSITIVE(device_is_subsystem_devtype(d, "block", t));
+ ASSERT_OK_ZERO(device_is_subsystem_devtype(d, "block", "hoge"));
+ ASSERT_OK_ZERO(device_is_subsystem_devtype(d, "block", ""));
+ ASSERT_OK_POSITIVE(device_is_subsystem_devtype(d, "block", NULL));
+ ASSERT_OK_ZERO(device_is_subsystem_devtype(d, "net", t));
+ ASSERT_OK_ZERO(device_is_subsystem_devtype(d, "net", "hoge"));
+ ASSERT_OK_ZERO(device_is_subsystem_devtype(d, "net", ""));
+ ASSERT_OK_ZERO(device_is_subsystem_devtype(d, "net", NULL));
+ ASSERT_OK_ZERO(device_is_subsystem_devtype(d, "subsystem", t));
+ ASSERT_OK_ZERO(device_is_subsystem_devtype(d, "subsystem", "hoge"));
+ ASSERT_OK_ZERO(device_is_subsystem_devtype(d, "subsystem", ""));
+ ASSERT_OK_ZERO(device_is_subsystem_devtype(d, "subsystem", NULL));
+ ASSERT_OK_ZERO(device_is_subsystem_devtype(d, NULL, t));
+ ASSERT_OK_ZERO(device_is_subsystem_devtype(d, NULL, "hoge"));
+ ASSERT_OK_ZERO(device_is_subsystem_devtype(d, NULL, ""));
+ ASSERT_OK_ZERO(device_is_subsystem_devtype(d, NULL, NULL));
+
+ const char *s;
+ ASSERT_OK(sd_device_get_sysname(d, &s));
+ ASSERT_OK_POSITIVE(device_sysname_startswith(d, s));
+ ASSERT_OK_POSITIVE(device_sysname_startswith(d, CHAR_TO_STR(s[0])));
+ ASSERT_OK_POSITIVE(device_sysname_startswith(d, ""));
+ ASSERT_OK_ZERO(device_sysname_startswith(d, "00"));
+ }
-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);
+ if (sd_device_new_from_subsystem_sysname(&dev, "net", "lo") >= 0) {
+ ASSERT_OK_POSITIVE(device_in_subsystem(dev, "net"));
+ ASSERT_OK_ZERO(device_in_subsystem(dev, "block"));
+ ASSERT_OK_ZERO(device_in_subsystem(dev, "subsystem"));
+ ASSERT_OK_POSITIVE(device_in_subsystem(dev, "", "net"));
+ ASSERT_OK_POSITIVE(device_in_subsystem(dev, "net", "block"));
+ ASSERT_OK_ZERO(device_in_subsystem(dev, "block", "subsystem"));
+ ASSERT_OK_ZERO(device_in_subsystem(dev, "block", ""));
+ ASSERT_OK_ZERO(device_in_subsystem(dev, ""));
+ ASSERT_OK_ZERO(device_in_subsystem(dev, NULL));
+
+ ASSERT_OK_POSITIVE(device_in_subsystem_strv(dev, STRV_MAKE("net")));
+ ASSERT_OK_POSITIVE(device_in_subsystem_strv(dev, STRV_MAKE("", "net")));
+ ASSERT_OK_POSITIVE(device_in_subsystem_strv(dev, STRV_MAKE("net", "block")));
+ ASSERT_OK_ZERO(device_in_subsystem_strv(dev, STRV_MAKE("block", "subsystem")));
+ ASSERT_OK_ZERO(device_in_subsystem_strv(dev, STRV_MAKE("block", "")));
+ ASSERT_OK_ZERO(device_in_subsystem_strv(dev, STRV_MAKE("")));
+ ASSERT_OK_ZERO(device_in_subsystem_strv(dev, STRV_MAKE(NULL)));
+ ASSERT_OK_ZERO(device_in_subsystem_strv(dev, NULL));
+
+ ASSERT_OK_ZERO(device_is_devtype(dev, "hoge"));
+ ASSERT_OK_ZERO(device_is_devtype(dev, ""));
+ ASSERT_OK_POSITIVE(device_is_devtype(dev, NULL));
+
+ ASSERT_OK_ZERO(device_is_subsystem_devtype(dev, "block", "hoge"));
+ ASSERT_OK_ZERO(device_is_subsystem_devtype(dev, "block", ""));
+ ASSERT_OK_ZERO(device_is_subsystem_devtype(dev, "block", NULL));
+ ASSERT_OK_ZERO(device_is_subsystem_devtype(dev, "net", "hoge"));
+ ASSERT_OK_ZERO(device_is_subsystem_devtype(dev, "net", ""));
+ ASSERT_OK_POSITIVE(device_is_subsystem_devtype(dev, "net", NULL));
+ ASSERT_OK_ZERO(device_is_subsystem_devtype(dev, "subsystem", "hoge"));
+ ASSERT_OK_ZERO(device_is_subsystem_devtype(dev, "subsystem", ""));
+ ASSERT_OK_ZERO(device_is_subsystem_devtype(dev, "subsystem", NULL));
+ ASSERT_OK_ZERO(device_is_subsystem_devtype(dev, NULL, "hoge"));
+ ASSERT_OK_ZERO(device_is_subsystem_devtype(dev, NULL, ""));
+ ASSERT_OK_ZERO(device_is_subsystem_devtype(dev, NULL, NULL));
+
+ ASSERT_OK_POSITIVE(device_sysname_startswith(dev, "lo"));
+ ASSERT_OK_POSITIVE(device_sysname_startswith(dev, "l"));
+ ASSERT_OK_POSITIVE(device_sysname_startswith(dev, ""));
+ ASSERT_OK_ZERO(device_sysname_startswith(dev, "00"));
+
+ dev = sd_device_unref(dev);
+ }
- FOREACH_DEVICE(e, d) {
- const char *t;
+ ASSERT_OK(sd_device_new_from_syspath(&dev, "/sys/class/net"));
+ ASSERT_OK_ZERO(device_in_subsystem(dev, "net"));
+ ASSERT_OK_ZERO(device_in_subsystem(dev, "block"));
+ ASSERT_OK_POSITIVE(device_in_subsystem(dev, "subsystem"));
+ ASSERT_OK_ZERO(device_in_subsystem(dev, "", "net"));
+ ASSERT_OK_ZERO(device_in_subsystem(dev, "net", "block"));
+ ASSERT_OK_POSITIVE(device_in_subsystem(dev, "block", "subsystem"));
+ ASSERT_OK_ZERO(device_in_subsystem(dev, "block", ""));
+ ASSERT_OK_ZERO(device_in_subsystem(dev, ""));
+ ASSERT_OK_ZERO(device_in_subsystem(dev, NULL));
+
+ ASSERT_OK_ZERO(device_in_subsystem_strv(dev, STRV_MAKE("net")));
+ ASSERT_OK_ZERO(device_in_subsystem_strv(dev, STRV_MAKE("", "net")));
+ ASSERT_OK_ZERO(device_in_subsystem_strv(dev, STRV_MAKE("net", "block")));
+ ASSERT_OK_POSITIVE(device_in_subsystem_strv(dev, STRV_MAKE("block", "subsystem")));
+ ASSERT_OK_ZERO(device_in_subsystem_strv(dev, STRV_MAKE("block", "")));
+ ASSERT_OK_ZERO(device_in_subsystem_strv(dev, STRV_MAKE("")));
+ ASSERT_OK_ZERO(device_in_subsystem_strv(dev, STRV_MAKE(NULL)));
+ ASSERT_OK_ZERO(device_in_subsystem_strv(dev, NULL));
+
+ ASSERT_OK_ZERO(device_is_devtype(dev, "hoge"));
+ ASSERT_OK_ZERO(device_is_devtype(dev, ""));
+ ASSERT_OK_POSITIVE(device_is_devtype(dev, NULL));
+
+ ASSERT_OK_ZERO(device_is_subsystem_devtype(dev, "block", "hoge"));
+ ASSERT_OK_ZERO(device_is_subsystem_devtype(dev, "block", ""));
+ ASSERT_OK_ZERO(device_is_subsystem_devtype(dev, "block", NULL));
+ ASSERT_OK_ZERO(device_is_subsystem_devtype(dev, "subsystem", "hoge"));
+ ASSERT_OK_ZERO(device_is_subsystem_devtype(dev, "subsystem", ""));
+ ASSERT_OK_POSITIVE(device_is_subsystem_devtype(dev, "subsystem", NULL));
+ ASSERT_OK_ZERO(device_is_subsystem_devtype(dev, NULL, "hoge"));
+ ASSERT_OK_ZERO(device_is_subsystem_devtype(dev, NULL, ""));
+ ASSERT_OK_ZERO(device_is_subsystem_devtype(dev, NULL, NULL));
+
+ ASSERT_OK_POSITIVE(device_sysname_startswith(dev, "net"));
+ ASSERT_OK_POSITIVE(device_sysname_startswith(dev, "n"));
+ ASSERT_OK_POSITIVE(device_sysname_startswith(dev, ""));
+ ASSERT_OK_ZERO(device_sysname_startswith(dev, "00"));
- 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));
- }
+ dev = sd_device_unref(dev);
- 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));
+ ASSERT_OK(sd_device_new_from_syspath(&dev, "/sys/class"));
+ ASSERT_OK_ZERO(device_in_subsystem(dev, "net"));
+ ASSERT_OK_ZERO(device_in_subsystem(dev, "block"));
+ ASSERT_OK_ZERO(device_in_subsystem(dev, "subsystem"));
+ ASSERT_OK_ZERO(device_in_subsystem(dev, "", "net"));
+ ASSERT_OK_ZERO(device_in_subsystem(dev, "net", "block"));
+ ASSERT_OK_ZERO(device_in_subsystem(dev, "block", "subsystem"));
+ ASSERT_OK_ZERO(device_in_subsystem(dev, "block", ""));
+ ASSERT_OK_ZERO(device_in_subsystem(dev, ""));
+ ASSERT_OK_POSITIVE(device_in_subsystem(dev, NULL));
+
+ ASSERT_OK_ZERO(device_in_subsystem_strv(dev, STRV_MAKE("net")));
+ ASSERT_OK_ZERO(device_in_subsystem_strv(dev, STRV_MAKE("", "net")));
+ ASSERT_OK_ZERO(device_in_subsystem_strv(dev, STRV_MAKE("net", "block")));
+ ASSERT_OK_ZERO(device_in_subsystem_strv(dev, STRV_MAKE("block", "subsystem")));
+ ASSERT_OK_ZERO(device_in_subsystem_strv(dev, STRV_MAKE("block", "")));
+ ASSERT_OK_ZERO(device_in_subsystem_strv(dev, STRV_MAKE("")));
+ ASSERT_OK_POSITIVE(device_in_subsystem_strv(dev, STRV_MAKE(NULL)));
+ ASSERT_OK_POSITIVE(device_in_subsystem_strv(dev, NULL));
+
+ ASSERT_OK_ZERO(device_is_devtype(dev, "hoge"));
+ ASSERT_OK_ZERO(device_is_devtype(dev, ""));
+ ASSERT_OK_POSITIVE(device_is_devtype(dev, NULL));
+
+ ASSERT_OK_ZERO(device_is_subsystem_devtype(dev, "block", "hoge"));
+ ASSERT_OK_ZERO(device_is_subsystem_devtype(dev, "block", ""));
+ ASSERT_OK_ZERO(device_is_subsystem_devtype(dev, "block", NULL));
+ ASSERT_OK_ZERO(device_is_subsystem_devtype(dev, "subsystem", "hoge"));
+ ASSERT_OK_ZERO(device_is_subsystem_devtype(dev, "subsystem", ""));
+ ASSERT_OK_ZERO(device_is_subsystem_devtype(dev, "subsystem", NULL));
+ ASSERT_OK_ZERO(device_is_subsystem_devtype(dev, NULL, "hoge"));
+ ASSERT_OK_ZERO(device_is_subsystem_devtype(dev, NULL, ""));
+ ASSERT_OK_POSITIVE(device_is_subsystem_devtype(dev, NULL, NULL));
+
+ ASSERT_OK_POSITIVE(device_sysname_startswith(dev, "class"));
+ ASSERT_OK_POSITIVE(device_sysname_startswith(dev, "c"));
+ ASSERT_OK_POSITIVE(device_sysname_startswith(dev, ""));
+ ASSERT_OK_ZERO(device_sysname_startswith(dev, "00"));
}
static int intro(void) {
TEST(sd_device_enumerator_add_all_parents) {
_cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
+ int r;
/* STEP 1: enumerate all block devices without all_parents() */
ASSERT_OK(sd_device_enumerator_new(&e));
unsigned devices_count_with_parents = 0;
unsigned devices_count_without_parents = 0;
FOREACH_DEVICE(e, dev) {
- ASSERT_TRUE(device_in_subsystem(dev, "block"));
- ASSERT_TRUE(device_is_devtype(dev, "partition"));
+ ASSERT_OK_POSITIVE(device_is_subsystem_devtype(dev, "block", "partition"));
devices_count_without_parents++;
}
unsigned not_filtered_parent_count = 0;
FOREACH_DEVICE(e, dev) {
- if (!device_in_subsystem(dev, "block") || !device_is_devtype(dev, "partition"))
+ ASSERT_OK(r = device_is_subsystem_devtype(dev, "block", "partition"));
+ if (r == 0)
not_filtered_parent_count++;
devices_count_with_parents++;
}
return r;
FOREACH_DEVICE(e, d) {
- const char *status, *enabled, *dash, *nn;
sd_device *p;
if (sd_device_get_parent(d, &p) < 0)
/* 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 (!device_in_subsystem(p, "drm"))
+ r = device_in_subsystem(p, "drm");
+ if (r < 0)
+ return r;
+ if (r == 0)
continue;
- if (sd_device_get_sysname(d, &nn) < 0)
- continue;
+ const char *nn;
+ r = sd_device_get_sysname(d, &nn);
+ if (r < 0)
+ return r;
/* Ignore internal displays: the type is encoded in the sysfs name, as the second dash
* separated item (the first is the card name, the last the connector number). We implement a
* deny list of external displays here, rather than an allow list of internal ones, to ensure
* we don't block suspends too eagerly. */
- dash = strchr(nn, '-');
+ const char *dash = strchr(nn, '-');
if (!dash)
continue;
continue;
/* Ignore ports that are not enabled */
- if (sd_device_get_sysattr_value(d, "enabled", &enabled) < 0 || !streq(enabled, "enabled"))
+ const char *enabled;
+ r = sd_device_get_sysattr_value(d, "enabled", &enabled);
+ if (r == -ENOENT)
+ continue;
+ if (r < 0)
+ return r;
+ if (!streq(enabled, "enabled"))
continue;
- /* We count any connector which is not explicitly
- * "disconnected" as connected. */
- if (sd_device_get_sysattr_value(d, "status", &status) < 0 || !streq(status, "disconnected"))
+ /* We count any connector which is not explicitly "disconnected" as connected. */
+ const char *status = NULL;
+ r = sd_device_get_sysattr_value(d, "status", &status);
+ if (r < 0 && r != -ENOENT)
+ return r;
+ if (!streq_ptr(status, "disconnected"))
n++;
}
}
static DeviceType detect_device_type(sd_device *dev) {
- const char *sysname;
-
- if (sd_device_get_sysname(dev, &sysname) < 0)
- return DEVICE_TYPE_UNKNOWN;
-
- if (device_in_subsystem(dev, "drm")) {
- if (startswith(sysname, "card"))
+ if (device_in_subsystem(dev, "drm") > 0) {
+ if (device_sysname_startswith(dev, "card") > 0)
return DEVICE_TYPE_DRM;
+ return DEVICE_TYPE_UNKNOWN;
+ }
- } else if (device_in_subsystem(dev, "input")) {
- if (startswith(sysname, "event"))
+ if (device_in_subsystem(dev, "input") > 0) {
+ if (device_sysname_startswith(dev, "event") > 0)
return DEVICE_TYPE_EVDEV;
- } else if (device_in_subsystem(dev, "hidraw")) {
- if (startswith(sysname, "hidraw"))
+ return DEVICE_TYPE_UNKNOWN;
+ }
+
+ if (device_in_subsystem(dev, "hidraw") > 0) {
+ if (device_sysname_startswith(dev, "hidraw") > 0)
return DEVICE_TYPE_HIDRAW;
+ return DEVICE_TYPE_UNKNOWN;
}
return DEVICE_TYPE_UNKNOWN;
static int manager_dispatch_vcsa_udev(sd_device_monitor *monitor, sd_device *device, void *userdata) {
Manager *m = ASSERT_PTR(userdata);
- const char *name;
+ int r;
assert(device);
/* Whenever a VCSA device is removed try to reallocate our
* VTs, to make sure our auto VTs never go away. */
- if (sd_device_get_sysname(device, &name) >= 0 &&
- startswith(name, "vcsa") &&
- device_for_action(device, SD_DEVICE_REMOVE))
+ r = device_sysname_startswith(device, "vcsa");
+ if (r < 0) {
+ log_device_debug_errno(device, r, "Failed to check device sysname, ignoring: %m");
+ return 0;
+ }
+ if (r == 0)
+ return 0;
+
+ if (device_for_action(device, SD_DEVICE_REMOVE))
seat_preallocate_vts(m->seat0);
return 0;
static int acquire_removable(sd_device *d) {
const char *v;
+ int r;
assert(d);
if (sd_device_get_sysattr_value(d, "removable", &v) >= 0)
break;
- if (sd_device_get_parent(d, &d) < 0)
+ r = sd_device_get_parent(d, &d);
+ if (r == -ENODEV)
return 0;
+ if (r < 0)
+ return r;
- if (!device_in_subsystem(d, "block"))
+ r = device_in_subsystem(d, "block");
+ if (r < 0)
+ return r;
+ if (r == 0)
return 0;
}
if (!link->sd_device)
return;
- if (!device_is_devtype(link->sd_device, "wlan"))
+ if (device_is_devtype(link->sd_device, "wlan") <= 0)
return;
r = sd_genl_socket_open(&genl);
if (r < 0)
return log_device_warning_errno(device, r, "Failed to get udev action, ignoring: %m");
- if (device_in_subsystem(device, "net"))
+ if (device_in_subsystem(device, "net") > 0)
r = manager_udev_process_link(m, device, action);
- else if (device_in_subsystem(device, "ieee80211"))
+ else if (device_in_subsystem(device, "ieee80211") > 0)
r = manager_udev_process_wiphy(m, device, action);
- else if (device_in_subsystem(device, "rfkill"))
+ else if (device_in_subsystem(device, "rfkill") > 0)
r = manager_udev_process_rfkill(m, device, action);
if (r < 0)
log_device_warning_errno(device, r, "Failed to process \"%s\" uevent, ignoring: %m",
if (!link->dev)
return -ENODEV;
- if (!device_is_devtype(link->dev, "wlan"))
+ r = device_is_devtype(link->dev, "wlan");
+ if (r < 0)
+ return r;
+ if (r == 0)
return -EOPNOTSUPP;
r = sd_device_new_child(&phy, link->dev, "phy80211");
}
if (FLAGS_SET(flags, BLOCKDEV_LIST_IGNORE_ZRAM)) {
- const char *dn;
-
- r = sd_device_get_sysname(dev, &dn);
+ r = device_sysname_startswith(dev, "zram");
if (r < 0) {
- log_warning_errno(r, "Failed to get device name of discovered block device '%s', ignoring: %m", node);
+ log_warning_errno(r, "Failed to check device name of discovered block device '%s', ignoring: %m", node);
continue;
}
-
- if (startswith(dn, "zram"))
+ if (r > 0)
continue;
}
}
int block_device_is_whole_disk(sd_device *dev) {
+ int r;
+
assert(dev);
- if (!device_in_subsystem(dev, "block"))
+ r = device_in_subsystem(dev, "block");
+ if (r < 0)
+ return r;
+ if (r == 0)
return -ENOTBLK;
return device_is_devtype(dev, "disk");
/* Partition block devices never have partition scanning on, there's no concept of sub-partitions for
* partitions. */
- if (device_is_devtype(dev, "partition"))
+ r = device_is_devtype(dev, "partition");
+ if (r < 0)
+ return r;
+ if (r > 0)
return false;
/* For loopback block device, especially for v5.19 or newer. Even if this is enabled, we also need to
if (sd_device_get_devname(dev, &devname) >= 0) {
mode_t mode = 0600;
- if (device_in_subsystem(dev, "block"))
+ r = device_in_subsystem(dev, "block");
+ if (r < 0)
+ return r;
+ if (r > 0)
mode |= S_IFBLK;
else
mode |= S_IFCHR;
_cleanup_free_ char *fn = NULL;
struct loop_info64 info;
- const char *name;
int r;
assert(dev);
* sometimes, depending on context, it's a good thing to return the string userspace can freely pick
* over the string automatically generated by the kernel. */
- r = sd_device_get_sysname(dev, &name);
+ r = device_sysname_startswith(dev, "loop");
if (r < 0)
return r;
-
- if (!startswith(name, "loop"))
+ if (r == 0)
goto notloop;
if (ioctl(fd, LOOP_GET_STATUS64, &info) < 0) {
if (r < 0)
return log_device_debug_errno(dev, r, "Failed to get device node: %m");
- if (!device_in_subsystem(dev, "block"))
- return log_device_debug_errno(dev, SYNTHETIC_ERRNO(EINVAL), "Invoked on non-block device '%s', refusing: %m", devnode);
- if (!device_is_devtype(dev, "partition"))
- return log_device_debug_errno(dev, SYNTHETIC_ERRNO(EINVAL), "Invoked on non-partition block device '%s', refusing: %m", devnode);
+ r = device_is_subsystem_devtype(dev, "block", "partition");
+ if (r < 0)
+ return log_device_debug_errno(dev, r, "Failed to check if the device '%s' is a partition, refusing: %m", devnode);
+ if (r == 0)
+ return log_device_debug_errno(dev, SYNTHETIC_ERRNO(EINVAL), "Invoked on non-partition device '%s', refusing.", devnode);
sd_device *parent;
r = sd_device_get_parent(dev, &parent);
if (r < 0)
return log_error_errno(r, "Failed to get parent of device '%s': %m", devnode);
- if (!device_in_subsystem(parent, "block"))
- return log_device_debug_errno(dev, SYNTHETIC_ERRNO(EINVAL), "Parent of block device '%s' is not a block device, refusing: %m", devnode);
- if (!device_is_devtype(parent, "disk"))
- return log_device_debug_errno(dev, SYNTHETIC_ERRNO(EINVAL), "Parent of block device '%s' is not a whole block device, refusing: %m", devnode);
+ r = device_is_subsystem_devtype(parent, "block", "disk");
+ if (r < 0)
+ return log_device_debug_errno(dev, r, "Failed to check if the parent of block device '%s' is a whole block device, refusing: %m", devnode);
+ if (r == 0)
+ return log_device_debug_errno(dev, SYNTHETIC_ERRNO(EINVAL), "Parent of block device '%s' is not a whole block device, refusing.", devnode);
const char *partn;
r = sd_device_get_property_value(dev, "PARTN", &partn);
const char *modalias = NULL;
/* look only at devices of a specific subsystem */
- if (subsystem && !device_in_subsystem(d, subsystem))
- goto next;
+ if (subsystem) {
+ r = device_in_subsystem(d, subsystem);
+ if (r < 0)
+ return r;
+ if (r == 0)
+ goto next;
+ }
(void) sd_device_get_property_value(d, "MODALIAS", &modalias);
- if (device_in_subsystem(d, "usb") && device_is_devtype(d, "usb_device")) {
+ r = device_is_subsystem_devtype(d, "usb", "usb_device");
+ if (r < 0)
+ return r;
+ if (r > 0) {
/* if the usb_device does not have a modalias, compose one */
if (!modalias)
modalias = modalias_usb(d, s, sizeof(s));
bitmask_key[NBITS(KEY_MAX)],
bitmask_rel[NBITS(REL_MAX)],
bitmask_props[NBITS(INPUT_PROP_MAX)];
- const char *sysname;
bool is_pointer, is_key;
+ int r;
/* walk up the parental chain until we find the real input device; the
* argument is very likely a subdevice of this, like eventN */
udev_builtin_add_property(event, "ID_INPUT_SWITCH", "1");
}
- if (sd_device_get_sysname(dev, &sysname) >= 0 &&
- startswith(sysname, "event"))
+ r = device_sysname_startswith(dev, "event");
+ if (r < 0)
+ return r;
+ if (r > 0)
extract_info(event);
return 0;
#define ONBOARD_14BIT_INDEX_MAX ((1U << 14) - 1)
#define ONBOARD_16BIT_INDEX_MAX ((1U << 16) - 1)
-/* skip intermediate virtio devices */
-static sd_device *device_skip_virtio(sd_device *dev) {
- /* there can only ever be one virtio bus per parent device, so we can
- * safely ignore any virtio buses. see
+static int device_get_parent_skip_virtio(sd_device *dev, sd_device **ret) {
+ int r;
+
+ assert(dev);
+ assert(ret);
+
+ /* This provides the parent device, but skips intermediate virtio devices. There can only ever be one
+ * virtio bus per parent device, so we can safely ignore any virtio buses. See
* https://lore.kernel.org/virtualization/CAPXgP137A=CdmggtVPUZXbnpTbU9Tewq-sOjg9T8ohYktct1kQ@mail.gmail.com/ */
- while (dev) {
- if (!device_in_subsystem(dev, "virtio"))
- break;
- if (sd_device_get_parent(dev, &dev) < 0)
- return NULL;
- }
+ for (;;) {
+ r = sd_device_get_parent(dev, &dev);
+ if (r < 0)
+ return r;
- return dev;
+ r = device_in_subsystem(dev, "virtio");
+ if (r < 0)
+ return r;
+ if (r == 0) {
+ *ret = dev;
+ return 0;
+ }
+ }
}
static int get_matching_parent(
assert(dev);
- r = sd_device_get_parent(dev, &parent);
+ if (skip_virtio)
+ r = device_get_parent_skip_virtio(dev, &parent);
+ else
+ r = sd_device_get_parent(dev, &parent);
if (r < 0)
return r;
- if (skip_virtio) {
- /* skip virtio subsystem if present */
- parent = device_skip_virtio(parent);
- if (!parent)
- return -ENODEV;
- }
-
/* 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;
- }
+ r = device_in_subsystem_strv(parent, parent_subsystems);
+ if (r < 0)
+ return r;
+ if (r == 0)
+ return -ENODEV;
- return -ENODEV;
+ if (ret)
+ *ret = parent;
+ return 0;
}
static int get_first_syspath_component(sd_device *dev, const char *prefix, char **ret) {
/* handle only ARPHRD_ETHER, ARPHRD_SLIP and ARPHRD_INFINIBAND devices */
switch (iftype) {
case ARPHRD_ETHER: {
- if (device_is_devtype(dev, "wlan"))
+ if (device_is_devtype(dev, "wlan") > 0)
*ret = "wl";
- else if (device_is_devtype(dev, "wwan"))
+ else if (device_is_devtype(dev, "wwan") > 0)
*ret = "ww";
else
*ret = "en";
*/
for (parent = dev; ; ) {
- if (!device_in_subsystem(parent, subsys))
+ if (device_in_subsystem(parent, subsys) <= 0)
break;
dev = parent;
static sd_device* handle_scsi(sd_device *parent, char **path, char **compat_path, bool *supported_parent) {
const char *id, *name;
- if (!device_is_devtype(parent, "scsi_device"))
+ if (device_is_devtype(parent, "scsi_device") <= 0)
return parent;
/* firewire */
const char *str, *port;
int r;
- if (!device_is_devtype(parent, "usb_interface") && !device_is_devtype(parent, "usb_device"))
+ if (device_is_devtype(parent, "usb_interface") <= 0 && device_is_devtype(parent, "usb_device") <= 0)
return parent;
if (sd_device_get_sysname(parent, &str) < 0)
if (sd_device_get_sysname(parent, &sysname) < 0) {
;
- } else if (device_in_subsystem(parent, "scsi_tape")) {
+ } else if (device_in_subsystem(parent, "scsi_tape") > 0) {
handle_scsi_tape(parent, &path);
- } else if (device_in_subsystem(parent, "scsi")) {
+ } else if (device_in_subsystem(parent, "scsi") > 0) {
parent = handle_scsi(parent, &path, &compat_path, &supported_parent);
supported_transport = true;
- } else if (device_in_subsystem(parent, "cciss")) {
+ } else if (device_in_subsystem(parent, "cciss") > 0) {
parent = handle_cciss(parent, &path);
supported_transport = true;
- } else if (device_in_subsystem(parent, "usb")) {
+ } else if (device_in_subsystem(parent, "usb") > 0) {
parent = handle_usb(parent, &path);
supported_transport = true;
- } else if (device_in_subsystem(parent, "bcma")) {
+ } else if (device_in_subsystem(parent, "bcma") > 0) {
parent = handle_bcma(parent, &path);
supported_transport = true;
- } else if (device_in_subsystem(parent, "serio")) {
+ } else if (device_in_subsystem(parent, "serio") > 0) {
const char *sysnum;
if (sd_device_get_sysnum(parent, &sysnum) >= 0) {
path_prepend(&path, "serio-%s", sysnum);
parent = skip_subsystem(parent, "serio");
}
- } else if (device_in_subsystem(parent, "pci")) {
+ } else if (device_in_subsystem(parent, "pci") > 0) {
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 (device_in_subsystem(parent, "platform")) {
+ } else if (device_in_subsystem(parent, "platform") > 0) {
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 (device_in_subsystem(parent, "amba")) {
+ } else if (device_in_subsystem(parent, "amba") > 0) {
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 (device_in_subsystem(parent, "acpi")) {
+ } else if (device_in_subsystem(parent, "acpi") > 0) {
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 (device_in_subsystem(parent, "xen")) {
+ } else if (device_in_subsystem(parent, "xen") > 0) {
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 (device_in_subsystem(parent, "virtio")) {
+ } else if (device_in_subsystem(parent, "virtio") > 0) {
parent = skip_subsystem(parent, "virtio");
supported_transport = true;
- } else if (device_in_subsystem(parent, "scm")) {
+ } else if (device_in_subsystem(parent, "scm") > 0) {
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 (device_in_subsystem(parent, "ccw")) {
+ } else if (device_in_subsystem(parent, "ccw") > 0) {
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 (device_in_subsystem(parent, "ccwgroup")) {
+ } else if (device_in_subsystem(parent, "ccwgroup") > 0) {
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 (device_in_subsystem(parent, "ap")) {
+ } else if (device_in_subsystem(parent, "ap") > 0) {
parent = handle_ap(parent, &path);
supported_transport = true;
supported_parent = true;
- } else if (device_in_subsystem(parent, "iucv")) {
+ } else if (device_in_subsystem(parent, "iucv") > 0) {
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 (device_in_subsystem(parent, "nvme") || device_in_subsystem(parent, "nvme-subsystem")) {
+ } else if (device_in_subsystem(parent, "nvme", "nvme-subsystem") > 0) {
const char *nsid;
if (sd_device_get_sysattr_value(dev, "nsid", &nsid) >= 0) {
if (compat_path)
path_prepend(&compat_path, "nvme-%s", nsid);
- if (device_in_subsystem(parent, "nvme-subsystem")) {
+ if (device_in_subsystem(parent, "nvme-subsystem") > 0) {
r = find_real_nvme_parent(dev, &dev_other_branch);
if (r < 0)
return r;
supported_parent = true;
supported_transport = true;
}
- } else if (device_in_subsystem(parent, "spi")) {
+ } else if (device_in_subsystem(parent, "spi") > 0) {
const char *sysnum;
if (sd_device_get_sysnum(parent, &sysnum) >= 0) {
* devices do not expose their buses and do not provide a unique
* and predictable name that way.
*/
- if (device_in_subsystem(dev, "block") && !supported_transport)
+ if (device_in_subsystem(dev, "block") > 0 && !supported_transport)
return -ENOENT;
add_id_with_usb_revision(event, path);
if (r < 0)
return r;
- return device_path_make_major_minor(device_in_subsystem(dev, "block") ? S_IFBLK : S_IFCHR, devnum, ret);
+ r = device_in_subsystem(dev, "block");
+ if (r < 0)
+ return r;
+
+ return device_path_make_major_minor(r > 0 ? S_IFBLK : S_IFCHR, devnum, ret);
}
int udev_node_update(sd_device *dev, sd_device *dev_old) {
assert(manager);
assert(dev);
- const char *sysname;
- r = sd_device_get_sysname(dev, &sysname);
+ r = device_sysname_startswith(dev, "dm-");
if (r < 0)
return r;
+ if (r > 0)
+ return synthesize_change_one(dev, dev);
- if (startswith(sysname, "dm-") || block_device_is_whole_disk(dev) <= 0)
+ r = block_device_is_whole_disk(dev);
+ if (r < 0)
+ return r;
+ if (r == 0)
return synthesize_change_one(dev, dev);
_cleanup_(pidref_done) PidRef pidref = PIDREF_NULL;
if (device_for_action(dev, SD_DEVICE_REMOVE))
goto irrelevant;
- r = sd_device_get_sysname(dev, &val);
- if (r < 0)
- return log_device_debug_errno(dev, r, "Failed to get sysname: %m");
-
/* Exclude the following devices:
* For "dm-", see the comment added by e918a1b5a94f270186dca59156354acd2a596494.
* For "md", see the commit message of 2e5b17d01347d3c3118be2b8ad63d20415dbb1f0,
* but not sure the assumption is still valid even when partitions are created on the md
* devices, surprisingly which seems to be possible, see PR #22973.
* For "drbd", see the commit message of fee854ee8ccde0cd28e0f925dea18cce35f3993d. */
- if (STARTSWITH_SET(val, "dm-", "md", "drbd"))
+ r = device_sysname_startswith(dev, "dm-", "md", "drbd");
+ if (r < 0)
+ return log_device_debug_errno(dev, r, "Failed to check sysname: %m");
+ if (r > 0)
goto irrelevant;
r = block_device_get_whole_disk(dev, &dev);
}
static int worker_mark_block_device_read_only(sd_device *dev) {
- _cleanup_close_ int fd = -EBADF;
- const char *val;
- int state = 1, r;
+ int r;
assert(dev);
if (!device_for_action(dev, SD_DEVICE_ADD))
return 0;
- if (!device_in_subsystem(dev, "block"))
- return 0;
-
- r = sd_device_get_sysname(dev, &val);
+ r = device_in_subsystem(dev, "block");
if (r < 0)
- return log_device_debug_errno(dev, r, "Failed to get sysname: %m");
+ return r;
+ if (r == 0)
+ return 0;
/* Exclude synthetic devices for now, this is supposed to be a safety feature to avoid modification
* of physical devices, and what sits on top of those doesn't really matter if we don't allow the
* underlying block devices to receive changes. */
- if (STARTSWITH_SET(val, "dm-", "md", "drbd", "loop", "nbd", "zram"))
+ r = device_sysname_startswith(dev, "dm-", "md", "drbd", "loop", "nbd", "zram");
+ if (r < 0)
+ return log_device_debug_errno(dev, r, "Failed to check sysname: %m");
+ if (r > 0)
return 0;
- fd = sd_device_open(dev, O_RDONLY|O_CLOEXEC|O_NONBLOCK|O_NOCTTY);
+ const char *val;
+ r = sd_device_get_devname(dev, &val);
+ if (r < 0)
+ return log_device_debug_errno(dev, r, "Failed to get device node: %m");
+
+ _cleanup_close_ int fd = sd_device_open(dev, O_RDONLY|O_CLOEXEC|O_NONBLOCK|O_NOCTTY);
if (fd < 0)
return log_device_debug_errno(dev, fd, "Failed to open '%s', ignoring: %m", val);
+ int state = 1;
if (ioctl(fd, BLKROSET, &state) < 0)
return log_device_warning_errno(dev, errno, "Failed to mark block device '%s' read-only: %m", val);