forked from chenxiaoyao/SoProtect
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathUtils.cpp
More file actions
80 lines (71 loc) · 1.86 KB
/
Utils.cpp
File metadata and controls
80 lines (71 loc) · 1.86 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <sys/types.h>
#include <fcntl.h>
#include "Utils.h"
#include "Log.h"
int loadMemoryMap(pid_t pid, MemoryMap *map, int *count) {
char raw[8000];
char name[MAX_NAME_LENGTH];
char *p;
unsigned long start, end;
MemoryMap *m;
int itemCount = 0, fd, returnValue;
sprintf(raw, "/proc/%d/maps", pid);
fd = open(raw, O_RDONLY);
if (fd < 0) {
GLogError("Utils", "Open process map file failed. ");
return -1;
}
memset(raw, 0, sizeof(raw));
p = raw;
while(true) {
returnValue = read(fd, p, sizeof(raw) - (p - raw));
if (returnValue < 0) {
GLogError("Utils", "Read prcess map file failed. ");
return -1;
}
if (returnValue == 0) {
break;
}
p += returnValue;
if (p > raw + sizeof(raw)) {
GLogError("Utils", "Read map file data overflow. ");
return -1;
}
}
close(fd);
p = strtok(raw, "\n");
m = map;
while (p) {
returnValue = sscanf(p, "%08lx-%08lx %*s %*s %*s %*s %s\n", &start, &end, name);
p = strtok(NULL, "\n");
if (returnValue == 2) {
m = map + itemCount++;
m->start = start;
m->end = end;
strcpy(m->name, MEMORY_ONLY);
continue;
}
int i = 0;
for (i = itemCount - 1; i >= 0; i--) {
m = map + i;
if (!strcmp(m->name, name)) {
break;
}
}
if (i >= 0) {
if (start < m->start) {
m->start = start;
}
if (end > m->end) {
m->end = end;
}
} else {
m = map + itemCount++;
m->start = start;
m->end = end;
strcpy(m->name, name);
}
}
*count = itemCount;
return 0;
}