Use the race free function for adding a proc file.
[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 /* Needed for procfs register functions */
22 #include <linux/proc_fs.h>
23 /* Needed for using the seq_file infrastructure */
24 #include <linux/seq_file.h>
25
26 MODULE_AUTHOR("Thadeu Lima de Souza Cascardo <cascardo@holoscopio.com>");
27 MODULE_LICENSE("GPL");
28
29 /* write our message to the sequential file */
30 static int hello_show(struct seq_file *file, void *data)
31 {
32         seq_puts(file, "Hello, world!\n");
33         return 0;
34 }
35
36 /* use the single sequential file */
37 static int hello_procfs_open(struct inode *inode, struct file *file)
38 {
39         return single_open(file, hello_show, NULL);
40 }
41
42 /* our file operations use the seq_file stuff to make it an easy task */
43 static const struct file_operations hello_procfs_fops =
44 {
45         .open = hello_procfs_open,
46         .read = seq_read,
47         .llseek = seq_lseek,
48         .release = seq_release,
49 };
50
51 static int hello_procfs_init(void)
52 {
53         /* create the proc entry and set its fops: there is a potential race
54          * here */
55         struct proc_dir_entry *entry;
56         entry = proc_create("hellop", 0666, NULL, &hello_procfs_fops);
57         return 0;
58 }
59
60 static void hello_procfs_exit(void)
61 {
62         /* remove our proc entry */
63         remove_proc_entry("hellop", NULL);
64 }
65
66 module_init(hello_procfs_init);
67 module_exit(hello_procfs_exit);