Linux内核模块编程入门-5(传递命令行参数到模块)

模块可以接受命令行参数,但是不是使用argc/argv。


为了让参数传递到模块中,声明变量来存放命令行参数作为全局变量,然后使用module_param()宏来建立该机制,该宏定义在linux/moduleprarm.h中。在运行时,insmod将使用任何的命令行参数,像insmod mymodule.ko myvariable=5来填充参数。为清楚起见,变量声明和宏应该放在模块的开头,下例代码清晰地展示了模块接受命令行参数的用法


module_param()宏接受3个参数:变量名,变量类型和sysfs中相应文件的权限。整型可以是普通整型或无符号整型。若要使用整型数组或字符串,则需要参考module_param_array()和module_param_string()。

int myint = 3;
module_param(myint, int, 0);

数组参数也是支持的。为了跟踪参数的数目,需要传递一个指向count变量的指针作为第三个参数。该参数是可选的,可以忽略该count变量,传递NULL来代替。

int myintarray[2];
module_param_array(myintarray, int, NULL, 0);    // not interested in count

int myshortarray[4];
int count;
module_param_array(myshortarray, short, , 0);    // put count int "count" variable

最后,有一个宏函数,MODULE_PARAM_DESC(),用来记录模块使用的参数,它有两个参数:变量名和自由格式字符串用来描述变量。


以下是示例:

/*
 * hello-4.c - Demonstrates command line argument passing to a module.
 */
#include <linux/module.h>	// Needed by all modules
#include <linux/kernel.h>	// Needed for KERN_INFO
#include <linux/init.h>		// Needed for the macros
#include <linux/moduleparam.h>
#include <linux/stat.h>

#define DRIVER_AUTHOR "Fantasy<@gmail.com>"
#define DRIVER_DESC	"A simple driver"

MODULE_LICENSE("GPL");
MODULE_AUTHOR(DRIVER_AUTHOR);
MODULE_DESCRIPTION(DRIVER_DESC);
MODULE_SUPPORTED_DEVICE("testdevice");

static short int myshort = 1;
static int myint = 420;
static long int mylong = 9999;
static char* mystring = "blah";
static int myintarray[2] = {-1, -1};
static int arr_argc = 0;

/*
 * module_param(foo, int, 0000)
 * The first param is the parameters name
 * The second param is it's data type
 * The final argument is the permissions bits,
 * for exposing parameters in sysfs(if non-zero) at a later stage
 */

module_param(myshort, short, S_IRUSR|S_IWUSR|S_IRGRP|S_IRGRP);
MODULE_PARM_DESC(myshort, "A short integer");

module_param(myint, int, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
MODULE_PARM_DESC(MYINT, "An integer");

module_param(mylong, long, S_IRUSR);
MODULE_PARM_DESC(mylong, "A long integer");

module_param(mystring, charp, 0000);
MODULE_PARM_DESC(mystring, "A character string");

/*
 * module_param_array(name, type, num, perm);
 * The first param is the parameters's(in this case the array's) name
 * The second param is the data type of the elements of the array
 * The third argument is a pointer to the variable that will store the number
 * of elements of the array initialized by the user at module loading time
 * The fourth argument is the permission bits
 */
module_param_array(myintarray, int, &arr_argc, 0000);
MODULE_PARM_DESC(myintArray, "An array of integers");

static int __init hello_5_init(void)
{
	int i;

	printk(KERN_INFO "Hello, World 5\n====================\n");
	printk(KERN_INFO "myshort is a short integer:%hd\n", myshort);
	printk(KERN_INFO "myint is an integer:%d\n", myint);
	printk(KERN_INFO "mylong is a long integer:%ld\n", mylong);
	printk(KERN_INFO "mystring is a string:%s\n", mystring);
	
	for(i = 0; i < (sizeof myintarray / sizeof(int)); i++)
	{
		printk(KERN_INFO "myintArray[%d] = %d\n", i, myintarray[i]);
	}
	printk(KERN_INFO "got %d arguments for myintArray.\n", arr_argc);

	return 0;
}

static void __exit hello_5_exit(void)
{
	printk(KERN_INFO "Goodbye, world 5\n");
}

module_init(hello_5_init);
module_exit(hello_5_exit);

输入如下命令时:

insmod hello_5.ko mystring="Fantasy" myshort=25 myintarray=20
输出为(通过vim /var/log/syslog查看):


输入如下命令时:

insmod hello_5.ko mystring="Fantasy" myshort=25 mylong=10000 myint=100 myintarray=20,30
输出为:


以上示例运行的Linux为:

root@ubuntu:/home/administrator/LKMP/ch2/hello_5# uname -a
Linux ubuntu 3.8.0-29-generic #42~precise1-Ubuntu SMP Wed Aug 14 16:19:23 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux


  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值