/*
* 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);
原文地址:http://blog.csdn.net/crazycoder8848/article/details/42580835