162306a36Sopenharmony_ciRules on how to access information in sysfs 262306a36Sopenharmony_ci=========================================== 362306a36Sopenharmony_ci 462306a36Sopenharmony_ciThe kernel-exported sysfs exports internal kernel implementation details 562306a36Sopenharmony_ciand depends on internal kernel structures and layout. It is agreed upon 662306a36Sopenharmony_ciby the kernel developers that the Linux kernel does not provide a stable 762306a36Sopenharmony_ciinternal API. Therefore, there are aspects of the sysfs interface that 862306a36Sopenharmony_cimay not be stable across kernel releases. 962306a36Sopenharmony_ci 1062306a36Sopenharmony_ciTo minimize the risk of breaking users of sysfs, which are in most cases 1162306a36Sopenharmony_cilow-level userspace applications, with a new kernel release, the users 1262306a36Sopenharmony_ciof sysfs must follow some rules to use an as-abstract-as-possible way to 1362306a36Sopenharmony_ciaccess this filesystem. The current udev and HAL programs already 1462306a36Sopenharmony_ciimplement this and users are encouraged to plug, if possible, into the 1562306a36Sopenharmony_ciabstractions these programs provide instead of accessing sysfs directly. 1662306a36Sopenharmony_ci 1762306a36Sopenharmony_ciBut if you really do want or need to access sysfs directly, please follow 1862306a36Sopenharmony_cithe following rules and then your programs should work with future 1962306a36Sopenharmony_civersions of the sysfs interface. 2062306a36Sopenharmony_ci 2162306a36Sopenharmony_ci- Do not use libsysfs 2262306a36Sopenharmony_ci It makes assumptions about sysfs which are not true. Its API does not 2362306a36Sopenharmony_ci offer any abstraction, it exposes all the kernel driver-core 2462306a36Sopenharmony_ci implementation details in its own API. Therefore it is not better than 2562306a36Sopenharmony_ci reading directories and opening the files yourself. 2662306a36Sopenharmony_ci Also, it is not actively maintained, in the sense of reflecting the 2762306a36Sopenharmony_ci current kernel development. The goal of providing a stable interface 2862306a36Sopenharmony_ci to sysfs has failed; it causes more problems than it solves. It 2962306a36Sopenharmony_ci violates many of the rules in this document. 3062306a36Sopenharmony_ci 3162306a36Sopenharmony_ci- sysfs is always at ``/sys`` 3262306a36Sopenharmony_ci Parsing ``/proc/mounts`` is a waste of time. Other mount points are a 3362306a36Sopenharmony_ci system configuration bug you should not try to solve. For test cases, 3462306a36Sopenharmony_ci possibly support a ``SYSFS_PATH`` environment variable to overwrite the 3562306a36Sopenharmony_ci application's behavior, but never try to search for sysfs. Never try 3662306a36Sopenharmony_ci to mount it, if you are not an early boot script. 3762306a36Sopenharmony_ci 3862306a36Sopenharmony_ci- devices are only "devices" 3962306a36Sopenharmony_ci There is no such thing like class-, bus-, physical devices, 4062306a36Sopenharmony_ci interfaces, and such that you can rely on in userspace. Everything is 4162306a36Sopenharmony_ci just simply a "device". Class-, bus-, physical, ... types are just 4262306a36Sopenharmony_ci kernel implementation details which should not be expected by 4362306a36Sopenharmony_ci applications that look for devices in sysfs. 4462306a36Sopenharmony_ci 4562306a36Sopenharmony_ci The properties of a device are: 4662306a36Sopenharmony_ci 4762306a36Sopenharmony_ci - devpath (``/devices/pci0000:00/0000:00:1d.1/usb2/2-2/2-2:1.0``) 4862306a36Sopenharmony_ci 4962306a36Sopenharmony_ci - identical to the DEVPATH value in the event sent from the kernel 5062306a36Sopenharmony_ci at device creation and removal 5162306a36Sopenharmony_ci - the unique key to the device at that point in time 5262306a36Sopenharmony_ci - the kernel's path to the device directory without the leading 5362306a36Sopenharmony_ci ``/sys``, and always starting with a slash 5462306a36Sopenharmony_ci - all elements of a devpath must be real directories. Symlinks 5562306a36Sopenharmony_ci pointing to /sys/devices must always be resolved to their real 5662306a36Sopenharmony_ci target and the target path must be used to access the device. 5762306a36Sopenharmony_ci That way the devpath to the device matches the devpath of the 5862306a36Sopenharmony_ci kernel used at event time. 5962306a36Sopenharmony_ci - using or exposing symlink values as elements in a devpath string 6062306a36Sopenharmony_ci is a bug in the application 6162306a36Sopenharmony_ci 6262306a36Sopenharmony_ci - kernel name (``sda``, ``tty``, ``0000:00:1f.2``, ...) 6362306a36Sopenharmony_ci 6462306a36Sopenharmony_ci - a directory name, identical to the last element of the devpath 6562306a36Sopenharmony_ci - applications need to handle spaces and characters like ``!`` in 6662306a36Sopenharmony_ci the name 6762306a36Sopenharmony_ci 6862306a36Sopenharmony_ci - subsystem (``block``, ``tty``, ``pci``, ...) 6962306a36Sopenharmony_ci 7062306a36Sopenharmony_ci - simple string, never a path or a link 7162306a36Sopenharmony_ci - retrieved by reading the "subsystem"-link and using only the 7262306a36Sopenharmony_ci last element of the target path 7362306a36Sopenharmony_ci 7462306a36Sopenharmony_ci - driver (``tg3``, ``ata_piix``, ``uhci_hcd``) 7562306a36Sopenharmony_ci 7662306a36Sopenharmony_ci - a simple string, which may contain spaces, never a path or a 7762306a36Sopenharmony_ci link 7862306a36Sopenharmony_ci - it is retrieved by reading the "driver"-link and using only the 7962306a36Sopenharmony_ci last element of the target path 8062306a36Sopenharmony_ci - devices which do not have "driver"-link just do not have a 8162306a36Sopenharmony_ci driver; copying the driver value in a child device context is a 8262306a36Sopenharmony_ci bug in the application 8362306a36Sopenharmony_ci 8462306a36Sopenharmony_ci - attributes 8562306a36Sopenharmony_ci 8662306a36Sopenharmony_ci - the files in the device directory or files below subdirectories 8762306a36Sopenharmony_ci of the same device directory 8862306a36Sopenharmony_ci - accessing attributes reached by a symlink pointing to another device, 8962306a36Sopenharmony_ci like the "device"-link, is a bug in the application 9062306a36Sopenharmony_ci 9162306a36Sopenharmony_ci Everything else is just a kernel driver-core implementation detail 9262306a36Sopenharmony_ci that should not be assumed to be stable across kernel releases. 9362306a36Sopenharmony_ci 9462306a36Sopenharmony_ci- Properties of parent devices never belong into a child device. 9562306a36Sopenharmony_ci Always look at the parent devices themselves for determining device 9662306a36Sopenharmony_ci context properties. If the device ``eth0`` or ``sda`` does not have a 9762306a36Sopenharmony_ci "driver"-link, then this device does not have a driver. Its value is empty. 9862306a36Sopenharmony_ci Never copy any property of the parent-device into a child-device. Parent 9962306a36Sopenharmony_ci device properties may change dynamically without any notice to the 10062306a36Sopenharmony_ci child device. 10162306a36Sopenharmony_ci 10262306a36Sopenharmony_ci- Hierarchy in a single device tree 10362306a36Sopenharmony_ci There is only one valid place in sysfs where hierarchy can be examined 10462306a36Sopenharmony_ci and this is below: ``/sys/devices.`` 10562306a36Sopenharmony_ci It is planned that all device directories will end up in the tree 10662306a36Sopenharmony_ci below this directory. 10762306a36Sopenharmony_ci 10862306a36Sopenharmony_ci- Classification by subsystem 10962306a36Sopenharmony_ci There are currently three places for classification of devices: 11062306a36Sopenharmony_ci ``/sys/block,`` ``/sys/class`` and ``/sys/bus.`` It is planned that these will 11162306a36Sopenharmony_ci not contain any device directories themselves, but only flat lists of 11262306a36Sopenharmony_ci symlinks pointing to the unified ``/sys/devices`` tree. 11362306a36Sopenharmony_ci All three places have completely different rules on how to access 11462306a36Sopenharmony_ci device information. It is planned to merge all three 11562306a36Sopenharmony_ci classification directories into one place at ``/sys/subsystem``, 11662306a36Sopenharmony_ci following the layout of the bus directories. All buses and 11762306a36Sopenharmony_ci classes, including the converted block subsystem, will show up 11862306a36Sopenharmony_ci there. 11962306a36Sopenharmony_ci The devices belonging to a subsystem will create a symlink in the 12062306a36Sopenharmony_ci "devices" directory at ``/sys/subsystem/<name>/devices``, 12162306a36Sopenharmony_ci 12262306a36Sopenharmony_ci If ``/sys/subsystem`` exists, ``/sys/bus``, ``/sys/class`` and ``/sys/block`` 12362306a36Sopenharmony_ci can be ignored. If it does not exist, you always have to scan all three 12462306a36Sopenharmony_ci places, as the kernel is free to move a subsystem from one place to 12562306a36Sopenharmony_ci the other, as long as the devices are still reachable by the same 12662306a36Sopenharmony_ci subsystem name. 12762306a36Sopenharmony_ci 12862306a36Sopenharmony_ci Assuming ``/sys/class/<subsystem>`` and ``/sys/bus/<subsystem>``, or 12962306a36Sopenharmony_ci ``/sys/block`` and ``/sys/class/block`` are not interchangeable is a bug in 13062306a36Sopenharmony_ci the application. 13162306a36Sopenharmony_ci 13262306a36Sopenharmony_ci- Block 13362306a36Sopenharmony_ci The converted block subsystem at ``/sys/class/block`` or 13462306a36Sopenharmony_ci ``/sys/subsystem/block`` will contain the links for disks and partitions 13562306a36Sopenharmony_ci at the same level, never in a hierarchy. Assuming the block subsystem to 13662306a36Sopenharmony_ci contain only disks and not partition devices in the same flat list is 13762306a36Sopenharmony_ci a bug in the application. 13862306a36Sopenharmony_ci 13962306a36Sopenharmony_ci- "device"-link and <subsystem>:<kernel name>-links 14062306a36Sopenharmony_ci Never depend on the "device"-link. The "device"-link is a workaround 14162306a36Sopenharmony_ci for the old layout, where class devices are not created in 14262306a36Sopenharmony_ci ``/sys/devices/`` like the bus devices. If the link-resolving of a 14362306a36Sopenharmony_ci device directory does not end in ``/sys/devices/``, you can use the 14462306a36Sopenharmony_ci "device"-link to find the parent devices in ``/sys/devices/``, That is the 14562306a36Sopenharmony_ci single valid use of the "device"-link; it must never appear in any 14662306a36Sopenharmony_ci path as an element. Assuming the existence of the "device"-link for 14762306a36Sopenharmony_ci a device in ``/sys/devices/`` is a bug in the application. 14862306a36Sopenharmony_ci Accessing ``/sys/class/net/eth0/device`` is a bug in the application. 14962306a36Sopenharmony_ci 15062306a36Sopenharmony_ci Never depend on the class-specific links back to the ``/sys/class`` 15162306a36Sopenharmony_ci directory. These links are also a workaround for the design mistake 15262306a36Sopenharmony_ci that class devices are not created in ``/sys/devices.`` If a device 15362306a36Sopenharmony_ci directory does not contain directories for child devices, these links 15462306a36Sopenharmony_ci may be used to find the child devices in ``/sys/class.`` That is the single 15562306a36Sopenharmony_ci valid use of these links; they must never appear in any path as an 15662306a36Sopenharmony_ci element. Assuming the existence of these links for devices which are 15762306a36Sopenharmony_ci real child device directories in the ``/sys/devices`` tree is a bug in 15862306a36Sopenharmony_ci the application. 15962306a36Sopenharmony_ci 16062306a36Sopenharmony_ci It is planned to remove all these links when all class device 16162306a36Sopenharmony_ci directories live in ``/sys/devices.`` 16262306a36Sopenharmony_ci 16362306a36Sopenharmony_ci- Position of devices along device chain can change. 16462306a36Sopenharmony_ci Never depend on a specific parent device position in the devpath, 16562306a36Sopenharmony_ci or the chain of parent devices. The kernel is free to insert devices into 16662306a36Sopenharmony_ci the chain. You must always request the parent device you are looking for 16762306a36Sopenharmony_ci by its subsystem value. You need to walk up the chain until you find 16862306a36Sopenharmony_ci the device that matches the expected subsystem. Depending on a specific 16962306a36Sopenharmony_ci position of a parent device or exposing relative paths using ``../`` to 17062306a36Sopenharmony_ci access the chain of parents is a bug in the application. 17162306a36Sopenharmony_ci 17262306a36Sopenharmony_ci- When reading and writing sysfs device attribute files, avoid dependency 17362306a36Sopenharmony_ci on specific error codes wherever possible. This minimizes coupling to 17462306a36Sopenharmony_ci the error handling implementation within the kernel. 17562306a36Sopenharmony_ci 17662306a36Sopenharmony_ci In general, failures to read or write sysfs device attributes shall 17762306a36Sopenharmony_ci propagate errors wherever possible. Common errors include, but are not 17862306a36Sopenharmony_ci limited to: 17962306a36Sopenharmony_ci 18062306a36Sopenharmony_ci ``-EIO``: The read or store operation is not supported, typically 18162306a36Sopenharmony_ci returned by the sysfs system itself if the read or store pointer 18262306a36Sopenharmony_ci is ``NULL``. 18362306a36Sopenharmony_ci 18462306a36Sopenharmony_ci ``-ENXIO``: The read or store operation failed 18562306a36Sopenharmony_ci 18662306a36Sopenharmony_ci Error codes will not be changed without good reason, and should a change 18762306a36Sopenharmony_ci to error codes result in user-space breakage, it will be fixed, or the 18862306a36Sopenharmony_ci the offending change will be reverted. 18962306a36Sopenharmony_ci 19062306a36Sopenharmony_ci Userspace applications can, however, expect the format and contents of 19162306a36Sopenharmony_ci the attribute files to remain consistent in the absence of a version 19262306a36Sopenharmony_ci attribute change in the context of a given attribute. 193