From: Yu Watanabe Date: Tue, 17 Aug 2021 13:46:32 +0000 (+0900) Subject: udevadm: introduce find_device_with_action() helper function X-Git-Tag: v250-rc1~814^2~3 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=d1429d8f788c9028cff65d8c552872fd59ecc34a;p=thirdparty%2Fsystemd.git udevadm: introduce find_device_with_action() helper function --- diff --git a/src/udev/udevadm-test.c b/src/udev/udevadm-test.c index f6ec2228840..5d1e0d11b8b 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 10191d88ba9..6be0e735604 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 91587c5492a..91663fdf3ea 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);