-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPID_resolver.c
37 lines (31 loc) · 1.23 KB
/
PID_resolver.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
//credit to https://stackoverflow.com/questions/14805896/how-do-i-get-the-full-path-for-a-process-on-os-x, to user AlphaMale and DustinB
//I only changed error functionality to avoid printing errors constantly, and also added in check for PID size (shouldn't exceed 5, but wanted to make sure null byte was accounted for)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <libproc.h>
int main (int argc, char* argv[])
{
int count = 0;
pid_t pid; int ret;
char pathbuf[PROC_PIDPATHINFO_MAXSIZE];
if ( argc > 1 ) {
if (strlen(argv[1]) > 6)
{
printf("Not a valid PID, please enter a valid PID.\n");
exit(-1);
}
pid = (pid_t) atoi(argv[1]);
ret = proc_pidpath (pid, pathbuf, sizeof(pathbuf));
if ( ret <= 0 ) {
//leaving in error messages here for debugging later.
//fprintf(stderr, "process %d: proc_pidpath ();\n", pid);
//fprintf(stderr, " %s\n", strerror(errno));
count = count + 1; //this is nonsense just to keep the code moving, definitely not the most efficient.
} else {
printf("PID is %d: %s\n", pid, pathbuf);
}
}
return 0;
}