How to read/write from/to a linux /proc file from

2019-04-08 13:54发布

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.

2条回答
祖国的老花朵
2楼-- · 2019-04-08 14:11

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.

查看更多
仙女界的扛把子
3楼-- · 2019-04-08 14:23

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

查看更多
登录 后发表回答