From d1429d8f788c9028cff65d8c552872fd59ecc34a Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Tue, 17 Aug 2021 22:46:32 +0900 Subject: [PATCH 1/4] udevadm: introduce find_device_with_action() helper function --- src/udev/udevadm-test.c | 21 ++++++++------------- src/udev/udevadm-util.c | 17 +++++++++++++++++ src/udev/udevadm-util.h | 1 + 3 files changed, 26 insertions(+), 13 deletions(-) diff --git a/src/udev/udevadm-test.c b/src/udev/udevadm-test.c index f6ec222884..5d1e0d11b8 100644 --- a/src/udev/udevadm-test.c +++ b/src/udev/udevadm-test.c @@ -21,11 +21,12 @@ #include "strxcpyx.h" #include "udev-builtin.h" #include "udev-event.h" +#include "udevadm-util.h" #include "udevadm.h" -static const char *arg_action = "add"; +static sd_device_action_t arg_action = SD_DEVICE_ADD; static ResolveNameTiming arg_resolve_name_timing = RESOLVE_NAME_EARLY; -static char arg_syspath[UDEV_PATH_SIZE] = {}; +static const char *arg_syspath = NULL; static int help(void) { @@ -65,7 +66,7 @@ static int parse_argv(int argc, char *argv[]) { if (a < 0) return log_error_errno(a, "Invalid action '%s'", optarg); - arg_action = device_action_to_string(a); + arg_action = a; break; } case 'N': @@ -84,15 +85,9 @@ static int parse_argv(int argc, char *argv[]) { assert_not_reached(); } - if (!argv[optind]) - return log_error_errno(SYNTHETIC_ERRNO(EINVAL), - "syspath parameter missing."); - - /* add /sys if needed */ - if (!path_startswith(argv[optind], "/sys")) - strscpyl(arg_syspath, sizeof(arg_syspath), "/sys", argv[optind], NULL); - else - strscpy(arg_syspath, sizeof(arg_syspath), argv[optind]); + arg_syspath = argv[optind]; + if (!arg_syspath) + return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "syspath parameter missing."); return 1; } @@ -127,7 +122,7 @@ int test_main(int argc, char *argv[], void *userdata) { goto out; } - r = device_new_from_synthetic_event(&dev, arg_syspath, arg_action); + r = find_device_with_action(arg_syspath, arg_action, &dev); if (r < 0) { log_error_errno(r, "Failed to open device '%s': %m", arg_syspath); goto out; diff --git a/src/udev/udevadm-util.c b/src/udev/udevadm-util.c index 10191d88ba..6be0e73560 100644 --- a/src/udev/udevadm-util.c +++ b/src/udev/udevadm-util.c @@ -93,3 +93,20 @@ int find_device(const char *id, const char *prefix, sd_device **ret) { return find_device_from_path(id, ret); } + +int find_device_with_action(const char *id, sd_device_action_t action, sd_device **ret) { + _cleanup_free_ char *path = NULL; + + 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; + } + + return device_new_from_synthetic_event(ret, id, device_action_to_string(action)); +} diff --git a/src/udev/udevadm-util.h b/src/udev/udevadm-util.h index 91587c5492..91663fdf3e 100644 --- a/src/udev/udevadm-util.h +++ b/src/udev/udevadm-util.h @@ -4,3 +4,4 @@ #include "sd-device.h" int find_device(const char *id, const char *prefix, sd_device **ret); +int find_device_with_action(const char *id, sd_device_action_t action, sd_device **ret); From 6de7fa875977c156c0e2c09df23544ec008c6ed9 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Tue, 17 Aug 2021 22:57:04 +0900 Subject: [PATCH 2/4] udevadm: introduce parse_device_action() helper function --- src/udev/udevadm-test.c | 20 ++++++-------------- src/udev/udevadm-trigger.c | 14 +++++--------- src/udev/udevadm-util.c | 19 +++++++++++++++++++ src/udev/udevadm-util.h | 1 + 4 files changed, 31 insertions(+), 23 deletions(-) diff --git a/src/udev/udevadm-test.c b/src/udev/udevadm-test.c index 5d1e0d11b8..01057e1256 100644 --- a/src/udev/udevadm-test.c +++ b/src/udev/udevadm-test.c @@ -50,25 +50,17 @@ static int parse_argv(int argc, char *argv[]) { {} }; - int c; + int r, c; while ((c = getopt_long(argc, argv, "a:N:Vh", options, NULL)) >= 0) switch (c) { - case 'a': { - sd_device_action_t a; - - if (streq(optarg, "help")) { - dump_device_action_table(); + case 'a': + r = parse_device_action(optarg, &arg_action); + if (r < 0) + return log_error_errno(r, "Invalid action '%s'", optarg); + if (r == 0) return 0; - } - - a = device_action_from_string(optarg); - if (a < 0) - return log_error_errno(a, "Invalid action '%s'", optarg); - - arg_action = a; break; - } case 'N': arg_resolve_name_timing = resolve_name_timing_from_string(optarg); if (arg_resolve_name_timing < 0) diff --git a/src/udev/udevadm-trigger.c b/src/udev/udevadm-trigger.c index a24073fb73..7c121c369b 100644 --- a/src/udev/udevadm-trigger.c +++ b/src/udev/udevadm-trigger.c @@ -312,17 +312,13 @@ int trigger_main(int argc, char *argv[], void *userdata) { else return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Unknown type --type=%s", optarg); break; - case 'c': { - if (streq(optarg, "help")) { - dump_device_action_table(); + case 'c': + r = parse_device_action(optarg, &action); + if (r < 0) + return log_error_errno(r, "Unknown action '%s'", optarg); + if (r == 0) return 0; - } - - action = device_action_from_string(optarg); - if (action < 0) - return log_error_errno(action, "Unknown action '%s'", optarg); break; - } case 's': r = sd_device_enumerator_add_match_subsystem(e, optarg, true); if (r < 0) diff --git a/src/udev/udevadm-util.c b/src/udev/udevadm-util.c index 6be0e73560..9649e5a207 100644 --- a/src/udev/udevadm-util.c +++ b/src/udev/udevadm-util.c @@ -110,3 +110,22 @@ int find_device_with_action(const char *id, sd_device_action_t action, sd_device return device_new_from_synthetic_event(ret, id, device_action_to_string(action)); } + +int parse_device_action(const char *str, sd_device_action_t *action) { + sd_device_action_t a; + + assert(str); + assert(action); + + if (streq(str, "help")) { + dump_device_action_table(); + return 0; + } + + a = device_action_from_string(str); + if (a < 0) + return a; + + *action = a; + return 1; +} diff --git a/src/udev/udevadm-util.h b/src/udev/udevadm-util.h index 91663fdf3e..7fb4556413 100644 --- a/src/udev/udevadm-util.h +++ b/src/udev/udevadm-util.h @@ -5,3 +5,4 @@ int find_device(const char *id, const char *prefix, sd_device **ret); int find_device_with_action(const char *id, sd_device_action_t action, sd_device **ret); +int parse_device_action(const char *str, sd_device_action_t *action); From 74614801f6bd33dfb2c051fa6b6d7c899c695cf6 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Tue, 17 Aug 2021 23:13:07 +0900 Subject: [PATCH 3/4] shell-completion: add missing uevent actions for udevadm --- shell-completion/zsh/_udevadm | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/shell-completion/zsh/_udevadm b/shell-completion/zsh/_udevadm index a19829c1de..87fdb5e42c 100644 --- a/shell-completion/zsh/_udevadm +++ b/shell-completion/zsh/_udevadm @@ -23,7 +23,7 @@ _udevadm_trigger(){ '--dry-run[Do not actually trigger the event.]' \ '--quiet[Suppress error logging in triggering events.]' \ '--type=[Trigger a specific type of devices.]:types:(devices subsystems failed)' \ - '--action=[Type of event to be triggered.]:actions:(add change remove)' \ + '--action=[Type of event to be triggered.]:actions:(add change remove move online offline bind unbind)' \ '--subsystem-match=[Trigger events for devices which belong to a matching subsystem.]' \ '--subsystem-nomatch=[Do not trigger events for devices which belong to a matching subsystem.]' \ '--attr-match=attribute=[Trigger events for devices with a matching sysfs attribute.]' \ @@ -74,7 +74,7 @@ _udevadm_monitor(){ (( $+functions[_udevadm_test] )) || _udevadm_test(){ _arguments \ - '--action=[The action string.]:actions:(add change remove)' \ + '--action=[The action string.]:actions:(add change remove move online offline bind unbind)' \ '--subsystem=[The subsystem string.]' \ '--help[Print help text.]' \ '*::devpath:_files -P /sys/ -W /sys' From 7ce05a8d6631b2e99fb101bd639838c5ce8f60a7 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Tue, 17 Aug 2021 23:14:29 +0900 Subject: [PATCH 4/4] udevadm: introduce -a|--action option for test-builtin command As net_setup_link builtin requires that a device action is set for the sd_device object. --- man/udevadm.xml | 12 ++++++++++++ shell-completion/bash/udevadm | 13 ++++++++++++- shell-completion/zsh/_udevadm | 3 +++ src/udev/udevadm-test-builtin.c | 24 +++++++++++++++++------- 4 files changed, 44 insertions(+), 8 deletions(-) diff --git a/man/udevadm.xml b/man/udevadm.xml index 90adc64543..2704156840 100644 --- a/man/udevadm.xml +++ b/man/udevadm.xml @@ -619,6 +619,18 @@ for device DEVPATH, and print debug output. + + + + + Type of event to be simulated. Possible actions are add, + remove, change, move, + online, offline, bind, + and unbind. Also, the special value help can be used + to list the possible actions. The default value is add. + + + diff --git a/shell-completion/bash/udevadm b/shell-completion/bash/udevadm index 81036f3588..4210366808 100644 --- a/shell-completion/bash/udevadm +++ b/shell-completion/bash/udevadm @@ -61,6 +61,7 @@ _udevadm() { [MONITOR_STANDALONE]='-k --kernel -u --udev -p --property' [MONITOR_ARG]='-s --subsystem-match -t --tag-match' [TEST]='-a --action -N --resolve-names' + [TEST_BUILTIN]='-a --action' ) local verbs=(info trigger settle control monitor test-builtin test) @@ -215,6 +216,16 @@ _udevadm() { ;; 'test-builtin') + if __contains_word "$prev" ${OPTS[TEST_BUILTIN]}; then + case $prev in + -a|--action) + comps=$( udevadm test-builtin --action help ) + ;; + esac + COMPREPLY=( $(compgen -W '$comps' -- "$cur") ) + return 0 + fi + for ((i=0; i < COMP_CWORD; i++)); do if __contains_word "${COMP_WORDS[i]}" "${builtins[@]}"; then builtin=${COMP_WORDS[i]} @@ -225,7 +236,7 @@ _udevadm() { if [[ -z $builtin ]]; then comps="${builtins[@]}" elif [[ $cur = -* ]]; then - comps="${OPTS[COMMON]}" + comps="${OPTS[COMMON]} ${OPTS[TEST_BUILTIN]}" else comps=$( __get_all_sysdevs ) local IFS=$'\n' diff --git a/shell-completion/zsh/_udevadm b/shell-completion/zsh/_udevadm index 87fdb5e42c..1179f81a8b 100644 --- a/shell-completion/zsh/_udevadm +++ b/shell-completion/zsh/_udevadm @@ -84,14 +84,17 @@ _udevadm_test(){ _udevadm_test-builtin(){ if (( CURRENT == 2 )); then _arguments \ + '--action=[The action string.]:actions:(add change remove move online offline bind unbind)' \ '--help[Print help text]' \ '*::builtins:(blkid btrfs hwdb input_id net_id net_setup_link kmod path_id usb_id uaccess)' elif (( CURRENT == 3 )); then _arguments \ + '--action=[The action string.]:actions:(add change remove move online offline bind unbind)' \ '--help[Print help text]' \ '*::syspath:_files -P /sys -W /sys' else _arguments \ + '--action=[The action string.]:actions:(add change remove move online offline bind unbind)' \ '--help[Print help text]' fi } diff --git a/src/udev/udevadm-test-builtin.c b/src/udev/udevadm-test-builtin.c index 992fc70807..cae2950c8f 100644 --- a/src/udev/udevadm-test-builtin.c +++ b/src/udev/udevadm-test-builtin.c @@ -11,14 +11,16 @@ #include "udevadm.h" #include "udevadm-util.h" +static sd_device_action_t arg_action = SD_DEVICE_ADD; static const char *arg_command = NULL; static const char *arg_syspath = NULL; static int help(void) { printf("%s test-builtin [OPTIONS] COMMAND DEVPATH\n\n" "Test a built-in command.\n\n" - " -h --help Print this message\n" - " -V --version Print version of the program\n\n" + " -h --help Print this message\n" + " -V --version Print version of the program\n\n" + " -a --action=ACTION|help Set action string\n" "Commands:\n", program_invocation_short_name); @@ -29,15 +31,23 @@ static int help(void) { static int parse_argv(int argc, char *argv[]) { static const struct option options[] = { - { "version", no_argument, NULL, 'V' }, - { "help", no_argument, NULL, 'h' }, + { "action", required_argument, NULL, 'a' }, + { "version", no_argument, NULL, 'V' }, + { "help", no_argument, NULL, 'h' }, {} }; - int c; + int r, c; - while ((c = getopt_long(argc, argv, "Vh", options, NULL)) >= 0) + while ((c = getopt_long(argc, argv, "a:Vh", options, NULL)) >= 0) switch (c) { + case 'a': + r = parse_device_action(optarg, &arg_action); + if (r < 0) + return log_error_errno(r, "Invalid action '%s'", optarg); + if (r == 0) + return 0; + break; case 'V': return print_version(); case 'h': @@ -81,7 +91,7 @@ int builtin_main(int argc, char *argv[], void *userdata) { goto finish; } - r = find_device(arg_syspath, "/sys", &dev); + r = find_device_with_action(arg_syspath, arg_action, &dev); if (r < 0) { log_error_errno(r, "Failed to open device '%s': %m", arg_syspath); goto finish;