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.
This commit is contained in:
Michael McConville 2016-01-02 22:05:20 -05:00
parent 3da36bbc61
commit 918cfd54d6
1 changed files with 14 additions and 8 deletions

View File

@ -130,11 +130,13 @@ char *OpenBSDProcessList_readProcessName(kvm_t* kd, struct kinfo_proc* kproc, in
size_t cpsz, len = 0, n; size_t cpsz, len = 0, n;
int i; 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); arg = kvm_getargv(kd, kproc, 500);
if (arg == NULL) { if (arg == NULL) {
// the FreeBSD port uses ki_comm, but we don't have it if ((s = strdup(kproc->p_comm)) == NULL) {
//return strndup(kproc->ki_comm);
if ((s = strdup("[zombie]")) == NULL) {
err(1, NULL); err(1, NULL);
} }
return s; return s;
@ -142,16 +144,20 @@ char *OpenBSDProcessList_readProcessName(kvm_t* kd, struct kinfo_proc* kproc, in
for (i = 0; arg[i] != NULL; i++) { for (i = 0; arg[i] != NULL; i++) {
len += strlen(arg[i]) + 1; len += strlen(arg[i]) + 1;
} }
if ((buf = s = malloc(len)) == NULL) if ((buf = s = malloc(len)) == NULL) {
if ((s = strdup(kproc->p_comm)) == NULL) {
err(1, NULL); err(1, NULL);
}
return s;
}
for (i = 0; arg[i] != NULL; i++) { for (i = 0; arg[i] != NULL; i++) {
n = strlcpy(buf, arg[i], (s + len) - buf); n = strlcpy(buf, arg[i], (s + len) - buf);
buf += n; buf += n;
if (i == 0) { if (i == 0) {
*basenameEnd = n; *basenameEnd = n;
} }
buf++;
*buf = ' '; *buf = ' ';
buf++;
} }
*(buf - 1) = '\0'; *(buf - 1) = '\0';
return s; return s;