Chinaunix首页 | 论坛 | 博客
  • 博客访问: 437570
  • 博文数量: 113
  • 博客积分: 446
  • 博客等级: 下士
  • 技术积分: 1229
  • 用 户 组: 普通用户
  • 注册时间: 2012-12-09 16:01
个人简介

Let's go!!!!!

文章分类

全部博文(113)

文章存档

2019年(5)

2018年(4)

2017年(9)

2016年(5)

2015年(39)

2014年(6)

2013年(28)

2012年(17)

分类: LINUX

2016-01-28 15:52:58

#include
#include
#include
#include
#include

#define STRINGLEN 1024

char global_buffer[STRINGLEN];

struct proc_dir_entry *hello_file;
struct proc_dir_entry *example_dir1;
struct proc_dir_entry *example_dir2;

int proc_read_hello(char *page, char **start, off_t off, int count, int *eof,void *data) {
   int len;
   len = sprintf(page, global_buffer); //把global_buffer的内容显示给访问者
   return len;
}

int proc_write_hello(struct file *file, const char *buffer, unsigned long count,void *data) {
   unsigned long len;
   int ret;
   if(count == STRINGLEN){
       len = STRINGLEN - 1;
   }
   else{
       len = count;
   }

   ret = copy_from_user(global_buffer, buffer, len);
   global_buffer[len] = '\0';
   return len;
}

static int __init proc_test_init(void) {
   example_dir1 = proc_mkdir("proc_test1", NULL);
   example_dir2 = proc_mkdir("proc_test2", example_dir1);
   hello_file = create_proc_entry("hello", S_IRUGO, example_dir2);
   strcpy(global_buffer, "hello");
   hello_file->read_proc = proc_read_hello;
   hello_file->write_proc = proc_write_hello;
   return 0;
}

static void __exit proc_test_exit(void) {
   remove_proc_entry("hello", example_dir2);
   remove_proc_entry("proc_test2", example_dir1);
   remove_proc_entry("proc_test1", NULL);
}

module_init(proc_test_init);
module_exit(proc_test_exit);
阅读(4452) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~