Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question
add comment

2 Answers

up vote 8 down vote accepted

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.)

share|improve this answer
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 9 '12 at 13:52
    
Looks like NETLINK allows me to do what I want (found out by looking into ifplugd). Thanks! –  David Mar 9 '12 at 14:33
    
I'm glad it helped, you're welcome. :) –  sr_ Mar 9 '12 at 14:46
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.