在我们使用模块的时候需要在装载时传递参数给模块,linux 支持在装载模块的同时传入参数 。
比如 (num 和who就是传入模块的参数):
$> insmod hello.ko num=10 who="jack"
模块参数必须用module_parm宏声明,这个宏定义在<linux/moduleparam.h>,刚才连个参数在模块中的定义如下:
static char *who = "world";
static int num = 1;
module_param(num, int, S_IRUGO);
module_param(who, charp, S_IRUGO);
内核支持的模块参数类型有:
bool
invbool (true变为false, false变为true)
charp 字符指针
int
long
short
uint
ulong
ushort
S_IRUGO是访问许可值,参见<linux/stat.h>中的定义(S_IWUGO ,S_IXUGO 等)。
示例:
//test.c
#include <linux/init.h>
#include <linux/module.h>
MODULE_LICENSE("GPL");
static int count = 1;
module_param(count, int, S_IRUGO);
static char *name = "test";
module_param(name, charp, S_IRUGO);
static __init int a(void)
{
printk("%s %d: count = %d, name = %s\n", __func__, __LINE__, count, name);
return 0;
}
static __exit void b(void)
{
printk("%s %d: count = %d, name = %s\n", __func__, __LINE__, count, name);
}
module_init(a);
module_exit(b);
$> insmod count=10 name=jacky