将字符串数组作为参数传递给linux内核模块

时间:2019-03-21 14:59:16

标签: c linux-kernel kernel-module

有什么方法可以将字符串数组传递给内核模块吗? 我想像这样通过它:

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");

1 个答案:

答案 0 :(得分:0)

module_param_array(array,charp,&number_of_elements,0644);期望arraychar *的数组。您只需将static char array[5][10];替换为static char *array[5];

正常的命令外壳(例如/ bin / sh)会将"string1","string2","string3"视为单个参数(假设您并没有弄乱外壳的IFS变量)。内核的模块参数解析器会将其视为单个参数:string1,string2,string3,并使用逗号将其拆分为三个以空值终止的字符串。您的char *array[5]内容将使用指向这些以null终止的字符串的指针填充,而您的number_of_elements将被设置为逗号分隔的字符串数。

相关问题