kernel proc File system--and--SEQ interface

Source: Internet
Author: User
Tags file permissions
kernel proc File system

Original address: http://blog.chinaunix.net/uid-20543672-id-3221530.html


All kernel modules to use proc should contain a <linux/proc_fs.h> header file. First, you should understand the most important data structures in the following proc programming:
struct Proc_dir_entry {
unsigned int low_ino;
unsigned int namelen;
const char *name; Entry file name
mode_t mode; File access rights mode
nlink_t Nlink;
uid_t uid; User ID of the file
gid_t GID; The group ID of the file
loff_t size;
const struct Inode_operations *proc_iops; File Inode action function
/*
* NULL->proc_fops means "PDE is going away RSN" or
* "PDE is just created". In either case, e.g.->read_proc won ' t be
* called because it ' s too late or too early, respectively.
*
* If you ' re allocating->proc_fops dynamically, save a pointer
* Somewhere.
*/
const struct File_operations *proc_fops; File action functions
struct Proc_dir_entry *next, *parent, *subdir; Sibling, parent, and subordinate child entry pointers for this portal
void *data;//file private data pointer
read_proc_t *read_proc; File read operation function pointer
write_proc_t *write_proc; File write operation function pointer
atomic_t count; /* Reference count * *
int pde_users; /* The count of the process call module * *
spinlock_t Pde_unload_lock; /* Proc_fops checks and pde_users bumps * *
struct completion *pde_unload_completion;
struct List_head pde_openers;/* call->open, but no->release process pointer was invoked.
}; This data structure represents a proc entry in the kernel and is represented as a file in the PROCFS. You can see some of the file-specific attribute members in this structure, such as UID, GID, mode, name, and so on. But in programming with the default proc API, we need to focus on the read and write function members of this portal:
read_proc_t *read_proc;
write_proc_t *write_proc; After creating the proc portal, if we use the kernel's default action function set Proc_file_operations, we need to initialize the two members above, mapping them to our predefined read and write functions. But if we implement the const struct File_operations * proc_fops ourselves, then we don't have to care about these two functions. The other members of this structure are basically not required to initialize ourselves, and the portal creation function will help us deal with it.

1. Create/proc Portal

PROCFS provides some interface functions for creating and deleting an entry file in the/proc file system:


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

Name: File name

Mode: File Permissions

Parent: The file in the/proc file system, which is a parental directory pointer.

The return value is the Proc_dir_entry pointer that created the completion (or null, which indicates an error occurred during create).

The returned pointer is then used to initialize other parameters of this file entry, such as the function that should be called when a read and write operation is performed on the file.


If the/Proc entry has private data and the operation functions required for this data, you can use the following function:

struct Proc_dir_entry *proc_create_data (const char *name, mode_t mode, struct proc_dir_entry, const *parent struct Erations *proc_fops, void *data);

Name: File name

Mode: File Permissions

Parent: The file in the/proc file system, which is a parental directory pointer.

Proc_fops: File action Functions (if you do not use the kernel's default action function set Proc_file_operations, you can use your own defined file_operations,/proc/by initializing it) The implementation of config.gz is a good example)

Data: Private pointer, which is assigned to the data member of the Proc_dir_entry created.

The return value is the Proc_dir_entry pointer that created the completion (or null, which indicates an error occurred during create).


We can also use Proc_mkdir, Proc_mkdir_mode, and Proc_symlink to create directories and soft links in the/proc file system.

struct Proc_dir_entry *proc_symlink (const char *name, struct proc_dir_entry *parent,const char *dest);

Name: Soft Link file name

Parent: The file in the/proc file system, which is a parental directory pointer.

Dest: Soft link target filename


struct Proc_dir_entry *proc_mkdir (const char *name, struct proc_dir_entry);

Name: Directory Name

Parent: Directory in the/proc file system.

The return value is the Proc_dir_entry pointer that created the completion (or null, which indicates an error occurred during create). The directory initialization permission created has read-only and executable permissions.


struct Proc_dir_entry *proc_mkdir_mode (const char *name, mode_t mode, struct proc_dir_entry);

Name: Directory Name

Mode: Directory Permissions

Parent: Directory in the/proc file system.

The return value is the Proc_dir_entry pointer that created the completion (or null, which indicates an error occurred during create).

All of the parent arguments above can be NULL (representing the/proc root directory) or many other values, depending on where we want to put the file. Some of the other parent proc_dir_entry you can use are listed below, along with their location in this file system.

Proc_dir_entry location in the file system
Proc_root_fs /proc

Proc_net /proc/net

Proc_bus /proc/bus

Proc_root_driver /proc/driver


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Note: There are two options for creating the/proc Portal:

(1) using the Proc default action function set Proc_file_operations, you can use the above function:

Create_proc_entry

Proc_create_data (of which proc_fops=null)

(2) Use the file_operations of your own implementation, please use:

Proc_create_data (which proc_fops= you to implement the Operation function)

for (2), the implementation of/proc/config.gz is a good example (/KERNEL/CONFIGS.C), and his read function is implemented by itself (but the kernel's LIBFS interface is used internally).

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


2, mapping callback functions (using PROC default action function set Proc_file_operations, but this default interface is becoming more and more invisible, such code will slowly be cleared out of the kernel line, we should use the Seq_file interface described later)

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.