kernel module: 读取现有的proc文件(proc_create)

huangapple go评论76阅读模式
英文:

kernel module: reading existing proc file (proc_create)

问题

抱歉,您提供的代码片段是英文文本,不需要翻译。如果您有其他需要翻译的内容,请随时提出。

英文:

I am an absolute newbie on Linux kernel. Sincere apologies if this has been answered. I have spent many hours and could not resolve it and hence decided to ask (reading Linux device drivers book as well). My problem statement: I would like to read a proc file (/proc/pid/maps) in my kernel module (a few more). There are numerous examples on proc_create which create a file and then write/read to it. I just want a read to the existing proc file. It appears all the previous options have been deprecated (read_proc, create_proc_read_entry and so on). An option that I read is to call proc_pid_maps_operations from task_mmu.c. This is involved when /proc/pid/maps is called? Is that the right approach? Or I can abstract it.

Code snippet of proc_create from various tutorials is here. The moment I change the name to an existing file, insmod fails.

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

答案1

得分: 0

I asked this question as I wanted to filter out /proc/pid/maps. It has a large number of entries and impacting the performance of my workload. Thanks to @0andriy and @Tsyvarev for guiding me as a newbie. I have included the code I have to filter and generate modified maps. I have attached code snippet in case it helps a newbie like me in generating their version of /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;
}

英文:

I asked this question as I wanted to filter out /proc/pid/maps. It has a large number of entries and impacting the performance of my workload. Thanks to @0andriy and @Tsyvarev for guiding me as a newbie. I have included the code I have to filter and generate modified maps. I have attached code snippet in case it helps a newbie like me in generating their version of /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;

huangapple
  • 本文由 发表于 2020年1月7日 01:39:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/59616582.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定