From: Yu Watanabe Date: Wed, 9 Jun 2021 14:27:20 +0000 (+0900) Subject: sd-device: introduce sd_device_new_from_ifname/ifindex() X-Git-Tag: v249-rc1~28^2~5 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=bd44a727f736d21dd47b5dfbdbe79d21acbdc2a4;p=thirdparty%2Fsystemd.git sd-device: introduce sd_device_new_from_ifname/ifindex() --- diff --git a/src/libsystemd/libsystemd.sym b/src/libsystemd/libsystemd.sym index 1606981e1e1..3730db6eef9 100644 --- a/src/libsystemd/libsystemd.sym +++ b/src/libsystemd/libsystemd.sym @@ -759,4 +759,6 @@ global: sd_device_get_usec_initialized; sd_device_trigger_with_uuid; sd_device_get_trigger_uuid; + sd_device_new_from_ifname; + sd_device_new_from_ifindex; } LIBSYSTEMD_248; diff --git a/src/libsystemd/sd-device/sd-device.c b/src/libsystemd/sd-device/sd-device.c index f31416da530..b5e7a1a44d1 100644 --- a/src/libsystemd/sd-device/sd-device.c +++ b/src/libsystemd/sd-device/sd-device.c @@ -14,10 +14,12 @@ #include "dirent-util.h" #include "fd-util.h" #include "fileio.h" +#include "format-util.h" #include "fs-util.h" #include "hashmap.h" #include "id128-util.h" #include "macro.h" +#include "netlink-util.h" #include "parse-util.h" #include "path-util.h" #include "set.h" @@ -245,6 +247,52 @@ _public_ int sd_device_new_from_devnum(sd_device **ret, char type, dev_t devnum) return sd_device_new_from_syspath(ret, syspath); } +static int device_new_from_main_ifname(sd_device **ret, const char *ifname) { + const char *syspath; + + assert(ret); + assert(ifname); + + syspath = strjoina("/sys/class/net/", ifname); + return sd_device_new_from_syspath(ret, syspath); +} + +_public_ int sd_device_new_from_ifname(sd_device **ret, const char *ifname) { + _cleanup_free_ char *main_name = NULL; + int r; + + assert_return(ret, -EINVAL); + assert_return(ifname, -EINVAL); + + r = parse_ifindex(ifname); + if (r > 0) + return sd_device_new_from_ifindex(ret, r); + + if (ifname_valid(ifname)) { + r = device_new_from_main_ifname(ret, ifname); + if (r >= 0) + return r; + } + + r = rtnl_resolve_link_alternative_name(NULL, ifname, &main_name); + if (r < 0) + return r; + + return device_new_from_main_ifname(ret, main_name); +} + +_public_ int sd_device_new_from_ifindex(sd_device **ret, int ifindex) { + char ifname[IF_NAMESIZE + 1]; + + assert_return(ret, -EINVAL); + assert_return(ifindex > 0, -EINVAL); + + if (!format_ifname(ifindex, ifname)) + return -ENODEV; + + return device_new_from_main_ifname(ret, ifname); +} + static int device_strjoin_new( const char *a, const char *b, @@ -643,37 +691,13 @@ _public_ int sd_device_new_from_device_id(sd_device **ret, const char *id) { } case 'n': { - _cleanup_(sd_device_unrefp) sd_device *device = NULL; - _cleanup_close_ int sk = -1; - struct ifreq ifr = {}; int ifindex; - r = ifr.ifr_ifindex = parse_ifindex(id + 1); - if (r < 0) - return r; + ifindex = parse_ifindex(id + 1); + if (ifindex < 0) + return ifindex; - sk = socket_ioctl_fd(); - if (sk < 0) - return sk; - - r = ioctl(sk, SIOCGIFNAME, &ifr); - if (r < 0) - return -errno; - - r = sd_device_new_from_subsystem_sysname(&device, "net", ifr.ifr_name); - if (r < 0) - return r; - - r = sd_device_get_ifindex(device, &ifindex); - if (r < 0) - return r; - - /* this is racey, so we might end up with the wrong device */ - if (ifr.ifr_ifindex != ifindex) - return -ENODEV; - - *ret = TAKE_PTR(device); - return 0; + return sd_device_new_from_ifindex(ret, ifindex); } case '+': { diff --git a/src/systemd/sd-device.h b/src/systemd/sd-device.h index 04599ea3821..7ab0a8334eb 100644 --- a/src/systemd/sd-device.h +++ b/src/systemd/sd-device.h @@ -62,6 +62,8 @@ int sd_device_new_from_devnum(sd_device **ret, char type, dev_t devnum); int sd_device_new_from_subsystem_sysname(sd_device **ret, const char *subsystem, const char *sysname); int sd_device_new_from_device_id(sd_device **ret, const char *id); int sd_device_new_from_stat_rdev(sd_device **ret, const struct stat *st); +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_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);