sysfs-rules.txt 8.2 KB

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