使用 sysfs show() 和 store() 函数
using sysfs show() and store() functions
关于使用这些功能,我有 2 个问题。我不完全理解 here:
写的文档
sysfs allocates a buffer of size (PAGE_SIZE) and passes it to the
method. Sysfs will call the method exactly once for each read or
write. This forces the following behavior on the method
implementations:
On read(2), the show() method should fill the entire buffer.
Recall that an attribute should only be exporting one value, or an
array of similar values, so this shouldn't be that expensive.
This allows userspace to do partial reads and forward seeks
arbitrarily over the entire file at will. If userspace seeks back to
zero or does a pread(2) with an offset of '0' the show() method will
be called again, rearmed, to fill the buffer.
On write(2), sysfs expects the entire buffer to be passed during the
first write. Sysfs then passes the entire buffer to the store() method.
A terminating null is added after the data on stores. This makes
functions like sysfs_streq() safe to use.
When writing sysfs files, userspace processes should first read the
entire file, modify the values it wishes to change, then write the
entire buffer back.
首先,当我read/write到带有read/write
的sysfs属性文件时,我是否保证我读取到的缓冲区/store
函数中的缓冲区将具有所有我想在该函数中读取的字节数,而不是分几个块调用它?
还有,空字符是怎么加的?也就是说,假设我写了n个字节,函数参数中写的字节数是n,null char会放在n+1处吗?
谢谢
第一个问题的答案是 - 不支持部分写入,缓冲区总是在调用 show 方法时填充。
第二个问题的答案也是肯定的。请参阅 sysfs 使用的实现 kernfs_fop_write()
- 它将分配最多 PAGE_SIZE+1 个字节,以便有足够的 space 来容纳 \0.
它分配 PAGE_SIZE
,如果写入了 >= PAGE_SIZE
,它会截断为 PAGE_SIZE
和 '[=13=]'
并记录错误。如果写入了 > PAGE_SIZE
,您可能损坏了内核内存。
为了保护,您应该使用 sysfs_emit
和 sysfs_emit_at
写入缓冲区。见 documentation.
关于使用这些功能,我有 2 个问题。我不完全理解 here:
写的文档sysfs allocates a buffer of size (PAGE_SIZE) and passes it to the method. Sysfs will call the method exactly once for each read or write. This forces the following behavior on the method implementations:
On read(2), the show() method should fill the entire buffer. Recall that an attribute should only be exporting one value, or an array of similar values, so this shouldn't be that expensive.
This allows userspace to do partial reads and forward seeks arbitrarily over the entire file at will. If userspace seeks back to zero or does a pread(2) with an offset of '0' the show() method will be called again, rearmed, to fill the buffer.
On write(2), sysfs expects the entire buffer to be passed during the first write. Sysfs then passes the entire buffer to the store() method. A terminating null is added after the data on stores. This makes functions like sysfs_streq() safe to use.
When writing sysfs files, userspace processes should first read the entire file, modify the values it wishes to change, then write the entire buffer back.
首先,当我read/write到带有read/write
的sysfs属性文件时,我是否保证我读取到的缓冲区/store
函数中的缓冲区将具有所有我想在该函数中读取的字节数,而不是分几个块调用它?
还有,空字符是怎么加的?也就是说,假设我写了n个字节,函数参数中写的字节数是n,null char会放在n+1处吗?
谢谢
第一个问题的答案是 - 不支持部分写入,缓冲区总是在调用 show 方法时填充。
第二个问题的答案也是肯定的。请参阅 sysfs 使用的实现 kernfs_fop_write()
- 它将分配最多 PAGE_SIZE+1 个字节,以便有足够的 space 来容纳 \0.
它分配 PAGE_SIZE
,如果写入了 >= PAGE_SIZE
,它会截断为 PAGE_SIZE
和 '[=13=]'
并记录错误。如果写入了 > PAGE_SIZE
,您可能损坏了内核内存。
为了保护,您应该使用 sysfs_emit
和 sysfs_emit_at
写入缓冲区。见 documentation.