]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - libexec/rpc.rstatd/rstat_proc.c
ping(8): Fix a mandoc related issue
[FreeBSD/FreeBSD.git] / libexec / rpc.rstatd / rstat_proc.c
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   "$FreeBSD$";
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/socket.h>
47 #include <sys/sysctl.h>
48 #include <sys/time.h>
49 #include <sys/resource.h>
50 #include <sys/param.h>
51
52 #include <err.h>
53 #include <errno.h>
54 #include <fcntl.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 #include <devstat.h>
63
64 #include <net/if.h>
65 #include <net/if_mib.h>
66
67 #undef FSHIFT                    /* Use protocol's shift and scale values */
68 #undef FSCALE
69 #undef if_ipackets
70 #undef if_ierrors
71 #undef if_opackets
72 #undef if_oerrors
73 #undef if_collisions
74 #include <rpcsvc/rstat.h>
75
76 int haveadisk(void);
77 void updatexfers(int, int *);
78 int stats_service(void);
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();
91 static int stat_is_init = 0;
92
93 static int      cp_time_xlat[RSTAT_CPUSTATES] = { CP_USER, CP_NICE, CP_SYS,
94                                                         CP_IDLE };
95 static long     bsd_cp_time[CPUSTATES];
96
97
98 #ifndef FSCALE
99 #define FSCALE (1 << 8)
100 #endif
101
102 void
103 stat_init(void)
104 {
105     stat_is_init = 1;
106     alarm(0);
107     updatestat();
108     (void) signal(SIGALRM, updatestat);
109     alarm(1);
110 }
111
112 statstime *
113 rstatproc_stats_3_svc(void *argp, 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 *
122 rstatproc_stats_2_svc(void *argp, 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 *
131 rstatproc_stats_1_svc(void *argp, 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 *
140 rstatproc_havedisk_3_svc(void *argp, struct svc_req *rqstp)
141 {
142     static u_int have;
143
144     if (! stat_is_init)
145         stat_init();
146     sincelastreq = 0;
147     have = haveadisk();
148         return(&have);
149 }
150
151 u_int *
152 rstatproc_havedisk_2_svc(void *argp, struct svc_req *rqstp)
153 {
154     return(rstatproc_havedisk_3_svc(argp, rqstp));
155 }
156
157 u_int *
158 rstatproc_havedisk_1_svc(void *argp, struct svc_req *rqstp)
159 {
160     return(rstatproc_havedisk_3_svc(argp, rqstp));
161 }
162
163 void
164 updatestat(void)
165 {
166         int i, hz;
167         struct clockinfo clockrate;
168         struct ifmibdata ifmd;
169         double avrun[3];
170         struct timeval tm, btm;
171         int mib[6];
172         size_t len;
173         uint64_t val;
174         int ifcount;
175
176 #ifdef DEBUG
177         fprintf(stderr, "entering updatestat\n");
178 #endif
179         if (sincelastreq >= closedown) {
180 #ifdef DEBUG
181                 fprintf(stderr, "about to closedown\n");
182 #endif
183                 if (from_inetd)
184                         exit(0);
185                 else {
186                         stat_is_init = 0;
187                         return;
188                 }
189         }
190         sincelastreq++;
191
192         mib[0] = CTL_KERN;
193         mib[1] = KERN_CLOCKRATE;
194         len = sizeof clockrate;
195         if (sysctl(mib, 2, &clockrate, &len, 0, 0) < 0) {
196                 syslog(LOG_ERR, "sysctl(kern.clockrate): %m");
197                 exit(1);
198         }
199         hz = clockrate.hz;
200
201         len = sizeof(bsd_cp_time);
202         if (sysctlbyname("kern.cp_time", bsd_cp_time, &len, 0, 0) < 0) {
203                 syslog(LOG_ERR, "sysctl(kern.cp_time): %m");
204                 exit(1);
205         }
206         for(i = 0; i < RSTAT_CPUSTATES ; i++)
207                 stats_all.s1.cp_time[i] = bsd_cp_time[cp_time_xlat[i]];
208
209         (void)getloadavg(avrun, sizeof(avrun) / sizeof(avrun[0]));
210
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
215         mib[0] = CTL_KERN;
216         mib[1] = KERN_BOOTTIME;
217         len = sizeof btm;
218         if (sysctl(mib, 2, &btm, &len, 0, 0) < 0) {
219                 syslog(LOG_ERR, "sysctl(kern.boottime): %m");
220                 exit(1);
221         }
222
223         stats_all.s2.boottime.tv_sec = btm.tv_sec;
224         stats_all.s2.boottime.tv_usec = btm.tv_usec;
225
226
227 #ifdef DEBUG
228         fprintf(stderr, "%d %d %d %d\n", stats_all.s1.cp_time[0],
229             stats_all.s1.cp_time[1], stats_all.s1.cp_time[2], stats_all.s1.cp_time[3]);
230 #endif
231
232 #define FETCH_CNT(stat, cnt) do {                                       \
233         len = sizeof(uint64_t);                                         \
234         if (sysctlbyname("vm.stats." #cnt , &val, &len, NULL, 0) < 0) { \
235                 syslog(LOG_ERR, "sysctl(vm.stats." #cnt "): %m");       \
236                 exit(1);                                                \
237         }                                                               \
238         stat = val;                                                     \
239 } while (0)
240
241         FETCH_CNT(stats_all.s1.v_pgpgin, vm.v_vnodepgsin);
242         FETCH_CNT(stats_all.s1.v_pgpgout, vm.v_vnodepgsout);
243         FETCH_CNT(stats_all.s1.v_pswpin, vm.v_swappgsin);
244         FETCH_CNT(stats_all.s1.v_pswpout, vm.v_swappgsout);
245         FETCH_CNT(stats_all.s1.v_intr, sys.v_intr);
246         FETCH_CNT(stats_all.s2.v_swtch, sys.v_swtch);
247         (void)gettimeofday(&tm, NULL);
248         stats_all.s1.v_intr -= hz*(tm.tv_sec - btm.tv_sec) +
249             hz*(tm.tv_usec - btm.tv_usec)/1000000;
250
251         /* update disk transfers */
252         updatexfers(RSTAT_DK_NDRIVE, stats_all.s1.dk_xfer);
253
254         mib[0] = CTL_NET;
255         mib[1] = PF_LINK;
256         mib[2] = NETLINK_GENERIC;
257         mib[3] = IFMIB_SYSTEM;
258         mib[4] = IFMIB_IFCOUNT;
259         len = sizeof ifcount;
260         if (sysctl(mib, 5, &ifcount, &len, 0, 0) < 0) {
261                 syslog(LOG_ERR, "sysctl(net.link.generic.system.ifcount): %m");
262                 exit(1);
263         }
264
265         stats_all.s1.if_ipackets = 0;
266         stats_all.s1.if_opackets = 0;
267         stats_all.s1.if_ierrors = 0;
268         stats_all.s1.if_oerrors = 0;
269         stats_all.s1.if_collisions = 0;
270         for (i = 1; i <= ifcount; i++) {
271                 len = sizeof ifmd;
272                 mib[3] = IFMIB_IFDATA;
273                 mib[4] = i;
274                 mib[5] = IFDATA_GENERAL;
275                 if (sysctl(mib, 6, &ifmd, &len, 0, 0) < 0) {
276                         if (errno == ENOENT)
277                                 continue;
278
279                         syslog(LOG_ERR, "sysctl(net.link.ifdata.%d.general)"
280                                ": %m", i);
281                         exit(1);
282                 }
283
284                 stats_all.s1.if_ipackets += ifmd.ifmd_data.ifi_ipackets;
285                 stats_all.s1.if_opackets += ifmd.ifmd_data.ifi_opackets;
286                 stats_all.s1.if_ierrors += ifmd.ifmd_data.ifi_ierrors;
287                 stats_all.s1.if_oerrors += ifmd.ifmd_data.ifi_oerrors;
288                 stats_all.s1.if_collisions += ifmd.ifmd_data.ifi_collisions;
289         }
290         (void)gettimeofday(&tm, NULL);
291         stats_all.s3.curtime.tv_sec = tm.tv_sec;
292         stats_all.s3.curtime.tv_usec = tm.tv_usec;
293         alarm(1);
294 }
295
296 /*
297  * returns true if have a disk
298  */
299 int
300 haveadisk(void)
301 {
302         register int i;
303         struct statinfo stats;
304         int num_devices, retval = 0;
305
306         if ((num_devices = devstat_getnumdevs(NULL)) < 0) {
307                 syslog(LOG_ERR, "rstatd: can't get number of devices: %s",
308                        devstat_errbuf);
309                 exit(1);
310         }
311
312         if (devstat_checkversion(NULL) < 0) {
313                 syslog(LOG_ERR, "rstatd: %s", devstat_errbuf);
314                 exit(1);
315         }
316
317         stats.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo));
318         bzero(stats.dinfo, sizeof(struct devinfo));
319
320         if (devstat_getdevs(NULL, &stats) == -1) {
321                 syslog(LOG_ERR, "rstatd: can't get device list: %s",
322                        devstat_errbuf);
323                 exit(1);
324         }
325         for (i = 0; i < stats.dinfo->numdevs; i++) {
326                 if (((stats.dinfo->devices[i].device_type
327                       & DEVSTAT_TYPE_MASK) == DEVSTAT_TYPE_DIRECT)
328                  && ((stats.dinfo->devices[i].device_type
329                       & DEVSTAT_TYPE_PASS) == 0)) {
330                         retval = 1;
331                         break;
332                 }
333         }
334
335         if (stats.dinfo->mem_ptr)
336                 free(stats.dinfo->mem_ptr);
337
338         free(stats.dinfo);
339         return(retval);
340 }
341
342 void
343 updatexfers(int numdevs, int *devs)
344 {
345         register int i, j, k, t;
346         struct statinfo stats;
347         int num_devices = 0;
348         u_int64_t total_transfers;
349
350         if ((num_devices = devstat_getnumdevs(NULL)) < 0) {
351                 syslog(LOG_ERR, "rstatd: can't get number of devices: %s",
352                        devstat_errbuf);
353                 exit(1);
354         }
355
356         if (devstat_checkversion(NULL) < 0) {
357                 syslog(LOG_ERR, "rstatd: %s", devstat_errbuf);
358                 exit(1);
359         }
360
361         stats.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo));
362         bzero(stats.dinfo, sizeof(struct devinfo));
363
364         if (devstat_getdevs(NULL, &stats) == -1) {
365                 syslog(LOG_ERR, "rstatd: can't get device list: %s",
366                        devstat_errbuf);
367                 exit(1);
368         }
369
370         for (i = 0, j = 0; i < stats.dinfo->numdevs && j < numdevs; i++) {
371                 if (((stats.dinfo->devices[i].device_type
372                       & DEVSTAT_TYPE_MASK) == DEVSTAT_TYPE_DIRECT)
373                  && ((stats.dinfo->devices[i].device_type
374                       & DEVSTAT_TYPE_PASS) == 0)) {
375                         total_transfers = 0;
376                         for (k = 0; k < DEVSTAT_N_TRANS_FLAGS; k++)
377                                 total_transfers +=
378                                     stats.dinfo->devices[i].operations[k];
379                         /*
380                          * XXX KDM If the total transfers for this device
381                          * are greater than the amount we can fit in a
382                          * signed integer, just set them to the maximum
383                          * amount we can fit in a signed integer.  I have a
384                          * feeling that the rstat protocol assumes 32-bit
385                          * integers, so this could well break on a 64-bit
386                          * architecture like the Alpha.
387                          */
388                         if (total_transfers > INT_MAX)
389                                 t = INT_MAX;
390                         else
391                                 t = total_transfers;
392                         devs[j] = t;
393                         j++;
394                 }
395         }
396
397         if (stats.dinfo->mem_ptr)
398                 free(stats.dinfo->mem_ptr);
399
400         free(stats.dinfo);
401 }
402
403 void
404 rstat_service(struct svc_req *rqstp, SVCXPRT *transp)
405 {
406         union {
407                 int fill;
408         } argument;
409         char *result;
410         bool_t (*xdr_argument)(), (*xdr_result)();
411         char *(*local)();
412
413         switch (rqstp->rq_proc) {
414         case NULLPROC:
415                 (void)svc_sendreply(transp, (xdrproc_t)xdr_void, NULL);
416                 goto leave;
417
418         case RSTATPROC_STATS:
419                 xdr_argument = xdr_void;
420                 xdr_result = xdr_statstime;
421                 switch (rqstp->rq_vers) {
422                 case RSTATVERS_ORIG:
423                         local = (char *(*)()) rstatproc_stats_1_svc;
424                         break;
425                 case RSTATVERS_SWTCH:
426                         local = (char *(*)()) rstatproc_stats_2_svc;
427                         break;
428                 case RSTATVERS_TIME:
429                         local = (char *(*)()) rstatproc_stats_3_svc;
430                         break;
431                 default:
432                         svcerr_progvers(transp, RSTATVERS_ORIG, RSTATVERS_TIME);
433                         goto leave;
434                         /*NOTREACHED*/
435                 }
436                 break;
437
438         case RSTATPROC_HAVEDISK:
439                 xdr_argument = xdr_void;
440                 xdr_result = xdr_u_int;
441                 switch (rqstp->rq_vers) {
442                 case RSTATVERS_ORIG:
443                         local = (char *(*)()) rstatproc_havedisk_1_svc;
444                         break;
445                 case RSTATVERS_SWTCH:
446                         local = (char *(*)()) rstatproc_havedisk_2_svc;
447                         break;
448                 case RSTATVERS_TIME:
449                         local = (char *(*)()) rstatproc_havedisk_3_svc;
450                         break;
451                 default:
452                         svcerr_progvers(transp, RSTATVERS_ORIG, RSTATVERS_TIME);
453                         goto leave;
454                         /*NOTREACHED*/
455                 }
456                 break;
457
458         default:
459                 svcerr_noproc(transp);
460                 goto leave;
461         }
462         bzero((char *)&argument, sizeof(argument));
463         if (!svc_getargs(transp, (xdrproc_t)xdr_argument, &argument)) {
464                 svcerr_decode(transp);
465                 goto leave;
466         }
467         result = (*local)(&argument, rqstp);
468         if (result != NULL &&
469             !svc_sendreply(transp, (xdrproc_t)xdr_result, result)) {
470                 svcerr_systemerr(transp);
471         }
472         if (!svc_freeargs(transp, (xdrproc_t)xdr_argument, &argument))
473                 errx(1, "unable to free arguments");
474 leave:
475         if (from_inetd)
476                 exit(0);
477 }