一、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被设置成提供的数的个数. 模块加载者拒绝比数组能放下的多的值。


三、使用实例

#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 who="world" times=5

#(1) hello, world!
#(2) hello, world!
#(3) hello, world!
#(4) hello, world!
#(5) hello, world!
# rmmod hello
# Goodbye,world!

:

a -- 如果加载模块hello时,没有输入任何参数,那么who的初始值为"world",times的初始值为1

 

b -- 同时向指针传递字符串的时候,不能传递这样的字符串who="hello world!".即字符串中间不能有空格

c --/sys/module/hello/parameters 该目录下生成变量对应的文件节点

更多相关文章

  1. 使用Bash编写Linux Shell脚本-9. 参数和子壳
  2. linux-参数-argparse模块-(未完待续)
  3. 在PreparedStatement中重用参数?
  4. LINUX下用SHELL脚本执行带输入输出参数的ORACLE存储过程并得到结
  5. C#中操作Oracle时的SQL语句参数的用法
  6. postgresql 数组 多了引号 空格处理
  7. 彻底理解初始化参数SERVICE_NAMES和客户端TNS中的SERVICE_NAME
  8. sql 存储过程参数为空则不作为条件
  9. mysql参数优化辅助工具之tuning-primer.sh

随机推荐

  1. 《Android的设计与实现:卷I》迷你书
  2. Android消息处理两大利器:Handler and Loo
  3. [Android]Android端ORM框架——RapidORM(
  4. iOS开发-Android 录制的mp4视频文件在IOS
  5. android与pc的故事
  6. 谈谈Android个人开发者的现状
  7. AndroidStudio快捷键设置2
  8. android activy加载
  9. Android(安卓)emulated sdcard
  10. Android内存溢出