xref: /freebsd/libexec/rpc.rstatd/rstat_proc.c (revision 11afcc8f9f96d657b8e6f7547c02c1957331fc96)
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 	"$Id: rstat_proc.c,v 1.9 1998/01/07 07:50:59 charnier Exp $";
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 
52 #include <err.h>
53 #include <fcntl.h>
54 #include <kvm.h>
55 #include <limits.h>
56 #include <signal.h>
57 #include <stdio.h>
58 #include <stdlib.h>
59 #include <string.h>
60 #include <syslog.h>
61 #include <unistd.h>
62 
63 #include <net/if.h>
64 #include <net/if_mib.h>
65 
66 #undef FSHIFT			 /* Use protocol's shift and scale values */
67 #undef FSCALE
68 #undef if_ipackets
69 #undef if_ierrors
70 #undef if_opackets
71 #undef if_oerrors
72 #undef if_collisions
73 #include <rpcsvc/rstat.h>
74 
75 struct nlist nl[] = {
76 #define	X_CPTIME	0
77 	{ "_cp_time" },
78 #define	X_CNT		1
79 	{ "_cnt" },
80 #define	X_DKXFER	2
81 	{ "_dk_xfer" },
82 #define	X_DKNDRIVE	3
83 	{ "_dk_ndrive" },
84 	{ "" },
85 };
86 
87 int havedisk __P((void));
88 void setup __P((void));
89 int stats_service();
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()
116 {
117     stat_is_init = 1;
118     setup();
119     updatestat();
120     (void) signal(SIGALRM, updatestat);
121     alarm(1);
122 }
123 
124 statstime *
125 rstatproc_stats_3_svc(argp, rqstp)
126     void			*argp;
127     struct svc_req		*rqstp;
128 {
129     if (! stat_is_init)
130         stat_init();
131     sincelastreq = 0;
132     return(&stats_all.s3);
133 }
134 
135 statsswtch *
136 rstatproc_stats_2_svc(argp, rqstp)
137     void			*argp;
138     struct svc_req		*rqstp;
139 {
140     if (! stat_is_init)
141         stat_init();
142     sincelastreq = 0;
143     return(&stats_all.s2);
144 }
145 
146 stats *
147 rstatproc_stats_1_svc(argp, rqstp)
148     void			*argp;
149     struct svc_req		*rqstp;
150 {
151     if (! stat_is_init)
152         stat_init();
153     sincelastreq = 0;
154     return(&stats_all.s1);
155 }
156 
157 u_int *
158 rstatproc_havedisk_3_svc(argp, rqstp)
159     void			*argp;
160     struct svc_req		*rqstp;
161 {
162     static u_int have;
163 
164     if (! stat_is_init)
165         stat_init();
166     sincelastreq = 0;
167     have = havedisk();
168 	return(&have);
169 }
170 
171 u_int *
172 rstatproc_havedisk_2_svc(argp, rqstp)
173     void			*argp;
174     struct svc_req		*rqstp;
175 {
176     return(rstatproc_havedisk_3_svc(argp, rqstp));
177 }
178 
179 u_int *
180 rstatproc_havedisk_1_svc(argp, rqstp)
181     void			*argp;
182     struct svc_req		*rqstp;
183 {
184     return(rstatproc_havedisk_3_svc(argp, rqstp));
185 }
186 
187 void
188 updatestat()
189 {
190 	int i, hz;
191 	struct clockinfo clockrate;
192 	struct vmmeter cnt;
193 	struct ifmibdata ifmd;
194 	double avrun[3];
195 	struct timeval tm, btm;
196 	int mib[6];
197 	size_t len;
198 	int ifcount;
199 
200 #ifdef DEBUG
201 	fprintf(stderr, "entering updatestat\n");
202 #endif
203 	if (sincelastreq >= closedown) {
204 #ifdef DEBUG
205                 fprintf(stderr, "about to closedown\n");
206 #endif
207                 if (from_inetd)
208                         exit(0);
209                 else {
210                         stat_is_init = 0;
211                         return;
212                 }
213 	}
214 	sincelastreq++;
215 
216 	mib[0] = CTL_KERN;
217 	mib[1] = KERN_CLOCKRATE;
218 	len = sizeof clockrate;
219 	if (sysctl(mib, 2, &clockrate, &len, 0, 0) < 0) {
220 		syslog(LOG_ERR, "sysctl(kern.clockrate): %m");
221 		exit(1);
222 	}
223 	hz = clockrate.hz;
224 
225 	if (kvm_read(kd, (long)nl[X_CPTIME].n_value, (char *)bsd_cp_time, sizeof(bsd_cp_time))
226 	    != sizeof(bsd_cp_time)) {
227 		syslog(LOG_ERR, "rstat: can't read cp_time from kmem");
228 		exit(1);
229 	}
230 	for(i = 0; i < RSTAT_CPUSTATES ; i++)
231 		stats_all.s1.cp_time[i] = bsd_cp_time[cp_time_xlat[i]];
232 
233         (void)getloadavg(avrun, sizeof(avrun) / sizeof(avrun[0]));
234 
235 	stats_all.s2.avenrun[0] = avrun[0] * FSCALE;
236 	stats_all.s2.avenrun[1] = avrun[1] * FSCALE;
237 	stats_all.s2.avenrun[2] = avrun[2] * FSCALE;
238 
239 	mib[0] = CTL_KERN;
240 	mib[1] = KERN_BOOTTIME;
241 	len = sizeof btm;
242 	if (sysctl(mib, 2, &btm, &len, 0, 0) < 0) {
243 		syslog(LOG_ERR, "sysctl(kern.boottime): %m");
244 		exit(1);
245 	}
246 
247 	stats_all.s2.boottime.tv_sec = btm.tv_sec;
248 	stats_all.s2.boottime.tv_usec = btm.tv_usec;
249 
250 
251 #ifdef DEBUG
252 	fprintf(stderr, "%d %d %d %d\n", stats_all.s1.cp_time[0],
253 	    stats_all.s1.cp_time[1], stats_all.s1.cp_time[2], stats_all.s1.cp_time[3]);
254 #endif
255 
256 	/* XXX - should use sysctl */
257  	if (kvm_read(kd, (long)nl[X_CNT].n_value, (char *)&cnt, sizeof cnt) != sizeof cnt) {
258 		syslog(LOG_ERR, "rstat: can't read cnt from kmem");
259 		exit(1);
260 	}
261 	stats_all.s1.v_pgpgin = cnt.v_vnodepgsin;
262 	stats_all.s1.v_pgpgout = cnt.v_vnodepgsout;
263 	stats_all.s1.v_pswpin = cnt.v_swappgsin;
264 	stats_all.s1.v_pswpout = cnt.v_swappgsout;
265 	stats_all.s1.v_intr = cnt.v_intr;
266 	gettimeofday(&tm, (struct timezone *) 0);
267 	stats_all.s1.v_intr -= hz*(tm.tv_sec - btm.tv_sec) +
268 	    hz*(tm.tv_usec - btm.tv_usec)/1000000;
269 	stats_all.s2.v_swtch = cnt.v_swtch;
270 
271 	/* XXX - should use sysctl */
272  	if (kvm_read(kd, (long)nl[X_DKXFER].n_value, (char *)stats_all.s1.dk_xfer, sizeof (stats_all.s1.dk_xfer))
273 	    != sizeof (stats_all.s1.dk_xfer)) {
274 		syslog(LOG_ERR, "rstat: can't read dk_xfer from kmem");
275 		exit(1);
276 	}
277 
278 	mib[0] = CTL_NET;
279 	mib[1] = PF_LINK;
280 	mib[2] = NETLINK_GENERIC;
281 	mib[3] = IFMIB_SYSTEM;
282 	mib[4] = IFMIB_IFCOUNT;
283 	len = sizeof ifcount;
284 	if (sysctl(mib, 5, &ifcount, &len, 0, 0) < 0) {
285 		syslog(LOG_ERR, "sysctl(net.link.generic.system.ifcount): %m");
286 		exit(1);
287 	}
288 
289 	stats_all.s1.if_ipackets = 0;
290 	stats_all.s1.if_opackets = 0;
291 	stats_all.s1.if_ierrors = 0;
292 	stats_all.s1.if_oerrors = 0;
293 	stats_all.s1.if_collisions = 0;
294 	for (i = 1; i <= ifcount; i++) {
295 		len = sizeof ifmd;
296 		mib[3] = IFMIB_IFDATA;
297 		mib[4] = i;
298 		mib[5] = IFDATA_GENERAL;
299 		if (sysctl(mib, 6, &ifmd, &len, 0, 0) < 0) {
300 			syslog(LOG_ERR, "sysctl(net.link.ifdata.%d.general)"
301 			       ": %m", i);
302 			exit(1);
303 		}
304 
305 		stats_all.s1.if_ipackets += ifmd.ifmd_data.ifi_ipackets;
306 		stats_all.s1.if_opackets += ifmd.ifmd_data.ifi_opackets;
307 		stats_all.s1.if_ierrors += ifmd.ifmd_data.ifi_ierrors;
308 		stats_all.s1.if_oerrors += ifmd.ifmd_data.ifi_oerrors;
309 		stats_all.s1.if_collisions += ifmd.ifmd_data.ifi_collisions;
310 	}
311 	gettimeofday((struct timeval *)&stats_all.s3.curtime,
312 		(struct timezone *) 0);
313 	alarm(1);
314 }
315 
316 void
317 setup()
318 {
319 	char errbuf[_POSIX2_LINE_MAX];
320 
321 	int en;
322 
323 	if ((kd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf)) == NULL) {
324 		syslog(LOG_ERR, "rpc.rstatd, %s", errbuf);
325 		exit(1);
326 	}
327 
328 	if ((en = kvm_nlist(kd, nl)) != 0) {
329 		syslog(LOG_ERR, "rstatd: Can't get namelist. %d", en);
330 		exit (1);
331         }
332 }
333 
334 /*
335  * returns true if have a disk
336  */
337 int
338 havedisk()
339 {
340 	int dk_ndrive;
341 
342 	if (kvm_nlist(kd, nl) != 0) {
343 		syslog(LOG_ERR, "rstatd: can't get namelist.(d)");
344 		exit (1);
345         }
346 
347 	if (kvm_read(kd, (long)nl[X_DKNDRIVE].n_value, (char *)&dk_ndrive,
348 		     sizeof dk_ndrive)!= sizeof dk_ndrive) {
349 		syslog(LOG_ERR, "rstat: can't read kmem");
350 		exit(1);
351 	}
352 	return (dk_ndrive != 0);
353 }
354 
355 void
356 rstat_service(rqstp, transp)
357 	struct svc_req *rqstp;
358 	SVCXPRT *transp;
359 {
360 	union {
361 		int fill;
362 	} argument;
363 	char *result;
364 	bool_t (*xdr_argument)(), (*xdr_result)();
365 	char *(*local)();
366 
367 	switch (rqstp->rq_proc) {
368 	case NULLPROC:
369 		(void)svc_sendreply(transp, xdr_void, (char *)NULL);
370 		goto leave;
371 
372 	case RSTATPROC_STATS:
373 		xdr_argument = xdr_void;
374 		xdr_result = xdr_statstime;
375                 switch (rqstp->rq_vers) {
376                 case RSTATVERS_ORIG:
377                         local = (char *(*)()) rstatproc_stats_1_svc;
378                         break;
379                 case RSTATVERS_SWTCH:
380                         local = (char *(*)()) rstatproc_stats_2_svc;
381                         break;
382                 case RSTATVERS_TIME:
383                         local = (char *(*)()) rstatproc_stats_3_svc;
384                         break;
385                 default:
386                         svcerr_progvers(transp, RSTATVERS_ORIG, RSTATVERS_TIME);
387                         goto leave;
388                         /*NOTREACHED*/
389                 }
390 		break;
391 
392 	case RSTATPROC_HAVEDISK:
393 		xdr_argument = xdr_void;
394 		xdr_result = xdr_u_int;
395                 switch (rqstp->rq_vers) {
396                 case RSTATVERS_ORIG:
397                         local = (char *(*)()) rstatproc_havedisk_1_svc;
398                         break;
399                 case RSTATVERS_SWTCH:
400                         local = (char *(*)()) rstatproc_havedisk_2_svc;
401                         break;
402                 case RSTATVERS_TIME:
403                         local = (char *(*)()) rstatproc_havedisk_3_svc;
404                         break;
405                 default:
406                         svcerr_progvers(transp, RSTATVERS_ORIG, RSTATVERS_TIME);
407                         goto leave;
408                         /*NOTREACHED*/
409                 }
410 		break;
411 
412 	default:
413 		svcerr_noproc(transp);
414 		goto leave;
415 	}
416 	bzero((char *)&argument, sizeof(argument));
417 	if (!svc_getargs(transp, xdr_argument, (caddr_t)&argument)) {
418 		svcerr_decode(transp);
419 		goto leave;
420 	}
421 	result = (*local)(&argument, rqstp);
422 	if (result != NULL && !svc_sendreply(transp, xdr_result, result)) {
423 		svcerr_systemerr(transp);
424 	}
425 	if (!svc_freeargs(transp, xdr_argument, (caddr_t)&argument))
426 		errx(1, "unable to free arguments");
427 leave:
428         if (from_inetd)
429                 exit(0);
430 }
431