blob: ce60ffa94d2d709681ed339fc4ef25369a2c377d [file] [log] [blame]
Kay Sievers46336002007-06-08 13:36:37 -07001Rules on how to access information in the Linux kernel sysfs
2
Randy Dunlap30b1b282007-07-23 21:05:02 -07003The kernel-exported sysfs exports internal kernel implementation details
Kay Sievers46336002007-06-08 13:36:37 -07004and depends on internal kernel structures and layout. It is agreed upon
5by the kernel developers that the Linux kernel does not provide a stable
Nathan Lynch83c79b52008-07-02 10:21:30 -07006internal API. Therefore, there are aspects of the sysfs interface that
7may not be stable across kernel releases.
Kay Sievers46336002007-06-08 13:36:37 -07008
9To minimize the risk of breaking users of sysfs, which are in most cases
10low-level userspace applications, with a new kernel release, the users
Randy Dunlap30b1b282007-07-23 21:05:02 -070011of sysfs must follow some rules to use an as-abstract-as-possible way to
Kay Sievers46336002007-06-08 13:36:37 -070012access this filesystem. The current udev and HAL programs already
13implement this and users are encouraged to plug, if possible, into the
Randy Dunlap30b1b282007-07-23 21:05:02 -070014abstractions these programs provide instead of accessing sysfs directly.
Kay Sievers46336002007-06-08 13:36:37 -070015
16But if you really do want or need to access sysfs directly, please follow
17the following rules and then your programs should work with future
18versions 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
Randy Dunlap30b1b282007-07-23 21:05:02 -070026 current kernel development. The goal of providing a stable interface
27 to sysfs has failed; it causes more problems than it solves. It
Kay Sievers46336002007-06-08 13:36:37 -070028 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
Randy Dunlap30b1b282007-07-23 21:05:02 -070034 application's behavior, but never try to search for sysfs. Never try
Kay Sievers46336002007-06-08 13:36:37 -070035 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
Randy Dunlap30b1b282007-07-23 21:05:02 -070041 kernel implementation details which should not be expected by
Kay Sievers46336002007-06-08 13:36:37 -070042 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
Randy Dunlap30b1b282007-07-23 21:05:02 -070049 - the kernel's path to the device directory without the leading
Masanari Iidac9f3f2d2013-07-18 01:29:12 +090050 /sys, and always starting with a slash
Kay Sievers46336002007-06-08 13:36:37 -070051 - all elements of a devpath must be real directories. Symlinks
52 pointing to /sys/devices must always be resolved to their real
Randy Dunlap30b1b282007-07-23 21:05:02 -070053 target and the target path must be used to access the device.
Kay Sievers46336002007-06-08 13:36:37 -070054 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
Randy Dunlap30b1b282007-07-23 21:05:02 -070074 - 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
Kay Sievers46336002007-06-08 13:36:37 -070076 bug in the application
77
78 o attributes
Randy Dunlap30b1b282007-07-23 21:05:02 -070079 - the files in the device directory or files below subdirectories
Kay Sievers46336002007-06-08 13:36:37 -070080 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
Randy Dunlap30b1b282007-07-23 21:05:02 -070084 Everything else is just a kernel driver-core implementation detail
Kay Sievers46336002007-06-08 13:36:37 -070085 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
Randy Dunlap30b1b282007-07-23 21:05:02 -070092 device properties may change dynamically without any notice to the
Kay Sievers46336002007-06-08 13:36:37 -070093 child device.
94
Randy Dunlap30b1b282007-07-23 21:05:02 -070095- Hierarchy in a single device tree
Kay Sievers46336002007-06-08 13:36:37 -070096 There is only one valid place in sysfs where hierarchy can be examined
97 and this is below: /sys/devices.
Randy Dunlap30b1b282007-07-23 21:05:02 -070098 It is planned that all device directories will end up in the tree
Kay Sievers46336002007-06-08 13:36:37 -070099 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
Randy Dunlap30b1b282007-07-23 21:05:02 -0700104 not contain any device directories themselves, but only flat lists of
Kay Sievers46336002007-06-08 13:36:37 -0700105 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
Randy Dunlap30b1b282007-07-23 21:05:02 -0700108 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
Kay Sievers46336002007-06-08 13:36:37 -0700111 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
Henrik Austad441ee4c2009-04-20 18:42:38 -0700116 ignored. If it does not exist, you always have to scan all three
Kay Sievers46336002007-06-08 13:36:37 -0700117 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
Randy Dunlap30b1b282007-07-23 21:05:02 -0700122 /sys/block and /sys/class/block are not interchangeable is a bug in
Kay Sievers46336002007-06-08 13:36:37 -0700123 the application.
124
125- Block
Randy Dunlap30b1b282007-07-23 21:05:02 -0700126 The converted block subsystem at /sys/class/block or
Kay Sievers46336002007-06-08 13:36:37 -0700127 /sys/subsystem/block will contain the links for disks and partitions
Thomas Weber88393162010-03-16 11:47:56 +0100128 at the same level, never in a hierarchy. Assuming the block subsystem to
Randy Dunlap30b1b282007-07-23 21:05:02 -0700129 contain only disks and not partition devices in the same flat list is
Kay Sievers46336002007-06-08 13:36:37 -0700130 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
Randy Dunlap30b1b282007-07-23 21:05:02 -0700134 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
Kay Sievers46336002007-06-08 13:36:37 -0700137 "device"-link to find the parent devices in /sys/devices/. That is the
Randy Dunlap30b1b282007-07-23 21:05:02 -0700138 single valid use of the "device"-link; it must never appear in any
Kay Sievers46336002007-06-08 13:36:37 -0700139 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
Randy Dunlap30b1b282007-07-23 21:05:02 -0700145 that class devices are not created in /sys/devices. If a device
Kay Sievers46336002007-06-08 13:36:37 -0700146 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
Randy Dunlap30b1b282007-07-23 21:05:02 -0700148 valid use of these links; they must never appear in any path as an
Kay Sievers46336002007-06-08 13:36:37 -0700149 element. Assuming the existence of these links for devices which are
Randy Dunlap30b1b282007-07-23 21:05:02 -0700150 real child device directories in the /sys/devices tree is a bug in
Kay Sievers46336002007-06-08 13:36:37 -0700151 the application.
152
Randy Dunlap30b1b282007-07-23 21:05:02 -0700153 It is planned to remove all these links when all class device
Kay Sievers46336002007-06-08 13:36:37 -0700154 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
Randy Dunlap30b1b282007-07-23 21:05:02 -0700162 position of a parent device or exposing relative paths using "../" to
163 access the chain of parents is a bug in the application.
Darren Hart00e262f2014-09-16 14:56:37 -0700164
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.