2

I know that I can poll the link state via the SIOCETHTOOL/ETHTOOL_GLINK ioctl but this would require to endlessly request the state. The same goes for parsing /var/log/messages. So my questin is, is there any way under Linux to wait on a state change of the ethernet cable from userspace?

David
  • 23

2 Answers2

7

How about ifplugd?

ifplugd is a Linux daemon which will automatically configure your ethernet device when a cable is plugged in and automatically unconfigure it if the cable is pulled. This is useful on laptops with onboard network adapters, since it will only configure the interface when a cable is really connected.

(There is also netplugd, but it was discontinued.)

sr_
  • 15,384
  • 1
    Thanks for the link. I was just interested how to do this myself (and not a complete program) but I might dig through the ifplugd code and see how it is done there. – David Mar 09 '12 at 13:52
  • Looks like NETLINK allows me to do what I want (found out by looking into ifplugd). Thanks! – David Mar 09 '12 at 14:33
  • I'm glad it helped, you're welcome. :) – sr_ Mar 09 '12 at 14:46
0

The solution of sr_ is better than mine, but you could use those ioctls with sleep to avoid the endless stressing of the resources. Write a loop which investigates if there is the questioned link up/down, then do something/nothing in either cases, and then sleep a few seconds. It will generate just a little load on your system.

vakufo
  • 667