C语言 在 Linux 上获取有关网络接口更改的通知
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2261759/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get notified about network interface change on Linux
提问by live2dream95
I need a way to notify my user space app when a network interface is enabled or disabled. I'm hoping to do this without resorting to polling. Does the kernel offer some sort of hook for triggering callback functions when network-related events occur?
我需要一种方法来在启用或禁用网络接口时通知我的用户空间应用程序。我希望在不诉诸投票的情况下做到这一点。当网络相关事件发生时,内核是否提供某种钩子来触发回调函数?
回答by Wyzard
I believe the netlink (man 7 netlink) facility provides information about network interfaces via the NETLINK_ROUTEfamily (man 7 rtnetlink). You may be able to select()or poll()on a netlink socket to get the information you want. I'm not certain of this, though; I haven't used it myself.
我相信 netlink ( man 7 netlink) 工具通过NETLINK_ROUTE系列 ( man 7 rtnetlink)提供有关网络接口的信息。您可以通过netlink 套接字select()或poll()在 netlink 套接字上获取所需信息。不过,我不确定这一点。我自己没用过。
At a higher level, if the system is running NetworkManager, that'll broadcast events via D-Bus when the system's network status changes. The Epiphany browser uses these events, for example, to automatically activate "Work Offline" mode when the system loses its network connection, and switch back to online mode when network connectivity resumes. There are D-Bus client libraries for a variety of languages, and it's less platform-specific than netlink, so this is what I'd recommend using.
在更高级别上,如果系统正在运行NetworkManager,那么当系统的网络状态发生变化时,它将通过 D-Bus 广播事件。例如,Epiphany 浏览器使用这些事件在系统失去网络连接时自动激活“离线工作”模式,并在网络连接恢复时切换回在线模式。有适用于各种语言的 D-Bus 客户端库,它比 netlink 更不特定于平台,所以这是我推荐使用的。
回答by Norman Ramsey
Options:
选项:
ifplugdwill run the script of your choice when a cable is plugged into or unplugged from your network interface.If you are using Debian you can add a script to the subdirectories of
/etc/network, where there are scripts that are run every time an interface goes up or down.If neither of the above are suitable for your needs, look into D-Bus. I've never had much luck using it successfully, but this is the sort of thing it was designed for.
ifplugd当电缆插入网络接口或从网络接口拔出时,将运行您选择的脚本。如果您使用的是 Debian,您可以将脚本添加到 的子目录中
/etc/network,其中有每次接口启动或关闭时运行的脚本。如果以上都不适合您的需求,请查看D-Bus。我从来没有成功地使用过它,但这就是它的设计目的。
回答by Tim Post
Without polling or relying on OS hooks like Norman Ramsey suggested, your only option is to periodically check the working interface against what it was a few seconds ago. That only leads to event / rx / tx queue hell, if the idea is to queue / block waiting for the chatter when the link is unavailable.
没有轮询或依赖于诺曼拉姆齐建议的操作系统挂钩,您唯一的选择是定期检查工作界面与几秒钟前的情况。如果想法是在链接不可用时排队/阻止等待聊天,那只会导致事件/接收/发送队列地狱。
Also, the Debian scripts work in conjunction with the init scripts, the hooks they provide won't tell you if root used ifconfig to down or modify a configured interface, or configured a new one that wasn't described to init. They only tell you if the user did something via /etc/init.d/networking, or if init did the same.
此外,Debian 脚本与 init 脚本一起工作,它们提供的钩子不会告诉您 root 是否使用 ifconfig 来关闭或修改已配置的接口,或者配置了一个未向 init 描述的新接口。它们只告诉您用户是否通过 /etc/init.d/networking 执行了某些操作,或者 init 是否执行了相同操作。
What is so terribly wretched about starting a thread to poll for changes?
启动一个线程来轮询更改有什么可怕的?
Short of that, yes, you could start a 'netnicmond' process that signaled a list of subscribers to let them know that somethingchanged, or a more complex one that actually conveyed the changes. You'd probably use netlink for that ...
除此之外,是的,您可以启动一个“netnicmond”流程,向订阅者列表发出信号,让他们知道某些事情发生了变化,或者更复杂的事情实际上传达了这些变化。您可能会为此使用 netlink ...
Have you considered just using netlink?
你有没有考虑过只使用netlink?
回答by t0mm13b
Why not do a forkin the code, using popento do either:
为什么不在fork代码中做一个,popen用来做任何一个:
- Issue
tail -f /var/log/messageslooking foreth*interfaces - Issue
ifconfigto see the list of adapters going up or down
- 发出
tail -f /var/log/messages寻找eth*接口 - 发出
ifconfig查看适配器列表上升或下降的问题
and parse the data using a pregex, do a sleep()for a period of time and recheck again.. Or you could go monitor the system log facility using the 'syslog.h' functions.
并使用 a 解析数据pregex,执行sleep()一段时间并再次检查。或者您可以使用“ syslog.h”函数来监视系统日志工具。
If you stick to the Posix standard, the code will be portable across different flavours of Unix/Linux/BSD...
如果你坚持 Posix 标准,代码将可以在不同风格的 Unix/Linux/BSD 之间移植......

