本文讲述如何编写Linux内核模块,需要两个文件 mytest.c,Makefile。
存放到同一个文件夹目录,然后make、make install,就可以完成内核模块的编译生成和安装。
然后通过dmesg命令就可以看到从模块初始化函数输出的信息。
mytest.c:
#include <linux/module.h>
#include <linux/crypto.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/scatterlist.h>
#include <linux/slab.h>
#include <linux/highmem.h>
static int __init test_init(void) {
printk(KERN_INFO"mytest init\n");
printk(KERN_INFO"ha ha\n");
return 0;
}
static void __exit test_exit(void)
{
printk(KERN_INFO"hw aes test exit\n");
}
module_init(test_init);
module_exit(test_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("feng");
KONAME = mytest.o IFTEST=$(shell lsmod | grep mytest | wc -l) KERNELDIR := /lib/modules/$(shell uname -r)/build PWD :=$(shell pwd) obj-m:=$(KONAME) module: make -C $(KERNELDIR) M=$(PWD) test_install : ifeq ($(IFTEST),1) rmmod mytest insmod mytest.ko else insmod mytest.ko endif install: test_install .PHONY:install clean: rm *.o *.mod.c modules.order Module.symvers .*.cmd *.ko rm -rf .tmp_versions
原文地址:http://blog.csdn.net/fengjingge/article/details/42124041