Skip to content

Instantly share code, notes, and snippets.

@starbops
Created May 28, 2013 12:06
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 starbops/5662285 to your computer and use it in GitHub Desktop.
Save starbops/5662285 to your computer and use it in GitHub Desktop.
#include <linux/module.h>
#include <linux/proc_fs.h>
extern int flag_ipaddr;
static struct proc_dir_entry *ipaddr_file;
static int proc_read_ipaddr(char *page, char **start, off_t off,
int count, int *eof, void *data)
{
printk("flag_ipaddr is %d\n", flag_ipaddr);
return 0;
}
static int proc_write_ipaddr(struct file *file, const char *buffer,
unsigned long count, void *data)
{
switch (buffer[0]) {
case '0':
flag_ipaddr = 0;
return count;
case '1':
flag_ipaddr = 1;
return count;
default:
return -EFAULT;
}
return count;
}
int init_module(void)
{
int ret = 0;
ipaddr_file = create_proc_entry("proc_ipaddr", 0644, NULL);
if (ipaddr_file == NULL) {
ret = -ENOMEM;
printk("Could not create proc entry\n");
} else {
ipaddr_file->read_proc = proc_read_ipaddr;
ipaddr_file->write_proc = proc_write_ipaddr;
ipaddr_file->owner = THIS_MODULE;
printk("/proc/%s initialized\n", "proc_ipaddr");
}
return ret;
}
void cleanup_module(void)
{
flag_ipaddr = 0;
remove_proc_entry("proc_ipaddr", NULL);
printk("/proc/%s removed\n", "proc_ipaddr");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment