今天看了一下模块参数,试着模仿书中的例子写了hellop.c的程序代码:
dolphin@dolphin-laptop:~/Documents/LDD/hellop$ cat hellop.c
#include <linux/init.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
MODULE_LICENSE( "Dual BSD/GPL" );
static char *whom = "dolphin";
static int howmany = 3;
module_param( howmany, int, S_IRUGO );
module_param( whom, charp, S_IRUGO );
static int __init hello_init( void ) {
int i;
for ( i = 0; i < howmany; i++ )
printk( KERN_ALERT "Hello, %s!/n", whom );
return 0;
}
static void __exit hello_exit( void ) {
printk( KERN_ALERT "Goodbye, %s!/n", whom );
}
module_init( hello_init );
module_exit( hello_exit );
其中S_IRUGO表示权限,charp是类似于字符串的类型。
所有的模块参数应该被赋予一个默认值,如果在insmod的时候不带任何参数,则按默认的值输出,如果要传参数,则使用如下方式:
sudo insmod hellop.ko howmany=2 whom="angel"
输出的结果也相应的发生改变,当然,也可以只传一个参数,另一个则按照默认值设置。