您的位置:首页 > 其它

【内核】:模块参数使用示例

2011-10-12 23:38 274 查看
代码:

#include <linux/module.h>

#include <linux/moduleparam.h> /* Optional, to include module_param() macros */

#include <linux/kernel.h> /* Optional, to include prink() prototype */

#include <linux/init.h> /* Optional, to include module_init() macros */

#include <linux/stat.h> /* Optional, to include S_IRUSR ... */

static int myint = -99;

static char *mystring = "i'm hungry";

static int myintary[]= {1,2,3,4};

static char *mystrary[] = {"apple", "orange", "banana"};

static int nstrs = 3;

module_param(myint, int, S_IRUSR|S_IWUSR);

MODULE_PARM_DESC(myint, "A trial integer");

module_param(mystring, charp, 0);

module_param_array(myintary, int, NULL, 0444);

module_param_array(mystrary, charp, &nstrs, 0664);

static int __init hello_init(void)

{

int i;

printk(KERN_INFO "myint is %d/n", myint);

printk(KERN_INFO "mystring is %s/n", mystring);

printk(KERN_INFO "myintary are");

for(i = 0; i < sizeof(myintary)/sizeof(int); i++)

printk(" %d", myintary[i]);

printk("/n");

printk(KERN_INFO "mystrary are");

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

printk(" %s", mystrary[i]);

printk("/n");

return 0;

}

static void __exit hello_exit(void)

{

}

module_init(hello_init);

module_exit(hello_exit);

运行:

insmod ./hello.ko myint=100 mystring="abc" myintary=-1,-2 mystrary="a","b"

dmesg输出:

myint is 100

mystring is abc

myintary are -1 -2 3 4

mystrary are a b

说明:

module_param() 和 module_param_array() 的作用就是让那些全局变量对 insmod 可见,使模块装载时可重新赋值。

module_param_array() 宏的第三个参数用来记录用户 insmod 时提供的给这个数组的元素个数,NULL 表示不关心用户提供的个数

module_param() 和 module_param_array() 最后一个参数权限值不能包含让普通用户也有写权限,否则编译报错。这点可参考 linux/moduleparam.h 中 __module_param_call() 宏的定义。

字符串数组中的字符串似乎不能包含逗号,否则一个字符串会被解析成两个
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: