linux - [Solved-5 Solutions] How to find the location of the executable in C - ubuntu - red hat - debian - linux server - linux pc



Linux - Problem :

How to find the location of the executable in C ?

Linux - Solution 1:

  • On Unixes with /proc really straight and realiable way is to:
    • readlink("/proc/self/exe", buf, bufsize) (Linux)
    • readlink("/proc/curproc/file", buf, bufsize) (FreeBSD)
    • readlink("/proc/self/path/a.out", buf, bufsize) (Solaris)
  • On Unixes without /proc (i.e. if above fails):
    • If argv[0] starts with "/" (absolute path) this is the path.
    • Otherwise if argv[0] contains "/" (relative path) append it to cwd (assuming it hasn't been changed yet).
    • Otherwise search directories in $PATH for executable argv[0].

Afterwards it may be reasonable to check whether the executable isn't actually a symlink. If it is resolve it relative to the symlink directory.

This step is not necessary in /proc method (at least for Linux). There the proc symlink points directly to executable.

Note that it is up to the calling process to set argv[0] correctly. It is right most of the times however there are occasions when the calling process cannot be trusted.

  • On Windows: use GetModuleFileName(NULL, buf, bufsize)

Linux - Solution 2:

Use GetModuleFileName() function if you are using Windows.

Linux - Solution 3:

As you've discovered, argv[0] can be set to anything at all by the parent process, and so need have no relation whatsoever to the actual name of the program or its location in the file system.

However, the following heuristic often works:

  • If argv[0] is an absolute path, assume this is the full path to the executable.
  • If argv[0] is a relative path, ie, it contains a /, determine the current working directory with getcwd() and then append argv[0] to it.
  • If argv[0] is a plain word, search $PATH looking for argv[0], and append argv[0] to whatever directory you find it in.

Note that all of these can be circumvented by the process which invoked the program in question. Finally, you can use linux-specific techniques, such as mentioned by emg-2. There are probably equivalent techniques on other operating systems.

Even supposing that the steps above give you a valid path name, you still might not have the path name you actually need. The presence of hard links means that you can have the following situation:

-- assume /app/bin/foo is the actual program
$ mkdir /some/where/else
$ ln /app/bin/foo /some/where/else/foo     # create a hard link to foo
$ /some/where/else/foo
click below button to copy the code. By - Linux tutorial - team

Now, the approach above will give /some/where/else/foo as the real path to the program. And, in fact, it is a real path to the program, just not the one you wanted. Note that this problem doesn't occur with symbolic links which are much more common in practice than hard links.

Linux - Solution 4:

The problem of finding the location of running executable is quite tricky and platform-specific in Linux and Unix.

If you need your executable location for discovering some configuration or resource files, maybe you should follow the Unix way of placing files in the system: put configs to /etc or /usr/local/etc or in current user home directory, and /usr/share is a good place to put your resource files.

Linux - Solution 5:

In many POSIX systems you could check a simlink located under /proc/PID/exe. For examples:

# file /proc/*/exe
/proc/1001/exe: symbolic link to /usr/bin/distccd
/proc/1023/exe: symbolic link to /usr/sbin/sendmail.sendmail
/proc/1043/exe: symbolic link to /usr/sbin/crond
click below button to copy the code. By - Linux tutorial - team

Related Searches to - linux - linux tutorial - How to find the location of the executable in C ?