Linux 驱动开发之内核模块开发 (三)—— 模块传参

释放双眼,带上耳机,听听看~!

一、module_param() 定义

         通常在用户态下编程,即
应用程序,可以通过main()的来传递命令行参数,而编写一个
内核模块,则通过module_param() 来传参。

         module_param()宏是Linux 2.6内核中新增的,该宏被定义

include/linux/moduleparam.h文件中,具体定义如下:

#define module_param(name, type, perm)       
module_param_named(name, name, type, perm)

所以我们通过宏module_param()定义一个模块参数:

module_param(name, type, perm);

参数的意义:

**name **    既是用户看到的参数名,又是模块内接受参数的变量; 

**type  **    表示参数的数据类型,是下列之一:byte, short, ushort, int, uint, long, ulong, charp, bool, invbool;     

**perm  **  指定了在sysfs中相应文件的访问权限。访问权限与linux文件访问权限相同的方式管理,如0644,或使用stat.h中的宏如S_IRUGO表示。

              0表示完全关闭在sysfs中相对应的项。

二、module_param() 使用方法

       module_param()宏不会声明变量,因此在使用宏之前,必须声明变量,典型地用法如下:

static unsigned int int_var = 0;   

module_param(int_var, uint, S_IRUGO);

     这些必须写在模块源文件的开头部分。即int_var是全局的。也可以使模块源文件内部的变量名与外部的参数名有不同的名字,通过**module_param_named()**定义。

a — module_param_named()

     
module_param_named(name, variable, type, perm);

**name        **外部(用户空间)可见的参数名;

variable    源文件内部的全局变量名;

**type **           类型

**perm **         权限

而module_param通过module_param_named实现,只不过name与variable相同。

例如:

static unsigned int max_test = 9;

module_param_name(maximum_line_test, max_test, int, 0);

b — 字符串参数

    如果模块参数是一个字符串时,通常使用charp类型定义这个模块参数。内核复制用户提供的字符串到内存,并且相对应的变量指向这个字符串。

例如:

static char *name;

module_param(name, charp, 0);

另一种方法是通过宏module_param_string()让内核把字符串直接复制到程序中的字符数组内。

module_param_string(name, string, len, perm);

这里,name是外部的参数名,string是内部的变量名,len是以string命名的buffer大小(可以小于buffer的大小,但是没有意义),perm表示sysfs的访问权限(或者perm是零,表示完全关闭相对应的sysfs项)。

例如:

static char species[BUF_LEN];

module_param_string(specifies, species, BUF_LEN, 0);

c — 数组参数

数组参数, 用逗号间隔的列表提供的值, 模块加载者也支持. 声明一个数组参数, 使用:

module_param_array(name, type, num, perm);

name  
数组的名子(也是参数名),

type    
数组元素的类型,

**num **   
一个整型变量,

**perm ** 
通常的权限值.

如果数组参数在加载时设置, num被设置成提供的数的个数. 模块加载者拒绝比数组能放下的多的值。

三、使用实例


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
1#include <linux/init.h>
2#include <linux/module.h>
3#include <linux/moduleparam.h>
4MODULE_LICENSE ("GPL");
5
6static char *who = "world";
7static int times = 1;
8module_param (times, int, S_IRUSR);
9module_param (who, charp, S_IRUSR);
10
11static int hello_init (void)
12{
13int i;
14for (i = 0; i < times; i++)
15printk (KERN_ALERT "(%d) hello, %s!\n", i, who);
16return 0;
17}
18
19static void hello_exit (void)
20{
21printk (KERN_ALERT "Goodbye, %s!\n", who);
22}
23
24module_init (hello_init);
25module_exit (hello_exit);
26
27

编译生成可执行文件hello

# insmod hello.ko who="world" times=5


1
2
3
4
5
6
7
8
1#(1) hello, world!
2#(2) hello, world!
3#(3) hello, world!
4#(4) hello, world!
5#(5) hello, world!
6# rmmod hello
7# Goodbye,world!
8

1
2
1**注**:** **
2

a — 如果加载模块hello时,没有输入任何参数,那么who的初始值为"world",times的初始值为1

**b — 同时向指针传递字符串的时候,不能传递这样的字符串 who="hello world!".即字符串中间不能有空格
**

**c –/sys/module/hello/parameters  该目录下生成变量对应的文件节点
**

给TA打赏
共{{data.count}}人
人已打赏
安全运维

WordPress网站专用docker容器环境带Waf

2020-7-18 20:04:44

安全运维

运维安全-Gitlab管理员权限安全思考

2021-9-19 9:16:14

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索