Linux Kernel Driver (V): 4. proc file system

Source: Internet
Author: User

1. Overview of proc file system

1. The proc file system is a mechanism for Kernel-to-user interaction. Through the proc file system, you can view the status of the Linux kernel in the user mode, you can also configure the kernel through the proc file system.

For example, you can use/proc/meminfo to query memory usage.

II. Introduction to proc files/subdirectories

1. APM: Advanced Power Management Information

2. Bus: Bus and equipment on the bus

3. Devices: available settings

4. DRIVER: enabled driver information

5. interrupts: interrupt Information

6. ioports: port usage information

7. Version: kernel version

Iii. Features of the proc file system

1. Each file has a strict permission: strictly stipulated --- readable? Writable? Which user is readable? Which user can write

2. You can use a text editing program to read: More, Cat, Vi, etc.

3. Not only files but also subdirectories

4. You can write your own kernel program to add a file under the/proc directory.

5. The file content is dynamically created and does not exist on the disk.

Iv. proc file system operations

1. In the proc file system, a file is described by a proc_dir_entry struct.

Struct proc_dir_entry {...... read_proc_t * read_proc; // when the user reads this proc file, this callback function will be called write_proc_t * write_proc; // when the user writes this proc file, this callback function will be called ......}

2. Create a proc file

struct  proc_dir_entry * create_proc_entry(const char * name,mode_t mode,struct proc_dir_entry * parent);

= Parameter:

(1) Name: name of the file to be created

(2) mode: the default attribute of the file to be created is 0755.

(3) parent: parent directory of the file, that is, where to store the created File

3. Create a directory

struct  proc_dir_entry * proc_mkdir(const char * name,mode_t mode,struct proc_dir_entry * parent);

= Parameter:

(1) Name: name of the directory to be created

(2) parent: parent directory of this directory

4. delete directories/Files

void remove_proc_entry(const char * name ,struct proc_dir_entry * parent);

= Parameter:

(1) Name: name of the file or directory to be deleted

(2) parent: directory name of the file

5. read/write: to allow users to read and write the added proc file, you need to mount the read/write callback function, that is, --> read_proc, write_proc

6. Read operations

int read_func(char * buffer,char **stat,off_t off,int count,int * peof,void *data);

= Parameter:

(1) Buff: Write the information to be returned to the user in the buffer. The maximum value cannot exceed page_size.

(2) stat: generally not used

(3) off: Offset

(4) count: number of bytes to read

(5) peof: when reading the end of a file, Set * peof to 1.

(6) data: generally not used

7. Write operations

int write_func(struct file *file,const char * buffer,unsigned long count,void * data);

= Parameter:

(1) file: The file structure corresponding to the proc file, which is generally ignored.

(2) buffer: the location of the data to be written

(3) count: data size to be written

(4) data: generally not used

5. Implement a proc file Process

1. Call create_proc_entry to create a struct proc_dir_entry

2. assign values to the created struct proc_dir_entry: read_proc, mode, owner, size, write_proc, etc.

6. Application of proc in development:

1. We can provide a proc interface in the driver so that users can configure the driver and the device. This is a mechanism for users to configure the kernel.

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.