From: Yu Watanabe Date: Wed, 5 Mar 2025 22:03:45 +0000 (+0900) Subject: sd_device: introduce device_get_ifname() X-Git-Tag: v258-rc1~1101^2~10 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=bec2f4dc3e2714409be6ba7e099eb524d475110a;p=thirdparty%2Fsystemd.git 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. --- diff --git a/src/libsystemd/sd-device/device-private.h b/src/libsystemd/sd-device/device-private.h index 7d925aa581d..dea77286339 100644 --- a/src/libsystemd/sd-device/device-private.h +++ b/src/libsystemd/sd-device/device-private.h @@ -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) { diff --git a/src/libsystemd/sd-device/sd-device.c b/src/libsystemd/sd-device/sd-device.c index 181a9256dd2..b7b308b3908 100644 --- a/src/libsystemd/sd-device/sd-device.c +++ b/src/libsystemd/sd-device/sd-device.c @@ -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; diff --git a/src/libsystemd/sd-device/test-sd-device.c b/src/libsystemd/sd-device/test-sd-device.c index 705a10d159f..b2b04562cde 100644 --- a/src/libsystemd/sd-device/test-sd-device.c +++ b/src/libsystemd/sd-device/test-sd-device.c @@ -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);