From: Yu Watanabe Date: Tue, 17 Aug 2021 13:57:04 +0000 (+0900) Subject: udevadm: introduce parse_device_action() helper function X-Git-Tag: v250-rc1~814^2~2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=6de7fa875977c156c0e2c09df23544ec008c6ed9;p=thirdparty%2Fsystemd.git udevadm: introduce parse_device_action() helper function --- diff --git a/src/udev/udevadm-test.c b/src/udev/udevadm-test.c index 5d1e0d11b8b..01057e12563 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 a24073fb734..7c121c369bc 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 6be0e735604..9649e5a2075 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 91663fdf3ea..7fb45564134 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);