简体   繁体   English

从C内核模块检查/ proc文件

[英]Check /proc file from C kernel module

I'd like to share a variable between kernel and user space and I've found that it's possible with procfs . 我想在内核空间和用户空间之间共享一个变量,我发现使用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. 我知道我必须在内核模块中创建/proc文件。 My question is, how to read the file from the kernel module? 我的问题是, 如何从内核模块读取文件?

Source : linux.die.net/lkmpg/x769.html 来源: 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. 为了明确起见,每当用户在procfs中写入您的文件时,此示例都会说明如何处理此类写入。

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. 在内核=> 3.10中,将proc_write移至结构file_operations,其中写入的声明不同,因此,最新的解决方案将不起作用。 You can implement typical file_operations.write(struct file *, const char __user *, size_t, loff_t *) and reference this to: 您可以实现典型的file_operations.write(结构文件*,const char __user *,size_t,loff_t *),并将其引用为:

struct proc_dir_entry your_proc_dir_entry{
.proc_fops = &your_fops,
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM