From 22cfda6332bcfe89e143ed9475032bec7222bfa3 Mon Sep 17 00:00:00 2001 From: Michael McConville Date: Sat, 2 Jan 2016 11:57:53 -0500 Subject: OpenBSD fixes and updates I forgot how awful the process name logic was. It was an initial hack to get it running, and I forgot to clean it up. I also had to change a few includes and error function uses. --- openbsd/OpenBSDProcessList.c | 56 +++++++++++++++++++++----------------------- openbsd/Platform.h | 1 + 2 files changed, 28 insertions(+), 29 deletions(-) (limited to 'openbsd') diff --git a/openbsd/OpenBSDProcessList.c b/openbsd/OpenBSDProcessList.c index 210328aa..eb4cd2e7 100644 --- a/openbsd/OpenBSDProcessList.c +++ b/openbsd/OpenBSDProcessList.c @@ -12,6 +12,7 @@ in the source distribution for its full text. #include #include +#include #include #include #include @@ -59,7 +60,7 @@ ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidWhiteList, ui size = sizeof(fscale); if (sysctl(fmib, 2, &fscale, &size, NULL, 0) < 0) - CRT_fatalError("fscale sysctl call failed"); + errx(1, "fscale sysctl call failed"); for (i = 0; i < pl->cpuCount; i++) { fpl->cpus[i].totalTime = 1; @@ -89,7 +90,7 @@ static inline void OpenBSDProcessList_scanMemoryInfo(ProcessList* pl) { size_t size = sizeof(uvmexp); if (sysctl(uvmexp_mib, 2, &uvmexp, &size, NULL, 0) < 0) { - CRT_fatalError("uvmexp sysctl call failed"); + errx(1, "uvmexp sysctl call failed"); } //kb_pagesize = uvmexp.pagesize / 1024; @@ -125,34 +126,31 @@ static inline void OpenBSDProcessList_scanMemoryInfo(ProcessList* pl) { } char *OpenBSDProcessList_readProcessName(kvm_t* kd, struct kinfo_proc* kproc, int* basenameEnd) { - char *str, *buf, **argv; - size_t cpsz; - size_t len = 500; - - argv = kvm_getargv(kd, kproc, 500); - - if (argv == NULL) - CRT_fatalError("kvm call failed"); - - str = buf = malloc(len+1); - if (str == NULL) - CRT_fatalError("out of memory"); - - while (*argv != NULL) { - cpsz = MIN(len, strlen(*argv)); - strncpy(buf, *argv, cpsz); - buf += cpsz; - len -= cpsz; - argv++; - if (len > 0) { - *buf = ' '; - buf++; - len--; - } - } + char *s, *buf, **arg; + size_t cpsz, len = 0, n; + int i; - *buf = '\0'; - return str; + arg = kvm_getargv(kd, kproc, 500); + if (arg == NULL) { + return "[zombie]"; + // the FreeBSD port uses ki_comm, but we don't have it + //return strndup(kproc->ki_comm); + } + for (i = 0; arg[i] != NULL; i++) { + len += strlen(arg[i]) + 1; + } + buf = s = malloc(len); + for (i = 0; arg[i] != NULL; i++) { + n = strlcpy(buf, arg[i], (s + len) - buf); + buf += n; + if (i == 0) { + *basenameEnd = n; + } + buf++; + *buf = ' '; + } + *(buf - 1) = '\0'; + return s; } /* diff --git a/openbsd/Platform.h b/openbsd/Platform.h index a59d2ef5..d8dea1b1 100644 --- a/openbsd/Platform.h +++ b/openbsd/Platform.h @@ -11,6 +11,7 @@ in the source distribution for its full text. */ #include "Action.h" +#include "SignalsPanel.h" #include "BatteryMeter.h" extern ProcessFieldData Process_fields[]; -- cgit v1.2.3 From 71703827063770d78438ec0051e70f579faafbf4 Mon Sep 17 00:00:00 2001 From: Michael McConville Date: Sat, 2 Jan 2016 12:11:26 -0500 Subject: Fix spelling of "maintainer" --- openbsd/OpenBSDCRT.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'openbsd') diff --git a/openbsd/OpenBSDCRT.c b/openbsd/OpenBSDCRT.c index 552ca30d..c5dcec4a 100644 --- a/openbsd/OpenBSDCRT.c +++ b/openbsd/OpenBSDCRT.c @@ -16,7 +16,7 @@ void CRT_handleSIGSEGV(int sgn) { CRT_done(); fprintf(stderr, "\n\nhtop " VERSION " aborting.\n"); fprintf(stderr, "\nUnfortunately, you seem to be using an unsupported platform!"); - fprintf(stderr, "\nPlease contact your platform package mantainer!\n\n"); + fprintf(stderr, "\nPlease contact your platform package maintainer!\n\n"); abort(); } -- cgit v1.2.3 From ae5c01f4859e3296292bd34b2efc5a7720368d98 Mon Sep 17 00:00:00 2001 From: Michael McConville Date: Sat, 2 Jan 2016 12:17:35 -0500 Subject: Use err() rather then errx() for sysctl() So that we can see errno. Pointed out by Michael Reed. --- openbsd/OpenBSDProcessList.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'openbsd') diff --git a/openbsd/OpenBSDProcessList.c b/openbsd/OpenBSDProcessList.c index eb4cd2e7..aa765a15 100644 --- a/openbsd/OpenBSDProcessList.c +++ b/openbsd/OpenBSDProcessList.c @@ -60,7 +60,7 @@ ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidWhiteList, ui size = sizeof(fscale); if (sysctl(fmib, 2, &fscale, &size, NULL, 0) < 0) - errx(1, "fscale sysctl call failed"); + err(1, "fscale sysctl call failed"); for (i = 0; i < pl->cpuCount; i++) { fpl->cpus[i].totalTime = 1; @@ -90,7 +90,7 @@ static inline void OpenBSDProcessList_scanMemoryInfo(ProcessList* pl) { size_t size = sizeof(uvmexp); if (sysctl(uvmexp_mib, 2, &uvmexp, &size, NULL, 0) < 0) { - errx(1, "uvmexp sysctl call failed"); + err(1, "uvmexp sysctl call failed"); } //kb_pagesize = uvmexp.pagesize / 1024; -- cgit v1.2.3 From c1b32892190ee0b605cac5087fb000f6cefc4b1e Mon Sep 17 00:00:00 2001 From: Michael McConville Date: Sat, 2 Jan 2016 12:20:40 -0500 Subject: Check for allocation failure Pointed out by Michael Reed. --- openbsd/OpenBSDProcessList.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'openbsd') diff --git a/openbsd/OpenBSDProcessList.c b/openbsd/OpenBSDProcessList.c index aa765a15..d6420853 100644 --- a/openbsd/OpenBSDProcessList.c +++ b/openbsd/OpenBSDProcessList.c @@ -139,7 +139,8 @@ char *OpenBSDProcessList_readProcessName(kvm_t* kd, struct kinfo_proc* kproc, in for (i = 0; arg[i] != NULL; i++) { len += strlen(arg[i]) + 1; } - buf = s = malloc(len); + if ((buf = s = malloc(len)) == NULL) + err(1, NULL); for (i = 0; arg[i] != NULL; i++) { n = strlcpy(buf, arg[i], (s + len) - buf); buf += n; -- cgit v1.2.3 From 3da36bbc61dbd27f717188a0a742bb637ca4f5bc Mon Sep 17 00:00:00 2001 From: Michael McConville Date: Sat, 2 Jan 2016 17:11:23 -0500 Subject: Use dynamically allocated memory for process names Even when they're constant, as is the case for zombie processes. --- openbsd/OpenBSDProcessList.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'openbsd') diff --git a/openbsd/OpenBSDProcessList.c b/openbsd/OpenBSDProcessList.c index d6420853..cbb90c91 100644 --- a/openbsd/OpenBSDProcessList.c +++ b/openbsd/OpenBSDProcessList.c @@ -132,9 +132,12 @@ char *OpenBSDProcessList_readProcessName(kvm_t* kd, struct kinfo_proc* kproc, in arg = kvm_getargv(kd, kproc, 500); if (arg == NULL) { - return "[zombie]"; // the FreeBSD port uses ki_comm, but we don't have it //return strndup(kproc->ki_comm); + if ((s = strdup("[zombie]")) == NULL) { + err(1, NULL); + } + return s; } for (i = 0; arg[i] != NULL; i++) { len += strlen(arg[i]) + 1; -- cgit v1.2.3 From 918cfd54d6ea3794f412ab891acd40bb6e555127 Mon Sep 17 00:00:00 2001 From: Michael McConville Date: Sat, 2 Jan 2016 22:05:20 -0500 Subject: Fall back to sysctl's command name, and a bugfix This is what OpenBSD's top(1) does when the libkvm call fails, and it's a good idea. This commit also fixes process name construction. The space was being written one character too far. --- openbsd/OpenBSDProcessList.c | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) (limited to 'openbsd') diff --git a/openbsd/OpenBSDProcessList.c b/openbsd/OpenBSDProcessList.c index cbb90c91..25f63349 100644 --- a/openbsd/OpenBSDProcessList.c +++ b/openbsd/OpenBSDProcessList.c @@ -130,28 +130,34 @@ char *OpenBSDProcessList_readProcessName(kvm_t* kd, struct kinfo_proc* kproc, in size_t cpsz, 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. + */ arg = kvm_getargv(kd, kproc, 500); if (arg == NULL) { - // the FreeBSD port uses ki_comm, but we don't have it - //return strndup(kproc->ki_comm); - if ((s = strdup("[zombie]")) == NULL) { - err(1, NULL); + if ((s = strdup(kproc->p_comm)) == NULL) { + err(1, NULL); } return s; } for (i = 0; arg[i] != NULL; i++) { len += strlen(arg[i]) + 1; } - if ((buf = s = malloc(len)) == NULL) - err(1, NULL); + if ((buf = s = malloc(len)) == NULL) { + if ((s = strdup(kproc->p_comm)) == NULL) { + err(1, NULL); + } + return s; + } for (i = 0; arg[i] != NULL; i++) { n = strlcpy(buf, arg[i], (s + len) - buf); buf += n; if (i == 0) { - *basenameEnd = n; + *basenameEnd = n; } - buf++; *buf = ' '; + buf++; } *(buf - 1) = '\0'; return s; -- cgit v1.2.3 From 198592a0f1d688f75820111be64ba9e2da4ac679 Mon Sep 17 00:00:00 2001 From: Michael McConville Date: Sun, 3 Jan 2016 16:56:33 -0500 Subject: Plug mem leak, improve CPU enumeration logic I think this leak may still exist in the FreeBSD port. --- openbsd/OpenBSDProcessList.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) (limited to 'openbsd') diff --git a/openbsd/OpenBSDProcessList.c b/openbsd/OpenBSDProcessList.c index 25f63349..9757ef91 100644 --- a/openbsd/OpenBSDProcessList.c +++ b/openbsd/OpenBSDProcessList.c @@ -48,19 +48,21 @@ 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; + int i, e; OpenBSDProcessList* fpl = calloc(1, sizeof(OpenBSDProcessList)); ProcessList* pl = (ProcessList*) fpl; size_t size = sizeof(pl->cpuCount); ProcessList_init(pl, Class(OpenBSDProcess), usersTable, pidWhiteList, userId); - pl->cpuCount = 1; // default to 1 on sysctl() error - (void)sysctl(mib, 2, &pl->cpuCount, &size, NULL, 0); + e = sysctl(mib, 2, &pl->cpuCount, &size, NULL, 0); + if (e == -1 || pl->cpuCount < 1) { + pl->cpuCount = 1; + } fpl->cpus = realloc(fpl->cpus, pl->cpuCount * sizeof(CPUData)); size = sizeof(fscale); if (sysctl(fmib, 2, &fscale, &size, NULL, 0) < 0) - err(1, "fscale sysctl call failed"); + err(1, "fscale sysctl call failed"); for (i = 0; i < pl->cpuCount; i++) { fpl->cpus[i].totalTime = 1; @@ -80,6 +82,8 @@ void ProcessList_delete(ProcessList* this) { const OpenBSDProcessList* fpl = (OpenBSDProcessList*) this; if (fpl->kd) kvm_close(fpl->kd); + free(fpl->cpus); + ProcessList_done(this); free(this); } -- cgit v1.2.3 From e595f6865e5d58cc6996124dac9df8384519ce20 Mon Sep 17 00:00:00 2001 From: Michael McConville Date: Mon, 4 Jan 2016 16:20:51 -0500 Subject: Rename variable for consistency Suggested by Hisham. --- openbsd/OpenBSDProcessList.c | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) (limited to 'openbsd') diff --git a/openbsd/OpenBSDProcessList.c b/openbsd/OpenBSDProcessList.c index 9757ef91..6da38efc 100644 --- a/openbsd/OpenBSDProcessList.c +++ b/openbsd/OpenBSDProcessList.c @@ -49,8 +49,8 @@ ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidWhiteList, ui int mib[] = { CTL_HW, HW_NCPU }; int fmib[] = { CTL_KERN, KERN_FSCALE }; int i, e; - OpenBSDProcessList* fpl = calloc(1, sizeof(OpenBSDProcessList)); - ProcessList* pl = (ProcessList*) fpl; + OpenBSDProcessList* opl = calloc(1, sizeof(OpenBSDProcessList)); + ProcessList* pl = (ProcessList*) opl; size_t size = sizeof(pl->cpuCount); ProcessList_init(pl, Class(OpenBSDProcess), usersTable, pidWhiteList, userId); @@ -58,31 +58,31 @@ ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidWhiteList, ui if (e == -1 || pl->cpuCount < 1) { pl->cpuCount = 1; } - fpl->cpus = realloc(fpl->cpus, pl->cpuCount * sizeof(CPUData)); + opl->cpus = realloc(opl->cpus, pl->cpuCount * sizeof(CPUData)); size = sizeof(fscale); if (sysctl(fmib, 2, &fscale, &size, NULL, 0) < 0) err(1, "fscale sysctl call failed"); for (i = 0; i < pl->cpuCount; i++) { - fpl->cpus[i].totalTime = 1; - fpl->cpus[i].totalPeriod = 1; + opl->cpus[i].totalTime = 1; + opl->cpus[i].totalPeriod = 1; } pageSizeKb = PAGE_SIZE_KB; // XXX: last arg should eventually be an errbuf - fpl->kd = kvm_open(NULL, NULL, NULL, KVM_NO_FILES, NULL); - assert(fpl->kd); + opl->kd = kvm_open(NULL, NULL, NULL, KVM_NO_FILES, NULL); + assert(opl->kd); return pl; } void ProcessList_delete(ProcessList* this) { - const OpenBSDProcessList* fpl = (OpenBSDProcessList*) this; - if (fpl->kd) kvm_close(fpl->kd); + const OpenBSDProcessList* opl = (OpenBSDProcessList*) this; + if (opl->kd) kvm_close(opl->kd); - free(fpl->cpus); + free(opl->cpus); ProcessList_done(this); free(this); @@ -102,7 +102,7 @@ static inline void OpenBSDProcessList_scanMemoryInfo(ProcessList* pl) { pl->totalMem = uvmexp.npages * pageSizeKb; /* - const OpenBSDProcessList* fpl = (OpenBSDProcessList*) pl; + const OpenBSDProcessList* opl = (OpenBSDProcessList*) pl; size_t len = sizeof(pl->totalMem); sysctl(MIB_hw_physmem, 2, &(pl->totalMem), &len, NULL, 0); @@ -114,7 +114,7 @@ static inline void OpenBSDProcessList_scanMemoryInfo(ProcessList* pl) { pl->cachedMem *= pageSizeKb; struct kvm_swap swap[16]; - int nswap = kvm_getswapinfo(fpl->kd, swap, sizeof(swap)/sizeof(swap[0]), 0); + int nswap = kvm_getswapinfo(opl->kd, swap, sizeof(swap)/sizeof(swap[0]), 0); pl->totalSwap = 0; pl->usedSwap = 0; for (int i = 0; i < nswap; i++) { @@ -180,7 +180,7 @@ double getpcpu(const struct kinfo_proc *kp) { } void ProcessList_goThroughEntries(ProcessList* this) { - OpenBSDProcessList* fpl = (OpenBSDProcessList*) this; + OpenBSDProcessList* opl = (OpenBSDProcessList*) this; Settings* settings = this->settings; bool hideKernelThreads = settings->hideKernelThreads; bool hideUserlandThreads = settings->hideUserlandThreads; @@ -194,7 +194,7 @@ void ProcessList_goThroughEntries(ProcessList* this) { OpenBSDProcessList_scanMemoryInfo(this); // use KERN_PROC_KTHREAD to also include kernel threads - struct kinfo_proc* kprocs = kvm_getprocs(fpl->kd, KERN_PROC_ALL, 0, sizeof(struct kinfo_proc), &count); + struct kinfo_proc* kprocs = kvm_getprocs(opl->kd, KERN_PROC_ALL, 0, sizeof(struct kinfo_proc), &count); //struct kinfo_proc* kprocs = getprocs(KERN_PROC_ALL, 0, &count); for (i = 0; i < count; i++) { @@ -218,11 +218,11 @@ void ProcessList_goThroughEntries(ProcessList* this) { proc->starttime_ctime = kproc->p_ustart_sec; proc->user = UsersTable_getRef(this->usersTable, proc->st_uid); ProcessList_add((ProcessList*)this, proc); - proc->comm = OpenBSDProcessList_readProcessName(fpl->kd, kproc, &proc->basenameOffset); + proc->comm = OpenBSDProcessList_readProcessName(opl->kd, kproc, &proc->basenameOffset); } else { if (settings->updateProcessNames) { free(proc->comm); - proc->comm = OpenBSDProcessList_readProcessName(fpl->kd, kproc, &proc->basenameOffset); + proc->comm = OpenBSDProcessList_readProcessName(opl->kd, kproc, &proc->basenameOffset); } } -- cgit v1.2.3