没有合适的资源?快使用搜索试试~ 我知道了~
首页Linux内核模块编程指南2.6:免费开源
Linux内核模块编程指南2.6:免费开源
需积分: 3 1 下载量 124 浏览量
更新于2024-08-02
收藏 540KB PDF 举报
《Linux内核模块编程指南2.6》是由Peter Jay Salzman、Michael Burian和Ori Pomerantz共同编著的一本开源书籍,于2001年由Salzman撰写,后续更新至2.6.4版本。该书在2007年5月18日发布,遵循开放源代码许可证(Open Software License, OSL)v1.1,允许用户在不侵犯版权的前提下自由复制、修改或分发。 本书的目标是提供一个详细的指南,帮助读者理解和学习如何编写Linux内核模块,即可以直接加载到Linux内核中的可动态加载的代码段。它涵盖了从模块的基本结构、编译过程、初始化和卸载机制,到内存管理、系统调用接口、信号处理和错误处理等核心概念。作者鼓励广泛地分享此书,无论是个人学习还是商业用途,只要保留原始版权声明,并遵守OSL协议的规定即可,无需事先得到作者的特别许可。 对于衍生作品和翻译,必须同样按照OSL授权,并确保原始版权信息不受改动。如果读者在书中添加了新的内容,也必须将其置于相同的开放许可之下。这是一本实用且自由的资源,适合Linux开发者深入理解并实践内核模块开发技术。通过阅读和实践这本书,读者能够提升自己的Linux内核编程技能,参与到开源社区的贡献中去。
资源详情
资源推荐
![](https://csdnimg.cn/release/download_crawler_static/1528629/bge.jpg)
printk(KERN_INFO "Hello, world 4\n");
return 0;
}
static void __exit cleanup_hello_4(void)
{
printk(KERN_INFO "Goodbye, world 4\n");
}
module_init(init_hello_4);
module_exit(cleanup_hello_4);
/*
* You can use strings, like this:
*/
/*
* Get rid of taint message by declaring code as GPL.
*/
MODULE_LICENSE("GPL");
/*
* Or with defines, like this:
*/
MODULE_AUTHOR(DRIVER_AUTHOR); /* Who wrote this module? */
MODULE_DESCRIPTION(DRIVER_DESC); /* What does this module do */
/*
* This module uses /dev/testdevice. The MODULE_SUPPORTED_DEVICE macro might
* be used in the future to help automatic configuration of modules, but is
* currently unused other than for documentation purposes.
*/
MODULE_SUPPORTED_DEVICE("testdevice");
2.6. Passing Command Line Arguments to a Module
Modules can take command line arguments, but not with the argc/argv you might be used to.
To allow arguments to be passed to your module, declare the variables that will take the values of the
command line arguments as global and then use the module_param() macro, (defined in
linux/moduleparam.h) to set the mechanism up. At runtime, insmod will fill the variables with any
command line arguments that are given, like ./insmod mymodule.ko myvariable=5. The variable
declarations and macros should be placed at the beginning of the module for clarity. The example code should
clear up my admittedly lousy explanation.
The module_param() macro takes 3 arguments: the name of the variable, its type and permissions for the
corresponding file in sysfs. Integer types can be signed as usual or unsigned. If you'd like to use arrays of
integers or strings see module_param_array() and module_param_string().
int myint = 3;
module_param(myint, int, 0);
Arrays are supported too, but things are a bit different now than they were in the 2.4. days. To keep track of
the number of parameters you need to pass a pointer to a count variable as third parameter. At your option,
you could also ignore the count and pass NULL instead. We show both possibilities here:
The Linux Kernel Module Programming Guide
Chapter 2. Hello World 11
![](https://csdnimg.cn/release/download_crawler_static/1528629/bgf.jpg)
int myintarray[2];
module_param_array(myintarray, int, NULL, 0); /* not interested in count */
int myshortarray[4];
int count;
module_parm_array(myshortarray, short, , 0); /* put count into "count" variable */
A good use for this is to have the module variable's default values set, like an port or IO address. If the
variables contain the default values, then perform autodetection (explained elsewhere). Otherwise, keep the
current value. This will be made clear later on.
Lastly, there's a macro function, MODULE_PARM_DESC(), that is used to document arguments that the
module can take. It takes two parameters: a variable name and a free form string describing that variable.
Example 2−7. hello−5.c
/*
* hello−5.c − Demonstrates command line argument passing to a module.
*/
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/stat.h>
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Peter Jay Salzman");
static short int myshort = 1;
static int myint = 420;
static long int mylong = 9999;
static char *mystring = "blah";
static int myintArray[2] = { −1, −1 };
static int arr_argc = 0;
/*
* module_param(foo, int, 0000)
* The first param is the parameters name
* The second param is it's data type
* The final argument is the permissions bits,
* for exposing parameters in sysfs (if non−zero) at a later stage.
*/
module_param(myshort, short, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
MODULE_PARM_DESC(myshort, "A short integer");
module_param(myint, int, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
MODULE_PARM_DESC(myint, "An integer");
module_param(mylong, long, S_IRUSR);
MODULE_PARM_DESC(mylong, "A long integer");
module_param(mystring, charp, 0000);
MODULE_PARM_DESC(mystring, "A character string");
/*
* module_param_array(name, type, num, perm);
* The first param is the parameter's (in this case the array's) name
* The second param is the data type of the elements of the array
* The third argument is a pointer to the variable that will store the number
* of elements of the array initialized by the user at module loading time
* The fourth argument is the permission bits
The Linux Kernel Module Programming Guide
Chapter 2. Hello World 12
![](https://csdnimg.cn/release/download_crawler_static/1528629/bg10.jpg)
*/
module_param_array(myintArray, int, &arr_argc, 0000);
MODULE_PARM_DESC(myintArray, "An array of integers");
static int __init hello_5_init(void)
{
int i;
printk(KERN_INFO "Hello, world 5\n=============\n");
printk(KERN_INFO "myshort is a short integer: %hd\n", myshort);
printk(KERN_INFO "myint is an integer: %d\n", myint);
printk(KERN_INFO "mylong is a long integer: %ld\n", mylong);
printk(KERN_INFO "mystring is a string: %s\n", mystring);
for (i = 0; i < (sizeof myintArray / sizeof (int)); i++)
{
printk(KERN_INFO "myintArray[%d] = %d\n", i, myintArray[i]);
}
printk(KERN_INFO "got %d arguments for myintArray.\n", arr_argc);
return 0;
}
static void __exit hello_5_exit(void)
{
printk(KERN_INFO "Goodbye, world 5\n");
}
module_init(hello_5_init);
module_exit(hello_5_exit);
I would recommend playing around with this code:
satan# insmod hello−5.ko mystring="bebop" mybyte=255 myintArray=−1
mybyte is an 8 bit integer: 255
myshort is a short integer: 1
myint is an integer: 20
mylong is a long integer: 9999
mystring is a string: bebop
myintArray is −1 and 420
satan# rmmod hello−5
Goodbye, world 5
satan# insmod hello−5.ko mystring="supercalifragilisticexpialidocious" \
> mybyte=256 myintArray=−1,−1
mybyte is an 8 bit integer: 0
myshort is a short integer: 1
myint is an integer: 20
mylong is a long integer: 9999
mystring is a string: supercalifragilisticexpialidocious
myintArray is −1 and −1
satan# rmmod hello−5
Goodbye, world 5
satan# insmod hello−5.ko mylong=hello
hello−5.o: invalid argument syntax for mylong: 'h'
2.7. Modules Spanning Multiple Files
Sometimes it makes sense to divide a kernel module between several source files.
The Linux Kernel Module Programming Guide
Chapter 2. Hello World 13
![](https://csdnimg.cn/release/download_crawler_static/1528629/bg11.jpg)
Here's an example of such a kernel module.
Example 2−8. start.c
/*
* start.c − Illustration of multi filed modules
*/
#include <linux/kernel.h> /* We're doing kernel work */
#include <linux/module.h> /* Specifically, a module */
int init_module(void)
{
printk(KERN_INFO "Hello, world − this is the kernel speaking\n");
return 0;
}
The next file:
Example 2−9. stop.c
/*
* stop.c − Illustration of multi filed modules
*/
#include <linux/kernel.h> /* We're doing kernel work */
#include <linux/module.h> /* Specifically, a module */
void cleanup_module()
{
printk(KERN_INFO "Short is the life of a kernel module\n");
}
And finally, the makefile:
Example 2−10. Makefile
obj−m += hello−1.o
obj−m += hello−2.o
obj−m += hello−3.o
obj−m += hello−4.o
obj−m += hello−5.o
obj−m += startstop.o
startstop−objs := start.o stop.o
all:
make −C /lib/modules/$(shell uname −r)/build M=$(PWD) modules
clean:
make −C /lib/modules/$(shell uname −r)/build M=$(PWD) clean
This is the complete makefile for all the examples we've seen so far. The first five lines are nothing special,
but for the last example we'll need two lines. First we invent an object name for our combined module, second
we tell make what object files are part of that module.
The Linux Kernel Module Programming Guide
Chapter 2. Hello World 14
剩余81页未读,继续阅读
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![text/html](https://img-home.csdnimg.cn/images/20210720083451.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://profile-avatar.csdnimg.cn/0f695de1bee14d149e3bd476cc7897d8_paomadi.jpg!1)
paomadi
- 粉丝: 467
- 资源: 29
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![](https://csdnimg.cn/release/wenkucmsfe/public/img/voice.245cc511.png)
最新资源
- 解决本地连接丢失无法上网的问题
- BIOS报警声音解析:故障原因与解决方法
- 广义均值移动跟踪算法在视频目标跟踪中的应用研究
- C++Builder快捷键大全:高效编程的秘密武器
- 网页制作入门:常用代码详解
- TX2440A开发板网络远程监控系统移植教程:易搭建与通用解决方案
- WebLogic10虚拟内存配置详解与优化技巧
- C#网络编程深度解析:Socket基础与应用
- 掌握Struts1:Java MVC轻量级框架详解
- 20个必备CSS代码段提升Web开发效率
- CSS样式大全:字体、文本、列表样式详解
- Proteus元件库大全:从基础到高级组件
- 74HC08芯片:高速CMOS四输入与门详细资料
- C#获取当前路径的多种方法详解
- 修复MySQL乱码问题:设置字符集为GB2312
- C语言的诞生与演进:从汇编到系统编程的革命
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
![](https://img-home.csdnimg.cn/images/20220527035711.png)
![](https://img-home.csdnimg.cn/images/20220527035711.png)
![](https://img-home.csdnimg.cn/images/20220527035111.png)
安全验证
文档复制为VIP权益,开通VIP直接复制
![](https://csdnimg.cn/release/wenkucmsfe/public/img/green-success.6a4acb44.png)