Home | History | Annotate | Line # | Download | only in rpc.rstatd
      1 /*	$NetBSD: rstat_proc.c,v 1.53 2018/10/30 21:18:39 kre 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 #if 0
     34 static char sccsid[] =
     35 	"from: @(#)rpc.rstatd.c 1.1 86/09/25 Copyr 1984 Sun Micro";
     36 static char sccsid[] =
     37 	"from: @(#)rstat_proc.c	2.2 88/08/01 4.0 RPCSRC";
     38 #endif
     39 __RCSID("$NetBSD: rstat_proc.c,v 1.53 2018/10/30 21:18:39 kre Exp $");
     40 
     41 /*
     42  * rstat service:  built with rstat.x and derived from rpc.rstatd.c
     43  *
     44  * Copyright (c) 1984 by Sun Microsystems, Inc.
     45  */
     46 
     47 #include <sys/param.h>
     48 #include <sys/sched.h>
     49 #include <sys/socket.h>
     50 #include <sys/ioctl.h>
     51 
     52 #include <errno.h>
     53 #include <stdio.h>
     54 #include <stdlib.h>
     55 #include <string.h>
     56 #include <signal.h>
     57 #include <fcntl.h>
     58 #include <limits.h>
     59 #include <syslog.h>
     60 #include <sys/sysctl.h>
     61 #include <uvm/uvm_extern.h>
     62 #include "drvstats.h"
     63 #include <net/if.h>
     64 
     65 /*
     66  * XXX
     67  *
     68  * this is a huge hack to stop `struct pmap' being
     69  * defined twice!
     70  */
     71 #define _RPC_PMAP_PROT_H_
     72 #include <rpc/rpc.h>
     73 
     74 #undef FSHIFT			 /* Use protocol's shift and scale values */
     75 #undef FSCALE
     76 #undef DK_NDRIVE
     77 #undef CPUSTATES
     78 #undef if_ipackets
     79 #undef if_ierrors
     80 #undef if_opackets
     81 #undef if_oerrors
     82 #undef if_collisions
     83 #include <rpcsvc/rstat.h>
     84 
     85 #define BSD_CPUSTATES	5	/* Use protocol's idea of CPU states */
     86 int	cp_xlat[CPUSTATES] = { CP_USER, CP_NICE, CP_SYS, CP_IDLE };
     87 
     88 int hz;
     89 
     90 extern int from_inetd;
     91 int sincelastreq = 0;		/* number of alarms since last request */
     92 extern int closedown;
     93 
     94 union {
     95 	struct stats s1;
     96 	struct statsswtch s2;
     97 	struct statstime s3;
     98 } stats_all;
     99 
    100 void updatestat(int);
    101 void stat_init(void);
    102 int havedisk(void);
    103 void rstat_service(struct svc_req *, SVCXPRT *);
    104 
    105 static int stat_is_init = 0;
    106 
    107 #ifndef FSCALE
    108 #define FSCALE (1 << 8)
    109 #endif
    110 
    111 void
    112 stat_init(void)
    113 {
    114 	stat_is_init = 1;
    115 	drvinit(0);
    116 	updatestat(0);
    117 	(void) signal(SIGALRM, updatestat);
    118 	alarm(1);
    119 }
    120 
    121 statstime *
    122 rstatproc_stats_3_svc(void *arg, struct svc_req *rqstp)
    123 {
    124 	if (!stat_is_init)
    125 	        stat_init();
    126 	sincelastreq = 0;
    127 	return (&stats_all.s3);
    128 }
    129 
    130 statsswtch *
    131 rstatproc_stats_2_svc(void *arg, struct svc_req *rqstp)
    132 {
    133 	if (!stat_is_init)
    134 	        stat_init();
    135 	sincelastreq = 0;
    136 	stats_all.s2.if_opackets = stats_all.s3.if_opackets;
    137 	return (&stats_all.s2);
    138 }
    139 
    140 stats *
    141 rstatproc_stats_1_svc(void *arg, struct svc_req *rqstp)
    142 {
    143 	if (!stat_is_init)
    144 	        stat_init();
    145 	sincelastreq = 0;
    146 	stats_all.s1.if_opackets = stats_all.s3.if_opackets;
    147 	return (&stats_all.s1);
    148 }
    149 
    150 u_int *
    151 rstatproc_havedisk_3_svc(void *arg, struct svc_req *rqstp)
    152 {
    153 	static u_int have;
    154 
    155 	if (!stat_is_init)
    156 	        stat_init();
    157 	sincelastreq = 0;
    158 	have = havedisk();
    159 	return (&have);
    160 }
    161 
    162 u_int *
    163 rstatproc_havedisk_2_svc(void *arg, struct svc_req *rqstp)
    164 {
    165 	return (rstatproc_havedisk_3_svc(arg, rqstp));
    166 }
    167 
    168 u_int *
    169 rstatproc_havedisk_1_svc(void *arg, struct svc_req *rqstp)
    170 {
    171 	return (rstatproc_havedisk_3_svc(arg, rqstp));
    172 }
    173 
    174 void
    175 updatestat(int dummy)
    176 {
    177 	struct if_nameindex *ifps;
    178 	struct ifdatareq ifdr;
    179 	size_t i, len;
    180 	int mib[2], s;
    181 	struct uvmexp_sysctl uvmexp;
    182 	double avrun[3];
    183 	struct timeval tm;
    184 	struct timespec btm;
    185 
    186 #ifdef DEBUG
    187 	syslog(LOG_DEBUG, "entering updatestat");
    188 #endif
    189 	if (sincelastreq >= closedown) {
    190 #ifdef DEBUG
    191                 syslog(LOG_DEBUG, "about to closedown");
    192 #endif
    193                 if (from_inetd)
    194                         exit(0);
    195                 else {
    196                         stat_is_init = 0;
    197                         return;
    198                 }
    199 	}
    200 	sincelastreq++;
    201 
    202 	/*
    203 	 * drvreadstats reads in the "disk_count" as well as the "disklist"
    204 	 * statistics.  It also retrieves "hz" and the "cp_time" array.
    205 	 */
    206 	drvreadstats();
    207 	memset(stats_all.s3.dk_xfer, 0, sizeof(stats_all.s3.dk_xfer));
    208 	for (i = 0; i < ndrive && i < DK_NDRIVE; i++)
    209 		stats_all.s3.dk_xfer[i] = cur.rxfer[i] + cur.wxfer[i];
    210 
    211 	for (i = 0; i < CPUSTATES; i++)
    212 		stats_all.s3.cp_time[i] = cur.cp_time[cp_xlat[i]];
    213         (void)getloadavg(avrun, sizeof(avrun) / sizeof(avrun[0]));
    214 	stats_all.s3.avenrun[0] = avrun[0] * FSCALE;
    215 	stats_all.s3.avenrun[1] = avrun[1] * FSCALE;
    216 	stats_all.s3.avenrun[2] = avrun[2] * FSCALE;
    217 	mib[0] = CTL_KERN;
    218 	mib[1] = KERN_BOOTTIME;
    219 	len = sizeof(btm);
    220 	if (sysctl(mib, 2, &btm, &len, NULL, 0) < 0) {
    221 		syslog(LOG_ERR, "can't sysctl kern.boottime");
    222 		exit(1);
    223 	}
    224 	stats_all.s3.boottime.tv_sec = btm.tv_sec;
    225 	stats_all.s3.boottime.tv_usec = (suseconds_t)(btm.tv_nsec / 1000L);
    226 
    227 
    228 #ifdef DEBUG
    229 	syslog(LOG_DEBUG, "%d %d %d %d %d\n", stats_all.s3.cp_time[0],
    230 	    stats_all.s3.cp_time[1], stats_all.s3.cp_time[2],
    231 	    stats_all.s3.cp_time[3], stats_all.s3.cp_time[4]);
    232 #endif
    233 
    234 	mib[0] = CTL_VM;
    235 	mib[1] = VM_UVMEXP2;
    236 	len = sizeof(uvmexp);
    237 	if (sysctl(mib, 2, &uvmexp, &len, NULL, 0) < 0) {
    238 		syslog(LOG_ERR, "can't sysctl vm.uvmexp2");
    239 		exit(1);
    240 	}
    241 	stats_all.s3.v_pgpgin = uvmexp.fltanget;
    242 	stats_all.s3.v_pgpgout = uvmexp.pdpageouts;
    243 	stats_all.s3.v_pswpin = uvmexp.swapins;
    244 	stats_all.s3.v_pswpout = uvmexp.swapouts;
    245 	stats_all.s3.v_intr = uvmexp.intrs;
    246 	stats_all.s3.v_swtch = uvmexp.swtch;
    247 	gettimeofday(&tm, (struct timezone *) 0);
    248 	stats_all.s3.v_intr -= hz*(tm.tv_sec - btm.tv_sec) +
    249 	    hz*(tm.tv_usec - (suseconds_t)(btm.tv_nsec / 1000))/1000000;
    250 
    251 	stats_all.s3.if_ipackets = 0;
    252 	stats_all.s3.if_opackets = 0;
    253 	stats_all.s3.if_ierrors = 0;
    254 	stats_all.s3.if_oerrors = 0;
    255 	stats_all.s3.if_collisions = 0;
    256 
    257 	ifps = if_nameindex();
    258 	if (ifps == NULL) {
    259 		syslog(LOG_ERR, "can't read interface list from kernel");
    260 		exit(1);
    261 	}
    262 	s = socket(AF_INET, SOCK_DGRAM, 0);
    263 	for (i = 0; s != -1 && ifps[i].if_name != NULL; ++i) {
    264 		strlcpy(ifdr.ifdr_name, ifps[i].if_name, sizeof(ifdr.ifdr_name));
    265 		if (ioctl(s, SIOCGIFDATA, &ifdr) != 0)
    266 			continue;
    267 		stats_all.s3.if_ipackets += ifdr.ifdr_data.ifi_ipackets;
    268 		stats_all.s3.if_opackets += ifdr.ifdr_data.ifi_opackets;
    269 		stats_all.s3.if_ierrors += ifdr.ifdr_data.ifi_ierrors;
    270 		stats_all.s3.if_oerrors += ifdr.ifdr_data.ifi_oerrors;
    271 		stats_all.s3.if_collisions += ifdr.ifdr_data.ifi_collisions;
    272 	}
    273 	if (s != -1)
    274 		close(s);
    275 	if_freenameindex(ifps);
    276 
    277 	stats_all.s3.curtime.tv_sec = tm.tv_sec;
    278 	stats_all.s3.curtime.tv_usec = tm.tv_usec;
    279 	alarm(1);
    280 }
    281 
    282 /*
    283  * returns true if have a disk
    284  */
    285 int
    286 havedisk(void)
    287 {
    288 	return ndrive != 0;
    289 }
    290 
    291 void
    292 rstat_service(struct svc_req *rqstp, SVCXPRT *transp)
    293 {
    294 	union {
    295 		int fill;
    296 	} argument;
    297 	char *result;
    298 	xdrproc_t xdr_argument, xdr_result;
    299 	char *(*local)(void *, struct svc_req *);
    300 
    301 	switch (rqstp->rq_proc) {
    302 	case NULLPROC:
    303 		(void)svc_sendreply(transp, (xdrproc_t)xdr_void, NULL);
    304 		goto leave;
    305 
    306 	case RSTATPROC_STATS:
    307 		xdr_argument = (xdrproc_t)xdr_void;
    308 		xdr_result = (xdrproc_t)xdr_statstime;
    309                 switch (rqstp->rq_vers) {
    310                 case RSTATVERS_ORIG:
    311                         local = (char *(*)(void *, struct svc_req *))
    312 				rstatproc_stats_1_svc;
    313                         break;
    314                 case RSTATVERS_SWTCH:
    315                         local = (char *(*)(void *, struct svc_req *))
    316 				rstatproc_stats_2_svc;
    317                         break;
    318                 case RSTATVERS_TIME:
    319                         local = (char *(*)(void *, struct svc_req *))
    320 				rstatproc_stats_3_svc;
    321                         break;
    322                 default:
    323                         svcerr_progvers(transp, RSTATVERS_ORIG, RSTATVERS_TIME);
    324                         goto leave;
    325                 }
    326 		break;
    327 
    328 	case RSTATPROC_HAVEDISK:
    329 		xdr_argument = (xdrproc_t)xdr_void;
    330 		xdr_result = (xdrproc_t)xdr_u_int;
    331                 switch (rqstp->rq_vers) {
    332                 case RSTATVERS_ORIG:
    333                         local = (char *(*)(void *, struct svc_req *))
    334 				rstatproc_havedisk_1_svc;
    335                         break;
    336                 case RSTATVERS_SWTCH:
    337                         local = (char *(*)(void *, struct svc_req *))
    338 				rstatproc_havedisk_2_svc;
    339                         break;
    340                 case RSTATVERS_TIME:
    341                         local = (char *(*)(void *, struct svc_req *))
    342 				rstatproc_havedisk_3_svc;
    343                         break;
    344                 default:
    345                         svcerr_progvers(transp, RSTATVERS_ORIG, RSTATVERS_TIME);
    346                         goto leave;
    347                 }
    348 		break;
    349 
    350 	default:
    351 		svcerr_noproc(transp);
    352 		goto leave;
    353 	}
    354 	memset((char *)&argument, 0, sizeof(argument));
    355 	if (!svc_getargs(transp, xdr_argument, (caddr_t)&argument)) {
    356 		svcerr_decode(transp);
    357 		goto leave;
    358 	}
    359 	result = (*local)(&argument, rqstp);
    360 	if (result != NULL && !svc_sendreply(transp, xdr_result, result)) {
    361 		svcerr_systemerr(transp);
    362 	}
    363 	if (!svc_freeargs(transp, xdr_argument, (caddr_t)&argument)) {
    364 		(void)fprintf(stderr, "unable to free arguments\n");
    365 		exit(1);
    366 	}
    367 leave:
    368         if (from_inetd)
    369                 exit(0);
    370 }
    371