sd-device: introduce sd_device_new_from_ifname/ifindex()

This commit is contained in:
Yu Watanabe 2021-06-09 23:27:20 +09:00
parent afdf6c3b60
commit bd44a727f7
3 changed files with 56 additions and 28 deletions

View file

@ -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;

View file

@ -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 '+': {

View file

@ -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);