- use getifaddrs(3)

- stop linking with libkvm now that it's no longer used

ok mpi@
This commit is contained in:
jasper 2015-12-07 14:58:54 +00:00
parent e1470fad1b
commit 94c946e035
3 changed files with 128 additions and 15 deletions

View File

@ -1,16 +1,16 @@
# $OpenBSD: Makefile,v 1.12 2013/03/11 11:35:58 espie Exp $
# $OpenBSD: Makefile,v 1.13 2015/12/07 14:58:54 jasper Exp $
COMMENT= wm-dockapp; simple network interface monitoring tool
DISTNAME= wmnetload-1.3
REVISION= 2
REVISION= 3
CATEGORIES= net x11 x11/windowmaker
HOMEPAGE= http://freshmeat.net/projects/wmnetload
# GPL
PERMIT_PACKAGE_CDROM= Yes
WANTLIB= X11 Xext Xpm c kvm m pthread-stubs xcb dockapp
WANTLIB= X11 Xext Xpm c m dockapp
MASTER_SITES= ftp://truffula.com/pub/

View File

@ -1,13 +1,12 @@
$OpenBSD: patch-configure,v 1.1.1.1 2003/05/11 02:56:56 wilfried Exp $
$OpenBSD: patch-configure,v 1.2 2015/12/07 14:58:54 jasper Exp $
--- configure.orig Sun Feb 23 10:08:00 2003
+++ configure Fri May 9 17:41:24 2003
@@ -3052,6 +3052,10 @@ case $host_os in
+++ configure Mon Dec 7 14:35:10 2015
@@ -3052,6 +3052,9 @@ case $host_os in
OS=freebsd
LIBRARY_RPATH="$LIBRARY_RPATH:/usr/X11R6/lib"
;;
+*openbsd*)
+ OS=openbsd
+ LIBS="$LIBS -lkvm"
+ ;;
*)
echo ""

View File

@ -1,11 +1,125 @@
$OpenBSD: patch-src_ifstat_openbsd_c,v 1.1 2013/11/25 22:06:40 sthen Exp $
--- src/ifstat_openbsd.c.orig Mon Nov 25 15:05:36 2013
+++ src/ifstat_openbsd.c Mon Nov 25 15:06:02 2013
@@ -27,6 +27,7 @@
$OpenBSD: patch-src_ifstat_openbsd_c,v 1.2 2015/12/07 14:58:54 jasper Exp $
use getifaddrs(3) instead of libkvm.
--- src/ifstat_openbsd.c.orig Tue Jan 29 09:09:18 2002
+++ src/ifstat_openbsd.c Mon Dec 7 14:31:38 2015
@@ -27,19 +27,14 @@
#include <sys/types.h>
#include <sys/socket.h>
#include <net/if.h>
+#include <net/if_var.h>
#include <fcntl.h>
#include <kvm.h>
#include <limits.h>
-#include <fcntl.h>
-#include <kvm.h>
-#include <limits.h>
-#include <nlist.h>
+#include <ifaddrs.h>
#include <stdlib.h>
-#include <string.h>
#include "ifstat.h"
#include "utils.h"
struct ifstatstate {
- void *ifnet_head;
- kvm_t *kd;
+ struct ifaddrs *ifap;
};
/*
@@ -51,8 +46,6 @@ ifstatstate_t *
if_statinit(void)
{
ifstatstate_t *statep;
- struct nlist ifnet[] = { { "_ifnet" }, { NULL }};
- char errbuf[_POSIX2_LINE_MAX];
statep = malloc(sizeof (ifstatstate_t));
if (statep == NULL) {
@@ -60,35 +53,8 @@ if_statinit(void)
return (NULL);
}
- /*
- * Just for the duration of kmem_openfiles(), get privileges
- * needed to access kmem.
- */
- chpriv(PRIV_GAIN);
- statep->kd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf);
- chpriv(PRIV_DROP);
- if (statep->kd == NULL) {
- warn("cannot access raw kernel memory: %s\n", errbuf);
- free(statep);
- return (NULL);
- }
-
- if (kvm_nlist(statep->kd, ifnet) == -1) {
- warn("cannot populate kernel namelist: %s\n",
- kvm_geterr(statep->kd));
- goto fail;
- }
-
- if (kvm_read(statep->kd, ifnet->n_value, &statep->ifnet_head,
- sizeof (ifnet->n_value)) != sizeof (ifnet->n_value)) {
- warn("cannot find ifnet list head: %s\n",
- kvm_geterr(statep->kd));
- goto fail;
- }
-
return (statep);
fail:
- (void) kvm_close(statep->kd);
free(statep);
return (NULL);
}
@@ -100,22 +66,33 @@ fail:
int
if_stats(const char *ifname, ifstatstate_t *statep, ifstats_t *ifstatsp)
{
- void *ifnet_addr = statep->ifnet_head;
- struct ifnet ifnet;
+ struct ifaddrs *ifa;
- for (; ifnet_addr != NULL; ifnet_addr = TAILQ_NEXT(&ifnet, if_list)) {
+ if (getifaddrs(&statep->ifap) != 0) {
+ warn("failed to get interface addresses");
+ return (0);
+ }
- if (kvm_read(statep->kd, (unsigned long)ifnet_addr, &ifnet,
- sizeof (struct ifnet)) != sizeof (struct ifnet))
- return (0);
+ for (ifa = statep->ifap; ifa != NULL; ifa = ifa->ifa_next) {
+ if (strcmp(ifname, ifa->ifa_name)) {
+ continue;
+ }
- if (strcmp(ifnet.if_xname, ifname) == 0) {
- ifstatsp->rxbytes = ifnet.if_ibytes;
- ifstatsp->txbytes = ifnet.if_obytes;
- return (1);
+ if (ifa->ifa_addr->sa_family == AF_LINK) {
+ struct sockaddr_dl *dl = (struct sockaddr_dl *)ifa->ifa_addr;
+ struct if_data *ifd = NULL;
+
+ ifd = ifa->ifa_data;
+
+ if (ifd != NULL) {
+ ifstatsp->rxbytes = ifd->ifi_ibytes;
+ ifstatsp->txbytes = ifd->ifi_obytes;
+ return 1;
+ }
}
}
+ freeifaddrs(statep->ifap);
return (0);
}
@@ -125,6 +102,6 @@ if_stats(const char *ifname, ifstatstate_t *statep, if
void
if_statfini(ifstatstate_t *statep)
{
- (void) kvm_close(statep->kd);
+ freeifaddrs(statep->ifap);
free(statep);
}