linux - 使用 sysfs show() 和 store() 函数

标签 linux linux-kernel linux-device-driver sysfs

关于使用这些功能,我有 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读/写sysfs属性文件时,我是否保证我读到的缓冲区/store函数中的缓冲区将包含我想在该函数中读取的所有字节,而不是在几个 block 中调用它?

还有,空字符是怎么加的?也就是说,假设我写了n个字节,函数参数中写的字节数是n,null char会放在n+1处吗?

谢谢

最佳答案

第一个问题的答案是 - 不支持部分写入,缓冲区总是在调用 show 方法时填充。

第二个问题的答案也是肯定的。请参阅 sysfs 使用的实现 kernfs_fop_write() - 它将分配最多 PAGE_SIZE+1 字节,以便有足够的空间容纳\0。

关于linux - 使用 sysfs show() 和 store() 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53415524/

相关文章:

linux - 在 bash 脚本中从 grep 重新格式化名称/内容对

linux - sendfile() 在两个映射文件上的用法 (linux)

linux - 我怎么知道我是否可以使用 FMA 指令集进行编译?

从 stdin 进行子采样的 Linux 命令

python - SQLAlchemy、FreeTDS 和 MSSQL DBAPI 错误 : Conversion between 47 and -1 datatypes is not supported

gcc - 汇编代码 `movl %1, %%ebx`是什么意思?

linux - "struct file_operations"参数是什么?

c - 如何使用模块获取进程ID、名称和状态

linux-kernel - 一个中断处理程序可以被同一个中断处理程序抢占吗?

Linux内核和realtek rtl8139驱动