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