Skip to content

Instantly share code, notes, and snippets.

@kerie
Created November 26, 2010 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kerie/716764 to your computer and use it in GitHub Desktop.
Save kerie/716764 to your computer and use it in GitHub Desktop.
#include<linux/module.h>
#include<linux/kernel.h>
#include<linux/proc_fs.h>
#include<linux/sched.h>
#include<linux/uaccess.h>
#define STRINGLEN 1024
char global_buffer[STRINGLEN];
struct proc_dir_entry *example_dir,*zhang_file,*keke_file;
int proc_read_zhang(char *page,char **start,off_t off,int count,int *eof,void *data)
{
int len;
len=sprintf(page,"pid: %d\n",current->pid);
return len;
}
int proc_read_keke(char *page,char **start,off_t off,int count,int *eof,void *data)
{
int len;
len=sprintf(page,"write: %s \n",global_buffer);
return len;
}
int proc_write_keke(struct file *file,const char *buffer,unsigned long count,void *data)
{
int len;
if(count>=STRINGLEN)
len=STRINGLEN-1;
else
len=count;
copy_from_user(global_buffer,buffer,len);
global_buffer[len]='\0';
return len;
}
int init_module()
{
example_dir=proc_mkdir("13081070",NULL);
zhang_file=create_proc_read_entry("zhang",0644,example_dir,proc_read_zhang,NULL);
keke_file=create_proc_entry("keke",0666,example_dir);
strcpy(global_buffer,"keke");
keke_file->read_proc=proc_read_keke;
keke_file->write_proc=proc_write_keke;
return 0;
}
void cleanup_module()
{
remove_proc_entry("zhang",example_dir);
remove_proc_entry("keke",example_dir);
remove_proc_entry("13081070",NULL);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment