Chinaunix首页 | 论坛 | 博客
  • 博客访问: 7534800
  • 博文数量: 961
  • 博客积分: 15795
  • 博客等级: 上将
  • 技术积分: 16612
  • 用 户 组: 普通用户
  • 注册时间: 2010-08-07 14:23
文章分类

全部博文(961)

文章存档

2016年(1)

2015年(61)

2014年(41)

2013年(51)

2012年(235)

2011年(391)

2010年(181)

分类: 嵌入式

2012-07-24 21:04:14

参考文章:

·Proc文件系统

内核proc文件系统与seq接口(4---seq_file接口编程浅析

 

点击(此处)折叠或打开

  1. /********************* proc文件系统 *************************/

  2. static void *scull_seq_start(struct seq_file *s, loff_t *pos)
  3. {    
  4.     if (*pos >= MEMDEV_NR_DEVS)        
  5.         return NULL; /* No more to read */    
  6.     return my_devices + *pos;
  7. }

  8. static void *scull_seq_next(struct seq_file *s, void *v, loff_t *pos)
  9. {
  10.     (*pos)++;
  11.     if (*pos >= MEMDEV_NR_DEVS)    
  12.         return NULL;    
  13.     return my_devices + *pos;
  14. }
  15. static void scull_seq_stop(struct seq_file *s, void *v)
  16. {    /* Actually, there's nothing to do here */}


  17. static int scull_seq_show(struct seq_file *s, void *v)
  18. {
  19.     struct mem_dev *dev = (struct mem_dev *) v;

  20.     seq_printf(s, "%d-%d: %s\n",MAJOR((dev->cdev).dev),MINOR((dev->cdev).dev),dev->data);

  21.     return 0;
  22. }

  23. static struct seq_operations mem_seq_ops = {
  24.     .start = scull_seq_start,    
  25.     .next = scull_seq_next,
  26.     .stop = scull_seq_stop,    
  27.     .show = scull_seq_show
  28. };


  29. static int mem_proc_open(struct inode *inode, struct file *file)
  30. {    
  31.     return seq_open(file, &mem_seq_ops);
  32. }

  33. static struct file_operations mem_proc_ops = {    
  34.     .owner = THIS_MODULE,    
  35.     .open = mem_proc_open, /* 只要实现这个函数 */    
  36.     .read = seq_read,    
  37.     .llseek = seq_lseek,    
  38.     .release = seq_release
  39. };

  40. static struct proc_dir_entry *mydir, *pfile;

  41. static int mem_create_proc(void)
  42. {    
  43.     mydir = proc_mkdir("mydir", NULL);    // 创建目录
  44.     if (!mydir) {
  45.         printk(KERN_ERR "Can't create /proc/mydir\n");
  46.         return -1;
  47.     }

  48.     pfile = create_proc_entry("pool", 0666, mydir); // 创建文件
  49.     if (!pfile) {
  50.         printk(KERN_ERR "Can't create /proc/mydir/pool\n");
  51.         remove_proc_entry("mydir", NULL);

  52.         return -1;
  53.     }
  54.     else
  55.         pfile->proc_fops = &mem_proc_ops;

  56.     return 0;
  57. }

  58. static void mem_remove_proc(void)
  59. {    
  60.     remove_proc_entry("pool", mydir);
  61.     remove_proc_entry("mydir", NULL);    
  62. }

源码: test.rar   

阅读(3373) | 评论(0) | 转发(4) |
给主人留下些什么吧!~~