]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
udevadm: make test and test-builtin command accept /dev path or device unit
authorYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 2 Feb 2022 03:40:23 +0000 (12:40 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 2 Feb 2022 11:34:38 +0000 (20:34 +0900)
man/udevadm.xml
src/udev/udevadm-test-builtin.c
src/udev/udevadm-util.c

index 6091a3421ff2cf3ae8217a8b1e3cb82b57170079..1f872111e56944a554b5656ee28f28bb03aea10d 100644 (file)
 
     <refsect2><title>udevadm test
       <arg choice="opt"><replaceable>options</replaceable></arg>
-      <arg><replaceable>devpath</replaceable></arg>
+      <arg choice="opt"><replaceable>devpath</replaceable>|<replaceable>file</replaceable>|<replaceable>unit</replaceable></arg>
     </title>
       <para>Simulate a udev event run for the given device, and print debug output.</para>
       <variablelist>
     <refsect2><title>udevadm test-builtin
       <arg choice="opt"><replaceable>options</replaceable></arg>
       <arg><replaceable>command</replaceable></arg>
-      <arg><replaceable>devpath</replaceable></arg>
+      <arg choice="opt"><replaceable>devpath</replaceable>|<replaceable>file</replaceable>|<replaceable>unit</replaceable></arg>
     </title>
       <para>Run a built-in command <replaceable>COMMAND</replaceable>
       for device <replaceable>DEVPATH</replaceable>, and print debug
index c266e16849b1c713504f4f75831eb7f59332d517..81b633611e9819fb6cea81504463caa98631523a 100644 (file)
@@ -66,7 +66,7 @@ static int parse_argv(int argc, char *argv[]) {
         arg_syspath = argv[optind++];
         if (!arg_syspath)
                 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
-                                       "syspath missing.");
+                                       "device is missing.");
 
         return 1;
 }
index 9649e5a2075b9672913152f5c654043dc8550956..52a726709af1176fef14e5f27f559f8f26a49ead 100644 (file)
@@ -71,44 +71,49 @@ static int find_device_from_unit(const char *unit_name, sd_device **ret) {
 }
 
 int find_device(const char *id, const char *prefix, sd_device **ret) {
-        _cleanup_free_ char *path = NULL;
-        int r;
-
         assert(id);
         assert(ret);
 
-        if (prefix) {
-                if (!path_startswith(id, prefix)) {
-                        id = path = path_join(prefix, id);
-                        if (!path)
-                                return -ENOMEM;
-                }
-        } else {
-                /* In cases where the argument is generic (no prefix specified),
-                 * check if the argument looks like a device unit name. */
-                r = find_device_from_unit(id, ret);
-                if (r >= 0)
-                        return r;
+        if (find_device_from_path(id, ret) >= 0)
+                return 0;
+
+        if (prefix && !path_startswith(id, prefix)) {
+                _cleanup_free_ char *path = NULL;
+
+                path = path_join(prefix, id);
+                if (!path)
+                        return -ENOMEM;
+
+                if (find_device_from_path(path, ret) >= 0)
+                        return 0;
         }
 
-        return find_device_from_path(id, ret);
+        /* Check if the argument looks like a device unit name. */
+        return find_device_from_unit(id, ret);
 }
 
 int find_device_with_action(const char *id, sd_device_action_t action, sd_device **ret) {
-        _cleanup_free_ char *path = NULL;
+        _cleanup_(sd_device_unrefp) sd_device *dev = NULL;
+        int r;
 
         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;
-        }
+        r = find_device(id, "/sys", &dev);
+        if (r < 0)
+                return r;
+
+        r = device_read_uevent_file(dev);
+        if (r < 0)
+                return r;
+
+        r = device_set_action(dev, action);
+        if (r < 0)
+                return r;
 
-        return device_new_from_synthetic_event(ret, id, device_action_to_string(action));
+        *ret = TAKE_PTR(dev);
+        return 0;
 }
 
 int parse_device_action(const char *str, sd_device_action_t *action) {