-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess_info.c
42 lines (35 loc) · 1020 Bytes
/
process_info.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
38
39
40
41
42
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "process_info.h"
#define PATH_SIZE 256
#define BUF_SIZE 256
int get_process_info(pid_t pid, process_info *p_info) {
char path[PATH_SIZE];
char buf[BUF_SIZE];
FILE *fd;
snprintf(path, PATH_SIZE-1, "/proc/%d/maps", pid);
fd = fopen(path, "r");
if(fd == NULL) {
fprintf(stderr, "Failed to open %s\n", path);
return -1;
}
// parse maps file
p_info->exec_name = NULL;
while(fgets(buf, BUF_SIZE-1, fd)) {
if(strstr(buf, "r-xp") && !strstr(buf, ".so")) {
*strchr(buf, '\n') = 0; // delete \n character
p_info->base_addr = strtoul(buf, NULL, 16);
p_info->exec_name = strdup(strchr(buf, '/'));
if(p_info->exec_name == NULL)
perror("strdup");
break;
}
}
fclose(fd);
if(p_info->exec_name == NULL) {
fprintf(stderr, "Failed to parse %s\n", path);
return -1;
}
return 0;
}