Linux Kernel proc file system usage example, Linux kernel proc example
/*
* Kernel programming test code
*
* Copyright (C) 2014 Sun Mingbao <sunmingbao@126.com>
* Dual licensed under the MIT and/or GPL licenses.
*
*/
# Include <linux/init. h>
# Include <linux/module. h>
# Include <linux/types. h>
# Include <linux/kernel. h>
# Include <linux/proc_fs.h>
# Include <linux/string. h>
MODULE_AUTHOR ("Sun Mingbao <sunmingbao@126.com> ");
MODULE_DESCRIPTION ("kernel programming test code ");
MODULE_VERSION ("1.0 ");
MODULE_LICENSE ("Dual MIT/GPL ");
# Define MODULE_NAME "test"
# Define WRITE_CONSOLE (fmt, args ...)\
Do \
{\
Printk (KERN_ALERT fmt, # args );\
} While (0)
# Define DBG_PRINT (fmt, args ...)\
Do \
{\
WRITE_CONSOLE (MODULE_NAME "_ DBG: % s (% d)-% s: \ n" fmt "\ n", _ FILE __,__ LINE __,__ FUNCTION __, # args );\
} While (0)
Static struct proc_dir_entry * my_proc_dir;
Static char proc_file_contents [] = "I love kernel programming very much \ n ";
Static int proc_file_len = sizeof (proc_file_contents)-1;
Static int misc_info_read_proc (char * buffer, char ** start, off_t offset, int length, int * eof, void * data)
{
Int ret;
Ret = snprintf (buffer, length, "% s", proc_file_contents + offset );
If (ret + offset = proc_file_len)
* Eof = 1;
Return ret;
}
Static int _ init create_my_proc_entries (void)
{
My_proc_dir = proc_mkdir (MODULE_NAME, NULL );
Create_proc_read_entry ("misc_info"
, 0
, My_proc_dir
, Misc_info_read_proc
, NULL );
Return 0;
}
Static int _ init test_init (void)
{
Int retval;
DBG_PRINT ("start ");
Retval = create_my_proc_entries ();
If (retval <0)
{
Goto EXIT;
}
DBG_PRINT ("start succeed ");
EXIT:
Return retval;
}
Static void _ exit remove_my_proc_entries (void)
{
Remove_proc_entry ("misc_info", my_proc_dir );
Remove_proc_entry (MODULE_NAME, NULL );
}
Static void _ exit test_exit (void)
{
DBG_PRINT ("quit ");
Remove_my_proc_entries ();
}
Module_init (test_init );
Module_exit (test_exit );