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



error: no such partition, grub rescue

If we come across the error in grub



It indicates that the partition on which grub was installed is no longer accessible. Which could be because it has been formatted or disconnected etc. There are a few ways of getting out of this situation.

If there is another partition on which we have a working OS we can boot into that OS using the following steps



The command in ls lists the partitions that grub is able to access.In the above example there are 3 partitions 1,2 and 4. If we know which among these has the working OS installed in it we can use the following set of commands to boot into it. Assume that (hd0,2) has the working OS.



Once we have booted into the working OS, if the another OS happens to be linux we can reinstall grub so that on next reboot the grub does not hang. To reinstall we need to find the partition on which grub needs to be installed which can be done using the command



The above output indicates that the hard disk on which operating system is installed is /dev/sda thus we can install grub using the command



If we do not have a working OS in any other partition then we need reinstall the grub using a live cd or a dvd or a live usb. Insert the live cd/dvd and boot the system. Once the live cd boots open a terminal and run the command



From the output look at the last column and the try to find the parition on which the os is installed. For example in the above case it is /dev/sda3.

Now execute the commands



The error should no longer appear.

Useful Links

http://www.gnu.org/software/grub/manual/html_node/GRUB-only-offers-a-rescue-shell.html#GRUB-only-offers-a-rescue-shell
http://www.gnu.org/software/grub/manual/html_node/Installing-GRUB-using-grub_002dinstall.html
http://howtoubuntu.org/how-to-repair-restore-reinstall-grub-2-with-a-ubuntu-live-cd#.UmgPBuHWSBs

For windows users

http://www.sevenforums.com/tutorials/20864-mbr-restore-windows-7-master-boot-record.html

exception emask 0x0 sact 0x0 serr 0x0 action 0x0

The following error might occur while the system boots.



To get around this we will need a live cd or a bootable usb. We can create a bootable usb using unetbootin. After booting using the live cd or the bootable usb, open a terminal and run the command



This will list all the disks available in the system. Choose the on in which linux is installed, assume it to be /dev/sda1. Now run the command



Answer yes for any repair related uestions thrown during the system check.

Once the check is over, reboot the system and try booting back into the ususal system. The error should no longer appear .


Creation of proc entry using proc_create_data in kernel version 3.10 and above.

In the post "Creating read only proc entry in kernel versions above 3.10. " we saw how to create a proc entry using the function proc_create.

Note: The following module is valid only of kernel version 3.10 and above.

Another function available in kernel version 3.10 and above for creation of proc entry is proc_create_data which is defined in proc_fs.hs as



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.
data: If any data needs to be passed to the proc entry.

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



Along with creation of the entry we are also passing a function to the proc entry in using the porinter "msg".

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



Next we need to add the function read_proc which will give to the user space the data that we want to export from the kernel space.



To access the data in the proc_dir_structure we need to make use of the function PDE_DATA to which we pass the file pointer. The function in turn returs a pointer to the data that was passed during the creation of the proc entry.

The message that we want to display will be defined in the function create_new_proc_entry in which we will also call the fuction for creation of proc entry.



The init and clean up functions for the module are



The full code for creation of proc entry using proc_create_data is

proc_read_data.c



The makefile for the compilation of the modules are



Compile and load the module using



We can see the output by using the cat command

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


Creating read only proc entry in kernel versions above 3.10.

The creation of proc entries has undergone a considerable change in kernel version 3.10 and above. In this post we will see one of the methods we can use in linux kernel version 3.10 and above let us see how we can create proc entries in version 3.10 and above.

The function is defined in proc_fs.h as



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



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



Next we need to add the function read_proc which will give to the user space the data that we want to export from the kernel space.



The message that we want to display will be defined in the function create_new_proc_entry in which we will also call the fuction for creation of proc entry.



The init and clean up functions for the module are



The full code of module for creation of proc entry using proc_create is



The makefile for the compilation of the module is



Compile and load the module using



We can see the output by using the cat command

We can see that the message that we passed in the read function of the module is being displayed when the proc entry is read.

Related Posts :

For kernel version before 3.10

Creating a proc entry - 1

Creating a proc read entry