Linux内核编程(启动参数)

王朝system·作者佚名  2006-11-24
窄屏简体版  字體: |||超大  

6.启动参数

在以前的许多例子里,我们要把一些东西强制地写入内核模块,比如/proc文件名或设备主码,以至我们可以用ioctl's处理它。这样句违背了Unix以及Linux的原则:写用户可以自由设定的灵活程序。

在程序或者内核模块启动之前通知它一些消息是通过命令行参数做到的。在内核模块的情况下,我们没有argc和argv参数,而是有更好的东西。我们可以在内核模块里定义全局变量,insmod会给我们赋值。

在这个内核模块中,我们定义了两个变量:str1和str2。你需要做的只是编译内核模块,然后运行str1=xxx str2=yyy。当调用init_module时,str1将指向串xxx,str2将指向串yyy。

在2.0版对这些参数没有类型检查。如果str1和str2的第一个字符是数字,内核就会把这些变量赋为整数,而不是指向串的指针。这在实际情况中你一定要检查类型。

另一方面,在2.2版本中,你可以使用宏MACRO_PARM告诉insmod你需要一个参数,它的名字和类型。这样解决了类型问题,并且允许内核模块接收以数字开始的串。

ex param.c

/* param.c

*

* Receive command line parameters at module installation

*/

/* Copyright (C) 1998-99 by Ori Pomerantz */

/* The necessary header files */

/* Standard in kernel modules */

#include /* Were doing kernel work */

#include /* Specifically, a module */

/* Deal with CONFIG_MODVERSIONS */

#if CONFIG_MODVERSIONS==1

#define MODVERSIONS

#include

#endif

#include /* I need NULL */

/* In 2.2.3 /usr/include/linux/version.h includes a

* macro for this, but 2.0.35 doesnt - so I add it

* here if necessary. */

#ifndef KERNEL_VERSION

#define KERNEL_VERSION(a,b,c) ((a)*65536+(b)*256+(c))

#endif

/* Emmanuel Papirakis:

*

* Prameter names are now (2.2) handled in a macro.

* The kernel doesnt resolve the symbol names

* like it seems to have once did.

*

* To pass parameters to a module, you have to use a macro

* defined in include/linux/modules.h (line 176).

* The macro takes two parameters. The parameters name and

* its type. The type is a letter in double quotes.

* For example, "i" should be an integer and "s" should

* be a string.

*/

char *str1, *str2;

#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,0)

MODULE_PARM(str1, "s");

MODULE_PARM(str2, "s");

#endif

/* Initialize the module - show the parameters */

int init_module()

{

if (str1 == NULL || str2 == NULL) {

printk("Next time, do insmod param str1=");

printk("str2=\n");

} else

printk("Strings:%s and %s\n", str1, str2);

#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,0)

printk("If you try to insmod this module twice,");

printk("(without rmmoding\n");

printk("it first), you might get the wrong");

printk("error message:\n");

printk("symbol for parameters str1 not found.\n");

#endif

return 0;

}

/* Cleanup */

void cleanup_module()

{

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航