udevadm: introduce find_device_with_action() helper function

This commit is contained in:
Yu Watanabe
2021-08-17 22:46:32 +09:00
parent 29278aa41d
commit d1429d8f78
3 changed files with 26 additions and 13 deletions

View File

@@ -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;

View File

@@ -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));
}

View File

@@ -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);