Use readlink("/proc/<pid>/exe", buf, bufsize)
to get the path to <pid>
's executable. This works on Linux, provided procfs
is available (it usually is).
Example usage:
int get_exe_for_pid(pid_t pid, char *buf, size_t bufsize) { char path[32]; sprintf(path, "/proc/%d/exe", pid); return readlink(path, buf, bufsize);}
Returns -1
on failure and sets errno
.