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