From: Yu Watanabe Date: Tue, 26 Jul 2022 19:16:55 +0000 (+0900) Subject: sd-device: introduce sd_device_new_child() X-Git-Tag: v252-rc1~505^2~9 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=68a52f597373bc012dd4113142b7d8bcd7aacb42;p=thirdparty%2Fsystemd.git sd-device: introduce sd_device_new_child() --- diff --git a/src/libsystemd/libsystemd.sym b/src/libsystemd/libsystemd.sym index fb6e6f6431b..5c5446e9876 100644 --- a/src/libsystemd/libsystemd.sym +++ b/src/libsystemd/libsystemd.sym @@ -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; diff --git a/src/libsystemd/sd-device/sd-device.c b/src/libsystemd/sd-device/sd-device.c index 3e31beb1f2b..1050234afe9 100644 --- a/src/libsystemd/sd-device/sd-device.c +++ b/src/libsystemd/sd-device/sd-device.c @@ -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; diff --git a/src/network/networkd-wiphy.c b/src/network/networkd-wiphy.c index 38d1ceeba1e..bc93252cb06 100644 --- a/src/network/networkd-wiphy.c +++ b/src/network/networkd-wiphy.c @@ -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; diff --git a/src/systemd/sd-device.h b/src/systemd/sd-device.h index 19dd4d9eefa..0b2f8f8b429 100644 --- a/src/systemd/sd-device.h +++ b/src/systemd/sd-device.h @@ -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); diff --git a/src/udev/udev-builtin-net_id.c b/src/udev/udev-builtin-net_id.c index 59db7641b58..63280440993 100644 --- a/src/udev/udev-builtin-net_id.c +++ b/src/udev/udev-builtin-net_id.c @@ -23,7 +23,6 @@ #include #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(s, syspath)) { char *suffix; suffix = strjoin("v", n);