1 /* $OpenBSD: rstat_proc.c,v 1.26 2004/09/15 19:05:35 deraadt 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 #ifndef lint
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 static char rcsid[] = "$OpenBSD: rstat_proc.c,v 1.26 2004/09/15 19:05:35 deraadt Exp $";
35 #endif
36
37 /*
38 * rstat service: built with rstat.x and derived from rpc.rstatd.c
39 *
40 * Copyright (c) 1984 by Sun Microsystems, Inc.
41 */
42
43 #include <sys/param.h>
44 #include <sys/vmmeter.h>
45 #include <sys/dkstat.h>
46 #include <sys/socket.h>
47 #include <sys/sysctl.h>
48 #include <net/if.h>
49 #include <uvm/uvm_extern.h>
50
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
54 #include <signal.h>
55 #include <syslog.h>
56 #include <fcntl.h>
57 #include <limits.h>
58 #include <errno.h>
59 #include <ifaddrs.h>
60 #include "dkstats.h"
61
62 #undef FSHIFT /* Use protocol's shift and scale values */
63 #undef FSCALE
64 #undef DK_NDRIVE
65 #undef CPUSTATES
66 #undef if_ipackets
67 #undef if_ierrors
68 #undef if_opackets
69 #undef if_oerrors
70 #undef if_collisions
71 #include <rpcsvc/rstat.h>
72
73 int cp_xlat[CPUSTATES] = { CP_USER, CP_NICE, CP_SYS, CP_IDLE };
74
75 extern int dk_ndrive; /* from dkstats.c */
76 extern struct _disk cur, last;
77 char *memf = NULL, *nlistf = NULL;
78 int hz;
79
80 extern int from_inetd;
81 int sincelastreq = 0; /* number of alarms since last request */
82 extern int closedown;
83
84 union {
85 struct stats s1;
86 struct statsswtch s2;
87 struct statstime s3;
88 } stats_all;
89
90 void updatestat(void);
91 void updatestatsig(int sig);
92 void setup(void);
93
94 volatile sig_atomic_t wantupdatestat;
95
96 static int stat_is_init = 0;
97
98 #ifndef FSCALE
99 #define FSCALE (1 << 8)
100 #endif
101
102 static void
stat_init(void)103 stat_init(void)
104 {
105 stat_is_init = 1;
106 setup();
107 updatestat();
108 (void) signal(SIGALRM, updatestatsig);
109 alarm(1);
110 }
111
112 statstime *
rstatproc_stats_3_svc(void * arg,struct svc_req * rqstp)113 rstatproc_stats_3_svc(void *arg, struct svc_req *rqstp)
114 {
115 if (!stat_is_init)
116 stat_init();
117 sincelastreq = 0;
118 return (&stats_all.s3);
119 }
120
121 statsswtch *
rstatproc_stats_2_svc(void * arg,struct svc_req * rqstp)122 rstatproc_stats_2_svc(void *arg, struct svc_req *rqstp)
123 {
124 if (!stat_is_init)
125 stat_init();
126 sincelastreq = 0;
127 return (&stats_all.s2);
128 }
129
130 stats *
rstatproc_stats_1_svc(void * arg,struct svc_req * rqstp)131 rstatproc_stats_1_svc(void *arg, struct svc_req *rqstp)
132 {
133 if (!stat_is_init)
134 stat_init();
135 sincelastreq = 0;
136 return (&stats_all.s1);
137 }
138
139 u_int *
rstatproc_havedisk_3_svc(void * arg,struct svc_req * rqstp)140 rstatproc_havedisk_3_svc(void *arg, struct svc_req *rqstp)
141 {
142 static u_int have;
143
144 if (!stat_is_init)
145 stat_init();
146 sincelastreq = 0;
147 have = dk_ndrive != 0;
148 return (&have);
149 }
150
151 u_int *
rstatproc_havedisk_2_svc(void * arg,struct svc_req * rqstp)152 rstatproc_havedisk_2_svc(void *arg, struct svc_req *rqstp)
153 {
154 return (rstatproc_havedisk_3_svc(arg, rqstp));
155 }
156
157 u_int *
rstatproc_havedisk_1_svc(void * arg,struct svc_req * rqstp)158 rstatproc_havedisk_1_svc(void *arg, struct svc_req *rqstp)
159 {
160 return (rstatproc_havedisk_3_svc(arg, rqstp));
161 }
162
163 /* ARGSUSED */
164 void
updatestatsig(int sig)165 updatestatsig(int sig)
166 {
167 wantupdatestat = 1;
168 }
169
170 void
updatestat(void)171 updatestat(void)
172 {
173 int i, mib[2], save_errno = errno;
174 struct uvmexp uvmexp;
175 size_t len;
176 struct if_data *ifdp;
177 struct ifaddrs *ifaddrs, *ifa;
178 double avrun[3];
179 struct timeval tm, btm;
180 long *cp_time = cur.cp_time;
181
182 #ifdef DEBUG
183 syslog(LOG_DEBUG, "entering updatestat");
184 #endif
185 if (sincelastreq >= closedown) {
186 #ifdef DEBUG
187 syslog(LOG_DEBUG, "about to closedown");
188 #endif
189 if (from_inetd)
190 _exit(0);
191 else {
192 stat_is_init = 0;
193 errno = save_errno;
194 return;
195 }
196 }
197 sincelastreq++;
198
199 /*
200 * dkreadstats reads in the "disk_count" as well as the "disklist"
201 * statistics. It also retrieves "hz" and the "cp_time" array.
202 */
203 dkreadstats();
204 memset(stats_all.s1.dk_xfer, '\0', sizeof(stats_all.s1.dk_xfer));
205 for (i = 0; i < dk_ndrive && i < DK_NDRIVE; i++)
206 stats_all.s1.dk_xfer[i] = cur.dk_rxfer[i] + cur.dk_wxfer[i];
207
208 for (i = 0; i < CPUSTATES; i++)
209 stats_all.s1.cp_time[i] = cp_time[cp_xlat[i]];
210 (void)getloadavg(avrun, sizeof(avrun) / sizeof(avrun[0]));
211 stats_all.s2.avenrun[0] = avrun[0] * FSCALE;
212 stats_all.s2.avenrun[1] = avrun[1] * FSCALE;
213 stats_all.s2.avenrun[2] = avrun[2] * FSCALE;
214 mib[0] = CTL_KERN;
215 mib[1] = KERN_BOOTTIME;
216 len = sizeof(btm);
217 if (sysctl(mib, 2, &btm, &len, NULL, 0) < 0) {
218 syslog(LOG_ERR, "can't sysctl kern.boottime: %m");
219 _exit(1);
220 }
221 stats_all.s2.boottime.tv_sec = btm.tv_sec;
222 stats_all.s2.boottime.tv_usec = btm.tv_usec;
223
224
225 #ifdef DEBUG
226 syslog(LOG_DEBUG, "%d %d %d %d", stats_all.s1.cp_time[0],
227 stats_all.s1.cp_time[1], stats_all.s1.cp_time[2],
228 stats_all.s1.cp_time[3]);
229 #endif
230
231 mib[0] = CTL_VM;
232 mib[1] = VM_UVMEXP;
233 len = sizeof(uvmexp);
234 if (sysctl(mib, 2, &uvmexp, &len, NULL, 0) < 0) {
235 syslog(LOG_ERR, "can't sysctl vm.uvmexp: %m");
236 _exit(1);
237 }
238 stats_all.s1.v_pgpgin = uvmexp.fltanget;
239 stats_all.s1.v_pgpgout = uvmexp.pdpageouts;
240 stats_all.s1.v_pswpin = uvmexp.swapins;
241 stats_all.s1.v_pswpout = uvmexp.swapouts;
242 stats_all.s1.v_intr = uvmexp.intrs;
243 stats_all.s2.v_swtch = uvmexp.swtch;
244 gettimeofday(&tm, (struct timezone *) 0);
245 stats_all.s1.v_intr -= hz*(tm.tv_sec - btm.tv_sec) +
246 hz*(tm.tv_usec - btm.tv_usec)/1000000;
247 stats_all.s1.if_ipackets = 0;
248 stats_all.s1.if_opackets = 0;
249 stats_all.s1.if_ierrors = 0;
250 stats_all.s1.if_oerrors = 0;
251 stats_all.s1.if_collisions = 0;
252 if (getifaddrs(&ifaddrs) == -1) {
253 syslog(LOG_ERR, "can't getifaddrs: %m");
254 _exit(1);
255 }
256 for (ifa = ifaddrs; ifa != NULL; ifa = ifa->ifa_next) {
257 if (ifa->ifa_addr->sa_family != AF_LINK)
258 continue;
259 ifdp = (struct if_data *)ifa->ifa_data;
260 stats_all.s1.if_ipackets += ifdp->ifi_ipackets;
261 stats_all.s1.if_opackets += ifdp->ifi_opackets;
262 stats_all.s1.if_ierrors += ifdp->ifi_ierrors;
263 stats_all.s1.if_oerrors += ifdp->ifi_oerrors;
264 stats_all.s1.if_collisions += ifdp->ifi_collisions;
265 }
266 freeifaddrs(ifaddrs);
267 stats_all.s3.curtime.tv_sec = tm.tv_sec;
268 stats_all.s3.curtime.tv_usec = tm.tv_usec;
269
270 alarm(1);
271 errno = save_errno;
272 }
273
274 void
setup(void)275 setup(void)
276 {
277 dkinit(0);
278 }
279
280 void rstat_service(struct svc_req *, SVCXPRT *);
281
282 void
rstat_service(struct svc_req * rqstp,SVCXPRT * transp)283 rstat_service(struct svc_req *rqstp, SVCXPRT *transp)
284 {
285 char *(*local)(void *, struct svc_req *);
286 xdrproc_t xdr_argument, xdr_result;
287 union {
288 int fill;
289 } argument;
290 char *result;
291
292 switch (rqstp->rq_proc) {
293 case NULLPROC:
294 (void)svc_sendreply(transp, xdr_void, (char *)NULL);
295 return;
296
297 case RSTATPROC_STATS:
298 xdr_argument = (xdrproc_t)xdr_void;
299 xdr_result = (xdrproc_t)xdr_statstime;
300 switch (rqstp->rq_vers) {
301 case RSTATVERS_ORIG:
302 local = (char *(*)(void *, struct svc_req *))
303 rstatproc_stats_1_svc;
304 break;
305 case RSTATVERS_SWTCH:
306 local = (char *(*)(void *, struct svc_req *))
307 rstatproc_stats_2_svc;
308 break;
309 case RSTATVERS_TIME:
310 local = (char *(*)(void *, struct svc_req *))
311 rstatproc_stats_3_svc;
312 break;
313 default:
314 svcerr_progvers(transp, RSTATVERS_ORIG, RSTATVERS_TIME);
315 return;
316 }
317 break;
318
319 case RSTATPROC_HAVEDISK:
320 xdr_argument = (xdrproc_t)xdr_void;
321 xdr_result = (xdrproc_t)xdr_u_int;
322 switch (rqstp->rq_vers) {
323 case RSTATVERS_ORIG:
324 local = (char *(*)(void *, struct svc_req *))
325 rstatproc_havedisk_1_svc;
326 break;
327 case RSTATVERS_SWTCH:
328 local = (char *(*)(void *, struct svc_req *))
329 rstatproc_havedisk_2_svc;
330 break;
331 case RSTATVERS_TIME:
332 local = (char *(*)(void *, struct svc_req *))
333 rstatproc_havedisk_3_svc;
334 break;
335 default:
336 svcerr_progvers(transp, RSTATVERS_ORIG, RSTATVERS_TIME);
337 return;
338 }
339 break;
340
341 default:
342 svcerr_noproc(transp);
343 return;
344 }
345 bzero((char *)&argument, sizeof(argument));
346 if (!svc_getargs(transp, xdr_argument, (caddr_t)&argument)) {
347 svcerr_decode(transp);
348 return;
349 }
350 result = (*local)(&argument, rqstp);
351 if (result != NULL && !svc_sendreply(transp, xdr_result, result)) {
352 svcerr_systemerr(transp);
353 }
354 if (!svc_freeargs(transp, xdr_argument, (caddr_t)&argument)) {
355 syslog(LOG_ERR, "unable to free arguments");
356 exit(1);
357 }
358 }
359