Home | History | Annotate | Line # | Download | only in rpc.rstatd
rstat_proc.c revision 1.13
      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.13 1995/06/24 16:05:38 pk 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 #ifdef vax
     92 #define	X_AVENRUN	6
     93 	{ "_avenrun" },
     94 #endif
     95 	"",
     96 };
     97 struct ifnet_head ifnetq;	/* chain of ethernet interfaces */
     98 int numintfs;
     99 int stats_service();
    100 
    101 extern int from_inetd;
    102 int sincelastreq = 0;		/* number of alarms since last request */
    103 extern int closedown;
    104 kvm_t *kfd;
    105 
    106 union {
    107 	struct stats s1;
    108 	struct statsswtch s2;
    109 	struct statstime s3;
    110 } stats_all;
    111 
    112 void updatestat();
    113 static stat_is_init = 0;
    114 extern int errno;
    115 
    116 #ifndef FSCALE
    117 #define FSCALE (1 << 8)
    118 #endif
    119 
    120 stat_init()
    121 {
    122 	stat_is_init = 1;
    123 	setup();
    124 	updatestat();
    125 	(void) signal(SIGALRM, updatestat);
    126 	alarm(1);
    127 }
    128 
    129 statstime *
    130 rstatproc_stats_3_svc(arg, rqstp)
    131 	void *arg;
    132 	struct svc_req *rqstp;
    133 {
    134 	if (!stat_is_init)
    135 	        stat_init();
    136 	sincelastreq = 0;
    137 	return (&stats_all.s3);
    138 }
    139 
    140 statsswtch *
    141 rstatproc_stats_2_svc(arg, rqstp)
    142 	void *arg;
    143 	struct svc_req *rqstp;
    144 {
    145 	if (!stat_is_init)
    146 	        stat_init();
    147 	sincelastreq = 0;
    148 	return (&stats_all.s2);
    149 }
    150 
    151 stats *
    152 rstatproc_stats_1_svc(arg, rqstp)
    153 	void *arg;
    154 	struct svc_req *rqstp;
    155 {
    156 	if (!stat_is_init)
    157 	        stat_init();
    158 	sincelastreq = 0;
    159 	return (&stats_all.s1);
    160 }
    161 
    162 u_int *
    163 rstatproc_havedisk_3_svc(arg, rqstp)
    164 	void *arg;
    165 	struct svc_req *rqstp;
    166 {
    167 	static u_int have;
    168 
    169 	if (!stat_is_init)
    170 	        stat_init();
    171 	sincelastreq = 0;
    172 	have = havedisk();
    173 	return (&have);
    174 }
    175 
    176 u_int *
    177 rstatproc_havedisk_2_svc(arg, rqstp)
    178 	void *arg;
    179 	struct svc_req *rqstp;
    180 {
    181 	return (rstatproc_havedisk_3_svc(arg, rqstp));
    182 }
    183 
    184 u_int *
    185 rstatproc_havedisk_1_svc(arg, rqstp)
    186 	void *arg;
    187 	struct svc_req *rqstp;
    188 {
    189 	return (rstatproc_havedisk_3_svc(arg, rqstp));
    190 }
    191 
    192 void
    193 updatestat()
    194 {
    195 	long off;
    196 	int i, hz;
    197 	struct vmmeter cnt;
    198 	struct ifnet ifnet;
    199 	double avrun[3];
    200 	struct timeval tm, btm;
    201 #ifdef BSD
    202 	int cp_time[BSD_CPUSTATES];
    203 #endif
    204 
    205 #ifdef DEBUG
    206 	syslog(LOG_DEBUG, "entering updatestat");
    207 #endif
    208 	if (sincelastreq >= closedown) {
    209 #ifdef DEBUG
    210                 syslog(LOG_DEBUG, "about to closedown");
    211 #endif
    212                 if (from_inetd)
    213                         exit(0);
    214                 else {
    215                         stat_is_init = 0;
    216                         return;
    217                 }
    218 	}
    219 	sincelastreq++;
    220 
    221 	if (kvm_read(kfd, (long)nl[X_HZ].n_value, (char *)&hz, sizeof hz) !=
    222 	    sizeof hz) {
    223 		syslog(LOG_ERR, "can't read hz from kmem");
    224 		exit(1);
    225 	}
    226 #ifdef BSD
    227  	if (kvm_read(kfd, (long)nl[X_CPTIME].n_value, (char *)cp_time,
    228 		     sizeof (cp_time))
    229 	    != sizeof (cp_time)) {
    230 		syslog(LOG_ERR, "can't read cp_time from kmem");
    231 		exit(1);
    232 	}
    233 	for (i = 0; i < CPUSTATES; i++)
    234 		stats_all.s1.cp_time[i] = cp_time[cp_xlat[i]];
    235 #else
    236  	if (kvm_read(kfd, (long)nl[X_CPTIME].n_value,
    237 		     (char *)stats_all.s1.cp_time,
    238 		     sizeof (stats_all.s1.cp_time))
    239 	    != sizeof (stats_all.s1.cp_time)) {
    240 		syslog(LOG_ERR, "can't read cp_time from kmem");
    241 		exit(1);
    242 	}
    243 #endif
    244 #ifdef vax
    245  	if (kvm_read(kfd, (long)nl[X_AVENRUN].n_value, (char *)avrun,
    246 		     sizeof (avrun)) != sizeof (avrun)) {
    247 		syslog(LOG_ERR, "can't read avenrun from kmem");
    248 		exit(1);
    249 	}
    250 #endif
    251 #ifdef BSD
    252         (void)getloadavg(avrun, sizeof(avrun) / sizeof(avrun[0]));
    253 #endif
    254 	stats_all.s2.avenrun[0] = avrun[0] * FSCALE;
    255 	stats_all.s2.avenrun[1] = avrun[1] * FSCALE;
    256 	stats_all.s2.avenrun[2] = avrun[2] * FSCALE;
    257  	if (kvm_read(kfd, (long)nl[X_BOOTTIME].n_value,
    258 		     (char *)&btm, sizeof (stats_all.s2.boottime))
    259 	    != sizeof (stats_all.s2.boottime)) {
    260 		syslog(LOG_ERR, "can't read boottime from kmem");
    261 		exit(1);
    262 	}
    263 	stats_all.s2.boottime.tv_sec = btm.tv_sec;
    264 	stats_all.s2.boottime.tv_usec = btm.tv_usec;
    265 
    266 
    267 #ifdef DEBUG
    268 	syslog(LOG_DEBUG, "%d %d %d %d\n", stats_all.s1.cp_time[0],
    269 	    stats_all.s1.cp_time[1], stats_all.s1.cp_time[2], stats_all.s1.cp_time[3]);
    270 #endif
    271 
    272  	if (kvm_read(kfd, (long)nl[X_CNT].n_value, (char *)&cnt, sizeof cnt) !=
    273 	    sizeof cnt) {
    274 		syslog(LOG_ERR, "can't read cnt from kmem");
    275 		exit(1);
    276 	}
    277 	stats_all.s1.v_pgpgin = cnt.v_pgpgin;
    278 	stats_all.s1.v_pgpgout = cnt.v_pgpgout;
    279 	stats_all.s1.v_pswpin = cnt.v_pswpin;
    280 	stats_all.s1.v_pswpout = cnt.v_pswpout;
    281 	stats_all.s1.v_intr = cnt.v_intr;
    282 	gettimeofday(&tm, (struct timezone *) 0);
    283 	stats_all.s1.v_intr -= hz*(tm.tv_sec - btm.tv_sec) +
    284 	    hz*(tm.tv_usec - btm.tv_usec)/1000000;
    285 	stats_all.s2.v_swtch = cnt.v_swtch;
    286 
    287  	if (kvm_read(kfd, (long)nl[X_DKXFER].n_value,
    288 		     (char *)stats_all.s1.dk_xfer,
    289 		     sizeof (stats_all.s1.dk_xfer))
    290 	    != sizeof (stats_all.s1.dk_xfer)) {
    291 		syslog(LOG_ERR, "can't read dk_xfer from kmem");
    292 		exit(1);
    293 	}
    294 
    295 	stats_all.s1.if_ipackets = 0;
    296 	stats_all.s1.if_opackets = 0;
    297 	stats_all.s1.if_ierrors = 0;
    298 	stats_all.s1.if_oerrors = 0;
    299 	stats_all.s1.if_collisions = 0;
    300 	for (off = (long)ifnetq.tqh_first, i = 0; off && i < numintfs; i++) {
    301 		if (kvm_read(kfd, off, (char *)&ifnet, sizeof ifnet) !=
    302 		    sizeof ifnet) {
    303 			syslog(LOG_ERR, "can't read ifnet from kmem");
    304 			exit(1);
    305 		}
    306 		stats_all.s1.if_ipackets += ifnet.if_data.ifi_ipackets;
    307 		stats_all.s1.if_opackets += ifnet.if_data.ifi_opackets;
    308 		stats_all.s1.if_ierrors += ifnet.if_data.ifi_ierrors;
    309 		stats_all.s1.if_oerrors += ifnet.if_data.ifi_oerrors;
    310 		stats_all.s1.if_collisions += ifnet.if_data.ifi_collisions;
    311 		off = (long)ifnet.if_list.tqe_next;
    312 	}
    313 	gettimeofday((struct timeval *)&stats_all.s3.curtime,
    314 		(struct timezone *) 0);
    315 	alarm(1);
    316 }
    317 
    318 setup()
    319 {
    320 	struct ifnet ifnet;
    321 	long off;
    322 	char errbuf[_POSIX2_LINE_MAX];
    323 
    324 	kfd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf);
    325 	if (kfd == NULL) {
    326 		syslog(LOG_ERR, "%s", errbuf);
    327 		exit (1);
    328 	}
    329 
    330 	if (kvm_nlist(kfd, nl) != 0) {
    331 		syslog(LOG_ERR, "can't get namelist");
    332 		exit (1);
    333         }
    334 
    335 	if (kvm_read(kfd, (long)nl[X_IFNET].n_value, &ifnetq,
    336                      sizeof ifnetq) != sizeof ifnetq)  {
    337 		syslog(LOG_ERR, "can't read ifnet queue head from kmem");
    338 		exit(1);
    339         }
    340 
    341 	numintfs = 0;
    342 	for (off = (long)ifnetq.tqh_first; off;) {
    343 		if (kvm_read(kfd, off, (char *)&ifnet, sizeof ifnet) !=
    344 		    sizeof ifnet) {
    345 			syslog(LOG_ERR, "can't read ifnet from kmem");
    346 			exit(1);
    347 		}
    348 		numintfs++;
    349 		off = (long)ifnet.if_list.tqe_next;
    350 	}
    351 }
    352 
    353 /*
    354  * returns true if have a disk
    355  */
    356 int
    357 havedisk()
    358 {
    359 	int i, cnt;
    360 	long  xfer[DK_NDRIVE];
    361 
    362 	if (kvm_nlist(kfd, nl) != 0) {
    363 		syslog(LOG_ERR, "can't get namelist");
    364 		exit (1);
    365         }
    366 
    367 	if (kvm_read(kfd, (long)nl[X_DKXFER].n_value,
    368 		     (char *)xfer, sizeof xfer) != sizeof xfer) {
    369 		syslog(LOG_ERR, "can't read dk_xfer from kmem");
    370 		exit(1);
    371 	}
    372 	cnt = 0;
    373 	for (i=0; i < DK_NDRIVE; i++)
    374 		cnt += xfer[i];
    375 	return (cnt != 0);
    376 }
    377 
    378 void
    379 rstat_service(rqstp, transp)
    380 	struct svc_req *rqstp;
    381 	SVCXPRT *transp;
    382 {
    383 	union {
    384 		int fill;
    385 	} argument;
    386 	char *result;
    387 	bool_t (*xdr_argument) __P((...)), (*xdr_result) __P((...));
    388 	char *(*local) __P((void *, struct svc_req *));
    389 
    390 	switch (rqstp->rq_proc) {
    391 	case NULLPROC:
    392 		(void)svc_sendreply(transp, xdr_void, (char *)NULL);
    393 		goto leave;
    394 
    395 	case RSTATPROC_STATS:
    396 		xdr_argument = xdr_void;
    397 		xdr_result = xdr_statstime;
    398                 switch (rqstp->rq_vers) {
    399                 case RSTATVERS_ORIG:
    400                         local = (char *(*) __P((void *, struct svc_req *)))
    401 				rstatproc_stats_1_svc;
    402                         break;
    403                 case RSTATVERS_SWTCH:
    404                         local = (char *(*) __P((void *, struct svc_req *)))
    405 				rstatproc_stats_2_svc;
    406                         break;
    407                 case RSTATVERS_TIME:
    408                         local = (char *(*) __P((void *, struct svc_req *)))
    409 				rstatproc_stats_3_svc;
    410                         break;
    411                 default:
    412                         svcerr_progvers(transp, RSTATVERS_ORIG, RSTATVERS_TIME);
    413                         goto leave;
    414                 }
    415 		break;
    416 
    417 	case RSTATPROC_HAVEDISK:
    418 		xdr_argument = xdr_void;
    419 		xdr_result = xdr_u_int;
    420                 switch (rqstp->rq_vers) {
    421                 case RSTATVERS_ORIG:
    422                         local = (char *(*) __P((void *, struct svc_req *)))
    423 				rstatproc_havedisk_1_svc;
    424                         break;
    425                 case RSTATVERS_SWTCH:
    426                         local = (char *(*) __P((void *, struct svc_req *)))
    427 				rstatproc_havedisk_2_svc;
    428                         break;
    429                 case RSTATVERS_TIME:
    430                         local = (char *(*) __P((void *, struct svc_req *)))
    431 				rstatproc_havedisk_3_svc;
    432                         break;
    433                 default:
    434                         svcerr_progvers(transp, RSTATVERS_ORIG, RSTATVERS_TIME);
    435                         goto leave;
    436                 }
    437 		break;
    438 
    439 	default:
    440 		svcerr_noproc(transp);
    441 		goto leave;
    442 	}
    443 	bzero((char *)&argument, sizeof(argument));
    444 	if (!svc_getargs(transp, xdr_argument, (caddr_t)&argument)) {
    445 		svcerr_decode(transp);
    446 		goto leave;
    447 	}
    448 	result = (*local)(&argument, rqstp);
    449 	if (result != NULL && !svc_sendreply(transp, xdr_result, result)) {
    450 		svcerr_systemerr(transp);
    451 	}
    452 	if (!svc_freeargs(transp, xdr_argument, (caddr_t)&argument)) {
    453 		(void)fprintf(stderr, "unable to free arguments\n");
    454 		exit(1);
    455 	}
    456 leave:
    457         if (from_inetd)
    458                 exit(0);
    459 }
    460