有没有办法将字符串数组传递给内核模块?我想像这样传递它:
insmod mod.ko array="string1","string2","string3"
有我的代码,但它没有编译:
#include<linux/module.h>
#include<linux/moduleparam.h>
static int number_of_elements = 0;
static char array[5][10];
module_param_array(array,charp,&number_of_elements,0644);
static int __init mod_init(void)
{
int i;
for(i=0; i<number_of_elements;i++)
{
pr_notice("%s\n",array[i]);
}
return 0;
}
static void __exit mod_exit(void)
{
pr_notice("End\n");
}
module_init(mod_init);
module_exit(mod_exit);
MODULE_AUTHOR("...");
MODULE_LICENSE("GPL");
MODULE_VERSION("1.0");
module_param_array(array,charp,&number_of_elements,0644);
希望array
成为一系列char *
。你只需要用static char array[5][10];
替换static char *array[5];
像/ bin / sh这样的普通命令shell会将"string1","string2","string3"
视为单个参数(假设您没有使用shell的IFS
变量)。内核的模块参数解析器将其视为单个参数:string1,string2,string3
并使用逗号将其拆分为三个以null结尾的字符串。你的char *array[5]
内容将填入指向这些以null结尾的字符串的指针,你的number_of_elements
将被设置为以逗号分隔的字符串数。