【内核】:模块参数使用示例
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() 宏的定义。
字符串数组中的字符串似乎不能包含逗号,否则一个字符串会被解析成两个
#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() 宏的定义。
字符串数组中的字符串似乎不能包含逗号,否则一个字符串会被解析成两个
相关文章推荐
- 内核模块编程---使用内核模块参数(module_param()) (2)
- 内核模块参数示例
- 使用module_param来向内核模块传递参数
- 使用module_param来向内核模块传递参数
- 内核模块参数使用
- 内核子系统或设备驱动可以直接编译到内核,也可以编译成模块,如果编译到内核,可以使用前一节介绍的方法通过内核启动参数来向它们传递参数,如果编译成模块,则可以通过命令行在插入模块时传递参数,或者在运行时,
- 使用module_param来向内核模块传递参数
- 使用module_param来向内核模块传递参数
- 模块参数使用示例
- 怎样根据使用环境需求来选择不同参数的光模块
- python使用fcntl模块实现程序加锁功能示例
- 内核模块中函数的导出和使用
- python使用os模块的os.walk遍历文件夹示例
- linux 下 find 的使用参数和技巧 以及使用示例
- .NET用使用存储过程获取输出参数的代码示例!
- Intent的参数设置与使用示例 ,实际开发中不可少
- 关于使用HttpModule模块处理登录验证示例
- insmod 内核模块参数传递
- ##########(python 解析参数方法 可用) Python optionParser模块的使用方法 #######
- 从给内核单独编译设备驱动模块,到使用模块