Question

I'd like to share a variable between kernel and user space and I've found that it's possible with procfs. The kernel module must act in certain way if given value is set. The user space program is responsible for changing this value, but the kernel module must read it when necessary.

I know that I must create the /proc file in the kernel module. My question is, how to read the file from the kernel module?

Was it helpful?

Solution

Source : linux.die.net/lkmpg/x769.html

/**
 * 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;
}

To clarify, in Your module whenever user writes to Your file in procfs, this example shows how to handle such write.

OTHER TIPS

In kernel >= 3.10 proc_write is moved to structure file_operations where declaration of write is different, so in newest your solution won't work. You can implement typical file_operations.write(struct file *, const char __user *, size_t, loff_t *) and reference this to:

struct proc_dir_entry your_proc_dir_entry{
.proc_fops = &your_fops,
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top