LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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



Reply
 
Search this Thread
Old 05-28-2012, 01:31 AM   #1
sindhu4sind
Member
 
Registered: Apr 2012
Posts: 38

Rep: Reputation: Disabled
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
 
Old 05-28-2012, 06:13 AM   #2
TheIndependentAquarius
Senior Member
 
Registered: Dec 2008
Posts: 4,633
Blog Entries: 29

Rep: Reputation: 897Reputation: 897Reputation: 897Reputation: 897Reputation: 897Reputation: 897Reputation: 897
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.
Old 05-28-2012, 09:18 AM   #3
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 943Reputation: 943Reputation: 943Reputation: 943Reputation: 943Reputation: 943Reputation: 943Reputation: 943
Quote:
Originally Posted by Anisha Kaul View Post
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 View Post
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.
Old 05-28-2012, 11:57 PM   #4
sindhu4sind
Member
 
Registered: Apr 2012
Posts: 38

Original Poster
Rep: Reputation: Disabled
Thank you Anisha Kaul, and Nominal Animal.. Its working now..
 
Old 05-29-2012, 12:01 AM   #5
sindhu4sind
Member
 
Registered: Apr 2012
Posts: 38

Original Poster
Rep: Reputation: Disabled
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!!
 
Old 05-29-2012, 01:00 AM   #6
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 943Reputation: 943Reputation: 943Reputation: 943Reputation: 943Reputation: 943Reputation: 943Reputation: 943
Quote:
Originally Posted by sindhu4sind View Post
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.
  


Reply

Tags
copy, kernel, userspace


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Wifi not working in Eee PPC 1000HE (Debian Squeeze), but ethernet is working someshpr Linux - Laptop and Netbook 8 10-10-2011 04:59 PM
copy_from_user and copy_to_user example krisonearth Programming 5 09-29-2009 06:08 PM
SED - minor changes work - Larger doesn't (working and non working code included) Nimoy Programming 17 09-22-2007 05:34 PM
Fortemedia FM801 card not working under FC5 on Intel 845 but working with windows morningkiran Linux - Hardware 0 11-30-2006 08:57 AM
acpi nearly working IBM thinkpad R40e 2684HVG no Fn key working, throttling OK Emmanuel_uk Linux - Laptop and Netbook 2 05-31-2006 02:41 AM


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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration