procfs信息读取实现案例


procfs信息读取实现案例:

  1. /********************************************** 
  2.  * Author: lewiyon@hotmail.com 
  3.  * File name: proc_sample.c 
  4.  * Description: create a file "proc_example" in the /proc,  
  5.  *              which allows read. 
  6.  * Date: 2011-12-11 
  7.  * Version: V1.0 
  8.  *********************************************/  
  9.   
  10. #include <linux/kernel.h> /* We're doing kernel work */   
  11. #include <linux/module.h> /* Specifically, a module */   
  12. #include <linux/proc_fs.h>    /* Necessary because we use proc fs */   
  13. #include <asm/uaccess.h>  /* for get_user and put_user */   
  14.   
  15. #define MESSAGE_LENGTH 80   
  16. #define PROC_NAME "proc_sample"   
  17.   
  18. unsigned int flag = 100;   
  19.   
  20. static struct proc_dir_entry *proc_sample;  
  21. static struct proc_dir_entry *sample, *sample_r;  
  22.   
  23. /** 
  24.  * proc_read_data() 
  25.  * @page - buffer to write the data in 
  26.  * @start - where the actual data has been written in page 
  27.  * @offset - same meaning as the read system call 
  28.  * @count - same meaning as the read system call 
  29.  * @eof - set if no more data needs to be returned 
  30.  * @data - pointer to our soft state 
  31.  */  
  32. static int proc_read_data(char *page, char **stat, off_t off,  
  33.                           int count, int *eof, void *data)  
  34. {  
  35.     int len;  
  36.     len = sprintf(page, "jiffies = %ld\n", jiffies);  
  37.     return len;  
  38. }  
  39.   
  40. /*  
  41.  * 模块初始化  
  42.  */  
  43. static int __init sample_init(void)  
  44. {     
  45.     int ret = 0;  
  46.       
  47.     /* 
  48.      * proc_mkdir(name, parent) 
  49.      * 在parent对应的目录下创建name目录 
  50.      * 返回目录对应的proc_dir_dentry 
  51.      */  
  52.     proc_sample = proc_mkdir(PROC_NAME, NULL);    
  53.     if (NULL == proc_sample) {  
  54.         ret = -ENOMEM;    
  55.         goto proc_sample_err;  
  56.     }  
  57.     /* 
  58.      * create_proc_entry(name, mode,parent) 
  59.      * 在parent对应的目录下创建name文件 
  60.      * 返回目录对应的proc_dir_dentry 
  61.      */  
  62.     sample = create_proc_entry("sample", 0644, proc_sample);      
  63.     if (NULL == sample) {  
  64.         ret = -ENOMEM;    
  65.         goto sample_err;  
  66.     }  
  67.       
  68.     sample_r = create_proc_read_entry("sample_r", 0444,   
  69.                 proc_sample, proc_read_data, NULL);   
  70.     if (NULL == sample_r) {  
  71.         ret = -ENOMEM;    
  72.         goto sample_r_err;  
  73.     }  
  74.   
  75.     printk(KERN_INFO "Create sample\n");   
  76.     return ret;  
  77.   
  78. sample_r_err:  
  79.     remove_proc_entry("sample", proc_sample);  
  80. sample_err:  
  81.     remove_proc_entry(PROC_NAME, NULL);  
  82. proc_sample_err:  
  83.     return ret;  
  84. }  
  85.   
  86. /* 
  87.  * 模块清理 
  88.  */  
  89. static void __exit sample_exit(void)  
  90. {  
  91.     remove_proc_entry("sample", proc_sample);  
  92.     remove_proc_entry("sample_r", proc_sample);  
  93.     remove_proc_entry(PROC_NAME, NULL);  
  94.     printk(KERN_INFO "Remove /proc/proc_sample\n");  
  95. }  
  96.   
  97. module_init(sample_init);  
  98. module_exit(sample_exit);  
  99. MODULE_LICENSE("GPL");  
  100. MODULE_AUTHOR("lewiyon <lewiyon@hotmail.com>");  

在/proc/创建文件目录proc_sampe,然后在其下创建了两个文件。其中sample_r可读取数据

相关内容