The Proc File System: In this abstract article, we mainly introduce how this file system works, and how the existing network code uses this file system, and we are in the program
How to write, use Proc Entry.
Overview: The Proc file system is frequently used in many programs, although it is designed to be a directory structure and inode as other file systems, but in fact, it is some registered functions.
They provide information on important system variables. The Proc directory has a lot of subdirectories - each running process one and some of the subsystem, such as a file system, interface, terminal, network (/ proc / net). in
There are also many files in the / proc home directory - IOPORTS, LOADAVG, and more. The files in the subdirectory of each process describe some information for this process, such as Command
Line, Current Working Directory, Status, etc. Nuclear Monitor All access to the Proc file system, with special functions to replace ordinary file operation functions. When a file in the / proc directory is created, it is annotated
Some functions to tell the kernel If this file is read or written, how should it be operated. Many files only allow read operations to print some status information to the console. It should be noted that the kernel is a function of the call generating status information while reading the operation, so if there is no reading, the next read operation may overwrite the original
Some information, so we'd better finish the status information at a time, the best way is to provide a large buffer, such as the Page_Size byte size is best, can be read at a time.
Network proc files: ARP displays Neighbor Table (ARP_TBL); IP address / hardware address, hardware type, device, logo, etc. (ARP_GET_INFO (): Net / IPv4 / ARP.C) DEV Displays statistics for each registered device. DEV_STAT Displays the number of packets that receive / discard and FASTROUTE statistics (dev_proc_stats (): net / core / dev.c) NetStat Display Sync cookie, pruning, and icmp statistics (netstat_get_info (): Net / IPv4 / Proc.c) RAW Displays the address, queue, and timeout information for each of the open original sockets to Struct Proto Raw_prot. (
GET_NETInfo (): Net / IPv4 / Proc.c) Route Displays the FIB table (Main_Table), The Interface, Address, Gateway, Flags, and Usage Information
(FIB_GET_PROCIINFO ()): Net / IPv4 / Fib_Frontend.c) RT_Cache Displays Routing Cache (RT_Hash_Table), The Interface, Address, Gateway, Usage, Source, And Other
Infomation. (RT_Cache_GET_INFO (): Net / IPv4 / Route.c) SockStat Display Number of Sockets That Have Been Used and Some Statistics On How Many Were TCP, UDP,
and RAW (afinet_get_info (): net / ipv4 / proc.c) tcp display address, queue, and timeout information for each open TCP socket form struct proto tcp_prot (get__netinfo (): net / ipv4 / proc.c) udp display address, Queue, And Timeout Information for Each Open UDP Socket Form Struct Proto UDP_PROT
(GET / IPV4 / PROC.C) Register Proc files: In this paragraph, we introduce a method of registering read-only proc files (only for Linux 2.0 and later version), we can implement file_Operations
And the inode_operations structure makes it more features. However, the latter is complicated than the first method we mentioned, and you can see how the source code is learned to know how the latter is implemented. Below
Method: Define a function, then registering / unregisterIing This function is in the kernel, providing the approximate number of tests, tracking system resources. But pay attention to: only
The kernel can register the proc file, and the user can create / install the kernel module (root user identity). The following process assumes that Linux source code has been installed, and the kernel is compiled into allowed
Use module.
Formatting a function to provide information static int int_proc_function (char * buf, char ** start, off_t offset, int LEN, int unused) is the function that Linux kernel is called when it read the proc file prototype. Among these most important parameters, BUF, a letter to the buffer, for the kernel store
interest. Other parameters generally do not change. In general, this function prints a header, then traverses a table, prints its content (using sprintf), and returns the length of the result string, the only limit
The system is buf that cannot exceed the Page_Size size (Page_SIZE is 4KB).
Create a Proc entry: Because it is part of the file system, this entry requires an inode, you can use the Proc_Dir_Entry structure to get it.
#include
The two functions are the same, except for the second use of Proc / Net as a directory. Returns 0 (success) or EAGAIN (there is no available inode).
Uninstall a proc entry: int proc_unregister (struct proc_dir_entry * dir, int inode) int proc_net_unregister (int inode) dir is a directory of files (entries) resides, inode is the inode number of this document (proc_dir_entry.low_ino), the second function Use the / proc / net directory. return
Returning to 0 (success) or EAGAIN (there is no available inode).
Examples:. Simple_entry.c / * simple_entry.c * * This program provides an example of how to install an entry into the * / proc File System All this entry does is display some statistical * information about IP * /.
#define MODULE #include