copying data to userland

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

 



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/


[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