diff options
author | Yu Watanabe <watanabe.yu+github@gmail.com> | 2022-02-02 12:40:23 +0900 |
---|---|---|
committer | Yu Watanabe <watanabe.yu+github@gmail.com> | 2022-02-02 20:34:38 +0900 |
commit | f4f3249539b849936309847812ad5bf92d797b86 (patch) | |
tree | e986e5f00e2c564a0042fae2b041ef853ca6757c /src/udev/udevadm-util.c | |
parent | 20798980883d47bf82e8b89b4f061c27262ce68c (diff) | |
download | systemd-f4f3249539b849936309847812ad5bf92d797b86.tar.gz |
udevadm: make test and test-builtin command accept /dev path or device unit
Diffstat (limited to 'src/udev/udevadm-util.c')
-rw-r--r-- | src/udev/udevadm-util.c | 53 |
1 files changed, 29 insertions, 24 deletions
diff --git a/src/udev/udevadm-util.c b/src/udev/udevadm-util.c index 9649e5a207..52a726709a 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) { |