Proc fs is a virtual file system mounted in /proc directory which has at least two functions:
- Exporting kernel state to user-space. It’s like a kernel’s point of view about the system.
- Communicating particular configuration to the kernel on the fly. It’s an interface to kernel components through which a system operator can interactive modify system behavior.
Shell commands that are common used with proc are: cat, echo and less. Examples of its usage could be:
- See system devices:
cat /proc/devices
- Enable IPv4 forwarding:
echo 1 >/proc/sys/net/ipv4/ip_forward
Now having enough about what we should know about operating proc from user space it is the moment to see how things are really done into kernel space.
Two main files from kernel source tree that you should inspect to see some proc API functions are:
- fs/proc/generic.c
- include/linux/proc_fs.h
Doing some Linux Cross Reference code search about proc_mkdir , create_proc_entry, and remove_proc_entry you will find that the kernel API is simple and their function signatures are like this:
struct proc_dir_entry *proc_mkdir (const char *name, struct proc_dir_entry *parent) struct proc_dir_entry *create_proc_entry (const char *name, mode_t mode, struct proc_dir_entry *parent) void remove_proc_entry(const char *name,struct proc_dir_entry *parent)
At this point is intuitive how we can create, or destroy directories and files under proc file system, but how do we process their I/O? The answer is like this: “Using a proc read and a proc write function setup using the resulting struct proc_dir_entry* after we link in the desired file.”
By a proc read function you should understand a function used by kernel to process data when a /proc file is read from user space; usually performed in the reader user process context; and by a proc write function a function that is used by the kernel to process data when someone writes in a proc file.
proc read and write functions have the prototype as follows:
int func_read(char *buffer, char **start, off_t offset,
*int count, int *peof, void *dat)
ssize_t func_write(struct file *filp, const char __user *buff,
unsigned long len, void *data)
Furthermore, you can find a good example at Basic proc file system module page.
[...] should read Basic proc file system kernel API page [...]
By: Basic proc file system module « Briefbox on October 2, 2008
at 11:37 am