一个最简单的linux内核模块。当他加载和退出时各会打出一行信息。可以作为程序的框架,也可以测试加载模块。
#include "linux/init.h"
#include "linux/module.h"
//MODULE_LICENSE("GPL"); --此处:编译进内核的代码一定要标识自己符合GPL协议等开源协议。模块可以不用
static int mysc_init(void)
{
printk("hello\n");
return 0;
}
static void mysc_exit(void)
{
printk("bye\n");
}
module_init(mysc_init);
module_exit(mysc_exit);
对应的Makefile (不是精简的,不过能用就行了。)
KERNELVERSION=$(shell uname -r)
KERNELDIR=/lib/modules/$(KERNELVERSION)/build
CC=gcc
ifneq ($(KERNELRELEASE),)
obj-m := mysc.o
else
PWD:=$(shell pwd)
modules:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
modules_install:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install
clean:
rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions
endif