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