From 57d9ecc5519a44d4d08f1739ab4b741ccfc2d35a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20G=C3=B6ttsche?= Date: Sat, 5 Dec 2020 22:57:07 +0100 Subject: [PATCH] OpenBSD update - compilation failures like `return &this->this;` -> `return &this->super;` - iwyu update - misc cleanup --- openbsd/OpenBSDProcess.c | 42 +++++----- openbsd/OpenBSDProcess.h | 11 ++- openbsd/OpenBSDProcessList.c | 152 +++++++++++++++++------------------ openbsd/OpenBSDProcessList.h | 9 ++- openbsd/Platform.c | 106 ++++++++---------------- openbsd/Platform.h | 3 + 6 files changed, 147 insertions(+), 176 deletions(-) diff --git a/openbsd/OpenBSDProcess.c b/openbsd/OpenBSDProcess.c index 4b79ea36..df5002ac 100644 --- a/openbsd/OpenBSDProcess.c +++ b/openbsd/OpenBSDProcess.c @@ -6,26 +6,15 @@ Released under the GNU GPLv2, see the COPYING file in the source distribution for its full text. */ -#include "Process.h" -#include "ProcessList.h" #include "OpenBSDProcess.h" -#include "Platform.h" -#include "CRT.h" #include -#include -#include +#include "CRT.h" +#include "Process.h" +#include "RichString.h" +#include "XUtils.h" -const ProcessClass OpenBSDProcess_class = { - .super = { - .extends = Class(Process), - .display = Process_display, - .delete = Process_delete, - .compare = OpenBSDProcess_compare - }, - .writeField = OpenBSDProcess_writeField, -}; ProcessFieldData Process_fields[] = { [0] = { @@ -200,7 +189,7 @@ Process* OpenBSDProcess_new(const Settings* settings) { OpenBSDProcess* this = xCalloc(sizeof(OpenBSDProcess), 1); Object_setClass(this, Class(OpenBSDProcess)); Process_init(&this->super, settings); - return &this->this; + return &this->super; } void Process_delete(Object* cast) { @@ -209,8 +198,8 @@ void Process_delete(Object* cast) { free(this); } -void OpenBSDProcess_writeField(const Process* this, RichString* str, ProcessField field) { - //OpenBSDProcess* fp = (OpenBSDProcess*) this; +static void OpenBSDProcess_writeField(const Process* this, RichString* str, ProcessField field) { + //const OpenBSDProcess* op = (const OpenBSDProcess*) this; char buffer[256]; buffer[255] = '\0'; int attr = CRT_colors[DEFAULT_COLOR]; //int n = sizeof(buffer) - 1; @@ -223,7 +212,7 @@ void OpenBSDProcess_writeField(const Process* this, RichString* str, ProcessFiel RichString_append(str, attr, buffer); } -long OpenBSDProcess_compare(const void* v1, const void* v2) { +static long OpenBSDProcess_compare(const void* v1, const void* v2) { const OpenBSDProcess *p1, *p2; const Settings *settings = ((const Process*)v1)->settings; @@ -235,6 +224,9 @@ long OpenBSDProcess_compare(const void* v1, const void* v2) { p1 = (const OpenBSDProcess*)v2; } + // remove if actually used + (void)p1; (void)p2; + switch (settings->sortKey) { // add OpenBSD-specific fields here default: @@ -242,6 +234,16 @@ long OpenBSDProcess_compare(const void* v1, const void* v2) { } } +const ProcessClass OpenBSDProcess_class = { + .super = { + .extends = Class(Process), + .display = Process_display, + .delete = Process_delete, + .compare = OpenBSDProcess_compare + }, + .writeField = OpenBSDProcess_writeField, +}; + bool Process_isThread(const Process* this) { - return (Process_isKernelThread(this)); + return Process_isKernelThread(this) || Process_isUserlandThread(this); } diff --git a/openbsd/OpenBSDProcess.h b/openbsd/OpenBSDProcess.h index 12ce1840..2d015136 100644 --- a/openbsd/OpenBSDProcess.h +++ b/openbsd/OpenBSDProcess.h @@ -8,6 +8,13 @@ Released under the GNU GPLv2, see the COPYING file in the source distribution for its full text. */ +#include + +#include "Object.h" +#include "Process.h" +#include "Settings.h" + + typedef enum OpenBSDProcessFields_ { // Add platform-specific fields here, with ids >= 100 LAST_PROCESSFIELD = 100, @@ -31,10 +38,6 @@ Process* OpenBSDProcess_new(const Settings* settings); void Process_delete(Object* cast); -void OpenBSDProcess_writeField(const Process* this, RichString* str, ProcessField field); - -long OpenBSDProcess_compare(const void* v1, const void* v2); - bool Process_isThread(const Process* this); #endif diff --git a/openbsd/OpenBSDProcessList.c b/openbsd/OpenBSDProcessList.c index f1ebb344..5412030f 100644 --- a/openbsd/OpenBSDProcessList.c +++ b/openbsd/OpenBSDProcessList.c @@ -6,46 +6,49 @@ Released under the GNU GPLv2, see the COPYING file in the source distribution for its full text. */ -#include "CRT.h" -#include "ProcessList.h" #include "OpenBSDProcessList.h" -#include "OpenBSDProcess.h" -#include "Macros.h" #include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include #include #include #include #include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "CRT.h" +#include "Macros.h" +#include "Object.h" +#include "OpenBSDProcess.h" +#include "Process.h" +#include "ProcessList.h" +#include "Settings.h" +#include "XUtils.h" + static long fscale; ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidMatchList, uid_t userId) { - int mib[] = { CTL_HW, HW_NCPU }; - int fmib[] = { CTL_KERN, KERN_FSCALE }; - int i, e; - OpenBSDProcessList* opl; - ProcessList* pl; + const int mib[] = { CTL_HW, HW_NCPU }; + const int fmib[] = { CTL_KERN, KERN_FSCALE }; + int r; size_t size; char errbuf[_POSIX2_LINE_MAX]; - opl = xCalloc(1, sizeof(OpenBSDProcessList)); - pl = (ProcessList*) opl; - size = sizeof(pl->cpuCount); + OpenBSDProcessList* opl = xCalloc(1, sizeof(OpenBSDProcessList)); + ProcessList* pl = (ProcessList*) opl; ProcessList_init(pl, Class(OpenBSDProcess), usersTable, pidMatchList, userId); - e = sysctl(mib, 2, &pl->cpuCount, &size, NULL, 0); - if (e == -1 || pl->cpuCount < 1) { + size = sizeof(pl->cpuCount); + r = sysctl(mib, 2, &pl->cpuCount, &size, NULL, 0); + if (r < 0 || pl->cpuCount < 1) { pl->cpuCount = 1; } opl->cpus = xCalloc(pl->cpuCount + 1, sizeof(CPUData)); @@ -55,7 +58,7 @@ ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidMatchList, ui err(1, "fscale sysctl call failed"); } - for (i = 0; i <= pl->cpuCount; i++) { + for (int i = 0; i <= pl->cpuCount; i++) { CPUData* d = opl->cpus + i; d->totalTime = 1; d->totalPeriod = 1; @@ -70,7 +73,7 @@ ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidMatchList, ui } void ProcessList_delete(ProcessList* this) { - const OpenBSDProcessList* opl = (OpenBSDProcessList*) this; + OpenBSDProcessList* opl = (OpenBSDProcessList*) this; if (opl->kd) { kvm_close(opl->kd); @@ -82,8 +85,8 @@ void ProcessList_delete(ProcessList* this) { free(this); } -static inline void OpenBSDProcessList_scanMemoryInfo(ProcessList* pl) { - static int uvmexp_mib[] = {CTL_VM, VM_UVMEXP}; +static void OpenBSDProcessList_scanMemoryInfo(ProcessList* pl) { + const int uvmexp_mib[] = { CTL_VM, VM_UVMEXP }; struct uvmexp uvmexp; size_t size_uvmexp = sizeof(uvmexp); @@ -92,9 +95,10 @@ static inline void OpenBSDProcessList_scanMemoryInfo(ProcessList* pl) { } pl->totalMem = uvmexp.npages * CRT_pageSizeKB; + pl->usedMem = (uvmexp.npages - uvmexp.free - uvmexp.paging) * CRT_pageSizeKB; // Taken from OpenBSD systat/iostat.c, top/machine.c and uvm_sysctl(9) - static int bcache_mib[] = {CTL_VFS, VFS_GENERIC, VFS_BCACHESTAT}; + const int bcache_mib[] = { CTL_VFS, VFS_GENERIC, VFS_BCACHESTAT }; struct bcachestats bcstats; size_t size_bcstats = sizeof(bcstats); @@ -103,52 +107,55 @@ static inline void OpenBSDProcessList_scanMemoryInfo(ProcessList* pl) { } pl->cachedMem = bcstats.numbufpages * CRT_pageSizeKB; - pl->usedMem = (uvmexp.npages - uvmexp.free - uvmexp.paging) * CRT_pageSizeKB; /* - const OpenBSDProcessList* opl = (OpenBSDProcessList*) pl; + * Copyright (c) 1994 Thorsten Lockert + * All rights reserved. + * + * Taken almost directly from OpenBSD's top(1) + * + * Originally released under a BSD-3 license + * Modified through htop developers applying GPL-2 + */ + int nswap = swapctl(SWAP_NSWAP, 0, 0); + if (nswap > 0) { + struct swapent swdev[nswap]; + int rnswap = swapctl(SWAP_STATS, swdev, nswap); - size_t len = sizeof(pl->totalMem); - sysctl(MIB_hw_physmem, 2, &(pl->totalMem), &len, NULL, 0); - pl->totalMem /= 1024; - sysctl(MIB_vm_stats_vm_v_wire_count, 4, &(pl->usedMem), &len, NULL, 0); - pl->usedMem *= CRT_pageSizeKB; - sysctl(MIB_vm_stats_vm_v_cache_count, 4, &(pl->cachedMem), &len, NULL, 0); - pl->cachedMem *= CRT_pageSizeKB; + /* Total things up */ + unsigned long long int total = 0, used = 0; + for (int i = 0; i < rnswap; i++) { + if (swdev[i].se_flags & SWF_ENABLE) { + used += (swdev[i].se_inuse / (1024 / DEV_BSIZE)); + total += (swdev[i].se_nblks / (1024 / DEV_BSIZE)); + } + } - struct kvm_swap swap[16]; - int nswap = kvm_getswapinfo(opl->kd, swap, ARRAYSIZE(swap), 0); - pl->totalSwap = 0; - pl->usedSwap = 0; - for (int i = 0; i < nswap; i++) { - pl->totalSwap += swap[i].ksw_total; - pl->usedSwap += swap[i].ksw_used; + pl->totalSwap = total; + pl->usedSwap = used; + } else { + pl->totalSwap = pl->usedSwap = 0; } - pl->totalSwap *= CRT_pageSizeKB; - pl->usedSwap *= CRT_pageSizeKB; - - pl->buffersMem = 0; // not exposed to userspace - */ } -char* OpenBSDProcessList_readProcessName(kvm_t* kd, struct kinfo_proc* kproc, int* basenameEnd) { - char *s, **arg; - size_t len = 0; - int i; - +static char* OpenBSDProcessList_readProcessName(kvm_t* kd, const struct kinfo_proc* kproc, int* basenameEnd) { /* * Like OpenBSD's top(1), we try to fall back to the command name * (argv[0]) if we fail to construct the full command. */ - arg = kvm_getargv(kd, kproc, 500); + char** arg = kvm_getargv(kd, kproc, 500); if (arg == NULL || *arg == NULL) { *basenameEnd = strlen(kproc->p_comm); return xStrdup(kproc->p_comm); } - for (i = 0; arg[i] != NULL; i++) { + + size_t len = 0; + for (int i = 0; arg[i] != NULL; i++) { len += strlen(arg[i]) + 1; /* room for arg and trailing space or NUL */ } + /* don't use xMalloc here - we want to handle huge argv's gracefully */ + char* s; if ((s = malloc(len)) == NULL) { *basenameEnd = strlen(kproc->p_comm); return xStrdup(kproc->p_comm); @@ -156,10 +163,9 @@ char* OpenBSDProcessList_readProcessName(kvm_t* kd, struct kinfo_proc* kproc, in *s = '\0'; - for (i = 0; arg[i] != NULL; i++) { + for (int i = 0; arg[i] != NULL; i++) { size_t n = strlcat(s, arg[i], len); if (i == 0) { - /* TODO: rename all basenameEnd to basenameLen, make size_t */ *basenameEnd = MINIMUM(n, len - 1); } /* the trailing space should get truncated anyway */ @@ -176,30 +182,25 @@ static double getpcpu(const struct kinfo_proc* kp) { if (fscale == 0) return 0.0; -#define fxtofl(fixpt) ((double)(fixpt) / fscale) - - return (100.0 * fxtofl(kp->p_pctcpu)); + return 100.0 * (double)kp->p_pctcpu / fscale; } -static inline void OpenBSDProcessList_scanProcs(OpenBSDProcessList* this) { +static void OpenBSDProcessList_scanProcs(OpenBSDProcessList* this) { const Settings* settings = this->super.settings; bool hideKernelThreads = settings->hideKernelThreads; bool hideUserlandThreads = settings->hideUserlandThreads; int count = 0; - // use KERN_PROC_KTHREAD to also include kernel threads - struct kinfo_proc* kprocs = kvm_getprocs(this->kd, KERN_PROC_ALL, 0, sizeof(struct kinfo_proc), &count); - //struct kinfo_proc* kprocs = getprocs(KERN_PROC_ALL, 0, &count); + const struct kinfo_proc* kprocs = kvm_getprocs(this->kd, KERN_PROC_KTHREAD, 0, sizeof(struct kinfo_proc), &count); for (int i = 0; i < count; i++) { - struct kinfo_proc* kproc = &kprocs[i]; + const struct kinfo_proc* kproc = &kprocs[i]; bool preExisting = false; Process* proc = ProcessList_getProcess(&this->super, kproc->p_pid, &preExisting, OpenBSDProcess_new); - OpenBSDProcess* fp = (OpenBSDProcess*) proc; + //OpenBSDProcess* fp = (OpenBSDProcess*) proc; - proc->show = ! ((hideKernelThreads && Process_isKernelThread(proc)) - || (hideUserlandThreads && Process_isUserlandThread(proc))); + proc->show = ! ((hideKernelThreads && Process_isKernelThread(proc)) || (hideUserlandThreads && Process_isUserlandThread(proc))); if (!preExisting) { proc->ppid = kproc->p_ppid; @@ -226,10 +227,8 @@ static inline void OpenBSDProcessList_scanProcs(OpenBSDProcessList* this) { proc->percent_mem = (proc->m_resident * CRT_pageSizeKB) / (double)(this->super.totalMem) * 100.0; proc->percent_cpu = CLAMP(getpcpu(kproc), 0.0, this->super.cpuCount * 100.0); //proc->nlwp = kproc->p_numthreads; - //proc->time = kproc->p_rtime_sec + ((kproc->p_rtime_usec + 500000) / 10); proc->nice = kproc->p_nice - 20; - proc->time = kproc->p_rtime_sec + ((kproc->p_rtime_usec + 500000) / 1000000); - proc->time *= 100; + proc->time = 100 * (kproc->p_rtime_sec + ((kproc->p_rtime_usec + 500000) / 1000000)); proc->priority = kproc->p_priority - PZERO; switch (kproc->p_stat) { @@ -261,10 +260,9 @@ static unsigned long long saturatingSub(unsigned long long a, unsigned long long } static void getKernelCPUTimes(int cpuId, u_int64_t* times) { - int mib[] = { CTL_KERN, KERN_CPTIME2, cpuId }; - size_t length = sizeof(u_int64_t) * CPUSTATES; - if (sysctl(mib, 3, times, &length, NULL, 0) == -1 || - length != sizeof(u_int64_t) * CPUSTATES) { + const int mib[] = { CTL_KERN, KERN_CPTIME2, cpuId }; + size_t length = sizeof(*times) * CPUSTATES; + if (sysctl(mib, 3, times, &length, NULL, 0) == -1 || length != sizeof(*times) * CPUSTATES) { CRT_fatalError("sysctl kern.cp_time2 failed"); } } diff --git a/openbsd/OpenBSDProcessList.h b/openbsd/OpenBSDProcessList.h index 1b40faf1..a6195a52 100644 --- a/openbsd/OpenBSDProcessList.h +++ b/openbsd/OpenBSDProcessList.h @@ -9,6 +9,13 @@ in the source distribution for its full text. */ #include +#include +#include + +#include "Hashtable.h" +#include "ProcessList.h" +#include "UsersTable.h" + typedef struct CPUData_ { unsigned long long int totalTime; @@ -43,8 +50,6 @@ ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidMatchList, ui void ProcessList_delete(ProcessList* this); -char* OpenBSDProcessList_readProcessName(kvm_t* kd, struct kinfo_proc* kproc, int* basenameEnd); - void ProcessList_goThroughEntries(ProcessList* super, bool pauseProcessUpdate); #endif diff --git a/openbsd/Platform.c b/openbsd/Platform.c index 38fbab67..dae80724 100644 --- a/openbsd/Platform.c +++ b/openbsd/Platform.c @@ -7,40 +7,39 @@ in the source distribution for its full text. */ #include "Platform.h" -#include "Macros.h" -#include "Meter.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + #include "CPUMeter.h" -#include "MemoryMeter.h" -#include "SwapMeter.h" -#include "TasksMeter.h" -#include "LoadAverageMeter.h" -#include "UptimeMeter.h" #include "ClockMeter.h" #include "DateMeter.h" #include "DateTimeMeter.h" #include "HostnameMeter.h" -#include "SignalsPanel.h" +#include "LoadAverageMeter.h" +#include "Macros.h" +#include "MemoryMeter.h" +#include "Meter.h" #include "OpenBSDProcess.h" #include "OpenBSDProcessList.h" - -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include "ProcessList.h" +#include "Settings.h" +#include "SignalsPanel.h" +#include "SwapMeter.h" +#include "TasksMeter.h" +#include "UptimeMeter.h" +#include "XUtils.h" ProcessField Platform_defaultFields[] = { PID, USER, PRIORITY, NICE, M_VIRT, M_RESIDENT, STATE, PERCENT_CPU, PERCENT_MEM, TIME, COMM, 0 }; @@ -131,10 +130,9 @@ void Platform_setBindings(Htop_Action* keys) { (void) keys; } -// preserved from FreeBSD port int Platform_getUptime() { struct timeval bootTime, currTime; - int mib[2] = { CTL_KERN, KERN_BOOTTIME }; + const int mib[2] = { CTL_KERN, KERN_BOOTTIME }; size_t size = sizeof(bootTime); int err = sysctl(mib, 2, &bootTime, &size, NULL, 0); @@ -148,7 +146,7 @@ int Platform_getUptime() { void Platform_getLoadAverage(double* one, double* five, double* fifteen) { struct loadavg loadAverage; - int mib[2] = { CTL_VM, VM_LOADAVG }; + const int mib[2] = { CTL_VM, VM_LOADAVG }; size_t size = sizeof(loadAverage); int err = sysctl(mib, 2, &loadAverage, &size, NULL, 0); @@ -164,12 +162,12 @@ void Platform_getLoadAverage(double* one, double* five, double* fifteen) { } int Platform_getMaxPid() { - // this is hard-coded in sys/sys/proc.h - no sysctl exists + // this is hard-coded in sys/proc.h - no sysctl exists return 99999; } double Platform_setCPUValues(Meter* this, int cpu) { - const OpenBSDProcessList* pl = (OpenBSDProcessList*) this->pl; + const OpenBSDProcessList* pl = (const OpenBSDProcessList*) this->pl; const CPUData* cpuData = &(pl->cpus[cpu]); double total = cpuData->totalPeriod == 0 ? 1 : cpuData->totalPeriod; double totalPercent; @@ -195,9 +193,6 @@ double Platform_setCPUValues(Meter* this, int cpu) { } totalPercent = CLAMP(totalPercent, 0.0, 100.0); - if (isnan(totalPercent)) { - totalPercent = 0.0; - } v[CPU_METER_TEMPERATURE] = NAN; @@ -216,45 +211,10 @@ void Platform_setMemoryValues(Meter* this) { this->values[2] = cachedMem; } -/* - * Copyright (c) 1994 Thorsten Lockert - * All rights reserved. - * - * Taken almost directly from OpenBSD's top(1) - */ void Platform_setSwapValues(Meter* this) { const ProcessList* pl = this->pl; - struct swapent* swdev; - unsigned long long int total, used; - int nswap, rnswap, i; - nswap = swapctl(SWAP_NSWAP, 0, 0); - if (nswap == 0) { - return; - } - - swdev = xCalloc(nswap, sizeof(*swdev)); - - rnswap = swapctl(SWAP_STATS, swdev, nswap); - if (rnswap == -1) { - free(swdev); - return; - } - - // if rnswap != nswap, then what? - - /* Total things up */ - total = used = 0; - for (i = 0; i < nswap; i++) { - if (swdev[i].se_flags & SWF_ENABLE) { - used += (swdev[i].se_inuse / (1024 / DEV_BSIZE)); - total += (swdev[i].se_nblks / (1024 / DEV_BSIZE)); - } - } - - this->total = pl->totalSwap = total; - this->values[0] = pl->usedSwap = used; - - free(swdev); + this->total = pl->totalSwap; + this->values[0] = pl->usedSwap; } char* Platform_getProcessEnv(pid_t pid) { @@ -350,7 +310,7 @@ static bool findDevice(const char* name, int* mib, struct sensordev* snsrdev, si } void Platform_getBattery(double* percent, ACPresence* isOnAC) { - static int mib[] = {CTL_HW, HW_SENSORS, 0, 0, 0}; + int mib[] = {CTL_HW, HW_SENSORS, 0, 0, 0}; struct sensor s; size_t slen = sizeof(struct sensor); struct sensordev snsrdev; diff --git a/openbsd/Platform.h b/openbsd/Platform.h index 07cde2f8..0e2d435f 100644 --- a/openbsd/Platform.h +++ b/openbsd/Platform.h @@ -14,9 +14,12 @@ in the source distribution for its full text. #include "Action.h" #include "BatteryMeter.h" #include "DiskIOMeter.h" +#include "Meter.h" +#include "Process.h" #include "ProcessLocksScreen.h" #include "SignalsPanel.h" + extern ProcessFieldData Process_fields[]; extern ProcessField Platform_defaultFields[];