如何从内核空间读取/写入 linux /proc 文件?

发布于 2024-10-07 11:07:44 字数 212 浏览 0 评论 0原文

我正在编写一个由用户程序和内核模块组成的程序。内核模块需要收集数据,然后将其“发送”到用户程序。这必须通过 /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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜清冷一曲。 2024-10-14 11:07:44

事情不是这样的。当用户空间程序打开这些文件时,它们是根据具体情况动态生成的。其中大多数是只读的,并由通用机制生成:

  • 使用 create_proc_read_entry 注册一个条目
  • 提供一个回调函数(按照惯例称为 read_proc),该函数在读取文件时调用
  • 该回调函数应该填充提供的缓冲区并(通常)调用 proc_calc_metrics更新提供给用户空间的文件指针等。

您(从内核)不会“写入”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:

  • Register an entry with create_proc_read_entry
  • Supply a callback function (called read_proc by convention) which is called when the file is read
  • This callback function should populate a supplied buffer and (typically) call proc_calc_metrics to update the file pointer etc supplied to userspace.

You (from the kernel) do not "write" to procfs files, you supply the results dynamically when userspace requests them.

短叹 2024-10-14 11:07:44

将数据传输到用户空间的方法之一是 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文