diff options
author | Todd C. Miller <millert@cvs.openbsd.org> | 2001-10-02 01:14:43 +0000 |
---|---|---|
committer | Todd C. Miller <millert@cvs.openbsd.org> | 2001-10-02 01:14:43 +0000 |
commit | 394460fedf1c2f99c7dd2b979c717946c78845e0 (patch) | |
tree | 848923b4549707c79143df122945fefd1ea067f6 /libexec/rpc.rstatd | |
parent | b59cc997801934f2bed24ba363b35152c8d1fbca (diff) |
o Remove #if{,n}def BSD stuff
o Replace nlist and kvm_* with sysctl() and getifaddrs(3)
Fixes an issue on alpha where we would run off the end of the interface list.
Diffstat (limited to 'libexec/rpc.rstatd')
-rw-r--r-- | libexec/rpc.rstatd/rstat_proc.c | 194 |
1 files changed, 36 insertions, 158 deletions
diff --git a/libexec/rpc.rstatd/rstat_proc.c b/libexec/rpc.rstatd/rstat_proc.c index 2a274739680..891086f81bb 100644 --- a/libexec/rpc.rstatd/rstat_proc.c +++ b/libexec/rpc.rstatd/rstat_proc.c @@ -1,4 +1,4 @@ -/* $OpenBSD: rstat_proc.c,v 1.16 2001/09/19 21:47:31 deraadt Exp $ */ +/* $OpenBSD: rstat_proc.c,v 1.17 2001/10/02 01:14:42 millert Exp $ */ /* * Sun RPC is a product of Sun Microsystems, Inc. and is provided for @@ -31,7 +31,7 @@ #ifndef lint /*static char sccsid[] = "from: @(#)rpc.rstatd.c 1.1 86/09/25 Copyr 1984 Sun Micro";*/ /*static char sccsid[] = "from: @(#)rstat_proc.c 2.2 88/08/01 4.0 RPCSRC";*/ -static char rcsid[] = "$OpenBSD: rstat_proc.c,v 1.16 2001/09/19 21:47:31 deraadt Exp $"; +static char rcsid[] = "$OpenBSD: rstat_proc.c,v 1.17 2001/10/02 01:14:42 millert Exp $"; #endif /* @@ -40,31 +40,25 @@ static char rcsid[] = "$OpenBSD: rstat_proc.c,v 1.16 2001/09/19 21:47:31 deraadt * Copyright (c) 1984 by Sun Microsystems, Inc. */ +#include <sys/param.h> +#include <sys/vmmeter.h> +#include <sys/dkstat.h> +#include <sys/socket.h> +#include <sys/sysctl.h> +#include <net/if.h> +#include <vm/vm.h> +#include <uvm/uvm_extern.h> + #include <stdio.h> #include <stdlib.h> #include <string.h> #include <signal.h> +#include <syslog.h> #include <fcntl.h> -#include <kvm.h> #include <limits.h> -#include <rpc/rpc.h> -#include <sys/socket.h> -#include <nlist.h> -#include <syslog.h> #include <errno.h> -#include <sys/param.h> -#ifdef BSD -#include <sys/vmmeter.h> -#include <sys/dkstat.h> +#include <ifaddrs.h> #include "dkstats.h" -#else -#include <sys/dk.h> -#endif -#include <net/if.h> - -#include <vm/vm.h> -#include <sys/sysctl.h> -#include <uvm/uvm_extern.h> #undef FSHIFT /* Use protocol's shift and scale values */ #undef FSCALE @@ -77,44 +71,16 @@ static char rcsid[] = "$OpenBSD: rstat_proc.c,v 1.16 2001/09/19 21:47:31 deraadt #undef if_collisions #include <rpcsvc/rstat.h> -#ifdef BSD -#define BSD_CPUSTATES 5 /* Use protocol's idea of CPU states */ int cp_xlat[CPUSTATES] = { CP_USER, CP_NICE, CP_SYS, CP_IDLE }; -#endif - -struct nlist nl[] = { -#define X_IFNET 0 - { "_ifnet" }, -#define X_BOOTTIME 1 - { "_boottime" }, -#ifndef BSD -#define X_HZ 2 - { "_hz" }, -#define X_CPTIME 3 - { "_cp_time" }, -#define X_DKXFER 4 - { "_dk_xfer" }, -#define X_CNT 5 - { "_cnt" }, -#else -#endif - { NULL }, -}; -#ifdef BSD extern int dk_ndrive; /* from dkstats.c */ extern struct _disk cur, last; char *memf = NULL, *nlistf = NULL; -#endif int hz; -struct ifnet_head ifnetq; /* chain of ethernet interfaces */ -int numintfs; - extern int from_inetd; int sincelastreq = 0; /* number of alarms since last request */ extern int closedown; -kvm_t *kfd; union { struct stats s1; @@ -125,7 +91,6 @@ union { int stats_service(); void updatestat(int); void setup(void); -int havedisk(void); static int stat_is_init = 0; @@ -186,7 +151,7 @@ rstatproc_havedisk_3_svc(arg, rqstp) if (!stat_is_init) stat_init(); sincelastreq = 0; - have = havedisk(); + have = dk_ndrive != 0; return (&have); } @@ -209,16 +174,14 @@ rstatproc_havedisk_1_svc(arg, rqstp) void updatestat(int sig) { - long off; int i, mib[2], save_errno = errno; struct uvmexp uvmexp; size_t len; - struct ifnet ifnet; + struct if_data *ifdp; + struct ifaddrs *ifaddrs, *ifa; double avrun[3]; struct timeval tm, btm; -#ifdef BSD long *cp_time = cur.cp_time; -#endif #ifdef DEBUG syslog(LOG_DEBUG, "entering updatestat"); @@ -246,32 +209,17 @@ updatestat(int sig) for (i = 0; i < dk_ndrive && i < DK_NDRIVE; i++) stats_all.s1.dk_xfer[i] = cur.dk_xfer[i]; -#ifdef BSD for (i = 0; i < CPUSTATES; i++) stats_all.s1.cp_time[i] = cp_time[cp_xlat[i]]; -#else - if (kvm_read(kfd, (long)nl[X_HZ].n_value, (char *)&hz, sizeof hz) != - sizeof hz) { - syslog(LOG_ERR, "can't read hz from kmem"); - _exit(1); - } - if (kvm_read(kfd, (long)nl[X_CPTIME].n_value, - (char *)stats_all.s1.cp_time, sizeof (stats_all.s1.cp_time)) - != sizeof (stats_all.s1.cp_time)) { - syslog(LOG_ERR, "can't read cp_time from kmem"); - _exit(1); - } -#endif -#ifdef BSD (void)getloadavg(avrun, sizeof(avrun) / sizeof(avrun[0])); -#endif stats_all.s2.avenrun[0] = avrun[0] * FSCALE; stats_all.s2.avenrun[1] = avrun[1] * FSCALE; stats_all.s2.avenrun[2] = avrun[2] * FSCALE; - if (kvm_read(kfd, (long)nl[X_BOOTTIME].n_value, - (char *)&btm, sizeof (stats_all.s2.boottime)) != - sizeof (stats_all.s2.boottime)) { - syslog(LOG_ERR, "can't read boottime from kmem"); + mib[0] = CTL_KERN; + mib[1] = KERN_BOOTTIME; + len = sizeof(btm); + if (sysctl(mib, 2, &btm, &len, NULL, 0) < 0) { + syslog(LOG_ERR, "can't sysctl kern.boottime: %m"); _exit(1); } stats_all.s2.boottime.tv_sec = btm.tv_sec; @@ -288,7 +236,7 @@ updatestat(int sig) mib[1] = VM_UVMEXP; len = sizeof(uvmexp); if (sysctl(mib, 2, &uvmexp, &len, NULL, 0) < 0) { - syslog(LOG_ERR, "can't sysctl vm.uvmexp"); + syslog(LOG_ERR, "can't sysctl vm.uvmexp: %m"); _exit(1); } stats_all.s1.v_pgpgin = uvmexp.fltanget; @@ -300,34 +248,26 @@ updatestat(int sig) gettimeofday(&tm, (struct timezone *) 0); stats_all.s1.v_intr -= hz*(tm.tv_sec - btm.tv_sec) + hz*(tm.tv_usec - btm.tv_usec)/1000000; - -#ifndef BSD - if (kvm_read(kfd, (long)nl[X_DKXFER].n_value, - (char *)stats_all.s1.dk_xfer, sizeof (stats_all.s1.dk_xfer)) != - sizeof (stats_all.s1.dk_xfer)) { - syslog(LOG_ERR, "can't read dk_xfer from kmem"); - _exit(1); - } -#endif - stats_all.s1.if_ipackets = 0; stats_all.s1.if_opackets = 0; stats_all.s1.if_ierrors = 0; stats_all.s1.if_oerrors = 0; stats_all.s1.if_collisions = 0; - for (off = (long)ifnetq.tqh_first, i = 0; off && i < numintfs; i++) { - if (kvm_read(kfd, off, (char *)&ifnet, sizeof ifnet) != - sizeof ifnet) { - syslog(LOG_ERR, "can't read ifnet from kmem"); - _exit(1); - } - stats_all.s1.if_ipackets += ifnet.if_data.ifi_ipackets; - stats_all.s1.if_opackets += ifnet.if_data.ifi_opackets; - stats_all.s1.if_ierrors += ifnet.if_data.ifi_ierrors; - stats_all.s1.if_oerrors += ifnet.if_data.ifi_oerrors; - stats_all.s1.if_collisions += ifnet.if_data.ifi_collisions; - off = (long)ifnet.if_list.tqe_next; + if (getifaddrs(&ifaddrs) == -1) { + syslog(LOG_ERR, "can't getifaddrs: %m"); + _exit(1); + } + for (ifa = ifaddrs; ifa != NULL; ifa = ifa->ifa_next) { + if (ifa->ifa_addr->sa_family != AF_LINK) + continue; + ifdp = (struct if_data *)ifa->ifa_data; + stats_all.s1.if_ipackets += ifdp->ifi_ipackets; + stats_all.s1.if_opackets += ifdp->ifi_opackets; + stats_all.s1.if_ierrors += ifdp->ifi_ierrors; + stats_all.s1.if_oerrors += ifdp->ifi_oerrors; + stats_all.s1.if_collisions += ifdp->ifi_collisions; } + freeifaddrs(ifaddrs); gettimeofday((struct timeval *)&stats_all.s3.curtime, (struct timezone *) 0); alarm(1); @@ -337,69 +277,7 @@ updatestat(int sig) void setup() { - struct ifnet ifnet; - long off; - char errbuf[_POSIX2_LINE_MAX]; - - kfd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf); - if (kfd == NULL) { - syslog(LOG_ERR, "%s", errbuf); - exit (1); - } - - if (kvm_nlist(kfd, nl) != 0) { - syslog(LOG_ERR, "can't get namelist"); - exit (1); - } - - if (kvm_read(kfd, (long)nl[X_IFNET].n_value, &ifnetq, - sizeof ifnetq) != sizeof ifnetq) { - syslog(LOG_ERR, "can't read ifnet queue head from kmem"); - exit(1); - } - - numintfs = 0; - for (off = (long)ifnetq.tqh_first; off;) { - if (kvm_read(kfd, off, (char *)&ifnet, sizeof ifnet) != - sizeof ifnet) { - syslog(LOG_ERR, "can't read ifnet from kmem"); - exit(1); - } - numintfs++; - off = (long)ifnet.if_list.tqe_next; - } -#ifdef BSD dkinit(0); -#endif -} - -/* - * returns true if have a disk - */ -int -havedisk() -{ -#ifdef BSD - return dk_ndrive != 0; -#else - int i, cnt; - long xfer[DK_NDRIVE]; - - if (kvm_nlist(kfd, nl) != 0) { - syslog(LOG_ERR, "can't get namelist"); - exit (1); - } - - if (kvm_read(kfd, (long)nl[X_DKXFER].n_value, - (char *)xfer, sizeof xfer) != sizeof xfer) { - syslog(LOG_ERR, "can't read dk_xfer from kmem"); - exit(1); - } - cnt = 0; - for (i=0; i < DK_NDRIVE; i++) - cnt += xfer[i]; - return (cnt != 0); -#endif } void |