Through command, you can get the full path of the exe running like this:
# ls -l /proc/<pid>/path/a.out
For e.g.
# ls -l /proc/$$/path/a.out
lrwxrwxrwx 1 root root 0 Nov 24 17:19 /proc/14921/path/a.out -> /usr/bin/bash
This way you can get the executable path.
More convinient way is:
# readlink -f /proc/<pid>/path/a.out
For e.g.:
# readlink -f /proc/$$/path/a.out
/usr/bin/bash
And programmatically you can do it like this:
#include <stdio.h>
#include <unistd.h>
#define BUF_SIZE 1024
int main(int argc, char *argv[]) {
char outbuf[BUF_SIZE] = {'\0'};
char inbuf[BUF_SIZE] = {'\0'};
ssize_t len;
if (argc != 2) {
printf ("Invalid argument\n");
return -1;
}
snprintf (inbuf, BUF_SIZE, "/proc/%s/path/a.out", argv[1]);
if ((len = readlink(inbuf, outbuf, BUF_SIZE-1)) != -1) {
outbuf[len] = '\0';
} else {
perror ("readlink failed: ");
return -1;
}
printf ("%s\n", outbuf);
return 0;
}
It's usage:
# ./a.out <pid>