Re: copying data to userland

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Wed, Mar 27, 2002 at 02:40:17PM +0530, shahnavaz wrote:
> 	I have written a small code to understand copy_to/from_user.
> 	
> 	I know there are lots of mistake in the code.Can someone correct 
> me..*Please*.I have searched a lot in this context but in vain.I  cannot find 
> any suitable example in "Linux Device Driver "Book in this context.

Uhm, there are *plenty* of examples in that book. All character driver
examples use copy_to_user(). If you think that's not enough, there is
about 50MB worth of driver code in the drivers directory in each
kernel, so I can't imagine there isn't a suitable example.

> #include ...
> int test_major=0,major;
> char *data="copy_to_user",*buf;
> 
> struct file_operations test_fops={
> ...
> };
> 
> int init_module()
> {
> 	int count;
> 	major=register_chrdev(test_major,"Test",&test_fops);
> 	if(major < 0)
> 	{
> 		printk(KERN_WARNING"cant get major %d\n",test_major);
> 		return -1;
> 	}
> 	else
> 		printk(KERN_WARNING"major number is %d\n",major);
> 	count=copy_to_user(buf,data,13);

This will fail. copy_to_user() needs a _userland_ buffer, and you just
provided it with a NULL pointer. copy_to_user() should be used in the
read() method of your driver, not in init_module(). For a simple
example, look at the function wdt_read() in drivers/char/wdt.c .


Erik

-- 
J.A.K. (Erik) Mouw, Information and Communication Theory Group, Faculty
of Information Technology and Systems, Delft University of Technology,
PO BOX 5031, 2600 GA Delft, The Netherlands  Phone: +31-15-2783635
Fax: +31-15-2781843  Email: J.A.K.Mouw@its.tudelft.nl
WWW: http://www-ict.its.tudelft.nl/~erik/
--
Kernelnewbies: Help each other learn about the Linux kernel.
Archive:       http://mail.nl.linux.org/kernelnewbies/
FAQ:           http://kernelnewbies.org/faq/


[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux