fix firemon, speed-up

This commit is contained in:
netblue30 2022-04-29 09:31:04 -04:00
parent c9d191df68
commit 54baf62b58
4 changed files with 23 additions and 14 deletions

1
.gitignore vendored
View file

@ -46,6 +46,7 @@ src/bash_completion/firejail.bash_completion
src/zsh_completion/_firejail
src/jailcheck/jailcheck
src/fnettrace/fnettrace
src/fzenity/fzenity
uids.h
seccomp
seccomp.debug

View file

@ -47,7 +47,7 @@ static char *get_user_name(uid_t uid) {
static char *get_header(void) {
char *rv;
if (asprintf(&rv, "%-5.5s %-9.9s %-10.10s %-10.10s %s",
if (asprintf(&rv, "%-7.7s %-9.9s %-10.10s %-10.10s %s",
"PID", "User", "RX(KB/s)", "TX(KB/s)", "Command") == -1)
errExit("asprintf");
@ -183,7 +183,7 @@ static void print_proc(int index, int itv, int col) {
sprintf(ptrtx, "%.03f", tx_kbps);
char buf[1024 + 1];
snprintf(buf, 1024, "%-5.5s %-9.9s %-10.10s %-10.10s %s",
snprintf(buf, 1024, "%-7.7s %-9.9s %-10.10s %-10.10s %s",
pidstr, ptruser, ptrrx, ptrtx, ptrcmd);
if (col < 1024)
buf[col] = '\0';

View file

@ -47,7 +47,7 @@ static char *get_user_name(uid_t uid) {
static char *get_header(void) {
char *rv;
if (asprintf(&rv, "%-5.5s %-9.9s %-8.8s %-8.8s %-5.5s %-4.4s %-9.9s %s",
if (asprintf(&rv, "%-7.7s %-9.9s %-8.8s %-8.8s %-5.5s %-4.4s %-9.9s %s",
"PID", "User", "RES(KiB)", "SHR(KiB)", "CPU%", "Prcs", "Uptime", "Command") == -1)
errExit("asprintf");
@ -165,7 +165,7 @@ static char *print_top(unsigned index, unsigned parent, unsigned *utime, unsigne
char prcs_str[10];
snprintf(prcs_str, 10, "%d", *cnt);
if (asprintf(&rv, "%-5.5s %-9.9s %-8.8s %-8.8s %-5.5s %-4.4s %-9.9s %s",
if (asprintf(&rv, "%-7.7s %-9.9s %-8.8s %-8.8s %-5.5s %-4.4s %-9.9s %s",
pidstr, ptruser, rss, shared, cpu_str, prcs_str, uptime_str, ptrcmd) == -1)
errExit("asprintf");

View file

@ -30,7 +30,7 @@
#define PIDS_BUFLEN 4096
//Process pids[max_pids];
Process *pids = NULL;
int max_pids=32769;
int max_pids=32769; // recalculated for every read_pid() call
// get the memory associated with this pid
void pid_getmem(unsigned pid, unsigned *rss, unsigned *shared) {
@ -303,20 +303,22 @@ void pid_store_cpu(unsigned index, unsigned parent, unsigned *utime, unsigned *s
// mon_pid: pid of sandbox to be monitored, 0 if all sandboxes are included
void pid_read(pid_t mon_pid) {
if (pids == NULL) {
FILE *fp = fopen("/proc/sys/kernel/pid_max", "r");
if (fp) {
int val;
if (fscanf(fp, "%d", &val) == 1) {
if (val >= max_pids)
max_pids = val + 1;
}
fclose(fp);
FILE *fp = fopen("/proc/sys/kernel/pid_max", "r");
if (fp) {
int val;
if (fscanf(fp, "%d", &val) == 1) {
if (val >= max_pids)
max_pids = val + 1;
}
fclose(fp);
}
if (pids == NULL) {
pids = malloc(sizeof(Process) * max_pids);
if (pids == NULL)
errExit("malloc");
}
memset(pids, 0, sizeof(Process) * max_pids);
pid_t mypid = getpid();
@ -332,9 +334,12 @@ void pid_read(pid_t mon_pid) {
struct dirent *entry;
char *end;
pid_t new_max_pids = 0;
while ((entry = readdir(dir))) {
pid_t pid = strtol(entry->d_name, &end, 10);
pid %= max_pids;
if (pid > new_max_pids)
new_max_pids = pid;
if (end == entry->d_name || *end)
continue;
if (pid == mypid)
@ -418,6 +423,9 @@ void pid_read(pid_t mon_pid) {
}
closedir(dir);
// update max_pid
max_pids = new_max_pids;
pid_t pid;
for (pid = 0; pid < max_pids; pid++) {
int parent = pids[pid].parent;