Added buffer length to proc entry.
authorThadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
Tue, 8 Dec 2009 17:58:24 +0000 (15:58 -0200)
committerThadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
Tue, 8 Dec 2009 17:58:24 +0000 (15:58 -0200)
helloc.c

index 9f983a2..b0271df 100644 (file)
--- a/helloc.c
+++ b/helloc.c
@@ -6,6 +6,8 @@
 /* Needed for copying to/from user space */
 #include <asm/uaccess.h>
 #include <linux/slab.h>
 /* Needed for copying to/from user space */
 #include <asm/uaccess.h>
 #include <linux/slab.h>
+#include <linux/proc_fs.h>
+#include <linux/seq_file.h>
 
 MODULE_LICENSE("GPL");
 MODULE_AUTHOR("Thadeu Lima de Souza Cascardo <cascardo@holoscopio.com>");
 
 MODULE_LICENSE("GPL");
 MODULE_AUTHOR("Thadeu Lima de Souza Cascardo <cascardo@holoscopio.com>");
@@ -108,6 +110,27 @@ static struct file_operations helloc_fops = {
        .write = helloc_write,
 };
 
        .write = helloc_write,
 };
 
+static int hello_len_show(struct seq_file *seq, void *data)
+{
+       struct message *msg = seq->private;
+       seq_printf(seq, "%d\n", msg->len);
+       return 0;
+}
+
+static int hello_proc_open(struct inode *ino, struct file *filp)
+{
+       return single_open(filp, hello_len_show, hello_message[0]);
+}
+
+static struct file_operations hello_proc_fops = {
+       .owner = THIS_MODULE,
+       .open = hello_proc_open,
+       .llseek = seq_lseek,
+       .read = seq_read,
+       .release = seq_release,
+};
+
+
 /* the device number and the char device struct */
 static dev_t dev;
 static struct cdev *cdev;
 /* the device number and the char device struct */
 static dev_t dev;
 static struct cdev *cdev;
@@ -145,6 +168,7 @@ out:
 
 static int __init helloc_init(void)
 {
 
 static int __init helloc_init(void)
 {
+       struct proc_dir_entry *hello_pde;
        int r;
        r = helloc_alloc();
        if (r)
        int r;
        r = helloc_alloc();
        if (r)
@@ -167,7 +191,12 @@ static int __init helloc_init(void)
        r = cdev_add(cdev, dev, DEVICE_NUMBER);
        if (r)
                goto out_add;
        r = cdev_add(cdev, dev, DEVICE_NUMBER);
        if (r)
                goto out_add;
+       hello_pde = proc_create("hello_len", 0666, NULL, &hello_proc_fops);
+       if (!hello_pde)
+               goto out_pde;
        return 0;
        return 0;
+out_pde:
+       cdev_del(cdev);
 out_add:
        /* release memory allocated to the cdev device */
        kfree(cdev);
 out_add:
        /* release memory allocated to the cdev device */
        kfree(cdev);
@@ -181,6 +210,7 @@ out_alloc2:
 
 static void __exit helloc_exit(void)
 {
 
 static void __exit helloc_exit(void)
 {
+       remove_proc_entry("hello_len", NULL);
        /* remove the chardev from the system */
        cdev_del(cdev);
        /* release the device number allocated */
        /* remove the chardev from the system */
        cdev_del(cdev);
        /* release the device number allocated */