现在的位置: 首页 > 综合 > 正文

模块参数

2013年10月26日 ⁄ 综合 ⁄ 共 933字 ⁄ 字号 评论关闭
/*带模块参数的驱动模块
  使用(insmod hello.ko howmany=n whom="str" )执行的话,可将n和"str"传给模块*/

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

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

MODULE_LICENSE("Dual BSD/GPL");//告诉内核,该模块才用了自由许可证,否则转载时会遭到内核抱怨

static int hello_init(void)//装载时运行
{
    int i;
    for(i=0;i<howmany;i++)
        printk(KERN_ALERT "hello %s./n",whom);//printk()内核态的输出函数;KERN_ALERT,显示的优先级
    return 0;
}

static void hello_exit(void)//卸载时运行
{
    printk(KERN_ALERT "Goodbye,cruel world. My first drivers is over!/n");
}

module_init(hello_init);    //指定装载时调用的函数
module_exit(hello_exit);    //指定卸载时调用的函数

//注意代码中的修改,增加了两个静态全局变量,howmany和whom,分别声明为命令行参数,这样的话,在加载的时候通过命令行穿进来参数了。
为了让参数对insmod可见,参数必须使用module_param宏声明, module_param宏的使用方法如下:
module_param(name, type, perm);  //name是参数名称,type是参数类型,perm是参数访问权限。
module_param_array(name, type, num, perm); //可以声明一个数组,name是数组名,type是数组元素的类型,num是数组元素个数,perm还是权限。
上面的例子中S_IRUGO表示主,组,其它用户都可写。

抱歉!评论已关闭.