Proc File system in Linux--LDD3 reading notes

Source: Internet
Author: User
Tags function prototype

1./proc File System Overview

The/proc file system is a file system created by the software that is used by the kernel to report information to the outside world. /proc each of the following files is associated with a kernel function, and the corresponding kernel function is used to generate the contents of the file when the file is read. We've seen a lot of these files, for example,/proc/modules always returns modules loaded in the current kernel.

/proc widely used in Linux file systems, many applications on modern Linux distributions, such as PS, top and uptime, get the information they need from/proc. Some drivers also report information through the/proc file system, and of course your driver can do the same. The/proc file system is dynamic, so your module can add or delete portals at any time. The full feature of the/proc entrance can be a complex beast that can be read or written. However, most of the time, files in/proc are read-only. A read-only/proc file is considered here.

However, before proceeding, it must be emphasized that adding files under/proc is not advocated. The/proc file system is seen by kernel developers as uncontrollable confusion, which is 108,000 miles from the original purpose of reporting the process running in the system. The recommended way to get information in the new code is through SYSFS. However, using SYSFS is a need to have an understanding of the Linux device model. And,/proc the following files are easier to create and fully conform to the purpose of debugging, so you will learn the/proc file system.

2. Create a/proc file

If you define a Read_proc function, you need to associate it with the entry in the/proc hierarchy. This is accomplished by a function called Create_proc_read_entry, which is a function prototype as follows:

struct Proc_dir_entry *create_proc_read_entry (const char *name,mode_t mode,struct proc_dir_entry *base,read_proc_t * Read_proc,void *data);
In the function prototype above, name is the name of the file to be created, mode is the file's protection mask, you can use the system default mask with zero, base specifies the directory where the file is created, and if base is NULL, the file created exists under the/proc directory; read_ Proc is the function that implements this file; The data parameter is ignored by the kernel, but is passed to the Read_proc function. To give an example:
Create_proc_read_entry ("Scullmem", 0/*default mode*/, null/*parent dir*/, SCULL_READ_PROCMEM, null/*client data*/);
In this example, we created a file under/proc, named Scullmem, with the system's default protection rights.

The directory entry pointer can be used to create the entire directory schema under/proc, however, it is easier to note that a file is placed in the/proc subdirectory as part of the path to the name of the new file to be created as long as that subdirectory already exists. For example, a frequently overlooked convention is that the/proc file associated with the driver should be placed in a driver subdirectory. For example, in the example above, we want to put the created file Scullmem into driver, just modify the first parameter to "Driver/scullmem".

The files in the/proc should be deleted when the module is unloaded. What Remove_proc_entry does is the opposite of what Create_proc_read_entry is doing. For example:

Remove_proc_entry ("Scullmem", NULL/*parent dir*/);
The unsuccessful removal of the file will cause the file to be called at an inappropriate time, or the kernel will crash after the module is unloaded. When using/proc as above, there are a few troublesome things to remember-no wonder it's not recommended now. The most important problem is when you delete the/proc file, the file may still be in use when you delete it. Because the/proc file does not have a related owner, it does not work on reference counts when they are used. This problem can simply be caused by using the following command before deleting the file:
Sleep  </proc/myfile
Another problem is registering two files of the same name. The kernel trusts the driver and does not check that the name of the file is registered, so if you are not careful, you may register multiple files of the same name. It's like the famous problem that happens in the classroom, these files are indistinguishable, whether you're visiting them or calling Remove_proc_entry.
3. Implementing files in/proc

All modules that use/proc should include the <linux/proc_fs.h> header file to define the appropriate function. To create a read-only/proc file, your driver must implement a function that generates some data when the file is read. When some processes use the Read function to read a file, the request passes through the function to reach your module. We'll take a look at this function first, and then introduce the registered interface. When a process reads the/proc file, the kernel allocates a page of memory that the driver can write to the page that will return the data to the user's space. The cache is passed to your Read_proc function. The Read_proc function is prototyped as follows:

Int (*read_proc) (char *page,char **start,off_t offset,int count,int *eof,void *data);
The parameter page is a pointer to the buffer where you can write data, and start is used by the function to indicate that the data of interest is being written to the page's location. Offset and count are the same as the parameters of the read function; EOF points to an integer that must be set by the driver to report the data that is not returned, and that data is dependent on a specific driver's pointer, which you can use as an internal bookkeeping.

This function should return the number of bytes of data written to the page buffer. Just like the read function. The other outputs are *eof and *start. EOF is a simple tag, but the use of start is complicated by writing, and its purpose is to help implement large (more than one page)/proc files. The start parameter is not commonly used, and its purpose is to indicate where in the page the Home Office is returned to the user space. When the Read_proc function is called, the *start parameter is set to NULL. If you keep it null, the kernel assumes that the parameter offset is 0 when the data is placed inside the page. In other words, the kernel assumes a simple Read_proc version that writes all the contents of the virtual file to the page, ignoring the offset parameter. Conversely, if you set a value that is not NULL for *start, the kernel assumes that the data pointed to by *start takes offset into account and is ready to return to user space. In general, the simple Proc_read method returns a very small amount of data and often ignores the *start parameter. A more complex approach is to set the value of the page's *start and simply place the data at the requested offset.

4. Example

Here, we have written a simple example to illustrate the use of/proc:

#include <linux/kernel.h> #include <linux/init.h> #include <linux/module.h> #include <linux/proc _fs.h>int Read_proc (char *page,char **start,off_t offset,int count,int *eof,void *data); static int __init test_proc_ init (void) {    create_proc_read_entry ("Read_proc", 0,null,read_proc,null);    return 0;} static void __exit test_proc_exit (void) {    remove_proc_entry ("Read_proc", NULL);} int Read_proc (char *page,char **start,off_t offset,int count,int *eof,void *data) {    int len = sprintf (page, "%s\n", " Hello World ");    return Len;} Module_init (Test_proc_init); Module_exit (Test_proc_exit); Module_license ("GPL"); Module_author ("Wangxq");

After inserting this module into the kernel, use the command:
Cat/proc/read_proc

You will see the output Hello World.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.