LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   copy_from_user working but copy_to_user not working! (http://www.linuxquestions.org/questions/programming-9/copy_from_user-working-but-copy_to_user-not-working-947176/)

sindhu4sind 05-28-2012 12:31 AM

copy_from_user working but copy_to_user not working!
 
Hello Helpful guys,

can anyone please point out my mistake, I am beginner,
Here in my code copy_from_user is working fine, but i don't know why copy_to_user is not working..

Here's my code:
Code:

char buf[10];
char buf1[10]= "K-HELLO";

static long device_ioctl(struct file *file, unsigned int cmd, unsigned long arg) {
        int ret_val;
        switch(cmd){
                case MESSAGEFROMUSER:
                        ret_val = copy_from_user (buf, (char *)arg, sizeof(buf));
                        printk("buffer received from user: %s\n",buf);
                        break;
                case MESSAGETOUSER:
                        ret_val = copy_to_user ((char *) arg, buf1, 10);
                        printk(KERN_ALERT"<1> buffer sent to user: %s\n",buf1);
                        break;
                default:
                        break;
        }
        return 0;
       
}
..
..
..
static void __exit my_exit(void){
    printk("Message from User : %s\n", buf);
    .....
}

Userspace code:
Code:

char buf[10]="U-HELLO";
char buf1[10];

ioctl (fd, MESSAGEFROMUSER, &buf);
ioctl (fd, MESSAGETOUSER, &buf1);
printf("\nMessage from Kernel : %s.\n", buf1);

Anyone can please make me correct..

Sindhu

TheIndependentAquarius 05-28-2012 05:13 AM

Print the "ret_val" variable and check its value w.r.t "copy_to_user".
Man page says that return values other than 0 indicate the number of bytes that couldn't
be copied.
And there are only 7 characters in "buf1" but you are asking it to copy 10.

Nominal Animal 05-28-2012 08:18 AM

Quote:

Originally Posted by Anisha Kaul (Post 4689304)
Man page says that return values other than 0 indicate the number of bytes that couldn't be copied.

Exactly. I think here is the current man page from kernel.org (The Linux Kernel API book, to be precise), here for 2.6.20.

Quote:

Originally Posted by Anisha Kaul (Post 4689304)
And there are only 7 characters in "buf1" but you are asking it to copy 10.

No, there are ten, since it's declared char[10]. The other three chars will be initialized to zero, as per array initialization rules.

sindhu4sind 05-28-2012 10:57 PM

Thank you Anisha Kaul, and Nominal Animal.. Its working now.. :)

sindhu4sind 05-28-2012 11:01 PM

And in one other scenario its ret_val is 0, But its not able to pass the message to the user space.. compiling without warning everything is fine, I am just getting a String from a function and trying to transfer it o userspace, but this is what its not working for!!

Nominal Animal 05-29-2012 12:00 AM

Quote:

Originally Posted by sindhu4sind (Post 4689892)
I am just getting a String from a function and trying to transfer it o userspace, but this is what its not working for!!

What do you mean, a String? What is that?

If you have a kernel function which returns a pointer (to a char array), then you cannot return that pointer to userspace and expect it to work. (If it did work, then it would mean userspace applications have direct access to kernel internals. They do not, so it will not work.)

You should not be using ioctl() to transfer arbitrary-length data like strings anyway; that is why read() and write() are there for.


All times are GMT -5. The time now is 03:39 PM.