1
1
mirror of https://github.com/systemd/systemd-stable.git synced 2024-10-27 01:55:32 +03:00

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.
This commit is contained in:
Yu Watanabe 2021-08-17 23:14:29 +09:00
parent 74614801f6
commit 7ce05a8d66
4 changed files with 44 additions and 8 deletions

View File

@ -619,6 +619,18 @@
for device <replaceable>DEVPATH</replaceable>, and print debug for device <replaceable>DEVPATH</replaceable>, and print debug
output.</para> output.</para>
<variablelist> <variablelist>
<varlistentry>
<term><option>-a</option></term>
<term><option>--action=<replaceable>ACTION</replaceable></option></term>
<listitem>
<para>Type of event to be simulated. Possible actions are <literal>add</literal>,
<literal>remove</literal>, <literal>change</literal>, <literal>move</literal>,
<literal>online</literal>, <literal>offline</literal>, <literal>bind</literal>,
and <literal>unbind</literal>. Also, the special value <literal>help</literal> can be used
to list the possible actions. The default value is <literal>add</literal>.</para>
</listitem>
</varlistentry>
<xi:include href="standard-options.xml" xpointer="help" /> <xi:include href="standard-options.xml" xpointer="help" />
</variablelist> </variablelist>
</refsect2> </refsect2>

View File

@ -61,6 +61,7 @@ _udevadm() {
[MONITOR_STANDALONE]='-k --kernel -u --udev -p --property' [MONITOR_STANDALONE]='-k --kernel -u --udev -p --property'
[MONITOR_ARG]='-s --subsystem-match -t --tag-match' [MONITOR_ARG]='-s --subsystem-match -t --tag-match'
[TEST]='-a --action -N --resolve-names' [TEST]='-a --action -N --resolve-names'
[TEST_BUILTIN]='-a --action'
) )
local verbs=(info trigger settle control monitor test-builtin test) local verbs=(info trigger settle control monitor test-builtin test)
@ -215,6 +216,16 @@ _udevadm() {
;; ;;
'test-builtin') '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 for ((i=0; i < COMP_CWORD; i++)); do
if __contains_word "${COMP_WORDS[i]}" "${builtins[@]}"; then if __contains_word "${COMP_WORDS[i]}" "${builtins[@]}"; then
builtin=${COMP_WORDS[i]} builtin=${COMP_WORDS[i]}
@ -225,7 +236,7 @@ _udevadm() {
if [[ -z $builtin ]]; then if [[ -z $builtin ]]; then
comps="${builtins[@]}" comps="${builtins[@]}"
elif [[ $cur = -* ]]; then elif [[ $cur = -* ]]; then
comps="${OPTS[COMMON]}" comps="${OPTS[COMMON]} ${OPTS[TEST_BUILTIN]}"
else else
comps=$( __get_all_sysdevs ) comps=$( __get_all_sysdevs )
local IFS=$'\n' local IFS=$'\n'

View File

@ -84,14 +84,17 @@ _udevadm_test(){
_udevadm_test-builtin(){ _udevadm_test-builtin(){
if (( CURRENT == 2 )); then if (( CURRENT == 2 )); then
_arguments \ _arguments \
'--action=[The action string.]:actions:(add change remove move online offline bind unbind)' \
'--help[Print help text]' \ '--help[Print help text]' \
'*::builtins:(blkid btrfs hwdb input_id net_id net_setup_link kmod path_id usb_id uaccess)' '*::builtins:(blkid btrfs hwdb input_id net_id net_setup_link kmod path_id usb_id uaccess)'
elif (( CURRENT == 3 )); then elif (( CURRENT == 3 )); then
_arguments \ _arguments \
'--action=[The action string.]:actions:(add change remove move online offline bind unbind)' \
'--help[Print help text]' \ '--help[Print help text]' \
'*::syspath:_files -P /sys -W /sys' '*::syspath:_files -P /sys -W /sys'
else else
_arguments \ _arguments \
'--action=[The action string.]:actions:(add change remove move online offline bind unbind)' \
'--help[Print help text]' '--help[Print help text]'
fi fi
} }

View File

@ -11,14 +11,16 @@
#include "udevadm.h" #include "udevadm.h"
#include "udevadm-util.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_command = NULL;
static const char *arg_syspath = NULL; static const char *arg_syspath = NULL;
static int help(void) { static int help(void) {
printf("%s test-builtin [OPTIONS] COMMAND DEVPATH\n\n" printf("%s test-builtin [OPTIONS] COMMAND DEVPATH\n\n"
"Test a built-in command.\n\n" "Test a built-in command.\n\n"
" -h --help Print this message\n" " -h --help Print this message\n"
" -V --version Print version of the program\n\n" " -V --version Print version of the program\n\n"
" -a --action=ACTION|help Set action string\n"
"Commands:\n", "Commands:\n",
program_invocation_short_name); program_invocation_short_name);
@ -29,15 +31,23 @@ static int help(void) {
static int parse_argv(int argc, char *argv[]) { static int parse_argv(int argc, char *argv[]) {
static const struct option options[] = { static const struct option options[] = {
{ "version", no_argument, NULL, 'V' }, { "action", required_argument, NULL, 'a' },
{ "help", no_argument, NULL, 'h' }, { "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) { 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': case 'V':
return print_version(); return print_version();
case 'h': case 'h':
@ -81,7 +91,7 @@ int builtin_main(int argc, char *argv[], void *userdata) {
goto finish; goto finish;
} }
r = find_device(arg_syspath, "/sys", &dev); r = find_device_with_action(arg_syspath, arg_action, &dev);
if (r < 0) { if (r < 0) {
log_error_errno(r, "Failed to open device '%s': %m", arg_syspath); log_error_errno(r, "Failed to open device '%s': %m", arg_syspath);
goto finish; goto finish;