diff options
| author | Kay Sievers <kay.sievers@vrfy.org> | 2007-06-08 16:36:37 -0400 |
|---|---|---|
| committer | Greg Kroah-Hartman <gregkh@suse.de> | 2007-07-11 19:09:00 -0400 |
| commit | 46336009b5009e9fab3bd623a3beb9c7421545ac (patch) | |
| tree | afb86b1d6a0c774b6e8a911e018f8743d9504d24 | |
| parent | 4eb6bf6bfb580afaf1e1a1d30cba17a078530cf4 (diff) | |
Rules on how to use sysfs in userspace programs
Here's a document to help clear things up.
Signed-off-by: Kay Sievers <kay.sievers@vrfy.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
| -rw-r--r-- | Documentation/sysfs-rules.txt | 166 |
1 files changed, 166 insertions, 0 deletions
diff --git a/Documentation/sysfs-rules.txt b/Documentation/sysfs-rules.txt new file mode 100644 index 000000000000..42861bb0bc9b --- /dev/null +++ b/Documentation/sysfs-rules.txt | |||
| @@ -0,0 +1,166 @@ | |||
| 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. As sysfs is a direct export of kernel internal | ||
| 7 | structures, the sysfs interface can not provide a stable interface eighter, | ||
| 8 | it may always change along with internal kernel changes. | ||
| 9 | |||
| 10 | To minimize the risk of breaking users of sysfs, which are in most cases | ||
| 11 | low-level userspace applications, with a new kernel release, the users | ||
| 12 | of sysfs must follow some rules to use an as abstract-as-possible way to | ||
| 13 | access this filesystem. The current udev and HAL programs already | ||
| 14 | implement this and users are encouraged to plug, if possible, into the | ||
| 15 | abstractions these programs provide instead of accessing sysfs | ||
| 16 | directly. | ||
| 17 | |||
| 18 | But if you really do want or need to access sysfs directly, please follow | ||
| 19 | the following rules and then your programs should work with future | ||
| 20 | versions of the sysfs interface. | ||
| 21 | |||
| 22 | - Do not use libsysfs | ||
| 23 | It makes assumptions about sysfs which are not true. Its API does not | ||
| 24 | offer any abstraction, it exposes all the kernel driver-core | ||
| 25 | implementation details in its own API. Therefore it is not better than | ||
| 26 | reading directories and opening the files yourself. | ||
| 27 | Also, it is not actively maintained, in the sense of reflecting the | ||
| 28 | current kernel-development. The goal of providing a stable interface | ||
| 29 | to sysfs has failed, it causes more problems, than it solves. It | ||
| 30 | violates many of the rules in this document. | ||
| 31 | |||
| 32 | - sysfs is always at /sys | ||
| 33 | Parsing /proc/mounts is a waste of time. Other mount points are a | ||
| 34 | system configuration bug you should not try to solve. For test cases, | ||
| 35 | possibly support a SYSFS_PATH environment variable to overwrite the | ||
| 36 | applications behavior, but never try to search for sysfs. Never try | ||
| 37 | to mount it, if you are not an early boot script. | ||
| 38 | |||
| 39 | - devices are only "devices" | ||
| 40 | There is no such thing like class-, bus-, physical devices, | ||
| 41 | interfaces, and such that you can rely on in userspace. Everything is | ||
| 42 | just simply a "device". Class-, bus-, physical, ... types are just | ||
| 43 | kernel implementation details, which should not be expected by | ||
| 44 | applications that look for devices in sysfs. | ||
| 45 | |||
| 46 | The properties of a device are: | ||
| 47 | o devpath (/devices/pci0000:00/0000:00:1d.1/usb2/2-2/2-2:1.0) | ||
| 48 | - identical to the DEVPATH value in the event sent from the kernel | ||
| 49 | at device creation and removal | ||
| 50 | - the unique key to the device at that point in time | ||
| 51 | - the kernels path to the device-directory without the leading | ||
| 52 | /sys, and always starting with with a slash | ||
| 53 | - all elements of a devpath must be real directories. Symlinks | ||
| 54 | pointing to /sys/devices must always be resolved to their real | ||
| 55 | target, and the target path must be used to access the device. | ||
| 56 | That way the devpath to the device matches the devpath of the | ||
| 57 | kernel used at event time. | ||
| 58 | - using or exposing symlink values as elements in a devpath string | ||
| 59 | is a bug in the application | ||
| 60 | |||
| 61 | o kernel name (sda, tty, 0000:00:1f.2, ...) | ||
| 62 | - a directory name, identical to the last element of the devpath | ||
| 63 | - applications need to handle spaces and characters like '!' in | ||
| 64 | the name | ||
| 65 | |||
| 66 | o subsystem (block, tty, pci, ...) | ||
| 67 | - simple string, never a path or a link | ||
| 68 | - retrieved by reading the "subsystem"-link and using only the | ||
| 69 | last element of the target path | ||
| 70 | |||
| 71 | o driver (tg3, ata_piix, uhci_hcd) | ||
| 72 | - a simple string, which may contain spaces, never a path or a | ||
| 73 | link | ||
| 74 | - it is retrieved by reading the "driver"-link and using only the | ||
| 75 | last element of the target path | ||
| 76 | - devices which do not have "driver"-link, just do not have a | ||
| 77 | driver; copying the driver value in a child device context, is a | ||
| 78 | bug in the application | ||
| 79 | |||
| 80 | o attributes | ||
| 81 | - the files in the device directory or files below a subdirectories | ||
| 82 | of the same device directory | ||
| 83 | - accessing attributes reached by a symlink pointing to another device, | ||
| 84 | like the "device"-link, is a bug in the application | ||
| 85 | |||
| 86 | Everything else is just a kernel driver-core implementation detail, | ||
| 87 | that should not be assumed to be stable across kernel releases. | ||
| 88 | |||
| 89 | - Properties of parent devices never belong into a child device. | ||
| 90 | Always look at the parent devices themselves for determining device | ||
| 91 | context properties. If the device 'eth0' or 'sda' does not have a | ||
| 92 | "driver"-link, then this device does not have a driver. Its value is empty. | ||
| 93 | Never copy any property of the parent-device into a child-device. Parent | ||
| 94 | device-properties may change dynamically without any notice to the | ||
| 95 | child device. | ||
| 96 | |||
| 97 | - Hierarchy in a single device-tree | ||
| 98 | There is only one valid place in sysfs where hierarchy can be examined | ||
| 99 | and this is below: /sys/devices. | ||
| 100 | It is planned, that all device directories will end up in the tree | ||
| 101 | below this directory. | ||
| 102 | |||
| 103 | - Classification by subsystem | ||
| 104 | There are currently three places for classification of devices: | ||
| 105 | /sys/block, /sys/class and /sys/bus. It is planned that these will | ||
| 106 | not contain any device-directories themselves, but only flat lists of | ||
| 107 | symlinks pointing to the unified /sys/devices tree. | ||
| 108 | All three places have completely different rules on how to access | ||
| 109 | device information. It is planned to merge all three | ||
| 110 | classification-directories into one place at /sys/subsystem, | ||
| 111 | following the layout of the bus-directories. All buses and | ||
| 112 | classes, including the converted block-subsystem, will show up | ||
| 113 | there. | ||
| 114 | The devices belonging to a subsystem will create a symlink in the | ||
| 115 | "devices" directory at /sys/subsystem/<name>/devices. | ||
| 116 | |||
| 117 | If /sys/subsystem exists, /sys/bus, /sys/class and /sys/block can be | ||
| 118 | ignored. If it does not exist, you have always to scan all three | ||
| 119 | places, as the kernel is free to move a subsystem from one place to | ||
| 120 | the other, as long as the devices are still reachable by the same | ||
| 121 | subsystem name. | ||
| 122 | |||
| 123 | Assuming /sys/class/<subsystem> and /sys/bus/<subsystem>, or | ||
| 124 | /sys/block and /sys/class/block are not interchangeable, is a bug in | ||
| 125 | the application. | ||
| 126 | |||
| 127 | - Block | ||
| 128 | The converted block-subsystem at /sys/class/block, or | ||
| 129 | /sys/subsystem/block will contain the links for disks and partitions | ||
| 130 | at the same level, never in a hierarchy. Assuming the block-subsytem to | ||
| 131 | contain only disks and not partition-devices in the same flat list is | ||
| 132 | a bug in the application. | ||
| 133 | |||
| 134 | - "device"-link and <subsystem>:<kernel name>-links | ||
| 135 | Never depend on the "device"-link. The "device"-link is a workaround | ||
| 136 | for the old layout, where class-devices are not created in | ||
| 137 | /sys/devices/ like the bus-devices. If the link-resolving of a | ||
| 138 | device-directory does not end in /sys/devices/, you can use the | ||
| 139 | "device"-link to find the parent devices in /sys/devices/. That is the | ||
| 140 | single valid use of the "device"-link, it must never appear in any | ||
| 141 | path as an element. Assuming the existence of the "device"-link for | ||
| 142 | a device in /sys/devices/ is a bug in the application. | ||
| 143 | Accessing /sys/class/net/eth0/device is a bug in the application. | ||
| 144 | |||
| 145 | Never depend on the class-specific links back to the /sys/class | ||
| 146 | directory. These links are also a workaround for the design mistake | ||
| 147 | that class-devices are not created in /sys/devices. If a device | ||
| 148 | directory does not contain directories for child devices, these links | ||
| 149 | may be used to find the child devices in /sys/class. That is the single | ||
| 150 | valid use of these links, they must never appear in any path as an | ||
| 151 | element. Assuming the existence of these links for devices which are | ||
| 152 | real child device directories in the /sys/devices tree, is a bug in | ||
| 153 | the application. | ||
| 154 | |||
| 155 | It is planned to remove all these links when when all class-device | ||
| 156 | directories live in /sys/devices. | ||
| 157 | |||
| 158 | - Position of devices along device chain can change. | ||
| 159 | Never depend on a specific parent device position in the devpath, | ||
| 160 | or the chain of parent devices. The kernel is free to insert devices into | ||
| 161 | the chain. You must always request the parent device you are looking for | ||
| 162 | by its subsystem value. You need to walk up the chain until you find | ||
| 163 | the device that matches the expected subsystem. Depending on a specific | ||
| 164 | position of a parent device, or exposing relative paths, using "../" to | ||
| 165 | access the chain of parents, is a bug in the application. | ||
| 166 | |||
