In my C++ application, my application does an
const size_t bufSize = PATH_MAX + 1;
// Read the symbolic link '/proc/self/exe'.
const char *linkName = "/proc/self/exe";
const int ret = int(readlink(linkName, dirNameBuffer, bufSize - 1));
No such file or directory.
Instead of using
readlink to discover the path to your own executable, you can directly call
/proc/self/exe. Since the kernel already has an open fd to processes that are currently executing, this will give you an fd regardless of whether the path has been replaced with a new executable or not.
Next, you can use
fexecve instead of
execv which accepts an
fd parameter instead of a
filename parameter for the executable.
int fd = open("/proc/self/exe", O_RDONLY); fexecve(fd, argv, envp);
Above code omits error handling for brevity.