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?

2 Answers
2

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.

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.