Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 10: Filesystems without Persistent Storage


10.1.5 Managing /proc Entries


Before theprocfilesystem can be put to meaningful use, it must be filled with entries containing data.
Several auxiliary routines are provided to add files, create directories, and so on, in order to make this
job as easy as possible for the remaining kernel sections. These routines are discussed below.

Thefactthatnewprocentries can be easily generated should not disguise the fact
that it is not accepted practice to use code todo this. Nevertheless, the simple, lean
interface can be very useful for opening up a communication channel for test
purposes between kernel and userspace with minimum effort.

I also discuss methods used by the kernel to scan the tree of all registeredprocentries to find required
information.

Creating and Registering Entries


New entries are added to theprocfilesystem in two steps. First, a new instance ofproc_dir_entryis
created together with all information needed to describe the entry. This instance is then registered in the
data structures ofprocso that it is visible to the outside. Because the two steps are never carried out
independently of each other, the kernel makes auxiliary functions available to combine both actions so
that new entries can be generated quickly and easily.

The most frequently used function is calledcreate_proc_entryand requires three arguments:

<proc_fs.h>
extern struct proc_dir_entry *create_proc_entry(const char *name, mode_t mode,
struct proc_dir_entry *parent);

❑ namespecifies the filename.
❑ modespecifies the access mode in the conventionalUnixscheme (user/group/others).
❑ parentis a pointer to theproc_dir_entryinstance of the directory where the file is to be
inserted.

Caution: The function fills only the essential elements of theproc_dir_entrystructure. It is therefore
necessary to make a few brief ‘‘manual‘‘corrections to the structure generated.

This is illustrated by the following sample code, which generates theproc/net/hyperCardentry to
supply information on a (unbelievably good) network card:

struct proc_dir_entry *entry = NULL;

entry = create_proc_entry("hyperCard", S_IFREG|S_IRUGO|S_IWUSR,
&proc_net);

if (!entry) {
printk(KERN_ERR "unable to create /proc/net/hyperCard\n");
return -EIO;
} else {
entry->read_proc = hypercard_proc_read;
Free download pdf