initial commit
[surgeon.git] / proc_utils.c
blobc1d17cd141b1c0a19d0d201b760b32fa4959b0c8
1 /*
2 * Copyright (C) 2010 gonzoj
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 #include <errno.h>
19 #include <limits.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <unistd.h>
25 #include "proc_utils.h"
27 #define PID_MAX 5
29 char *
30 read_line(FILE *file)
32 static char *line = NULL;
34 if (line)
36 free(line);
37 line = NULL;
40 int i = 0;
41 while (!feof(file))
43 if (i % 512 == 0)
45 line = (char *) realloc(line, i + 512);
46 if (line == NULL)
48 printf("err: %s\n", strerror(errno));
49 return NULL;
53 char chr = fgetc(file);
54 if (chr == EOF)
56 break;
58 if (chr == '\n')
60 line[i] = '\0';
61 break;
63 line[i++] = chr;
66 return line;
69 int
70 proc_get_object_base(const char *object, vaddr_t *base)
72 char *path =
73 (char *) malloc(strlen("/proc/") + PID_MAX + strlen("/maps") + 1);
74 if (path == NULL)
76 printf("err: %s\n", strerror(errno));
77 return 0;
79 sprintf(path, "/proc/%u/maps", pid);
80 FILE *maps = fopen(path, "r");
81 if (maps == NULL)
83 printf("err: %s\n", strerror(errno));
84 free(path);
85 return 0;
88 int found = 0;
89 char *line;
90 while ((line = read_line(maps)))
92 if (strstr(line, object))
94 char *tmp = strchr(line, '-');
95 if (tmp)
97 *tmp = '\0';
98 sscanf(line, "%lx", (long unsigned *) base);
99 found = 1;
100 break;
105 fclose(maps);
106 free(path);
108 return found;
111 char *
112 proc_get_object_path(vaddr_t object_base)
114 static char *object_path = NULL;
116 if (object_path)
118 free(object_path);
119 object_path = NULL;
122 char *path =
123 (char *) malloc(strlen("/proc/") + PID_MAX + strlen("/maps") + 1);
124 if (path == NULL)
126 printf("err: %s\n", strerror(errno));
127 return NULL;
129 sprintf(path, "/proc/%u/maps", pid);
130 FILE *maps = fopen(path, "r");
131 if (maps == NULL)
133 printf("err: %s\n", strerror(errno));
134 free(path);
135 return NULL;
138 char *line;
139 while ((line = read_line(maps)))
141 char *tmp = strchr(line, '-');
142 if (tmp)
144 *tmp++ = '\0';
145 vaddr_t base;
146 sscanf(line, "%lx", (long unsigned *) &base);
147 if (base == object_base)
149 object_path = (char *) malloc(strlen(tmp));
150 strcpy(object_path, strchr(tmp, '/'));
151 break;
156 fclose(maps);
157 free(path);
159 return object_path;
162 char *
163 proc_get_executable()
165 static char *executable;
167 if (executable)
169 free(executable);
170 executable = NULL;
173 char *path = (char *) malloc(strlen("/proc/") + PID_MAX + strlen("/exe") + 1);
174 if (path == NULL)
176 printf("err: %s\n", strerror(errno));
177 return NULL;
179 sprintf(path, "/proc/%u/exe", pid);
180 executable = (char *) malloc(PATH_MAX); // let's hope that one is defined :(
181 if (readlink(path, executable, PATH_MAX) == -1)
183 printf("err: %s\n", strerror(errno));
184 free(path);
185 return NULL;
188 free(path);
190 return executable;
194 proc_iterate_addrspace(vaddr_t *base)
196 static FILE *maps = NULL;
197 if (maps == NULL)
199 char *path = (char *) malloc(strlen("/proc/") + PID_MAX + strlen("/maps")
200 + 1);
201 if (path == NULL)
203 printf("err: %s\n", strerror(errno));
204 return 0;
206 sprintf(path, "/proc/%u/maps", pid);
207 maps = fopen(path, "r");
208 if (maps == NULL)
210 printf("err: %s\n", strerror(errno));
211 free(path);
212 return 0;
214 free(path);
217 char *line;
218 while ((line = read_line(maps)))
220 char *tmp = strchr(line, '-');
221 if (tmp)
223 *tmp++ = '\0';
224 strtok(tmp, " ");
225 tmp = strtok(NULL, " ");
226 if (strchr(tmp, 'r'))
228 strtok(NULL, " ");
229 strtok(NULL, " ");
230 strtok(NULL, " ");
231 tmp = strtok(NULL, " ");
232 if (tmp)
234 if (strstr(tmp, "/dev") == tmp)
236 continue;
239 sscanf(line, "%lx", (long unsigned *) base);
240 return 1;
242 else
244 continue;
248 fclose(maps);
249 maps = NULL;
251 return 0;