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.