Getting information from sysfs

network-interfacesysfs

I'm creating a script for polling information about network interfaces. For this I get some data from /sysfs.

Everything went fine until I wanted to clarify all possible states of an interface (Which are they btw? I'm aware for now only about up, down and unknown). I went through /usr/src/linux/Documentation/sysfs-rules.txt and found this:

Accessing /sys/class/net/eth0/device is a bug in the application

Is there a reason for this?
Could someone explain me if I'm doing something wrong by getting information from /sysfs?

I don't iterate through all interfaces in /sysfs but get all network interfaces with getifaddrs(3). Perhaps there is another method to get iface status?
Thanks.

Best Answer

It's the device link in class directories that you aren't supposed to use. The idea is that /sys/class/net/eth0 is a symbolic link to somewhere under /sys/devices, and the device link merely links to a (grand-)*parent directory; instead of using the device link, you're supposed to walk back to a parent directory if needed.

Accessing files in /sys/class/net/eth0/ is fine.

If you're refering to the operational status found in /sys/class/net/eth0/operstate, there are a few more. The names are defined in net/core/net-sysfs.c and the constants in include/uapi/linux/if.h. They come from RFC 2863.

Related Question