Annotation of src/usr.bin/rup/rup.c, Revision 1.24
1.24 ! deraadt 1: /* $OpenBSD: rup.c,v 1.23 2008/07/09 19:41:56 sobrado Exp $ */
1.2 deraadt 2:
1.1 deraadt 3: /*-
4: * Copyright (c) 1993, John Brezak
5: * All rights reserved.
6: *
7: * Redistribution and use in source and binary forms, with or without
8: * modification, are permitted provided that the following conditions
9: * are met:
10: * 1. Redistributions of source code must retain the above copyright
11: * notice, this list of conditions and the following disclaimer.
12: * 2. Redistributions in binary form must reproduce the above copyright
13: * notice, this list of conditions and the following disclaimer in the
14: * documentation and/or other materials provided with the distribution.
1.20 deraadt 15: * 3. The name of the author may not be used to endorse or promote products
16: * derived from this software without specific prior written permission.
1.1 deraadt 17: *
18: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28: * SUCH DAMAGE.
29: */
30:
31: #include <stdio.h>
32: #include <stdlib.h>
33: #include <string.h>
34: #include <time.h>
35: #include <sys/param.h>
36: #include <sys/socket.h>
37: #include <netdb.h>
38: #include <rpc/rpc.h>
1.7 deraadt 39: #include <rpc/pmap_clnt.h>
1.1 deraadt 40: #include <arpa/inet.h>
41: #include <err.h>
42:
43: #undef FSHIFT /* Use protocol's shift and scale values */
44: #undef FSCALE
45: #include <rpcsvc/rstat.h>
46:
1.11 aaron 47: #define HOST_WIDTH 27
1.1 deraadt 48:
49: int printtime; /* print the remote host(s)'s time */
50:
51: struct host_list {
52: struct host_list *next;
53: struct in_addr addr;
54: } *hosts;
55:
1.15 millert 56: void usage(void);
57: int print_rup_data(char *, statstime *host_stat);
1.7 deraadt 58:
1.18 deraadt 59: static int
1.17 deraadt 60: search_host(struct in_addr addr)
1.1 deraadt 61: {
62: struct host_list *hp;
1.22 moritz 63:
1.1 deraadt 64: if (!hosts)
65: return(0);
66:
67: for (hp = hosts; hp != NULL; hp = hp->next) {
68: if (hp->addr.s_addr == addr.s_addr)
69: return(1);
70: }
71: return(0);
72: }
73:
1.18 deraadt 74: static void
1.17 deraadt 75: remember_host(struct in_addr addr)
1.1 deraadt 76: {
77: struct host_list *hp;
78:
79: if (!(hp = (struct host_list *)malloc(sizeof(struct host_list)))) {
80: err(1, NULL);
81: /* NOTREACHED */
82: }
83: hp->addr.s_addr = addr.s_addr;
84: hp->next = hosts;
85: hosts = hp;
86: }
87:
88:
89: struct rup_data {
90: char *host;
91: struct statstime statstime;
92: };
93: struct rup_data *rup_data;
94: int rup_data_idx = 0;
95: int rup_data_max = 0;
96:
1.17 deraadt 97: enum sort_type {
1.1 deraadt 98: SORT_NONE,
99: SORT_HOST,
100: SORT_LDAV,
101: SORT_UPTIME
102: };
103: enum sort_type sort_type;
104:
1.18 deraadt 105: static int
1.17 deraadt 106: compare(const void *v1, const void *v2)
1.1 deraadt 107: {
1.17 deraadt 108: const struct rup_data *d1 = v1;
109: const struct rup_data *d2 = v2;
110:
1.1 deraadt 111: switch(sort_type) {
112: case SORT_HOST:
113: return strcmp(d1->host, d2->host);
114: case SORT_LDAV:
1.3 deraadt 115: return d1->statstime.avenrun[0]
1.1 deraadt 116: - d2->statstime.avenrun[0];
117: case SORT_UPTIME:
1.17 deraadt 118: return d1->statstime.boottime.tv_sec
1.1 deraadt 119: - d2->statstime.boottime.tv_sec;
120: default:
121: /* something's really wrong here */
122: abort();
123: }
124: }
125:
1.18 deraadt 126: static void
1.17 deraadt 127: remember_rup_data(char *host, struct statstime *st)
1.1 deraadt 128: {
1.14 deraadt 129: if (rup_data_idx >= rup_data_max) {
1.19 tedu 130: int newsize;
131: struct rup_data *newrup;
132:
133: newsize = rup_data_max + 16;
134: newrup = realloc(rup_data, newsize * sizeof(struct rup_data));
135: if (newrup == NULL) {
1.14 deraadt 136: err(1, NULL);
1.1 deraadt 137: /* NOTREACHED */
1.14 deraadt 138: }
1.19 tedu 139: rup_data = newrup;
140: rup_data_max = newsize;
1.14 deraadt 141: }
1.22 moritz 142:
1.19 tedu 143: if ((rup_data[rup_data_idx].host = strdup(host)) == NULL)
144: err(1, NULL);
1.1 deraadt 145: rup_data[rup_data_idx].statstime = *st;
146: rup_data_idx++;
147: }
148:
149:
1.18 deraadt 150: static int
1.17 deraadt 151: rstat_reply(char *replyp, struct sockaddr_in *raddrp)
1.1 deraadt 152: {
153: struct hostent *hp;
154: char *host;
155: statstime *host_stat = (statstime *)replyp;
156:
157: if (!search_host(raddrp->sin_addr)) {
158: hp = gethostbyaddr((char *)&raddrp->sin_addr.s_addr,
1.3 deraadt 159: sizeof(struct in_addr), AF_INET);
1.1 deraadt 160: if (hp)
161: host = hp->h_name;
162: else
163: host = inet_ntoa(raddrp->sin_addr);
164:
165: remember_host(raddrp->sin_addr);
166:
1.3 deraadt 167: if (sort_type != SORT_NONE)
1.1 deraadt 168: remember_rup_data(host, host_stat);
1.3 deraadt 169: else
1.1 deraadt 170: print_rup_data(host, host_stat);
171: }
172:
173: return (0);
174: }
175:
176:
177: int
1.17 deraadt 178: print_rup_data(char *host, statstime *host_stat)
1.1 deraadt 179: {
1.16 deraadt 180: unsigned int ups = 0, upm = 0, uph = 0, upd = 0;
181: struct tm *tmp_time, host_time;
182: char days_buf[16], hours_buf[16];
1.1 deraadt 183:
1.9 millert 184: if (printtime)
1.11 aaron 185: printf("%-*.*s", HOST_WIDTH-8, HOST_WIDTH-8, host);
1.9 millert 186: else
187: printf("%-*.*s", HOST_WIDTH, HOST_WIDTH, host);
1.1 deraadt 188:
189: tmp_time = localtime((time_t *)&host_stat->curtime.tv_sec);
190: host_time = *tmp_time;
191:
192: host_stat->curtime.tv_sec -= host_stat->boottime.tv_sec;
193:
1.12 millert 194: if (host_stat->curtime.tv_sec > 0)
1.22 moritz 195: ups = host_stat->curtime.tv_sec;
196: upd = ups / (3600 * 24);
197: ups -= upd * 3600 * 24;
198: uph = ups / 3600;
199: ups -= uph * 3600;
200: upm = ups / 60;
1.6 tholo 201:
202: if (upd != 0)
1.16 deraadt 203: snprintf(days_buf, sizeof days_buf, "%3u day%s, ", upd,
204: (upd > 1) ? "s" : "");
1.1 deraadt 205: else
206: days_buf[0] = '\0';
207:
1.6 tholo 208: if (uph != 0)
1.16 deraadt 209: snprintf(hours_buf, sizeof hours_buf, "%2u:%02u, ",
210: uph, upm);
1.1 deraadt 211: else
1.6 tholo 212: if (upm != 0)
1.16 deraadt 213: snprintf(hours_buf, sizeof hours_buf, "%2u min%s ",
214: upm, (upm == 1) ? ", " : "s,");
1.1 deraadt 215: else
216: hours_buf[0] = '\0';
217:
218: if (printtime)
1.4 deraadt 219: printf(" %2d:%02d%cm",
220: (host_time.tm_hour % 12) ? (host_time.tm_hour % 12) : 12,
1.3 deraadt 221: host_time.tm_min,
222: (host_time.tm_hour >= 12) ? 'p' : 'a');
1.1 deraadt 223:
224: printf(" up %9.9s%9.9s load average: %.2f %.2f %.2f\n",
1.3 deraadt 225: days_buf, hours_buf,
1.22 moritz 226: (double)host_stat->avenrun[0] / FSCALE,
227: (double)host_stat->avenrun[1] / FSCALE,
228: (double)host_stat->avenrun[2] / FSCALE);
1.1 deraadt 229:
230: return(0);
231: }
232:
233:
1.18 deraadt 234: static void
1.17 deraadt 235: onehost(char *host)
1.1 deraadt 236: {
237: CLIENT *rstat_clnt;
238: statstime host_stat;
239: static struct timeval timeout = {25, 0};
1.5 deraadt 240: extern char *__progname;
1.22 moritz 241:
1.1 deraadt 242: rstat_clnt = clnt_create(host, RSTATPROG, RSTATVERS_TIME, "udp");
243: if (rstat_clnt == NULL) {
1.5 deraadt 244: fprintf(stderr, "%s: %s", __progname,
245: clnt_spcreateerror(host));
1.1 deraadt 246: return;
247: }
248:
249: bzero((char *)&host_stat, sizeof(host_stat));
1.3 deraadt 250: if (clnt_call(rstat_clnt, RSTATPROC_STATS, xdr_void, NULL,
251: xdr_statstime, &host_stat, timeout) != RPC_SUCCESS) {
1.5 deraadt 252: fprintf(stderr, "%s: %s", __progname,
253: clnt_sperror(rstat_clnt, host));
1.13 deraadt 254: clnt_destroy(rstat_clnt);
1.1 deraadt 255: return;
256: }
257:
1.21 moritz 258: if (sort_type != SORT_NONE)
259: remember_rup_data(host, &host_stat);
260: else
261: print_rup_data(host, &host_stat);
262:
1.1 deraadt 263: clnt_destroy(rstat_clnt);
264: }
265:
1.18 deraadt 266: static void
1.17 deraadt 267: allhosts(void)
1.1 deraadt 268: {
269: statstime host_stat;
270: enum clnt_stat clnt_stat;
1.5 deraadt 271: extern char *__progname;
1.1 deraadt 272:
273: if (sort_type != SORT_NONE) {
1.21 moritz 274: printf("collecting responses...\n");
1.1 deraadt 275: fflush(stdout);
276: }
277:
278: clnt_stat = clnt_broadcast(RSTATPROG, RSTATVERS_TIME, RSTATPROC_STATS,
1.7 deraadt 279: xdr_void, NULL, xdr_statstime, (char *)&host_stat, rstat_reply);
1.1 deraadt 280: if (clnt_stat != RPC_SUCCESS && clnt_stat != RPC_TIMEDOUT) {
1.8 deraadt 281: fprintf(stderr, "%s: %s\n", __progname, clnt_sperrno(clnt_stat));
1.1 deraadt 282: exit(1);
283: }
284: }
285:
1.7 deraadt 286: int
1.17 deraadt 287: main(int argc, char *argv[])
1.1 deraadt 288: {
289: int ch;
1.21 moritz 290: size_t i;
1.1 deraadt 291: extern int optind;
292:
293: sort_type = SORT_NONE;
294: while ((ch = getopt(argc, argv, "dhlt")) != -1)
295: switch (ch) {
296: case 'd':
297: printtime = 1;
298: break;
299: case 'h':
300: sort_type = SORT_HOST;
301: break;
302: case 'l':
303: sort_type = SORT_LDAV;
304: break;
305: case 't':
306: sort_type = SORT_UPTIME;
307: break;
308: default:
309: usage();
310: /*NOTREACHED*/
311: }
1.22 moritz 312:
1.1 deraadt 313: setlinebuf(stdout);
314:
315: if (argc == optind)
316: allhosts();
317: else {
318: for (; optind < argc; optind++)
319: onehost(argv[optind]);
1.21 moritz 320: }
321:
322: if (sort_type != SORT_NONE) {
323: qsort(rup_data, rup_data_idx, sizeof(struct rup_data),
324: compare);
325:
326: for (i = 0; i < rup_data_idx; i++) {
327: print_rup_data(rup_data[i].host,
328: &rup_data[i].statstime);
329: }
1.1 deraadt 330: }
331:
332: exit(0);
333: }
334:
335:
1.7 deraadt 336: void
1.17 deraadt 337: usage(void)
1.1 deraadt 338: {
1.23 sobrado 339: fprintf(stderr, "usage: rup [-dhlt] [host ...]\n");
1.1 deraadt 340: exit(1);
341: }