Change this to support the new way swap device information is exported

via sysctl, and clean up some style and (size_t != int) issues.
This commit is contained in:
Thomas Moestl 2001-06-01 22:57:07 +00:00
parent 5c5c8fa826
commit e04a7c4ae5

View File

@ -27,7 +27,10 @@ static const char rcsid[] =
#include <sys/blist.h>
#include <sys/sysctl.h>
#include <vm/vm_param.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <kvm.h>
#include <nlist.h>
@ -65,9 +68,7 @@ static int kvm_getswapinfo2(kvm_t *kd, struct kvm_swap *swap_ary,
static int kvm_getswapinfo_kvm(kvm_t *, struct kvm_swap *, int, int);
static int kvm_getswapinfo_sysctl(kvm_t *, struct kvm_swap *, int, int);
static int nlist_init(kvm_t *);
static int getsysctl(kvm_t *, char *, void *, int);
static int getsysctl2(kvm_t *, char *, char *, char *, int, void *, int);
static int getsysctl(kvm_t *, char *, void *, size_t);
#define SVAR(var) __STRING(var) /* to force expansion */
#define KGET(idx, var) \
@ -89,15 +90,15 @@ static int getsysctl2(kvm_t *, char *, char *, char *, int, void *, int);
return (0); \
}
#define GETSWDEVNAME(dev, str, flags) \
if (dev == NODEV) { \
strlcpy(str, "[NFS swap]", sizeof(str)); \
} else { \
snprintf( \
str, sizeof(str),"%s%s", \
((flags & SWIF_DEV_PREFIX) ? _PATH_DEV : ""), \
devname(dev, S_IFCHR) \
); \
#define GETSWDEVNAME(dev, str, flags) \
if (dev == NODEV) { \
strlcpy(str, "[NFS swap]", sizeof(str)); \
} else { \
snprintf( \
str, sizeof(str),"%s%s", \
((flags & SWIF_DEV_PREFIX) ? _PATH_DEV : ""), \
devname(dev, S_IFCHR) \
); \
}
int
@ -458,11 +459,11 @@ getswapinfo_radix(kvm_t *kd, struct kvm_swap *swap_ary, int swap_max, int flags)
);
}
#define GETSYSCTL(kd, name, var) \
getsysctl(kd, name, &(var), sizeof(var))
#define GETSYSCTL2(kd, pref, suff, buf, var) \
getsysctl2(kd, pref, suff, buf, sizeof(buf), &(var), \
sizeof(var))
#define GETSYSCTL(kd, name, var) \
getsysctl(kd, name, &(var), sizeof(var))
/* The maximum MIB length for vm.swap_info and an additional device number */
#define SWI_MAXMIB 3
int
kvm_getswapinfo_sysctl(
@ -471,53 +472,61 @@ kvm_getswapinfo_sysctl(
int swap_max,
int flags
) {
int ti = 0;
udev_t dev;
char node[15];
char buf[20];
int used, ttl, i;
if (!GETSYSCTL(kd, "vm.nswapdev", unswdev))
return -1;
ti = unswdev;
if (ti >= swap_max)
ti = swap_max - 1;
if (ti >= 0)
bzero(swap_ary, sizeof(struct kvm_swap) * (ti + 1));
int ti, ttl;
size_t mibi, len;
int soid[SWI_MAXMIB];
struct xswdev xsd;
struct kvm_swap tot;
if (!GETSYSCTL(kd, "vm.dmmax", dmmax))
return -1;
for (i = 0; i < unswdev; ++i) {
if (snprintf(node, sizeof(node), "vm.swapdev%d.", i) >=
sizeof(node)) {
_kvm_err(kd, kd->program, "XXX: node buffer too small");
return -1;
}
if (!GETSYSCTL2(kd, node, "nblks", buf, ttl))
return -1;
if (!GETSYSCTL2(kd, node, "used", buf, used))
return -1;
ttl -= dmmax;
if (i < ti) {
if (!GETSYSCTL2(kd, node, "dev", buf, dev))
return -1;
if (!GETSYSCTL2(kd, node, "flags", buf,
swap_ary[i].ksw_flags))
return -1;
swap_ary[i].ksw_total = ttl;
swap_ary[i].ksw_used = used;
GETSWDEVNAME(dev, swap_ary[i].ksw_devname, flags);
}
if (ti >= 0) {
swap_ary[ti].ksw_total += ttl;
swap_ary[ti].ksw_used += used;
}
mibi = SWI_MAXMIB - 1;
if (sysctlnametomib("vm.swap_info", soid, &mibi) == -1) {
_kvm_err(kd, kd->program, "sysctlnametomib failed: %s",
strerror(errno));
return -1;
}
bzero(&tot, sizeof(tot));
for (unswdev = 0;; unswdev++) {
soid[mibi] = unswdev;
len = sizeof(xsd);
if (sysctl(soid, mibi + 1, &xsd, &len, NULL, 0) == -1) {
if (errno == ENOENT)
break;
_kvm_err(kd, kd->program, "cannot read sysctl: %s.",
strerror(errno));
return -1;
}
if (len != sizeof(xsd)) {
_kvm_err(kd, kd->program, "struct xswdev has unexpected "
"size; kernel and libkvm out of sync?");
return -1;
}
if (xsd.xsw_version != XSWDEV_VERSION) {
_kvm_err(kd, kd->program, "struct xswdev version "
"mismatch; kernel and libkvm out of sync?");
return -1;
}
ttl = xsd.xsw_nblks - dmmax;
if (unswdev < swap_max - 1) {
bzero(&swap_ary[unswdev], sizeof(swap_ary[unswdev]));
swap_ary[unswdev].ksw_total = ttl;
swap_ary[unswdev].ksw_used = xsd.xsw_used;
swap_ary[unswdev].ksw_flags = xsd.xsw_flags;
GETSWDEVNAME(xsd.xsw_dev, swap_ary[unswdev].ksw_devname,
flags);
}
tot.ksw_total += ttl;
tot.ksw_used += xsd.xsw_used;
}
ti = unswdev;
if (ti >= swap_max)
ti = swap_max - 1;
if (ti >= 0)
swap_ary[ti] = tot;
return(ti);
}
@ -574,11 +583,12 @@ getsysctl (
kvm_t *kd,
char *name,
void *ptr,
int len
size_t len
) {
int nlen = len;
size_t nlen = len;
if (sysctlbyname(name, ptr, &nlen, NULL, 0) == -1) {
_kvm_err(kd, kd->program, "cannot read sysctl %s", name);
_kvm_err(kd, kd->program, "cannot read sysctl %s:%s", name,
strerror(errno));
return (0);
}
if (nlen != len) {
@ -587,24 +597,3 @@ getsysctl (
}
return (1);
}
static int
getsysctl2 (
kvm_t *kd,
char *pref,
char *suff,
char *buf,
int buflen,
void *ptr,
int len
) {
if (strlcpy(buf, pref, buflen) >= buflen) {
_kvm_err(kd, kd->program, "getsysctl2: string buffer too small");
return (0);
}
if (strlcat(buf, suff, buflen) >= buflen) {
_kvm_err(kd, kd->program, "getsysctl2: string buffer too small");
return (0);
}
return getsysctl(kd, buf, ptr, len);
}