blob: b3f7d6635025e0cf480b22e11c7f6c27b334c729 [file] [log] [blame]
Juan Cespedes1fe93d51998-03-13 00:29:21 +01001#include <sys/types.h>
2#include <stdio.h>
3#include <string.h>
4#include <signal.h>
Juan Cespedes273ea6d1998-03-14 23:02:40 +01005#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 Cespedes1fe93d51998-03-13 00:29:21 +010013
14/*
15 * Returns a file name corresponding to a running pid
16 */
17char * pid2name(pid_t pid)
18{
19 char proc_exe[1024];
20
21 if (!kill(pid, 0)) {
Juan Cespedes273ea6d1998-03-14 23:02:40 +010022 int delay=0;
23
Juan Cespedes1fe93d51998-03-13 00:29:21 +010024 sprintf(proc_exe, "/proc/%d/exe", pid);
Juan Cespedes273ea6d1998-03-14 23:02:40 +010025
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 Cespedes1fe93d51998-03-13 00:29:21 +010032 }
Juan Cespedes273ea6d1998-03-14 23:02:40 +010033 return NULL;
Juan Cespedes1fe93d51998-03-13 00:29:21 +010034}