Linux kernel 絕對路徑之d_path篇

來源:互聯網
上載者:User
一. 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,得到絕對路徑了

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.