]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
udevadm: introduce find_device_with_action() helper function
authorYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 17 Aug 2021 13:46:32 +0000 (22:46 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 17 Aug 2021 14:22:26 +0000 (23:22 +0900)
src/udev/udevadm-test.c
src/udev/udevadm-util.c
src/udev/udevadm-util.h

index f6ec22288403e28fb065ccc34dc78ce9232c7384..5d1e0d11b8b721ac5f4a415f2a02eda1796a7832 100644 (file)
 #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;
index 10191d88ba9dbc22967c3687c44f65e4e2c62deb..6be0e7356046483aaa4f93840e558d4c6f3886b8 100644 (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));
+}
index 91587c5492ad9fc1cb44292da30d69390f1051f5..91663fdf3ea97e48eace4816063a2fa4ca8d1040 100644 (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);