⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 node19.html

📁 Linux可卸载模块编程
💻 HTML
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<!--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>Startup Parameters</TITLE>
<META NAME="description" CONTENT="Startup Parameters">
<META NAME="keywords" CONTENT="mpg">
<META NAME="resource-type" CONTENT="document">
<META NAME="distribution" CONTENT="global">
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<LINK REL="STYLESHEET" HREF="mpg.css">
<LINK REL="next" HREF="node20.html">
<LINK REL="previous" HREF="node18.html">
<LINK REL="up" HREF="mpg.html">
<LINK REL="next" HREF="node20.html">
</HEAD>
<BODY >
<!--Navigation Panel-->
<A NAME="tex2html611"
 HREF="node20.html">
<IMG WIDTH="37" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="next"
 SRC="next_motif.gif"></A> 
<A NAME="tex2html607"
 HREF="mpg.html">
<IMG WIDTH="26" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="up"
 SRC="up_motif.gif"></A> 
<A NAME="tex2html601"
 HREF="node18.html">
<IMG WIDTH="63" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="previous"
 SRC="previous_motif.gif"></A> 
<A NAME="tex2html609"
 HREF="node1.html">
<IMG WIDTH="65" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="contents"
 SRC="contents_motif.gif"></A> 
<A NAME="tex2html610"
 HREF="node34.html">
<IMG WIDTH="43" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="index"
 SRC="index_motif.gif"></A> 
<BR>
<B> Next:</B> <A NAME="tex2html612"
 HREF="node20.html">System Calls</A>
<B> Up:</B> <A NAME="tex2html608"
 HREF="mpg.html">Linux Kernel Module Programming</A>
<B> Previous:</B> <A NAME="tex2html602"
 HREF="node18.html">Talking to Device Files</A>
<BR>
<BR>
<!--End of Navigation Panel-->

<H1><A NAME="SECTION00800000000000000000">&#160;</A><A NAME="startup-param">&#160;</A><A NAME="461">&#160;</A>
<A NAME="462">&#160;</A>
<BR>
Startup Parameters
</H1>

<P>
In many of the previous examples, we had to hard-wire something into
the kernel module, such as the file name for <TT>/proc</TT> files or the major
device number for the device so we can have <TT>ioctl</TT>'s to it. This goes
against the grain of the Unix, and Linux, philosophy which is to write
flexible program the user can customize.
<A NAME="465">&#160;</A>

<P>
The way to tell a program, or a kernel module, something it needs before
it can start working is by command line parameters. In the case of kernel
modules, we don't get <TT>argc</TT> and <TT>argv</TT> -- instead, we get something 
better. We can define global variables in the kernel module and <TT>insmod</TT>
will fill them for us.
<A NAME="469">&#160;</A>
<A NAME="470">&#160;</A>

<P>
In this kernel module, we define two of them: <TT>str1</TT> and <TT>str2</TT>. All 
you need to do is compile the kernel module and then run 
<TT>insmod str1=xxx str2=yyy</TT>.
When <TT>init_module</TT> is called, <TT>str1</TT> will point to the string 
`<TT>xxx</TT>' and <TT>str2</TT> to the string `<TT>yyy</TT>'.
<A NAME="479">&#160;</A>

<P>
In version 2.0 there is no type checking on these 
arguments<A NAME="tex2html158"
 HREF="footnode.html#foot480"><SUP>6.1</SUP></A>. If the first character of <TT>str1</TT> or <TT>str2</TT> is a 
digit the kernel will fill the variable with the value of the integer, 
rather than a pointer to the string. If a real life situation you have to 
check for this.
<A NAME="483">&#160;</A>

<P>
On the other hand, in version 2.2 you use the macro <TT>MACRO_PARM</TT> to
tell <TT>insmod</TT> that you expect a parameters, its name <EM>and its type</EM>. 
This solves the type problem and allows kernel modules to receive strings 
which begin with a digit, for example.
<A NAME="487">&#160;</A>
<A NAME="488">&#160;</A>

<P>
ex
 
<FONT SIZE="+1"><B>param.c</B></FONT> 
<A NAME="493">&#160;</A><A NAME="494">&#160;</A> 

<P>
<PRE>
 
/* 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 &lt;linux/kernel.h&gt;   /* We're doing kernel work */
#include &lt;linux/module.h&gt;   /* Specifically, a module */

/* Deal with CONFIG_MODVERSIONS */
#if CONFIG_MODVERSIONS==1
#define MODVERSIONS
#include &lt;linux/modversions.h&gt;
#endif        


#include &lt;stdio.h&gt;  /* I need NULL */


/* In 2.2.3 /usr/include/linux/version.h includes a 
 * macro for this, but 2.0.35 doesn't - 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 doesn't 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 parameter's name and
 * it's 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 &gt;= 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=&lt;something&gt;");
    printk("str2=&lt;something&gt;\n");
  } else
    printk("Strings:%s and %s\n", str1, str2);

#if LINUX_VERSION_CODE &gt;= 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;
}


/* Cleanup */
void cleanup_module()
{
}
</PRE>

<P>
<HR>
<!--Navigation Panel-->
<A NAME="tex2html611"
 HREF="node20.html">
<IMG WIDTH="37" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="next"
 SRC="next_motif.gif"></A> 
<A NAME="tex2html607"
 HREF="mpg.html">
<IMG WIDTH="26" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="up"
 SRC="up_motif.gif"></A> 
<A NAME="tex2html601"
 HREF="node18.html">
<IMG WIDTH="63" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="previous"
 SRC="previous_motif.gif"></A> 
<A NAME="tex2html609"
 HREF="node1.html">
<IMG WIDTH="65" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="contents"
 SRC="contents_motif.gif"></A> 
<A NAME="tex2html610"
 HREF="node34.html">
<IMG WIDTH="43" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="index"
 SRC="index_motif.gif"></A> 
<BR>
<B> Next:</B> <A NAME="tex2html612"
 HREF="node20.html">System Calls</A>
<B> Up:</B> <A NAME="tex2html608"
 HREF="mpg.html">Linux Kernel Module Programming</A>
<B> Previous:</B> <A NAME="tex2html602"
 HREF="node18.html">Talking to Device Files</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 + -