如何在不轮询内核的情况下获取C程序中新添加的NIC的pci地址?

如何在不轮询内核的情况下获取C程序中新添加的NIC的pci地址?,c,sockets,netlink,C,Sockets,Netlink,根据下面的链接,我了解到通过创建netlink套接字并监听RTMGRP_链接,我们可以检测事件(网络接口创建/删除/向上/向下事件) 一旦检测到新添加的接口,是否可以获取其pci地址?一旦知道其接口名称(如eth0),就可以使用ethtool API查询该信息: #include <linux/ethtool.h> #include <linux/sockios.h> .. and other socket headers. ... int sock = sock

根据下面的链接,我了解到通过创建netlink套接字并监听RTMGRP_链接,我们可以检测事件(网络接口创建/删除/向上/向下事件)


一旦检测到新添加的接口,是否可以获取其pci地址?

一旦知道其接口名称(如eth0),就可以使用ethtool API查询该信息:

#include <linux/ethtool.h> 
#include <linux/sockios.h>
.. and other socket headers.
...

 int sock = socket(PF_INET, SOCK_DGRAM, 0);

 struct ifreq ifr;
 struct ethtool_cmd cmd;
 struct ethtool_drvinfo drvinfo;

 memset(&ifr, 0, sizeof ifr);
 memset(&cmd, 0, sizeof cmd);
 memset(&drvinfo, 0, sizeof drvinfo);
 strcpy(ifr.ifr_name, "eno1");

 ifr.ifr_data = &drvinfo;
 drvinfo.cmd = ETHTOOL_GDRVINFO;

 if(ioctl(sock, SIOCETHTOOL, &ifr) < 0) {
    perror("ioctl")
    return;
 }
$ ethtool -i eno1
driver: e1000e
version: 2.3.2-k
firmware-version: 0.13-4
bus-info: 0000:00:19.0
supports-statistics: yes
supports-test: yes
supports-eeprom-access: yes
supports-register-dump: yes
supports-priv-flags: no