📄 启动参数.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0059)http://yangxingjun.myrice.com/chinesehow/kernel/node19.html -->
<!--Converted with LaTeX2HTML 98.1 release (February 19th, 1998)originally by Nikos Drakos (nikos@cbl.leeds.ac.uk), CBLU, University of Leeds* revised and updated by: Marcus Hennecke, Ross Moore, Herb Swan* with significant contributions from: Jens Lippmann, Marek Rouchal, Martin Wilck and others --><HTML><HEAD><TITLE>启动参数</TITLE>
<META content="Startup Parameters" name=description>
<META content=mpg name=keywords>
<META content=document name=resource-type>
<META content=global name=distribution>
<META http-equiv=Content-Type content="text/html; charset=GB2312"><LINK
href="C:\Documents and Settings\nonoka\My Documents\启动参数.files\error(2).htm"
rel=STYLESHEET><LINK href="node20.html" rel=next><LINK href="node18.html"
rel=previous><LINK href="mpg.html" rel=up><LINK href="node20.html" rel=next>
<META content="MSHTML 6.00.2800.1106" name=GENERATOR></HEAD>
<BODY><!--Navigation Panel--><A
href="http://yangxingjun.myrice.com/chinesehow/kernel/node20.html"
name=tex2html611><IMG height=24 alt=next src="启动参数.files/next_motif.gif"
width=37 align=bottom border=0></A> <A
href="http://yangxingjun.myrice.com/chinesehow/kernel/mpg.html"
name=tex2html607><IMG height=24 alt=up src="启动参数.files/error.htm" width=26
align=bottom border=0></A> <A
href="http://yangxingjun.myrice.com/chinesehow/kernel/node18.html"
name=tex2html601><IMG height=24 alt=previous
src="C:\Documents and Settings\nonoka\My Documents\启动参数.files\error(1).htm"
width=63 align=bottom border=0></A> <A
href="http://yangxingjun.myrice.com/chinesehow/kernel/node1.html"
name=tex2html609><IMG height=24 alt=contents src="启动参数.files/contents_motif.gif"
width=65 align=bottom border=0></A> <A
href="http://yangxingjun.myrice.com/chinesehow/kernel/node34.html"
name=tex2html610><IMG height=24 alt=index src="启动参数.files/index_motif.gif"
width=43 align=bottom border=0></A> <BR><B>Next:</B> <A
href="http://yangxingjun.myrice.com/chinesehow/kernel/node20.html"
name=tex2html612>系统调用</A> <B>Up:</B> <A
href="http://yangxingjun.myrice.com/chinesehow/kernel/mpg.html"
name=tex2html608>Linux 内核模块编程</A> <B>Previous:</B> <A
href="http://yangxingjun.myrice.com/chinesehow/kernel/node18.html"
name=tex2html602>和设备文件对话</A> <BR><BR><!--End of Navigation Panel-->
<H1><A name=SECTION00800000000000000000> </A><A
name=startup-param> </A><A name=461> </A> <A name=462> </A>
<BR>启动参数 </H1>
<P>在前面的很多例子中,我们不得不硬性的将一些东西写如内核模块,例如在 <TT>/proc</TT> 中的文件名或设备的主设备号,因此我们可以对它
<TT>ioctl</TT>。 这和 Unix及 Linux的编写用户可以定制的灵活的程序精神是相矛盾的。 <A name=465> </A>
<P>在程序或内核模块能开始工作之前告诉它一些需要的东西的办法是通过命令行参数。在内核模块的情况下,我们不能得到 <TT>argc</TT> 和
<TT>argv</TT> -- 代替的,我们得到更好的东西。我们可以在内核模块中定义全局变量并且 <TT>insmod</TT>将为我们填充它们。 <A
name=469> </A> <A name=470> </A>
<P>在这个内核模块中,我们定义了两个: <TT>str1</TT> 和
<TT>str2</TT>。你所需要做的全部就是编译那个内核模块然后用<TT>insmod str1=xxx
str2=yyy</TT>运行它。当<TT>init_module</TT> 被调用,<TT>str1</TT> 将指向字符串‘<TT>xxx</TT>’,
<TT>str2</TT> 将指向‘<TT>yyy</TT>’。 <A name=479> </A>
<P>在 2.0 版中对这些参数<A
href="http://yangxingjun.myrice.com/chinesehow/kernel/footnode.html#foot480"
name=tex2html158><SUP>6.1</SUP></A>没有类型检查。如果<TT>str1</TT> 或 <TT>str2</TT>
的第一个字符是数字,内核将用整数值填充变量而不是字符串指针。如果是在实际的情形下你需要检查这个。 <A name=483> </A>
<P>另一方面,在 2.2 版中你用宏 <TT>MACRO_PARM</TT> 告诉 <TT>insmod</TT>
你期待一个参数,它的名字<EM>和类型</EM> 。这解决了类型的问题并且允许内核模块接收以数字开始的字符串。 <A name=487> </A>
<A name=488> </A>
<P>范例 <FONT size=+1><B>param.c</B></FONT> <A name=493> </A><A
name=494> </A>
<P><PRE>
/* param.c
*
* 在模块被安装时接收命令行参数
*/
/* Copyright (C) 1998-99 by Ori Pomerantz */
/* 必要头文件 */
/* 标准头文件 */
#include <linux/kernel.h> /* 内核工作 */
#include <linux/module.h> /* 明确指定是模块 */
/* 处理 CONFIG_MODVERSIONS */
#if CONFIG_MODVERSIONS==1
#define MODVERSIONS
#include <linux/modversions.h>
#endif
#include <stdio.h> /* 我需要 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=<something>");
printk("str2=<something>\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()
{
}
</PRE>
<P>
<HR>
<!--Navigation Panel--><A
href="http://yangxingjun.myrice.com/chinesehow/kernel/node20.html"
name=tex2html611><IMG height=24 alt=next src="启动参数.files/next_motif.gif"
width=37 align=bottom border=0></A> <A
href="http://yangxingjun.myrice.com/chinesehow/kernel/mpg.html"
name=tex2html607><IMG height=24 alt=up src="启动参数.files/error.htm" width=26
align=bottom border=0></A> <A
href="http://yangxingjun.myrice.com/chinesehow/kernel/node18.html"
name=tex2html601><IMG height=24 alt=previous
src="C:\Documents and Settings\nonoka\My Documents\启动参数.files\error(1).htm"
width=63 align=bottom border=0></A> <A
href="http://yangxingjun.myrice.com/chinesehow/kernel/node1.html"
name=tex2html609><IMG height=24 alt=contents src="启动参数.files/contents_motif.gif"
width=65 align=bottom border=0></A> <A
href="http://yangxingjun.myrice.com/chinesehow/kernel/node34.html"
name=tex2html610><IMG height=24 alt=index src="启动参数.files/index_motif.gif"
width=43 align=bottom border=0></A> <BR><B>Next:</B> <A
href="http://yangxingjun.myrice.com/chinesehow/kernel/node20.html"
name=tex2html612>系统调用</A> <B>Up:</B> <A
href="http://yangxingjun.myrice.com/chinesehow/kernel/mpg.html"
name=tex2html608>Linux 内核模块编程</A> <B>Previous:</B> <A
href="http://yangxingjun.myrice.com/chinesehow/kernel/node18.html"
name=tex2html602>和设备文件对话</A> <!--End of Navigation Panel-->
<ADDRESS><I></I><BR><I>1999-05-19</I> </ADDRESS></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -