Juan Cespedes | 1fe93d5 | 1998-03-13 00:29:21 +0100 | [diff] [blame] | 1 | #include <sys/types.h> |
| 2 | #include <stdio.h> |
| 3 | #include <string.h> |
| 4 | #include <signal.h> |
Juan Cespedes | 273ea6d | 1998-03-14 23:02:40 +0100 | [diff] [blame^] | 5 | #include <unistd.h> |
| 6 | |
| 7 | /* /proc/pid doesn't exist just after the fork, and sometimes `ltrace' |
| 8 | * couldn't open it to find the executable. So it may be necessary to |
| 9 | * have a bit delay |
| 10 | */ |
| 11 | |
| 12 | #define MAX_DELAY 100000 /* 100000 microseconds = 0.1 seconds */ |
Juan Cespedes | 1fe93d5 | 1998-03-13 00:29:21 +0100 | [diff] [blame] | 13 | |
| 14 | /* |
| 15 | * Returns a file name corresponding to a running pid |
| 16 | */ |
| 17 | char * pid2name(pid_t pid) |
| 18 | { |
| 19 | char proc_exe[1024]; |
| 20 | |
| 21 | if (!kill(pid, 0)) { |
Juan Cespedes | 273ea6d | 1998-03-14 23:02:40 +0100 | [diff] [blame^] | 22 | int delay=0; |
| 23 | |
Juan Cespedes | 1fe93d5 | 1998-03-13 00:29:21 +0100 | [diff] [blame] | 24 | sprintf(proc_exe, "/proc/%d/exe", pid); |
Juan Cespedes | 273ea6d | 1998-03-14 23:02:40 +0100 | [diff] [blame^] | 25 | |
| 26 | while(delay<MAX_DELAY) { |
| 27 | if (!access(proc_exe, F_OK)) { |
| 28 | return strdup(proc_exe); |
| 29 | } |
| 30 | delay += 1000; /* 1 milisecond */ |
| 31 | } |
Juan Cespedes | 1fe93d5 | 1998-03-13 00:29:21 +0100 | [diff] [blame] | 32 | } |
Juan Cespedes | 273ea6d | 1998-03-14 23:02:40 +0100 | [diff] [blame^] | 33 | return NULL; |
Juan Cespedes | 1fe93d5 | 1998-03-13 00:29:21 +0100 | [diff] [blame] | 34 | } |