From: Yu Watanabe Date: Wed, 2 Feb 2022 03:40:23 +0000 (+0900) Subject: udevadm: make test and test-builtin command accept /dev path or device unit X-Git-Tag: v251-rc1~371^2~2 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=f4f3249539b849936309847812ad5bf92d797b86;p=thirdparty%2Fsystemd.git udevadm: make test and test-builtin command accept /dev path or device unit --- diff --git a/man/udevadm.xml b/man/udevadm.xml index 6091a3421ff..1f872111e56 100644 --- a/man/udevadm.xml +++ b/man/udevadm.xml @@ -595,7 +595,7 @@ udevadm test <arg choice="opt"><replaceable>options</replaceable></arg> - <arg><replaceable>devpath</replaceable></arg> + <arg choice="opt"><replaceable>devpath</replaceable>|<replaceable>file</replaceable>|<replaceable>unit</replaceable></arg> Simulate a udev event run for the given device, and print debug output. @@ -631,7 +631,7 @@ udevadm test-builtin <arg choice="opt"><replaceable>options</replaceable></arg> <arg><replaceable>command</replaceable></arg> - <arg><replaceable>devpath</replaceable></arg> + <arg choice="opt"><replaceable>devpath</replaceable>|<replaceable>file</replaceable>|<replaceable>unit</replaceable></arg> Run a built-in command COMMAND for device DEVPATH, and print debug diff --git a/src/udev/udevadm-test-builtin.c b/src/udev/udevadm-test-builtin.c index c266e16849b..81b633611e9 100644 --- a/src/udev/udevadm-test-builtin.c +++ b/src/udev/udevadm-test-builtin.c @@ -66,7 +66,7 @@ static int parse_argv(int argc, char *argv[]) { arg_syspath = argv[optind++]; if (!arg_syspath) return log_error_errno(SYNTHETIC_ERRNO(EINVAL), - "syspath missing."); + "device is missing."); return 1; } diff --git a/src/udev/udevadm-util.c b/src/udev/udevadm-util.c index 9649e5a2075..52a726709af 100644 --- a/src/udev/udevadm-util.c +++ b/src/udev/udevadm-util.c @@ -71,44 +71,49 @@ static int find_device_from_unit(const char *unit_name, sd_device **ret) { } int find_device(const char *id, const char *prefix, sd_device **ret) { - _cleanup_free_ char *path = NULL; - int r; - assert(id); assert(ret); - if (prefix) { - if (!path_startswith(id, prefix)) { - id = path = path_join(prefix, id); - if (!path) - return -ENOMEM; - } - } else { - /* In cases where the argument is generic (no prefix specified), - * check if the argument looks like a device unit name. */ - r = find_device_from_unit(id, ret); - if (r >= 0) - return r; + if (find_device_from_path(id, ret) >= 0) + return 0; + + if (prefix && !path_startswith(id, prefix)) { + _cleanup_free_ char *path = NULL; + + path = path_join(prefix, id); + if (!path) + return -ENOMEM; + + if (find_device_from_path(path, ret) >= 0) + return 0; } - return find_device_from_path(id, ret); + /* Check if the argument looks like a device unit name. */ + return find_device_from_unit(id, ret); } int find_device_with_action(const char *id, sd_device_action_t action, sd_device **ret) { - _cleanup_free_ char *path = NULL; + _cleanup_(sd_device_unrefp) sd_device *dev = NULL; + int r; assert(id); assert(ret); assert(action >= 0 && action < _SD_DEVICE_ACTION_MAX); - if (!path_startswith(id, "/sys")) { - path = path_join("/sys", id); - if (!path) - return -ENOMEM; - id = path; - } + r = find_device(id, "/sys", &dev); + if (r < 0) + return r; + + r = device_read_uevent_file(dev); + if (r < 0) + return r; + + r = device_set_action(dev, action); + if (r < 0) + return r; - return device_new_from_synthetic_event(ret, id, device_action_to_string(action)); + *ret = TAKE_PTR(dev); + return 0; } int parse_device_action(const char *str, sd_device_action_t *action) {