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

Re: passing array from kernel space to user space



On Sat, 2006-04-01 at 00:27 +0200, Vincenzo Mallozzi wrote:
> Hi all,
> I've created an array with the kmalloc function in my
> kernel module and I want to pass it in user space by
> means of the ioctl commands but I don't know how can I
> perform such an operation.
> I've heard about copy_to_user function but I haven't
> understood how I can use it in order to pass a whole
> array.
> Can anyone help me? There are other solutions you can
> suggest me?
> Thanks.
> 
> Vincenzo Mallozzi

An array is just a special pointer. It can be passed using
copy_to_user(). 

It can be used as a pointer.

/* To create a character array of size ARRAY_SIZE */
char *p = kmalloc(sizeof(char) * ARRAY_SIZE), GPF_KERNEL);
if(copy_to_user(user_buffer, p, ARRAY_SIZE)){
   return -EFAULT;
}

If it is an array of pointers, then 
I suggest you create two ioctl calls
1) to get the size of the array.
2) to get a member of the array for a particular index.
   This member can be copied to user space using copy_to_user.

hope it helps
taha







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