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

时间:2021-05-15 02:51:49

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.

我正在编写一个由用户程序和内核模块组成的程序。内核模块需要收集数据然后“发送”到用户程序。这必须通过/ proc文件完成。现在,我创建文件,一切都很好,并花了很长时间阅读互联网的答案,但仍然找不到。你如何从内核空间读/写一个/ proc文件?提供给procfile的write_proc和read_proc用于从USER空间读取和写入数据,而我需要该模块能够自己编写/ proc文件。

2 个解决方案

#1


9  

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
  • 使用create_proc_read_entry注册一个条目

  • Supply a callback function (called read_proc by convention) which is called when the file is read
  • 提供一个回调函数(按约定称为read_proc),在读取文件时调用该函数

  • This callback function should populate a supplied buffer and (typically) call proc_calc_metrics to update the file pointer etc supplied to userspace.
  • 此回调函数应填充提供的缓冲区,并(通常)调用proc_calc_metrics以更新提供给用户空间的文件指针等。

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

您(从内核)不“写”到procfs文件,您在用户空间请求时动态提供结果。

#2


0  

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.

将数据传递到用户空间的方法之一是seq_files。为了配置(写入)内核参数,您可能需要考虑sys-fs节点。

Thanks, Vijay

#1


9  

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
  • 使用create_proc_read_entry注册一个条目

  • Supply a callback function (called read_proc by convention) which is called when the file is read
  • 提供一个回调函数(按约定称为read_proc),在读取文件时调用该函数

  • This callback function should populate a supplied buffer and (typically) call proc_calc_metrics to update the file pointer etc supplied to userspace.
  • 此回调函数应填充提供的缓冲区,并(通常)调用proc_calc_metrics以更新提供给用户空间的文件指针等。

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

您(从内核)不“写”到procfs文件,您在用户空间请求时动态提供结果。

#2


0  

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.

将数据传递到用户空间的方法之一是seq_files。为了配置(写入)内核参数,您可能需要考虑sys-fs节点。

Thanks, Vijay