linux系统中绝对路径,Linux kernel 绝对路径之d_path篇

摘自:https://blog.csdn.net/cenziboy/article/details/8761621

一. d_path函数说明

d_path 是内核提供的根据dentry和vfsmount获取绝对路径函数

此函数有2个版本,以内核版本2.6.25为分界

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

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

结构体path原型如下

struct path {

struct vfsmount *mnt;

struct dentry *dentry;

};

仅仅是对 vfsmount 和 dentry进行了简单封装而已

二.获取进程路径

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;

}

三. 获取文件路径

根据文件描述符获取文件路径主要是从task_struct中取得文件的dentry和文件所在文件系统vfsmount

#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,25)

base_path = current->files->fdt->fd[fd]->f_path;

#else

base_dp = current->files->fdt->fd[fd]->f_path.dentry;

vfsmnt_ptr = current->files->fdt->fd[fd]->f_path.mnt;

然后就可以使用d_path,得到绝对路径了

标签:kernel,task,struct,dentry,vma,char,path,绝对路径

来源: https://www.cnblogs.com/LiuYanYGZ/p/12269567.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值