1. hello.c
#include <linux/init.h>
#include <linux/module.h>
MODULE_LICENSE("Dual BSD/GPL");
static int hello_init(void)
{
printk(KERN_ALERT "Hello, world\n");
return 0; // insmod加载时候判断用,返回值为负值,则加载失败
}
static void hello_exit(void)
{
printk(KERN_ALERT "Goodbye, cruel world\n");
}
module_init(hello_init);
module_exit(hello_exit);
/*
module_init and module_exit lines use special kernel macros to indicate the role of
these two functions. Another special macro (MODULE_LICENSE)is used to tell the
kernel that this module bears a free license; without such a declaration, the kernel
complains when the module is loaded.
#include <linux/module.h>
#include <linux/init.h>
所有的可加载的module都需要include这两个头文件. 对于一些需要在加载时候传递参数的module,还需要include "moduleparam.h"
*/
//*******************************************************
2. Makefile
obj-m := hello.o
KERNELDIR := /usr/src/kernels/$(shell uname -r)
PWD := $(shell pwd)
all:
.PHONY: clean
clean:
superuser can use insmod and rmmod to test the module hello.ko
insmod ./hello.ko
rmmod hello