Obtain the absolute path of the executable file corresponding to the current process in the Linux kernel.

Source: Internet
Author: User

The title is very vague. I have been tossing this issue over the past two days. I will record the results.

Let's talk about how to do this at the application layer. It's easy:

# Include <stdio. h> <br/> # include <unistd. h> </P> <p> int main () {<br/> char link [100], path [100]; <br/> sprintf (link, "/proc/% d/EXE", getpid (); <br/> readlink (link, path, sizeof (PATH )); <br/> printf ("% s/n", PATH); <br/> return 0; <br/>}

At the kernel layer, all information about the current process is included in current. There are several related variables in current:

 

1. Current-> COMM: a 16-byte char array that records the segment path of the current process. That is to say, if the full path of the executable file of the current process is/home/yaog/test, the comm content is "test", but the full path cannot be obtained from comm.

 

2. Current-> mm/current-> active_mm: Specifies the mm_struct structure when the two variables are used to describe the virtual memory. And current-> MM

/Current-> active_mm is used to describe the page information of the current process. This looks a bit reliable. Sure enough:

# If linux_version_code> = 0x020616 <br/> P-> MM-> MMAP-> vm_file-> f_path.dentry-> d_name.name <br/> # else <br/> P-> MM-> MMAP-> vm_file-> f_dentry-> d_name.name <br/> # endif

You can get "test" in "/home/yaog/test ".

# If linux_version_code> = 0x020616 <br/> P-> MM-> MMAP-> vm_file-> f_path.dentry-> d_parent <br/> # else <br/> P-> MM-> MMAP-> vm_file-> f_dentry-> d_parent <br/> # endif

Get the Father's Day point, that is, "yaog" in "/home/yaog/test", so that you can search for it in turn. Until "/" is found.

This is not complete yet: the executable file may be mounted and Its mount point information should also be obtained:

# If linux_version_code> = 0x020616 <br/> current-> MM-> MMAP-> vm_file-> f_path.mnt-> mnt_mountpoint-> d_name.name <br/> # else <br/> current-> MM-> MMAP-> vm_file-> f_vfsmnt-> mnt_mountpoint-> d_name.name <br/> # endif

Then we can combine the two parts.

For example, the path of an executable file in the file system is "/yaog/test", and the file system is mounted on the "/home" node of the system.

The path of the executable file is put together by "/home" and "/yaog/test" --> "/home/yaog/test ".

 

3. Unfortunately, the above method has a problem. It works well on some Linux systems, but in some systems, according to the above method, we get "/lib/xxxx. so, rather than the desired "/home/yaog/test ". (/Home/yaog/test calls this so)

 

The final solution goes back to the application layer:

Filp = filp_open ("/proc/(current-> PID)/EXE", 0, 0)

After obtaining the filp, find it again using the method in 2, and then OK.

 

 

 

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.