Linux 内核模块:读取现有的proc文件(proc_create)
我绝对是Linux内核的新手。如果已经回答了,真诚的道歉。我已经花了很多时间,无法解决它,因此决定询问(同时阅读Linux设备驱动程序手册)。我的问题陈述:我想读取内核模块中的一个proc文件(/proc/pid/maps)(还有一些)。在proc_create上有许多示例,它们创建一个文件,然后对其进行写入/读取。我只想读取现有的proc文件。似乎以前的所有选项都已弃用(read_proc、create_proc\u read_entry等)。我读到的一个选项是从task_mmu.c调用proc_pid_maps_操作。调用/proc/pid/maps时会涉及到这一点吗?这是正确的方法吗?或者我可以把它抽象出来 这里是各种教程中创建proc_的代码片段。当我将名称更改为现有文件时,insmod将失败Linux 内核模块:读取现有的proc文件(proc_create),linux,module,kernel,kernel-module,proc,Linux,Module,Kernel,Kernel Module,Proc,我绝对是Linux内核的新手。如果已经回答了,真诚的道歉。我已经花了很多时间,无法解决它,因此决定询问(同时阅读Linux设备驱动程序手册)。我的问题陈述:我想读取内核模块中的一个proc文件(/proc/pid/maps)(还有一些)。在proc_create上有许多示例,它们创建一个文件,然后对其进行写入/读取。我只想读取现有的proc文件。似乎以前的所有选项都已弃用(read_proc、create_proc\u read_entry等)。我读到的一个选项是从task_mmu.c调用pro
if (!proc_create( "testcpuinfo", // define ENTRY_NAME "hello_world"
0, // permissions 0644
NULL, // proc directory
&fops)) // file_operations
{
printk("ERROR! proc_create\n");
remove_proc_entry(ENTRY_NAME, NULL);
return -ENOMEM;
}
我问这个问题是因为我想过滤掉/proc/pid/maps。它有大量的条目,影响我工作负载的性能。感谢@0andriy和@Tsyvarev作为新手指导我。我已经包括了我必须过滤和生成修改过的地图的代码。我附上了代码片段,以防它能帮助像我这样的新手生成他们的/proc/pid/maps版本
static int proc_show(struct seq_file *s, void *v) {
struct task_struct *task;
struct pid *pid_struct;
struct mm_struct *mm;
struct vm_area_struct *vma;
unsigned long start, end;
const char *region = NULL;
// Look for task which PID was provided as parameter, falling back to current task if not found
if(pid == 0) {
printk(KERN_INFO "pages_activity: no pid argument provided, using current process instead\n");
task = current;
} else {
pid_struct = find_get_pid(pid);
if(pid_struct == NULL) {
printk(KERN_INFO "pages_activity: process with pid %d not found, using current process instead\n", pid);
task = current;
} else {
task = pid_task(pid_struct, PIDTYPE_PID);
}
}
mm = task->mm;
vma = mm->mmap;
使用专用(用户空间)ABI读取内核内部保存的信息是错误的方法。明白了。谢谢这是否意味着我使用task_mmu.c中的proc_pid_maps_操作?这就是base.c调用的例程。有什么建议吗?“我想在我的内核模块中读取一个proc文件(/proc/pid/maps)”—无论是使用
proc\u create
创建文件还是通过其他方式创建文件,其读取都是相同的。请参阅关于在内核模块中读取文件的问题: