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