diff options
Diffstat (limited to 'src/lib')
-rw-r--r-- | src/lib/pid.c | 42 |
1 files changed, 11 insertions, 31 deletions
diff --git a/src/lib/pid.c b/src/lib/pid.c index 3cf0df909..5e9b20c94 100644 --- a/src/lib/pid.c +++ b/src/lib/pid.c | |||
@@ -74,8 +74,14 @@ void pid_get_cpu_time(unsigned pid, unsigned *utime, unsigned *stime) { | |||
74 | if (fgets(line, PIDS_BUFLEN - 1, fp)) { | 74 | if (fgets(line, PIDS_BUFLEN - 1, fp)) { |
75 | char *ptr = line; | 75 | char *ptr = line; |
76 | // jump 13 fields | 76 | // jump 13 fields |
77 | |||
78 | // end of comm string | ||
79 | ptr = strchr(ptr, ')'); | ||
80 | if (ptr == NULL) | ||
81 | goto myexit; | ||
82 | |||
77 | int i; | 83 | int i; |
78 | for (i = 0; i < 13; i++) { | 84 | for (i = 0; i < 11; i++) { |
79 | while (*ptr != ' ' && *ptr != '\t' && *ptr != '\0') | 85 | while (*ptr != ' ' && *ptr != '\t' && *ptr != '\0') |
80 | ptr++; | 86 | ptr++; |
81 | if (*ptr == '\0') | 87 | if (*ptr == '\0') |
@@ -273,36 +279,9 @@ void pid_print_list(unsigned index, int nowrap) { | |||
273 | print_elem(index, nowrap); | 279 | print_elem(index, nowrap); |
274 | } | 280 | } |
275 | 281 | ||
276 | // recursivity!!! | ||
277 | void pid_store_cpu(unsigned index, unsigned parent, unsigned *utime, unsigned *stime) { | ||
278 | if (pids[index].level == 1) { | ||
279 | *utime = 0; | ||
280 | *stime = 0; | ||
281 | } | ||
282 | |||
283 | // Remove unused parameter warning | ||
284 | (void)parent; | ||
285 | |||
286 | unsigned utmp = 0; | ||
287 | unsigned stmp = 0; | ||
288 | pid_get_cpu_time(index, &utmp, &stmp); | ||
289 | *utime += utmp; | ||
290 | *stime += stmp; | ||
291 | |||
292 | unsigned i; | ||
293 | for (i = index + 1; i < (unsigned)max_pids; i++) { | ||
294 | if (pids[i].parent == (pid_t)index) | ||
295 | pid_store_cpu(i, index, utime, stime); | ||
296 | } | ||
297 | |||
298 | if (pids[index].level == 1) { | ||
299 | pids[index].utime = *utime; | ||
300 | pids[index].stime = *stime; | ||
301 | } | ||
302 | } | ||
303 | |||
304 | // mon_pid: pid of sandbox to be monitored, 0 if all sandboxes are included | 282 | // mon_pid: pid of sandbox to be monitored, 0 if all sandboxes are included |
305 | void pid_read(pid_t mon_pid) { | 283 | void pid_read(pid_t mon_pid) { |
284 | unsigned old_max_pids = max_pids; | ||
306 | FILE *fp = fopen("/proc/sys/kernel/pid_max", "r"); | 285 | FILE *fp = fopen("/proc/sys/kernel/pid_max", "r"); |
307 | if (fp) { | 286 | if (fp) { |
308 | int val; | 287 | int val; |
@@ -314,12 +293,13 @@ void pid_read(pid_t mon_pid) { | |||
314 | } | 293 | } |
315 | 294 | ||
316 | if (pids == NULL) { | 295 | if (pids == NULL) { |
296 | old_max_pids = max_pids; | ||
317 | pids = malloc(sizeof(Process) * max_pids); | 297 | pids = malloc(sizeof(Process) * max_pids); |
318 | if (pids == NULL) | 298 | if (pids == NULL) |
319 | errExit("malloc"); | 299 | errExit("malloc"); |
320 | } | 300 | } |
321 | 301 | ||
322 | memset(pids, 0, sizeof(Process) * max_pids); | 302 | memset(pids, 0, sizeof(Process) * old_max_pids); |
323 | pid_t mypid = getpid(); | 303 | pid_t mypid = getpid(); |
324 | 304 | ||
325 | DIR *dir; | 305 | DIR *dir; |
@@ -424,7 +404,7 @@ void pid_read(pid_t mon_pid) { | |||
424 | closedir(dir); | 404 | closedir(dir); |
425 | 405 | ||
426 | // update max_pid | 406 | // update max_pid |
427 | max_pids = new_max_pids; | 407 | max_pids = new_max_pids + 1; |
428 | 408 | ||
429 | pid_t pid; | 409 | pid_t pid; |
430 | for (pid = 0; pid < max_pids; pid++) { | 410 | for (pid = 0; pid < max_pids; pid++) { |