In the post "creating proc read entry" we saw how to create a proc entry that can only be read and not written to.
The proc entries can also be created such that we can write data into the proc entry, the data in turn can be used for configuring modules in the kernel. For eg the default level of logging of the printk function call is defined by the value of CONSOLE_LOG_LEVEL. All the messages that have the priority more than this will be displayed on the console (While working in the text mode, not in the graphics mode ). The value of this can be found out by "cat /proc/sys/kernel/printk".
For eg the output may be
The first number in the output is the default CONSOLE_LOGL_EVEL, in this case all the printk messages which have loglevel higher than 4 (3,2,1) are displayed on the screen.
To change this loglevel we can just write the required log level into the proc entry are follows
This will change the CONSOLE_LOG_LEVEL from 6 to 7.
Thus we can see that creating write_proc entries can be very useful in configuring the functioning of the kernel as well while debugging a module.
In this post we will see how we can create a proc entry which can be read from as well as written to.
To create a write proc entry we will have to make use of the func tion "create_proc_entry"
Where the arguments are
name: The name of the entry that we want to create.
mode : The permissions for the entry created.
parent: In case the entry needs to be created in a sub directory under /proc, we will have to give the full path.
In our example we will use
Thus "hello" is the name of the entry, by passing 0666 we are giving read and write permission to every one,
And we are creating the entry straight under /proc hence the third argument is NULL.
This returns a pointer to a structure of the kind proc_dir_entry. You can look into the contents of the structure in the file linux/proc_fs.h
The structure has various fields and we need to initialize only the ones we feel is relevant for our module.
In our example we want to implement read and write functions of proc hence we will have to initialize the read_proc and write_proc in the structure to point to the respective read and write functions.
Thus the initializations will be
Thus we have to implement a function by the name read_proc that will be called when the proc entry is read and a function by the name write_proc that will be called when the proc entry is written to.
We will use an array by the name proc_data to store the data that will written into the proc entry. The read function thus will output the contents of the proc_data array whenever the proc entry is read as shown below.
The write function will have to written to accept data from the user and put it into the array proc_data as shown below.
Before writing into the proc entry we make sure that amount of write does not exceed the size of the array.
In case the amount of write from user space is more than the size of the array then we truncate the data to the maximum size array
The full code of the module is as below.
proc_write_read.c :
Makefile required to compile the above module is as below. Malefile:
To compile the module run the following commands as super user
If there are no errors run the following command
After inserting successfully, write some data into the proc entry as shown below.
Now read the proc entry and see if the same data written above is shown or not.
What ever you write into the proc etnry, you should be able to read it back using the cat command.
Reference Books
The proc entries can also be created such that we can write data into the proc entry, the data in turn can be used for configuring modules in the kernel. For eg the default level of logging of the printk function call is defined by the value of CONSOLE_LOG_LEVEL. All the messages that have the priority more than this will be displayed on the console (While working in the text mode, not in the graphics mode ). The value of this can be found out by "cat /proc/sys/kernel/printk".
For eg the output may be
The first number in the output is the default CONSOLE_LOGL_EVEL, in this case all the printk messages which have loglevel higher than 4 (3,2,1) are displayed on the screen.
To change this loglevel we can just write the required log level into the proc entry are follows
This will change the CONSOLE_LOG_LEVEL from 6 to 7.
Thus we can see that creating write_proc entries can be very useful in configuring the functioning of the kernel as well while debugging a module.
In this post we will see how we can create a proc entry which can be read from as well as written to.
To create a write proc entry we will have to make use of the func tion "create_proc_entry"
Where the arguments are
name: The name of the entry that we want to create.
mode : The permissions for the entry created.
parent: In case the entry needs to be created in a sub directory under /proc, we will have to give the full path.
In our example we will use
Thus "hello" is the name of the entry, by passing 0666 we are giving read and write permission to every one,
And we are creating the entry straight under /proc hence the third argument is NULL.
This returns a pointer to a structure of the kind proc_dir_entry. You can look into the contents of the structure in the file linux/proc_fs.h
The structure has various fields and we need to initialize only the ones we feel is relevant for our module.
In our example we want to implement read and write functions of proc hence we will have to initialize the read_proc and write_proc in the structure to point to the respective read and write functions.
Thus the initializations will be
Thus we have to implement a function by the name read_proc that will be called when the proc entry is read and a function by the name write_proc that will be called when the proc entry is written to.
We will use an array by the name proc_data to store the data that will written into the proc entry. The read function thus will output the contents of the proc_data array whenever the proc entry is read as shown below.
The write function will have to written to accept data from the user and put it into the array proc_data as shown below.
Before writing into the proc entry we make sure that amount of write does not exceed the size of the array.
In case the amount of write from user space is more than the size of the array then we truncate the data to the maximum size array
The full code of the module is as below.
proc_write_read.c :
Makefile required to compile the above module is as below. Malefile:
To compile the module run the following commands as super user
If there are no errors run the following command
After inserting successfully, write some data into the proc entry as shown below.
Now read the proc entry and see if the same data written above is shown or not.
What ever you write into the proc etnry, you should be able to read it back using the cat command.
Reference Books
Wonderful and rare information about proc's and linux module programming! thanks a lot! keep up the good work!
ReplyDeleteThanks, helpful stuff!
ReplyDeletegreat example!
ReplyDeleteOnly problem is that you don't clear the buffer between each calls, so a second call to the module with a shorter string will print the string and the left overs.
Thanks for the feedback, will look into it :-)
Deletei want to read /proc/meminfo from kernel module.please tell me how to read that file
ReplyDeleteHi
Deletethe code for /proc/meminfo is present in fs/proc/meminfo.c it is a seq entry.
You can look at the function meminfo_proc_show in meminfo.c and figure out what information you want to extract.
As far as I know, you can not read /proc/meminfo as a file from kernel, but you can access the same information that meminfo displays by looking into the above mentioned function.
Thanks for sharing This is a great post, I stumbled across your article while looking for some random stuff. Thanks for sharing, I’ll be sure to return regularly.
ReplyDeletebuy-college-essays
custom-paper-writing
Accounts Software For Small Business