模块可以接受命令行参数,但是不是使用argc/argv。
为了让参数传递到模块中,声明变量来存放命令行参数作为全局变量,然后使用module_param()宏来建立该机制,该宏定义在linux/moduleprarm.h中。在运行时,insmod将使用任何的命令行参数,像insmod mymodule.ko myvariable=5来填充参数。为清楚起见,变量声明和宏应该放在模块的开头,下例代码清晰地展示了模块接受命令行参数的用法
module_param()宏接受3个参数:变量名,变量类型和sysfs中相应文件的权限。整型可以是普通整型或无符号整型。若要使用整型数组或字符串,则需要参考module_param_array()和module_param_string()。
int myint = 3; module_param(myint, int, 0);
int myintarray[2]; module_param_array(myintarray, int, NULL, 0); // not interested in count int myshortarray[4]; int count; module_param_array(myshortarray, short, , 0); // put count int "count" variable
以下是示例:
/* * hello-4.c - Demonstrates command line argument passing to a module. */ #include <linux/module.h> // Needed by all modules #include <linux/kernel.h> // Needed for KERN_INFO #include <linux/init.h> // Needed for the macros #include <linux/moduleparam.h> #include <linux/stat.h> #define DRIVER_AUTHOR "Fantasy<@gmail.com>" #define DRIVER_DESC "A simple driver" MODULE_LICENSE("GPL"); MODULE_AUTHOR(DRIVER_AUTHOR); MODULE_DESCRIPTION(DRIVER_DESC); MODULE_SUPPORTED_DEVICE("testdevice"); 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_IRGRP); 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 parameters'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"); static int __init hello_5_init(void) { int i; printk(KERN_INFO "Hello, World 5\n====================\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; } static void __exit hello_5_exit(void) { printk(KERN_INFO "Goodbye, world 5\n"); } module_init(hello_5_init); module_exit(hello_5_exit);
insmod hello_5.ko mystring="Fantasy" myshort=25 myintarray=20输出为(通过vim /var/log/syslog查看):
输入如下命令时:
insmod hello_5.ko mystring="Fantasy" myshort=25 mylong=10000 myint=100 myintarray=20,30输出为:
以上示例运行的Linux为:
root@ubuntu:/home/administrator/LKMP/ch2/hello_5# uname -a Linux ubuntu 3.8.0-29-generic #42~precise1-Ubuntu SMP Wed Aug 14 16:19:23 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux