Home | History | Annotate | Line # | Download | only in rpc.rstatd
rstat_proc.c revision 1.23
      1 /*	$NetBSD: rstat_proc.c,v 1.23 1998/02/12 05:27:51 mrg Exp $	*/
      2 
      3 /*
      4  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
      5  * unrestricted use provided that this legend is included on all tape
      6  * media and as a part of the software program in whole or part.  Users
      7  * may copy or modify Sun RPC without charge, but are not authorized
      8  * to license or distribute it to anyone else except as part of a product or
      9  * program developed by the user.
     10  *
     11  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
     12  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
     13  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
     14  *
     15  * Sun RPC is provided with no support and without any obligation on the
     16  * part of Sun Microsystems, Inc. to assist in its use, correction,
     17  * modification or enhancement.
     18  *
     19  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
     20  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
     21  * OR ANY PART THEREOF.
     22  *
     23  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
     24  * or profits or other special, indirect and consequential damages, even if
     25  * Sun has been advised of the possibility of such damages.
     26  *
     27  * Sun Microsystems, Inc.
     28  * 2550 Garcia Avenue
     29  * Mountain View, California  94043
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 #ifndef lint
     34 #if 0
     35 static char sccsid[] = "from: @(#)rpc.rstatd.c 1.1 86/09/25 Copyr 1984 Sun Micro";
     36 static char sccsid[] = "from: @(#)rstat_proc.c	2.2 88/08/01 4.0 RPCSRC";
     37 #else
     38 __RCSID("$NetBSD: rstat_proc.c,v 1.23 1998/02/12 05:27:51 mrg Exp $");
     39 #endif
     40 #endif
     41 
     42 /*
     43  * rstat service:  built with rstat.x and derived from rpc.rstatd.c
     44  *
     45  * Copyright (c) 1984 by Sun Microsystems, Inc.
     46  */
     47 
     48 #include <sys/param.h>
     49 #include <sys/errno.h>
     50 #include <sys/socket.h>
     51 
     52 #include <stdio.h>
     53 #include <stdlib.h>
     54 #include <string.h>
     55 #include <signal.h>
     56 #include <fcntl.h>
     57 #include <kvm.h>
     58 #include <limits.h>
     59 #include <nlist.h>
     60 #include <syslog.h>
     61 #ifdef BSD
     62 #include <sys/sysctl.h>
     63 #if defined(UVM)
     64 #include <vm/vm.h>
     65 #include <uvm/uvm_extern.h>
     66 #else
     67 #include <sys/vmmeter.h>
     68 #endif
     69 #include <sys/dkstat.h>
     70 #include "dkstats.h"
     71 #else
     72 #include <sys/dk.h>
     73 #endif
     74 
     75 #include <net/if.h>
     76 
     77 /*
     78  * XXX
     79  *
     80  * this is a huge hack to stop `struct pmap' being
     81  * defined twice!
     82  */
     83 #define _RPC_PMAP_PROT_H_
     84 #include <rpc/rpc.h>
     85 
     86 #undef FSHIFT			 /* Use protocol's shift and scale values */
     87 #undef FSCALE
     88 #undef DK_NDRIVE
     89 #undef CPUSTATES
     90 #undef if_ipackets
     91 #undef if_ierrors
     92 #undef if_opackets
     93 #undef if_oerrors
     94 #undef if_collisions
     95 #include <rpcsvc/rstat.h>
     96 
     97 #ifdef BSD
     98 #define BSD_CPUSTATES	5	/* Use protocol's idea of CPU states */
     99 int	cp_xlat[CPUSTATES] = { CP_USER, CP_NICE, CP_SYS, CP_IDLE };
    100 #endif
    101 
    102 struct nlist nl[] = {
    103 #define	X_IFNET		0
    104 	{ "_ifnet" },
    105 #if !defined(UVM)
    106 #define	X_CNT		1
    107 	{ "_cnt" },
    108 #endif
    109 	{ NULL },
    110 };
    111 
    112 extern int dk_ndrive;		/* From dkstats.c */
    113 extern struct _disk cur, last;
    114 int hz;
    115 char *memf = NULL, *nlistf = NULL;
    116 
    117 struct ifnet_head ifnetq;	/* chain of ethernet interfaces */
    118 int numintfs;
    119 
    120 extern int from_inetd;
    121 int sincelastreq = 0;		/* number of alarms since last request */
    122 extern int closedown;
    123 kvm_t *kfd;
    124 
    125 union {
    126 	struct stats s1;
    127 	struct statsswtch s2;
    128 	struct statstime s3;
    129 } stats_all;
    130 
    131 extern void dkreadstats __P((void));
    132 extern int dkinit __P((int));
    133 
    134 void updatestat __P((int));
    135 void setup __P((void));
    136 void stat_init __P((void));
    137 int havedisk __P((void));
    138 void rstat_service __P((struct svc_req *, SVCXPRT *));
    139 
    140 static stat_is_init = 0;
    141 extern int errno;
    142 
    143 #ifndef FSCALE
    144 #define FSCALE (1 << 8)
    145 #endif
    146 
    147 void
    148 stat_init()
    149 {
    150 	stat_is_init = 1;
    151 	setup();
    152 	updatestat(0);
    153 	(void) signal(SIGALRM, updatestat);
    154 	alarm(1);
    155 }
    156 
    157 statstime *
    158 rstatproc_stats_3_svc(arg, rqstp)
    159 	void *arg;
    160 	struct svc_req *rqstp;
    161 {
    162 	if (!stat_is_init)
    163 	        stat_init();
    164 	sincelastreq = 0;
    165 	return (&stats_all.s3);
    166 }
    167 
    168 statsswtch *
    169 rstatproc_stats_2_svc(arg, rqstp)
    170 	void *arg;
    171 	struct svc_req *rqstp;
    172 {
    173 	if (!stat_is_init)
    174 	        stat_init();
    175 	sincelastreq = 0;
    176 	stats_all.s2.if_opackets = stats_all.s3.if_opackets;
    177 	return (&stats_all.s2);
    178 }
    179 
    180 stats *
    181 rstatproc_stats_1_svc(arg, rqstp)
    182 	void *arg;
    183 	struct svc_req *rqstp;
    184 {
    185 	if (!stat_is_init)
    186 	        stat_init();
    187 	sincelastreq = 0;
    188 	stats_all.s1.if_opackets = stats_all.s3.if_opackets;
    189 	return (&stats_all.s1);
    190 }
    191 
    192 u_int *
    193 rstatproc_havedisk_3_svc(arg, rqstp)
    194 	void *arg;
    195 	struct svc_req *rqstp;
    196 {
    197 	static u_int have;
    198 
    199 	if (!stat_is_init)
    200 	        stat_init();
    201 	sincelastreq = 0;
    202 	have = havedisk();
    203 	return (&have);
    204 }
    205 
    206 u_int *
    207 rstatproc_havedisk_2_svc(arg, rqstp)
    208 	void *arg;
    209 	struct svc_req *rqstp;
    210 {
    211 	return (rstatproc_havedisk_3_svc(arg, rqstp));
    212 }
    213 
    214 u_int *
    215 rstatproc_havedisk_1_svc(arg, rqstp)
    216 	void *arg;
    217 	struct svc_req *rqstp;
    218 {
    219 	return (rstatproc_havedisk_3_svc(arg, rqstp));
    220 }
    221 
    222 void
    223 updatestat(dummy)
    224 	int dummy;
    225 {
    226 	long off;
    227 	int i;
    228 	size_t len;
    229 	int mib[2];
    230 #if defined(UVM)
    231 	struct uvmexp uvmexp;
    232 #else
    233 	struct vmmeter cnt;
    234 #endif
    235 	struct ifnet ifnet;
    236 	double avrun[3];
    237 	struct timeval tm, btm;
    238 
    239 #ifdef DEBUG
    240 	syslog(LOG_DEBUG, "entering updatestat");
    241 #endif
    242 	if (sincelastreq >= closedown) {
    243 #ifdef DEBUG
    244                 syslog(LOG_DEBUG, "about to closedown");
    245 #endif
    246                 if (from_inetd)
    247                         exit(0);
    248                 else {
    249                         stat_is_init = 0;
    250                         return;
    251                 }
    252 	}
    253 	sincelastreq++;
    254 
    255 	/*
    256 	 * dkreadstats reads in the "disk_count" as well as the "disklist"
    257 	 * statistics.  It also retrieves "hz" and the "cp_time" array.
    258 	 */
    259 	dkreadstats();
    260 	memset(stats_all.s3.dk_xfer, 0, sizeof(stats_all.s3.dk_xfer));
    261 	for (i = 0; i < dk_ndrive && i < DK_NDRIVE; i++)
    262 		stats_all.s3.dk_xfer[i] = cur.dk_xfer[i];
    263 
    264 #ifdef BSD
    265 	for (i = 0; i < CPUSTATES; i++)
    266 		stats_all.s3.cp_time[i] = cur.cp_time[cp_xlat[i]];
    267 #else
    268  	if (kvm_read(kfd, (long)nl[X_CPTIME].n_value,
    269 		     (char *)stats_all.s3.cp_time,
    270 		     sizeof (stats_all.s3.cp_time))
    271 	    != sizeof (stats_all.s3.cp_time)) {
    272 		syslog(LOG_ERR, "can't read cp_time from kmem");
    273 		exit(1);
    274 	}
    275 #endif
    276 #ifdef BSD
    277         (void)getloadavg(avrun, sizeof(avrun) / sizeof(avrun[0]));
    278 #endif
    279 	stats_all.s3.avenrun[0] = avrun[0] * FSCALE;
    280 	stats_all.s3.avenrun[1] = avrun[1] * FSCALE;
    281 	stats_all.s3.avenrun[2] = avrun[2] * FSCALE;
    282 	mib[0] = CTL_KERN;
    283 	mib[1] = KERN_BOOTTIME;
    284 	len = sizeof(btm);
    285 	if (sysctl(mib, 2, &btm, &len, NULL, 0) < 0) {
    286 		syslog(LOG_ERR, "can't sysctl kern.boottime");
    287 		exit(1);
    288 	}
    289 	stats_all.s3.boottime.tv_sec = btm.tv_sec;
    290 	stats_all.s3.boottime.tv_usec = btm.tv_usec;
    291 
    292 
    293 #ifdef DEBUG
    294 	syslog(LOG_DEBUG, "%d %d %d %d\n", stats_all.s3.cp_time[0],
    295 	    stats_all.s3.cp_time[1], stats_all.s3.cp_time[2], stats_all.s3.cp_time[3]);
    296 #endif
    297 
    298 #if defined(UVM)
    299 	mib[0] = CTL_VM;
    300 	mib[1] = VM_UVMEXP;
    301 	len = sizeof(uvmexp);
    302 	if (sysctl(mib, 2, &uvmexp, &len, NULL, 0) < 0) {
    303 		syslog(LOG_ERR, "can't sysctl vm.uvmexp");
    304 		exit(1);
    305 	}
    306 	stats_all.s3.v_pgpgin = uvmexp.fltanget;
    307 	stats_all.s3.v_pgpgout = uvmexp.pdpageouts;
    308 	stats_all.s3.v_pswpin = uvmexp.swapins;
    309 	stats_all.s3.v_pswpout = uvmexp.swapouts;
    310 	stats_all.s3.v_intr = uvmexp.intrs;
    311 	stats_all.s3.v_swtch = uvmexp.swtch;
    312 #else
    313  	if (kvm_read(kfd, (long)nl[X_CNT].n_value, (char *)&cnt, sizeof cnt) !=
    314 	    sizeof cnt) {
    315 		syslog(LOG_ERR, "can't read cnt from kmem");
    316 		exit(1);
    317 	}
    318 	stats_all.s3.v_pgpgin = cnt.v_pgpgin;
    319 	stats_all.s3.v_pgpgout = cnt.v_pgpgout;
    320 	stats_all.s3.v_pswpin = cnt.v_pswpin;
    321 	stats_all.s3.v_pswpout = cnt.v_pswpout;
    322 	stats_all.s3.v_intr = cnt.v_intr;
    323 	stats_all.s3.v_swtch = cnt.v_swtch;
    324 #endif
    325 	gettimeofday(&tm, (struct timezone *) 0);
    326 	stats_all.s3.v_intr -= hz*(tm.tv_sec - btm.tv_sec) +
    327 	    hz*(tm.tv_usec - btm.tv_usec)/1000000;
    328 
    329 	stats_all.s3.if_ipackets = 0;
    330 	stats_all.s3.if_opackets = 0;
    331 	stats_all.s3.if_ierrors = 0;
    332 	stats_all.s3.if_oerrors = 0;
    333 	stats_all.s3.if_collisions = 0;
    334 	for (off = (long)ifnetq.tqh_first, i = 0; off && i < numintfs; i++) {
    335 		if (kvm_read(kfd, off, (char *)&ifnet, sizeof ifnet) !=
    336 		    sizeof ifnet) {
    337 			syslog(LOG_ERR, "can't read ifnet from kmem");
    338 			exit(1);
    339 		}
    340 		stats_all.s3.if_ipackets += ifnet.if_data.ifi_ipackets;
    341 		stats_all.s3.if_opackets += ifnet.if_data.ifi_opackets;
    342 		stats_all.s3.if_ierrors += ifnet.if_data.ifi_ierrors;
    343 		stats_all.s3.if_oerrors += ifnet.if_data.ifi_oerrors;
    344 		stats_all.s3.if_collisions += ifnet.if_data.ifi_collisions;
    345 		off = (long)ifnet.if_list.tqe_next;
    346 	}
    347 	gettimeofday((struct timeval *)&stats_all.s3.curtime,
    348 		(struct timezone *) 0);
    349 	alarm(1);
    350 }
    351 
    352 void
    353 setup()
    354 {
    355 	struct ifnet ifnet;
    356 	long off;
    357 	char errbuf[_POSIX2_LINE_MAX];
    358 
    359 	kfd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf);
    360 	if (kfd == NULL) {
    361 		syslog(LOG_ERR, "%s", errbuf);
    362 		exit (1);
    363 	}
    364 
    365 	if (kvm_nlist(kfd, nl) != 0) {
    366 		syslog(LOG_ERR, "can't get namelist");
    367 		exit (1);
    368         }
    369 
    370 	if (kvm_read(kfd, (long)nl[X_IFNET].n_value, &ifnetq,
    371                      sizeof ifnetq) != sizeof ifnetq)  {
    372 		syslog(LOG_ERR, "can't read ifnet queue head from kmem");
    373 		exit(1);
    374         }
    375 
    376 	numintfs = 0;
    377 	for (off = (long)ifnetq.tqh_first; off;) {
    378 		if (kvm_read(kfd, off, (char *)&ifnet, sizeof ifnet) !=
    379 		    sizeof ifnet) {
    380 			syslog(LOG_ERR, "can't read ifnet from kmem");
    381 			exit(1);
    382 		}
    383 		numintfs++;
    384 		off = (long)ifnet.if_list.tqe_next;
    385 	}
    386 	dkinit(0);
    387 }
    388 
    389 /*
    390  * returns true if have a disk
    391  */
    392 int
    393 havedisk()
    394 {
    395 	return dk_ndrive != 0;
    396 }
    397 
    398 void
    399 rstat_service(rqstp, transp)
    400 	struct svc_req *rqstp;
    401 	SVCXPRT *transp;
    402 {
    403 	union {
    404 		int fill;
    405 	} argument;
    406 	char *result;
    407 	xdrproc_t xdr_argument, xdr_result;
    408 	char *(*local) __P((void *, struct svc_req *));
    409 
    410 	switch (rqstp->rq_proc) {
    411 	case NULLPROC:
    412 		(void)svc_sendreply(transp, xdr_void, (char *)NULL);
    413 		goto leave;
    414 
    415 	case RSTATPROC_STATS:
    416 		xdr_argument = (xdrproc_t)xdr_void;
    417 		xdr_result = (xdrproc_t)xdr_statstime;
    418                 switch (rqstp->rq_vers) {
    419                 case RSTATVERS_ORIG:
    420                         local = (char *(*) __P((void *, struct svc_req *)))
    421 				rstatproc_stats_1_svc;
    422                         break;
    423                 case RSTATVERS_SWTCH:
    424                         local = (char *(*) __P((void *, struct svc_req *)))
    425 				rstatproc_stats_2_svc;
    426                         break;
    427                 case RSTATVERS_TIME:
    428                         local = (char *(*) __P((void *, struct svc_req *)))
    429 				rstatproc_stats_3_svc;
    430                         break;
    431                 default:
    432                         svcerr_progvers(transp, RSTATVERS_ORIG, RSTATVERS_TIME);
    433                         goto leave;
    434                 }
    435 		break;
    436 
    437 	case RSTATPROC_HAVEDISK:
    438 		xdr_argument = (xdrproc_t)xdr_void;
    439 		xdr_result = (xdrproc_t)xdr_u_int;
    440                 switch (rqstp->rq_vers) {
    441                 case RSTATVERS_ORIG:
    442                         local = (char *(*) __P((void *, struct svc_req *)))
    443 				rstatproc_havedisk_1_svc;
    444                         break;
    445                 case RSTATVERS_SWTCH:
    446                         local = (char *(*) __P((void *, struct svc_req *)))
    447 				rstatproc_havedisk_2_svc;
    448                         break;
    449                 case RSTATVERS_TIME:
    450                         local = (char *(*) __P((void *, struct svc_req *)))
    451 				rstatproc_havedisk_3_svc;
    452                         break;
    453                 default:
    454                         svcerr_progvers(transp, RSTATVERS_ORIG, RSTATVERS_TIME);
    455                         goto leave;
    456                 }
    457 		break;
    458 
    459 	default:
    460 		svcerr_noproc(transp);
    461 		goto leave;
    462 	}
    463 	bzero((char *)&argument, sizeof(argument));
    464 	if (!svc_getargs(transp, xdr_argument, (caddr_t)&argument)) {
    465 		svcerr_decode(transp);
    466 		goto leave;
    467 	}
    468 	result = (*local)(&argument, rqstp);
    469 	if (result != NULL && !svc_sendreply(transp, xdr_result, result)) {
    470 		svcerr_systemerr(transp);
    471 	}
    472 	if (!svc_freeargs(transp, xdr_argument, (caddr_t)&argument)) {
    473 		(void)fprintf(stderr, "unable to free arguments\n");
    474 		exit(1);
    475 	}
    476 leave:
    477         if (from_inetd)
    478                 exit(0);
    479 }
    480