歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> procfs信息讀取實現案例

procfs信息讀取實現案例

日期:2017/3/1 10:49:38   编辑:Linux編程

procfs信息讀取實現案例:

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

在/proc/創建文件目錄proc_sampe,然後在其下創建了兩個文件。其中sample_r可讀取數據

Copyright © Linux教程網 All Rights Reserved