如何检测/proc/net/文件中的更改? [英] How to detect changes in /proc/net/ files?
问题描述
我期待通过查看/proc/net文件夹的内容来创建网络监视器.我的具体要求是我不能进行任何数据包嗅探或类似操作.我所有的来源都是/proc/net.例如,我可以从/proc/net/tcp等获取所有活动的TCP连接详细信息.
I'm looking forward to create a network monitor by looking into the contents of /proc/net folder. It's my specific requirement that I can't do any packet sniffing or anything like that. All my source is /proc/net.For example I can get all the active TCP connection details from /proc/net/tcp etc.
这些文件的内容一直在变化,所以我想连续读取这些文件,但也需要仅在其内容更改时才读取,也就是说,如果没有网络连接,文件内容将不会更改,而我不会不需要阅读它们.
The contents of these files keep on changing, so I want to read these files continuously but also I need to read only when it's contents changes, that is if there is no network connected the file contents won't change and I don't' need to read them.
我查看了inotify,但未检测到/proc/net/文件中的更改.
I looked into inotify but it does not detect the changes in /proc/net/ files.
inotifywatch /proc/net/
我想连续轮询是无效的.因此,正在寻找建议. 预先感谢..
Continuous polling I guess will be ineffective. So looking for a suggestion.. Thanks in advance..
推荐答案
您是否检查了gio库?您可以将手表添加到打开的文件中,并指定在哪些事件上获得回调.
Did you check the gio libraries? You can add a watch to an open file, and specify on which events you get a callback.
https://developer.gnome.org/glib/stable/glib-IO-Channels.html
此外,您可能对此很感兴趣(似乎较新的版本已包含此补丁程序):
Also, this might be of interest to you (it seems newer versions would have this patch already included):
https://gitorious.org/gnome-essentials/glib/commit/68f9255ec6434b25339cfd6055013e898 /a>
https://gitorious.org/gnome-essentials/glib/commit/68f9255ec6434b25339cfd6055013e898730d0e7