0

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?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
2rdv1
  • 25
  • 4
  • Using proc to share variables to userspace is discouraged and mainline won't accept any code that does that unless you have a really, really good reason to create a proc entry. Use sysfs instead which was built to solve this very problem. – tangrs Jan 25 '14 at 14:52
  • @tangrs , I'll try _sysfs_ then. Thanks for all your reply! – 2rdv1 Jan 26 '14 at 18:15

2 Answers2

0

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.

brainovergrow
  • 458
  • 4
  • 13
0

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,
}
marcinioski
  • 122
  • 1