Re: copy_to_user() returns an error

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Could u tell me specifically where u r getting error and what's the error.

Becoz if u r trying to copy this strucher to user -
struct myStruct
        {
        char* name;
        int value1;
         }data1,data2;
data.name="hello";
data.value1=20;
--
copy_to_user(arg,&data,sizeof(data)  //this will fail
--
becoz it think it will just copy the pointer not the value. This pointer is bad address for user.
 Also make sure u allocate space for variable name in user space.
----
__copy_from_user(&data2, (void*)arg, sizeof(data2)) ;
__copy_to_user(data2.name,data1.name,.....);
   data2.value1= data1.value1;
 __copy_to_user((void*)arg, &data2, sizeof(data2));
---
 

I think it should work.Correct me if i am wrong.




-manisha

On 8/20/06, John Que <qwejohn@xxxxxxxxx> wrote:
Hello,
I am trying to copy a table (an array of structs) from
kernel to user space in a kernel module ioctl method.

I do it thus;
...
...
struct myStruct
        {
        char* name;
        int value1;
        int value2;
        struct myStruct *next;
        };

static struct myStruct* myTable = NULL;
there is allocation of memOry for myTable in the module init() thus:
myTable = kmalloc( MAXELEM* sizeof(struct MYStruct), GFP_KERNEL);
and it is filled with values.

(MAXENTIRES is 1000 - maybe it is too big ??)
...
...
...
static int my_ioctl(struct inode* inode,
                                                                        struct file* file,
                                                                                unsigned int cmd,
                                                                                unsigned long data)
  {
        switch (cmd)
                {
                case MYIOCTL:
                        {
                        int res;

                        res = copy_to_user((void __user*)data,
                                                                                relayTable,
                                                                                numberOfPorts* sizeof(struct relayStruct));


                        printk("res is %d\n",res);
                        }
                ...
                ...
                }

copy_to_user() should return 0 in case of success;
I keep getting positive number after calling copy_to_user();
(it is 240 or 160 or 80, depends on which trial I made)

I also tried to copy only one element (numberOfPorts=1) , and
I had also tried : copy_to_user((struct relayStruct*)data,
and also copy_to_user() returned error.

When I try to use this IOCTL from user space I get garbage
for the table entries.

Any ideas what is wrong here?

Regards,
John

--
Kernelnewbies: Help each other learn about the Linux kernel.
Archive:       http://mail.nl.linux.org/kernelnewbies/
FAQ:           http://kernelnewbies.org/faq/



[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux