/proc 虚拟文件系统(实例)

  Linux下有一个神奇的目录/proc,经常会运行 cat /proc/cpuinfo 命令查看cpu信息,/proc下的确有cpuinfo文件,但是这个文件不是物理存在的,是软件虚拟出来的,与普通文件不同,该文件是动态的。通过/proc可以实现用户态与内核态之间的通信。在内核模式下,可以很方便的创建/proc子目录,并进行读写操作,只不过此时你需要实现文件读写接口,因为内核不知道如何处理该文件。

   下面创建/proc/test目录,并新建log文件,进行读写操作。

 

一.系统API

extern struct proc_dir_entry proc_mkdir(const char *dir_name,struct proc_dir_entry *parent);

新建/proc子目录,如parent为NULL,则在/proc根下建立目录

extern struct proc_dir_entry proc_create_entry(const char *name,mode_t mode,struct proc_dir_entry *parent);

在/proc下新建虚拟文件

extern void *remove_proc_entry(const char *name,struct proc_dir_entry *parent);

删除新建的文件或目录 

 

二.编码

C代码   收藏代码
  1. #ifndef __KERNEL__  
  2. #define __KERNEL__  
  3. #endif  /* __KERNEL__ */  
  4.   
  5. #include <linux/module.h>  
  6. #include <linux/kernel.h>  
  7. #include <linux/init.h>  
  8. #include <linux/types.h>  
  9. #include <linux/netdevice.h>  
  10. #include <linux/proc_fs.h>  
  11. #include <linux/inet.h>  
  12. #include <linux/vmalloc.h>  
  13.   
  14. #define MAX_COOKIE_LENGTH PAGE_SIZE  
  15.   
  16. static struct proc_dir_entry *test_proc_dir;  
  17. static struct proc_dir_entry *log_proc_dir;  
  18. static char *cookie_pot;     // 内核缓冲区,用于写数据  
  19. static int tValue = 12;      // 显示值  
  20.   
  21. // 读取日志文件函数  
  22. int ProcLogRead( char *buffer,  
  23.                         char **start,  
  24.                         off_t offset,  
  25.                         int length,  
  26.                         int *eof,  
  27.                         void *data )  
  28. {  
  29.     int len;  
  30.     if( offset > 0 )  
  31.     {  
  32.         *eof = 1;  
  33.         return 0;  
  34.     }  
  35.   
  36.     len = sprintf(buffer, "number:%x\n",tValue);  
  37.     return len;  
  38. }  
  39.   
  40.   
  41. // 写日志文件函数  
  42. int ProcLogWrite( struct file *filp,  
  43.                             const char __user *buff,  
  44.                             unsigned long len,  
  45.                             void *data)  
  46. {  
  47.     if( copy_from_user( cookie_pot,buff,len ) )  // 拷贝用户空间值至内核缓冲区  
  48.     {  
  49.         return -EFAULT;  
  50.     }  
  51.       
  52.     sscanf(cookie_pot,"%d",&tValue);    // 保存至全局变量tValue  
  53.     printk(KERN_ALERT "%s len:%lu vl:%d\n",cookie_pot,len,tValue);  
  54.     return len;  
  55. }  
  56.   
  57.   
  58. static int __init testproc_init(void)  
  59. {  
  60.     int ret = 0;  
  61.   
  62.     printk(KERN_ALERT "proc test init\n");  
  63.   
  64.     cookie_pot = (char*)vmalloc( MAX_COOKIE_LENGTH );   // 为内核缓冲区分配空间  
  65.     if(!cookie_pot)  
  66.     {  
  67.         ret = -ENOMEM;  
  68.     }  
  69.     else  
  70.     {  
  71.         memset(cookie_pot,0,MAX_COOKIE_LENGTH);  
  72.   
  73.         test_proc_dir = proc_mkdir("test",init_net.proc_net);       // 新建/proc/net/test目录,注:2.6.32以上内核,用init_net.proc_net取代先前的pro_net  
  74.         log_proc_dir = create_proc_entry("log",0644,test_proc_dir);     // 新建文件 /proc/net/test/log  
  75.   
  76.         if(test_proc_dir == NULL  
  77.             || log_proc_dir == NULL)  
  78.         {  
  79.             ret = -ENOMEM;  
  80.             vfree(cookie_pot);  
  81.         }  
  82.         else  
  83.         {  
  84.             // 注册读写函数  
  85.             log_proc_dir->read_proc = ProcLogRead;  
  86.             log_proc_dir->write_proc = ProcLogWrite;  
  87.         }  
  88.     }  
  89.   
  90.     return 0;  
  91. }  
  92.   
  93.   
  94. static void __exit testproc_exit(void)  
  95. {  
  96.     printk(KERN_ALERT "clean test proc\n");  
  97.   
  98.     remove_proc_entry("log",test_proc_dir);     // 删除log文件  
  99.     remove_proc_entry("test",init_net.proc_net);    // 删除test目录  
  100.   
  101.     vfree(cookie_pot);  
  102. }  
  103.   
  104. module_init(testproc_init);  
  105. module_exit(testproc_exit);  
  106.   
  107. MODULE_LICENSE("Dual BSD/GPL");  
  108. MODULE_AUTHOR("kettas");  
  109. MODULE_DESCRIPTION("proc test");  
  110. MODULE_VERSION("1.0.0");  
  111. MODULE_ALIAS("Proc 01");  

 

三.编译运行

 

Shell代码   收藏代码
  1. [scada@linux proc_test]$ sudo insmod proc_test.ko  
  2. [scada@linux proc_test]$ ll /proc/net/test/  
  3. 总用量 0  
  4. -rw-r--r--. 1 root root 0 1月  11 22:14 log  
  5. [scada@linux proc_test]$ cat /proc/net/test/log   
  6. number:c  

 

 

四.接口操作

    上面用cat命令直接查看log文件,既然内核提供了通用的read(),write()文件访问接口,那试试。

C代码   收藏代码
  1. #include <unistd.h>  
  2. #include <stdio.h>  
  3. #include <sys/types.h>  
  4. #include <sys/stat.h>  
  5. #include <fcntl.h>  
  6. #include <assert.h>  
  7. #include <string.h>  
  8.   
  9. int main(int argc,char **argv)  
  10. {  
  11.     int fd = open("/proc/net/test/log",O_RDWR,0);  
  12.     assert(fd != -1);  
  13.   
  14.     char *vl = "10";  // 此处为字符串   
  15.   
  16.     // write  
  17.     int ret = write(fd,vl,strlen(vl));  
  18.     printf("ret:%d\n",ret);  
  19.   
  20.     // read  
  21.     char buff[100] = {};  
  22.     ret = read(fd,buff,100);  
  23.     assert(ret != -1);  
  24.   
  25.     printf("read:%s\n",buff);  
  26.     close(fd);  
  27.   
  28.     return 0;  
  29. }  

 

运行:

Shell代码   收藏代码
  1. [scada@linux proc_test]$ sudo ./write_test  
  2. ret:2  
  3. read:number:a  

   向/proc/net/test/log写入10后,显示了16进制结果a,测试OK

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值