Linux最简单内核模块编写
2011-10-09 17:42
459 查看
hello.h
/*
* head file of hello.ko
* location of following files : /usr/src/$(uname -r)/include/
*/
#include<linux/init.h> //module_init(),module_exit()
#include<linux/module.h> //MODULE_AUTHOR(),MODULE_LICENSE()
#include<linux/kernel.h> //KERN_EMERG
hello.c
/*
* source file of hello.ko
* location: /usr/src/$(uname -r)/driver/net/hello/
*/
#include "hello.h" //find head file in current dir
/*
* hello_init : initial function, called when module is installed
* return 0 if succeeded
*/
static int hello_init(void)
{
printk(KERN_EMERG"hello.ko is installed!\n");
return 0;
}
/*
* hello_exit : called when module is uninstalled
*/
static void hello_exit(void)
{
printk(KERN_EMERG"hello.ko is uninstalled!\n");
}
module_init(hello_init);
module_exit(hello_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("zhangsk");
Makefile
obj-m := hello.o
all:
make -C /lib/modules/$(shell uname -r)/build SUBDIRS=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build SUBDIRS=$(PWD) clean
然后在当前文目录中输入: insmod hello.ko
OK, 可以看到屏幕中输出: hello.ko is installed!
/*
* head file of hello.ko
* location of following files : /usr/src/$(uname -r)/include/
*/
#include<linux/init.h> //module_init(),module_exit()
#include<linux/module.h> //MODULE_AUTHOR(),MODULE_LICENSE()
#include<linux/kernel.h> //KERN_EMERG
hello.c
/*
* source file of hello.ko
* location: /usr/src/$(uname -r)/driver/net/hello/
*/
#include "hello.h" //find head file in current dir
/*
* hello_init : initial function, called when module is installed
* return 0 if succeeded
*/
static int hello_init(void)
{
printk(KERN_EMERG"hello.ko is installed!\n");
return 0;
}
/*
* hello_exit : called when module is uninstalled
*/
static void hello_exit(void)
{
printk(KERN_EMERG"hello.ko is uninstalled!\n");
}
module_init(hello_init);
module_exit(hello_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("zhangsk");
Makefile
obj-m := hello.o
all:
make -C /lib/modules/$(shell uname -r)/build SUBDIRS=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build SUBDIRS=$(PWD) clean
然后在当前文目录中输入: insmod hello.ko
OK, 可以看到屏幕中输出: hello.ko is installed!
相关文章推荐
- Linux最简单内核模块编写
- 一个最简单的Linux 2.6内核驱动模块
- Linux:内核模块实现替换系统调用的简单例子
- Linux环境下最简单内核模块的实践
- 【Linux编译内核】编写并添加内核模块
- Linux驱动部分-(一)内核模块的编写
- linux 2.6内核简单驱动编写
- Linux:内核模块实现替换系统调用的简单例子
- Linux编写内核模块及文件读写
- 在编写最简单的linux 内核驱动, 编译后出现的问题以及解决方法。
- 编写简单的内核模块及内核源码下载,内核模块Makefile编写
- 编写简单的内核模块
- 【Linux】【Kernel】一个简单的内核模块例子
- linux 设备驱动开发学习笔记(一):最简单的内核模块
- GDB简单调试linux内核与模块的方法
- 编写、编译、安装、移除一个内核模块的基本过程,不能再简单了的
- Linux 内核模块编写与安装
- GDB简单调试linux内核与模块的方法
- Linux:内核模块实现替换系统调用的简单例子
- OS实习-编写一个简单的内核模块