Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get event for NIC/ethernet card link status on linux

Tags:

c

linux

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 ?

like image 745
user1255770 Avatar asked Oct 23 '25 09:10

user1255770


2 Answers

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

like image 50
ninjalj Avatar answered Oct 24 '25 23:10

ninjalj


If the contents of this file /sys/class/net/eth0/carrier are 1, then the interface eth0 has carrier.

like image 26
Bash Avatar answered Oct 24 '25 23:10

Bash