Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
05-28-2012, 12:31 AM
|
#1
|
|
Member
Registered: Apr 2012
Posts: 38
Rep: 
|
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
|
|
|
|
05-28-2012, 05:13 AM
|
#2
|
|
Senior Member
Registered: Dec 2008
Location: Gurgaon, India
Distribution: OpenSUSE 11.4
Posts: 4,581
|
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.
|
|
|
1 members found this post helpful.
|
05-28-2012, 08:18 AM
|
#3
|
|
Senior Member
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
|
Quote:
Originally Posted by Anisha Kaul
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
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.
|
|
|
1 members found this post helpful.
|
05-28-2012, 10:57 PM
|
#4
|
|
Member
Registered: Apr 2012
Posts: 38
Original Poster
Rep: 
|
Thank you Anisha Kaul, and Nominal Animal.. Its working now.. 
|
|
|
|
05-28-2012, 11:01 PM
|
#5
|
|
Member
Registered: Apr 2012
Posts: 38
Original Poster
Rep: 
|
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!!
|
|
|
|
05-29-2012, 12:00 AM
|
#6
|
|
Senior Member
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
|
Quote:
Originally Posted by sindhu4sind
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.
|
|
|
2 members found this post helpful.
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 07:23 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|