1
0
mirror of https://github.com/systemd/systemd.git synced 2025-03-21 02:50:18 +03:00

sd_device: introduce device_get_ifname()

sd-device replaces '!' in sysname with '/'. Hence, sysname and ifname
may be different. Let's get network interface name through INTERFACE
property.
This commit is contained in:
Yu Watanabe 2025-03-06 07:03:45 +09:00
parent 32b0e689d6
commit bec2f4dc3e
3 changed files with 24 additions and 0 deletions

View File

@ -21,6 +21,7 @@ int device_opendir(sd_device *device, const char *subdir, DIR **ret);
int device_get_sysnum_unsigned(sd_device *device, unsigned *ret);
int device_get_property_bool(sd_device *device, const char *key);
int device_get_property_int(sd_device *device, const char *key, int *ret);
int device_get_ifname(sd_device *device, const char **ret);
int device_get_sysattr_int(sd_device *device, const char *sysattr, int *ret_value);
int device_get_sysattr_unsigned_full(sd_device *device, const char *sysattr, unsigned base, unsigned *ret_value);
static inline int device_get_sysattr_unsigned(sd_device *device, const char *sysattr, unsigned *ret_value) {

View File

@ -871,6 +871,21 @@ _public_ int sd_device_get_ifindex(sd_device *device, int *ifindex) {
return 0;
}
int device_get_ifname(sd_device *device, const char **ret) {
int r;
assert_return(device, -EINVAL);
/* First, check if the device is a network interface. */
r = sd_device_get_ifindex(device, NULL);
if (r < 0)
return r;
/* The sysname and ifname may be different, as '!' in sysname are replaced with '/'.
* For network interfaces, we can use INTERFACE property. */
return sd_device_get_property_value(device, "INTERFACE", ret);
}
_public_ int sd_device_new_from_device_id(sd_device **ret, const char *id) {
int r;

View File

@ -52,6 +52,14 @@ static void test_sd_device_one(sd_device *d) {
else {
ASSERT_GT(ifindex, 0);
const char *ifname;
ASSERT_OK(device_get_ifname(d, &ifname));
ASSERT_NOT_NULL(endswith(syspath, ifname));
if (strchr(sysname, '/'))
ASSERT_FALSE(streq(ifname, sysname));
else
ASSERT_STREQ(ifname, sysname);
r = sd_device_new_from_ifindex(&dev, ifindex);
if (r < 0) {
ASSERT_ERROR(r, ENODEV);