LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel
User Name
Password
Linux - Kernel This forum is for all discussion relating to the Linux kernel.

Notices


Reply
  Search this Thread
Old 02-22-2008, 02:53 PM   #1
leoremoto
LQ Newbie
 
Registered: Feb 2008
Posts: 3

Rep: Reputation: 0
problems with copy_to_user()


Hi all,

I'm trying to use the function copy_to_user() to transfer an integer into user space but it transfers only 1 byte. For example, if I try to transfer the integer 300, it transfers only the first byte from 300:

300=12Ch

in this case only 2Ch will be transfered, so in the user space, with the function fread(), I get always 44 (44 = 2Ch). I have tried to change the parameters from fread(), to take more bytes, but it didn't work. I'm still getting only 1 byte per transfer.

Any Idea what could be this problem?

Thank you,
Leonardo
 
Old 02-22-2008, 03:12 PM   #2
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
Quote:
Originally Posted by leoremoto View Post
Any Idea what could be this problem?
The function copy_to_user takes the number of bytes as its third parameter.
 
Old 02-22-2008, 03:18 PM   #3
leoremoto
LQ Newbie
 
Registered: Feb 2008
Posts: 3

Original Poster
Rep: Reputation: 0
"The function copy_to_user takes the number of bytes as its third parameter."

I know, I have used also sizeof() in the third parameter but it does not work
 
Old 02-22-2008, 03:29 PM   #4
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
Would you show us some sample code?
 
Old 02-22-2008, 03:54 PM   #5
leoremoto
LQ Newbie
 
Registered: Feb 2008
Posts: 3

Original Poster
Rep: Reputation: 0
ssize_t read(struct file *filp, int __user *buff,
size_t count, loff_t *offp)
{
int value;
copy_to_user(buf, &value,4);
return 1;
}


This is a simplfied version of this function's implementation. The rest is the same as in the "linux Device Drivers" book.
thank you for your help
Leonardo

Last edited by leoremoto; 02-22-2008 at 03:59 PM.
 
Old 02-22-2008, 04:18 PM   #6
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
Quote:
Originally Posted by leoremoto View Post
return 1;
You should return the number of bytes read (not 1, but 4). Other than that, it looks OK (except you never actually assign to value, and you have an extra ‘f’ in one of your variable names).

Btw, you if you enclose sourcecode like this: [code]Paste code here[/code], it is easier to read on the forum.

Last edited by osor; 02-22-2008 at 04:19 PM.
 
Old 02-26-2008, 04:29 AM   #7
bay_sic
LQ Newbie
 
Registered: Feb 2008
Posts: 2

Rep: Reputation: 0
I seem to have stumbled on same kind of problem.

No matter how many bytes I try to send only one byte goes through. In kernel space I do like this:

Code:
copy_to_user(buf, &data_buf, 32);
So I want 32 bytes transferred which are in a buffer data_buf. In user space I have something like this:

Code:
bytes_read = fread(buf, size, count, MYDEVICE);
printf("\nFile size: %i", ftell(MYDEVICE));
fread should return how many data elements have been succesfully read. And that should go to same value as count, if I understood correctly. If I increase size from 1 that means bytes_read goes to zero. File size seems also to be 1B. Is this how it should be ??

As I said only first data byte gets through and I have hard time figuring that out...


Fixed it now. I had a couple of mishaps there. One of them was to somehow mix size and count in fread. Silly me!

Last edited by bay_sic; 02-27-2008 at 01:52 AM. Reason: Fixed it.
 
Old 07-20-2012, 07:19 AM   #8
dsw248
LQ Newbie
 
Registered: Jul 2012
Posts: 1

Rep: Reputation: Disabled
u may mistake the copy to user()

copy_to _user(__user *buf,__ker *buf,count) is transmit the address,and it has the bits limit (8 bit at most), the value count is to point out how many values that will transmit at the address

example :
int the test program

int distance[3];
...
read(fd, R_buf, 3);

int the read function of device driver


static ElemType dsw248_read(
struct file *file,
char __user *buf,
size_t count ,
loff_t *f_pos)
{
char ker_buf[3];
......
copy_to_user(buf, ker_buf, count);
return count;
}

that will trans 3 numbers at most and each number such as ker_buf[0] is 8 bit, that way 258 will return 2 (256+2) the 256 lost;


you can use __put_user() or __copy_to_user () to trans int numbers


使用 __put_user() 来实现数据复制。说明一下,put_user 和 copy_to_user 都是有内核地址检查的。 put_user() 是有 access_ok() 代码的 __put_user(), 其执行小数据(1、2、4、8字节)的复制,效率比 copy_to_user 稍高。

Last edited by dsw248; 07-20-2012 at 07:22 AM.
 
Old 07-20-2012, 01:28 PM   #9
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,610
Blog Entries: 4

Rep: Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905
Hey... the Linux kernel is stuffed with "working" examples of this function. Simply grep one, and copy it exactly!

"Use the Source, Luke!!"
 
  


Reply


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
copy_to_user within module. maheshstms Linux - Kernel 5 02-11-2014 05:04 AM
Cannot use copy_to_user inside the open() sys call routine archieval Programming 4 05-26-2007 09:09 AM
Kernel Programming - copy_to_user jamie_barrow Programming 1 08-13-2006 09:31 AM
in what library is "copy_to_user"? hesher Programming 4 04-16-2004 05:21 PM
Problems, problems, problems. Lets start with the ES 1868 AudioDrive Kre8ive Linux - Newbie 1 08-06-2003 07:04 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel

All times are GMT -5. The time now is 09:03 AM.

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
Open Source Consulting | Domain Registration