Chinaunix首页 | 论坛 | 博客
  • 博客访问: 210130
  • 博文数量: 80
  • 博客积分: 213
  • 博客等级: 入伍新兵
  • 技术积分: 435
  • 用 户 组: 普通用户
  • 注册时间: 2011-10-27 11:13
文章分类

全部博文(80)

文章存档

2012年(46)

2011年(34)

分类:

2012-03-24 02:02:22

原文地址:proc文件系统 作者:luozhiyong131

  1. #include <linux/module.h>
  2. #include <linux/proc_fs.h>
  3. #include <asm/uaccess.h>

  4. static struct proc_dir_entry *mydir;
  5. static struct proc_dir_entry *pfile;

  6. static char msg[255];

  7. static int myproc_read(char *page, char **start, off_t off, int count, int *eof, void *data)
  8. {
  9.         int len = strlen(msg);

  10.         if (off >= len)
  11.                 return 0;

  12.         if (count > len - off)
  13.                 count = len - off;

  14.         memcpy(page + off, msg + off, count);
  15.         return off + count;
  16. }

  17. static int myproc_write(struct file *file, const char __user *buffer, unsigned long count, void *data)
  18. {
  19.         unsigned long count2 = count;

  20.         if (count2 >= sizeof(msg))
  21.                 count2 = sizeof(msg) - 1;

  22.         if (copy_from_user(msg, buffer, count2))
  23.                 return -EFAULT;

  24.         msg[count2] = '\0';
  25.         return count;
  26. }

  27. static int __init myproc_init(void)
  28. {
  29.         mydir = proc_mkdir("mydir", NULL);
  30.         if (!mydir) {
  31.                 printk(KERN_ERR "Can't create /proc/mydir\n");
  32.                 return -1;
  33.         }

  34.         pfile = create_proc_entry("pool", 0666, mydir);
  35.         if (!pfile) {
  36.                 printk(KERN_ERR "Can't create /proc/mydir/pool\n");
  37.                 remove_proc_entry("mydir", NULL);
  38.                 return -1;
  39.         }

  40.         pfile->read_proc = myproc_read;
  41.         pfile->write_proc = myproc_write;

  42.         return 0;
  43. }

  44. static void __exit myproc_exit(void)
  45. {
  46.         remove_proc_entry("pool", mydir);
  47.         remove_proc_entry("mydir", NULL);
  48. }

  49. module_init(myproc_init);
  50. module_exit(myproc_exit);

  51. MODULE_LICENSE("GPL");
  52. MODULE_AUTHOR("David Xie");
阅读(626) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~