请输入您要查询的百科知识:

 

词条 module_param
释义

module_param

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

参数用 module_param 宏定义来声明, 它定义在 moduleparam.h.

module_param(name,type,perm);

module_param 使用了 3 个参数: 变量名, 它的类型, 以及一个权限掩码用来做一个辅助的 sysfs 入口(啥意思). 这个宏定义应当放在任何函数之外, 典型地是出现在源文件的前面.定义如:

static char *whom = "world";

static int howmany = 1;

module_param(howmany, int, S_IRUGO);

module_param(whom, charp, S_IRUGO);

模块参数支持许多类型:

bool

invbool

一个布尔型( true 或者 false)值(相关的变量应当是 int 类型). invbool 类型颠倒了值, 所以真值变成 false, 反之亦然.

charp

一个字符指针值. 内存为用户提供的字串分配, 指针因此设置.

int

long

short

uint

ulong

ushort

基本的变长整型值. 以 u 开头的是无符号值.

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

module_param_array(name,type,num,perm);

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

type 是数组元素的类型,

num 是一个整型变量,

perm 是通常的权限值.

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

perm参数的作用是什么?

最后的 module_param 字段是一个权限值; 你应当使用 <linux/stat.h> 中定义的值. 这个值控制谁可以存取这些模块参数在 sysfs 中的表示.如果 perm 被设为 0, 就根本没有 sysfs 项. 否则, 它出现在 /sys/module下面, 带有给定的权限. 使用 S_IRUGO 作为参数可以被所有人读取, 但是不能改变; S_IRUGO|S_IWUSR 允许 root 来改变参数. 注意, 如果一个参数被 sysfs 修改, 你的模块看到的参数值也改变了, 但是你的模块没有任何其他的通知. 你应当不要使模块参数可写, 除非你准备好检测这个改变并且因而作出反应.

测试模块,源程序hello.c内容如下:

#include <linux/init.h>

#include <linux/module.h>

#include <linux/moduleparam.h>

MODULE_LICENSE("Dual BSD/GPL");

static char *who= "world";

static int times = 1;

module_param(times,int,S_IRUSR);

module_param(who,charp,S_IRUSR);

static int hello_init(void)

{

int i;

for(i=0;i<times;i++)

printk(KERN_ALERT "(%d) hello, %s!\",i,who);

return 0;

}

static void hello_exit(void)

{

printk(KERN_ALERT"Goodbye, %s!\",who);

}

module_init(hello_init);

module_exit(hello_exit);

编译生成可执行文件hello

插入:

# insmod hello who="world" times=5

出现5次"hello,world!":

#(1)hello,world!

#(2)hello,world!

#(3)hello,world!

#(4)hello,world!

#(5)hello,world!

卸载:

# rmmod hello

出现:

#Goodbye,world!

随便看

 

百科全书收录4421916条中文百科知识,基本涵盖了大多数领域的百科知识,是一部内容开放、自由的电子版百科全书。

 

Copyright © 2004-2023 Cnenc.net All Rights Reserved
更新时间:2025/3/1 15:06:31