Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 591
Petar Avatar asked Dec 09 '10 15:12

Petar


People also ask

Where do you find the code to read from a file in the kernel?

The source code is stored in a file named mainc. c within the /init directory. The code initializes the kernel and some initial processes.

What is Create_proc_entry?

create_proc_entry() Both of these functions are defined in the file linux/proc_fs. h. The create_proc_entry is a generic function that allows to create both read as well as write entries. create_proc_read_entry is a function specific to create only read entries.


1 Answers

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.

like image 170
MarkR Avatar answered Oct 01 '22 12:10

MarkR