]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
udev-builtin-net_id: do not assume the current interface name is ethX
authorYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 28 Sep 2022 09:40:41 +0000 (18:40 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Sat, 29 Jul 2023 13:14:24 +0000 (22:14 +0900)
Otherwise, `udevadm test-builtin` does not work as expected.

src/udev/udev-builtin-net_id.c

index aeb7bceaa57271e8becd7005595b776c4ff603bf..dbeabf8f0d75e57282278efb77c8ed14c94ca845 100644 (file)
@@ -501,9 +501,9 @@ static int dev_pci_slot(sd_device *dev, const LinkInfo *info, NetNames *names) {
 }
 
 static int names_vio(sd_device *dev, const char *prefix, bool test) {
+        const char *syspath, *subsystem, *p, *s;
         sd_device *parent;
-        unsigned busid, slotid, ethid;
-        const char *syspath, *subsystem;
+        unsigned slotid;
         int r;
 
         assert(dev);
@@ -531,12 +531,27 @@ static int names_vio(sd_device *dev, const char *prefix, bool test) {
         if (r < 0)
                 return log_device_debug_errno(dev, r, "sd_device_get_syspath() failed: %m");
 
-        r = sscanf(syspath, "/sys/devices/vio/%4x%4x/net/eth%u", &busid, &slotid, &ethid);
-        log_device_debug(dev, "Parsing vio slot information from syspath \"%s\": %s",
-                         syspath, r == 3 ? "success" : "failure");
-        if (r != 3)
+        p = path_startswith(syspath, "/sys/devices/vio/");
+        if (!p)
                 return -EINVAL;
 
+        r = path_find_first_component(&p, /* accept_dot_dot = */ false, &s);
+        if (r < 0)
+                return r;
+        if (r != 8)
+                return log_device_debug_errno(dev, SYNTHETIC_ERRNO(EINVAL),
+                                              "VIO bus ID and slot ID have invalid length: %s", syspath);
+
+        s = strndupa(s, 8);
+        if (!in_charset(s, HEXDIGITS))
+                return log_device_debug_errno(dev, SYNTHETIC_ERRNO(EINVAL),
+                                              "VIO bus ID and slot ID contain invalid characters: %s", s);
+
+        /* Parse only slot ID (tha last 4 hexdigits). */
+        r = safe_atou_full(s + 4, 16, &slotid);
+        if (r < 0)
+                return log_device_debug_errno(dev, r, "Failed to parse VIO slot from syspath \"%s\": %m", syspath);
+
         char str[ALTIFNAMSIZ];
         if (snprintf_ok(str, sizeof str, "%sv%u", prefix, slotid))
                 udev_builtin_add_property(dev, test, "ID_NET_NAME_SLOT", str);