首页 运维 网络学院 查看内容

Linux内核编程(启动参数)

2005-6-12 12:46 691 0

摘要: 6.启动参数 在以前的许多例子里,我们要把一些东西强制地写入内核模块,比如/proc文件名或设备主码,以至我们可以用...
关键词: 内核 module 模块 printk parameters version kernel include str 参数

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= "); } else printk("Strings:%s and %s ", str1, str2); #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,0) printk("If you try to insmod this module twice,"); printk("(without rmmoding "); printk("it first), you might get the wrong"); printk("error message: "); printk("symbol for parameters str1 not found. "); #endif return 0; } /* Cleanup */ void cleanup_module() { }
声明:文章版权归原作者所有 部分文章转自互联网 如有侵权请联系 [邮箱地址] 删除

路过

雷人

握手

鲜花

鸡蛋

最新评论

返回顶部