POSIX: How do you define your own process file image so that you can execute yourself?

How to determine the file that represents the executable file of the current process?

The problem is that argv[0]it is not reliable, as if it were called through execXp, a record of the path that satisfied the unqualified team may or may not be added. In addition, any subscriber exec*can replace argv[0]with ANYTHING.

Therefore, if you run ps, you are not guaranteed what argv[0]will happen "/usr/bin/ps". In fact, you probably guarantee OPPOSITE.

I need one of the following:

  • full path name (without performing a search on the path myself, so that I do not use envp, not the one used by the shell)
  • live, pre-open file descriptor to a file corresponding to the current process image
  • some magic descriptor that matches the code segment currently in memory (not quite sure about the BSS segment)

Thus, at startup, I can quickly open FD for my own executable file (for case 1., if the file is deleted and becomes inaccessible), and then, at a much later date, call:

int fexecve(int fd, char *const argv[], char *const envp[]);

to fork/ exec(typically required for OS-X to prevent unreliability of the global and system descriptor after fork()). In other words (this example is stupid, of course):

void magicallyReplicate(argc, argv)
{
  if (!fork()) {
    magicallyExecMyself(argc, argv);
  }
}

or even just:

void startOver(argc, argv)
{
  magicallyExecMyself(argc, argv);
}

Of course, in my example, I will use different ones argvso that I can work in a different mode.

( "" : clone(), fork() lineage)

+4
1

, MacOS ( libc , , getaddrinfo segfault, fork exec) - , ​​ . main (http://unixjunkie.blogspot.co.uk/2006/02/char-apple-argument-vector.html) _NSGetExecutablePath, - , , ​​ .

. : /proc/self/exe

+2

All Articles