0

I read some questions here but couldn't really find the specific problem I'am faced with here...

I need to implement a "DeviceCache" in a particular project which caches all device-names found in /proc/net/dev . The Language is C/++

So I thought about a seperate thread looking every X seconds in the directory mentioned above but was encouraged to find a more direct way. How can I register a method of my process to the device manager of linux? Is there a similar way like events/signals?

I looked in other sites but couldn't find any helpful code... Im relatively new to Linux-programming but willing to learn new things :)

Ahnihmuhs
  • 107
  • 1
  • 2
  • 7
  • Why are you caching something like that? Reading the file is likely to be plenty fast. What are you really trying to accomplish? – bdonlan Sep 23 '11 at 20:10
  • The DeviceCache will be used by a socket-framework in order to take the "fastet" device to communicate. So if one of the devices are unplugged the system should change the device to the second fast one – Ahnihmuhs Sep 23 '11 at 20:12
  • `/proc/net/dev` does not give media-connected information – bdonlan Sep 23 '11 at 20:14
  • Is NetworkManager available on your distro of choice? – bdonlan Sep 23 '11 at 20:14
  • but it lists all the plugged/installed devices like eth, wlan and ppp - which I use to connect to another socket. Or did I misunderstand that file? PS: AS this code should run on embedded systems, i can't install any other libraries as the standard ones in linux, so I assume NetworkManager is not installed either – Ahnihmuhs Sep 23 '11 at 20:16
  • @Ahnihmuhs, /proc/net/dev lists all registered interfaces. This includes ones where the cable is unplugged, or where they're disabled, or where they are software interfaces (pppN for example) that are unconfigured... – bdonlan Sep 23 '11 at 20:20
  • If you really want to go this way - You can use inotify (http://en.wikipedia.org/wiki/Inotify) to watch for changes in order to prevent polling. – Atmocreations Sep 23 '11 at 20:24
  • aah okay but that's no problem though, I just need to update the cache everytime an (new) interface is registered/unregistered, regardless of the actual operational reliability :S Yeah it sounds weird but hey, thats my assignment : – Ahnihmuhs Sep 23 '11 at 20:24

1 Answers1

1

Based on your comments, what you really want is to track which network interfaces are operational at any given time.

The only true way to determine if a network interface is up is to test it - after all, the router on the other end may be down. You could send pings out periodically, for example.

However, if you just want to know if the media goes down (ie, the network cable is unplugged), take a look at these SO questions:

If you just want to be notified of the actual hardware-level registration of interfaces (eg, when a USB NIC is plugged in), you can use udev events if your platform has udev; otherwise, I believe there's another netlink category for hardware addition/removal events.

Community
  • 1
  • 1
bdonlan
  • 224,562
  • 31
  • 268
  • 324