Implement procfs demonstration.
[cascardo/kernel/samples/03.proc/.git] / hello_procfs.c
1 /*
2  *  Copyright (C) 2009  Thadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 2 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License along
15  *  with this program; if not, write to the Free Software Foundation, Inc.,
16  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18
19
20 #include <linux/module.h>
21 #include <linux/proc_fs.h>
22 #include <linux/seq_file.h>
23
24 MODULE_AUTHOR("Thadeu Lima de Souza Cascardo <cascardo@holoscopio.com>");
25 MODULE_LICENSE("GPL");
26
27 static int hello_show(struct seq_file *file, void *data)
28 {
29         seq_puts(file, "Hello, world!\n");
30         return 0;
31 }
32
33 #if 0
34 static void * hello_start(struct seq_file *file, loff_t *pos)
35 {
36         return NULL;
37 }
38
39 static void hello_stop(struct seq_file *file, void *data)
40 {
41 }
42
43 static void * hello_next(struct seq_file *file, void *data, loff_t *pos)
44 {
45         return NULL;
46 }
47
48 static const struct seq_operations hello_seqops =
49 {
50         .start = hello_start,
51         .stop = hello_stop,
52         .next = hello_next,
53         .show = hello_show,
54 };
55 #endif
56
57 static int hello_procfs_open(struct inode *inode, struct file *file)
58 {
59         return single_open(file, hello_show, NULL);
60 }
61
62 static const struct file_operations hello_procfs_fops =
63 {
64         .open = hello_procfs_open,
65         .read = seq_read,
66         .llseek = seq_lseek,
67         .release = seq_release,
68 };
69
70 static int hello_procfs_init(void)
71 {
72         struct proc_dir_entry *entry;
73         entry = create_proc_entry("gnu", 0666, NULL);
74         entry->proc_fops = &hello_procfs_fops;
75         return 0;
76 }
77
78 static void hello_procfs_exit(void)
79 {
80         remove_proc_entry("gnu", NULL);
81 }
82
83 module_init(hello_procfs_init);
84 module_exit(hello_procfs_exit);