sysfs-rules.rst 9.4 KB

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