How do I get the inode number of the current process's executable inside kernel module?
In Linux v0.11 task_struct
had an executable
member of type m_inode *
. I am开发者_Go百科 looking for something similar.
Does the exec/execve system call store this information anywhere or is it lost upon loading into memory?
There's no direct link like that anymore. The proc_exe_link()
function gets this information by looking for the first executable vma in the task that is mapping a file. You would do that for current
with something like:
struct dentry *dentry = NULL;
struct vfsmount *mnt = NULL;
struct vm_area_struct * vma;
down_read(¤t->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(¤t->mm->mmap_sem);
if (dentry) {
/* inode is dentry->d_inode */
}
Well, it is not lost, of course. Just that in recent Linux kernel, it is a bit complex to track it.
For a recent Linux kernel, With a pointer of 'struct task_struct', you first need to get the 'struct mm_struct', by:
mm = get_task_mm(task);
and then
exe_file = get_mm_exe_file(mm);
now you have the 'struct file' pointer to the exec file, with 'struct file', you can get its inode by:
struct inode *inode = file->f_path.dentry->d_inode;
BTW, the definition of get_mm_exe_file() is
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;
}
精彩评论