Tech Support > Operating Systems > Linux / Variants > copy_to_user vs sprintf
copy_to_user vs sprintf
Posted by Hagit on January 28th, 2004


I just cant get it!
I wrote a module that created its own entry in the /proc file system.
My module implemented read and write functions. When the user call
read(fd, &buff, 1) I am trying to copy to the user buff the character
B. When I use the
copy_to_user() function, the user doesnt get my B, but when I use the
sprintf() function the user gets the B. I cant understand it. what is
wrong???
The relevant calls are described below:

char raed_info = 'B';

copy_to_user(page, &read_info, sizeof(read_info));

sprintf(page,"%c",read_info);


Thanks
Hagit

Posted by Victor Nazarov on January 28th, 2004


Hagit wrote:
I assume this to be `read_info'

in the read(fd, &buff, 1) call you must supply only one char to user,
but sprintf writes 2 ('B', '\0') so it's not right way.
Why don't you use memcpy (page, &read_info, sizeof (read_info)). I think
this should work.

copy_to_user seems to be not Standart C Library function neither POSIX
one. What is the code?

Vir


Posted by Dances With Crows on January 28th, 2004


On Wed, 28 Jan 2004 16:34:14 +0300, Victor Nazarov staggered into the
Black Sun and said:
Yeah, if it was mispelled in the actual code, Hagit would've gotten a
snytax error.

FWIW, I had a look in /usr/src/linux/drivers/char/rtc.c , where they use
sprintf() instead of copy_to_user() when the user reads the /proc entry.
In that particular module, copy_to_user is only used once, in the
rtc_ioctl function.

copy_to_user is a Linux-kernel specific function that copies data from
kernel space to user space. It's used all over the place and is defined
in /usr/src/linux/include/asm-i386/uaccess.h for the x86, at least.

--
Matt G|There is no Darkness in Eternity/But only Light too dim for us to see
Brainbench MVP for Linux Admin / mail: TRAP + SPAN don't belong
http://www.brainbench.com / Hire me!
-----------------------------/ http://crow202.dyndns.org/~mhgraham/resume


Similar Posts