#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"
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,
}
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 '+': {
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);