标准通网站建设,上海新闻发布会,采购网站建设,手机网站是怎么制作的一、module_param() 定义 通常在用户态下编程#xff0c;即应用程序#xff0c;可以通过main()的来传递命令行参数#xff0c;而编写一个内核模块#xff0c;则通过module_param() 来传参。 module_param()宏是Linux 2.6内核中新增的#xff0c;该宏被定义在include/linux… 一、module_param() 定义 通常在用户态下编程即应用程序可以通过main()的来传递命令行参数而编写一个内核模块则通过module_param() 来传参。 module_param()宏是Linux 2.6内核中新增的该宏被定义在include/linux/moduleparam.h文件中具体定义如下 #define module_param(name, type, perm) module_param_named(name, name, type, perm) 所以我们通过宏module_param()定义一个模块参数 module_param(name, type, perm); 参数的意义 name 既是用户看到的参数名又是模块内接受参数的变量 type 表示参数的数据类型是下列之一byte, short, ushort, int, uint, long, ulong, charp, bool, invbool perm 指定了在sysfs中相应文件的访问权限。访问权限与linux文件访问权限相同的方式管理如0644或使用stat.h中的宏如S_IRUGO表示。 0表示完全关闭在sysfs中相对应的项。 二、module_param() 使用方法 module_param()宏不会声明变量因此在使用宏之前必须声明变量典型地用法如下 static unsigned int int_var 0; module_param(int_var, uint, S_IRUGO); 这些必须写在模块源文件的开头部分。即int_var是全局的。也可以使模块源文件内部的变量名与外部的参数名有不同的名字通过module_param_named()定义。 a -- module_param_named() module_param_named(name, variable, type, perm); name 外部(用户空间)可见的参数名 variable 源文件内部的全局变量名 type 类型 perm 权限 而module_param通过module_param_named实现只不过name与variable相同。 例如 static unsigned int max_test 9; module_param_name(maximum_line_test, max_test, int, 0); b -- 字符串参数 如果模块参数是一个字符串时通常使用charp类型定义这个模块参数。内核复制用户提供的字符串到内存并且相对应的变量指向这个字符串。 例如 static char *name; module_param(name, charp, 0); 另一种方法是通过宏module_param_string()让内核把字符串直接复制到程序中的字符数组内。 module_param_string(name, string, len, perm); 这里name是外部的参数名string是内部的变量名len是以string命名的buffer大小可以小于buffer的大小但是没有意义perm表示sysfs的访问权限或者perm是零表示完全关闭相对应的sysfs项。 例如 static char species[BUF_LEN] module_param_string(specifies, species, BUF_LEN, 0); c -- 数组参数 数组参数, 用逗号间隔的列表提供的值, 模块加载者也支持. 声明一个数组参数, 使用: module_param_array(name, type, num, perm); name 数组的名子(也是参数名), type 数组元素的类型, num 一个整型变量, perm 通常的权限值. 如果数组参数在加载时设置, num被设置成提供的数的个数. 模块加载者拒绝比数组能放下的多的值。 三、使用实例 [cpp] view plaincopy #include linux/init.h #include linux/module.h #include linux/moduleparam.h MODULE_LICENSE (GPL); static char *who world; static int times 1; module_param (times, int, S_IRUSR); module_param (who, charp, S_IRUSR); static int hello_init (void) { int i; for (i 0; i times; i) printk (KERN_ALERT (%d) hello, %s!\n, i, who); return 0; } static void hello_exit (void) { printk (KERN_ALERT Goodbye, %s!\n, who); } module_init (hello_init); module_exit (hello_exit); 编译生成可执行文件hello # insmod hello.ko whoworld times5 [cpp] view plaincopy #(1) hello, world! #(2) hello, world! #(3) hello, world! #(4) hello, world! #(5) hello, world! # rmmod hello # Goodbye,world! 注
:a -- 如果加载模块hello时没有输入任何参数那么who的初始值为worldtimes的初始值为1 b -- 同时向指针传递字符串的时候不能传递这样的字符串 whohello world!.即字符串中间不能有空格 c --/sys/module/hello/parameters 该目录下生成变量对应的文件节点