Changeset View
Changeset View
Standalone View
Standalone View
top/utils.c
Show All 11 Lines | |||||
*/ | */ | ||||
/* | /* | ||||
* This file contains various handy utilities used by top. | * This file contains various handy utilities used by top. | ||||
*/ | */ | ||||
#include "top.h" | #include "top.h" | ||||
#include <sys/param.h> | |||||
#include <sys/sysctl.h> | |||||
#include <sys/user.h> | |||||
#include <stdlib.h> | #include <stdlib.h> | ||||
#include <stdio.h> | #include <stdio.h> | ||||
#include <string.h> | #include <string.h> | ||||
#include <fcntl.h> | |||||
#include <paths.h> | |||||
#include <kvm.h> | |||||
void quit(int); | |||||
int | int | ||||
atoiwi(char *str) | atoiwi(char *str) | ||||
{ | { | ||||
int len; | int len; | ||||
len = strlen(str); | len = strlen(str); | ||||
if (len != 0) | if (len != 0) | ||||
{ | { | ||||
▲ Show 20 Lines • Show All 421 Lines • ▼ Show 20 Lines | unsigned long long amt; | ||||
} | } | ||||
} | } | ||||
p = strecpy(p, itoa((int)amt)); | p = strecpy(p, itoa((int)amt)); | ||||
*p++ = tag; | *p++ = tag; | ||||
*p = '\0'; | *p = '\0'; | ||||
return(ret); | return(ret); | ||||
} | |||||
int | |||||
find_pid(pid_t pid) | |||||
{ | |||||
kvm_t *kd = NULL; | |||||
struct kinfo_proc *pbase = NULL; | |||||
int i, nproc; | |||||
kd = kvm_open(NULL, _PATH_DEVNULL, NULL, O_RDONLY, "kvm_open"); | |||||
if (kd == NULL) | |||||
eadler: Can we use a named constant for 23? Also are we sure we want to quit and not generate an error… | |||||
novelAuthorUnsubmitted Not Done Inline ActionsDone. 23 is also used in other places, so updated those as well. Also added an error message. novel: Done. 23 is also used in other places, so updated those as well. Also added an error message. | |||||
quit(23); | |||||
pbase = kvm_getprocs(kd, KERN_PROC_PID, pid, &nproc); | |||||
if (pbase == NULL) | |||||
quit(23); | |||||
eadlerUnsubmitted Not Done Inline Actionsditto to above eadler: ditto to above | |||||
novelAuthorUnsubmitted Not Done Inline ActionsI re-wrote this chunk a bit. As we request specific pid from kvm_getprocs(), it's normal if it returns NULL, should not exit in this case and let the caller of find_pid() handle the invalid pid. I've also updated kvm_open() call to set errstr to NULL as we generate our own messages for when pid was not found. Also, I noticed as I leak kvm_t because not calling kvm_close(), should be fixed now as well. novel: I re-wrote this chunk a bit. As we request specific pid from kvm_getprocs(), it's normal if it… | |||||
if ((nproc == 1) && (pbase->ki_pid == pid)) { | |||||
printf("pid found!\n"); | |||||
return 1; | |||||
} | |||||
exit(12); | |||||
eadlerUnsubmitted Not Done Inline Actionsditto: lets use a named constant? eadler: ditto: lets use a named constant? | |||||
novelAuthorUnsubmitted Not Done Inline ActionsThat's actually a debug leftover, removed. novel: That's actually a debug leftover, removed. | |||||
return 0; | |||||
} | } |
Can we use a named constant for 23? Also are we sure we want to quit and not generate an error message?
also, I prefer braces even for single line ifs.