九鼎创展论坛中文版English
登录 | 立即注册 设为首页收藏本站 切换到宽版
查看: 4776|回复: 0
打印 上一主题 下一主题

proc编程(proc_create_data)

[复制链接]
跳转到指定楼层
楼主
发表于 2014-10-21 23:18:27 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
[size=13.333333015441895px]test_main.c:
  1. #include <linux/init.h>
  2. #include <linux/module.h>
  3. #include <linux/types.h>
  4. #include <linux/slab.h>
  5. #include <linux/fs.h>
  6. #include <linux/proc_fs.h>
  7. #include <linux/seq_file.h>
  8. #include <net/net_namespace.h>
  9. #include <linux/mm.h>
  10. MODULE_LICENSE("GPL");
  11. struct _DATA_INFO{
  12.     int data1;
  13.     int data2;
  14. };
  15. static struct _DATA_INFO data_info[2];

  16. /* PROC stuff */
  17. static void *dl_seq_start(struct seq_file *s, loff_t *pos)
  18. {
  19.     static unsigned long counter = 0;
  20.     if ( *pos == 0 )
  21.     {
  22.         return &counter;
  23.     }
  24.     else
  25.     {
  26.         *pos = 0;
  27.         return NULL;
  28.     }
  29. }
  30. static void *dl_seq_next(struct seq_file *s, void *v, loff_t *pos)
  31. {
  32.     return NULL;
  33. }
  34. static void dl_seq_stop(struct seq_file *s, void *v)
  35. {
  36. }
  37. static int dl_seq_show(struct seq_file *s, void *v)
  38. {
  39.     struct proc_dir_entry *pde = s->private;
  40.     struct _DATA_INFO *info = pde->data;
  41.     seq_printf(s, "%d----%d",info->data1,info->data2);
  42.     return 0;
  43. }
  44. static struct seq_operations dl_seq_ops = {
  45.     .start = dl_seq_start,
  46.     .next = dl_seq_next,
  47.     .stop = dl_seq_stop,
  48.     .show = dl_seq_show
  49. };
  50. static int dl_proc_open(struct inode *inode, struct file *file)
  51. {
  52.     int ret = seq_open(file, &dl_seq_ops);
  53.     if (!ret) {
  54.     struct seq_file *sf = file->private_data;
  55.     sf->private = PDE(inode);
  56.     }
  57.     return ret;
  58. }
  59. static const struct file_operations dl_file_ops = {
  60.     .owner = THIS_MODULE,
  61.     .open = dl_proc_open,
  62.     .read = seq_read,
  63.     .llseek = seq_lseek,
  64.     .release = seq_release
  65. };
  66. void init_mem(void)
  67. {
  68.     data_info[0].inflow=1;
  69.     data_info[0].upflow=2;
  70.     proc_create_data("proc_test1", 0, init_net.proc_net, &dl_file_ops, &data_info[0]);
  71.     data_info[1].inflow=3;
  72.     data_info[1].upflow=4;
  73.     proc_create_data("proc_test2", 0, init_net.proc_net, &dl_file_ops, &data_info[1]);
  74. }
  75. static int __init init_mem_pool(void)
  76. {
  77.     init_mem();
  78.     return 0;
  79. }
  80. static void __exit exit_mem_pool(void)
  81. {
  82.     remove_proc_entry("proc_test1", init_net.proc_net);
  83.     remove_proc_entry("proc_test2", init_net.proc_net);
  84. }
  85. module_init(init_mem_pool);
  86. module_exit(exit_mem_pool);

  87. Makefile:
  88. TARGET = test_main
  89. CURRENT = $(shell uname -r)
  90. KDIR = /lib/modules/$(CURRENT)/build
  91. PWD = $(shell pwd)
  92. CF = *.o .*o.d .*.cmd .*.flags *.mod.c Module.* modules.* .tmp_*
  93. obj-m := $(TARGET).o
  94. default:
  95.     make -C $(KDIR) SUBDIRS=$(PWD) modules
  96. clean:
  97.     -rm -rf $(CF)
  98.     -include $(KDIR)/Rules.make
复制代码
[size=13.333333015441895px]运行:
  1. insmod test_main.ko
  2. cat /proc/net/proc_test1
  3. cat /proc/net/proc_test2
复制代码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|小黑屋|深圳市九鼎创展科技官方论坛 ( 粤ICP备11028681号-2  

GMT+8, 2024-4-25 01:25 , Processed in 0.017093 second(s), 16 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表