1 /* Standard includes for modules */
2 #include <linux/kernel.h>
3 #include <linux/module.h>
4 #include <linux/init.h>
6 /* for proc_dir_entry and create_proc_entry */
7 #include <linux/proc_fs.h>
9 /* For sprintf and snprintf */
10 #include <linux/string.h>
12 /* For copy_from_user */
13 #include <linux/uaccess.h>
15 MODULE_LICENSE("GPL");
16 MODULE_AUTHOR("Ormi <ormi.ormi@gmail.com>");
17 MODULE_DESCRIPTION("Simple module using procfs");
19 static char our_buf
[256];
21 int buf_read(char *buf
, char **start
, off_t offset
, int count
, int *eof
, void *data
)
24 /* For example - when content of our_buf is "hello" - when user executes command "cat /proc/test_proc"
25 he will see content of our_buf(in our example "hello" */
26 len
= snprintf(buf
, count
, "%s", our_buf
);
30 /* When user writes to our entry. For example "echo "aa" > /proc/test_ptoc". "aa" will be stored in our_buf.
31 Then, when user reads from our entry(cat /proc/test_proc) he will see "aa" */
32 static int buf_write(struct file
*file
, const char *buf
, unsigned long count
, void *data
)
34 /* If count is bigger than 255, data which user wants to write is too big to fit in our_buf. We don't want
35 any buffer overflows, so we read only 255 bytes */
38 /* Here we read from buf to our_buf */
39 copy_from_user(our_buf
, buf
, count
);
40 /* we write NULL to end the string */
41 our_buf
[count
] = '\0';
45 int __init
start_module(void)
48 /* We create our entry */
49 struct proc_dir_entry
*de
= create_proc_entry("test_proc", 0666, 0);
51 /* Set pointers to our functions reading and writing */
52 de
->read_proc
= buf_read
;
53 de
->write_proc
= buf_write
;
55 /* We initialize our_buf with some text. */
56 sprintf(our_buf
, "hello");
61 void __exit
exit_module(void)
63 /* We delete our entry */
64 remove_proc_entry("test_proc", NULL
);
67 module_init(start_module
);
68 module_exit(exit_module
);
This page took 0.987033 seconds and 4 git commands to generate.