blob: 201fb6d57449ea38d3ae3f45a2e91a3f409bc1c8 [file] [log] [blame]
Juan Cespedesd44c6b81998-09-25 14:48:42 +02001#if HAVE_CONFIG_H
2#include "config.h"
3#endif
4
Juan Cespedes1fe93d51998-03-13 00:29:21 +01005#include <sys/types.h>
6#include <stdio.h>
7#include <string.h>
8#include <signal.h>
Juan Cespedes273ea6d1998-03-14 23:02:40 +01009#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
Ian Wienand3219f322006-02-16 06:00:00 +010016#define MAX_DELAY 100000 /* 100000 microseconds = 0.1 seconds */
Juan Cespedes1fe93d51998-03-13 00:29:21 +010017
18/*
19 * Returns a file name corresponding to a running pid
20 */
Ian Wienand3219f322006-02-16 06:00:00 +010021char *pid2name(pid_t pid)
22{
Juan Cespedes1fe93d51998-03-13 00:29:21 +010023 char proc_exe[1024];
24
25 if (!kill(pid, 0)) {
Ian Wienand3219f322006-02-16 06:00:00 +010026 int delay = 0;
Juan Cespedes273ea6d1998-03-14 23:02:40 +010027
Juan Cespedes1fe93d51998-03-13 00:29:21 +010028 sprintf(proc_exe, "/proc/%d/exe", pid);
Juan Cespedes273ea6d1998-03-14 23:02:40 +010029
Ian Wienand3219f322006-02-16 06:00:00 +010030 while (delay < MAX_DELAY) {
Juan Cespedes273ea6d1998-03-14 23:02:40 +010031 if (!access(proc_exe, F_OK)) {
32 return strdup(proc_exe);
33 }
34 delay += 1000; /* 1 milisecond */
35 }
Juan Cespedes1fe93d51998-03-13 00:29:21 +010036 }
Juan Cespedes273ea6d1998-03-14 23:02:40 +010037 return NULL;
Juan Cespedes1fe93d51998-03-13 00:29:21 +010038}