Linux Kernel absolute path: d_path

Source: Internet
Author: User
1. d_path Function Description

D_path is provided by the kernel to obtain the absolute path Function Based on dentry and vfsmount.

This function has two versions. The kernel version 2.6.25 is the division.

Extern char * d_path (const struct path *, char *, INT );

Extern char * d_path (struct dentry *, struct vfsmount *, char *, INT );

The structure path prototype is as follows:

struct path {struct vfsmount *mnt;struct dentry *dentry;};

It only encapsulates vfsmount and dentry.

2. Obtain the process path

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;}/** get path (a struct contains dentry and vfsmount), refer to from FS/proc/base. 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);/** call d_path to obtain the absolute path */ret_ptr = d_path (& base_path, tpath, 512 ); return ret_ptr ;}

3. Obtain the file path

Obtaining the file path based on the file descriptor mainly involves obtaining the dentry of the file from task_struct and the file system vfsmount where the file is located.

#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;

Then you can use d_path to obtain the absolute path.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.