blob: 4251c1d2a8a716ac8e4d60628d1e5ffdc6a6b290 [file] [log] [blame]
Juan Cespedesd44c6b81998-09-25 14:48:42 +02001#include "config.h"
Juan Cespedesd44c6b81998-09-25 14:48:42 +02002
Juan Cespedes1fe93d51998-03-13 00:29:21 +01003#include <sys/types.h>
4#include <stdio.h>
5#include <string.h>
6#include <signal.h>
Juan Cespedes273ea6d1998-03-14 23:02:40 +01007#include <unistd.h>
8
9/* /proc/pid doesn't exist just after the fork, and sometimes `ltrace'
10 * couldn't open it to find the executable. So it may be necessary to
11 * have a bit delay
12 */
13
Ian Wienand2d45b1a2006-02-20 22:48:07 +010014#define MAX_DELAY 100000 /* 100000 microseconds = 0.1 seconds */
Juan Cespedes1fe93d51998-03-13 00:29:21 +010015
16/*
Juan Cespedese0660df2009-05-21 18:14:39 +020017 * Returns a (malloc'd) file name corresponding to a running pid
Juan Cespedes1fe93d51998-03-13 00:29:21 +010018 */
Juan Cespedesf1350522008-12-16 18:19:58 +010019char *
20pid2name(pid_t pid) {
Juan Cespedes1fe93d51998-03-13 00:29:21 +010021 char proc_exe[1024];
22
23 if (!kill(pid, 0)) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +010024 int delay = 0;
Juan Cespedes273ea6d1998-03-14 23:02:40 +010025
Juan Cespedes1fe93d51998-03-13 00:29:21 +010026 sprintf(proc_exe, "/proc/%d/exe", pid);
Juan Cespedes273ea6d1998-03-14 23:02:40 +010027
Ian Wienand2d45b1a2006-02-20 22:48:07 +010028 while (delay < MAX_DELAY) {
Juan Cespedes273ea6d1998-03-14 23:02:40 +010029 if (!access(proc_exe, F_OK)) {
30 return strdup(proc_exe);
31 }
32 delay += 1000; /* 1 milisecond */
33 }
Juan Cespedes1fe93d51998-03-13 00:29:21 +010034 }
Juan Cespedes273ea6d1998-03-14 23:02:40 +010035 return NULL;
Juan Cespedes1fe93d51998-03-13 00:29:21 +010036}