0

We have an embedded SoC running BusyBox Linux (kernel 2.6.x), and we have a need to monitor or at least notice in a timely manner when the network connection goes down or comes up (catching other events would be good but not essential).

I've spent a long time googling & reading SO threads and there seems to be a ton of different answers depending on the exact task at hand on the particular OS and the phases of the moon etc.

Our specific criteria are:

  • We are looking from inside a C program, so C code is preferable to command line calls.
  • Although the interface is always there, we can't guarantee it is or has ever been up (I have seen comments on some examples that only work when the interface is up even if the link is down)
  • It would be nice not to have to poll, but rather to send/catch status change messages as and when they happen. I believe the kernel may already get such messages from the driver, but I'm not sure if there's anything we can hook into?

I've narrowed the likely seeming answers down to a few candidates but can't work out which is the nicest (least overhead, most reliable, least likely to break in future versions):

  • cat sys/class/net/eth0/operstate
  • cat sys/class/net/eth0/carrier (I can find no good explanation of the difference between these two)
  • Using ifreq or various sequences of ioctl calls to read the socket status (looks kinda messy to me) as per answers here and here (more tidy looking).
  • Somehow catch status change messages???
Community
  • 1
  • 1
John U
  • 2,886
  • 3
  • 27
  • 39

1 Answers1

0

You can use inotify to keep check on the /sys/class/net/eth0/operstate. Inotify allows different events to be watched on specified file or directory e.g. CREATE, MODIFY, WRITE etc.

You can seen the working example here

Arshan
  • 736
  • 6
  • 19