diff options
-rw-r--r-- | src/firemon/netstats.c | 18 | ||||
-rw-r--r-- | src/firemon/procevent.c | 31 | ||||
-rw-r--r-- | src/firemon/top.c | 34 | ||||
-rw-r--r-- | src/fnettrace/static-ip-map | 3 | ||||
-rw-r--r-- | src/include/pid.h | 26 | ||||
-rw-r--r-- | src/lib/pid.c | 42 |
6 files changed, 90 insertions, 64 deletions
diff --git a/src/firemon/netstats.c b/src/firemon/netstats.c index 7d86ca45e..156efe67d 100644 --- a/src/firemon/netstats.c +++ b/src/firemon/netstats.c | |||
@@ -106,10 +106,8 @@ void get_stats(int parent) { | |||
106 | } | 106 | } |
107 | 107 | ||
108 | // store data | 108 | // store data |
109 | pids[parent].rx_delta = rx - pids[parent].rx; | 109 | pids[parent].option.netstats.rx = rx - pids[parent].option.netstats.rx; |
110 | pids[parent].rx = rx; | 110 | pids[parent].option.netstats.tx = tx - pids[parent].option.netstats.tx; |
111 | pids[parent].tx_delta = tx - pids[parent].tx; | ||
112 | pids[parent].tx = tx; | ||
113 | 111 | ||
114 | 112 | ||
115 | free(fname); | 113 | free(fname); |
@@ -117,10 +115,8 @@ void get_stats(int parent) { | |||
117 | return; | 115 | return; |
118 | 116 | ||
119 | errexit: | 117 | errexit: |
120 | pids[parent].rx = 0; | 118 | pids[parent].option.netstats.rx = 0; |
121 | pids[parent].tx = 0; | 119 | pids[parent].option.netstats.tx = 0; |
122 | pids[parent].rx_delta = 0; | ||
123 | pids[parent].tx_delta = 0; | ||
124 | } | 120 | } |
125 | 121 | ||
126 | 122 | ||
@@ -174,11 +170,11 @@ static void print_proc(int index, int itv, int col) { | |||
174 | ptruser = ""; | 170 | ptruser = ""; |
175 | 171 | ||
176 | 172 | ||
177 | float rx_kbps = ((float) pids[index].rx_delta / 1000) / itv; | 173 | float rx_kbps = ((float) pids[index].option.netstats.rx / 1000) / itv; |
178 | char ptrrx[15]; | 174 | char ptrrx[15]; |
179 | sprintf(ptrrx, "%.03f", rx_kbps); | 175 | sprintf(ptrrx, "%.03f", rx_kbps); |
180 | 176 | ||
181 | float tx_kbps = ((float) pids[index].tx_delta / 1000) / itv; | 177 | float tx_kbps = ((float) pids[index].option.netstats.tx / 1000) / itv; |
182 | char ptrtx[15]; | 178 | char ptrtx[15]; |
183 | sprintf(ptrtx, "%.03f", tx_kbps); | 179 | sprintf(ptrtx, "%.03f", tx_kbps); |
184 | 180 | ||
@@ -205,7 +201,7 @@ void netstats(void) { | |||
205 | while (1) { | 201 | while (1) { |
206 | // set pid table | 202 | // set pid table |
207 | int i; | 203 | int i; |
208 | int itv = 1; // 1 second interval | 204 | int itv = 3; // 3 second interval |
209 | pid_read(0); | 205 | pid_read(0); |
210 | 206 | ||
211 | // start rx/tx measurements | 207 | // start rx/tx measurements |
diff --git a/src/firemon/procevent.c b/src/firemon/procevent.c index ccc1ba1c6..4edbaab07 100644 --- a/src/firemon/procevent.c +++ b/src/firemon/procevent.c | |||
@@ -417,18 +417,18 @@ static void __attribute__((noreturn)) procevent_monitor(const int sock, pid_t my | |||
417 | sprintf(lineptr, " %u", pid); | 417 | sprintf(lineptr, " %u", pid); |
418 | lineptr += strlen(lineptr); | 418 | lineptr += strlen(lineptr); |
419 | 419 | ||
420 | char *user = pids[pid].user; | 420 | char *user = pids[pid].option.event.user; |
421 | if (!user) | 421 | if (!user) |
422 | user = pid_get_user_name(pids[pid].uid); | 422 | user = pid_get_user_name(pids[pid].uid); |
423 | if (user) { | 423 | if (user) { |
424 | pids[pid].user = user; | 424 | pids[pid].option.event.user = user; |
425 | sprintf(lineptr, " (%s)", user); | 425 | sprintf(lineptr, " (%s)", user); |
426 | lineptr += strlen(lineptr); | 426 | lineptr += strlen(lineptr); |
427 | } | 427 | } |
428 | 428 | ||
429 | 429 | ||
430 | int sandbox_closed = 0; // exit sandbox flag | 430 | int sandbox_closed = 0; // exit sandbox flag |
431 | char *cmd = pids[pid].cmd; | 431 | char *cmd = pids[pid].option.event.cmd; |
432 | if (!cmd) { | 432 | if (!cmd) { |
433 | cmd = pid_proc_cmdline(pid); | 433 | cmd = pid_proc_cmdline(pid); |
434 | } | 434 | } |
@@ -465,10 +465,10 @@ static void __attribute__((noreturn)) procevent_monitor(const int sock, pid_t my | |||
465 | 465 | ||
466 | // unflag pid for exit events | 466 | // unflag pid for exit events |
467 | if (remove_pid) { | 467 | if (remove_pid) { |
468 | if (pids[pid].user) | 468 | if (pids[pid].option.event.user) |
469 | free(pids[pid].user); | 469 | free(pids[pid].option.event.user); |
470 | if (pids[pid].cmd) | 470 | if (pids[pid].option.event.cmd) |
471 | free(pids[pid].cmd); | 471 | free(pids[pid].option.event.cmd); |
472 | memset(&pids[pid], 0, sizeof(Process)); | 472 | memset(&pids[pid], 0, sizeof(Process)); |
473 | } | 473 | } |
474 | 474 | ||
@@ -485,9 +485,9 @@ static void __attribute__((noreturn)) procevent_monitor(const int sock, pid_t my | |||
485 | 485 | ||
486 | // on uid events the uid is changing | 486 | // on uid events the uid is changing |
487 | if (proc_ev->what == PROC_EVENT_UID) { | 487 | if (proc_ev->what == PROC_EVENT_UID) { |
488 | if (pids[pid].user) | 488 | if (pids[pid].option.event.user) |
489 | free(pids[pid].user); | 489 | free(pids[pid].option.event.user); |
490 | pids[pid].user = 0; | 490 | pids[pid].option.event.user = 0; |
491 | pids[pid].uid = pid_get_uid(pid); | 491 | pids[pid].uid = pid_get_uid(pid); |
492 | } | 492 | } |
493 | 493 | ||
@@ -505,6 +505,17 @@ void procevent(pid_t pid) { | |||
505 | exit(1); | 505 | exit(1); |
506 | } | 506 | } |
507 | 507 | ||
508 | // set max_pids to the max value allowed by the kernel | ||
509 | FILE *fp = fopen("/proc/sys/kernel/pid_max", "r"); | ||
510 | if (fp) { | ||
511 | int val; | ||
512 | if (fscanf(fp, "%d", &val) == 1) { | ||
513 | if (val >= max_pids) | ||
514 | max_pids = val + 1; | ||
515 | } | ||
516 | fclose(fp); | ||
517 | } | ||
518 | |||
508 | // monitor using netlink | 519 | // monitor using netlink |
509 | int sock = procevent_netlink_setup(); | 520 | int sock = procevent_netlink_setup(); |
510 | if (sock < 0) { | 521 | if (sock < 0) { |
diff --git a/src/firemon/top.c b/src/firemon/top.c index 0633370ac..56f171f5b 100644 --- a/src/firemon/top.c +++ b/src/firemon/top.c | |||
@@ -154,8 +154,8 @@ static char *print_top(unsigned index, unsigned parent, unsigned *utime, unsigne | |||
154 | 154 | ||
155 | // cpu | 155 | // cpu |
156 | itv *= clocktick; | 156 | itv *= clocktick; |
157 | float ud = (float) (*utime - pids[index].utime) / itv * 100; | 157 | float ud = (float) (*utime - pids[index].option.top.utime) / itv * 100; |
158 | float sd = (float) (*stime - pids[index].stime) / itv * 100; | 158 | float sd = (float) (*stime - pids[index].option.top.stime) / itv * 100; |
159 | float cd = ud + sd; | 159 | float cd = ud + sd; |
160 | *cpu = cd; | 160 | *cpu = cd; |
161 | char cpu_str[10]; | 161 | char cpu_str[10]; |
@@ -179,6 +179,34 @@ static char *print_top(unsigned index, unsigned parent, unsigned *utime, unsigne | |||
179 | return rv; | 179 | return rv; |
180 | } | 180 | } |
181 | 181 | ||
182 | // recursivity!!! | ||
183 | void pid_store_cpu(unsigned index, unsigned parent, unsigned *utime, unsigned *stime) { | ||
184 | if (pids[index].level == 1) { | ||
185 | *utime = 0; | ||
186 | *stime = 0; | ||
187 | } | ||
188 | |||
189 | // Remove unused parameter warning | ||
190 | (void)parent; | ||
191 | |||
192 | unsigned utmp = 0; | ||
193 | unsigned stmp = 0; | ||
194 | pid_get_cpu_time(index, &utmp, &stmp); | ||
195 | *utime += utmp; | ||
196 | *stime += stmp; | ||
197 | |||
198 | unsigned i; | ||
199 | for (i = index + 1; i < (unsigned)max_pids; i++) { | ||
200 | if (pids[i].parent == (pid_t)index) | ||
201 | pid_store_cpu(i, index, utime, stime); | ||
202 | } | ||
203 | |||
204 | if (pids[index].level == 1) { | ||
205 | pids[index].option.top.utime = *utime; | ||
206 | pids[index].option.top.stime = *stime; | ||
207 | } | ||
208 | } | ||
209 | |||
182 | 210 | ||
183 | typedef struct node_t { | 211 | typedef struct node_t { |
184 | struct node_t *next; | 212 | struct node_t *next; |
@@ -267,7 +295,7 @@ void top(void) { | |||
267 | 295 | ||
268 | // set pid table | 296 | // set pid table |
269 | int i; | 297 | int i; |
270 | int itv = 1; // 1 second interval | 298 | int itv = 3; // 3 second interval |
271 | pid_read(0); | 299 | pid_read(0); |
272 | 300 | ||
273 | // start cpu measurements | 301 | // start cpu measurements |
diff --git a/src/fnettrace/static-ip-map b/src/fnettrace/static-ip-map index 17ffe7f82..97bed7a1f 100644 --- a/src/fnettrace/static-ip-map +++ b/src/fnettrace/static-ip-map | |||
@@ -96,6 +96,9 @@ | |||
96 | 199.59.148.0/22 Twitter | 96 | 199.59.148.0/22 Twitter |
97 | 208.80.152.0/22 Wikipedia | 97 | 208.80.152.0/22 Wikipedia |
98 | 98 | ||
99 | # StackPath | ||
100 | 151.139.0.0/16 StackPath | ||
101 | |||
99 | # Akamai | 102 | # Akamai |
100 | 23.0.0.0/12 Akamai | 103 | 23.0.0.0/12 Akamai |
101 | 23.32.0.0/11 Akamai | 104 | 23.32.0.0/11 Akamai |
diff --git a/src/include/pid.h b/src/include/pid.h index 7e235b713..d2f912b2a 100644 --- a/src/include/pid.h +++ b/src/include/pid.h | |||
@@ -31,14 +31,23 @@ typedef struct { | |||
31 | unsigned char zombie; | 31 | unsigned char zombie; |
32 | pid_t parent; | 32 | pid_t parent; |
33 | uid_t uid; | 33 | uid_t uid; |
34 | char *user; | 34 | |
35 | char *cmd; | 35 | union { |
36 | unsigned utime; | 36 | struct event_t { |
37 | unsigned stime; | 37 | char *user; |
38 | unsigned long long rx; // network rx, bytes | 38 | char *cmd; |
39 | unsigned long long tx; // networking tx, bytes | 39 | } event; |
40 | unsigned rx_delta; | 40 | |
41 | unsigned tx_delta; | 41 | struct top_t { |
42 | unsigned utime; | ||
43 | unsigned stime; | ||
44 | } top; | ||
45 | |||
46 | struct netstats_t { | ||
47 | unsigned long long rx; // network rx, bytes | ||
48 | unsigned long long tx; // networking tx, bytes | ||
49 | } netstats; | ||
50 | } option; | ||
42 | } Process; | 51 | } Process; |
43 | //extern Process pids[max_pids]; | 52 | //extern Process pids[max_pids]; |
44 | extern Process *pids; | 53 | extern Process *pids; |
@@ -52,7 +61,6 @@ char *pid_get_user_name(uid_t uid); | |||
52 | // print functions | 61 | // print functions |
53 | void pid_print_tree(unsigned index, unsigned parent, int nowrap); | 62 | void pid_print_tree(unsigned index, unsigned parent, int nowrap); |
54 | void pid_print_list(unsigned index, int nowrap); | 63 | void pid_print_list(unsigned index, int nowrap); |
55 | void pid_store_cpu(unsigned index, unsigned parent, unsigned *utime, unsigned *stime); | ||
56 | void pid_read(pid_t mon_pid); | 64 | void pid_read(pid_t mon_pid); |
57 | 65 | ||
58 | #endif | 66 | #endif |
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++) { |