In the posts "creating proc read entry",creating read/write proc entry we saw how to create read only proc entries and read write proc entries.
In both the examples we created the proc entry under /proc and not in any sub diretory in /proc.
In this post we will see how to create a folder under /proc and then create an entry in it.
We will use the same example as in the post create read write proc entry and extend it further.
To create folder under /proc we use the function
proc_mkdir(,struct proc_dir_entry *parent);
Arguments :
Folder name: The name of the folder that will be created under /proc.
Parent: In case the folder needs to be created in a sub folder under /proc a pointer to the same is passed else it can be left as NULL.
proc_mkdir in turn returns a pointer to the folder which will be of the kind "struct proc_dir_entry"
If we want to create folder by the name "newparent" then the function call will look as below
struct proc_dir_entry *proc_parent
proc_parent = proc_mkdir("newparent",NULL)
The call to create_proc_entry will become
proc_write_entry = create_proc_entry("hello",0666,proc_parent);
Thus "hello" is the name of the entry, by passing 0666 we are giving read and write permission to every one,
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 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
proc_write_entry->read_proc = read_proc ;
proc_write_entry->write_proc = write_proc;
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. int read_proc(char *buf,char **start,off_t offset,int count,int *eof,void *data ) { int len=0; len = sprintf(buf,"\n %s\n ",proc_data); return len; } The write function will have to written to accept data from the user and put it into the array proc_data as shown below.
int write_proc(struct file *file,const char *buf,int count,void *data ) { if(count > MAX_PROC_SIZE) count = MAX_PROC_SIZE; if(copy_from_user(proc_data, buf, count)) return -EFAULT; return count; }
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_dir.c :
Makefile:
Now run the following commands as root
$ make
$ insmod proc_dir.ko
After inserting successfully, write some data into the proc entry as shown below.
$ echo "Hello World" > /proc/newparent/hello
You can see that the new entry in inside the folder "newparent" and not under /proc Now read the proc entry and see if the same data written above is shown or not.
$cat /proc/newparent/hello
Hello world
What ever you written into the proc etnry, you should be able to read it back using the cat command.
Reference Books
In both the examples we created the proc entry under /proc and not in any sub diretory in /proc.
In this post we will see how to create a folder under /proc and then create an entry in it.
We will use the same example as in the post create read write proc entry and extend it further.
To create folder under /proc we use the function
proc_mkdir(
Arguments :
Folder name: The name of the folder that will be created under /proc.
Parent: In case the folder needs to be created in a sub folder under /proc a pointer to the same is passed else it can be left as NULL.
proc_mkdir in turn returns a pointer to the folder which will be of the kind "struct proc_dir_entry"
If we want to create folder by the name "newparent" then the function call will look as below
struct proc_dir_entry *proc_parent
proc_parent = proc_mkdir("newparent",NULL)
The call to create_proc_entry will become
proc_write_entry = create_proc_entry("hello",0666,proc_parent);
Thus "hello" is the name of the entry, by passing 0666 we are giving read and write permission to every one,
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 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
proc_write_entry->read_proc = read_proc ;
proc_write_entry->write_proc = write_proc;
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. int read_proc(char *buf,char **start,off_t offset,int count,int *eof,void *data ) { int len=0; len = sprintf(buf,"\n %s\n ",proc_data); return len; } The write function will have to written to accept data from the user and put it into the array proc_data as shown below.
int write_proc(struct file *file,const char *buf,int count,void *data ) { if(count > MAX_PROC_SIZE) count = MAX_PROC_SIZE; if(copy_from_user(proc_data, buf, count)) return -EFAULT; return count; }
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_dir.c :
Makefile:
Now run the following commands as root
$ make
$ insmod proc_dir.ko
After inserting successfully, write some data into the proc entry as shown below.
$ echo "Hello World" > /proc/newparent/hello
You can see that the new entry in inside the folder "newparent" and not under /proc Now read the proc entry and see if the same data written above is shown or not.
$cat /proc/newparent/hello
Hello world
What ever you written into the proc etnry, you should be able to read it back using the cat command.
Reference Books
No comments:
Post a Comment