device-util: introduce several more helper functions

This also makes device_in_subsystem() and device_is_devtype() return
negative error on critical error
This commit is contained in:
Yu Watanabe
2025-02-27 13:19:03 +09:00
parent b7d60b2966
commit ab1bd9daed
30 changed files with 533 additions and 237 deletions

View File

@@ -361,7 +361,10 @@ int verb_chid(int argc, char *argv[], void *userdata) {
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]);

View File

@@ -103,15 +103,16 @@ static int find_pci_or_platform_parent(sd_device *device, sd_device **ret) {
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);
@@ -122,7 +123,10 @@ static int find_pci_or_platform_parent(sd_device *device, sd_device **ret) {
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);
@@ -140,7 +144,10 @@ static int find_pci_or_platform_parent(sd_device *device, sd_device **ret) {
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;
}
@@ -197,7 +204,10 @@ static int validate_device(sd_device *device) {
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);
@@ -242,7 +252,7 @@ static int validate_device(sd_device *device) {
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");
@@ -284,7 +294,7 @@ static int validate_device(sd_device *device) {
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;
@@ -335,6 +345,7 @@ static int clamp_brightness(
unsigned *brightness) {
unsigned new_brightness, min_brightness;
int r;
assert(device);
assert(brightness);
@@ -345,7 +356,10 @@ static int clamp_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);
@@ -361,7 +375,7 @@ static int clamp_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;
@@ -369,7 +383,10 @@ static bool shall_clamp(sd_device *device, unsigned *ret) {
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 {
@@ -561,7 +578,10 @@ static int verb_load(int argc, char *argv[], void *userdata) {
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) {

View File

@@ -2084,8 +2084,13 @@ static int action_detach(const char *path) {
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) {

View File

@@ -394,7 +394,10 @@ static int enumerator_sort_devices(sd_device_enumerator *enumerator) {
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);

View File

@@ -471,6 +471,7 @@ DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC(sd_device_monitor, sd_device_monitor, devic
static int check_subsystem_filter(sd_device_monitor *m, sd_device *device) {
const char *s, *d;
int r;
assert(m);
assert(device);
@@ -479,13 +480,9 @@ static int check_subsystem_filter(sd_device_monitor *m, sd_device *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;

View File

@@ -441,7 +441,10 @@ static int device_verify(sd_device *device) {
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,

View File

@@ -132,24 +132,65 @@ 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;
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,

View File

@@ -103,7 +103,16 @@ 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_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_;

View File

@@ -314,7 +314,10 @@ int device_new_from_mode_and_devnum(sd_device **ret, mode_t mode, dev_t devnum)
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);
@@ -414,8 +417,9 @@ static int device_new_from_path_join(
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);
@@ -843,7 +847,10 @@ int device_read_uevent_file(sd_device *device) {
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,
@@ -1245,9 +1252,14 @@ _public_ int sd_device_get_subsystem(sd_device *device, const char **ret) {
}
_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);
@@ -1287,10 +1299,10 @@ _public_ int sd_device_get_parent_with_subsystem_devtype(sd_device *device, cons
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)
@@ -1710,15 +1722,20 @@ _public_ int sd_device_get_device_id(sd_device *device, const char **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;
@@ -1730,7 +1747,10 @@ _public_ int sd_device_get_device_id(sd_device *device, const char **ret) {
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);
@@ -2797,7 +2817,10 @@ _public_ int sd_device_open(sd_device *device, int flags) {
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. */

View File

@@ -6,79 +6,203 @@
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);
assert_se(sd_device_new_from_syspath(&dev, "/sys/class/net") >= 0);
assert_se(!device_in_subsystem(dev, "net"));
assert_se(!device_in_subsystem(dev, "disk"));
assert_se(device_in_subsystem(dev, "subsystem"));
assert_se(!device_in_subsystem(dev, ""));
assert_se(!device_in_subsystem(dev, NULL));
dev = sd_device_unref(dev);
assert_se(sd_device_new_from_syspath(&dev, "/sys/class") >= 0);
assert_se(!device_in_subsystem(dev, "net"));
assert_se(!device_in_subsystem(dev, "disk"));
assert_se(!device_in_subsystem(dev, "subsystem"));
assert_se(!device_in_subsystem(dev, ""));
assert_se(device_in_subsystem(dev, NULL));
}
TEST(device_is_devtype) {
TEST(device_in_subsystem_devtype_sysname_startswith) {
_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);
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));
FOREACH_DEVICE(e, d) {
const char *t;
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_se(sd_device_get_devtype(d, &t) >= 0);
assert_se(device_is_devtype(d, t));
assert_se(!device_is_devtype(d, "hoge"));
assert_se(!device_is_devtype(d, ""));
assert_se(!device_is_devtype(d, NULL));
ASSERT_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));
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"));
}
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));
_cleanup_(sd_device_unrefp) sd_device *dev = NULL;
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);
}
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"));
dev = sd_device_unref(dev);
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) {

View File

@@ -539,6 +539,7 @@ TEST(sd_device_enumerator_add_match_parent) {
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));
@@ -552,8 +553,7 @@ TEST(sd_device_enumerator_add_all_parents) {
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++;
}
@@ -564,7 +564,8 @@ TEST(sd_device_enumerator_add_all_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++;
}

View File

@@ -608,7 +608,6 @@ static int manager_count_external_displays(Manager *m) {
return r;
FOREACH_DEVICE(e, d) {
const char *status, *enabled, *dash, *nn;
sd_device *p;
if (sd_device_get_parent(d, &p) < 0)
@@ -617,17 +616,22 @@ 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 (!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;
@@ -639,12 +643,21 @@ static int manager_count_external_displays(Manager *m) {
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++;
}

View File

@@ -273,21 +273,22 @@ static void session_device_stop(SessionDevice *sd) {
}
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;

View File

@@ -628,16 +628,22 @@ static int manager_dispatch_device_udev(sd_device_monitor *monitor, sd_device *d
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;

View File

@@ -1302,6 +1302,7 @@ static int acquire_description(sd_device *d) {
static int acquire_removable(sd_device *d) {
const char *v;
int r;
assert(d);
@@ -1313,10 +1314,16 @@ static int acquire_removable(sd_device *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;
}

View File

@@ -327,7 +327,7 @@ static void acquire_wlan_link_info(LinkInfo *link) {
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);

View File

@@ -170,11 +170,11 @@ 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");
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",

View File

@@ -126,7 +126,10 @@ static int link_get_wiphy(Link *link, Wiphy **ret) {
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");

View File

@@ -37,15 +37,12 @@ int blockdev_list(BlockDevListFlags flags) {
}
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;
}

View File

@@ -57,9 +57,14 @@ static int fd_get_devnum(int fd, BlockDeviceLookupFlag flags, dev_t *ret) {
}
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");
@@ -414,7 +419,10 @@ int blockdev_partscan_enabled(sd_device *dev) {
/* 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

View File

@@ -151,7 +151,10 @@ static int run(int argc, char *argv[]) {
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;

View File

@@ -364,7 +364,6 @@ static int read_loopback_backing_inode(
_cleanup_free_ char *fn = NULL;
struct loop_info64 info;
const char *name;
int r;
assert(dev);
@@ -381,11 +380,10 @@ static int read_loopback_backing_inode(
* 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) {

View File

@@ -318,20 +318,22 @@ static int verb_copy(UdevEvent *event, sd_device *dev) {
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);

View File

@@ -88,12 +88,20 @@ static int udev_builtin_hwdb_search(
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));

View File

@@ -384,8 +384,8 @@ static int builtin_input_id(UdevEvent *event, int argc, char *argv[]) {
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 */
@@ -425,8 +425,10 @@ static int builtin_input_id(UdevEvent *event, int argc, char *argv[]) {
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;

View File

@@ -43,20 +43,29 @@
#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;
r = device_in_subsystem(dev, "virtio");
if (r < 0)
return r;
if (r == 0) {
*ret = dev;
return 0;
}
}
return dev;
}
static int get_matching_parent(
@@ -70,26 +79,23 @@ 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) {
@@ -1284,9 +1290,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: {
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";

View File

@@ -89,7 +89,7 @@ static sd_device* skip_subsystem(sd_device *dev, const char *subsys) {
*/
for (parent = dev; ; ) {
if (!device_in_subsystem(parent, subsys))
if (device_in_subsystem(parent, subsys) <= 0)
break;
dev = parent;
@@ -404,7 +404,7 @@ 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 *id, *name;
if (!device_is_devtype(parent, "scsi_device"))
if (device_is_devtype(parent, "scsi_device") <= 0)
return parent;
/* firewire */
@@ -519,7 +519,7 @@ static sd_device* handle_usb(sd_device *parent, char **path) {
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)
@@ -699,95 +699,95 @@ static int builtin_path_id(UdevEvent *event, int argc, char *argv[]) {
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) {
@@ -795,7 +795,7 @@ static int builtin_path_id(UdevEvent *event, int argc, char *argv[]) {
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;
@@ -807,7 +807,7 @@ static int builtin_path_id(UdevEvent *event, int argc, char *argv[]) {
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) {
@@ -838,7 +838,7 @@ static int builtin_path_id(UdevEvent *event, int argc, char *argv[]) {
* 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);

View File

@@ -506,7 +506,11 @@ static int device_get_devpath_by_devnum(sd_device *dev, char **ret) {
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) {

View File

@@ -199,12 +199,16 @@ static int synthesize_change(Manager *manager, sd_device *dev) {
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;

View File

@@ -47,17 +47,16 @@ int udev_get_whole_disk(sd_device *dev, sd_device **ret_device, const char **ret
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);
@@ -149,9 +148,7 @@ nolock:
}
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);
@@ -161,23 +158,31 @@ static int worker_mark_block_device_read_only(sd_device *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);