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

📄 x762.html

📁 linux驱动开发
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<HTML
><HEAD
><TITLE
>Using /proc For Input</TITLE
><META
NAME="GENERATOR"
CONTENT="Microsoft FrontPage 4.0"><LINK
REL="HOME"
TITLE="The Linux Kernel Module Programming Guide"
HREF="index.html"><LINK
REL="UP"
TITLE="Using /proc For Input"
HREF="c760.html"><LINK
REL="PREVIOUS"
TITLE="Using /proc For Input"
HREF="c760.html"><LINK
REL="NEXT"
TITLE="Talking To Device Files"
HREF="c846.html"></HEAD
><BODY
CLASS="SECT1"
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#0000FF"
VLINK="#840084"
ALINK="#0000FF"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="3"
ALIGN="center"
>The Linux KerneLinux内核驱动模块编程指南 (内核版本2.2, 2.4)</TH  
></TR  
><TR  
><TD  
WIDTH="10%"  
ALIGN="left"  
VALIGN="bottom"  
><A  
HREF="c760.html"  
ACCESSKEY="P"  
>返回</A 
></TD 
><TD 
WIDTH="80%" 
ALIGN="center" 
VALIGN="bottom" 
><A 
NAME="AEN760" 
></A 
>第6章. 使用 /proc 作为输入</TD  
><TD  
WIDTH="10%"  
ALIGN="right"  
VALIGN="bottom"  
><A  
HREF="c846.html"  
ACCESSKEY="N"  
>继续</A 
></TD 
></TR 
></TABLE 
><HR 
ALIGN="LEFT" 
WIDTH="100%"></DIV 
><DIV 
CLASS="SECT1" 
><H1 
CLASS="SECT1" 
><A 
NAME="AEN762" 
></A 
>6.1. <A 
NAME="AEN760" 
></A 
>第6章. 使用 /proc 作为输入</H1  
><P  
>现在我们有两种从内核模块获得输出的方法:我们可以注册一个设备驱动并用  
	<B  
CLASS="COMMAND"  
>mknod</B  
>  生成一个设备文件,或者我们可以建立一个 <TT 
CLASS="FILENAME" 
>/proc</TT 
>   
  文件。这样内核就可以告诉我们重要的信息。剩下的唯一问题是我们没法反馈信息。第一种方法是向  
  <TT 
CLASS="FILENAME" 
>/proc</TT 
>  文件系统写入信息。</P 
><P 
>由于 /proc  
  文件系统是为内核输出其运行信息而设计的,它并未向内核输入信息提供了任何准备。结构体 <TT 
CLASS="VARNAME" 
> proc_dir_entry</TT  
>   
  并没有指向输入函数的指针,而是指向了一个输出函数。作为替代办法,向  
  <TT 
CLASS="FILENAME" 
>/proc</TT 
> 
  写入信息,我们可以使用标准的文件系统提供的机制。</P 
><P 
>在Linux中有一种标准的注册文件系统的方法。既然每种文件系统都必须有处理文件索引节点inode和文件本身的函数<A 
NAME="AEN781" 
HREF="#FTN.AEN781" 
><SPAN 
CLASS="footnote" 
>[1]</SPAN 
></A 
><SPAN 
CLASS="footnote" 
>,那么就一定有种结构体去存放这些函数的指针。这就是结构体</SPAN 
> <TT 
CLASS="VARNAME" 
> inode_operations</TT  
><SPAN  
CLASS="footnote"  
>,它其中又包含一个指向结构体</SPAN 
> <TT 
CLASS="VARNAME" 
> file_operations</TT  
><SPAN  
CLASS="footnote"  
> 的指针。在</SPAN
> /proc 
  文件系统中,当我们需要注册一个新文件时,我们被允许选择哪一个 <TT 
CLASS="VARNAME" 
> inode_operations</TT  
>  结构体。这就是我们将使用的机制,用包含结构体<SPAN 
CLASS="footnote" 
></SPAN
> <TT
CLASS="VARNAME"
> file_operations</TT 
><SPAN 
CLASS="footnote" 
> 指针的结构体</SPAN
> <TT 
CLASS="VARNAME" 
> inode_operations</TT  
>  来指向我们的 <TT 
CLASS="FUNCTION" 
>module_input</TT 
> 和 <TT 
CLASS="FUNCTION" 
>module_output</TT 
>  函数。</P
><P
>需要注意的是“读”和“写”的含义在内核中是反过来的。“读”意味着输出,而“写”意味着输入。这是从用户的角度来看待问题的——如果一个进程只能从内核的“输出”获得输入,而内核也是从进程的输出中得到“输入”的。</P
><P 
>在这儿另一件有趣的事就是 <TT  
CLASS="FUNCTION"  
>module_permission</TT  
>  函数了。该函数在每个进程想要对 <TT  
CLASS="FILENAME"  
>/proc</TT  
>  
  文件系统内的文件操作时被调用,它来决定是否操作被允许。目前它只是对操作和操作所属用户的UID进行判断,但它可以也把其它的东西包括进来,像还有哪些别的进程在对该文件进行操作,当前的时间,或是我们最后接收到的输入。</P 
><P 
>加入宏 <TT  
CLASS="FUNCTION"  
>put_user</TT  
> 和 <TT 
CLASS="FUNCTION" 
>get_user</TT 
>  
  的原因是Linux的内存是使用分页机制的(在Intel架构下是如此,但其它架构下有可能不同)。这就意味着指针自身并不是指向一个确实的物理内存地址,而知是分页中的一个地址,而且你必须知道哪些分页将来是可用的。其中内核本身占用一个分页,其它的每个进程都有自己的分页。</P 
><P 
>进程能看得到的分页只有属于它自己的,所以当编写用户程序时,不用考虑分页的存在。但是当你编写内核模块时,你就会访问由系统自动管理的内核所在的分页。当一块内存缓冲区中的内容要在当前运行中的进程和内核之间传递时,内核的函数就接收指向在进程分页中的该内存缓冲区的指针。宏 put_user 和 get_user 
  允许你进行这样的访问内存的操作。</P 
><DIV 
CLASS="EXAMPLE" 
><A 
NAME="AEN843" 
></A 
><P 
><B 
>Example 6-1. procfs.c</B 
></P 
><TABLE 
BORDER="0" 
BGCOLOR="#E0E0E0" 
WIDTH="100%" 
><TR 
><TD 
><FONT 
COLOR="#000000" 
><PRE 
CLASS="PROGRAMLISTING" 
>/*  procfs.c -  create a &quot;file&quot; in /proc, which allows both input and output.
 */

#include &lt;linux/kernel.h&gt;   /* We're doing kernel work */
#include &lt;linux/module.h&gt;   /* Specifically, a module */

/* Necessary because we use proc fs */
#include &lt;linux/proc_fs.h&gt;


/* 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



#if LINUX_VERSION_CODE &gt;= KERNEL_VERSION(2,2,0)
#include &lt;asm/uaccess.h&gt;  /* for get_user and put_user */
#endif

/* The module's file functions ********************** */


/* Here we keep the last message received, to prove 
 * that we can process our input */
#define MESSAGE_LENGTH 80
static char Message[MESSAGE_LENGTH];


/* Since we use the file operations struct, we can't 
 * use the special proc output provisions - we have to 
 * use a standard read function, which is this function */
#if LINUX_VERSION_CODE &amp;gt;= KERNEL_VERSION(2,2,0)
static ssize_t module_output(
    struct file *file,   /* The file read */
    char *buf, /* The buffer to put data to (in the
                * user segment) */
    size_t len,  /* The length of the buffer */
    loff_t *offset) /* Offset in the file - ignore */
#else
static int module_output(
    struct inode *inode, /* The inode read */
    struct file *file,   /* The file read */
    char *buf, /* The buffer to put data to (in the
                * user segment) */
    int len)  /* The length of the buffer */
#endif
{
  static int finished = 0;
  int i;
  char message[MESSAGE_LENGTH+30];

  /* We return 0 to indicate end of file, that we have 
   * no more information. Otherwise, processes will 
   * continue to read from us in an endless loop. */
  if (finished) {
    finished = 0;
    return 0;
  }

  /* We use put_user to copy the string from the kernel's 
   * memory segment to the memory segment of the process 
   * that called us. get_user, BTW, is
   * used for the reverse. */
  sprintf(message, &quot;Last input:%s&quot;, Message);
  for(i=0; i&amp;lt;len &amp;&amp; message[i]; i++) 
    put_user(message[i], buf+i);


  /* Notice, we assume here that the size of the message 
   * is below len, or it will be received cut. In a real 
   * life situation, if the size of the message is less 
   * than len then we'd return len and on the second call 
   * start filling the buffer with the len+1'th byte of 
   * the message. */
  finished = 1; 

  return i;  /* Return the number of bytes &quot;read&quot; */
}


/* This function receives input from the user when the 
 * user writes to the /proc file. */
#if LINUX_VERSION_CODE &gt;= KERNEL_VERSION(2,2,0)
static ssize_t module_input(
    struct file *file,   /* The file itself */
    const char *buf,     /* The buffer with input */
    size_t length,       /* The buffer's length */
    loff_t *offset)      /* offset to file - ignore */
#else
static int module_input(
    struct inode *inode, /* The file's inode */
    struct file *file,   /* The file itself */
    const char *buf,     /* The buffer with the input */
    int length)          /* The buffer's length */
#endif
{
  int i;

  /* Put the input into Message, where module_output 
   * will later be able to use it */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -