问题
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.
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..
回答1:
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/68f9255ec6434b25339cfd6055013e898730d0e7
https://www.google.com.ar/url?sa=t&rct=j&q=&esrc=s&source=web&cd=2&cad=rja&uact=8&ved=0CCEQFjAB&url=https%3A%2F%2Fmail.gnome.org%2Farchives%2Fcommits-list%2F2011-September%2Fmsg13539.html&ei=02qMVJ7cHse1sQTm2oKwDg&usg=AFQjCNEJpurm10iskmcHVkc81oAv8_7MLQ&sig2=e48bXfZxW_BwvNCjdpsfSw&bvm=bv.81828268,d.cWc
来源:https://stackoverflow.com/questions/27460949/how-to-detect-changes-in-proc-net-files