Chapter 3. Communicating with userland

Table of Contents

Reading data
Writing data
A single call back for many files

Instead of reading (or writing) information directly from kernel memory, procfs works with call back functions for files: functions that are called when a specific file is being read or written. Such functions have to be initialised after the procfs file is created by setting the read_proc and/or write_proc fields in the struct proc_dir_entry* that the function create_proc_entry returned:

struct proc_dir_entry* entry;

entry->read_proc = read_proc_foo;
entry->write_proc = write_proc_foo;
    

If you only want to use a the read_proc, the function create_proc_read_entry described in the section called “Convenience functions” may be used to create and initialise the procfs entry in one single call.

Reading data

The read function is a call back function that allows userland processes to read data from the kernel. The read function should have the following format:

int read_func(char*  page,
 char**  start,
 off_t  off,
 int  count,
 int*  eof,
 void*  data);

The read function should write its information into the page. For proper use, the function should start writing at an offset of off in page and write at most count bytes, but because most read functions are quite simple and only return a small amount of information, these two parameters are usually ignored (it breaks pagers like more and less, but cat still works).

If the off and count parameters are properly used, eof should be used to signal that the end of the file has been reached by writing 1 to the memory location eof points to.

The parameter start doesn't seem to be used anywhere in the kernel. The data parameter can be used to create a single call back function for several files, see the section called “A single call back for many files”.

The read_func function must return the number of bytes written into the page.

Chapter 5, Example shows how to use a read call back function.