Re: memory allocation

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

 



As i know, you have to allocate memory either statically or dynamically  
in user space for var.string and pass pointer thro. ioctl.

copy_to_user() is not going to allocate memory, it just copies data from
kernel space to user space.

Ensure that allocated memory for string member will always be more than
len+1 (of kernel) to avoid memory corruption.

Regards,
Madhu


On Thu, 16 Jan 2003, Mandeep S. Brar wrote:

> hi,
> I am a newbie into linux programming. Ps help me with a small problem i am
> facing while writing a sample program for a driver module.
> I have a structure like
> struct devstruct {
> 	char* string;
> 	int len;
> };
> I want to return information from the driver to a user process which queries
> me with ioctl. The code has the following outline...
> 
> USER PROCESS
> ...........
> struct devstruct var;
> ioctl(fd, READ_COMMAND, &var);
> printf("%s",var.string);
> .....................
> 
> 
> DRIVER Module
> 
> char* data="hello";
> int len=5;
> 
> int myioctl (struct inode *inode, struct file *filp, unsigned int cmd,
> unsigned long arg){
> 	switch(cmd) {
> 		.............
> 
> 		case READ_COMMAND: 
> 			{
> 				.....
> 				struct devstruct* var = (struct
> devstruct*)arg;
> 				var->len = len;
> 				/*********
> 					allocate memory in user space?
> 				**********/
> 				var->string = (allocate memory = len+1 in
> user space)
> 				copy_to_user(var->string,data,len+1);
> 				.....
> 			}
> 			break;		
> 		.............
> 	}
> }
> My question is that how do i allocate memory in user space when i am in
> kernel space or will copy_to_user do it automatically?. is there a variant
> of kmalloc or what??
> 
> 
> Thanks in advance
> Mandeep
> 
> 
> --
> 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