范例 param.c
/* param.c
*
* 在模块被安装时接收命令行参数
*/
/* Copyright (C) 1998-99 by Ori Pomerantz */
/* 必要头文件 */
/* 标准头文件 */
#include /* 内核工作 */
#include /* 明确指定是模块 */
/* 处理 CONFIG_MODVERSIONS */
#if CONFIG_MODVERSIONS==1
#define MODVERSIONS
#include
#endif
#include /* 我需要 NULL */
/* 在 2.2.3 版/usr/include/linux/version.h 包含这个宏
* 但 2.0.35版不包括-因此加入以备需要 */
#ifndef KERNEL_VERSION
#define KERNEL_VERSION(a,b,c) ((a)*65536+(b)*256+(c))
#endif
/* Emmanuel Papirakis:
*
* 现在(2.2版),在宏里参数名现在被处理。内核不能像它好象已经作过的那样解决符号名。
*
* 为了向模块传送参数,你不得不使用 include/linux/modules.h (第 176 行)里面定义的一个宏。
* 宏需要两个参数。参数名和它的类型。类型是用双引号引住的一个字母。例如:
* "i" 将为整数, "s" 将为字符串。
*/
char *str1, *str2;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,0)
MODULE_PARM(str1, "s");
MODULE_PARM(str2, "s");
#endif
/* 初始化模块--显示参数 */
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 rmmod'ing\n");
printk("it first), you might get the wrong");
printk("error message:\n");
printk("'symbol for parameters str1 not found'.\n");
#endif
return 0;
}
/* 清除 */
void cleanup_module()
{
} |