| 1 |
/* SPDX-License-Identifier: GPL-2.0-or-later */ |
| 2 |
|
| 3 |
#include "device-private.h" |
| 4 |
#include "device-util.h" |
| 5 |
#include "format-table.h" |
| 6 |
#include "help-util.h" |
| 7 |
#include "log.h" |
| 8 |
#include "options.h" |
| 9 |
#include "udev-builtin.h" |
| 10 |
#include "udevadm.h" |
| 11 |
#include "udevadm-util.h" |
| 12 |
|
| 13 |
static sd_device_action_t arg_action = SD_DEVICE_ADD; |
| 14 |
static const char *arg_command = NULL; |
| 15 |
static const char *arg_syspath = NULL; |
| 16 |
|
| 17 |
static int help(void) { |
| 18 |
_cleanup_(table_unrefp) Table *options = NULL; |
| 19 |
int r; |
| 20 |
|
| 21 |
r = option_parser_get_help_table_ns("udevadm-test-builtin", &options); |
| 22 |
if (r < 0) |
| 23 |
return r; |
| 24 |
|
| 25 |
help_cmdline("test-builtin [OPTIONS] COMMAND DEVPATH"); |
| 26 |
help_abstract("Test a built-in command."); |
| 27 |
help_section("Options"); |
| 28 |
r = table_print_or_warn(options); |
| 29 |
if (r < 0) |
| 30 |
return r; |
| 31 |
|
| 32 |
help_section("Commands"); |
| 33 |
udev_builtin_list(); |
| 34 |
return 0; |
| 35 |
} |
| 36 |
|
| 37 |
static int parse_argv(int argc, char *argv[]) { |
| 38 |
int r; |
| 39 |
|
| 40 |
assert(argc >= 0); |
| 41 |
assert(argv); |
| 42 |
|
| 43 |
OptionParser opts = { argc, argv, .namespace = "udevadm-test-builtin" }; |
| 44 |
|
| 45 |
FOREACH_OPTION_OR_RETURN(c, &opts) |
| 46 |
switch (c) { |
| 47 |
|
| 48 |
OPTION_NAMESPACE("udevadm-test-builtin"): {} |
| 49 |
|
| 50 |
OPTION_COMMON_HELP: |
| 51 |
return help(); |
| 52 |
|
| 53 |
OPTION('V', "version", NULL, "Show package version"): |
| 54 |
return print_version(); |
| 55 |
|
| 56 |
OPTION('a', "action", "ACTION|help", "Set action string"): |
| 57 |
r = parse_device_action(opts.arg, &arg_action); |
| 58 |
if (r <= 0) |
| 59 |
return r; |
| 60 |
break; |
| 61 |
} |
| 62 |
|
| 63 |
if (option_parser_get_n_args(&opts) != 2) |
| 64 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Expected two arguments: command string and device path."); |
| 65 |
|
| 66 |
char **args = option_parser_get_args(&opts); |
| 67 |
arg_command = ASSERT_PTR(args[0]); |
| 68 |
arg_syspath = ASSERT_PTR(args[1]); |
| 69 |
return 1; |
| 70 |
} |
| 71 |
|
| 72 |
int verb_builtin_main(int argc, char *argv[], uintptr_t _data, void *userdata) { |
| 73 |
_cleanup_(udev_event_unrefp) UdevEvent *event = NULL; |
| 74 |
_cleanup_(sd_device_unrefp) sd_device *dev = NULL; |
| 75 |
UdevBuiltinCommand cmd; |
| 76 |
int r; |
| 77 |
|
| 78 |
log_set_max_level(LOG_DEBUG); |
| 79 |
log_setup(); |
| 80 |
|
| 81 |
r = parse_argv(argc, argv); |
| 82 |
if (r <= 0) |
| 83 |
return r; |
| 84 |
|
| 85 |
udev_builtin_init(); |
| 86 |
UDEV_BUILTIN_DESTRUCTOR; |
| 87 |
|
| 88 |
cmd = udev_builtin_lookup(arg_command); |
| 89 |
if (cmd < 0) |
| 90 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Unknown command '%s'", arg_command); |
| 91 |
|
| 92 |
r = find_device_with_action(arg_syspath, arg_action, &dev); |
| 93 |
if (r < 0) |
| 94 |
return log_error_errno(r, "Failed to open device '%s': %m", arg_syspath); |
| 95 |
|
| 96 |
event = udev_event_new(dev, NULL, EVENT_UDEVADM_TEST_BUILTIN); |
| 97 |
if (!event) |
| 98 |
return log_oom(); |
| 99 |
|
| 100 |
if (arg_action != SD_DEVICE_REMOVE) { |
| 101 |
/* For net_setup_link */ |
| 102 |
r = device_clone_with_db(dev, &event->dev_db_clone); |
| 103 |
if (r < 0) |
| 104 |
return log_device_error_errno(dev, r, "Failed to clone device: %m"); |
| 105 |
} |
| 106 |
|
| 107 |
r = udev_builtin_run(event, cmd, arg_command); |
| 108 |
if (r < 0) |
| 109 |
return log_debug_errno(r, "Builtin command '%s' fails: %m", arg_command); |
| 110 |
|
| 111 |
return 0; |
| 112 |
} |