1. 程式人生 > >linux 核心模組程式設計之模組引數(四)

linux 核心模組程式設計之模組引數(四)

通過巨集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);

他的編譯與執行可以檢視我前面的文章,我這裡就不重複說明了,執行結果如下: