docs
[mirrors/Programs.git] / c / lkm / proc.c
CommitLineData
21c4e167
H
1/* Standard includes for modules */
2#include <linux/kernel.h>
3#include <linux/module.h>
4#include <linux/init.h>
5
6/* for proc_dir_entry and create_proc_entry */
7#include <linux/proc_fs.h>
8
9/* For sprintf and snprintf */
10#include <linux/string.h>
11
12/* For copy_from_user */
13#include <linux/uaccess.h>
14
15MODULE_LICENSE("GPL");
16MODULE_AUTHOR("Ormi <ormi.ormi@gmail.com>");
17MODULE_DESCRIPTION("Simple module using procfs");
18
19static char our_buf[256];
20
21int buf_read(char *buf, char **start, off_t offset, int count, int *eof, void *data)
22{
23 int len;
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);
27 return len;
28}
29
30/* When user writes to our entry. For example "echo "aa" > /proc/test_ptoc". "aa" will be stored in our_buf.
31Then, when user reads from our entry(cat /proc/test_proc) he will see "aa" */
32static int buf_write(struct file *file, const char *buf, unsigned long count, void *data)
33{
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 */
36 if(count > 255)
37 count = 255;
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';
42 return count;
43}
44
45int __init start_module(void)
46{
47
48 /* We create our entry */
49 struct proc_dir_entry *de = create_proc_entry("test_proc", 0666, 0);
50
51 /* Set pointers to our functions reading and writing */
52 de->read_proc = buf_read;
53 de->write_proc = buf_write;
54
55 /* We initialize our_buf with some text. */
56 sprintf(our_buf, "hello");
57
58 return 0 ;
59}
60
61void __exit exit_module(void)
62{
63 /* We delete our entry */
64 remove_proc_entry("test_proc", NULL);
65}
66
67module_init(start_module);
68module_exit(exit_module);
This page took 0.221814 seconds and 4 git commands to generate.