C – How do I use select() to wait for the Ethernet interface status to change?

How do I use select() to wait for the Ethernet interface status to change?… here is a solution to the problem.

How do I use select() to wait for the Ethernet interface status to change?

My program has to detect when the Ethernet interface changes to the RUNNING state.

I can use ioctl() polling run flag:

if( ioctl( dummy_fd, SIOCGIFFLAGS, &ifr ) != -1 )
    return ifr.ifr_flags & IFF_RUNNING;

But you want to get information immediately without polling. Currently the program uses select() to wait for other events. So it’s best to use select() to detect changes in the state of the interface.

There is a way to do it with select, which I did read from the web. But I can’t find that page anymore.

Solution

The kernel sends information about changes to the network interface via netlink; See, for example, here for an example< a href="http://softengcrunch.blogspot.cz/2010/12/communicating-with-kernel-via-netlink.html" rel="noreferrer noopener nofollow" > http://softengcrunch.blogspot.cz/2010/12/communicating-with-kernel-via-netlink.html
A fast and dirty hack is to poll after any NetLink event (when select wakes up on the NetLink socket) without actually parsing the NetLink packet; )

Related Problems and Solutions