首页 > 解决方案 > 在linux内核的系统调用中找不到pid

问题描述

使用 Ubuntu 18.04.3 x64 我将内核从 4.xx 升级到 5.1.0。然后我在下面做了一个新的系统调用。

SYSCALL_DEFINE1(get_mem_info, pid_t, input_pid)
{
    printk("Starting system call (get_mem_info)\n");
    struct task_struct *task;
    int found = 0;
    for (task = &init_task; next_task(task) != &init_task; task = next_task(task))
    {
        if (task->pid == input_pid)
        {
            found = 1;
            printk("Task was found\n");
            break;
        }
    }
    if (found)
    {
        struct vm_area_struct *vma;
        unsigned long total_size = 0;
        int i = 0;

        vma = task->mm->mmap;

        while (vma != NULL)
        {
            printk("The number %d vma's access permissions is %lu\n", i, vma->vm_page_prot.pgprot);
            printk("The file name mapped to this vma is %s\n", vma->vm_file->f_path.dentry->d_name.name);

            total_size += (vma->vm_end - vma->vm_start);
            i++;
            vma = vma->vm_next;
        }

        printk("The size of the process' virtual address space is %lu\n", total_size);
    }
    else
    {
        printk("Task not found\n");
    }
  return 0;
}

然后我编译了内核来反映这个变化。然后使用测试文件我有以下内容:

#include <linux/unistd.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>

#define __NR_get_mem_info 338


int main(int argc, char *argv[])
{
    pid_t pid = getpid();   //get current process pid
    syscall (__NR_get_mem_info, pid);
    return 0;
}

但是当我运行测试器文件时,我得到:

"Task not found."

问题出在哪里?

标签: clinux-kernelsystem-callspid

解决方案


推荐阅读