How to read / write from / to a linux / proc file from kernel space?

I am writing a program consisting of a user program and a kernel module. The kernel module needs to collect data, which it then “sends” to the user program. This must be done through the / proc file. Now I create a file, everything is in order, and spent centuries reading the Internet for an answer and still can not find it. How do you read / write the / proc file from kernel space? Write_proc and read_proc provided in procfile are used to read and write data from the USER space, while I need a module to write the / proc file itself.

+5
source share
2 answers

This is not how it works. When a user-space program opens files, they are generated on the fly in each case. Most of them are readonly and are generated by a common mechanism:

  • Register a record using create_proc_read_entry
  • Put a callback function (called read_proc by convention) that is called when the file is read.
  • This callback function should fill in the supplied buffer and (usually) call proc_calc_metrics to update the file pointer, etc., delivered to user space.

You (from the kernel) do not "write" procfs files, you dynamically send results when user space requests them.

+9
source

seq_files. () sys-fs.

,

0

All Articles