diff options
author | Benny Baumann <BenBE@geshi.org> | 2020-11-01 01:09:51 +0100 |
---|---|---|
committer | Benny Baumann <BenBE@geshi.org> | 2020-11-02 22:15:01 +0100 |
commit | 45869513bfebba80cc2ab42e4218f68b34b1e6ac (patch) | |
tree | f064631dbff141bf1c945db8cff40b7bb82fd169 /linux | |
parent | 61e14d4bb25268593019e6df3eb02264b4ac8e0e (diff) |
Embracing branches
Diffstat (limited to 'linux')
-rw-r--r-- | linux/Battery.c | 43 | ||||
-rw-r--r-- | linux/IOPriorityPanel.c | 14 | ||||
-rw-r--r-- | linux/LinuxProcess.c | 6 | ||||
-rw-r--r-- | linux/LinuxProcessList.c | 146 | ||||
-rw-r--r-- | linux/Platform.c | 21 | ||||
-rw-r--r-- | linux/SELinuxMeter.c | 21 |
6 files changed, 182 insertions, 69 deletions
diff --git a/linux/Battery.c b/linux/Battery.c index 326f35fd..ba7d153a 100644 --- a/linux/Battery.c +++ b/linux/Battery.c @@ -52,9 +52,11 @@ static unsigned long int parseBatInfo(const char* fileName, const unsigned short struct dirent* dirEntry = readdir(batteryDir); if (!dirEntry) break; + char* entryName = dirEntry->d_name; if (!String_startsWith(entryName, "BAT")) continue; + batteries[nBatteries] = xStrdup(entryName); nBatteries++; } @@ -74,12 +76,14 @@ static unsigned long int parseBatInfo(const char* fileName, const unsigned short for (unsigned short int j = 0; j < lineNum; j++) { free(line); line = String_readLine(file); - if (!line) break; + if (!line) + break; } fclose(file); - if (!line) break; + if (!line) + break; char* foundNumStr = String_getToken(line, wordNum); const unsigned long int foundNum = atoi(foundNumStr); @@ -122,8 +126,11 @@ static ACPresence procAcpiCheck(void) { continue; } char* line = String_readLine(file); + fclose(file); - if (!line) continue; + + if (!line) + continue; char* isOnline = String_getToken(line, 2); free(line); @@ -139,8 +146,10 @@ static ACPresence procAcpiCheck(void) { } } - if (dir) + if (dir) { closedir(dir); + } + return isOn; } @@ -170,14 +179,21 @@ static inline ssize_t xread(int fd, void* buf, size_t count) { size_t alreadyRead = 0; for (;;) { ssize_t res = read(fd, buf, count); - if (res == -1 && errno == EINTR) continue; + if (res == -1) { + if (errno == EINTR) + continue; + return -1; + } + if (res > 0) { buf = ((char*)buf) + res; count -= res; alreadyRead += res; } - if (res == -1) return -1; - if (count == 0 || res == 0) return alreadyRead; + + if (count == 0 || res == 0) { + return alreadyRead; + } } } @@ -197,6 +213,7 @@ static void Battery_getSysData(double* level, ACPresence* isOnAC) { struct dirent* dirEntry = readdir(dir); if (!dirEntry) break; + const char* entryName = dirEntry->d_name; char filePath[256]; @@ -257,14 +274,18 @@ static void Battery_getSysData(double* level, ACPresence* isOnAC) { fullSize = atoi(value); totalFull += fullSize; full = true; - if (now) break; + if (now) { + break; + } continue; } value = (!now) ? match(energy, "NOW=") : NULL; if (value) { totalRemain += atoi(value); now = true; - if (full) break; + if (full) { + break; + } continue; } } @@ -288,7 +309,9 @@ static void Battery_getSysData(double* level, ACPresence* isOnAC) { char buffer[2] = ""; for (;;) { ssize_t res = read(fd3, buffer, 1); - if (res == -1 && errno == EINTR) continue; + if (res == -1 && errno == EINTR) { + continue; + } break; } close(fd3); diff --git a/linux/IOPriorityPanel.c b/linux/IOPriorityPanel.c index 9f85e792..cd4b2e60 100644 --- a/linux/IOPriorityPanel.c +++ b/linux/IOPriorityPanel.c @@ -21,7 +21,11 @@ Panel* IOPriorityPanel_new(IOPriority currPrio) { Panel_setHeader(this, "IO Priority:"); Panel_add(this, (Object*) ListItem_new("None (based on nice)", IOPriority_None)); - if (currPrio == IOPriority_None) Panel_setSelected(this, 0); + + if (currPrio == IOPriority_None) { + Panel_setSelected(this, 0); + } + static const struct { int klass; const char* name; } classes[] = { { .klass = IOPRIO_CLASS_RT, .name = "Realtime" }, { .klass = IOPRIO_CLASS_BE, .name = "Best-effort" }, @@ -33,11 +37,15 @@ Panel* IOPriorityPanel_new(IOPriority currPrio) { xSnprintf(name, sizeof(name) - 1, "%s %d %s", classes[c].name, i, i == 0 ? "(High)" : (i == 7 ? "(Low)" : "")); IOPriority ioprio = IOPriority_tuple(classes[c].klass, i); Panel_add(this, (Object*) ListItem_new(name, ioprio)); - if (currPrio == ioprio) Panel_setSelected(this, Panel_size(this) - 1); + if (currPrio == ioprio) { + Panel_setSelected(this, Panel_size(this) - 1); + } } } Panel_add(this, (Object*) ListItem_new("Idle", IOPriority_Idle)); - if (currPrio == IOPriority_Idle) Panel_setSelected(this, Panel_size(this) - 1); + if (currPrio == IOPriority_Idle) { + Panel_setSelected(this, Panel_size(this) - 1); + } return this; } diff --git a/linux/LinuxProcess.c b/linux/LinuxProcess.c index a5d7eccf..b1bd24d5 100644 --- a/linux/LinuxProcess.c +++ b/linux/LinuxProcess.c @@ -168,8 +168,9 @@ dynamically derived from the cpu nice level of the process: io_priority = (cpu_nice + 20) / 5. -- From ionice(1) man page */ static int LinuxProcess_effectiveIOPriority(const LinuxProcess* this) { - if (IOPriority_class(this->ioPriority) == IOPRIO_CLASS_NONE) + if (IOPriority_class(this->ioPriority) == IOPRIO_CLASS_NONE) { return IOPriority_tuple(IOPRIO_CLASS_BE, (this->super.nice + 20) / 5); + } return this->ioPriority; } @@ -290,8 +291,9 @@ void LinuxProcess_writeField(const Process* this, RichString* str, ProcessField case PERCENT_SWAP_DELAY: LinuxProcess_printDelay(lp->swapin_delay_percent, buffer, n); break; #endif case CTXT: - if (lp->ctxt_diff > 1000) + if (lp->ctxt_diff > 1000) { attr |= A_BOLD; + } xSnprintf(buffer, n, "%5lu ", lp->ctxt_diff); break; case SECATTR: snprintf(buffer, n, "%-30s ", lp->secattr ? lp->secattr : "?"); break; diff --git a/linux/LinuxProcessList.c b/linux/LinuxProcessList.c index 6e7906d5..e91666d0 100644 --- a/linux/LinuxProcessList.c +++ b/linux/LinuxProcessList.c @@ -56,14 +56,21 @@ static ssize_t xread(int fd, void* buf, size_t count) { size_t alreadyRead = 0; for (;;) { ssize_t res = read(fd, buf, count); - if (res == -1 && errno == EINTR) continue; + if (res == -1) { + if (errno == EINTR) + continue; + return -1; + } + if (res > 0) { buf = ((char*)buf) + res; count -= res; alreadyRead += res; } - if (res == -1) return -1; - if (count == 0 || res == 0) return alreadyRead; + + if (count == 0 || res == 0) { + return alreadyRead; + } } } @@ -78,6 +85,7 @@ static void LinuxProcessList_initTtyDrivers(LinuxProcessList* this) { int fd = open(PROCTTYDRIVERSFILE, O_RDONLY); if (fd == -1) return; + char* buf = NULL; int bufSize = MAX_READ; int bufLen = 0; @@ -160,21 +168,24 @@ static void LinuxProcessList_initNetlinkSocket(LinuxProcessList* this) { static int LinuxProcessList_computeCPUcount(void) { FILE* file = fopen(PROCSTATFILE, "r"); - if (file == NULL) + if (file == NULL) { CRT_fatalError("Cannot open " PROCSTATFILE); + } int cpus = 0; char buffer[PROC_LINE_LENGTH + 1]; while (fgets(buffer, sizeof(buffer), file)) { - if (String_startsWith(buffer, "cpu")) + if (String_startsWith(buffer, "cpu")) { cpus++; + } } fclose(file); /* subtract raw cpu entry */ - if (cpus > 0) + if (cpus > 0) { cpus--; + } return cpus; } @@ -218,16 +229,18 @@ ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidMatchList, ui // Read btime { FILE* statfile = fopen(PROCSTATFILE, "r"); - if (statfile == NULL) + if (statfile == NULL) { CRT_fatalError("Cannot open " PROCSTATFILE); + } while (true) { char buffer[PROC_LINE_LENGTH + 1]; if (fgets(buffer, sizeof(buffer), statfile) == NULL) { CRT_fatalError("No btime in " PROCSTATFILE); } else if (String_startsWith(buffer, "btime ")) { - if (sscanf(buffer, "btime %lld\n", &btime) != 1) + if (sscanf(buffer, "btime %lld\n", &btime) != 1) { CRT_fatalError("Failed to parse btime from " PROCSTATFILE); + } break; } } @@ -298,16 +311,19 @@ static bool LinuxProcessList_readStatFile(Process* process, const char* dirname, int size = xread(fd, buf, MAX_READ); close(fd); - if (size <= 0) return false; + if (size <= 0) + return false; buf[size] = '\0'; assert(process->pid == atoi(buf)); char* location = strchr(buf, ' '); - if (!location) return false; + if (!location) + return false; location += 2; char* end = strrchr(location, ')'); - if (!end) return false; + if (!end) + return false; int commsize = MINIMUM(end - location, commLenIn - 1); // deepcode ignore BufferOverflow: commsize is bounded by the allocated length passed in by commLen, saved into commLenIn @@ -359,8 +375,9 @@ static bool LinuxProcessList_readStatFile(Process* process, const char* dirname, location = strchr(location, ' ') + 1; } location += 1; - for (int i = 0; i < 15; i++) + for (int i = 0; i < 15; i++) { location = strchr(location, ' ') + 1; + } process->exit_signal = strtol(location, &location, 10); location += 1; assert(location != NULL); @@ -411,7 +428,9 @@ static void LinuxProcessList_readIoFile(LinuxProcess* process, const char* dirna char buffer[1024]; ssize_t buflen = xread(fd, buffer, 1023); close(fd); - if (buflen < 1) return; + if (buflen < 1) + return; + buffer[buflen] = '\0'; unsigned long long last_read = process->io_read_bytes; unsigned long long last_write = process->io_write_bytes; @@ -464,6 +483,7 @@ static bool LinuxProcessList_readStatmFile(LinuxProcess* process, const char* di FILE* statmfile = fopen(filename, "r"); if (!statmfile) return false; + int r = fscanf(statmfile, "%ld %ld %ld %ld %ld %ld %ld", &process->super.m_size, &process->super.m_resident, @@ -636,9 +656,13 @@ static void LinuxProcessList_readCGroupFile(LinuxProcess* process, const char* d while (!feof(file) && left > 0) { char buffer[PROC_LINE_LENGTH + 1]; char* ok = fgets(buffer, PROC_LINE_LENGTH, file); - if (!ok) break; + if (!ok) + break; + char* group = strchr(buffer, ':'); - if (!group) break; + if (!group) + break; + if (at != output) { *at = ';'; at++; @@ -662,6 +686,7 @@ static void LinuxProcessList_readVServerData(LinuxProcess* process, const char* FILE* file = fopen(filename, "r"); if (!file) return; + char buffer[PROC_LINE_LENGTH + 1]; process->vxid = 0; while (fgets(buffer, PROC_LINE_LENGTH, file)) { @@ -711,19 +736,22 @@ static void LinuxProcessList_readCtxtData(LinuxProcess* process, const char* dir FILE* file = fopen(filename, "r"); if (!file) return; + char buffer[PROC_LINE_LENGTH + 1]; unsigned long ctxt = 0; while (fgets(buffer, PROC_LINE_LENGTH, file)) { if (String_startsWith(buffer, "voluntary_ctxt_switches:")) { unsigned long vctxt; int ok = sscanf(buffer, "voluntary_ctxt_switches:\t%lu", &vctxt); - if (ok >= 1) + if (ok >= 1) { ctxt += vctxt; + } } else if (String_startsWith(buffer, "nonvoluntary_ctxt_switches:")) { unsigned long nvctxt; int ok = sscanf(buffer, "nonvoluntary_ctxt_switches:\t%lu", &nvctxt); - if (ok >= 1) + if (ok >= 1) { ctxt += nvctxt; + } } } fclose(file); @@ -749,10 +777,12 @@ static void LinuxProcessList_readSecattrData(LinuxProcess* process, const char* return; } char* newline = strchr(buffer, '\n'); - if (newline) + if (newline) { *newline = '\0'; - if (process->secattr && String_eq(process->secattr, buffer)) + } + if (process->secattr && String_eq(process->secattr, buffer)) { return; + } free(process->secattr); process->secattr = xStrdup(buffer); } @@ -905,17 +935,28 @@ static char* LinuxProcessList_updateTtyDevice(TtyDriver* ttyDrivers, unsigned in for (;;) { xAsprintf(&fullPath, "%s/%d", ttyDrivers[i].path, idx); int err = stat(fullPath, &sstat); - if (err == 0 && major(sstat.st_rdev) == maj && minor(sstat.st_rdev) == min) return fullPath; + if (err == 0 && major(sstat.st_rdev) == maj && minor(sstat.st_rdev) == min) { + return fullPath; + } free(fullPath); + xAsprintf(&fullPath, "%s%d", ttyDrivers[i].path, idx); err = stat(fullPath, &sstat); - if (err == 0 && major(sstat.st_rdev) == maj && minor(sstat.st_rdev) == min) return fullPath; + if (err == 0 && major(sstat.st_rdev) == maj && minor(sstat.st_rdev) == min) { + return fullPath; + } free(fullPath); - if (idx == min) break; + + if (idx == min) { + break; + } + idx = min; } int err = stat(ttyDrivers[i].path, &sstat); - if (err == 0 && tty_nr == sstat.st_rdev) return xStrdup(ttyDrivers[i].path); + if (err == 0 && tty_nr == sstat.st_rdev) { + return xStrdup(ttyDrivers[i].path); + } } char* out; xAsprintf(&out, "/dev/%u:%u", maj, min); @@ -933,7 +974,9 @@ static bool LinuxProcessList_recurseProcTree(LinuxProcessList* this, const char* #endif dir = opendir(dirname); - if (!dir) return false; + if (!dir) + return false; + int cpus = pl->cpuCount; bool hideKernelThreads = settings->hideKernelThreads; bool hideUserlandThreads = settings->hideUserlandThreads; @@ -1001,15 +1044,21 @@ static bool LinuxProcessList_recurseProcTree(LinuxProcessList* this, const char* unsigned int tty_nr = proc->tty_nr; if (! LinuxProcessList_readStatFile(proc, dirname, name, command, &commLen)) goto errorReadingProcess; + if (tty_nr != proc->tty_nr && this->ttyDrivers) { free(lp->ttyDevice); lp->ttyDevice = LinuxProcessList_updateTtyDevice(this->ttyDrivers, proc->tty_nr); } - if (settings->flags & PROCESS_FLAG_LINUX_IOPRIO) + + if (settings->flags & PROCESS_FLAG_LINUX_IOPRIO) { LinuxProcess_updateIOPriority(lp); + } + float percent_cpu = (lp->utime + lp->stime - lasttimes) / period * 100.0; proc->percent_cpu = CLAMP(percent_cpu, 0.0, cpus * 100.0); - if (isnan(proc->percent_cpu)) proc->percent_cpu = 0.0; + if (isnan(proc->percent_cpu)) + proc->percent_cpu = 0.0; + proc->percent_mem = (proc->m_resident * CRT_pageSizeKB) / (double)(pl->totalMem) * 100.0; if (!preExisting) { @@ -1051,18 +1100,22 @@ static bool LinuxProcessList_recurseProcTree(LinuxProcessList* this, const char* #endif #ifdef HAVE_CGROUP - if (settings->flags & PROCESS_FLAG_LINUX_CGROUP) + if (settings->flags & PROCESS_FLAG_LINUX_CGROUP) { LinuxProcessList_readCGroupFile(lp, dirname, name); + } #endif - if (settings->flags & PROCESS_FLAG_LINUX_OOM) + if (settings->flags & PROCESS_FLAG_LINUX_OOM) { LinuxProcessList_readOomData(lp, dirname, name); + } - if (settings->flags & PROCESS_FLAG_LINUX_CTXT) + if (settings->flags & PROCESS_FLAG_LINUX_CTXT) { LinuxProcessList_readCtxtData(lp, dirname, name); + } - if (settings->flags & PROCESS_FLAG_LINUX_SECATTR) + if (settings->flags & PROCESS_FLAG_LINUX_SECATTR) { LinuxProcessList_readSecattrData(lp, dirname, name); + } if (proc->state == 'Z' && (proc->basenameOffset == 0)) { proc->basenameOffset = -1; @@ -1072,8 +1125,9 @@ static bool LinuxProcessList_recurseProcTree(LinuxProcessList* this, const char* proc->basenameOffset = -1; setCommand(proc, command, commLen); } else if (settings->showThreadNames) { - if (! LinuxProcessList_readCmdlineFile(proc, dirname, name)) + if (! LinuxProcessList_readCmdlineFile(proc, dirname, name)) { goto errorReadingProcess; + } } if (Process_isKernelThread(proc)) { pl->kernelThreads++; @@ -1293,10 +1347,13 @@ static inline double LinuxProcessList_scanCPUTime(LinuxProcessList* this) { // 5, 7, 8 or 9 of these fields will be set. // The rest will remain at zero. char* ok = fgets(buffer, PROC_LINE_LENGTH, file); - if (!ok) buffer[0] = '\0'; - if (i == 0) + if (!ok) { + buffer[0] = '\0'; + } + + if (i == 0) { (void) sscanf(buffer, "cpu %16llu %16llu %16llu %16llu %16llu %16llu %16llu %16llu %16llu %16llu", &usertime, &nicetime, &systemtime, &idletime, &ioWait, &irq, &softIrq, &steal, &guest, &guestnice); - else { + } else { int cpuid; (void) sscanf(buffer, "cpu%4d %16llu %16llu %16llu %16llu %16llu %16llu %16llu %16llu %16llu %16llu", &cpuid, &usertime, &nicetime, &systemtime, &idletime, &ioWait, &irq, &softIrq, &steal, &guest, &guestnice); assert(cpuid == i - 1); @@ -1404,13 +1461,15 @@ static void scanCPUFreqencyFromCPUinfo(LinuxProcessList* this) { (sscanf(buffer, "cpu MHz : %lf", &frequency) == 1) || (sscanf(buffer, "cpu MHz: %lf", &frequency) == 1) ) { - if (cpuid < 0 || cpuid > (cpus - 1)) + if (cpuid < 0 || cpuid > (cpus - 1)) { continue; + } CPUData* cpuData = &(this->cpus[cpuid + 1]); /* do not override sysfs data */ - if (isnan(cpuData->frequency)) + if (isnan(cpuData->frequency)) { cpuData->frequency = frequency; + } numCPUsWithFrequency++; totalFrequency += frequency; } else if (buffer[0] == '\n') { @@ -1419,19 +1478,22 @@ static void scanCPUFreqencyFromCPUinfo(LinuxProcessList* this) { } fclose(file); - if (numCPUsWithFrequency > 0) + if (numCPUsWithFrequency > 0) { this->cpus[0].frequency = totalFrequency / numCPUsWithFrequency; + } } static void LinuxProcessList_scanCPUFrequency(LinuxProcessList* this) { int cpus = this->super.cpuCount; assert(cpus > 0); - for (int i = 0; i <= cpus; i++) + for (int i = 0; i <= cpus; i++) { this->cpus[i].frequency = NAN; + } - if (scanCPUFreqencyFromSysCPUFreq(this) == 0) + if (scanCPUFreqencyFromSysCPUFreq(this) == 0) { return; + } scanCPUFreqencyFromCPUinfo(this); } @@ -1447,12 +1509,14 @@ void ProcessList_goThroughEntries(ProcessList* super, bool pauseProcessUpdate) { double period = LinuxProcessList_scanCPUTime(this); - if (settings->showCPUFrequency) + if (settings->showCPUFrequency) { LinuxProcessList_scanCPUFrequency(this); + } // in pause mode only gather global data for meters (CPU/memory/...) - if (pauseProcessUpdate) + if (pauseProcessUpdate) { return; + } struct timeval tv; gettimeofday(&tv, NULL); diff --git a/linux/Platform.c b/linux/Platform.c index 3da3f029..66200acc 100644 --- a/linux/Platform.c +++ b/linux/Platform.c @@ -98,15 +98,18 @@ static Htop_Reaction Platform_actionSetIOPriority(State* st) { Panel* panel = st->panel; LinuxProcess* p = (LinuxProcess*) Panel_getSelected(panel); - if (!p) return HTOP_OK; + if (!p) + return HTOP_OK; + IOPriority ioprio1 = p->ioPriority; Panel* ioprioPanel = IOPriorityPanel_new(ioprio1); void* set = Action_pickFromVector(st, ioprioPanel, 21, true); if (set) { IOPriority ioprio2 = IOPriorityPanel_getIOPriority(ioprioPanel); - bool ok = MainPanel_foreachProcess((MainPanel*)panel, LinuxProcess_setIOPriority, (Arg){ .i = ioprio2 }, NULL); - if (!ok) + bool ok = MainPanel_foreachProcess((MainPanel*)panel, LinuxProcess_setIOPriority, (Arg) { .i = ioprio2 }, NULL); + if (!ok) { beep(); + } } Panel_delete((Object*)ioprioPanel); return HTOP_REFRESH | HTOP_REDRAW_BAR | HTOP_UPDATE_PANELHDR; @@ -162,7 +165,9 @@ int Platform_getUptime() { if (fd) { int n = fscanf(fd, "%64lf", &uptime); fclose(fd); - if (n <= 0) return 0; + if (n <= 0) { + return 0; + } } return floor(uptime); } @@ -185,7 +190,9 @@ void Platform_getLoadAverage(double* one, double* five, double* fifteen) { int Platform_getMaxPid() { FILE* file = fopen(PROCDIR "/sys/kernel/pid_max", "r"); - if (!file) return -1; + if (!file) + return -1; + int maxPid = 4194303; int match = fscanf(file, "%32d", &maxPid); (void) match; @@ -221,7 +228,9 @@ double Platform_setCPUValues(Meter* this, int cpu) { percent = v[0] + v[1] + v[2] + v[3]; } percent = CLAMP(percent, 0.0, 100.0); - if (isnan(percent)) percent = 0.0; + if (isnan(percent)) { + percent = 0.0; + } v[CPU_METER_FREQUENCY] = cpuData->frequency; diff --git a/linux/SELinuxMeter.c b/linux/SELinuxMeter.c index 275eefef..ee1d8470 100644 --- a/linux/SELinuxMeter.c +++ b/linux/SELinuxMeter.c @@ -32,16 +32,19 @@ static bool enforcing = false; static bool hasSELinuxMount(void) { struct statfs sfbuf; int r = statfs("/sys/fs/selinux", &sfbuf); - if (r != 0) + if (r != 0) { return false; + } - if (sfbuf.f_type != SELINUX_MAGIC) + if (sfbuf.f_type != SELINUX_MAGIC) { return false; + } struct statvfs vfsbuf; r = statvfs("/sys/fs/selinux", &vfsbuf); - if (r != 0 || (vfsbuf.f_flag & ST_RDONLY)) + if (r != 0 || (vfsbuf.f_flag & ST_RDONLY)) { return false; + } return true; } @@ -51,22 +54,26 @@ static bool isSelinuxEnabled(void) { } static bool isSelinuxEnforcing(void) { - if (!enabled) + if (!enabled) { return false; + } int fd = open("/sys/fs/selinux/enforce", O_RDONLY); - if (fd < 0) + if (fd < 0) { return false; + } char buf[20] = {0}; int r = read(fd, buf, sizeof(buf) - 1); close(fd); - if (r < 0) + if (r < 0) { return false; + } int enforce = 0; - if (sscanf(buf, "%d", &enforce) != 1) + if (sscanf(buf, "%d", &enforce) != 1) { return false; + } return !!enforce; } |