Read and Write a /proc File

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

 



Hi,

I am following "Linux Kernel Module Programming Guide" and have a question on the reading from and writing to a /proc file. Please see the code snippet below, which is taken from the above mentioned guide.

For the full example, please see
http://www.tldp.org/LDP/lkmpg/2.6/html/x769.html

My question :
In the read function, we just copy a string to the variable "buffer" . We are not using put_user() here.
 But in write function , we see copy_from_user() being used, because we are copying a string from user space to kernel space.
Why we are not using put_user() in read function ? Are we not copying something from kernel space to user space there ? Why this difference between read and write for a proc file ?
But while reading character device file, we use put_user() in read function.

int 
procfile_read(char *buffer,
char **buffer_location,
off_t offset, int buffer_length, int *eof, void *data)
{
int ret;

printk(KERN_INFO "procfile_read (/proc/%s) called\n", PROCFS_NAME);

if (offset > 0) {
/* we have finished to read, return 0 */
ret = 0;
} else {
/* fill the buffer, return the buffer size */
memcpy(buffer, procfs_buffer, procfs_buffer_size);
ret = procfs_buffer_size;
}

return ret;
}

/**
* This function is called with the /proc file is written
*
*/
int procfile_write(struct file *file, const char *buffer, unsigned long count,
void *data)
{
/* get buffer size */
procfs_buffer_size = count;
if (procfs_buffer_size > PROCFS_MAX_SIZE ) {
procfs_buffer_size = PROCFS_MAX_SIZE;
}

/* write data to the buffer */
if ( copy_from_user(procfs_buffer, buffer, procfs_buffer_size) ) {
return -EFAULT;
}

return procfs_buffer_size;
}


Thanks,
Rajaram.


Yahoo! Mail
Bring photos to life! New PhotoMail makes sharing a breeze.

[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