here is simple example to demonstrate adding /proc file system entry for the module, as follows
shiv@ubuntu:~/ds/linx$ cat heloproc.c
/*
* Kernel program demo to illustrate how to create/add entry to
* /proc file system
*/
#include <linux/init.h>
#include <linux/module.h>
#include <linux/proc_fs.h>
/* Module license */
MODULE_LICENSE("Dual BSD/GPL");
/*
* This is the service routine for added /proc entry
* when user try to look into /proc/<entry added> this routine will be executed
*
* char *buf - below is page allocated by kernel and passed to service routine
* eof and start would be used only when using /proc for
* getting more than one page (usually page is 4KB)
* for complete info you could refer linux docs
*/
int demo_read_fs(char *buf, char **start, off_t offset, int count, int *eof, void *data)
{
int len=0;
len = sprintf(buf, "This is Demo proc fs creation\n\n");
return len;
}
/*
* create_proc_read_entry() used for adding /proc fs entry in linux
* name of the entry will be "Demoprocfs"
* and it will be seen as /proc/Demoprocfs
*/
static void add_proc_fs_entry()
{
create_proc_read_entry("Demoprocfs", 0, NULL, demo_read_fs, NULL);
}
/* module init */
static int proc_fs_init(void)
{
printk(KERN_ALERT "loading demo proc fs file module\n");
/* add /proc fs entry */
add_proc_fs_entry();
return 0;
}
/* module unloaded when we remove it */
static void proc_fs_exit(void)
{
printk(KERN_ALERT "unloaded demo proc fs file module");
/*
* when we unload module, it will remove /proc entry added
* when loading the module
*/
remove_proc_entry("Demoprocfs", NULL);
}
module_init(proc_fs_init);
module_exit(proc_fs_exit);
add_proc_fs_entry();
return 0;
}
/* module unloaded when we remove it */
static void proc_fs_exit(void)
{
printk(KERN_ALERT "unloaded demo proc fs file module");
/*
* when we unload module, it will remove /proc entry added
* when loading the module
*/
remove_proc_entry("Demoprocfs", NULL);
}
module_init(proc_fs_init);
module_exit(proc_fs_exit);
/* end of the program */
Testing of sample code written above
reference:
Linux device drivers by Jonathan Corbert, Alessandro, Greg Kroah-Hartman, Oreilly publication
Linux source : http://git.kernel.org/?p=linux/kernel/git/torvalds/linux-2.6.git;a=tree;hb=HEAD
Thanks,
-Shiv
Comments
Post a Comment