2.8 The Proc File System

   


All files in the /proc directory are virtual files. They do not exist on any memory medium, but are generated directly by the kernel upon each read access. A proc file is normally a text file showing information about specific parts of the kernel. For example, the commands lspci or apm show you information from the proc files /proc/pci und /proc/apm, respectively, and information about the current devices on the PCI bus or the state of the notebook battery.

The possibilities of the proc file system to display information on the kernel easily in the user mode are used by many system developers. Files and directories in the /proc directory can be easily implemented. In addition, you can register and unregister dynamically, so that the proc directory is often used by modules.

The files and directories in the /proc directory are essentially based on the proc_dir_entry structure, shown in Figure 2-10. Such a structure represents either a directory or a file. The directory proc is represented by the variable proc_root. The attributes and methods of the proc_dir_entry structure have the following meaning:

Figure 2-10. Structure of proc_dir_entry.
 struct proc_dir_entry {       unsigned short          low_ino;       unsigned short          namelen;       const char              *name;       mode_t                  mode;       nlink_t                 nlink;       uid_t                   uid;       gid_t                   gid;       unsigned long           size;       ...       struct proc_dir_entry   *next, *parent, *subdir;       void                    *data;       int                     (*get_info)(buffer, start, off, count);       int                     (*read_proc) (buffer, start, off, count, eof, data);       int                     (*write_proc)(file, buffer, count, data);       int                     (*readlink_proc)(proc_dir_entry, page);       unsigned int            count; /* use count */       int                     deleted; /* delete flag */ }; 

  • low_ino is the file's Inode number. This value is filled automatically by proc_register when the file is initialized.

  • namelen specifies the length of the file or directory name, name.

  • name is a pointer to the name of the file (or directory).

  • mode specifies the file's mode; this value is set to S_DIR for directories.

  • nlink specifies the number of links to this file (default = 1).

  • uid or gid specifies the user or group ID of the file.

  • size specifies the length of the file as shown when the directory is displayed.

  • data is a pointer that can point to private data.

  • next, parent, and subdir are pointers to link the proc directory structure.

  • read_proc() runs when you read-access a proc file. The only task of this function is to fill the buffer with the file's output and return the number of written characters as result.

  • write_proc() is called when you write-access the proc file.

In earlier kernel versions, a proc_dir_entry structure had to be created and initialized for each entry to be added to the proc directory. As we have seen above, many of the variables in the structure are needed only after registration.

The following functions were defined to simplify handling of proc entries.

create_proc_entry()

fs/prof/generic.c


create_proc_entry(name, mode, parent) creates a file with name in the proc directory. The relative path to /proc/ can be specified in a name, or a pointer to the proc_dir_entry structure of the directory, in which the file should appear, can be set in the parameter parent. References to the /proc and /proc/net directories can be obtained from the pointers proc_root and proc_net. The parameter mode lets you pass flags for file properties of the proc file you want to create. Normally, this is filled with value 0.

As a result of this function, you obtain a pointer to the proc_dir_entry structure created. Now you can enter handling routines for read and write operations on the proc file. You can also set the pointer data to private data of a proc entry. This is necessary especially when a read or write function is used for several proc files.

The following source text is a good example to show you how a proc file, /proc/net/test, is created and initialized:

 test_entry = create_proc_entry("test", 0600, proc_net); test_entry->nlink = 1; test_entry->data = (void *) &test_data; test_entry->read_proc = test_read_proc; test_entry->write_proc = test_write_proc; 

remove_proc_entry()

fs/proc/generic.c


remove_proc_entry(name, parent) removes the proc file specified in name. As with create_proc_entry(), you can either state the relative path to /proc or the proc_dir_entry structure of the directory where the file name is located.

proc_mkdir()

fs/proc/generic.c


Though create_proc_entry() can be used to create directories in the proc directory, the kernel offers a simpler way with proc_mkdir(name, parent). The parameters name and parent can be used as in the functions described above. The result of this function is a pointer to the proc_dir_entry structure of the directory you created. The example in Appendix D shows how you can create the directory /proc/test by using this function.

create_proc_read_entry()

include/Linux/proc_fs.h


We often want to create files in the proc directory merely to display certain information. This means that it is sufficient to register a function to handle a read access to the proc file. Though you can use create_proc_entry() and then register the read function, as in our example above, the kernel offers another function to achieve this in one step.

The function create_proc_read_entry(name, mode, base, get_info) creates the proc file name and uses the function get_info() to initialize read accesses. The parameters name, mode, and base are used as in create_proc_entry().

When there is no write access to the proc file proc/net/test and no private data has to be passed in the above example, then this function can be simplified as follows, where get_info() is the method used to handle read access to the proc file:

 test_entry = create_proc_read_entry("test", 0600, proc_net, test_get_info); 

create_proc_info_entry( )

include/linux/proc_fs.h


create_proc_info_entry(name, mode, base, read_proc, data) creates a file in the proc directory, just as create_proc_read_entry()$$$, but it additionally sets the parameter data in the proc_dir_entry structure. This variant is used when the read function read_proc() is needed more than once. Note that it has to be reentrant, and the pointer data to the private data passed corresponds to the proc file called.

This means that the above example can be replaced by the following function call:

 test_entry = create_proc_read_entry("test", 0600, proc_net, test_read_proc, &test_data); 


       


    Linux Network Architecture
    Linux Network Architecture
    ISBN: 131777203
    EAN: N/A
    Year: 2004
    Pages: 187

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net