linux 内核模块编程之模块参数(四)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/gs1069405343/article/details/50465035

通过宏module_param指定模块参数,模块参数用于在加载模块时传递给模块。

module_param(name, type, perm)

name是模块参数的名字

type是这个参数的类型,常见值:boolintcharp(字符串型)

perm是模块参数的访问权限

perm常见值:

S_IRUGO:任何用户都对/sys/module中出现的该参数具有读权限

S_IWUSR:允许root用户修改/sys/module中出现的该参数 

例如:

int a =3;//可初始化,也可不用

char *st:

module_param(a,int,S_IRUGO);

module_param(st,charp,S_IRUGO);


示例代码如下:

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

MODULE_LICENSE("GPL");

static char *name = "David Xie";
static int age = 30;

module_param(age, int, S_IRUGO);
module_param(name, charp, S_IRUGO);
static int hello_init()
{
    printk(KERN_EMERG"name:[%s]\n",name);
    printk(KERN_EMERG"age:[%d]\n",age);
    return 0;
}

static void hello_exit()
{
    printk(KERN_INFO"module exit\n");
}

module_init(hello_init);
module_exit(hello_exit);

他的编译与运行可以查看我前面的文章,我这里就不重复说明了,执行结果如下:













猜你喜欢

转载自blog.csdn.net/gs1069405343/article/details/50465035