sd-device: introduce sd_device_new_child()

This commit is contained in:
Yu Watanabe
2022-08-05 21:49:23 +09:00
parent 578355684d
commit 68a52f5973
5 changed files with 36 additions and 17 deletions
+2
View 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;
+23
View 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;
+1 -6
View 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;
+2
View 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);
+8 -11
View 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 (chase_symlinks(virtfn_link_file, NULL, 0, &virtfn_pci_syspath, NULL) < 0)
if (sd_device_new_child(&virtfn_pcidev, physfn_pcidev, de->d_name) < 0)
continue;
if (streq(syspath, virtfn_pci_syspath)) {
if (sd_device_get_syspath(virtfn_pcidev, &s) < 0)
continue;
if (streq(s, syspath)) {
char *suffix;
suffix = strjoin("v", n);