Linux kernel 如何获取当前进程的inode编号';内核模块中的可执行文件?

Linux kernel 如何获取当前进程的inode编号';内核模块中的可执行文件?,linux-kernel,Linux Kernel,在Linux v0.11中,task\u struct有一个可执行文件成员,类型为m\u inode*。我在找类似的东西 exec/execve系统调用是否将此信息存储在任何位置,还是在加载到内存时丢失了此信息?再也没有类似的直接链接了。proc_exe_link()函数通过查找映射文件的任务中的第一个可执行vma来获取此信息。对于当前的current,您可以使用类似于以下内容的操作: struct dentry *dentry = NULL; struct vfsmount *mnt = NU

在Linux v0.11中,
task\u struct
有一个
可执行文件
成员,类型为
m\u inode*
。我在找类似的东西


exec/execve系统调用是否将此信息存储在任何位置,还是在加载到内存时丢失了此信息?

再也没有类似的直接链接了。
proc_exe_link()
函数通过查找映射文件的任务中的第一个可执行vma来获取此信息。对于当前的
current
,您可以使用类似于以下内容的操作:

struct dentry *dentry = NULL;
struct vfsmount *mnt = NULL;
struct vm_area_struct * vma;

down_read(&current->mm->mmap_sem);

vma = current->mm->mmap;
while (vma) {
    if ((vma->vm_flags & VM_EXECUTABLE) && vma->vm_file)
        break;
    vma = vma->vm_next;
}

if (vma) {
    mnt = mntget(vma->vm_file->f_path.mnt);
    dentry = dget(vma->vm_file->f_path.dentry);
}

up_read(&current->mm->mmap_sem);

if (dentry) {
    /* inode is dentry->d_inode */
}

当然,它没有丢失。只是在最近的Linux内核中,跟踪它有点复杂

对于最近的Linux内核,指针为“struct task_struct”,首先需要通过以下方式获取“struct mm_struct”:

    mm = get_task_mm(task); 
然后

    exe_file = get_mm_exe_file(mm);
现在您有了指向exec文件的“struct file”指针,使用“struct file”,您可以通过以下方式获取其inode:

    struct inode *inode = file->f_path.dentry->d_inode;
顺便说一句,get_mm_exe_file()的定义是

struct file *get_mm_exe_file(struct mm_struct *mm)
{       
        struct file *exe_file;

        /* We need mmap_sem to protect against races with removal of
         * VM_EXECUTABLE vmas */
        down_read(&mm->mmap_sem);
        exe_file = mm->exe_file;
        if (exe_file)
                get_file(exe_file);
        up_read(&mm->mmap_sem);
        return exe_file;
}