From 22cfda6332bcfe89e143ed9475032bec7222bfa3 Mon Sep 17 00:00:00 2001 From: Michael McConville Date: Sat, 2 Jan 2016 11:57:53 -0500 Subject: [PATCH 1/5] 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 | 54 +++++++++++++++++------------------- openbsd/Platform.h | 1 + 2 files changed, 27 insertions(+), 28 deletions(-) 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; + char *s, *buf, **arg; + size_t cpsz, len = 0, n; + int i; - 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--; - } + 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); } - - *buf = '\0'; - return str; + 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[]; From ae5c01f4859e3296292bd34b2efc5a7720368d98 Mon Sep 17 00:00:00 2001 From: Michael McConville Date: Sat, 2 Jan 2016 12:17:35 -0500 Subject: [PATCH 2/5] 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(-) 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; From c1b32892190ee0b605cac5087fb000f6cefc4b1e Mon Sep 17 00:00:00 2001 From: Michael McConville Date: Sat, 2 Jan 2016 12:20:40 -0500 Subject: [PATCH 3/5] Check for allocation failure Pointed out by Michael Reed. --- openbsd/OpenBSDProcessList.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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; From 3da36bbc61dbd27f717188a0a742bb637ca4f5bc Mon Sep 17 00:00:00 2001 From: Michael McConville Date: Sat, 2 Jan 2016 17:11:23 -0500 Subject: [PATCH 4/5] 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(-) 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; From 918cfd54d6ea3794f412ab891acd40bb6e555127 Mon Sep 17 00:00:00 2001 From: Michael McConville Date: Sat, 2 Jan 2016 22:05:20 -0500 Subject: [PATCH 5/5] 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(-) 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;