Полезная информация

next up previous contents
Next: The scheduler Up: The /proc filesystem Previous: Structure of the /proc

Programming the /proc filesystem

 

Note: the code fragments in this section won't match the sources for your own kernel exactly, as the /proc filesystem has been expanded since this was originally written, and is being expanding still more. For instance, the root_dir structure has nearly doubled in size from the one quoted here below.

Unlike in most filesystems, not all inode numbers in the proc filesystem are unique. Some files are declared in structures like

screen1660

and some files are dynamically created as the filesystem is read. All the process directories (those with numerical names and self) essentially have inode numbers that are the pid shifted left 16 bits, but the files within those directories re-use low (1-10 or so) inode numbers, which are added at runtime to the pid of the process. This is done in inode.c by careful re-assignment of inode_operation structures.

Most of the short read-only files in the root directory and in each process subdirectory one use a simplified interface provided by the array_inode_operations structure, within array.c.

Other directories, such as /proc/net/, have their own inode numbers. For instance, the net directory itself has inode number 8. The files within that directory use inode numbers from the range 128-160, and those are uniquely identified in inode.c and the files given the proper permissions when looked up and read.

Adding a file is relatively simple, and is left as an exersize for the reader. Adding a new directory is a little bit harder. Assuming that it is not a dynamically allocated directory like the process directories, here are the steps:gif

  1. Choose a unique range of inode numbers, giving yourself a reasonable amount of room for expansion. Then, right before the line

    add a section that looks like this:

    screen1670

    but modify it to to do what you want. For instance, perhaps you have a range of 200-256, and some files, inodes 200, 201, and 202, and some directories, which are inodes 204 and 205. You also have a file that is readable only by root, inode 206.

    Your example might look like this:

    screen1672

  2. Find the definition of the files. If your files will go in a subdirectory of /proc, for instance, you will look in root.c, and find the following:

    You will then add a new file to this structure, like this, using the next available inode number:

    screen1676

    You will then have to provide for this new directory in inode.c, so:

    screen1678

    becomes

    screen1680

  3. You now have to provide for the contents of the files within the foo directory. Make a file called proc/foo.c, following the following model:gif [The code in proc_lookupfoo() and proc_readfoo() should be abstracted, as the functionality is used in more than one place.]

    screen1685

  4. Filling in the directories dir1 and dir2 is left as an excersize. In most cases, such directories will not be needed. However, if they are, the steps presented here may be applied recursively to add files to a directory at another level. Notice that I saved a range of 200-256 for /proc/foo/ and all its subdirectories, so there are plenty of unused inode numbers in that range for your files in dir1 and dir2. I suggest reserving a range for each directory, in case you need to expand. Also, I suggest keeping all the extra data and functions in foo.c, rather than making yet another file, unless the files in the dir1 and dir2 directories are significantly different in concept than foo.
  5. Make the appropriate changes to fs/proc/Makefile. This is also left as an excersize for the reader.

[Please note: I have made changes similar to these (I wrote the /proc/net/ support). However, this has been written from memory, and may be unintentionally incomplete. If you notice any inadequacies, please explain them to me in as complete detail as possible. My email address is johnsonm@sunsite.unc.edu]


next up previous contents
Next: The scheduler Up: The /proc filesystem Previous: Structure of the /proc

Converted on:
Mon Apr 1 10:20:16 EST 1996