proc.c revision f13505251e6402460f6cc7ec84e0d8ca91607b4f
1#if HAVE_CONFIG_H 2#include "config.h" 3#endif 4 5#include <sys/types.h> 6#include <stdio.h> 7#include <string.h> 8#include <signal.h> 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 */ 17 18/* 19 * Returns a file name corresponding to a running pid 20 */ 21char * 22pid2name(pid_t pid) { 23 char proc_exe[1024]; 24 25 if (!kill(pid, 0)) { 26 int delay = 0; 27 28 sprintf(proc_exe, "/proc/%d/exe", pid); 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 } 36 } 37 return NULL; 38} 39