从命令行传递参数给内核模块

模块也可以从命令行获取参数。但不是通过以前你习惯的argc/argv

要传递参数给模块,首先将获取参数值的变量声明为全局变量。然后使用宏MODULE_PARM()(在头文件linux/module.h)。运行时,insmod将给变量赋予命令行的参数,如同 ./insmod mymodule.o myvariable=5。为使代码清晰,变量的声明和宏都应该放在 模块代码的开始部分。以下的代码范例也许将比我公认差劲的解说更好。

MODULE_PARM()需要两个参数,变量的名字和其类型。支持的类型有" b": 比特型,"h": 短整型, "i": 整数型," l: 长整型和 "s": 字符串型,其中正数型既可为signed也可为unsigned。 字符串类型应该声明为"char *"这样insmod就可以为它们分配内存空间。你应该总是为你的变量赋初值。 这是内核编程,代码要编写的十分谨慎。举个例子:

int myint = 3;
char *mystr;

MODULE_PARM(myint, "i");
MODULE_PARM(mystr, "s");

数组同样被支持。在宏MODULE_PARM中在类型符号前面的整型值意味着一个指定了最大长度的数组。 用'-'隔开的两个数字则分别意味着最小和最大长度。下面的例子中,就声明了一个最小长度为2,最大长度为4的整形数组。

int myshortArray[4];
MODULE_PARM (myintArray, "3-9i");

将初始值设为缺省使用的IO端口或IO内存是一个不错的作法。如果这些变量有缺省值,则可以进行自动设备检测, 否则保持当前设置的值。我们将在后续章节解释清楚相关内容。在这里我只是演示如何向一个模块传递参数。

最后,还有这样一个宏,MODULE_PARM_DESC()被用来注解该模块可以接收的参数。该宏 两个参数:变量名和一个格式自由的对该变量的描述。

Example 2-7. hello-5.c

/*
* hello-5.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");
MODULE_AUTHOR("Peter Jay Salzman");

static short int myshort = 1;
static int myint = 420;
static long int mylong = 9999;
static char *mystring = "blah";

/*
* 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");

static int __init hello_5_init(void)
{
printk(KERN_ALERT "Hello, world 5/n=============/n");
printk(KERN_ALERT "myshort is a short integer: %hd/n", myshort);
printk(KERN_ALERT "myint is an integer: %d/n", myint);
printk(KERN_ALERT "mylong is a long integer: %ld/n", mylong);
printk(KERN_ALERT "mystring is a string: %s/n", mystring);
return 0;
}

static void __exit hello_5_exit(void)
{
printk(KERN_ALERT "Goodbye, world 5/n");
}

module_init(hello_5_init);
module_exit(hello_5_exit);

我建议用下面的方法实验你的模块:

satan# insmod hello-5.o mystring="bebop" mybyte=255 myintArray=-1
mybyte is an 8 bit integer: 255
myshort is a short integer: 1
myint is an integer: 20
mylong is a long integer: 9999
mystring is a string: bebop
myintArray is -1 and 420

satan# rmmod hello-5
Goodbye, world 5

satan# insmod hello-5.o mystring="supercalifragilisticexpialidocious" /
> mybyte=256 myintArray=-1,-1
mybyte is an 8 bit integer: 0
myshort is a short integer: 1
myint is an integer: 20
mylong is a long integer: 9999
mystring is a string: supercalifragilisticexpialidocious
myintArray is -1 and -1

satan# rmmod hello-5
Goodbye, world 5

satan# insmod hello-5.o mylong=hello
hello-5.o: invalid argument syntax for mylong: 'h'

module_param(name, type, perm)是一个宏,向当前模块传入参数,对源码分析如下

在include/linux/moduleparam.h中

#define module_param(name, type, perm) /
module_param_named(name, name, type, perm)

#define module_param_named(name, value, type, perm) /
param_check_##type(name, &(value)); /
module_param_call(name, param_set_##type, param_get_##type, &value, perm); /
__MODULE_PARM_TYPE(name, #type)



#define module_param_call(name, set, get, arg, perm) /
__module_param_call(MODULE_PARAM_PREFIX, name, set, get, arg, perm)


#define __module_param_call(prefix, name, set, get, arg, perm) /
/* Default value instead of permissions? */ /
static int __param_perm_check_##name __attribute__((unused)) = /
BUILD_BUG_ON_ZERO((perm) < 0 || (perm) > 0777 || ((perm) & 2)); /
static char __param_str_##name[] = prefix #name; /
static struct kernel_param const __param_##name /
__attribute_used__ /
__attribute__ ((unused,__section__ ("__param"),aligned(sizeof(void *)))) /
= { __param_str_##name, perm, set, get, arg }



__attibute__ 是gcc的关键字,可参考
http://gcc.gnu.org/onlinedocs/gcc-4.0.0/gcc/Variable-Attributes.html
__attibute__将参数编译到__param段中,

module_param是一步一步展开的,
上面几个宏在include/linux/moduleparam.h中的顺序刚好相反

module_param宏的类似函数调用的顺序
module_param->module_param_named->module_param_call->__module_param_call

展开的顺序正好相反
__module_param_call->module_param_call->module_param_named->module_param



type类型可以是byte,short,ushort,int,
uint,long,ulong,charp(注:字符指针),bool,invbool,
perm表示此参数在sysfs文件系统中所对应的文件节点的属性。
权限在include/linux/stat.h中有定义
比如:
#define S_IRWXU 00700
#define S_IRUSR 00400
#define S_IWUSR 00200
#define S_IXUSR 00100

#define S_IRWXG 00070
#define S_IRGRP 00040
#define S_IWGRP 00020
#define S_IXGRP 00010

#define S_IRWXO 00007
#define S_IROTH 00004
#define S_IWOTH 00002
#define S_IXOTH 00001

当perm为0时,表示此参数不存在 sysfs文件系统下对应的文件节点。
模块被加载后,在/sys/module/ 目录下将出现以此模块名命名的目录。


测试一下,插入一个驱动模块mp.ko,
sudo insmod mp.ko
发现 /sys/module下出现mp这个文件夹
mp中有drivers/,sections/,parameters/,initstate,refcnt,srcversion

其中initstate 里面内容为:live
refcnt里面内容为:0
srcversion里面内容为:F9BDEBF706329B443C28E08,很长,应该是一个唯一数字
driver文件夹里面是空的
sections有__param,__versions
__param里面内容为0xd081e0a8
__versions里面内容为0xd081e100

parameters有count,info(注:自己定义的参数)
count里面的内容为1(注:输出次数)
info里面的内容为a site about linux driver.(注:输出内容)


如果此模块存在perm不为0的命令行参数,
在此模块的目录下将出现parameters目录,
包含一系列以参数名命名的文件节点,
这些文件的权限值等于perm,文件的内容为参数的值。


在/sys/module/mp/出现的参数,在module结构中的对应如下
struct module
{
enum module_state state;

/* Member of list of modules */
struct list_head list;

......

/* Sysfs stuff. */
struct module_kobject mkobj;
struct module_param_attrs *param_attrs;
struct module_attribute *modinfo_attrs;
const char *version;
const char *srcversion;
struct kobject *drivers_dir;

/* Exported symbols */
const struct kernel_symbol *syms;
unsigned int num_syms;
const unsigned long *crcs;

......

/* Section attributes */
struct module_sect_attrs *sect_attrs;
#endif

......
};


enum module_state
{
MODULE_STATE_LIVE,
MODULE_STATE_COMING,
MODULE_STATE_GOING,
};

state对应/sys/module/mp/initstate
drivers_dir对应/sys/module/mp/driver文件夹
version对应/sys/module/mp/sections/__version
srcversion对应/sys/module/mp/srcversion


测试模块,源程序mp.c内容如下:


#include <linux/module.h>
#include <linux/moduleparam.h>

static char *info= "a site about linux driver.";
static int count= 1;
static int mp= 0;

module_param(mp,int,0);

module_param(count,int,S_IRUSR);

module_param(info,charp,S_IRUSR);

MODULE_AUTHOR("ioctrl");
MODULE_LICENSE("Dual BSD/GPL");

static int hello_init(void)
{
int i;
for(i=0;i<count;i++)
{
printk(KERN_NOTICEwww.linuxdriver.cn is ");
printk(info);
printk("/n");
}
return 0;
}

static void hello_exit(void)
{
printk(KERN_NOTICE"exit!/n");
}

module_init(hello_init);
module_exit(hello_exit);

编译

手工插入,插入时传递参数 count=10,info="驱动开发网站。"
命令如下:
sudo insmod mp.ko count=10 info="驱动开发网站。"
然后用dmesg查看消息
看到如下,共10条记录
[16122.280000]www.linuxdriver.cn is 驱动开发网站
[16122.280000]www.linuxdriver.cn is 驱动开发网站
[16122.280000]www.linuxdriver.cn is 驱动开发网站
[16122.280000]www.linuxdriver.cn is 驱动开发网站
[16122.280000]www.linuxdriver.cn is 驱动开发网站
[16122.280000]www.linuxdriver.cn is 驱动开发网站
[16122.280000]www.linuxdriver.cn is 驱动开发网站
[16122.280000]www.linuxdriver.cn is 驱动开发网站
[16122.280000]www.linuxdriver.cn is 驱动开发网站
[16122.280000]www.linuxdriver.cn is 驱动开发网站


 

 

==============================================================

===============================================================

在用户态下编程可以通过main()的来传递命令行参数,而编写一个内核模块则通过module_param() 

参数用 moudle_param 宏定义来声明, 它定义在 moduleparam.h.

module_param(name,type,perm);

module_param 使用了 3 个参数: 变量名, 它的类型, 以及一个权限掩码用来做一个辅助的 sysfs 入口(啥意思). 这个宏定义应当放在任何函数之外, 典型地是出现在源文件的前面.定义如:

static char *whom = "world";
static int howmany = 1;
module_param(howmany, int, S_IRUGO);
module_param(whom, charp, S_IRUGO);

模块参数支持许多类型:

bool
invbool 一个布尔型( true 或者 false)值(相关的变量应当是 int 类型). invbool 类型颠倒了值, 所以真值变成 false, 反之亦然. charp 一个字符指针值. 内存为用户提供的字串分配, 指针因此设置. int
long
short
uint
ulong
ushort 基本的变长整型值. 以 u 开头的是无符号值. 数组参数, 用逗号间隔的列表提供的值, 模块加载者也支持. 声明一个数组参数, 使用: module_param_array(name,type,num,perm);
这里 name 是你的数组的名子(也是参数名),  type 是数组元素的类型, num 是一个整型变量,  perm 是通常的权限值.  如果数组参数在加载时设置, num 被设置成提供的数的个数. 模块加载者拒绝比数组能放下的多的值.

perm参数的作用是什么?

最后的 module_param 字段是一个权限值; 你应当使用 <linux/stat.h> 中定义的值. 这个值控制谁可以存取这些模块参数在 sysfs 中的表示.如果 perm 被设为 0, 就根本没有 sysfs 项. 否则, 它出现在 /sys/module下面, 带有给定的权限. 使用 S_IRUGO 作为参数可以被所有人读取, 但是不能改变; S_IRUGO|S_IWUSR 允许 root 来改变参数. 注意, 如果一个参数被 sysfs 修改, 你的模块看到的参数值也改变了, 但是你的模块没有任何其他的通知. 你应当不要使模块参数可写, 除非你准备好检测这个改变并且因而作出反应.

怎么传入一个自定义的类型?

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
资源包主要包含以下内容: ASP项目源码:每个资源包中都包含完整的ASP项目源码,这些源码采用了经典的ASP技术开发,结构清晰、注释详细,帮助用户轻松理解整个项目的逻辑和实现方式。通过这些源码,用户可以学习到ASP的基本语法、服务器端脚本编写方法、数据库操作、用户权限管理等关键技术。 数据库设计文件:为了方便用户更好地理解系统的后台逻辑,每个项目中都附带了完整的数据库设计文件。这些文件通常包括数据库结构图、数据表设计文档,以及示例数据SQL脚本。用户可以通过这些文件快速搭建项目所需的数据库环境,并了解各个数据表之间的关系和作用。 详细的开发文档:每个资源包都附有详细的开发文档,文档内容包括项目背景介绍、功能模块说明、系统流程图、用户界面设计以及关键代码解析等。这些文档为用户提供了深入的学习材料,使得即便是从零开始的开发者也能逐步掌握项目开发的全过程。 项目演示与使用指南:为帮助用户更好地理解和使用这些ASP项目,每个资源包中都包含项目的演示文件和使用指南。演示文件通常以视频或图文形式展示项目的主要功能和操作流程,使用指南则详细说明了如何配置开发环境、部署项目以及常见问题的解决方法。 毕业设计参考:对于正在准备毕业设计的学生来说,这些资源包是绝佳的参考材料。每个项目不仅功能完善、结构清晰,还符合常见的毕业设计要求和标准。通过这些项目,学生可以学习到如何从零开始构建一个完整的Web系统,并积累丰富的项目经验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值