Based on kernel version 4.3. Page generated on 2015-11-02 12:51 EST.
1 Rules on how to access information in the Linux kernel sysfs 2 3 The kernel-exported sysfs exports internal kernel implementation details 4 and depends on internal kernel structures and layout. It is agreed upon 5 by the kernel developers that the Linux kernel does not provide a stable 6 internal API. Therefore, there are aspects of the sysfs interface that 7 may not be stable across kernel releases. 8 9 To minimize the risk of breaking users of sysfs, which are in most cases 10 low-level userspace applications, with a new kernel release, the users 11 of sysfs must follow some rules to use an as-abstract-as-possible way to 12 access this filesystem. The current udev and HAL programs already 13 implement this and users are encouraged to plug, if possible, into the 14 abstractions these programs provide instead of accessing sysfs directly. 15 16 But if you really do want or need to access sysfs directly, please follow 17 the following rules and then your programs should work with future 18 versions of the sysfs interface. 19 20 - Do not use libsysfs 21 It makes assumptions about sysfs which are not true. Its API does not 22 offer any abstraction, it exposes all the kernel driver-core 23 implementation details in its own API. Therefore it is not better than 24 reading directories and opening the files yourself. 25 Also, it is not actively maintained, in the sense of reflecting the 26 current kernel development. The goal of providing a stable interface 27 to sysfs has failed; it causes more problems than it solves. It 28 violates many of the rules in this document. 29 30 - sysfs is always at /sys 31 Parsing /proc/mounts is a waste of time. Other mount points are a 32 system configuration bug you should not try to solve. For test cases, 33 possibly support a SYSFS_PATH environment variable to overwrite the 34 application's behavior, but never try to search for sysfs. Never try 35 to mount it, if you are not an early boot script. 36 37 - devices are only "devices" 38 There is no such thing like class-, bus-, physical devices, 39 interfaces, and such that you can rely on in userspace. Everything is 40 just simply a "device". Class-, bus-, physical, ... types are just 41 kernel implementation details which should not be expected by 42 applications that look for devices in sysfs. 43 44 The properties of a device are: 45 o devpath (/devices/pci0000:00/0000:00:1d.1/usb2/2-2/2-2:1.0) 46 - identical to the DEVPATH value in the event sent from the kernel 47 at device creation and removal 48 - the unique key to the device at that point in time 49 - the kernel's path to the device directory without the leading 50 /sys, and always starting with a slash 51 - all elements of a devpath must be real directories. Symlinks 52 pointing to /sys/devices must always be resolved to their real 53 target and the target path must be used to access the device. 54 That way the devpath to the device matches the devpath of the 55 kernel used at event time. 56 - using or exposing symlink values as elements in a devpath string 57 is a bug in the application 58 59 o kernel name (sda, tty, 0000:00:1f.2, ...) 60 - a directory name, identical to the last element of the devpath 61 - applications need to handle spaces and characters like '!' in 62 the name 63 64 o subsystem (block, tty, pci, ...) 65 - simple string, never a path or a link 66 - retrieved by reading the "subsystem"-link and using only the 67 last element of the target path 68 69 o driver (tg3, ata_piix, uhci_hcd) 70 - a simple string, which may contain spaces, never a path or a 71 link 72 - it is retrieved by reading the "driver"-link and using only the 73 last element of the target path 74 - devices which do not have "driver"-link just do not have a 75 driver; copying the driver value in a child device context is a 76 bug in the application 77 78 o attributes 79 - the files in the device directory or files below subdirectories 80 of the same device directory 81 - accessing attributes reached by a symlink pointing to another device, 82 like the "device"-link, is a bug in the application 83 84 Everything else is just a kernel driver-core implementation detail 85 that should not be assumed to be stable across kernel releases. 86 87 - Properties of parent devices never belong into a child device. 88 Always look at the parent devices themselves for determining device 89 context properties. If the device 'eth0' or 'sda' does not have a 90 "driver"-link, then this device does not have a driver. Its value is empty. 91 Never copy any property of the parent-device into a child-device. Parent 92 device properties may change dynamically without any notice to the 93 child device. 94 95 - Hierarchy in a single device tree 96 There is only one valid place in sysfs where hierarchy can be examined 97 and this is below: /sys/devices. 98 It is planned that all device directories will end up in the tree 99 below this directory. 100 101 - Classification by subsystem 102 There are currently three places for classification of devices: 103 /sys/block, /sys/class and /sys/bus. It is planned that these will 104 not contain any device directories themselves, but only flat lists of 105 symlinks pointing to the unified /sys/devices tree. 106 All three places have completely different rules on how to access 107 device information. It is planned to merge all three 108 classification directories into one place at /sys/subsystem, 109 following the layout of the bus directories. All buses and 110 classes, including the converted block subsystem, will show up 111 there. 112 The devices belonging to a subsystem will create a symlink in the 113 "devices" directory at /sys/subsystem/<name>/devices. 114 115 If /sys/subsystem exists, /sys/bus, /sys/class and /sys/block can be 116 ignored. If it does not exist, you always have to scan all three 117 places, as the kernel is free to move a subsystem from one place to 118 the other, as long as the devices are still reachable by the same 119 subsystem name. 120 121 Assuming /sys/class/<subsystem> and /sys/bus/<subsystem>, or 122 /sys/block and /sys/class/block are not interchangeable is a bug in 123 the application. 124 125 - Block 126 The converted block subsystem at /sys/class/block or 127 /sys/subsystem/block will contain the links for disks and partitions 128 at the same level, never in a hierarchy. Assuming the block subsystem to 129 contain only disks and not partition devices in the same flat list is 130 a bug in the application. 131 132 - "device"-link and <subsystem>:<kernel name>-links 133 Never depend on the "device"-link. The "device"-link is a workaround 134 for the old layout, where class devices are not created in 135 /sys/devices/ like the bus devices. If the link-resolving of a 136 device directory does not end in /sys/devices/, you can use the 137 "device"-link to find the parent devices in /sys/devices/. That is the 138 single valid use of the "device"-link; it must never appear in any 139 path as an element. Assuming the existence of the "device"-link for 140 a device in /sys/devices/ is a bug in the application. 141 Accessing /sys/class/net/eth0/device is a bug in the application. 142 143 Never depend on the class-specific links back to the /sys/class 144 directory. These links are also a workaround for the design mistake 145 that class devices are not created in /sys/devices. If a device 146 directory does not contain directories for child devices, these links 147 may be used to find the child devices in /sys/class. That is the single 148 valid use of these links; they must never appear in any path as an 149 element. Assuming the existence of these links for devices which are 150 real child device directories in the /sys/devices tree is a bug in 151 the application. 152 153 It is planned to remove all these links when all class device 154 directories live in /sys/devices. 155 156 - Position of devices along device chain can change. 157 Never depend on a specific parent device position in the devpath, 158 or the chain of parent devices. The kernel is free to insert devices into 159 the chain. You must always request the parent device you are looking for 160 by its subsystem value. You need to walk up the chain until you find 161 the device that matches the expected subsystem. Depending on a specific 162 position of a parent device or exposing relative paths using "../" to 163 access the chain of parents is a bug in the application. 164 165 - When reading and writing sysfs device attribute files, avoid dependency 166 on specific error codes wherever possible. This minimizes coupling to 167 the error handling implementation within the kernel. 168 169 In general, failures to read or write sysfs device attributes shall 170 propagate errors wherever possible. Common errors include, but are not 171 limited to: 172 173 -EIO: The read or store operation is not supported, typically returned by 174 the sysfs system itself if the read or store pointer is NULL. 175 176 -ENXIO: The read or store operation failed 177 178 Error codes will not be changed without good reason, and should a change 179 to error codes result in user-space breakage, it will be fixed, or the 180 the offending change will be reverted. 181 182 Userspace applications can, however, expect the format and contents of 183 the attribute files to remain consistent in the absence of a version 184 attribute change in the context of a given attribute.