模块参数使用示例
2008-10-26 14:53
197 查看
代码:
#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() 宏的定义。
字符串数组中的字符串似乎不能包含逗号,否则一个字符串会被解析成两个
相关文章推荐
- 【内核】:模块参数使用示例
- .Net环境下使用基于Completion Port的网络通信模块示例(源)
- 开发一个使用upstream的示例模块
- NET用使用存储过程获取输出参数的代码示例!
- .NET用使用存储过程获取输出参数的代码示例!
- python logging 模块完整使用示例
- SPC5607B的eDMA模块使用示例
- python使用ctypes模块调用windowsapi获取系统版本示例
- PHP伪静态路径使用正则过滤重复参数示例--(网站名泉州)
- linux 下 find 的使用参数和技巧 以及使用示例
- .NET用使用存储过程获取输出参数的代码示例!
- 内核子系统或设备驱动可以直接编译到内核,也可以编译成模块,如果编译到内核,可以使用前一节介绍的方法通过内核启动参数来向它们传递参数,如果编译成模块,则可以通过命令行在插入模块时传递参数,或者在运行时,
- Python解析命令行读取参数--argparse模块使用方法
- 内核模块编程---使用内核模块参数(module_param()) (2)
- Redis(Windows安装方法与Java调用实例 & 配置文件参数说明 & Java使用Redis所用Jar包 & Redis与Memcached区别 & redis-cli.exe命令及示例)
- 2 C#串口或TCP远程采集数据 chart图表使用示例 保存数据到access数据库和每日.txt文件并实时显示各参数曲线
- iframe的各项参数整理附说明及使用示例
- Python中atexit模块的基本使用示例
- C语言可变参数使用示例
- 使用GP。tool 创建地图切片示例 参数说明