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