I'm writing a char device driver for Linux within a module.
I need to use a module parameter array to display some state information about open devices, but this is not working properly. I'm also using an int parameter (not array) and they are initialized like this:
static int open_permissions[3] = {1,2,3};
static int count;
module_param_array(open_permissions, int, &count, 0660);
static int allow_live_bait = 1; /* default to on */
module_param(allow_live_bait, int, 0660); /* a int type */
Now these parameters are listed in the VFS /sys/module/mymodule/parameters
as they should be. If I open the allow_live_bait
parameter with a text editor, it shows me the number 1
correctly, but if I open the open_permissions
parameter (always using a text editor) it does not show anything.
Also, this is not a problem of an incorrect initialization because I tried to print the values of the array in the init_module function like this and they are correctly initialized:
for(j = 0;j<3;j++){
printk("%s : open permission %d : %d",MODNAME,j,open_permissions[j]);
}
What I would like to know is if this is a normal behavior when using array parameters or if I'm doing something wrong.
This is because module_param_array()
allows one of two options in regards to the third argument (nump
i.e. the count of supplied values):
Pass NULL
if you want a parameter array of fixed size. In this way, no matter what the user passes when inserting the module (or modifying the parameter through sysfs), the parameter under sysfs will always report all the values of the array. The user can override from zero up to all of the values. If the user does not pass the parameter, the default values will stay unchanged and you will see sysfs report 1,2,3
. You can see this as the module always considering the entire parameter array "meaningful".
Pass a pointer to an integer for a "dynamically" sized parameter array. The underlying array will still be statically sized, of course, but the semantics are different. The user is now expected to pass up to N (size of the array) parameters, and the number of passed parameters on module insertion (or upon modification through sysfs) is reflected on the count pointer passed to module_param_array()
. The module should only consider indexes from 0
up to count
meaningful, and the count defaults to 0
if the user does not pass the parameter at all.
It seems to me like you coded your module parameter following option #2, and then inserted the module without passing the parameter, meaning that count=0
. What you probably wanted to do was to just go with option #1 instead.