So I want to be able to get the full path of a running process (which I have the process ID for) without using any commands on the command line. Anyone has any ideas on how to do this?
I do have the PID, is there any function that by passing the PID can return the full path of that process as a char *?
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
.