Re: copying data to userland

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

 




Here is a suggestion:

buf shuld be a pointer to an *user land* memory region which has to be
passed to the kernel. That is the prototype of copy_to_user:

copy_to_user(to, from, sizeof(from));

So , buf in your program is not in user land.

I dont know if there is a way of passing an argument to init_module so
that u can do a copy_to_user there. Generally , u wuld define another
fn/.., in ur case that is called when u do a read :

struct file_operations{

	read: foo_read;
};

and in foo_read:

int foo_read(unsigned long arg)
{
	char *cptr = (char *)arg;
	int ret;

	ret = copy_to_user(cptr,data,sizeof(data);

	/* check ret value for error */

	return ret;
}

User land:

int main()
{
	char buf[20];

	/* open*/

	read(fd, buf, /*whatever*/);

	printf("%s\n",buf);
}

Pointers from user land are suppossed to be used in kernel as unsigned
longs

Narasimhamurthy Giri, Clemson University Computer Science Dept.
-------------------------------------------------------------------------------
Judge not lest ye be judged yourself.
-------------------------------------------------------------------------------


On Wed, 27 Mar 2002, shahnavaz wrote:

> Hi,
> 	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.
>
> TIA
> shan
>
>
> #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);
> 	if(count < 0){
> 		printk(KERN_WARNING"data could not be copied\n");
> 	}
> 	else
> 		printk(KERN_WARNING"data copied\n");
> 	return 0;
> }
> void cleanup_module()
> {
> 	unregister_chrdev(major,"Test");
> }
>
> After compiling i have done #mknode Test c 254 0
>
> Userland program:
>
> int main()
> {
> 	char buff[13];
>
> 	int fd=open("/dev/Test",O_RDONLY);
>
> 	read(fd,&buff,13);
> 	printf("%s\n",buff);
> 	return 0;
> }
>
>
> When i run the userland program it prints garbage.
> --
> Kernelnewbies: Help each other learn about the Linux kernel.
> Archive:       http://mail.nl.linux.org/kernelnewbies/
> FAQ:           http://kernelnewbies.org/faq/
>
>

--
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