hello.c:
#include
#include
MODULE_LICENSE("Dual BSD/GPL");
MODULE_AUTHOR("Veritastry");
static int many = 0;
module_param(many, int, S_IRUGO);
static int hello_init(void)
{
printk(KERN_ALERT "Hello, world mod %d\n",many);
return 0;
}
static void hello_exit(void)
{
printk(KERN_ALERT "Goodbye, cruel world\n");
}
module_init(hello_init);
module_exit(hello_exit);
Makefile
#if KERNELRELEASE is defined, we've been invoked from the
# kernel build system and can use its language.
ifneq ($(KERNELRELEASE),)
obj-m := hello.o
# Otherwise we were called directly from the command
# line; invoke the kernel build system.
else
KERNELDIR ?= /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
default:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
endif
这个简单的程序告诉大家应该怎么写一个模块,MODULE_LICENSE告诉内核该模块的版权信息,很多情况下,用GPL或者BSD,或者两个,因为
一个私有模块一般很难得到社区的帮助。module_init和module_exit用于向内核注册模块的初始化函数和模块推出函数。如程序所示,初始
化函数是hello_init,而退出函数是hello_exit。
阅读(440) | 评论(0) | 转发(0) |