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