summaryrefslogtreecommitdiffstats
path: root/usr.bin/procstat
diff options
context:
space:
mode:
authorpeter <peter@FreeBSD.org>2008-12-01 01:45:52 +0000
committerpeter <peter@FreeBSD.org>2008-12-01 01:45:52 +0000
commit6c7a0d64c96e8f0c9db396ce39bc593787ff794f (patch)
treef1bfdb6756551bc00eddfa072afc1c6e93ada5df /usr.bin/procstat
parent16271b5bfa14998575e875d8911bbe565051831c (diff)
downloadFreeBSD-src-6c7a0d64c96e8f0c9db396ce39bc593787ff794f.zip
FreeBSD-src-6c7a0d64c96e8f0c9db396ce39bc593787ff794f.tar.gz
Use libutil wrapper for kinfo_get{file|vmmap}()
Diffstat (limited to 'usr.bin/procstat')
-rw-r--r--usr.bin/procstat/Makefile4
-rw-r--r--usr.bin/procstat/procstat_files.c35
-rw-r--r--usr.bin/procstat/procstat_vm.c41
3 files changed, 16 insertions, 64 deletions
diff --git a/usr.bin/procstat/Makefile b/usr.bin/procstat/Makefile
index e3c77e4..6cb5895 100644
--- a/usr.bin/procstat/Makefile
+++ b/usr.bin/procstat/Makefile
@@ -12,4 +12,8 @@ SRCS= procstat.c \
procstat_threads.c \
procstat_vm.c
+LDADD+= -lutil
+DPADD+= ${LIBUTIL}
+WARNS?= 4
+
.include <bsd.prog.mk>
diff --git a/usr.bin/procstat/procstat_files.c b/usr.bin/procstat/procstat_files.c
index 3667913..1f620f3 100644
--- a/usr.bin/procstat/procstat_files.c
+++ b/usr.bin/procstat/procstat_files.c
@@ -41,6 +41,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <libutil.h>
#include "procstat.h"
@@ -134,42 +135,18 @@ void
procstat_files(pid_t pid, struct kinfo_proc *kipp)
{
struct kinfo_file *freep, *kif;
- int error, name[4];
- unsigned int i;
+ int i, cnt;
const char *str;
- size_t len;
if (!hflag)
printf("%5s %-16s %4s %1s %1s %-8s %3s %7s %-3s %-12s\n",
"PID", "COMM", "FD", "T", "V", "FLAGS", "REF", "OFFSET",
"PRO", "NAME");
- name[0] = CTL_KERN;
- name[1] = KERN_PROC;
- name[2] = KERN_PROC_FILEDESC;
- name[3] = pid;
-
- error = sysctl(name, 4, NULL, &len, NULL, 0);
- if (error < 0 && errno != ESRCH && errno != EPERM) {
- warn("sysctl: kern.proc.filedesc: %d", pid);
- return;
- }
- if (error < 0)
- return;
-
- freep = kif = malloc(len);
- if (kif == NULL)
- err(-1, "malloc");
-
- if (sysctl(name, 4, kif, &len, NULL, 0) < 0) {
- warn("sysctl: kern.proc.filedesc %d", pid);
- free(freep);
- return;
- }
-
- for (i = 0; i < len / sizeof(*kif); i++, kif++) {
- if (kif->kf_structsize != sizeof(*kif))
- errx(-1, "kinfo_file mismatch");
+ freep = kinfo_getfile(pid, &cnt);
+ for (i = 0; i < cnt; i++) {
+ kif = &freep[i];
+
printf("%5d ", pid);
printf("%-16s ", kipp->ki_comm);
switch (kif->kf_fd) {
diff --git a/usr.bin/procstat/procstat_vm.c b/usr.bin/procstat/procstat_vm.c
index a658382..f7ffe58 100644
--- a/usr.bin/procstat/procstat_vm.c
+++ b/usr.bin/procstat/procstat_vm.c
@@ -34,6 +34,7 @@
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
+#include <libutil.h>
#include "procstat.h"
@@ -41,10 +42,9 @@ void
procstat_vm(pid_t pid, struct kinfo_proc *kipp __unused)
{
struct kinfo_vmentry *freep, *kve;
- int error, name[4], ptrwidth;
- unsigned int i;
+ int ptrwidth;
+ int i, cnt;
const char *str;
- size_t len;
ptrwidth = 2*sizeof(void *) + 2;
if (!hflag)
@@ -52,38 +52,9 @@ procstat_vm(pid_t pid, struct kinfo_proc *kipp __unused)
"PID", ptrwidth, "START", ptrwidth, "END", "PRT", "RES",
"PRES", "REF", "SHD", "FL", "TP", "PATH");
- name[0] = CTL_KERN;
- name[1] = KERN_PROC;
- name[2] = KERN_PROC_VMMAP;
- name[3] = pid;
-
- len = 0;
- error = sysctl(name, 4, NULL, &len, NULL, 0);
- if (error < 0 && errno != ESRCH && errno != EPERM) {
- warn("sysctl: kern.proc.vmmap: %d", pid);
- return;
- }
- if (error < 0)
- return;
-
- /*
- * Especially if running procstat -sv, we may need room for more
- * mappings when printing than were present when we queried, so pad
- * out the allocation a bit.
- */
- len += sizeof(*kve) * 3;
- freep = kve = malloc(len);
- if (kve == NULL)
- err(-1, "malloc");
- if (sysctl(name, 4, kve, &len, NULL, 0) < 0) {
- warn("sysctl: kern.proc.vmmap: %d", pid);
- free(freep);
- return;
- }
-
- for (i = 0; i < (len / sizeof(*kve)); i++, kve++) {
- if (kve->kve_structsize != sizeof(*kve))
- errx(-1, "kinfo_vmentry structure mismatch");
+ freep = kinfo_getvmmap(pid, &cnt);
+ for (i = 0; i < cnt; i++) {
+ kve = &freep[i];
printf("%5d ", pid);
printf("%*p ", ptrwidth, kve->kve_start);
printf("%*p ", ptrwidth, kve->kve_end);
OpenPOWER on IntegriCloud