1

is there any way to pass array of strings to kernel-module? I would like to pass it like this:

insmod mod.ko array="string1","string2","string3"

There is my code but it is not compiling:

#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");
3
  • Not sure if there is a way, but of course you can always do insmod mod.ko array="string1,string2,string3" and split the string in your module code Commented Mar 21, 2019 at 15:04
  • You need an array of char *: static char *array[5];. Commented Mar 21, 2019 at 18:21
  • 1
    @Ctx That's effectively what happens in the kernel's array parameter parser anyway. It simply splits the parameter value into comma-separated chunks. (This means you cannot include a comma within one of the comma-separated strings, because there is no "escape" mechanism.) Commented Mar 21, 2019 at 19:04

1 Answer 1

1

module_param_array(array,charp,&number_of_elements,0644); expects array to be an array of char *. You simply need to replace static char array[5][10]; with static char *array[5];

A normal command shell such as /bin/sh will treat "string1","string2","string3" as a single parameter (assuming you haven't been messing around with the shell's IFS variable). The kernel's module parameter parser will see it as a single parameter: string1,string2,string3 and use the commas to split it into three null-terminated strings. Your char *array[5] contents will be filled in with the pointers to these null-terminated strings and your number_of_elements will be set to the number of comma-separated strings.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.