内核模块格式:
1.入口函数(加载函数)
#include <linux/init.h>
#define module_init(x) __initcall(x); //注册加载函数
#define __define_initcall(level,fn,id) \
static initcall_t __initcall_##fn##id = fn;
//static initcall_t __initcall_xxx6 = fn;
typedef int (*initcall_t)(void);
#include <linux/init.h>
int __init demo_init(void)
{
return 0;
}
module_init(demo_init);
2.出口函数(卸载函数)
#define module_exit(x) __exitcall(x);
typedef void (*exitcall_t)(void);
void __exit demo_exit(void)
{
}
module_exit(demo_exit);
3.GPL协议声明
/* "GPL" [GNU Public License v2 or later]*/
#define MODULE_LICENSE(_license) MODULE_INFO(license, _license)
#include <linux/module.h>
MODULE_LICENSE("GPL");
内核模块例程
//创建c源码文件 demo.c
#include <linux/init.h>
#include <linux/module.h>
/*加载回调函数,在用户加载该内核模块的时候,会调用该函数*/
int __init demo_init(void)
{
printk(KERN_DEBUG "---%s---%s---%d---\n",__FILE__,__func__,__LINE___);
return 0;
}
/*卸载回调函数,在用户卸载该内核模块的时候,会调用该函数*/
void __exit demo_exit(void)
{
printk(KERN_DEBUG "---%s---%s---%d---\n",__FILE__,__func__,__LINE___);
}
module_init(demo_init); //注册加载函数
module_exit(demo_exit); //注册卸载函数
MODULE_LICENSE("GPL"); //GPL协议声明