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