1
1
mirror of https://github.com/systemd/systemd-stable.git synced 2024-12-24 21:34:08 +03:00

udevadm: make test and test-builtin command accept /dev path or device unit

This commit is contained in:
Yu Watanabe 2022-02-02 12:40:23 +09:00
parent 2079898088
commit f4f3249539
3 changed files with 32 additions and 27 deletions

View File

@ -595,7 +595,7 @@
<refsect2><title>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>
</title>
<para>Simulate a udev event run for the given device, and print debug output.</para>
<variablelist>
@ -631,7 +631,7 @@
<refsect2><title>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>
</title>
<para>Run a built-in command <replaceable>COMMAND</replaceable>
for device <replaceable>DEVPATH</replaceable>, and print debug

View File

@ -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;
}

View File

@ -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;
return device_new_from_synthetic_event(ret, id, device_action_to_string(action));
r = device_read_uevent_file(dev);
if (r < 0)
return r;
r = device_set_action(dev, action);
if (r < 0)
return r;
*ret = TAKE_PTR(dev);
return 0;
}
int parse_device_action(const char *str, sd_device_action_t *action) {