[PLUG] Problem in proc write

paras beparas at gmail.com
Tue Jun 23 23:09:35 PDT 2009


Hello Sir,

I wrote a module for writing in /proc filesystem.

************************** hello.c ******************************
#include <linux/module.h>       /* Needed by all modules */
#include <linux/kernel.h>       /* Needed for KERN_INFO */
#include <linux/proc_fs.h>
#include <asm/uaccess.h>               /* For copy_from_user() */
#include <linux/init.h>
#include <linux/sched.h>
#include <linux/slab.h>

struct proc_dir_entry *phandle, *handle;

static int mychrdev_proc_write(struct file *filep, const char *buffer,
unsigned long length, void *data){
    int ret, val;

        printk(KERN_INFO "Entering Function %s \n", __FUNCTION__);

        ret = copy_from_user(&val,  buffer, length);
            if(ret < 0) {
                    printk(KERN_INFO "copy_from_user fail\n");
            }

        printk(KERN_INFO "val = %d \n", val);
        return length;
}

static int __init  mychrdev_init(void){
        printk(KERN_INFO "Entering Function %s \n", __FUNCTION__);

    phandle = proc_mkdir("test", NULL);
    if(phandle == NULL){
        printk(KERN_ERR "fail proc_mkdir \n");
        return -1;
    }

    handle = create_proc_entry("mychrdev1", 0666, phandle);
    if(handle == NULL){
        printk(KERN_ERR "fail create_proc_entry \n");
        return -1;
    }

    handle->write_proc = mychrdev_proc_write;
    handle->data = (void *) "1";

    return 0;
}

static void __exit mychrdev_exit(void){
        printk(KERN_INFO "Entering Function %s \n", __FUNCTION__);

        remove_proc_entry("mychrdev1", phandle);
        remove_proc_entry("test", NULL);
}

MODULE_LICENSE("GPL");
module_init(mychrdev_init);
module_exit(mychrdev_exit);

****************************** END
**********************************************


When I run this program, the o/p is
--------------------------------- O/P
------------------------------------------

/ # echo 1 > /proc/test/mychrdev1
Entering Function mychrdev_proc_write
val = 822738944
...........................???????
/ # echo 2 > /proc/test/mychrdev1
Entering Function mychrdev_proc_write
val = 839551684
...............................?????
/ # echo "1" > /proc/test/mychrdev1
Entering Function mychrdev_proc_write
val = 822738944
/ #

-------------------------------- END
--------------------------------------------

So my question is Why val variable gives us this garbage value ?


Thanks In Advance


More information about the plug-mail mailing list