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

全部博文(961)

文章存档

2016年(1)

2015年(61)

2014年(41)

2013年(51)

2012年(235)

2011年(391)

2010年(181)

分类: 嵌入式

2011-01-14 21:42:28

  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");
阅读(1421) | 评论(0) | 转发(3) |
0

上一篇:电机控制

下一篇:oops错误分析

给主人留下些什么吧!~~