#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);