rstat_proc.c revision 1.28 1 /* $NetBSD: rstat_proc.c,v 1.28 1999/03/24 05:50:50 mrg Exp $ */
2
3 /*
4 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
5 * unrestricted use provided that this legend is included on all tape
6 * media and as a part of the software program in whole or part. Users
7 * may copy or modify Sun RPC without charge, but are not authorized
8 * to license or distribute it to anyone else except as part of a product or
9 * program developed by the user.
10 *
11 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
12 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
13 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
14 *
15 * Sun RPC is provided with no support and without any obligation on the
16 * part of Sun Microsystems, Inc. to assist in its use, correction,
17 * modification or enhancement.
18 *
19 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
20 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
21 * OR ANY PART THEREOF.
22 *
23 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
24 * or profits or other special, indirect and consequential damages, even if
25 * Sun has been advised of the possibility of such damages.
26 *
27 * Sun Microsystems, Inc.
28 * 2550 Garcia Avenue
29 * Mountain View, California 94043
30 */
31
32 #include <sys/cdefs.h>
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "from: @(#)rpc.rstatd.c 1.1 86/09/25 Copyr 1984 Sun Micro";
36 static char sccsid[] = "from: @(#)rstat_proc.c 2.2 88/08/01 4.0 RPCSRC";
37 #else
38 __RCSID("$NetBSD: rstat_proc.c,v 1.28 1999/03/24 05:50:50 mrg Exp $");
39 #endif
40 #endif
41
42 /*
43 * rstat service: built with rstat.x and derived from rpc.rstatd.c
44 *
45 * Copyright (c) 1984 by Sun Microsystems, Inc.
46 */
47
48 #include <sys/param.h>
49 #include <sys/socket.h>
50
51 #include <errno.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <signal.h>
56 #include <fcntl.h>
57 #include <kvm.h>
58 #include <limits.h>
59 #include <nlist.h>
60 #include <syslog.h>
61 #ifdef BSD
62 #include <sys/sysctl.h>
63 #include <vm/vm.h>
64 #include <uvm/uvm_extern.h>
65 #include <sys/dkstat.h>
66 #include "dkstats.h"
67 #else
68 #include <sys/dk.h>
69 #endif
70
71 #include <net/if.h>
72
73 /*
74 * XXX
75 *
76 * this is a huge hack to stop `struct pmap' being
77 * defined twice!
78 */
79 #define _RPC_PMAP_PROT_H_
80 #include <rpc/rpc.h>
81
82 #undef FSHIFT /* Use protocol's shift and scale values */
83 #undef FSCALE
84 #undef DK_NDRIVE
85 #undef CPUSTATES
86 #undef if_ipackets
87 #undef if_ierrors
88 #undef if_opackets
89 #undef if_oerrors
90 #undef if_collisions
91 #include <rpcsvc/rstat.h>
92
93 #ifdef BSD
94 #define BSD_CPUSTATES 5 /* Use protocol's idea of CPU states */
95 int cp_xlat[CPUSTATES] = { CP_USER, CP_NICE, CP_SYS, CP_IDLE };
96 #endif
97
98 struct nlist nl[] = {
99 #define X_IFNET 0
100 { "_ifnet" },
101 { NULL },
102 };
103
104 extern int dk_ndrive; /* From dkstats.c */
105 extern struct _disk cur, last;
106 int hz;
107 char *memf = NULL, *nlistf = NULL;
108
109 struct ifnet_head ifnetq; /* chain of ethernet interfaces */
110 int numintfs;
111
112 extern int from_inetd;
113 int sincelastreq = 0; /* number of alarms since last request */
114 extern int closedown;
115 kvm_t *kfd;
116
117 union {
118 struct stats s1;
119 struct statsswtch s2;
120 struct statstime s3;
121 } stats_all;
122
123 extern void dkreadstats __P((void));
124 extern int dkinit __P((int, gid_t));
125
126 void updatestat __P((int));
127 void setup __P((void));
128 void stat_init __P((void));
129 int havedisk __P((void));
130 void rstat_service __P((struct svc_req *, SVCXPRT *));
131
132 static int stat_is_init = 0;
133
134 #ifndef FSCALE
135 #define FSCALE (1 << 8)
136 #endif
137
138 void
139 stat_init()
140 {
141 stat_is_init = 1;
142 setup();
143 updatestat(0);
144 (void) signal(SIGALRM, updatestat);
145 alarm(1);
146 }
147
148 statstime *
149 rstatproc_stats_3_svc(arg, rqstp)
150 void *arg;
151 struct svc_req *rqstp;
152 {
153 if (!stat_is_init)
154 stat_init();
155 sincelastreq = 0;
156 return (&stats_all.s3);
157 }
158
159 statsswtch *
160 rstatproc_stats_2_svc(arg, rqstp)
161 void *arg;
162 struct svc_req *rqstp;
163 {
164 if (!stat_is_init)
165 stat_init();
166 sincelastreq = 0;
167 stats_all.s2.if_opackets = stats_all.s3.if_opackets;
168 return (&stats_all.s2);
169 }
170
171 stats *
172 rstatproc_stats_1_svc(arg, rqstp)
173 void *arg;
174 struct svc_req *rqstp;
175 {
176 if (!stat_is_init)
177 stat_init();
178 sincelastreq = 0;
179 stats_all.s1.if_opackets = stats_all.s3.if_opackets;
180 return (&stats_all.s1);
181 }
182
183 u_int *
184 rstatproc_havedisk_3_svc(arg, rqstp)
185 void *arg;
186 struct svc_req *rqstp;
187 {
188 static u_int have;
189
190 if (!stat_is_init)
191 stat_init();
192 sincelastreq = 0;
193 have = havedisk();
194 return (&have);
195 }
196
197 u_int *
198 rstatproc_havedisk_2_svc(arg, rqstp)
199 void *arg;
200 struct svc_req *rqstp;
201 {
202 return (rstatproc_havedisk_3_svc(arg, rqstp));
203 }
204
205 u_int *
206 rstatproc_havedisk_1_svc(arg, rqstp)
207 void *arg;
208 struct svc_req *rqstp;
209 {
210 return (rstatproc_havedisk_3_svc(arg, rqstp));
211 }
212
213 void
214 updatestat(dummy)
215 int dummy;
216 {
217 long off;
218 int i;
219 size_t len;
220 int mib[2];
221 struct uvmexp uvmexp;
222 struct ifnet ifnet;
223 double avrun[3];
224 struct timeval tm, btm;
225
226 #ifdef DEBUG
227 syslog(LOG_DEBUG, "entering updatestat");
228 #endif
229 if (sincelastreq >= closedown) {
230 #ifdef DEBUG
231 syslog(LOG_DEBUG, "about to closedown");
232 #endif
233 if (from_inetd)
234 exit(0);
235 else {
236 stat_is_init = 0;
237 return;
238 }
239 }
240 sincelastreq++;
241
242 /*
243 * dkreadstats reads in the "disk_count" as well as the "disklist"
244 * statistics. It also retrieves "hz" and the "cp_time" array.
245 */
246 dkreadstats();
247 memset(stats_all.s3.dk_xfer, 0, sizeof(stats_all.s3.dk_xfer));
248 for (i = 0; i < dk_ndrive && i < DK_NDRIVE; i++)
249 stats_all.s3.dk_xfer[i] = cur.dk_xfer[i];
250
251 #ifdef BSD
252 for (i = 0; i < CPUSTATES; i++)
253 stats_all.s3.cp_time[i] = cur.cp_time[cp_xlat[i]];
254 #else
255 if (kvm_read(kfd, (long)nl[X_CPTIME].n_value,
256 (char *)stats_all.s3.cp_time,
257 sizeof (stats_all.s3.cp_time))
258 != sizeof (stats_all.s3.cp_time)) {
259 syslog(LOG_ERR, "can't read cp_time from kmem");
260 exit(1);
261 }
262 #endif
263 #ifdef BSD
264 (void)getloadavg(avrun, sizeof(avrun) / sizeof(avrun[0]));
265 #endif
266 stats_all.s3.avenrun[0] = avrun[0] * FSCALE;
267 stats_all.s3.avenrun[1] = avrun[1] * FSCALE;
268 stats_all.s3.avenrun[2] = avrun[2] * FSCALE;
269 mib[0] = CTL_KERN;
270 mib[1] = KERN_BOOTTIME;
271 len = sizeof(btm);
272 if (sysctl(mib, 2, &btm, &len, NULL, 0) < 0) {
273 syslog(LOG_ERR, "can't sysctl kern.boottime");
274 exit(1);
275 }
276 stats_all.s3.boottime.tv_sec = btm.tv_sec;
277 stats_all.s3.boottime.tv_usec = btm.tv_usec;
278
279
280 #ifdef DEBUG
281 syslog(LOG_DEBUG, "%d %d %d %d\n", stats_all.s3.cp_time[0],
282 stats_all.s3.cp_time[1], stats_all.s3.cp_time[2], stats_all.s3.cp_time[3]);
283 #endif
284
285 mib[0] = CTL_VM;
286 mib[1] = VM_UVMEXP;
287 len = sizeof(uvmexp);
288 if (sysctl(mib, 2, &uvmexp, &len, NULL, 0) < 0) {
289 syslog(LOG_ERR, "can't sysctl vm.uvmexp");
290 exit(1);
291 }
292 stats_all.s3.v_pgpgin = uvmexp.fltanget;
293 stats_all.s3.v_pgpgout = uvmexp.pdpageouts;
294 stats_all.s3.v_pswpin = uvmexp.swapins;
295 stats_all.s3.v_pswpout = uvmexp.swapouts;
296 stats_all.s3.v_intr = uvmexp.intrs;
297 stats_all.s3.v_swtch = uvmexp.swtch;
298 gettimeofday(&tm, (struct timezone *) 0);
299 stats_all.s3.v_intr -= hz*(tm.tv_sec - btm.tv_sec) +
300 hz*(tm.tv_usec - btm.tv_usec)/1000000;
301
302 stats_all.s3.if_ipackets = 0;
303 stats_all.s3.if_opackets = 0;
304 stats_all.s3.if_ierrors = 0;
305 stats_all.s3.if_oerrors = 0;
306 stats_all.s3.if_collisions = 0;
307 for (off = (long)ifnetq.tqh_first, i = 0; off && i < numintfs; i++) {
308 if (kvm_read(kfd, off, (char *)&ifnet, sizeof ifnet) !=
309 sizeof ifnet) {
310 syslog(LOG_ERR, "can't read ifnet from kmem");
311 exit(1);
312 }
313 stats_all.s3.if_ipackets += ifnet.if_data.ifi_ipackets;
314 stats_all.s3.if_opackets += ifnet.if_data.ifi_opackets;
315 stats_all.s3.if_ierrors += ifnet.if_data.ifi_ierrors;
316 stats_all.s3.if_oerrors += ifnet.if_data.ifi_oerrors;
317 stats_all.s3.if_collisions += ifnet.if_data.ifi_collisions;
318 off = (long)ifnet.if_list.tqe_next;
319 }
320 gettimeofday((struct timeval *)&stats_all.s3.curtime,
321 (struct timezone *) 0);
322 alarm(1);
323 }
324
325 void
326 setup()
327 {
328 struct ifnet ifnet;
329 long off;
330 char errbuf[_POSIX2_LINE_MAX];
331
332 kfd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf);
333 if (kfd == NULL) {
334 syslog(LOG_ERR, "%s", errbuf);
335 exit (1);
336 }
337
338 if (kvm_nlist(kfd, nl) != 0) {
339 syslog(LOG_ERR, "can't get namelist");
340 exit (1);
341 }
342
343 if (kvm_read(kfd, (long)nl[X_IFNET].n_value, &ifnetq,
344 sizeof ifnetq) != sizeof ifnetq) {
345 syslog(LOG_ERR, "can't read ifnet queue head from kmem");
346 exit(1);
347 }
348
349 numintfs = 0;
350 for (off = (long)ifnetq.tqh_first; off;) {
351 if (kvm_read(kfd, off, (char *)&ifnet, sizeof ifnet) !=
352 sizeof ifnet) {
353 syslog(LOG_ERR, "can't read ifnet from kmem");
354 exit(1);
355 }
356 numintfs++;
357 off = (long)ifnet.if_list.tqe_next;
358 }
359 dkinit(0, getgid());
360 }
361
362 /*
363 * returns true if have a disk
364 */
365 int
366 havedisk()
367 {
368 return dk_ndrive != 0;
369 }
370
371 void
372 rstat_service(rqstp, transp)
373 struct svc_req *rqstp;
374 SVCXPRT *transp;
375 {
376 union {
377 int fill;
378 } argument;
379 char *result;
380 xdrproc_t xdr_argument, xdr_result;
381 char *(*local) __P((void *, struct svc_req *));
382
383 switch (rqstp->rq_proc) {
384 case NULLPROC:
385 (void)svc_sendreply(transp, xdr_void, (char *)NULL);
386 goto leave;
387
388 case RSTATPROC_STATS:
389 xdr_argument = (xdrproc_t)xdr_void;
390 xdr_result = (xdrproc_t)xdr_statstime;
391 switch (rqstp->rq_vers) {
392 case RSTATVERS_ORIG:
393 local = (char *(*) __P((void *, struct svc_req *)))
394 rstatproc_stats_1_svc;
395 break;
396 case RSTATVERS_SWTCH:
397 local = (char *(*) __P((void *, struct svc_req *)))
398 rstatproc_stats_2_svc;
399 break;
400 case RSTATVERS_TIME:
401 local = (char *(*) __P((void *, struct svc_req *)))
402 rstatproc_stats_3_svc;
403 break;
404 default:
405 svcerr_progvers(transp, RSTATVERS_ORIG, RSTATVERS_TIME);
406 goto leave;
407 }
408 break;
409
410 case RSTATPROC_HAVEDISK:
411 xdr_argument = (xdrproc_t)xdr_void;
412 xdr_result = (xdrproc_t)xdr_u_int;
413 switch (rqstp->rq_vers) {
414 case RSTATVERS_ORIG:
415 local = (char *(*) __P((void *, struct svc_req *)))
416 rstatproc_havedisk_1_svc;
417 break;
418 case RSTATVERS_SWTCH:
419 local = (char *(*) __P((void *, struct svc_req *)))
420 rstatproc_havedisk_2_svc;
421 break;
422 case RSTATVERS_TIME:
423 local = (char *(*) __P((void *, struct svc_req *)))
424 rstatproc_havedisk_3_svc;
425 break;
426 default:
427 svcerr_progvers(transp, RSTATVERS_ORIG, RSTATVERS_TIME);
428 goto leave;
429 }
430 break;
431
432 default:
433 svcerr_noproc(transp);
434 goto leave;
435 }
436 memset((char *)&argument, 0, sizeof(argument));
437 if (!svc_getargs(transp, xdr_argument, (caddr_t)&argument)) {
438 svcerr_decode(transp);
439 goto leave;
440 }
441 result = (*local)(&argument, rqstp);
442 if (result != NULL && !svc_sendreply(transp, xdr_result, result)) {
443 svcerr_systemerr(transp);
444 }
445 if (!svc_freeargs(transp, xdr_argument, (caddr_t)&argument)) {
446 (void)fprintf(stderr, "unable to free arguments\n");
447 exit(1);
448 }
449 leave:
450 if (from_inetd)
451 exit(0);
452 }
453