PageRenderTime 32ms CodeModel.GetById 24ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

Plain Text | 163 lines | 141 code | 22 blank | 0 comment | 0 complexity | 31df4193a1aadb8e5bd263a72329bd35 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, GPL-2.0, LGPL-2.0, AGPL-1.0
  1Rules on how to access information in the Linux kernel sysfs
  3The kernel-exported sysfs exports internal kernel implementation details
  4and depends on internal kernel structures and layout. It is agreed upon
  5by the kernel developers that the Linux kernel does not provide a stable
  6internal API. Therefore, there are aspects of the sysfs interface that
  7may not be stable across kernel releases.
  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
 11of sysfs must follow some rules to use an as-abstract-as-possible way to
 12access this filesystem. The current udev and HAL programs already
 13implement this and users are encouraged to plug, if possible, into the
 14abstractions these programs provide instead of accessing sysfs directly.
 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.
 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.
 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.
 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.
 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 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
 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
 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
 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
 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
 84  Everything else is just a kernel driver-core implementation detail
 85  that should not be assumed to be stable across kernel releases.
 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.
 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.
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.
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.
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.
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 subsytem to
129  contain only disks and not partition devices in the same flat list is
130  a bug in the application.
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.
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.
153  It is planned to remove all these links when all class device
154  directories live in /sys/devices.
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.