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