Is there any API(or special file) on Linux that provides an event on the change in link status of an ethernet card without polling ? i.e. when you plug in/out the cable ?
You can get link up/down events via netlink. You would create a PF_NETLINK socket, bind to the RTMGRP_LINK group, and wait for RTM_NEWLINK/RTM_DELLINK messages. Link up events have IFF_RUNNING and IFF_LOWER_UP set in ifinfomsg.flags, while link down events have both flags cleared (funnily enough, I'm getting RTM_NEWLINK messages for both type of events).
If the contents of this file /sys/class/net/eth0/carrier are 1, then the interface eth0 has carrier.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With