如何从内核空间读取/写入 linux /proc 文件?
我正在编写一个由用户程序和内核模块组成的程序。内核模块需要收集数据,然后将其“发送”到用户程序。这必须通过 /proc 文件来完成。现在,我创建了文件,一切都很好,并且花了很长时间在互联网上寻找答案,但仍然找不到。如何从内核空间读取/写入 /proc 文件?提供给 procfile 的 write_proc 和 read_proc 用于从用户空间读取和写入数据,而我需要该模块能够写入 /proc 文件本身。
I am writing a program consisting of user program and a kernel module. The kernel module needs to gather data that it will then "send" to the user program. This has to be done via a /proc file. Now, I create the file, everything is fine, and spent ages reading the internet for answer and still cannot find one. How do you read/write a /proc file from the kernel space ? The write_proc and read_proc supplied to the procfile are used to read and write data from USER space, whereas I need the module to be able to write the /proc file itself.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
事情不是这样的。当用户空间程序打开这些文件时,它们是根据具体情况动态生成的。其中大多数是只读的,并由通用机制生成:
您(从内核)不会“写入”procfs 文件,而是在用户空间请求时动态提供结果。
That's not how it works. When a userspace program opens the files, they are generated on the fly on a case-by-case basis. Most of them are readonly and generated by a common mechanism:
You (from the kernel) do not "write" to procfs files, you supply the results dynamically when userspace requests them.
将数据传输到用户空间的方法之一是 seq_files。为了配置(写入)内核参数,您可能需要考虑 sys-fs 节点。
谢谢,
维杰
One of the approaches to get data across to the user space would be seq_files. In order to configure (write) kernel parameters you may want to consider sys-fs nodes.
Thanks,
Vijay