]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
sd-device: introduce sd_device_new_child()
authorYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 26 Jul 2022 19:16:55 +0000 (04:16 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Fri, 5 Aug 2022 12:49:23 +0000 (21:49 +0900)
src/libsystemd/libsystemd.sym
src/libsystemd/sd-device/sd-device.c
src/network/networkd-wiphy.c
src/systemd/sd-device.h
src/udev/udev-builtin-net_id.c

index fb6e6f6431b4753733ab204e72ab8b7f1e86561f..5c5446e9876fa844d4ff05002fc487b1d5e95d30 100644 (file)
@@ -784,6 +784,8 @@ global:
         sd_bus_message_read_strv_extend;
         sd_bus_error_setfv;
 
+        sd_device_new_child;
+
         sd_id128_string_equal;
 
         sd_hwdb_new_from_path;
index 3e31beb1f2b4d20c810e6ba9ffe583b286200750..1050234afe980c21d8702696eac218615c498804 100644 (file)
@@ -861,6 +861,29 @@ _public_ int sd_device_get_syspath(sd_device *device, const char **ret) {
         return 0;
 }
 
+_public_ int sd_device_new_child(sd_device **ret, sd_device *device, const char *suffix) {
+        _cleanup_free_ char *path = NULL;
+        const char *s;
+        int r;
+
+        assert_return(ret, -EINVAL);
+        assert_return(device, -EINVAL);
+        assert_return(suffix, -EINVAL);
+
+        if (!path_is_normalized(suffix))
+                return -EINVAL;
+
+        r = sd_device_get_syspath(device, &s);
+        if (r < 0)
+                return r;
+
+        path = path_join(s, suffix);
+        if (!path)
+                return -ENOMEM;
+
+        return sd_device_new_from_syspath(ret, path);
+}
+
 static int device_new_from_child(sd_device **ret, sd_device *child) {
         _cleanup_free_ char *path = NULL;
         const char *syspath;
index 38d1ceeba1eae016cd7ceb2f5cf0758d5c679471..bc93252cb06cfec4b77c918933efde4966f31f7d 100644 (file)
@@ -125,12 +125,7 @@ static int link_get_wiphy(Link *link, Wiphy **ret) {
         if (!streq_ptr(s, "wlan"))
                 return -EOPNOTSUPP;
 
-        r = sd_device_get_syspath(link->dev, &s);
-        if (r < 0)
-                return r;
-
-        s = strjoina(s, "/phy80211");
-        r = sd_device_new_from_syspath(&phy, s);
+        r = sd_device_new_child(&phy, link->dev, "phy80211");
         if (r < 0)
                 return r;
 
index 19dd4d9eefae8c692446a1e4c876c086cc03a681..0b2f8f8b4292d8ccbfc8ae4e0dced6549871e563 100644 (file)
@@ -67,6 +67,8 @@ int sd_device_new_from_path(sd_device **ret, const char *path);
 int sd_device_new_from_ifname(sd_device **ret, const char *ifname);
 int sd_device_new_from_ifindex(sd_device **ret, int ifindex);
 
+int sd_device_new_child(sd_device **ret, sd_device *device, const char *suffix);
+
 int sd_device_get_parent(sd_device *child, sd_device **ret);
 int sd_device_get_parent_with_subsystem_devtype(sd_device *child, const char *subsystem, const char *devtype, sd_device **ret);
 
index 59db7641b58f4457013924393901f1e001c01a4e..63280440993f561b43bfe2a801131533de2275b7 100644 (file)
@@ -23,7 +23,6 @@
 #include <linux/pci_regs.h>
 
 #include "alloc-util.h"
-#include "chase-symlinks.h"
 #include "device-util.h"
 #include "dirent-util.h"
 #include "fd-util.h"
@@ -109,8 +108,7 @@ static int get_virtfn_info(sd_device *pcidev, sd_device **ret_physfn_pcidev, cha
                 return r;
 
         /* Get physical function's pci device. */
-        physfn_syspath = strjoina(syspath, "/physfn");
-        r = sd_device_new_from_syspath(&physfn_pcidev, physfn_syspath);
+        r = sd_device_new_child(&physfn_pcidev, pcidev, "physfn");
         if (r < 0)
                 return r;
 
@@ -124,21 +122,20 @@ static int get_virtfn_info(sd_device *pcidev, sd_device **ret_physfn_pcidev, cha
                 return -errno;
 
         FOREACH_DIRENT_ALL(de, dir, break) {
-                _cleanup_free_ char *virtfn_link_file = NULL, *virtfn_pci_syspath = NULL;
-                const char *n;
+                _cleanup_(sd_device_unrefp) sd_device *virtfn_pcidev = NULL;
+                const char *n, *s;
 
                 n = startswith(de->d_name, "virtfn");
                 if (!n)
                         continue;
 
-                virtfn_link_file = path_join(physfn_syspath, de->d_name);
-                if (!virtfn_link_file)
-                        return -ENOMEM;
+                if (sd_device_new_child(&virtfn_pcidev, physfn_pcidev, de->d_name) < 0)
+                        continue;
 
-                if (chase_symlinks(virtfn_link_file, NULL, 0, &virtfn_pci_syspath, NULL) < 0)
+                if (sd_device_get_syspath(virtfn_pcidev, &s) < 0)
                         continue;
 
-                if (streq(syspath, virtfn_pci_syspath)) {
+                if (streq(ssyspath)) {
                         char *suffix;
 
                         suffix = strjoin("v", n);