aboutsummaryrefslogtreecommitdiffstats
path: root/src/firemon
diff options
context:
space:
mode:
authorLibravatar netblue30 <netblue30@protonmail.com>2022-06-02 17:03:36 -0400
committerLibravatar netblue30 <netblue30@protonmail.com>2022-06-02 17:03:36 -0400
commit741aaa4f59f3604f6c139fedcf06c0a2f910a60c (patch)
treea899d718e3ef65b7553eadc858eeb5b1a482fe97 /src/firemon
parentspeed up firemon (diff)
downloadfirejail-741aaa4f59f3604f6c139fedcf06c0a2f910a60c.tar.gz
firejail-741aaa4f59f3604f6c139fedcf06c0a2f910a60c.tar.zst
firejail-741aaa4f59f3604f6c139fedcf06c0a2f910a60c.zip
firemon rework
Diffstat (limited to 'src/firemon')
-rw-r--r--src/firemon/netstats.c16
-rw-r--r--src/firemon/procevent.c31
-rw-r--r--src/firemon/top.c32
3 files changed, 57 insertions, 22 deletions
diff --git a/src/firemon/netstats.c b/src/firemon/netstats.c
index 31abd5471..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
119errexit: 117errexit:
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
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 3e7323ded..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!!!
183void 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
183typedef struct node_t { 211typedef struct node_t {
184 struct node_t *next; 212 struct node_t *next;