Linux Driver 入门 - parameters- 动态模块传参
Abstract
- What are Module parameters?
- Ways to change these module parameters
- module_param macro
- Array module parameters
- Source code in this blog: https://github.com/yubaoliu/Linux/tree/master/driver
Pass parameters to a module
There are two ways to pass these parameters.
1. While insmod or modprobe
Ex: $insmod <Module.ko> [Module_parameters-1 = value-1] [Module_parameters-2 = value-2]
2. Reading parameter assignment from the configuration file "/etc/modprobe.conf"
module_param
/* * module_param(foo, int, 0000) * The first param is the parameters name * The second param is it's data type * The final argument is the permissions bits, * for exposing parameters in sysfs (if non-zero) at a later stage. */ module_param(myshort, short, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); MODULE_PARM_DESC(myshort, "A short integer"); module_param(myint, int, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); MODULE_PARM_DESC(myint, "An integer"); module_param(mylong, long, S_IRUSR); MODULE_PARM_DESC(mylong, "A long integer"); module_param(mystring, charp, 0000); MODULE_PARM_DESC(mystring, "A character string");S_IRUGO=(S_IRUSR|S_IRGRP|S_IROTH)
S_IRUSR:用户读 00400S_IRGRP:用户组读 00040S_IROTH: 其他读 00004
Example source code:
#include <linux/init.h> #include <linux/module.h> #include <linux/moduleparam.h> int param_var=0; module_param(param_var,int,S_IRUSR|S_IWUSR); void display(void) { printk(KERN_ALERT "TEST:param_var=%d \n", param_var); } static int hello_init(void) { printk(KERN_ALERT "TEST: hello world, this is hello world module\n"); display(); return 0; } static void hello_exit(void) { printk(KERN_ALERT "TEST: Good bye, from hello world module\n"); } module_init(hello_init); module_exit(hello_exit);
Run:
yubao@yubao-ThinkPad-E560:~/MyProjects/Linux/driver/HelloWorldParameter$ sudo insmod hello.ko p aram_var=100
Result: (dmesg)
[20811.755698] TEST: hello world, this is hello world module [20811.755701] TEST:param_var=100
Possible Error:
If you use
void display()
you tend to encounter this error:
warning: function declaration isn’t a prototype.
How to solve:
void display(void)
Pass array parameter
/* * module_param_array(name, type, num, perm); * The first param is the parameter's (in this case the array's) name * The second param is the data type of the elements of the array * The third argument is a pointer to the variable that will store the number * of elements of the array initialized by the user at module loading time * The fourth argument is the permission bits */ module_param_array(myintArray, int, &arr_argc, 0000);
Source:
#include <linux/init.h> #include <linux/module.h> #include <linux/moduleparam.h> int param_var[3]={0,0,0}; module_param_array(param_var,int,NULL,S_IRUSR|S_IWUSR); void display(void) { printk(KERN_ALERT "TEST:param_var[0]=%d \n", param_var[0]); printk(KERN_ALERT "TEST:param_var[1]=%d \n", param_var[1]); printk(KERN_ALERT "TEST:param_var[2]=%d \n", param_var[2]); } static int hello_init(void) { printk(KERN_ALERT "TEST: hello world, this is hello world module\n"); display(); return 0; } static void hello_exit(void) { printk(KERN_ALERT "TEST: Good bye, from hello world module\n"); } module_init(hello_init); module_exit(hello_exit);
Run:
yubao@yubao-ThinkPad-E560:~/MyProjects/Linux/driver/HelloWorldParameter$ sudo insmod hello.ko p aram_var=10,25,111
Result:(dmesg)
[21586.818714] TEST: hello world, this is hello world module [21586.818718] TEST:param_var[0]=10 [21586.818719] TEST:param_var[1]=25 [21586.818720] TEST:param_var[2]=111
Example:
/* * moduleParameters.c - Demonstrates command line argument passing to a module. */ #include <linux/module.h> #include <linux/moduleparam.h> #include <linux/kernel.h> #include <linux/init.h> #include <linux/stat.h> MODULE_LICENSE("GPL"); static short int myshort = 1; static int myint = 420; static long int mylong = 9999; static char *mystring = "blah"; static int myintArray[2] = { -1, -1 }; static int arr_argc = 0; /* * module_param(foo, int, 0000) * The first param is the parameters name * The second param is it's data type * The final argument is the permissions bits, * for exposing parameters in sysfs (if non-zero) at a later stage. */ module_param(myshort, short, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); MODULE_PARM_DESC(myshort, "A short integer"); module_param(myint, int, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); MODULE_PARM_DESC(myint, "An integer"); module_param(mylong, long, S_IRUSR); MODULE_PARM_DESC(mylong, "A long integer"); module_param(mystring, charp, 0000); MODULE_PARM_DESC(mystring, "A character string"); /* * module_param_array(name, type, num, perm); * The first param is the parameter's (in this case the array's) name * The second param is the data type of the elements of the array * The third argument is a pointer to the variable that will store the number * of elements of the array initialized by the user at module loading time * The fourth argument is the permission bits */ module_param_array(myintArray, int, &arr_argc, 0000); MODULE_PARM_DESC(myintArray, "An array of integers"); int init_module (void) { int i; printk(KERN_INFO "Module Parameters:=============\n"); printk(KERN_INFO "myshort is a short integer: %hd\n", myshort); printk(KERN_INFO "myint is an integer: %d\n", myint); printk(KERN_INFO "mylong is a long integer: %ld\n", mylong); printk(KERN_INFO "mystring is a string: %s\n", mystring); for (i = 0; i < (sizeof myintArray / sizeof (int)); i++) { printk(KERN_INFO "myintArray[%d] = %d\n", i, myintArray[i]); } printk(KERN_INFO "got %d arguments for myintArray.\n", arr_argc); return 0; } void cleanup_module (void) { printk(KERN_INFO "moduleParameters finished.\n"); }
Result:
yubao@yubao-ThinkPad-E560:~/MyProjects/Linux/driver/parameters$ sudo dmesg -c
yubao@yubao-ThinkPad-E560:~/MyProjects/Linux/driver/parameters$ sudo insmod moduleParameters.ko myshort=3 myint=100 myintArray=25,67 mylong=888 mystring="yubao"
yubao@yubao-ThinkPad-E560:~/MyProjects/Linux/driver/parameters$ dmesg
[31153.421469] Module Parameters:=============
[31153.421475] myshort is a short integer: 3
[31153.421478] myint is an integer: 100
[31153.421481] mylong is a long integer: 888
[31153.421484] mystring is a string: yubao
[31153.421488] myintArray[0] = 25
[31153.421492] myintArray[1] = 67
[31153.421495] got 2 arguments for myintArray.
yubao@yubao-ThinkPad-E560:~/MyProjects/Linux/driver/parameters$ sudo rmmod moduleParameters
yubao@yubao-ThinkPad-E560:~/MyProjects/Linux/driver/parameters$ dmesg
[31153.421469] Module Parameters:=============
[31153.421475] myshort is a short integer: 3
[31153.421478] myint is an integer: 100
[31153.421481] mylong is a long integer: 888
[31153.421484] mystring is a string: yubao
[31153.421488] myintArray[0] = 25
[31153.421492] myintArray[1] = 67
[31153.421495] got 2 arguments for myintArray.
[31167.661870] moduleParameters finished.
References:
- Passing Command Line Arguments to a Module, https://www.tldp.org/LDP/lkmpg/2.6/html/x323.html
-
Use of Module Parameters in Kernel Programming, http://linuxkernel51.blogspot.com/2011/03/use-of-module-parameters-in-kernel.html
- moduleParameters, https://www.cs.bham.ac.uk/~exr/lectures/systems/09_10/examples/moduleParameters.c