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

📄 -proc 文件系统.htm

📁 这是我做linux系统初始化过程分析时在网上收集到的资料
💻 HTM
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0059)http://yangxingjun.myrice.com/chinesehow/kernel/node16.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>/proc 文件系统</TITLE>
<META content="The /proc File System" 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\-proc 文件系统.files\error(2).htm" 
rel=STYLESHEET><LINK href="node17.html" rel=next><LINK href="node14.html" 
rel=previous><LINK href="mpg.html" rel=up><LINK href="node17.html" rel=next>
<META content="MSHTML 6.00.2800.1106" name=GENERATOR></HEAD>
<BODY><!--Navigation Panel--><A 
href="http://yangxingjun.myrice.com/chinesehow/kernel/node17.html" 
name=tex2html575><IMG height=24 alt=next src="-proc 文件系统.files/next_motif.gif" 
width=37 align=bottom border=0></A> <A 
href="http://yangxingjun.myrice.com/chinesehow/kernel/mpg.html" 
name=tex2html571><IMG height=24 alt=up src="-proc 文件系统.files/error.htm" width=26 
align=bottom border=0></A> <A 
href="http://yangxingjun.myrice.com/chinesehow/kernel/node15.html" 
name=tex2html565><IMG height=24 alt=previous 
src="C:\Documents and Settings\nonoka\My Documents\-proc 文件系统.files\error(1).htm" 
width=63 align=bottom border=0></A> <A 
href="http://yangxingjun.myrice.com/chinesehow/kernel/node1.html" 
name=tex2html573><IMG height=24 alt=contents 
src="-proc 文件系统.files/contents_motif.gif" width=65 align=bottom border=0></A> <A 
href="http://yangxingjun.myrice.com/chinesehow/kernel/node34.html" 
name=tex2html574><IMG height=24 alt=index src="-proc 文件系统.files/index_motif.gif" 
width=43 align=bottom border=0></A> <BR><B>Next:</B> <A 
href="http://yangxingjun.myrice.com/chinesehow/kernel/node17.html" 
name=tex2html576>将/proc作为输入</A> <B>Up:</B> <A 
href="http://yangxingjun.myrice.com/chinesehow/kernel/mpg.html" 
name=tex2html572>Linux内核模块编程</A> <B>Previous:</B> <A 
href="http://yangxingjun.myrice.com/chinesehow/kernel/node15.html" 
name=tex2html566>多内核版本源文件</A> <BR><BR><!--End of Navigation Panel-->
<H1><A name=SECTION00500000000000000000>&nbsp;</A><A name=proc-fs>&nbsp;</A><A 
name=310>&nbsp;</A> <A name=311>&nbsp;</A> <A name=312>&nbsp;</A> <BR>/proc 文件系统 
</H1>
<P>在Linux中有额外的机制可以为内核和内核模块将信息发送给进程 -- <TT>/proc</TT> 
文件系统。最初设计的目的是允许更方便的对进程信息进行访问(因此得名),现在它被每一个有有趣的东西报告的内核使用,例如 
<TT>/proc/modules</TT> 有模块的列表 <TT>/proc/meminfo</TT> 有内存使用的统计表。 <A 
name=316>&nbsp;</A> <A name=317>&nbsp;</A> 
<P>使用proc 文件系统的方法和使用设备驱动程序非常相似--创建一个 <TT>/proc</TT> 
文件需要的所有信息的结构,包括任何处理函数的指针(在我们的例子中只有一个,当某人试图从 <TT>/proc</TT> 
文件读时调用的那一个)。然后,<TT>init_module</TT> 在内核中登记该结构而<TT>cleanup_module</TT> 注销它。 
<P>我们使用 <TT>proc_register_dynamic</TT><A 
href="http://yangxingjun.myrice.com/chinesehow/kernel/footnode.html#foot323" 
name=tex2html97><SUP>3.1</SUP></A> 
的原因是我们不想预先决定我们的文件的节点数字,而是为防止冲突而由内核决定它。通常的文件系统存在于磁盘上而不是内存中(<TT>/proc</TT> 
在内存中),在这中情况下,节点数是是指向文件的索引节点所在的磁盘位置的指针。节点包含文件的信息(例如文件的存取权限)和指向磁盘位置或文件数据可以被找到的几个位置的指针。 
<A name=325>&nbsp;</A> <A name=326>&nbsp;</A> <A name=327>&nbsp;</A> 
<P>因为当文件被打开或关闭的时候不能得到调用,所以在这个模块中没有地方放置 <TT>MOD_INC_USE_COUNT</TT> 和 
<TT>MOD_DEC_USE_COUNT</TT>,并且,如果文件被打开随后模块被移除,我们没有办法避免后果。在下一 
章我们会看到一个艰难的但更灵活的可以处理<TT>/proc</TT>文件的实现方式,它也可以让我们防止那个问题。 
<P>范例 <FONT size=+1><B>procfs.c</B></FONT> <A name=335>&nbsp;</A><A 
name=336>&nbsp;</A> 
<P><PRE> 
/* procfs.c -  create a "file" in /proc 
 * Copyright (C) 1998-1999 by Ori Pomerantz
 */


/* 必要的头文件 */

/* 内核模块标准头文件 */
#include &lt;linux/kernel.h&gt;   /* 内核工作 */
#include &lt;linux/module.h&gt;   /* 明确指定是模块 */

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


/* 使用proc 文件系统所必要的 */
#include &lt;linux/proc_fs.h&gt;



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



/* 将数据放入 proc 文件

   参数
   ====
   1. 如果你决定使用缓冲区,数据应该被插入它。
   2. 字符指针的指针。如果你不想使用由内核分配的缓冲区时这将有用。
   3. 文件的当前位置。
   4. 第一个参数中的缓冲区的大小。
   5. Zero (为将来使用?)。


   用法和返回值
   ============
   如果你像我那样使用自己的缓冲区,将它放在第二个参数的位置并返回在那个缓冲区中使用的字节数。

   返回值为0 意味着这次没有更多信息(文件尾)。负数返回值是错误条件。
   

   更多信息
   ======== 
   我不是通过读文档而发现用这个函数去做什么的,而是通过读使用它的代码。我只想看看什么使用了
   proc_dir_entry的get_info成员(如果你有兴趣的话,我使用了一个查找和搜索的组合),我发现它被
   &lt;内核源文件目录&gt;/fs/proc/array.c使用。

   如果有什么关于内核的事情不清楚,这是一个通常的办法。在 Linux 中我们有自由的获取源代码的巨大优势--使用它。
 */
int procfile_read(char *buffer, 
		  char **buffer_location, 
		  off_t offset, 
		  int buffer_length, 
		  int zero)
{
  int len;  /* The number of bytes actually used */

  /* 这是静态的,因此当我们离开这个函数时它还会待在内存中 */
  static char my_buffer[80];  

  static int count = 1;

  /* 我们将所有的信息放在一个里面,所以如果用户问是否有更多的信息,答案总是“没有”。
   *
   * 这是很重要的,因为来自库的标准读函数将持续发布读系统调用直到内核答复没有更多信息
   * 或它的缓冲区被填满。
   */
  if (offset &gt; 0)
    return 0;

  /* 填充缓冲区并得到长度 */
  len = sprintf(my_buffer, 
    "For the %d%s time, go away!\n", count,
    (count % 100 &gt; 10 &amp;&amp; count % 100 &lt; 14) ? "th" : 
      (count % 10 == 1) ? "st" :
        (count % 10 == 2) ? "nd" :
          (count % 10 == 3) ? "rd" : "th" );
  count++;

  /* 告诉调用函数缓冲区在哪儿*/
  *buffer_location = my_buffer;

  /* 返回长度 */
  return len;
}


struct proc_dir_entry Our_Proc_File = 
  {
    0, /* 节点数 - 忽略,它将被 proc_register[_dynamic] 填充*/
    4, /* 文件名长度 */
    "test", /* 文件名*/
    S_IFREG | S_IRUGO, /* 文件模式 - 这是一个可以被拥有者,用户组和其他所有的用户读取的
                        * 普通文件 */
    1,	/* 连接数 (文件被引用的目录数) */
    0, 0,  /* 文件的UID和GID - 我们将它赋予 root */
    80, /* 用ls报告的文件大小。 */
    NULL, /* 使用节点的函数(连接,删除,等等)--我们不支持 */
    procfile_read, /* 对这个文件的读函数,当某人试图葱它读入什么时被调用。 */
    NULL /* 我们能够在这儿有一个填充文件节点的函数,允许我们修改权限和拥有权,等等。 */
  }; 





/* 初始化模块--登记 proc 文件 */
int init_module()
{
  /* proc_register[_dynamic] 成功则成功,否则失败。 */
#if LINUX_VERSION_CODE &gt; KERNEL_VERSION(2,2,0)
  /* 在 2.2版中,如果在这个结构中它为0则 proc_register 自动的分配一个动态的节点数
   * 因此不再需要proc_register_dynamic
   */
  return proc_register(&amp;proc_root, &amp;Our_Proc_File);
#else
  return proc_register_dynamic(&amp;proc_root, &amp;Our_Proc_File);
#endif
 
  /* proc_root 是 proc文件系统的根目录。这是我们想让我们的文件所处的位置 */
}


/* 清除 - 从 /proc中注销我们的文件 */
void cleanup_module()
{
  proc_unregister(&amp;proc_root, Our_Proc_File.low_ino);
}
</PRE>
<P>
<HR>
<!--Navigation Panel--><A 
href="http://yangxingjun.myrice.com/chinesehow/kernel/node17.html" 
name=tex2html575><IMG height=24 alt=next src="-proc 文件系统.files/next_motif.gif" 
width=37 align=bottom border=0></A> <A 
href="http://yangxingjun.myrice.com/chinesehow/kernel/mpg.html" 
name=tex2html571><IMG height=24 alt=up src="-proc 文件系统.files/error.htm" width=26 
align=bottom border=0></A> <A 
href="http://yangxingjun.myrice.com/chinesehow/kernel/node15.html" 
name=tex2html565><IMG height=24 alt=previous 
src="C:\Documents and Settings\nonoka\My Documents\-proc 文件系统.files\error(1).htm" 
width=63 align=bottom border=0></A> <A 
href="http://yangxingjun.myrice.com/chinesehow/kernel/node1.html" 
name=tex2html573><IMG height=24 alt=contents 
src="-proc 文件系统.files/contents_motif.gif" width=65 align=bottom border=0></A> <A 
href="http://yangxingjun.myrice.com/chinesehow/kernel/node34.html" 
name=tex2html574><IMG height=24 alt=index src="-proc 文件系统.files/index_motif.gif" 
width=43 align=bottom border=0></A> <BR><B>Next:</B> <A 
href="http://yangxingjun.myrice.com/chinesehow/kernel/node17.html" 
name=tex2html576>将 /proc 作为输入</A> <B>Up:</B> <A 
href="http://yangxingjun.myrice.com/chinesehow/kernel/mpg.html" 
name=tex2html572>Linux 内核模块编程</A> <B>Previous:</B> <A 
href="http://yangxingjun.myrice.com/chinesehow/kernel/node15.html" 
name=tex2html566>多内核版本源文件</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 + -