-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess_info.c
52 lines (41 loc) · 1.12 KB
/
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
43
44
45
46
47
48
49
50
51
52
#include "process_info.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "globals.h"
int get_process_info(pid_t pid, process_info *p_info) {
char path[PATH_MAX];
char buf[BUF_SIZE];
FILE *fd;
int is_stop;
p_info->exec_name = NULL;
is_stop = 0;
snprintf(path, PATH_MAX-1, "/proc/%d/maps", pid);
fd = fopen(path, "r");
if(fd == NULL) {
perror("fopen");
return -1;
}
while(fgets(buf, BUF_SIZE-1, fd) != NULL && is_stop < 2) {
if(strstr(buf, "r-xp") != NULL &&
strstr(buf, ".so") == NULL) {
*strchr(buf, '\n') = 0;
p_info->host_addr = strtoul(buf, NULL, 16);
p_info->exec_name = strdup(strchr(buf, '/'));
is_stop++;
} else if(strstr(buf, LIBC_PATH) != NULL) {
p_info->libc_addr = strtoul(buf, NULL, 16);
is_stop++;
}
}
fclose(fd);
if(p_info->exec_name == NULL) {
perror("strdup");
return -1;
}
if(is_stop != 2) {
fputs("libc don't load to process", stderr);
return -1;
}
return 0;
}