From 0c0b205ceb318f5c0ff32a57f78b512809ad7eef Mon Sep 17 00:00:00 2001 From: Geoff Rehmet Date: Sun, 28 Aug 1994 14:49:04 +0000 Subject: [PATCH] rstatd -- from FreeBSD-1.1.5.1, with changes for 4.4-Lite kvm interface. Original rstatd by John Brezak Submitted by: Geoff. --- libexec/rpc.rstatd/Makefile | 10 + libexec/rpc.rstatd/rpc.rstatd.8 | 61 +++++ libexec/rpc.rstatd/rstat_proc.c | 459 ++++++++++++++++++++++++++++++++ libexec/rpc.rstatd/rstatd.c | 117 ++++++++ 4 files changed, 647 insertions(+) create mode 100644 libexec/rpc.rstatd/Makefile create mode 100644 libexec/rpc.rstatd/rpc.rstatd.8 create mode 100644 libexec/rpc.rstatd/rstat_proc.c create mode 100644 libexec/rpc.rstatd/rstatd.c diff --git a/libexec/rpc.rstatd/Makefile b/libexec/rpc.rstatd/Makefile new file mode 100644 index 000000000000..312f7cf7c3fe --- /dev/null +++ b/libexec/rpc.rstatd/Makefile @@ -0,0 +1,10 @@ +# $Id: Makefile,v 1.2 1993/11/10 03:45:40 smace Exp $ + +PROG = rpc.rstatd +SRCS = rstatd.c rstat_proc.c +MAN8 = rpc.rstatd.8 + +DPADD= ${LIBRPCSVC} ${LIBUTIL} ${LIBKVM} +LDADD= -lrpcsvc -lutil -lkvm + +.include diff --git a/libexec/rpc.rstatd/rpc.rstatd.8 b/libexec/rpc.rstatd/rpc.rstatd.8 new file mode 100644 index 000000000000..0644bbbf7f0d --- /dev/null +++ b/libexec/rpc.rstatd/rpc.rstatd.8 @@ -0,0 +1,61 @@ +.\" -*- nroff -*- +.\" +.\" Copyright (c) 1985, 1991 The Regents of the University of California. +.\" All rights reserved. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" 3. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND +.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE +.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +.\" SUCH DAMAGE. +.\" +.\" $Id: rpc.rstatd.8,v 1.1 1993/09/16 00:27:45 jtc Exp $ +.\" +.Dd June 7, 1993 +.Dt RPC.RSTATD 8 +.Os BSD 4.3 +.Sh NAME +.Nm rpc.rstatd +.Nd kernel statistics server +.Sh SYNOPSIS +.Nm /usr/libexec/rpc.rstatd +.Sh DESCRIPTION +.Nm rpc.rstatd +is a server which returns performance statistics obtained from the kernel. +These statistics are read using the +.Xr rup 1 +command. +The +.Nm rpc.rstatd +daemon is normally invoked by +.Xr inetd 8 . +.Pp +.Nm rpc.rstatd +uses an RPC protocol defined in +.Pa /usr/include/rpcsvc/rstat.x . +.Sh SEE ALSO +.Xr rup 1 , +.Xr inetd 8 + diff --git a/libexec/rpc.rstatd/rstat_proc.c b/libexec/rpc.rstatd/rstat_proc.c new file mode 100644 index 000000000000..51de3869f048 --- /dev/null +++ b/libexec/rpc.rstatd/rstat_proc.c @@ -0,0 +1,459 @@ +/* + * Sun RPC is a product of Sun Microsystems, Inc. and is provided for + * unrestricted use provided that this legend is included on all tape + * media and as a part of the software program in whole or part. Users + * may copy or modify Sun RPC without charge, but are not authorized + * to license or distribute it to anyone else except as part of a product or + * program developed by the user. + * + * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE + * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR + * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE. + * + * Sun RPC is provided with no support and without any obligation on the + * part of Sun Microsystems, Inc. to assist in its use, correction, + * modification or enhancement. + * + * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE + * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC + * OR ANY PART THEREOF. + * + * In no event will Sun Microsystems, Inc. be liable for any lost revenue + * or profits or other special, indirect and consequential damages, even if + * Sun has been advised of the possibility of such damages. + * + * Sun Microsystems, Inc. + * 2550 Garcia Avenue + * Mountain View, California 94043 + */ +#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[] = "$Id: rstat_proc.c,v 1.2 1993/09/23 18:48:55 jtc Exp $"; +#endif + +/* + * rstat service: built with rstat.x and derived from rpc.rstatd.c + * + * Copyright (c) 1984 by Sun Microsystems, Inc. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#ifdef BSD +#include +#include +#else +#include +#endif +#include + +#undef FSHIFT /* Use protocol's shift and scale values */ +#undef FSCALE +#undef if_ipackets +#undef if_ierrors +#undef if_opackets +#undef if_oerrors +#undef if_collisions +#include + +struct nlist nl[] = { +#define X_CPTIME 0 + { "_cp_time" }, +#define X_CNT 1 + { "_cnt" }, +#define X_IFNET 2 + { "_ifnet" }, +#define X_DKXFER 3 + { "_dk_xfer" }, +#define X_BOOTTIME 4 + { "_boottime" }, +#define X_HZ 5 + { "_hz" }, +#ifdef vax +#define X_AVENRUN 6 + { "_avenrun" }, +#endif + "", +}; +int firstifnet, numintfs; /* chain of ethernet interfaces */ +int stats_service(); + +extern int from_inetd; +int sincelastreq = 0; /* number of alarms since last request */ +extern int closedown; + +union { + struct stats s1; + struct statsswtch s2; + struct statstime s3; +} stats_all; + +void updatestat(); +static stat_is_init = 0; +static kvm_t *kd; +extern int errno; + +#if defined(BSD) +static int cp_time_xlat[RSTAT_CPUSTATES] = { CP_USER, CP_NICE, CP_SYS, + CP_IDLE }; +static long bsd_cp_time[CPUSTATES]; +#endif + + +#ifndef FSCALE +#define FSCALE (1 << 8) +#endif + +#ifndef BSD +/* + * BSD has the kvm facility for getting info from the + * kernel. If you aren't on BSD, this surfices. + */ +int kmem; + +kvm_read(kd, off, addr, size) + void * kd; + unsigned long off, size; + char *addr; +{ + int len; + if (lseek(kmem, (long)off, 0) == -1) + return(-1); + return(read(kmem, addr, size)); +} + +kvm_nlist(kd, nl) + void * kd; + struct nlist *nl; +{ + int n = nlist("/vmunix", nl); + if (nl[0].n_value == 0) + return(n); + + if ((kmem = open("/dev/kmem", 0)) < 0) + return(-1); + return(0); +} +#endif + +stat_init() +{ + stat_is_init = 1; + setup(); + updatestat(); + (void) signal(SIGALRM, updatestat); + alarm(1); +} + +statstime * +rstatproc_stats_3() +{ + if (! stat_is_init) + stat_init(); + sincelastreq = 0; + return(&stats_all.s3); +} + +statsswtch * +rstatproc_stats_2() +{ + if (! stat_is_init) + stat_init(); + sincelastreq = 0; + return(&stats_all.s2); +} + +stats * +rstatproc_stats_1() +{ + if (! stat_is_init) + stat_init(); + sincelastreq = 0; + return(&stats_all.s1); +} + +u_int * +rstatproc_havedisk_3() +{ + static u_int have; + + if (! stat_is_init) + stat_init(); + sincelastreq = 0; + have = havedisk(); + return(&have); +} + +u_int * +rstatproc_havedisk_2() +{ + return(rstatproc_havedisk_3()); +} + +u_int * +rstatproc_havedisk_1() +{ + return(rstatproc_havedisk_3()); +} + +void +updatestat() +{ + int off, i, hz; + struct vmmeter cnt; + struct ifnet ifnet; + double avrun[3]; + struct timeval tm, btm; + +#ifdef DEBUG + fprintf(stderr, "entering updatestat\n"); +#endif + if (sincelastreq >= closedown) { +#ifdef DEBUG + fprintf(stderr, "about to closedown\n"); +#endif + if (from_inetd) + exit(0); + else { + stat_is_init = 0; + return; + } + } + sincelastreq++; + + if (kvm_read(kd, (long)nl[X_HZ].n_value, (char *)&hz, sizeof hz) != sizeof hz) { + syslog(LOG_ERR, "rstat: can't read hz from kmem\n"); + exit(1); + } +#if defined(BSD) + if (kvm_read(kd, (long)nl[X_CPTIME].n_value, (char *)bsd_cp_time, sizeof(bsd_cp_time)) + != sizeof(bsd_cp_time)) { + syslog(LOG_ERR, "rstat: can't read cp_time from kmem\n"); + exit(1); + } + for(i = 0; i < RSTAT_CPUSTATES ; i++) + stats_all.s1.cp_time[i] = bsd_cp_time[cp_time_xlat[i]]; +#else + if (kvm_read(kd, (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, "rstat: can't read cp_time from kmem\n"); + exit(1); + } +#endif +#ifdef vax + if (kvm_read(kd, (long)nl[X_AVENRUN].n_value, (char *)avrun, sizeof (avrun)) != sizeof (avrun)) { + syslog(LOG_ERR, "rstat: can't read avenrun from kmem\n"); + 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(kd, (long)nl[X_BOOTTIME].n_value, (char *)&btm, sizeof (stats_all.s2.boottime)) + != sizeof (stats_all.s2.boottime)) { + syslog(LOG_ERR, "rstat: can't read boottime from kmem\n"); + exit(1); + } + stats_all.s2.boottime.tv_sec = btm.tv_sec; + stats_all.s2.boottime.tv_usec = btm.tv_usec; + + +#ifdef DEBUG + fprintf(stderr, "%d %d %d %d\n", stats_all.s1.cp_time[0], + stats_all.s1.cp_time[1], stats_all.s1.cp_time[2], stats_all.s1.cp_time[3]); +#endif + + if (kvm_read(kd, (long)nl[X_CNT].n_value, (char *)&cnt, sizeof cnt) != sizeof cnt) { + syslog(LOG_ERR, "rstat: can't read cnt from kmem\n"); + exit(1); + } + stats_all.s1.v_pgpgin = cnt.v_pgpgin; + stats_all.s1.v_pgpgout = cnt.v_pgpgout; + stats_all.s1.v_pswpin = cnt.v_pswpin; + stats_all.s1.v_pswpout = cnt.v_pswpout; + stats_all.s1.v_intr = cnt.v_intr; + 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; + stats_all.s2.v_swtch = cnt.v_swtch; + + if (kvm_read(kd, (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, "rstat: can't read dk_xfer from kmem\n"); + exit(1); + } + + 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 = firstifnet, i = 0; off && i < numintfs; i++) { + if (kvm_read(kd, off, (char *)&ifnet, sizeof ifnet) != sizeof ifnet) { + syslog(LOG_ERR, "rstat: can't read ifnet from kmem\n"); + 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 = (int) ifnet.if_next; + } + gettimeofday((struct timeval *)&stats_all.s3.curtime, + (struct timezone *) 0); + alarm(1); +} + +setup() +{ + struct ifnet ifnet; + int off; + char errbuf[_POSIX2_LINE_MAX]; + + int en; + + if ((kd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf)) == NULL) { + syslog(LOG_ERR, "rpc.rstatd, %s", errbuf); + exit(1); + } + + if ((en = kvm_nlist(kd, nl)) != 0) { + syslog(LOG_ERR, "rstatd: Can't get namelist. %d", en); + exit (1); + } + + if (kvm_read(kd, (long)nl[X_IFNET].n_value, &firstifnet, + sizeof(int)) != sizeof(int)) { + syslog(LOG_ERR, "rstat: can't read firstifnet from kmem\n"); + exit(1); + } + + numintfs = 0; + for (off = firstifnet; off;) { + if (kvm_read(kd, off, (char *)&ifnet, sizeof ifnet) != sizeof ifnet) { + syslog(LOG_ERR, "rstat: can't read ifnet from kmem\n"); + exit(1); + } + numintfs++; + off = (int) ifnet.if_next; + } +} + +/* + * returns true if have a disk + */ +havedisk() +{ + int i, cnt; + long xfer[DK_NDRIVE]; + + if (kvm_nlist(kd, nl) != 0) { + syslog(LOG_ERR, "rstatd: Can't get namelist.(d)"); + exit (1); + } + + if (kvm_read(kd, (long)nl[X_DKXFER].n_value, (char *)xfer, sizeof xfer)!= sizeof xfer) { + syslog(LOG_ERR, "rstat: can't read kmem\n"); + exit(1); + } + cnt = 0; + for (i=0; i < DK_NDRIVE; i++) + cnt += xfer[i]; + return (cnt != 0); +} + +void +rstat_service(rqstp, transp) + struct svc_req *rqstp; + SVCXPRT *transp; +{ + union { + int fill; + } argument; + char *result; + bool_t (*xdr_argument)(), (*xdr_result)(); + char *(*local)(); + + switch (rqstp->rq_proc) { + case NULLPROC: + (void)svc_sendreply(transp, xdr_void, (char *)NULL); + goto leave; + + case RSTATPROC_STATS: + xdr_argument = xdr_void; + xdr_result = xdr_statstime; + switch (rqstp->rq_vers) { + case RSTATVERS_ORIG: + local = (char *(*)()) rstatproc_stats_1; + break; + case RSTATVERS_SWTCH: + local = (char *(*)()) rstatproc_stats_2; + break; + case RSTATVERS_TIME: + local = (char *(*)()) rstatproc_stats_3; + break; + default: + svcerr_progvers(transp, RSTATVERS_ORIG, RSTATVERS_TIME); + goto leave; + /*NOTREACHED*/ + } + break; + + case RSTATPROC_HAVEDISK: + xdr_argument = xdr_void; + xdr_result = xdr_u_int; + switch (rqstp->rq_vers) { + case RSTATVERS_ORIG: + local = (char *(*)()) rstatproc_havedisk_1; + break; + case RSTATVERS_SWTCH: + local = (char *(*)()) rstatproc_havedisk_2; + break; + case RSTATVERS_TIME: + local = (char *(*)()) rstatproc_havedisk_3; + break; + default: + svcerr_progvers(transp, RSTATVERS_ORIG, RSTATVERS_TIME); + goto leave; + /*NOTREACHED*/ + } + break; + + default: + svcerr_noproc(transp); + goto leave; + } + bzero((char *)&argument, sizeof(argument)); + if (!svc_getargs(transp, xdr_argument, &argument)) { + svcerr_decode(transp); + goto leave; + } + result = (*local)(&argument, rqstp); + if (result != NULL && !svc_sendreply(transp, xdr_result, result)) { + svcerr_systemerr(transp); + } + if (!svc_freeargs(transp, xdr_argument, &argument)) { + (void)fprintf(stderr, "unable to free arguments\n"); + exit(1); + } +leave: + if (from_inetd) + exit(0); +} diff --git a/libexec/rpc.rstatd/rstatd.c b/libexec/rpc.rstatd/rstatd.c new file mode 100644 index 000000000000..369817151154 --- /dev/null +++ b/libexec/rpc.rstatd/rstatd.c @@ -0,0 +1,117 @@ +/*- + * Copyright (c) 1993, John Brezak + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef lint +static char rcsid[] = "$Id: rstatd.c,v 1.1 1993/09/16 00:27:49 jtc Exp $"; +#endif /* not lint */ + +#include +#include +#include +#include +#include + +extern void rstat_service(); + +int from_inetd = 1; /* started from inetd ? */ +int closedown = 20; /* how long to wait before going dormant */ + +void +cleanup() +{ + (void) pmap_unset(RSTATPROG, RSTATVERS_TIME); + (void) pmap_unset(RSTATPROG, RSTATVERS_SWTCH); + (void) pmap_unset(RSTATPROG, RSTATVERS_ORIG); + exit(0); +} + +main(argc, argv) + int argc; + char *argv[]; +{ + SVCXPRT *transp; + int sock = 0; + int proto = 0; + struct sockaddr_in from; + int fromlen; + + if (argc == 2) + closedown = atoi(argv[1]); + if (closedown <= 0) + closedown = 20; + + /* + * See if inetd started us + */ + if (getsockname(0, (struct sockaddr *)&from, &fromlen) < 0) { + from_inetd = 0; + sock = RPC_ANYSOCK; + proto = IPPROTO_UDP; + } + + if (!from_inetd) { + daemon(0, 0); + + (void)pmap_unset(RSTATPROG, RSTATVERS_TIME); + (void)pmap_unset(RSTATPROG, RSTATVERS_SWTCH); + (void)pmap_unset(RSTATPROG, RSTATVERS_ORIG); + + (void) signal(SIGINT, cleanup); + (void) signal(SIGTERM, cleanup); + (void) signal(SIGHUP, cleanup); + } + + openlog("rpc.rstatd", LOG_CONS|LOG_PID, LOG_DAEMON); + + transp = svcudp_create(sock); + if (transp == NULL) { + syslog(LOG_ERR, "cannot create udp service."); + exit(1); + } + if (!svc_register(transp, RSTATPROG, RSTATVERS_TIME, rstat_service, proto)) { + syslog(LOG_ERR, "unable to register (RSTATPROG, RSTATVERS_TIME, udp)."); + exit(1); + } + if (!svc_register(transp, RSTATPROG, RSTATVERS_SWTCH, rstat_service, proto)) { + syslog(LOG_ERR, "unable to register (RSTATPROG, RSTATVERS_SWTCH, udp)."); + exit(1); + } + if (!svc_register(transp, RSTATPROG, RSTATVERS_ORIG, rstat_service, proto)) { + syslog(LOG_ERR, "unable to register (RSTATPROG, RSTATVERS_ORIG, udp)."); + exit(1); + } + + svc_run(); + syslog(LOG_ERR, "svc_run returned"); + exit(1); +}