/*
* Kernel Programming test code
*
* Copyright (C) Sun Mingbao <[email protected]>
* 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 <[email protected]>");
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);
Linux kernel proc File system Usage Example