d_path articles Linux kernel absolute path of [turn]

Transfer: https://blog.csdn.net/cenziboy/article/details/8761621

A. D_path Function Description


d_path is provided by the kernel to obtain the absolute path based on function dentry and vfsmount

This function has two versions, with kernel version 2.6.25 for the demarcation

extern char *d_path(const struct path *, char *, int); 

extern char * d_path(struct dentry *, struct vfsmount *, char *, int);

Prototype path following structure

path {struct
struct * vfsmount mnt;
struct dentry * dentry;
};
only vfsmount and the dentry is a simple package just

二.获取进程路径
char* get_absolute_path(struct task_struct * task)
{
char * ret_ptr = NULL;
char * tpath = NULL ;
struct vm_area_struct * vma = NULL;
struct path base_path;

tpath = (char*)kmalloc(512, 0);
if(NULL == tpath || NULL == task)
{
return NULL;
}
memset(tpath,'\0',512);

task_lock(task);
if(task->mm && task->mm->mmap)
{
vma = task->mm->mmap;
}
else
{
task_unlock(task);
kfree(tpath);
return NULL;
}

/*
* 取得 path(a struct含dentry和vfsmount),参考自 fs/proc/base.c中proc_exe_link
*/
while(vma)
{
if ((vma->vm_flags & VM_EXECUTABLE) && vma->vm_file)
{
base_path = vma->vm_file->f_path;
break;
}
vma = vma->vm_next;
}
task_unlock(task);

/*
* 调用 d_path, 得到绝对路径
*/
ret_ptr = d_path(&base_path, tpath, 512);

return ret_ptr;
}

III. Get File Path
mainly obtained the document from the task_struct descriptor get file path based on where the file system and file dentry vfsmount

LINUX_VERSION_CODE #if> kernel_version = (2,6,25)
base_path to current- => Files-> fdt-> FD [FD] -> f_path;
#else
base_dp to current- => Files-> fdt-> FD [FD] - > f_path.dentry;
vfsmnt_ptr to current- => Files-> fdt-> FD [FD] -> f_path.mnt;
then use d_path, obtained absolute path

----------------
Disclaimer: This article is the original article CSDN bloggers "nehc", and follow CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement. .
Original link: https: //blog.csdn.net/cenziboy/article/details/8761621

Guess you like

Origin www.cnblogs.com/sky-heaven/p/12103109.html