rstat_proc.c revision 1.27 1 /* $NetBSD: rstat_proc.c,v 1.27 1999/01/11 22:40:00 kleink 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.27 1999/01/11 22:40:00 kleink 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 #if defined(UVM)
64 #include <vm/vm.h>
65 #include <uvm/uvm_extern.h>
66 #else
67 #include <sys/vmmeter.h>
68 #endif
69 #include <sys/dkstat.h>
70 #include "dkstats.h"
71 #else
72 #include <sys/dk.h>
73 #endif
74
75 #include <net/if.h>
76
77 /*
78 * XXX
79 *
80 * this is a huge hack to stop `struct pmap' being
81 * defined twice!
82 */
83 #define _RPC_PMAP_PROT_H_
84 #include <rpc/rpc.h>
85
86 #undef FSHIFT /* Use protocol's shift and scale values */
87 #undef FSCALE
88 #undef DK_NDRIVE
89 #undef CPUSTATES
90 #undef if_ipackets
91 #undef if_ierrors
92 #undef if_opackets
93 #undef if_oerrors
94 #undef if_collisions
95 #include <rpcsvc/rstat.h>
96
97 #ifdef BSD
98 #define BSD_CPUSTATES 5 /* Use protocol's idea of CPU states */
99 int cp_xlat[CPUSTATES] = { CP_USER, CP_NICE, CP_SYS, CP_IDLE };
100 #endif
101
102 struct nlist nl[] = {
103 #define X_IFNET 0
104 { "_ifnet" },
105 #if !defined(UVM)
106 #define X_CNT 1
107 { "_cnt" },
108 #endif
109 { NULL },
110 };
111
112 extern int dk_ndrive; /* From dkstats.c */
113 extern struct _disk cur, last;
114 int hz;
115 char *memf = NULL, *nlistf = NULL;
116
117 struct ifnet_head ifnetq; /* chain of ethernet interfaces */
118 int numintfs;
119
120 extern int from_inetd;
121 int sincelastreq = 0; /* number of alarms since last request */
122 extern int closedown;
123 kvm_t *kfd;
124
125 union {
126 struct stats s1;
127 struct statsswtch s2;
128 struct statstime s3;
129 } stats_all;
130
131 extern void dkreadstats __P((void));
132 extern int dkinit __P((int, gid_t));
133
134 void updatestat __P((int));
135 void setup __P((void));
136 void stat_init __P((void));
137 int havedisk __P((void));
138 void rstat_service __P((struct svc_req *, SVCXPRT *));
139
140 static int stat_is_init = 0;
141
142 #ifndef FSCALE
143 #define FSCALE (1 << 8)
144 #endif
145
146 void
147 stat_init()
148 {
149 stat_is_init = 1;
150 setup();
151 updatestat(0);
152 (void) signal(SIGALRM, updatestat);
153 alarm(1);
154 }
155
156 statstime *
157 rstatproc_stats_3_svc(arg, rqstp)
158 void *arg;
159 struct svc_req *rqstp;
160 {
161 if (!stat_is_init)
162 stat_init();
163 sincelastreq = 0;
164 return (&stats_all.s3);
165 }
166
167 statsswtch *
168 rstatproc_stats_2_svc(arg, rqstp)
169 void *arg;
170 struct svc_req *rqstp;
171 {
172 if (!stat_is_init)
173 stat_init();
174 sincelastreq = 0;
175 stats_all.s2.if_opackets = stats_all.s3.if_opackets;
176 return (&stats_all.s2);
177 }
178
179 stats *
180 rstatproc_stats_1_svc(arg, rqstp)
181 void *arg;
182 struct svc_req *rqstp;
183 {
184 if (!stat_is_init)
185 stat_init();
186 sincelastreq = 0;
187 stats_all.s1.if_opackets = stats_all.s3.if_opackets;
188 return (&stats_all.s1);
189 }
190
191 u_int *
192 rstatproc_havedisk_3_svc(arg, rqstp)
193 void *arg;
194 struct svc_req *rqstp;
195 {
196 static u_int have;
197
198 if (!stat_is_init)
199 stat_init();
200 sincelastreq = 0;
201 have = havedisk();
202 return (&have);
203 }
204
205 u_int *
206 rstatproc_havedisk_2_svc(arg, rqstp)
207 void *arg;
208 struct svc_req *rqstp;
209 {
210 return (rstatproc_havedisk_3_svc(arg, rqstp));
211 }
212
213 u_int *
214 rstatproc_havedisk_1_svc(arg, rqstp)
215 void *arg;
216 struct svc_req *rqstp;
217 {
218 return (rstatproc_havedisk_3_svc(arg, rqstp));
219 }
220
221 void
222 updatestat(dummy)
223 int dummy;
224 {
225 long off;
226 int i;
227 size_t len;
228 int mib[2];
229 #if defined(UVM)
230 struct uvmexp uvmexp;
231 #else
232 struct vmmeter cnt;
233 #endif
234 struct ifnet ifnet;
235 double avrun[3];
236 struct timeval tm, btm;
237
238 #ifdef DEBUG
239 syslog(LOG_DEBUG, "entering updatestat");
240 #endif
241 if (sincelastreq >= closedown) {
242 #ifdef DEBUG
243 syslog(LOG_DEBUG, "about to closedown");
244 #endif
245 if (from_inetd)
246 exit(0);
247 else {
248 stat_is_init = 0;
249 return;
250 }
251 }
252 sincelastreq++;
253
254 /*
255 * dkreadstats reads in the "disk_count" as well as the "disklist"
256 * statistics. It also retrieves "hz" and the "cp_time" array.
257 */
258 dkreadstats();
259 memset(stats_all.s3.dk_xfer, 0, sizeof(stats_all.s3.dk_xfer));
260 for (i = 0; i < dk_ndrive && i < DK_NDRIVE; i++)
261 stats_all.s3.dk_xfer[i] = cur.dk_xfer[i];
262
263 #ifdef BSD
264 for (i = 0; i < CPUSTATES; i++)
265 stats_all.s3.cp_time[i] = cur.cp_time[cp_xlat[i]];
266 #else
267 if (kvm_read(kfd, (long)nl[X_CPTIME].n_value,
268 (char *)stats_all.s3.cp_time,
269 sizeof (stats_all.s3.cp_time))
270 != sizeof (stats_all.s3.cp_time)) {
271 syslog(LOG_ERR, "can't read cp_time from kmem");
272 exit(1);
273 }
274 #endif
275 #ifdef BSD
276 (void)getloadavg(avrun, sizeof(avrun) / sizeof(avrun[0]));
277 #endif
278 stats_all.s3.avenrun[0] = avrun[0] * FSCALE;
279 stats_all.s3.avenrun[1] = avrun[1] * FSCALE;
280 stats_all.s3.avenrun[2] = avrun[2] * FSCALE;
281 mib[0] = CTL_KERN;
282 mib[1] = KERN_BOOTTIME;
283 len = sizeof(btm);
284 if (sysctl(mib, 2, &btm, &len, NULL, 0) < 0) {
285 syslog(LOG_ERR, "can't sysctl kern.boottime");
286 exit(1);
287 }
288 stats_all.s3.boottime.tv_sec = btm.tv_sec;
289 stats_all.s3.boottime.tv_usec = btm.tv_usec;
290
291
292 #ifdef DEBUG
293 syslog(LOG_DEBUG, "%d %d %d %d\n", stats_all.s3.cp_time[0],
294 stats_all.s3.cp_time[1], stats_all.s3.cp_time[2], stats_all.s3.cp_time[3]);
295 #endif
296
297 #if defined(UVM)
298 mib[0] = CTL_VM;
299 mib[1] = VM_UVMEXP;
300 len = sizeof(uvmexp);
301 if (sysctl(mib, 2, &uvmexp, &len, NULL, 0) < 0) {
302 syslog(LOG_ERR, "can't sysctl vm.uvmexp");
303 exit(1);
304 }
305 stats_all.s3.v_pgpgin = uvmexp.fltanget;
306 stats_all.s3.v_pgpgout = uvmexp.pdpageouts;
307 stats_all.s3.v_pswpin = uvmexp.swapins;
308 stats_all.s3.v_pswpout = uvmexp.swapouts;
309 stats_all.s3.v_intr = uvmexp.intrs;
310 stats_all.s3.v_swtch = uvmexp.swtch;
311 #else
312 if (kvm_read(kfd, (long)nl[X_CNT].n_value, (char *)&cnt, sizeof cnt) !=
313 sizeof cnt) {
314 syslog(LOG_ERR, "can't read cnt from kmem");
315 exit(1);
316 }
317 stats_all.s3.v_pgpgin = cnt.v_pgpgin;
318 stats_all.s3.v_pgpgout = cnt.v_pgpgout;
319 stats_all.s3.v_pswpin = cnt.v_pswpin;
320 stats_all.s3.v_pswpout = cnt.v_pswpout;
321 stats_all.s3.v_intr = cnt.v_intr;
322 stats_all.s3.v_swtch = cnt.v_swtch;
323 #endif
324 gettimeofday(&tm, (struct timezone *) 0);
325 stats_all.s3.v_intr -= hz*(tm.tv_sec - btm.tv_sec) +
326 hz*(tm.tv_usec - btm.tv_usec)/1000000;
327
328 stats_all.s3.if_ipackets = 0;
329 stats_all.s3.if_opackets = 0;
330 stats_all.s3.if_ierrors = 0;
331 stats_all.s3.if_oerrors = 0;
332 stats_all.s3.if_collisions = 0;
333 for (off = (long)ifnetq.tqh_first, i = 0; off && i < numintfs; i++) {
334 if (kvm_read(kfd, off, (char *)&ifnet, sizeof ifnet) !=
335 sizeof ifnet) {
336 syslog(LOG_ERR, "can't read ifnet from kmem");
337 exit(1);
338 }
339 stats_all.s3.if_ipackets += ifnet.if_data.ifi_ipackets;
340 stats_all.s3.if_opackets += ifnet.if_data.ifi_opackets;
341 stats_all.s3.if_ierrors += ifnet.if_data.ifi_ierrors;
342 stats_all.s3.if_oerrors += ifnet.if_data.ifi_oerrors;
343 stats_all.s3.if_collisions += ifnet.if_data.ifi_collisions;
344 off = (long)ifnet.if_list.tqe_next;
345 }
346 gettimeofday((struct timeval *)&stats_all.s3.curtime,
347 (struct timezone *) 0);
348 alarm(1);
349 }
350
351 void
352 setup()
353 {
354 struct ifnet ifnet;
355 long off;
356 char errbuf[_POSIX2_LINE_MAX];
357
358 kfd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf);
359 if (kfd == NULL) {
360 syslog(LOG_ERR, "%s", errbuf);
361 exit (1);
362 }
363
364 if (kvm_nlist(kfd, nl) != 0) {
365 syslog(LOG_ERR, "can't get namelist");
366 exit (1);
367 }
368
369 if (kvm_read(kfd, (long)nl[X_IFNET].n_value, &ifnetq,
370 sizeof ifnetq) != sizeof ifnetq) {
371 syslog(LOG_ERR, "can't read ifnet queue head from kmem");
372 exit(1);
373 }
374
375 numintfs = 0;
376 for (off = (long)ifnetq.tqh_first; off;) {
377 if (kvm_read(kfd, off, (char *)&ifnet, sizeof ifnet) !=
378 sizeof ifnet) {
379 syslog(LOG_ERR, "can't read ifnet from kmem");
380 exit(1);
381 }
382 numintfs++;
383 off = (long)ifnet.if_list.tqe_next;
384 }
385 dkinit(0, getgid());
386 }
387
388 /*
389 * returns true if have a disk
390 */
391 int
392 havedisk()
393 {
394 return dk_ndrive != 0;
395 }
396
397 void
398 rstat_service(rqstp, transp)
399 struct svc_req *rqstp;
400 SVCXPRT *transp;
401 {
402 union {
403 int fill;
404 } argument;
405 char *result;
406 xdrproc_t xdr_argument, xdr_result;
407 char *(*local) __P((void *, struct svc_req *));
408
409 switch (rqstp->rq_proc) {
410 case NULLPROC:
411 (void)svc_sendreply(transp, xdr_void, (char *)NULL);
412 goto leave;
413
414 case RSTATPROC_STATS:
415 xdr_argument = (xdrproc_t)xdr_void;
416 xdr_result = (xdrproc_t)xdr_statstime;
417 switch (rqstp->rq_vers) {
418 case RSTATVERS_ORIG:
419 local = (char *(*) __P((void *, struct svc_req *)))
420 rstatproc_stats_1_svc;
421 break;
422 case RSTATVERS_SWTCH:
423 local = (char *(*) __P((void *, struct svc_req *)))
424 rstatproc_stats_2_svc;
425 break;
426 case RSTATVERS_TIME:
427 local = (char *(*) __P((void *, struct svc_req *)))
428 rstatproc_stats_3_svc;
429 break;
430 default:
431 svcerr_progvers(transp, RSTATVERS_ORIG, RSTATVERS_TIME);
432 goto leave;
433 }
434 break;
435
436 case RSTATPROC_HAVEDISK:
437 xdr_argument = (xdrproc_t)xdr_void;
438 xdr_result = (xdrproc_t)xdr_u_int;
439 switch (rqstp->rq_vers) {
440 case RSTATVERS_ORIG:
441 local = (char *(*) __P((void *, struct svc_req *)))
442 rstatproc_havedisk_1_svc;
443 break;
444 case RSTATVERS_SWTCH:
445 local = (char *(*) __P((void *, struct svc_req *)))
446 rstatproc_havedisk_2_svc;
447 break;
448 case RSTATVERS_TIME:
449 local = (char *(*) __P((void *, struct svc_req *)))
450 rstatproc_havedisk_3_svc;
451 break;
452 default:
453 svcerr_progvers(transp, RSTATVERS_ORIG, RSTATVERS_TIME);
454 goto leave;
455 }
456 break;
457
458 default:
459 svcerr_noproc(transp);
460 goto leave;
461 }
462 memset((char *)&argument, 0, 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 (void)fprintf(stderr, "unable to free arguments\n");
473 exit(1);
474 }
475 leave:
476 if (from_inetd)
477 exit(0);
478 }
479