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