Skip to content

Instantly share code, notes, and snippets.

@kerie
Created November 26, 2010 14:22
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/716772 to your computer and use it in GitHub Desktop.
Save kerie/716772 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, *hello_file, *current_file, *symlink;
int proc_read_current (char *page, char **start, off_t off, int count, int *eof, void *data)
{
int len;
//MOD_INC_USE_COUNT;
try_module_get(THIS_MODULE);
len=sprintf(page, "cunt process usages: \nname: %s \npid: %d\n", current->comm, current->pid);
//MOD_DEC_USE_COUNT;
module_put(THIS_MODULE);
return len;
}
int proc_read_hello(char *page, char **start, off_t off, int count, int *eof, void *data)
{
int len;
try_module_get(THIS_MODULE);
len=sprintf(page, "hello message:\n %s write: %s\n", current->comm, global_buffer);
module_put(THIS_MODULE);
return len;
}
int proc_write_hello(struct file *fiel, const char *buffer, unsigned long count, void *data)
{
int len;
try_module_get(THIS_MODULE);
if(count>=STRINGLEN)
len=STRINGLEN-1;
else
len=count;
copy_from_user(global_buffer, buffer, len);
global_buffer[len]='\0';
module_put(THIS_MODULE);
return len;
}
int init_module()
{
example_dir=proc_mkdir("proc_test", NULL);
current_file=create_proc_read_entry("current", 0644, example_dir, proc_read_current, NULL);
hello_file=create_proc_entry("hello", 0666, example_dir);
strcpy(global_buffer, "hello");
hello_file->read_proc=proc_read_hello;
hello_file->write_proc=proc_write_hello;
return 0;
}
void cleanup_module()
{
remove_proc_entry("current_too", example_dir);
remove_proc_entry("hello", example_dir);
remove_proc_entry("current", example_dir);
remove_proc_entry("proc_test", NULL);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment