Rules on how to access information in sysfs¶
The kernel-exported sysfs exports internal kernel implementation detailsand depends on internal kernel structures and layout. It is agreed uponby the kernel developers that the Linux kernel does not provide a stableinternal API. Therefore, there are aspects of the sysfs interface thatmay not be stable across kernel releases.
To minimize the risk of breaking users of sysfs, which are in most caseslow-level userspace applications, with a new kernel release, the usersof sysfs must follow some rules to use an as-abstract-as-possible way toaccess this filesystem. The current udev and HAL programs alreadyimplement this and users are encouraged to plug, if possible, into theabstractions these programs provide instead of accessing sysfs directly.
But if you really do want or need to access sysfs directly, please followthe following rules and then your programs should work with futureversions of the sysfs interface.
- Do not use libsysfs
It makes assumptions about sysfs which are not true. Its API does notoffer any abstraction, it exposes all the kernel driver-coreimplementation details in its own API. Therefore it is not better thanreading directories and opening the files yourself.Also, it is not actively maintained, in the sense of reflecting thecurrent kernel development. The goal of providing a stable interfaceto sysfs has failed; it causes more problems than it solves. Itviolates many of the rules in this document.
- sysfs is always at
/sys Parsing
/proc/mountsis a waste of time. Other mount points are asystem configuration bug you should not try to solve. For test cases,possibly support aSYSFS_PATHenvironment variable to overwrite theapplication’s behavior, but never try to search for sysfs. Never tryto mount it, if you are not an early boot script.
- sysfs is always at
- devices are only “devices”
There is no such thing like class-, bus-, physical devices,interfaces, and such that you can rely on in userspace. Everything isjust simply a “device”. Class-, bus-, physical, ... types are justkernel implementation details which should not be expected byapplications that look for devices in sysfs.
The properties of a device are:
devpath (
/devices/pci0000:00/0000:00:1d.1/usb2/2-2/2-2:1.0)identical to the DEVPATH value in the event sent from the kernelat device creation and removal
the unique key to the device at that point in time
the kernel’s path to the device directory without the leading
/sys, and always starting with a slashall elements of a devpath must be real directories. Symlinkspointing to /sys/devices must always be resolved to their realtarget and the target path must be used to access the device.That way the devpath to the device matches the devpath of thekernel used at event time.
using or exposing symlink values as elements in a devpath stringis a bug in the application
kernel name (
sda,tty,0000:00:1f.2, ...)a directory name, identical to the last element of the devpath
applications need to handle spaces and characters like
!inthe name
subsystem (
block,tty,pci, ...)simple string, never a path or a link
retrieved by reading the “subsystem”-link and using only thelast element of the target path
driver (
tg3,ata_piix,uhci_hcd)a simple string, which may contain spaces, never a path or alink
it is retrieved by reading the “driver”-link and using only thelast element of the target path
devices which do not have “driver”-link just do not have adriver; copying the driver value in a child device context is abug in the application
attributes
the files in the device directory or files below subdirectoriesof the same device directory
accessing attributes reached by a symlink pointing to another device,like the “device”-link, is a bug in the application
Everything else is just a kernel driver-core implementation detailthat should not be assumed to be stable across kernel releases.
- Properties of parent devices never belong into a child device.
Always look at the parent devices themselves for determining devicecontext properties. If the device
eth0orsdadoes not have a“driver”-link, then this device does not have a driver. Its value is empty.Never copy any property of the parent-device into a child-device. Parentdevice properties may change dynamically without any notice to thechild device.
- Hierarchy in a single device tree
There is only one valid place in sysfs where hierarchy can be examinedand this is below:
/sys/devices.It is planned that all device directories will end up in the treebelow this directory.
- Classification by subsystem
There are currently three places for classification of devices:
/sys/block,/sys/classand/sys/bus.It is planned that these willnot contain any device directories themselves, but only flat lists ofsymlinks pointing to the unified/sys/devicestree.All three places have completely different rules on how to accessdevice information. It is planned to merge all threeclassification directories into one place at/sys/subsystem,following the layout of the bus directories. All buses andclasses, including the converted block subsystem, will show upthere.The devices belonging to a subsystem will create a symlink in the“devices” directory at/sys/subsystem/<name>/devices,If
/sys/subsystemexists,/sys/bus,/sys/classand/sys/blockcan be ignored. If it does not exist, you always have to scan all threeplaces, as the kernel is free to move a subsystem from one place tothe other, as long as the devices are still reachable by the samesubsystem name.Assuming
/sys/class/<subsystem>and/sys/bus/<subsystem>, or/sys/blockand/sys/class/blockare not interchangeable is a bug inthe application.
- Block
The converted block subsystem at
/sys/class/blockor/sys/subsystem/blockwill contain the links for disks and partitionsat the same level, never in a hierarchy. Assuming the block subsystem tocontain only disks and not partition devices in the same flat list isa bug in the application.
- “device”-link and <subsystem>:<kernel name>-links
Never depend on the “device”-link. The “device”-link is a workaroundfor the old layout, where class devices are not created in
/sys/devices/like the bus devices. If the link-resolving of adevice directory does not end in/sys/devices/, you can use the“device”-link to find the parent devices in/sys/devices/, That is thesingle valid use of the “device”-link; it must never appear in anypath as an element. Assuming the existence of the “device”-link fora device in/sys/devices/is a bug in the application.Accessing/sys/class/net/eth0/deviceis a bug in the application.Never depend on the class-specific links back to the
/sys/classdirectory. These links are also a workaround for the design mistakethat class devices are not created in/sys/devices.If a devicedirectory does not contain directories for child devices, these linksmay be used to find the child devices in/sys/class.That is the singlevalid use of these links; they must never appear in any path as anelement. Assuming the existence of these links for devices which arereal child device directories in the/sys/devicestree is a bug inthe application.It is planned to remove all these links when all class devicedirectories live in
/sys/devices.
- Position of devices along device chain can change.
Never depend on a specific parent device position in the devpath,or the chain of parent devices. The kernel is free to insert devices intothe chain. You must always request the parent device you are looking forby its subsystem value. You need to walk up the chain until you findthe device that matches the expected subsystem. Depending on a specificposition of a parent device or exposing relative paths using
../toaccess the chain of parents is a bug in the application.
- When reading and writing sysfs device attribute files, avoid dependency
on specific error codes wherever possible. This minimizes coupling tothe error handling implementation within the kernel.
In general, failures to read or write sysfs device attributes shallpropagate errors wherever possible. Common errors include, but are notlimited to:
-EIO: The read or store operation is not supported, typicallyreturned by the sysfs system itself if the read or store pointerisNULL.-ENXIO: The read or store operation failedError codes will not be changed without good reason, and should a changeto error codes result in user-space breakage, it will be fixed, or thethe offending change will be reverted.
Userspace applications can, however, expect the format and contents ofthe attribute files to remain consistent in the absence of a versionattribute change in the context of a given attribute.