Annotation of src/usr.bin/rup/rup.c, Revision 1.20
1.20 ! deraadt 1: /* $OpenBSD: rup.c,v 1.19 2003/10/07 17:18:44 tedu 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: #ifndef lint
1.20 ! deraadt 32: static char rcsid[] = "$OpenBSD: rup.c,v 1.19 2003/10/07 17:18:44 tedu Exp $";
1.1 deraadt 33: #endif /* not lint */
34:
35: #include <stdio.h>
36: #include <stdlib.h>
37: #include <string.h>
38: #include <time.h>
39: #include <sys/param.h>
40: #include <sys/socket.h>
41: #include <netdb.h>
42: #include <rpc/rpc.h>
1.7 deraadt 43: #include <rpc/pmap_clnt.h>
1.1 deraadt 44: #include <arpa/inet.h>
45: #include <err.h>
46:
47: #undef FSHIFT /* Use protocol's shift and scale values */
48: #undef FSCALE
49: #include <rpcsvc/rstat.h>
50:
1.11 aaron 51: #define HOST_WIDTH 27
1.1 deraadt 52:
53: int printtime; /* print the remote host(s)'s time */
54:
55: struct host_list {
56: struct host_list *next;
57: struct in_addr addr;
58: } *hosts;
59:
1.15 millert 60: void usage(void);
61: int print_rup_data(char *, statstime *host_stat);
1.7 deraadt 62:
1.18 deraadt 63: static int
1.17 deraadt 64: search_host(struct in_addr addr)
1.1 deraadt 65: {
66: struct host_list *hp;
67:
68: if (!hosts)
69: return(0);
70:
71: for (hp = hosts; hp != NULL; hp = hp->next) {
72: if (hp->addr.s_addr == addr.s_addr)
73: return(1);
74: }
75: return(0);
76: }
77:
1.18 deraadt 78: static void
1.17 deraadt 79: remember_host(struct in_addr addr)
1.1 deraadt 80: {
81: struct host_list *hp;
82:
83: if (!(hp = (struct host_list *)malloc(sizeof(struct host_list)))) {
84: err(1, NULL);
85: /* NOTREACHED */
86: }
87: hp->addr.s_addr = addr.s_addr;
88: hp->next = hosts;
89: hosts = hp;
90: }
91:
92:
93: struct rup_data {
94: char *host;
95: struct statstime statstime;
96: };
97: struct rup_data *rup_data;
98: int rup_data_idx = 0;
99: int rup_data_max = 0;
100:
1.17 deraadt 101: enum sort_type {
1.1 deraadt 102: SORT_NONE,
103: SORT_HOST,
104: SORT_LDAV,
105: SORT_UPTIME
106: };
107: enum sort_type sort_type;
108:
1.18 deraadt 109: static int
1.17 deraadt 110: compare(const void *v1, const void *v2)
1.1 deraadt 111: {
1.17 deraadt 112: const struct rup_data *d1 = v1;
113: const struct rup_data *d2 = v2;
114:
1.1 deraadt 115: switch(sort_type) {
116: case SORT_HOST:
117: return strcmp(d1->host, d2->host);
118: case SORT_LDAV:
1.3 deraadt 119: return d1->statstime.avenrun[0]
1.1 deraadt 120: - d2->statstime.avenrun[0];
121: case SORT_UPTIME:
1.17 deraadt 122: return d1->statstime.boottime.tv_sec
1.1 deraadt 123: - d2->statstime.boottime.tv_sec;
124: default:
125: /* something's really wrong here */
126: abort();
127: }
128: }
129:
1.18 deraadt 130: static void
1.17 deraadt 131: remember_rup_data(char *host, struct statstime *st)
1.1 deraadt 132: {
1.14 deraadt 133: if (rup_data_idx >= rup_data_max) {
1.19 tedu 134: int newsize;
135: struct rup_data *newrup;
136:
137: newsize = rup_data_max + 16;
138: newrup = realloc(rup_data, newsize * sizeof(struct rup_data));
139: if (newrup == NULL) {
1.14 deraadt 140: err(1, NULL);
1.1 deraadt 141: /* NOTREACHED */
1.14 deraadt 142: }
1.19 tedu 143: rup_data = newrup;
144: rup_data_max = newsize;
1.14 deraadt 145: }
1.1 deraadt 146:
1.19 tedu 147: if ((rup_data[rup_data_idx].host = strdup(host)) == NULL)
148: err(1, NULL);
1.1 deraadt 149: rup_data[rup_data_idx].statstime = *st;
150: rup_data_idx++;
151: }
152:
153:
1.18 deraadt 154: static int
1.17 deraadt 155: rstat_reply(char *replyp, struct sockaddr_in *raddrp)
1.1 deraadt 156: {
157: struct hostent *hp;
158: char *host;
159: statstime *host_stat = (statstime *)replyp;
160:
161: if (!search_host(raddrp->sin_addr)) {
162: hp = gethostbyaddr((char *)&raddrp->sin_addr.s_addr,
1.3 deraadt 163: sizeof(struct in_addr), AF_INET);
1.1 deraadt 164: if (hp)
165: host = hp->h_name;
166: else
167: host = inet_ntoa(raddrp->sin_addr);
168:
169: remember_host(raddrp->sin_addr);
170:
1.3 deraadt 171: if (sort_type != SORT_NONE)
1.1 deraadt 172: remember_rup_data(host, host_stat);
1.3 deraadt 173: else
1.1 deraadt 174: print_rup_data(host, host_stat);
175: }
176:
177: return (0);
178: }
179:
180:
181: int
1.17 deraadt 182: print_rup_data(char *host, statstime *host_stat)
1.1 deraadt 183: {
1.16 deraadt 184: unsigned int ups = 0, upm = 0, uph = 0, upd = 0;
185: struct tm *tmp_time, host_time;
186: char days_buf[16], hours_buf[16];
1.1 deraadt 187:
1.9 millert 188: if (printtime)
1.11 aaron 189: printf("%-*.*s", HOST_WIDTH-8, HOST_WIDTH-8, host);
1.9 millert 190: else
191: printf("%-*.*s", HOST_WIDTH, HOST_WIDTH, host);
1.1 deraadt 192:
193: tmp_time = localtime((time_t *)&host_stat->curtime.tv_sec);
194: host_time = *tmp_time;
195:
196: host_stat->curtime.tv_sec -= host_stat->boottime.tv_sec;
197:
1.12 millert 198: if (host_stat->curtime.tv_sec > 0)
199: ups=host_stat->curtime.tv_sec;
1.6 tholo 200: upd=ups/(3600*24);
201: ups-=upd*3600*24;
202: uph=ups/3600;
203: ups-=uph*3600;
204: upm=ups/60;
205:
206: if (upd != 0)
1.16 deraadt 207: snprintf(days_buf, sizeof days_buf, "%3u day%s, ", upd,
208: (upd > 1) ? "s" : "");
1.1 deraadt 209: else
210: days_buf[0] = '\0';
211:
1.6 tholo 212: if (uph != 0)
1.16 deraadt 213: snprintf(hours_buf, sizeof hours_buf, "%2u:%02u, ",
214: uph, upm);
1.1 deraadt 215: else
1.6 tholo 216: if (upm != 0)
1.16 deraadt 217: snprintf(hours_buf, sizeof hours_buf, "%2u min%s ",
218: upm, (upm == 1) ? ", " : "s,");
1.1 deraadt 219: else
220: hours_buf[0] = '\0';
221:
222: if (printtime)
1.4 deraadt 223: printf(" %2d:%02d%cm",
224: (host_time.tm_hour % 12) ? (host_time.tm_hour % 12) : 12,
1.3 deraadt 225: host_time.tm_min,
226: (host_time.tm_hour >= 12) ? 'p' : 'a');
1.1 deraadt 227:
228: printf(" up %9.9s%9.9s load average: %.2f %.2f %.2f\n",
1.3 deraadt 229: days_buf, hours_buf,
230: (double)host_stat->avenrun[0]/FSCALE,
231: (double)host_stat->avenrun[1]/FSCALE,
232: (double)host_stat->avenrun[2]/FSCALE);
1.1 deraadt 233:
234: return(0);
235: }
236:
237:
1.18 deraadt 238: static void
1.17 deraadt 239: onehost(char *host)
1.1 deraadt 240: {
241: CLIENT *rstat_clnt;
242: statstime host_stat;
243: static struct timeval timeout = {25, 0};
1.5 deraadt 244: extern char *__progname;
1.1 deraadt 245:
246: rstat_clnt = clnt_create(host, RSTATPROG, RSTATVERS_TIME, "udp");
247: if (rstat_clnt == NULL) {
1.5 deraadt 248: fprintf(stderr, "%s: %s", __progname,
249: clnt_spcreateerror(host));
1.1 deraadt 250: return;
251: }
252:
253: bzero((char *)&host_stat, sizeof(host_stat));
1.3 deraadt 254: if (clnt_call(rstat_clnt, RSTATPROC_STATS, xdr_void, NULL,
255: xdr_statstime, &host_stat, timeout) != RPC_SUCCESS) {
1.5 deraadt 256: fprintf(stderr, "%s: %s", __progname,
257: clnt_sperror(rstat_clnt, host));
1.13 deraadt 258: clnt_destroy(rstat_clnt);
1.1 deraadt 259: return;
260: }
261:
262: print_rup_data(host, &host_stat);
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: size_t i;
273:
274: if (sort_type != SORT_NONE) {
275: printf("collecting responses...");
276: fflush(stdout);
277: }
278:
279: clnt_stat = clnt_broadcast(RSTATPROG, RSTATVERS_TIME, RSTATPROC_STATS,
1.7 deraadt 280: xdr_void, NULL, xdr_statstime, (char *)&host_stat, rstat_reply);
1.1 deraadt 281: if (clnt_stat != RPC_SUCCESS && clnt_stat != RPC_TIMEDOUT) {
1.8 deraadt 282: fprintf(stderr, "%s: %s\n", __progname, clnt_sperrno(clnt_stat));
1.1 deraadt 283: exit(1);
284: }
285:
286: if (sort_type != SORT_NONE) {
287: putchar('\n');
1.3 deraadt 288: qsort(rup_data, rup_data_idx, sizeof(struct rup_data),
289: compare);
1.1 deraadt 290:
291: for (i = 0; i < rup_data_idx; i++) {
1.3 deraadt 292: print_rup_data(rup_data[i].host,
293: &rup_data[i].statstime);
1.1 deraadt 294: }
295: }
296: }
297:
1.7 deraadt 298: int
1.17 deraadt 299: main(int argc, char *argv[])
1.1 deraadt 300: {
301: int ch;
302: extern int optind;
303:
304: sort_type = SORT_NONE;
305: while ((ch = getopt(argc, argv, "dhlt")) != -1)
306: switch (ch) {
307: case 'd':
308: printtime = 1;
309: break;
310: case 'h':
311: sort_type = SORT_HOST;
312: break;
313: case 'l':
314: sort_type = SORT_LDAV;
315: break;
316: case 't':
317: sort_type = SORT_UPTIME;
318: break;
319: default:
320: usage();
321: /*NOTREACHED*/
322: }
323:
324: setlinebuf(stdout);
325:
326: if (argc == optind)
327: allhosts();
328: else {
329: for (; optind < argc; optind++)
330: onehost(argv[optind]);
331: }
332:
333: exit(0);
334: }
335:
336:
1.7 deraadt 337: void
1.17 deraadt 338: usage(void)
1.1 deraadt 339: {
340: fprintf(stderr, "Usage: rup [-dhlt] [hosts ...]\n");
341: exit(1);
342: }