Linux 核心 給模組傳遞引數

2021-06-07 14:37:16 字數 3416 閱讀 5958

對於如何向模組傳遞引數,linux

kernel 提供了乙個簡單的框架。其允許驅動程式宣告引數,並且使用者在系統

啟動或模組裝載時為引數指定相應值,在驅動程式裡,引數的用法如同全域性變數。

使用下面的巨集時需要包含標頭檔案/moduleparam.h>。

通過巨集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中相對應的項。

這些巨集不會宣告變數,因此在使用巨集之前,必須宣告變數,典型地用法如下:

static unsigned int int_var = 0;   

module_param(int_var, uint, s_irugo);

這些必須寫在模組原始檔的開頭部分。即int_var是全域性的。也可以使模組原始檔內部的變 量名與外部的引數名有不同的名字,通過module_param_named()定義。 module_param_named(name, variable, type, perm);其中name是外部可見的引數名,variable是 原始檔內部的全域性變數名,而module_param通過module_param_named實現,只不過name與variable相同。

例如:static unsigned int max_test = 9;

module_param_name(maximum_line_test, max_test, int, 0);

如果模組引數是乙個字串時,通常使用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);

如果需要傳遞多個引數可以通過巨集module_param_array()實現。 

module_param_array(name, type, nump, perm);

其中,name既是外部模組的引數名又是程式內部的變數名,type是資料型別,perm是sysfs的訪問許可權。指標nump指向乙個整數,其值表示有多少個引數存放在陣列name中。值得注意是name陣列必須靜態分配。

例如:static int finsh[max_fish];

static int nr_fish;

module_param_array(fish, int, &nr_fish, 0444); //最終傳遞陣列元素個數存在nr_fish中

通過巨集module_param_array_named()使得內部的陣列名與外部的引數名有不同的名字。

例如:module_param_array_named(name, array, type, nump, perm);

通過巨集module_parm_desc()對引數進行說明:

static unsigned short size = 1;

module_param(size, ushort, 0644);

module_parm_desc(size, 「the size in inches of the fishing pole」

「connected to this computer.」 );

說明:from  

module_param() 和 module_param_array() 的作用就是讓那些全域性變數對 insmod 可見,使模組裝載時可重新賦值。

module_param_array() 巨集的第三個引數用來記錄使用者 insmod 時提供的給這個陣列的元素個數,null 表示不關心使用者提供的個數

module_param() 和 module_param_array() 最後乙個引數許可權值不能包含讓普通使用者也有寫許可權,否則編譯報錯。這點可參考 linux/moduleparam.h 中 __module_param_call() 巨集的定義。

字串陣列中的字串似乎不能包含逗號,否則乙個字串會被解析成兩個

乙個測試用例:parm_hello.c

#include

#include

#include

#define max_array 6

static int int_var = 0;

static const char *str_var = "default";

static int int_array[6];

int narr;

module_param(int_var, int, 0644);

module_parm_desc(int_var, "a integer variable");

module_param(str_var, charp, 0644);

module_parm_desc(str_var, "a string variable");

module_param_array(int_array, int, &narr, 0644);

module_parm_desc(int_array, "a integer array");

static int __init hello_init(void)

return 0;

}static void __exit hello_exit(void)

module_init(hello_init);

module_exit(hello_exit);

module_license("gpl");

module_author("ydzhang");

module_description("this module is a example.");

測試:insmod parm_hello.ko int_var=100 str_var=hello int_array=100,200

Linux核心模組傳遞引數

如果需要向核心模組中傳遞引數,可以使用函式 module param 引數名,引數型別,讀寫許可權 1 引數名稱 不必解釋 2 引數型別 byte,short,short,int,uint,long,ulong,charp,bool,invbool 3 讀寫許可權 一般為s irugo 例子 傳遞乙...

module param核心模組引數傳遞

在使用者態下程式設計可以通過main 的來傳遞命令列引數,而編寫乙個核心模組則通過module param 引數用 moudle param 巨集定義來宣告,它定義在 moduleparam.h.module param name,type,perm module param 使用了 3 個引數 變...

linux核心模組引數

在裝載核心模組時,使用者可以向模組傳遞引數,形式為 insmod modprobe 模組名 引數名 引數值。如果不傳遞,引數將使用模組內定義的預設值。我們可以使用以下方法為模組定義乙個引數 module param 引數名,引數型別,引數讀 寫許可權 如下 定義了乙個整型引數 static int ...