The headline is very clumsy. These two days in the toss of the matter, the toss of the results recorded.
First of all, in the application layer how to do this thing, very simple:
[CPP] view plain copy print? #include <stdio.h> #include <unistd.h> int main () {char link[100], path[100]; sprintf (link, "/proc/%d/exe", Getpid ()); Readlink (link, path, sizeof (path)); printf ("%s/n", Path); return 0; #include <stdio.h> #include <unistd.h> int main () {char link[100], path[100]; sprintf (link, "/proc/%d/exe ", Getpid ()); Readlink (link, path, sizeof (path)); printf ("%s/n", Path); return 0; }
At the kernel layer, all of the information for the current process is contained in present. There are several related variables in current:
1, Current->comm: is a 16-byte sized char array that records the segment path of the current process. That is, if the full path to the executable of the current process is/home/yaog/test, then the Comm content is "test", but Comm cannot get the full path.
2, CURRENT->MM/CURRENT->ACTIVE_MM: These two variables when mm_struct structure, mm_struct used to describe virtual memory. and current->mm
/CURRENT->ACTIVE_MM is the page information that is used to describe the current process. This looks a little plausible. Sure enough:
[CPP] view plain copy print? #if linux_version_code >= 0x020616 p->mm->mmap->vm_file->f_path.dentry->d_name.name #else p->mm ->mmap->vm_file->f_dentry->d_name.name #endif #if linux_version_code >= 0x020616 p->mm->mmap-& Gt;vm_file->f_path.dentry->d_name.name #else P->mm->mmap->vm_file->f_dentry->d_name.name # endif
You can get "test" in "/home/yaog/test", and then based on
[CPP] view plain copy print? #if linux_version_code >=0x020616 p->mm->mmap->vm_file->f_path.dentry->d_parent #else P->MM-&G T;mmap->vm_file->f_dentry->d_parent #endif #if linux_version_code >=0x020616 p->mm->mmap->vm_ File->f_path.dentry->d_parent #else p->mm->mmap->vm_file->f_dentry->d_parent #endif
Get Father node, is "/home/yaog/test" in the "Yaog", so in turn to look up. Until you find the "/".
This is not finished: The executable file may be mounted, and the mount point information will be obtained:
[CPP] view plain copy print? #if linux_version_code >= 0x020616 current->mm->mmap->vm_file->f_path.mnt->mnt_mountpoint->d_ Name.name #else current->mm->mmap->vm_file->f_vfsmnt->mnt_mountpoint->d_name.name #endif #if LIN Ux_version_code >= 0x020616 Current->mm->mmap->vm_file->f_path.mnt->mnt_mountpoint->d_ Name.name #else current->mm->mmap->vm_file->f_vfsmnt->mnt_mountpoint->d_name.name #endif
Then put the two pieces together.
For example, the path of an executable file in its filesystem is "/yaog/test" and the filesystem is mounted on the system's "/home" node.
Then the path of this executable file is "/home" and "/yaog/test" Together--> "/home/yaog/test".
3. Unfortunately, there is a problem with the above method. It works well on some Linux systems, but in some systems, by the way, it's the "/lib/xxxx.so" path, not the "/home/yaog/test" we want. (/home/yaog/test called this so)
The final solution goes back to the application layer:
[CPP] view plain copy print? FILP = Filp_open ("/proc/(current->pid)/exe", 0,0) Filp = Filp_open ("/proc/(current->pid)/exe", 0,0)
After you get this FILP, then follow the 2 method to find it again, OK