小白學Linux之module_param()函數學習

來源:互聯網
上載者:User

module_param()的學習

一.module_param
1.為什麼引入
 在使用者態下編程可以通過main()來傳遞命令列參數,而編寫一個核心模組則可通過module_param()來傳遞命令列參數.
2. module_param宏是Linux 2.6核心中新增的,該宏被定義在include/linux/moduleparam.h檔案中,具體定義如下:
   /* Helper functions: type is byte, short, ushort, int, uint, long,
   ulong, charp, bool or invbool, or XXX if you define param_get_XXX,
   param_set_XXX and param_check_XXX.

*/
   #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(name, type, perm)               
    module_param_named(name, name, type, perm)
由此可知 module_param的實現是通過module_param_named(name, name, type, perm)的。
3.module_param使用了3個參數:變數名,它的類型,以及一個許可權掩碼用來做一個輔助的sysfs入口。
這個宏定義應當放在任何函數之外,典型地是出現在源檔案的前面。
eg: static char *whom="world"
    static int    tige=1;
    module_param(tiger,int,S_IRUGO);
    module_param(whom,charp,S_IRUGO);
4.模組參數支援許多類型:
bool
invbool
一個布爾型( true 或者 false)值(相關的變數應當是 int 類型). invbool 類型顛倒了值, 所以真值變成 false, 反之亦然.
charp :一個字元指標值. 記憶體為使用者提供的字串分配, 指標因此設定.
int
long
short
uint
ulong
ushort
基本的變長整型值. 以 u 開頭的是無符號值.
5.數組參數, 用逗號間隔的列表提供的值, 模組載入者也支援。

聲明一個數組參數, 使用:
module_param_array(name,type,num,perm);
這裡 name 是你的數組的名子(也是參數名),
type 是數組元素的類型,
num 是一個整型變數,
perm 是通常的許可權值.
如果數組參數在載入時設定, num 被設定成提供的數的個數. 模組載入者拒絕比數組能放下的多的值.

Tiger-John說明:

perm參數的作用是什嗎?
最後的 module_param 欄位是一個許可權值,表示此參數在sysfs檔案系統中所對應的檔案節點的屬性。你應當使用 <linux/stat.h> 中定義的值. 這個值控制誰可以存取這些模組參數在 sysfs 中的表示.當perm為0時,表示此參數不存在 sysfs檔案系統下對應的檔案節點。 否則, 模組被載入後,在/sys/module/ 目錄下將出現以此模組名命名的目錄, 帶有給定的許可權.。
許可權在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
使用 S_IRUGO 參數可以被所有人讀取, 但是不能改變; S_IRUGO|S_IWUSR 允許 root 來改變參數. 注意, 如果一個參數被 sysfs 修改, 你的模組看到的參數值也改變了, 但是你的模組沒有任何其他的通知. 你應當不要使模組參數可寫, 除非你準備好檢測這個改變並且因而作出反應.

二執行個體:

說了這麼多,看一個程式體驗以下:

1.module_param.c

/*<br />* file name : module_param.c<br />* author : tiger-John<br />*/<br />#include<linux/init.h><br />#include<linux/module.h><br />#include<linux/kernel.h><br />MODULE_LICENSE("GPL");<br />static char *who;<br />static int times;<br />module_param(who,charp,0644);<br />module_param(times,int,0644);<br />static int __init hello_init(void)<br />{<br />int i;<br />for(i = 1;i <= times;i++)<br />printk("%d %s!/n",i,who);<br />return 0;<br />}<br />static void __exit hello_exit(void)<br />{<br />printk("Goodbye,%s!/n",who);<br />}<br />module_init(hello_init);<br />module_exit(hello_exit);

2.編寫Makefile檔案

1 obj-m:=module_param.o<br /> 2 CURRENT_PATH:=$(shell pwd)<br /> 3 VERSION_NUM :=$(shell uname -r)<br /> 4 LINUX_PATH :=/usr/src/linux-headers-$(VERSION_NUM)<br /> 5<br /> 6 all :<br /> 7 make -C $(LINUX_PATH) M=$(CURRENT_PATH) modules<br /> 8 clean :<br /> 9 make -C $(LINUX_PATH) M=$(CURRENT_PATH) clean<br />

3.在終端輸入:make

4 .載入模組: sudo insmdo module_param.ko who=tiger times=4

5.dmesg :查看結果。

過程執行個體:

a.在終端輸入:make

think@ubuntu:~/module_param$ make
make -C /usr/src/linux-headers-2.6.32-25-generic M=/home/think/module_param modules
make[1]: 正在進入目錄 `/usr/src/linux-headers-2.6.32-25-generic'
  Building modules, stage 2.
  MODPOST 1 modules
make[1]:正在離開目錄 `/usr/src/linux-headers-2.6.32-25-generic'
think@ubuntu:~/module_param$

b.在終端輸入: sudo insmod module_param.ko who=tiger times=4

think@ubuntu:~/module_param$ sudo insmod module_param.ko who=tiger times=4

c 在終端輸入:dmesg

[ 4297.711137] 1  tiger!
[ 4297.711139] 2  tiger!
[ 4297.711140] 3  tiger!
[ 4297.711141] 4  tiger!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.