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 30 #ifndef lint 31 #if 0 32 static char sccsid[] = "from: @(#)rpc.rstatd.c 1.1 86/09/25 Copyr 1984 Sun Micro"; 33 static char sccsid[] = "from: @(#)rstat_proc.c 2.2 88/08/01 4.0 RPCSRC"; 34 #endif 35 static const char rcsid[] = 36 "$FreeBSD$"; 37 #endif 38 39 /* 40 * rstat service: built with rstat.x and derived from rpc.rstatd.c 41 * 42 * Copyright (c) 1984 by Sun Microsystems, Inc. 43 */ 44 45 #include <sys/types.h> 46 #include <sys/socket.h> 47 #include <sys/sysctl.h> 48 #include <sys/time.h> 49 #include <sys/resource.h> 50 #include <sys/vmmeter.h> 51 #include <sys/param.h> 52 53 #include <err.h> 54 #include <errno.h> 55 #include <fcntl.h> 56 #include <kvm.h> 57 #include <limits.h> 58 #include <nlist.h> 59 #include <signal.h> 60 #include <stdio.h> 61 #include <stdlib.h> 62 #include <string.h> 63 #include <syslog.h> 64 #include <unistd.h> 65 #include <devstat.h> 66 67 #include <net/if.h> 68 #include <net/if_mib.h> 69 70 #undef FSHIFT /* Use protocol's shift and scale values */ 71 #undef FSCALE 72 #undef if_ipackets 73 #undef if_ierrors 74 #undef if_opackets 75 #undef if_oerrors 76 #undef if_collisions 77 #include <rpcsvc/rstat.h> 78 79 struct nlist nl[] = { 80 #define X_CPTIME 0 81 { "_cp_time" }, 82 #define X_CNT 1 83 { "_cnt" }, 84 { "" }, 85 }; 86 87 int haveadisk(void); 88 void updatexfers(int, int *); 89 void setup(void); 90 int stats_service(void); 91 92 extern int from_inetd; 93 int sincelastreq = 0; /* number of alarms since last request */ 94 extern int closedown; 95 96 union { 97 struct stats s1; 98 struct statsswtch s2; 99 struct statstime s3; 100 } stats_all; 101 102 void updatestat(); 103 static int stat_is_init = 0; 104 static kvm_t *kd; 105 106 static int cp_time_xlat[RSTAT_CPUSTATES] = { CP_USER, CP_NICE, CP_SYS, 107 CP_IDLE }; 108 static long bsd_cp_time[CPUSTATES]; 109 110 111 #ifndef FSCALE 112 #define FSCALE (1 << 8) 113 #endif 114 115 void 116 stat_init(void) 117 { 118 stat_is_init = 1; 119 setup(); 120 alarm(0); 121 updatestat(); 122 (void) signal(SIGALRM, updatestat); 123 alarm(1); 124 } 125 126 statstime * 127 rstatproc_stats_3_svc(void *argp, struct svc_req *rqstp) 128 { 129 if (! stat_is_init) 130 stat_init(); 131 sincelastreq = 0; 132 return(&stats_all.s3); 133 } 134 135 statsswtch * 136 rstatproc_stats_2_svc(void *argp, struct svc_req *rqstp) 137 { 138 if (! stat_is_init) 139 stat_init(); 140 sincelastreq = 0; 141 return(&stats_all.s2); 142 } 143 144 stats * 145 rstatproc_stats_1_svc(void *argp, struct svc_req *rqstp) 146 { 147 if (! stat_is_init) 148 stat_init(); 149 sincelastreq = 0; 150 return(&stats_all.s1); 151 } 152 153 u_int * 154 rstatproc_havedisk_3_svc(void *argp, struct svc_req *rqstp) 155 { 156 static u_int have; 157 158 if (! stat_is_init) 159 stat_init(); 160 sincelastreq = 0; 161 have = haveadisk(); 162 return(&have); 163 } 164 165 u_int * 166 rstatproc_havedisk_2_svc(void *argp, struct svc_req *rqstp) 167 { 168 return(rstatproc_havedisk_3_svc(argp, rqstp)); 169 } 170 171 u_int * 172 rstatproc_havedisk_1_svc(void *argp, struct svc_req *rqstp) 173 { 174 return(rstatproc_havedisk_3_svc(argp, rqstp)); 175 } 176 177 void 178 updatestat(void) 179 { 180 int i, hz; 181 struct clockinfo clockrate; 182 struct vmmeter cnt; 183 struct ifmibdata ifmd; 184 double avrun[3]; 185 struct timeval tm, btm; 186 int mib[6]; 187 size_t len; 188 int ifcount; 189 190 #ifdef DEBUG 191 fprintf(stderr, "entering updatestat\n"); 192 #endif 193 if (sincelastreq >= closedown) { 194 #ifdef DEBUG 195 fprintf(stderr, "about to closedown\n"); 196 #endif 197 kvm_close(kd); 198 if (from_inetd) 199 exit(0); 200 else { 201 stat_is_init = 0; 202 return; 203 } 204 } 205 sincelastreq++; 206 207 mib[0] = CTL_KERN; 208 mib[1] = KERN_CLOCKRATE; 209 len = sizeof clockrate; 210 if (sysctl(mib, 2, &clockrate, &len, 0, 0) < 0) { 211 syslog(LOG_ERR, "sysctl(kern.clockrate): %m"); 212 exit(1); 213 } 214 hz = clockrate.hz; 215 216 if (kvm_read(kd, (long)nl[X_CPTIME].n_value, (char *)bsd_cp_time, sizeof(bsd_cp_time)) 217 != sizeof(bsd_cp_time)) { 218 syslog(LOG_ERR, "rstat: can't read cp_time from kmem"); 219 exit(1); 220 } 221 for(i = 0; i < RSTAT_CPUSTATES ; i++) 222 stats_all.s1.cp_time[i] = bsd_cp_time[cp_time_xlat[i]]; 223 224 (void)getloadavg(avrun, sizeof(avrun) / sizeof(avrun[0])); 225 226 stats_all.s2.avenrun[0] = avrun[0] * FSCALE; 227 stats_all.s2.avenrun[1] = avrun[1] * FSCALE; 228 stats_all.s2.avenrun[2] = avrun[2] * FSCALE; 229 230 mib[0] = CTL_KERN; 231 mib[1] = KERN_BOOTTIME; 232 len = sizeof btm; 233 if (sysctl(mib, 2, &btm, &len, 0, 0) < 0) { 234 syslog(LOG_ERR, "sysctl(kern.boottime): %m"); 235 exit(1); 236 } 237 238 stats_all.s2.boottime.tv_sec = btm.tv_sec; 239 stats_all.s2.boottime.tv_usec = btm.tv_usec; 240 241 242 #ifdef DEBUG 243 fprintf(stderr, "%d %d %d %d\n", stats_all.s1.cp_time[0], 244 stats_all.s1.cp_time[1], stats_all.s1.cp_time[2], stats_all.s1.cp_time[3]); 245 #endif 246 247 /* XXX - should use sysctl */ 248 if (kvm_read(kd, (long)nl[X_CNT].n_value, (char *)&cnt, sizeof cnt) != sizeof cnt) { 249 syslog(LOG_ERR, "rstat: can't read cnt from kmem"); 250 exit(1); 251 } 252 stats_all.s1.v_pgpgin = cnt.v_vnodepgsin; 253 stats_all.s1.v_pgpgout = cnt.v_vnodepgsout; 254 stats_all.s1.v_pswpin = cnt.v_swappgsin; 255 stats_all.s1.v_pswpout = cnt.v_swappgsout; 256 stats_all.s1.v_intr = cnt.v_intr; 257 gettimeofday(&tm, (struct timezone *) 0); 258 stats_all.s1.v_intr -= hz*(tm.tv_sec - btm.tv_sec) + 259 hz*(tm.tv_usec - btm.tv_usec)/1000000; 260 stats_all.s2.v_swtch = cnt.v_swtch; 261 262 /* update disk transfers */ 263 updatexfers(RSTAT_DK_NDRIVE, stats_all.s1.dk_xfer); 264 265 mib[0] = CTL_NET; 266 mib[1] = PF_LINK; 267 mib[2] = NETLINK_GENERIC; 268 mib[3] = IFMIB_SYSTEM; 269 mib[4] = IFMIB_IFCOUNT; 270 len = sizeof ifcount; 271 if (sysctl(mib, 5, &ifcount, &len, 0, 0) < 0) { 272 syslog(LOG_ERR, "sysctl(net.link.generic.system.ifcount): %m"); 273 exit(1); 274 } 275 276 stats_all.s1.if_ipackets = 0; 277 stats_all.s1.if_opackets = 0; 278 stats_all.s1.if_ierrors = 0; 279 stats_all.s1.if_oerrors = 0; 280 stats_all.s1.if_collisions = 0; 281 for (i = 1; i <= ifcount; i++) { 282 len = sizeof ifmd; 283 mib[3] = IFMIB_IFDATA; 284 mib[4] = i; 285 mib[5] = IFDATA_GENERAL; 286 if (sysctl(mib, 6, &ifmd, &len, 0, 0) < 0) { 287 if (errno == ENOENT) 288 continue; 289 290 syslog(LOG_ERR, "sysctl(net.link.ifdata.%d.general)" 291 ": %m", i); 292 exit(1); 293 } 294 295 stats_all.s1.if_ipackets += ifmd.ifmd_data.ifi_ipackets; 296 stats_all.s1.if_opackets += ifmd.ifmd_data.ifi_opackets; 297 stats_all.s1.if_ierrors += ifmd.ifmd_data.ifi_ierrors; 298 stats_all.s1.if_oerrors += ifmd.ifmd_data.ifi_oerrors; 299 stats_all.s1.if_collisions += ifmd.ifmd_data.ifi_collisions; 300 } 301 gettimeofday((struct timeval *)&stats_all.s3.curtime, 302 (struct timezone *) 0); 303 alarm(1); 304 } 305 306 void 307 setup() 308 { 309 char errbuf[_POSIX2_LINE_MAX]; 310 311 int en; 312 313 if ((kd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf)) == NULL) { 314 syslog(LOG_ERR, "rpc.rstatd, %s", errbuf); 315 exit(1); 316 } 317 318 if ((en = kvm_nlist(kd, nl)) != 0) { 319 syslog(LOG_ERR, "rstatd: Can't get namelist. %d", en); 320 exit (1); 321 } 322 } 323 324 /* 325 * returns true if have a disk 326 */ 327 int 328 haveadisk(void) 329 { 330 register int i; 331 struct statinfo stats; 332 int num_devices, retval = 0; 333 334 if ((num_devices = devstat_getnumdevs(NULL)) < 0) { 335 syslog(LOG_ERR, "rstatd: can't get number of devices: %s", 336 devstat_errbuf); 337 exit(1); 338 } 339 340 if (devstat_checkversion(NULL) < 0) { 341 syslog(LOG_ERR, "rstatd: %s", devstat_errbuf); 342 exit(1); 343 } 344 345 stats.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo)); 346 bzero(stats.dinfo, sizeof(struct devinfo)); 347 348 if (devstat_getdevs(NULL, &stats) == -1) { 349 syslog(LOG_ERR, "rstatd: can't get device list: %s", 350 devstat_errbuf); 351 exit(1); 352 } 353 for (i = 0; i < stats.dinfo->numdevs; i++) { 354 if (((stats.dinfo->devices[i].device_type 355 & DEVSTAT_TYPE_MASK) == DEVSTAT_TYPE_DIRECT) 356 && ((stats.dinfo->devices[i].device_type 357 & DEVSTAT_TYPE_PASS) == 0)) { 358 retval = 1; 359 break; 360 } 361 } 362 363 if (stats.dinfo->mem_ptr) 364 free(stats.dinfo->mem_ptr); 365 366 free(stats.dinfo); 367 return(retval); 368 } 369 370 void 371 updatexfers(int numdevs, int *devs) 372 { 373 register int i, j, k, t; 374 struct statinfo stats; 375 int num_devices = 0; 376 u_int64_t total_transfers; 377 378 if ((num_devices = devstat_getnumdevs(NULL)) < 0) { 379 syslog(LOG_ERR, "rstatd: can't get number of devices: %s", 380 devstat_errbuf); 381 exit(1); 382 } 383 384 if (devstat_checkversion(NULL) < 0) { 385 syslog(LOG_ERR, "rstatd: %s", devstat_errbuf); 386 exit(1); 387 } 388 389 stats.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo)); 390 bzero(stats.dinfo, sizeof(struct devinfo)); 391 392 if (devstat_getdevs(NULL, &stats) == -1) { 393 syslog(LOG_ERR, "rstatd: can't get device list: %s", 394 devstat_errbuf); 395 exit(1); 396 } 397 398 for (i = 0, j = 0; i < stats.dinfo->numdevs && j < numdevs; i++) { 399 if (((stats.dinfo->devices[i].device_type 400 & DEVSTAT_TYPE_MASK) == DEVSTAT_TYPE_DIRECT) 401 && ((stats.dinfo->devices[i].device_type 402 & DEVSTAT_TYPE_PASS) == 0)) { 403 total_transfers = 0; 404 for (k = 0; k < DEVSTAT_N_TRANS_FLAGS; k++) 405 total_transfers += 406 stats.dinfo->devices[i].operations[k]; 407 /* 408 * XXX KDM If the total transfers for this device 409 * are greater than the amount we can fit in a 410 * signed integer, just set them to the maximum 411 * amount we can fit in a signed integer. I have a 412 * feeling that the rstat protocol assumes 32-bit 413 * integers, so this could well break on a 64-bit 414 * architecture like the Alpha. 415 */ 416 if (total_transfers > INT_MAX) 417 t = INT_MAX; 418 else 419 t = total_transfers; 420 devs[j] = t; 421 j++; 422 } 423 } 424 425 if (stats.dinfo->mem_ptr) 426 free(stats.dinfo->mem_ptr); 427 428 free(stats.dinfo); 429 } 430 431 void 432 rstat_service(struct svc_req *rqstp, SVCXPRT *transp) 433 { 434 union { 435 int fill; 436 } argument; 437 char *result; 438 bool_t (*xdr_argument)(), (*xdr_result)(); 439 char *(*local)(); 440 441 switch (rqstp->rq_proc) { 442 case NULLPROC: 443 (void)svc_sendreply(transp, (xdrproc_t)xdr_void, NULL); 444 goto leave; 445 446 case RSTATPROC_STATS: 447 xdr_argument = xdr_void; 448 xdr_result = xdr_statstime; 449 switch (rqstp->rq_vers) { 450 case RSTATVERS_ORIG: 451 local = (char *(*)()) rstatproc_stats_1_svc; 452 break; 453 case RSTATVERS_SWTCH: 454 local = (char *(*)()) rstatproc_stats_2_svc; 455 break; 456 case RSTATVERS_TIME: 457 local = (char *(*)()) rstatproc_stats_3_svc; 458 break; 459 default: 460 svcerr_progvers(transp, RSTATVERS_ORIG, RSTATVERS_TIME); 461 goto leave; 462 /*NOTREACHED*/ 463 } 464 break; 465 466 case RSTATPROC_HAVEDISK: 467 xdr_argument = xdr_void; 468 xdr_result = xdr_u_int; 469 switch (rqstp->rq_vers) { 470 case RSTATVERS_ORIG: 471 local = (char *(*)()) rstatproc_havedisk_1_svc; 472 break; 473 case RSTATVERS_SWTCH: 474 local = (char *(*)()) rstatproc_havedisk_2_svc; 475 break; 476 case RSTATVERS_TIME: 477 local = (char *(*)()) rstatproc_havedisk_3_svc; 478 break; 479 default: 480 svcerr_progvers(transp, RSTATVERS_ORIG, RSTATVERS_TIME); 481 goto leave; 482 /*NOTREACHED*/ 483 } 484 break; 485 486 default: 487 svcerr_noproc(transp); 488 goto leave; 489 } 490 bzero((char *)&argument, sizeof(argument)); 491 if (!svc_getargs(transp, (xdrproc_t)xdr_argument, &argument)) { 492 svcerr_decode(transp); 493 goto leave; 494 } 495 result = (*local)(&argument, rqstp); 496 if (result != NULL && 497 !svc_sendreply(transp, (xdrproc_t)xdr_result, result)) { 498 svcerr_systemerr(transp); 499 } 500 if (!svc_freeargs(transp, (xdrproc_t)xdr_argument, &argument)) 501 errx(1, "unable to free arguments"); 502 leave: 503 if (from_inetd) 504 exit(0); 505 } 506