[PLUG] Problem in proc write

paras beparas at gmail.com
Fri Jun 26 23:29:34 PDT 2009


On Thu, Jun 25, 2009 at 11:12 AM, Shakthi Kannan <shakthimaan at gmail.com>wrote:

> Hi,
>
> --- On Wed, Jun 24, 2009 at 11:39 AM, paras<beparas at gmail.com> wrote:
> |    int ret, val;
> |
> |        printk(KERN_INFO "Entering Function %s \n", __FUNCTION__);
> |
> |        ret = copy_from_user(&val,  buffer, length);
> \--
>
> 'val' can be used as 'char *' buffer.
>
> ---
> | / # echo 1 > /proc/test/mychrdev1
> | Entering Function mychrdev_proc_write
> | val = 822738944
> \--
>
> 1. echo by default adds a \n in the output. So, if you printk the
> length value in your code, you will get length as '2'. To suppress the
> newline and just get '1' in the above, you should use:
>
>  echo -n 1 > /proc/test/mychrdev1
>
> 2. You should initialize your local variables, val, and ret to zero.
>
> If you do printk %d, %x on val for the above, it will print '49' and
> '31' respectively. They are the decimal and hexadecimal equivalents of
> the character '1' that has been passed.
>
> Also refer, 'Access the Linux kernel using the /proc filesystem':
> http://www.ibm.com/developerworks/linux/library/l-proc.html
>
> Not sure why you want to use /proc, as /sysfs is used in 2.6.
>
> SK
>

Thank you for great support.
I want to add one more point in this.
we can replace copy_from_user with sscanf .
e.g.Try Replacing..

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

with

*sscanf(buffer, "%d", &val);*




>
> --
> Shakthi Kannan
> http://www.shakthimaan.com
>
> _______________________________________
> Pune GNU/Linux Users Group Mailing List


More information about the plug-mail mailing list