summaryrefslogtreecommitdiff
path: root/src/core/device.c
diff options
context:
space:
mode:
authorYu Watanabe <watanabe.yu+github@gmail.com>2022-04-28 22:22:12 +0900
committerYu Watanabe <watanabe.yu+github@gmail.com>2022-04-30 04:14:10 +0900
commit0e38cee8838a2aca9285557620718857e11969f5 (patch)
tree5402bc21a6a7c237d0d6ba9de4a4f6514802e5c4 /src/core/device.c
parentf374631a56c967073719168c1877993c9ad4912a (diff)
downloadsystemd-0e38cee8838a2aca9285557620718857e11969f5.tar.gz
core/device: use sd_device_new_from_devname() to verify the device node
Diffstat (limited to 'src/core/device.c')
-rw-r--r--src/core/device.c41
1 files changed, 11 insertions, 30 deletions
diff --git a/src/core/device.c b/src/core/device.c
index 6d662872cc..5d08697acc 100644
--- a/src/core/device.c
+++ b/src/core/device.c
@@ -946,45 +946,26 @@ static int device_dispatch_io(sd_device_monitor *monitor, sd_device *dev, void *
return 0;
}
-static int validate_node(Manager *m, const char *node, sd_device **ret) {
- struct stat st;
+static int validate_node(const char *node, sd_device **ret) {
+ _cleanup_(sd_device_unrefp) sd_device *dev = NULL;
int r;
- assert(m);
assert(node);
assert(ret);
/* Validates a device node that showed up in /proc/swaps or /proc/self/mountinfo if it makes sense for us to
* track. Note that this validator is fine within missing device nodes, but not with badly set up ones! */
- if (!path_startswith(node, "/dev")) {
+ r = sd_device_new_from_devname(&dev, node);
+ if (r == -ENODEV) {
*ret = NULL;
- return 0; /* bad! */
+ return 0; /* good! (though missing) */
}
+ if (r < 0)
+ return r; /* bad! */
- if (stat(node, &st) < 0) {
- if (errno != ENOENT)
- return log_error_errno(errno, "Failed to stat() device node file %s: %m", node);
-
- *ret = NULL;
- return 1; /* good! (though missing) */
-
- } else {
- _cleanup_(sd_device_unrefp) sd_device *dev = NULL;
-
- r = sd_device_new_from_stat_rdev(&dev, &st);
- if (r == -ENOENT) {
- *ret = NULL;
- return 1; /* good! (though missing) */
- } else if (r == -ENOTTY) {
- *ret = NULL;
- return 0; /* bad! (not a device node but some other kind of file system node) */
- } else if (r < 0)
- return log_error_errno(r, "Failed to get udev device from devnum %u:%u: %m", major(st.st_rdev), minor(st.st_rdev));
-
- *ret = TAKE_PTR(dev);
- return 1; /* good! */
- }
+ *ret = TAKE_PTR(dev);
+ return 1; /* good! */
}
void device_found_node(Manager *m, const char *node, DeviceFound found, DeviceFound mask) {
@@ -1012,10 +993,10 @@ void device_found_node(Manager *m, const char *node, DeviceFound found, DeviceFo
* under the name referenced in /proc/swaps or /proc/self/mountinfo. But first, let's validate if
* everything is alright with the device node. */
- if (validate_node(m, node, &dev) <= 0)
+ if (validate_node(node, &dev) < 0)
return; /* Don't create a device unit for this if the device node is borked. */
- (void) device_setup_unit(m, dev, node, false);
+ (void) device_setup_unit(m, dev, node, false); /* 'dev' may be NULL. */
}
/* Update the device unit's state, should it exist */