Pages

Creating a read write proc entry in kernel versions above 3.10

In the post Creating read only proc entry in kernel versions above 3.10 we saw how to create a read proc entry in the kernel version 3.10 and above. In continuation to the same let us see how we can create a proc entry into which we can write data.

We can create a proc entry using the function



Where:

name: The name of the proc entry
mode: The access mode for proc entry
parent: The name of the parent directory under /proc
proc_fops: The structure in which the file operations for the proc entry will be created.

For example to create a proc entry by the name "hello" under /proc the above function will be defined are

For example to create a proc entry by the name "hello_write" under /proc the above function will be defined are



Now we need to create file_operations structure proc_fops in which we can map the read and write functions for the proc entry.



Next we need to add the functions read_proc and write_proc which will give write data to the proc entry and then read data from the proc entry.

The write function will recieve data from the user space using the function copy_from_user into a char pointer "msg".



Thus the write function will look as below.



Once data is written to the proc entry we can read from the proc entry using a read function, i.e tranfer data to the user space using the function copy_to_user function.

The read function can be as below.



The creation of proc entry and the intialization of the msg pointer can be done in a function create_new_proc_entry.



The create_new_proc_entry will be called in the init function.



The proc entry that was created will be removed in the exit function using remove_proc_entry.



Thus the full code for creating a write proc entry looks as below.

proc_rw.c



The makefile for the compilation of the modules are



Compile and load the module using



To test the proc entry let us first write data into the entry

Now we can read the data back



We can see that the message that we wrote into the proc entry, the same data is being displayed when it is read. The data will remain as long as it is not overwritten by new data.

Related Posts:

Creating read only proc entry in kernel versions above 3.10.

For kernel version before 3.10

Creating a proc entry - 1

Creating a proc read entry



1 comment:

  1. msg=kmalloc(GFP_KERNEL,10*sizeof(char));

    usage of kmalloc is wrong!
    Definition is - void * kmalloc (size_t size, gfp_t flags);


    ReplyDelete