1 : #define PATH_MAX_TOSTRING(x) #x
2 : #define PATH_MAX_STRING(x) PATH_MAX_TOSTRING(x)
3 : #include <stdlib.h>
4 : #include <stdio.h>
5 : #include <string.h>
6 : #include <limits.h>
7 : #include <unistd.h>
8 : #include "platform.h"
9 : #include "shared-libraries.h"
10 :
11 : #ifndef __GLIBC__
12 : /* a crapy version of getline, because it's not included in bionic */
13 : static ssize_t getline(char **lineptr, size_t *n, FILE *stream)
14 : {
15 : char *ret;
16 : if (!*lineptr) {
17 : *lineptr = (char*)malloc(4096);
18 : }
19 : ret = fgets(*lineptr, 4096, stream);
20 : if (!ret)
21 : return 0;
22 : return strlen(*lineptr);
23 : }
24 : #endif
25 :
26 0 : SharedLibraryInfo SharedLibraryInfo::GetInfoForSelf()
27 : {
28 0 : pid_t pid = getpid();
29 0 : SharedLibraryInfo info;
30 : char path[PATH_MAX];
31 0 : snprintf(path, PATH_MAX, "/proc/%d/maps", pid);
32 0 : FILE *maps = fopen(path, "r");
33 0 : char *line = NULL;
34 0 : int count = 0;
35 0 : size_t line_size = 0;
36 0 : while (maps && getline (&line, &line_size, maps) > 0) {
37 : int ret;
38 : //XXX: needs input sanitizing
39 : unsigned long start;
40 : unsigned long end;
41 0 : char perm[6] = "";
42 : unsigned long offset;
43 0 : char name[PATH_MAX] = "";
44 : ret = sscanf(line,
45 : "%lx-%lx %6s %lx %*s %*x %" PATH_MAX_STRING(PATH_MAX) "s\n",
46 0 : &start, &end, perm, &offset, name);
47 0 : if (!strchr(perm, 'x')) {
48 : // Ignore non executable entries
49 0 : continue;
50 : }
51 0 : if (ret != 5 && ret != 4) {
52 0 : LOG("Get maps line failed");
53 0 : continue;
54 : }
55 0 : SharedLibrary shlib(start, end, offset, name);
56 0 : info.AddSharedLibrary(shlib);
57 0 : if (count > 10000) {
58 0 : LOG("Get maps failed");
59 : break;
60 : }
61 0 : count++;
62 : }
63 0 : free(line);
64 : return info;
65 : }
|