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
32static char sccsid[] = "from: @(#)rpc.rstatd.c 1.1 86/09/25 Copyr 1984 Sun Micro";
33static char sccsid[] = "from: @(#)rstat_proc.c	2.2 88/08/01 4.0 RPCSRC";
34#endif
35static 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/socket.h>
47#include <sys/sysctl.h>
48#include <sys/time.h>
49#include <sys/resource.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 <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
77int haveadisk(void);
78void updatexfers(int, int *);
79int stats_service(void);
80
81extern int from_inetd;
82int sincelastreq = 0;		/* number of alarms since last request */
83extern int closedown;
84
85union {
86    struct stats s1;
87    struct statsswtch s2;
88    struct statstime s3;
89} stats_all;
90
91void updatestat();
92static int stat_is_init = 0;
93
94static int	cp_time_xlat[RSTAT_CPUSTATES] = { CP_USER, CP_NICE, CP_SYS,
95							CP_IDLE };
96static long	bsd_cp_time[CPUSTATES];
97
98
99#ifndef FSCALE
100#define FSCALE (1 << 8)
101#endif
102
103void
104stat_init(void)
105{
106    stat_is_init = 1;
107    alarm(0);
108    updatestat();
109    (void) signal(SIGALRM, updatestat);
110    alarm(1);
111}
112
113statstime *
114rstatproc_stats_3_svc(void *argp, struct svc_req *rqstp)
115{
116    if (! stat_is_init)
117        stat_init();
118    sincelastreq = 0;
119    return(&stats_all.s3);
120}
121
122statsswtch *
123rstatproc_stats_2_svc(void *argp, struct svc_req *rqstp)
124{
125    if (! stat_is_init)
126        stat_init();
127    sincelastreq = 0;
128    return(&stats_all.s2);
129}
130
131stats *
132rstatproc_stats_1_svc(void *argp, struct svc_req *rqstp)
133{
134    if (! stat_is_init)
135        stat_init();
136    sincelastreq = 0;
137    return(&stats_all.s1);
138}
139
140u_int *
141rstatproc_havedisk_3_svc(void *argp, struct svc_req *rqstp)
142{
143    static u_int have;
144
145    if (! stat_is_init)
146        stat_init();
147    sincelastreq = 0;
148    have = haveadisk();
149	return(&have);
150}
151
152u_int *
153rstatproc_havedisk_2_svc(void *argp, struct svc_req *rqstp)
154{
155    return(rstatproc_havedisk_3_svc(argp, rqstp));
156}
157
158u_int *
159rstatproc_havedisk_1_svc(void *argp, struct svc_req *rqstp)
160{
161    return(rstatproc_havedisk_3_svc(argp, rqstp));
162}
163
164void
165updatestat(void)
166{
167	int i, hz;
168	struct clockinfo clockrate;
169	struct vmmeter cnt;
170	struct ifmibdata ifmd;
171	double avrun[3];
172	struct timeval tm, btm;
173	int mib[6];
174	size_t len;
175	int ifcount;
176
177#ifdef DEBUG
178	fprintf(stderr, "entering updatestat\n");
179#endif
180	if (sincelastreq >= closedown) {
181#ifdef DEBUG
182                fprintf(stderr, "about to closedown\n");
183#endif
184                if (from_inetd)
185                        exit(0);
186                else {
187                        stat_is_init = 0;
188                        return;
189                }
190	}
191	sincelastreq++;
192
193	mib[0] = CTL_KERN;
194	mib[1] = KERN_CLOCKRATE;
195	len = sizeof clockrate;
196	if (sysctl(mib, 2, &clockrate, &len, 0, 0) < 0) {
197		syslog(LOG_ERR, "sysctl(kern.clockrate): %m");
198		exit(1);
199	}
200	hz = clockrate.hz;
201
202	len = sizeof(bsd_cp_time);
203	if (sysctlbyname("kern.cp_time", bsd_cp_time, &len, 0, 0) < 0) {
204		syslog(LOG_ERR, "sysctl(kern.cp_time): %m");
205		exit(1);
206	}
207	for(i = 0; i < RSTAT_CPUSTATES ; i++)
208		stats_all.s1.cp_time[i] = bsd_cp_time[cp_time_xlat[i]];
209
210        (void)getloadavg(avrun, sizeof(avrun) / sizeof(avrun[0]));
211
212	stats_all.s2.avenrun[0] = avrun[0] * FSCALE;
213	stats_all.s2.avenrun[1] = avrun[1] * FSCALE;
214	stats_all.s2.avenrun[2] = avrun[2] * FSCALE;
215
216	mib[0] = CTL_KERN;
217	mib[1] = KERN_BOOTTIME;
218	len = sizeof btm;
219	if (sysctl(mib, 2, &btm, &len, 0, 0) < 0) {
220		syslog(LOG_ERR, "sysctl(kern.boottime): %m");
221		exit(1);
222	}
223
224	stats_all.s2.boottime.tv_sec = btm.tv_sec;
225	stats_all.s2.boottime.tv_usec = btm.tv_usec;
226
227
228#ifdef DEBUG
229	fprintf(stderr, "%d %d %d %d\n", stats_all.s1.cp_time[0],
230	    stats_all.s1.cp_time[1], stats_all.s1.cp_time[2], stats_all.s1.cp_time[3]);
231#endif
232
233#define	FETCH_CNT(stat, cnt) do {					\
234	len = sizeof((stat));						\
235	if (sysctlbyname("vm.stats." #cnt , &(stat), &len, 0, 0) < 0) { \
236		syslog(LOG_ERR, "sysctl(vm.stats." #cnt "): %m"); \
237		exit(1);						\
238	}								\
239} while (0)
240
241	FETCH_CNT(stats_all.s1.v_pgpgin, vm.v_vnodepgsin);
242	FETCH_CNT(stats_all.s1.v_pgpgout, vm.v_vnodepgsout);
243	FETCH_CNT(stats_all.s1.v_pswpin, vm.v_swappgsin);
244	FETCH_CNT(stats_all.s1.v_pswpout, vm.v_swappgsout);
245	FETCH_CNT(stats_all.s1.v_intr, sys.v_intr);
246	FETCH_CNT(stats_all.s2.v_swtch, sys.v_swtch);
247	gettimeofday(&tm, (struct timezone *) 0);
248	stats_all.s1.v_intr -= hz*(tm.tv_sec - btm.tv_sec) +
249	    hz*(tm.tv_usec - btm.tv_usec)/1000000;
250
251	/* update disk transfers */
252	updatexfers(RSTAT_DK_NDRIVE, stats_all.s1.dk_xfer);
253
254	mib[0] = CTL_NET;
255	mib[1] = PF_LINK;
256	mib[2] = NETLINK_GENERIC;
257	mib[3] = IFMIB_SYSTEM;
258	mib[4] = IFMIB_IFCOUNT;
259	len = sizeof ifcount;
260	if (sysctl(mib, 5, &ifcount, &len, 0, 0) < 0) {
261		syslog(LOG_ERR, "sysctl(net.link.generic.system.ifcount): %m");
262		exit(1);
263	}
264
265	stats_all.s1.if_ipackets = 0;
266	stats_all.s1.if_opackets = 0;
267	stats_all.s1.if_ierrors = 0;
268	stats_all.s1.if_oerrors = 0;
269	stats_all.s1.if_collisions = 0;
270	for (i = 1; i <= ifcount; i++) {
271		len = sizeof ifmd;
272		mib[3] = IFMIB_IFDATA;
273		mib[4] = i;
274		mib[5] = IFDATA_GENERAL;
275		if (sysctl(mib, 6, &ifmd, &len, 0, 0) < 0) {
276			if (errno == ENOENT)
277				continue;
278
279			syslog(LOG_ERR, "sysctl(net.link.ifdata.%d.general)"
280			       ": %m", i);
281			exit(1);
282		}
283
284		stats_all.s1.if_ipackets += ifmd.ifmd_data.ifi_ipackets;
285		stats_all.s1.if_opackets += ifmd.ifmd_data.ifi_opackets;
286		stats_all.s1.if_ierrors += ifmd.ifmd_data.ifi_ierrors;
287		stats_all.s1.if_oerrors += ifmd.ifmd_data.ifi_oerrors;
288		stats_all.s1.if_collisions += ifmd.ifmd_data.ifi_collisions;
289	}
290	gettimeofday((struct timeval *)&stats_all.s3.curtime,
291		(struct timezone *) 0);
292	alarm(1);
293}
294
295/*
296 * returns true if have a disk
297 */
298int
299haveadisk(void)
300{
301	register int i;
302	struct statinfo stats;
303	int num_devices, retval = 0;
304
305	if ((num_devices = devstat_getnumdevs(NULL)) < 0) {
306		syslog(LOG_ERR, "rstatd: can't get number of devices: %s",
307		       devstat_errbuf);
308		exit(1);
309	}
310
311	if (devstat_checkversion(NULL) < 0) {
312		syslog(LOG_ERR, "rstatd: %s", devstat_errbuf);
313		exit(1);
314	}
315
316	stats.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo));
317	bzero(stats.dinfo, sizeof(struct devinfo));
318
319	if (devstat_getdevs(NULL, &stats) == -1) {
320		syslog(LOG_ERR, "rstatd: can't get device list: %s",
321		       devstat_errbuf);
322		exit(1);
323	}
324	for (i = 0; i < stats.dinfo->numdevs; i++) {
325		if (((stats.dinfo->devices[i].device_type
326		      & DEVSTAT_TYPE_MASK) == DEVSTAT_TYPE_DIRECT)
327		 && ((stats.dinfo->devices[i].device_type
328		      & DEVSTAT_TYPE_PASS) == 0)) {
329			retval = 1;
330			break;
331		}
332	}
333
334	if (stats.dinfo->mem_ptr)
335		free(stats.dinfo->mem_ptr);
336
337	free(stats.dinfo);
338	return(retval);
339}
340
341void
342updatexfers(int numdevs, int *devs)
343{
344	register int i, j, k, t;
345	struct statinfo stats;
346	int num_devices = 0;
347	u_int64_t total_transfers;
348
349	if ((num_devices = devstat_getnumdevs(NULL)) < 0) {
350		syslog(LOG_ERR, "rstatd: can't get number of devices: %s",
351		       devstat_errbuf);
352		exit(1);
353	}
354
355	if (devstat_checkversion(NULL) < 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 (devstat_getdevs(NULL, &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 = 0;
375			for (k = 0; k < DEVSTAT_N_TRANS_FLAGS; k++)
376				total_transfers +=
377				    stats.dinfo->devices[i].operations[k];
378			/*
379			 * XXX KDM If the total transfers for this device
380			 * are greater than the amount we can fit in a
381			 * signed integer, just set them to the maximum
382			 * amount we can fit in a signed integer.  I have a
383			 * feeling that the rstat protocol assumes 32-bit
384			 * integers, so this could well break on a 64-bit
385			 * architecture like the Alpha.
386			 */
387			if (total_transfers > INT_MAX)
388				t = INT_MAX;
389			else
390				t = total_transfers;
391			devs[j] = t;
392			j++;
393		}
394	}
395
396	if (stats.dinfo->mem_ptr)
397		free(stats.dinfo->mem_ptr);
398
399	free(stats.dinfo);
400}
401
402void
403rstat_service(struct svc_req *rqstp, SVCXPRT *transp)
404{
405	union {
406		int fill;
407	} argument;
408	char *result;
409	bool_t (*xdr_argument)(), (*xdr_result)();
410	char *(*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 = xdr_void;
419		xdr_result = xdr_statstime;
420                switch (rqstp->rq_vers) {
421                case RSTATVERS_ORIG:
422                        local = (char *(*)()) rstatproc_stats_1_svc;
423                        break;
424                case RSTATVERS_SWTCH:
425                        local = (char *(*)()) rstatproc_stats_2_svc;
426                        break;
427                case RSTATVERS_TIME:
428                        local = (char *(*)()) 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 = xdr_void;
439		xdr_result = xdr_u_int;
440                switch (rqstp->rq_vers) {
441                case RSTATVERS_ORIG:
442                        local = (char *(*)()) rstatproc_havedisk_1_svc;
443                        break;
444                case RSTATVERS_SWTCH:
445                        local = (char *(*)()) rstatproc_havedisk_2_svc;
446                        break;
447                case RSTATVERS_TIME:
448                        local = (char *(*)()) 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, (xdrproc_t)xdr_argument, &argument)) {
463		svcerr_decode(transp);
464		goto leave;
465	}
466	result = (*local)(&argument, rqstp);
467	if (result != NULL &&
468	    !svc_sendreply(transp, (xdrproc_t)xdr_result, result)) {
469		svcerr_systemerr(transp);
470	}
471	if (!svc_freeargs(transp, (xdrproc_t)xdr_argument, &argument))
472		errx(1, "unable to free arguments");
473leave:
474        if (from_inetd)
475                exit(0);
476}
477