Use the race free function for adding a proc file.
[cascardo/kernel/samples/03.proc/.git] / hello_procfs.c
index 1390972..5e0e082 100644 (file)
 
 
 #include <linux/module.h>
+/* Needed for procfs register functions */
 #include <linux/proc_fs.h>
+/* Needed for using the seq_file infrastructure */
+#include <linux/seq_file.h>
 
 MODULE_AUTHOR("Thadeu Lima de Souza Cascardo <cascardo@holoscopio.com>");
 MODULE_LICENSE("GPL");
 
+/* write our message to the sequential file */
+static int hello_show(struct seq_file *file, void *data)
+{
+       seq_puts(file, "Hello, world!\n");
+       return 0;
+}
+
+/* use the single sequential file */
+static int hello_procfs_open(struct inode *inode, struct file *file)
+{
+       return single_open(file, hello_show, NULL);
+}
+
+/* our file operations use the seq_file stuff to make it an easy task */
+static const struct file_operations hello_procfs_fops =
+{
+       .open = hello_procfs_open,
+       .read = seq_read,
+       .llseek = seq_lseek,
+       .release = seq_release,
+};
+
 static int hello_procfs_init(void)
 {
+       /* create the proc entry and set its fops: there is a potential race
+        * here */
+       struct proc_dir_entry *entry;
+       entry = proc_create("hellop", 0666, NULL, &hello_procfs_fops);
        return 0;
 }
 
 static void hello_procfs_exit(void)
 {
+       /* remove our proc entry */
+       remove_proc_entry("hellop", NULL);
 }
 
 module_init(hello_procfs_init);