From 8c82a38002ce09db2a0b83dab8b598cf1ab0596c Mon Sep 17 00:00:00 2001 From: Daniel Lange Date: Mon, 11 Apr 2016 13:01:17 +0200 Subject: Imported Upstream version 2.0.1 --- openbsd/OpenBSDProcessList.c | 127 +++++++++++++++++++++++++++---------------- openbsd/OpenBSDProcessList.h | 14 ++++- 2 files changed, 91 insertions(+), 50 deletions(-) (limited to 'openbsd') diff --git a/openbsd/OpenBSDProcessList.c b/openbsd/OpenBSDProcessList.c index 97e40cb..14d9c2d 100644 --- a/openbsd/OpenBSDProcessList.c +++ b/openbsd/OpenBSDProcessList.c @@ -10,18 +10,20 @@ in the source distribution for its full text. #include "OpenBSDProcessList.h" #include "OpenBSDProcess.h" -#include -#include #include #include -#include +#include +#include #include #include +#include #include +#include #include -#include +#include +#include #include -#include +#include /*{ @@ -42,22 +44,37 @@ typedef struct OpenBSDProcessList_ { }*/ +/* + * avoid relying on or conflicting with MIN() and MAX() in sys/param.h + */ +#ifndef MINIMUM +#define MINIMUM(x, y) ((x) > (y) ? (y) : (x)) +#endif + +#ifndef MAXIMUM +#define MAXIMUM(x, y) ((x) > (y) ? (x) : (y)) +#endif + #ifndef CLAMP -#define CLAMP(x,low,high) (((x)>(high))?(high):(((x)<(low))?(low):(x))) +#define CLAMP(x, low, high) (((x) > (high)) ? (high) : MAXIMUM(x, low)) #endif -static int pageSizeKb; static long fscale; ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidWhiteList, uid_t userId) { int mib[] = { CTL_HW, HW_NCPU }; int fmib[] = { CTL_KERN, KERN_FSCALE }; int i, e; - OpenBSDProcessList* opl = xCalloc(1, sizeof(OpenBSDProcessList)); - ProcessList* pl = (ProcessList*) opl; - size_t size = sizeof(pl->cpuCount); - + OpenBSDProcessList* opl; + ProcessList* pl; + size_t size; + char errbuf[_POSIX2_LINE_MAX]; + + opl = xCalloc(1, sizeof(OpenBSDProcessList)); + pl = (ProcessList*) opl; + size = sizeof(pl->cpuCount); ProcessList_init(pl, Class(OpenBSDProcess), usersTable, pidWhiteList, userId); + e = sysctl(mib, 2, &pl->cpuCount, &size, NULL, 0); if (e == -1 || pl->cpuCount < 1) { pl->cpuCount = 1; @@ -65,26 +82,29 @@ ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidWhiteList, ui opl->cpus = xRealloc(opl->cpus, pl->cpuCount * sizeof(CPUData)); size = sizeof(fscale); - if (sysctl(fmib, 2, &fscale, &size, NULL, 0) < 0) + if (sysctl(fmib, 2, &fscale, &size, NULL, 0) < 0) { err(1, "fscale sysctl call failed"); + } for (i = 0; i < pl->cpuCount; i++) { opl->cpus[i].totalTime = 1; opl->cpus[i].totalPeriod = 1; } - pageSizeKb = PAGE_SIZE_KB; - - // XXX: last arg should eventually be an errbuf - opl->kd = kvm_open(NULL, NULL, NULL, KVM_NO_FILES, NULL); - assert(opl->kd); + opl->kd = kvm_openfiles(NULL, NULL, NULL, KVM_NO_FILES, errbuf); + if (opl->kd == NULL) { + errx(1, "kvm_open: %s", errbuf); + } return pl; } void ProcessList_delete(ProcessList* this) { const OpenBSDProcessList* opl = (OpenBSDProcessList*) this; - if (opl->kd) kvm_close(opl->kd); + + if (opl->kd) { + kvm_close(opl->kd); + } free(opl->cpus); @@ -95,15 +115,26 @@ void ProcessList_delete(ProcessList* this) { static inline void OpenBSDProcessList_scanMemoryInfo(ProcessList* pl) { static int uvmexp_mib[] = {CTL_VM, VM_UVMEXP}; struct uvmexp uvmexp; - size_t size = sizeof(uvmexp); + size_t size_uvmexp = sizeof(uvmexp); - if (sysctl(uvmexp_mib, 2, &uvmexp, &size, NULL, 0) < 0) { + if (sysctl(uvmexp_mib, 2, &uvmexp, &size_uvmexp, NULL, 0) < 0) { err(1, "uvmexp sysctl call failed"); } - //kb_pagesize = uvmexp.pagesize / 1024; - pl->usedMem = uvmexp.active * pageSizeKb; - pl->totalMem = uvmexp.npages * pageSizeKb; + pl->totalMem = uvmexp.npages * PAGE_SIZE_KB; + + // Taken from OpenBSD systat/iostat.c, top/machine.c and uvm_sysctl(9) + static int bcache_mib[] = {CTL_VFS, VFS_GENERIC, VFS_BCACHESTAT}; + struct bcachestats bcstats; + size_t size_bcstats = sizeof(bcstats); + + if (sysctl(bcache_mib, 3, &bcstats, &size_bcstats, NULL, 0) < 0) { + err(1, "cannot get vfs.bcachestat"); + } + + pl->cachedMem = bcstats.numbufpages * PAGE_SIZE_KB; + pl->freeMem = uvmexp.free * PAGE_SIZE_KB; + pl->usedMem = (uvmexp.npages - uvmexp.free - uvmexp.paging) * PAGE_SIZE_KB; /* const OpenBSDProcessList* opl = (OpenBSDProcessList*) pl; @@ -112,10 +143,10 @@ static inline void OpenBSDProcessList_scanMemoryInfo(ProcessList* pl) { 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 *= pageSizeKb; + pl->usedMem *= PAGE_SIZE_KB; pl->freeMem = pl->totalMem - pl->usedMem; sysctl(MIB_vm_stats_vm_v_cache_count, 4, &(pl->cachedMem), &len, NULL, 0); - pl->cachedMem *= pageSizeKb; + pl->cachedMem *= PAGE_SIZE_KB; struct kvm_swap swap[16]; int nswap = kvm_getswapinfo(opl->kd, swap, sizeof(swap)/sizeof(swap[0]), 0); @@ -125,8 +156,8 @@ static inline void OpenBSDProcessList_scanMemoryInfo(ProcessList* pl) { pl->totalSwap += swap[i].ksw_total; pl->usedSwap += swap[i].ksw_used; } - pl->totalSwap *= pageSizeKb; - pl->usedSwap *= pageSizeKb; + pl->totalSwap *= PAGE_SIZE_KB; + pl->usedSwap *= PAGE_SIZE_KB; pl->sharedMem = 0; // currently unused pl->buffersMem = 0; // not exposed to userspace @@ -134,40 +165,40 @@ static inline void OpenBSDProcessList_scanMemoryInfo(ProcessList* pl) { } char *OpenBSDProcessList_readProcessName(kvm_t* kd, struct kinfo_proc* kproc, int* basenameEnd) { - char *s, *buf, **arg; - size_t cpsz, len = 0, n; + char *s, **arg; + size_t len = 0, n; int i; /* - * We attempt to fall back to just the command name (argv[0]) if we - * fail to construct the full command at any point. + * 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); - if (arg == NULL) { - if ((s = xStrdup(kproc->p_comm)) == NULL) { - err(1, NULL); - } - return s; + if (arg == NULL || *arg == NULL) { + *basenameEnd = strlen(kproc->p_comm); + return xStrdup(kproc->p_comm); } for (i = 0; arg[i] != NULL; i++) { - len += strlen(arg[i]) + 1; + len += strlen(arg[i]) + 1; /* room for arg and trailing space or NUL */ } - if ((buf = s = xMalloc(len)) == NULL) { - if ((s = xStrdup(kproc->p_comm)) == NULL) { - err(1, NULL); - } - return s; + /* don't use xMalloc here - we want to handle huge argv's gracefully */ + if ((s = malloc(len)) == NULL) { + *basenameEnd = strlen(kproc->p_comm); + return xStrdup(kproc->p_comm); } + + *s = '\0'; + for (i = 0; arg[i] != NULL; i++) { - n = strlcpy(buf, arg[i], (s + len) - buf); - buf += n; + n = strlcat(s, arg[i], len); if (i == 0) { - *basenameEnd = n; + /* TODO: rename all basenameEnd to basenameLen, make size_t */ + *basenameEnd = MINIMUM(n, len-1); } - *buf = ' '; - buf++; + /* the trailing space should get truncated anyway */ + strlcat(s, " ", len); } - *(buf - 1) = '\0'; + return s; } diff --git a/openbsd/OpenBSDProcessList.h b/openbsd/OpenBSDProcessList.h index 192fb34..ba9e6d1 100644 --- a/openbsd/OpenBSDProcessList.h +++ b/openbsd/OpenBSDProcessList.h @@ -27,10 +27,20 @@ typedef struct OpenBSDProcessList_ { } OpenBSDProcessList; -#ifndef CLAMP -#define CLAMP(x,low,high) (((x)>(high))?(high):(((x)<(low))?(low):(x))) +/* + * avoid relying on or conflicting with MIN() and MAX() in sys/param.h + */ +#ifndef MINIMUM +#define MINIMUM(x, y) ((x) > (y) ? (y) : (x)) #endif +#ifndef MAXIMUM +#define MAXIMUM(x, y) ((x) > (y) ? (x) : (y)) +#endif + +#ifndef CLAMP +#define CLAMP(x, low, high) (((x) > (high)) ? (high) : MAXIMUM(x, low)) +#endif ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidWhiteList, uid_t userId); -- cgit v1.2.3