Annotation of src/usr.bin/rup/rup.c, Revision 1.31
1.31 ! deraadt 1: /* $OpenBSD: rup.c,v 1.30 2015/08/20 22:32:41 deraadt 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/socket.h>
36: #include <netdb.h>
37: #include <rpc/rpc.h>
1.7 deraadt 38: #include <rpc/pmap_clnt.h>
1.1 deraadt 39: #include <arpa/inet.h>
40: #include <err.h>
1.26 okan 41: #include <unistd.h>
1.1 deraadt 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:
1.30 deraadt 79: if (!(hp = malloc(sizeof(struct host_list)))) {
1.1 deraadt 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;
1.31 ! deraadt 134: newrup = recallocarray(rup_data, rup_data_max, newsize,
1.27 doug 135: sizeof(struct rup_data));
1.19 tedu 136: if (newrup == NULL) {
1.14 deraadt 137: err(1, NULL);
1.1 deraadt 138: /* NOTREACHED */
1.14 deraadt 139: }
1.19 tedu 140: rup_data = newrup;
141: rup_data_max = newsize;
1.14 deraadt 142: }
1.22 moritz 143:
1.19 tedu 144: if ((rup_data[rup_data_idx].host = strdup(host)) == NULL)
145: err(1, NULL);
1.1 deraadt 146: rup_data[rup_data_idx].statstime = *st;
147: rup_data_idx++;
148: }
149:
150:
1.18 deraadt 151: static int
1.17 deraadt 152: rstat_reply(char *replyp, struct sockaddr_in *raddrp)
1.1 deraadt 153: {
154: struct hostent *hp;
155: char *host;
156: statstime *host_stat = (statstime *)replyp;
157:
158: if (!search_host(raddrp->sin_addr)) {
159: hp = gethostbyaddr((char *)&raddrp->sin_addr.s_addr,
1.3 deraadt 160: sizeof(struct in_addr), AF_INET);
1.1 deraadt 161: if (hp)
162: host = hp->h_name;
163: else
164: host = inet_ntoa(raddrp->sin_addr);
165:
166: remember_host(raddrp->sin_addr);
167:
1.3 deraadt 168: if (sort_type != SORT_NONE)
1.1 deraadt 169: remember_rup_data(host, host_stat);
1.3 deraadt 170: else
1.1 deraadt 171: print_rup_data(host, host_stat);
172: }
173:
174: return (0);
175: }
176:
177:
178: int
1.17 deraadt 179: print_rup_data(char *host, statstime *host_stat)
1.1 deraadt 180: {
1.16 deraadt 181: unsigned int ups = 0, upm = 0, uph = 0, upd = 0;
182: struct tm *tmp_time, host_time;
183: char days_buf[16], hours_buf[16];
1.25 deraadt 184: time_t tim;
1.1 deraadt 185:
1.9 millert 186: if (printtime)
1.11 aaron 187: printf("%-*.*s", HOST_WIDTH-8, HOST_WIDTH-8, host);
1.9 millert 188: else
189: printf("%-*.*s", HOST_WIDTH, HOST_WIDTH, host);
1.1 deraadt 190:
1.25 deraadt 191: tim = host_stat->curtime.tv_sec;
192: tmp_time = localtime(&tim);
1.1 deraadt 193: host_time = *tmp_time;
194:
195: host_stat->curtime.tv_sec -= host_stat->boottime.tv_sec;
196:
1.12 millert 197: if (host_stat->curtime.tv_sec > 0)
1.22 moritz 198: ups = host_stat->curtime.tv_sec;
199: upd = ups / (3600 * 24);
200: ups -= upd * 3600 * 24;
201: uph = ups / 3600;
202: ups -= uph * 3600;
203: upm = ups / 60;
1.6 tholo 204:
205: if (upd != 0)
1.16 deraadt 206: snprintf(days_buf, sizeof days_buf, "%3u day%s, ", upd,
207: (upd > 1) ? "s" : "");
1.1 deraadt 208: else
209: days_buf[0] = '\0';
210:
1.6 tholo 211: if (uph != 0)
1.16 deraadt 212: snprintf(hours_buf, sizeof hours_buf, "%2u:%02u, ",
213: uph, upm);
1.1 deraadt 214: else
1.6 tholo 215: if (upm != 0)
1.16 deraadt 216: snprintf(hours_buf, sizeof hours_buf, "%2u min%s ",
217: upm, (upm == 1) ? ", " : "s,");
1.1 deraadt 218: else
219: hours_buf[0] = '\0';
220:
221: if (printtime)
1.4 deraadt 222: printf(" %2d:%02d%cm",
223: (host_time.tm_hour % 12) ? (host_time.tm_hour % 12) : 12,
1.3 deraadt 224: host_time.tm_min,
225: (host_time.tm_hour >= 12) ? 'p' : 'a');
1.1 deraadt 226:
227: printf(" up %9.9s%9.9s load average: %.2f %.2f %.2f\n",
1.3 deraadt 228: days_buf, hours_buf,
1.22 moritz 229: (double)host_stat->avenrun[0] / FSCALE,
230: (double)host_stat->avenrun[1] / FSCALE,
231: (double)host_stat->avenrun[2] / FSCALE);
1.1 deraadt 232:
233: return(0);
234: }
235:
236:
1.18 deraadt 237: static void
1.17 deraadt 238: onehost(char *host)
1.1 deraadt 239: {
240: CLIENT *rstat_clnt;
241: statstime host_stat;
242: static struct timeval timeout = {25, 0};
1.5 deraadt 243: extern char *__progname;
1.22 moritz 244:
1.1 deraadt 245: rstat_clnt = clnt_create(host, RSTATPROG, RSTATVERS_TIME, "udp");
246: if (rstat_clnt == NULL) {
1.5 deraadt 247: fprintf(stderr, "%s: %s", __progname,
248: clnt_spcreateerror(host));
1.1 deraadt 249: return;
250: }
251:
252: bzero((char *)&host_stat, sizeof(host_stat));
1.3 deraadt 253: if (clnt_call(rstat_clnt, RSTATPROC_STATS, xdr_void, NULL,
254: xdr_statstime, &host_stat, timeout) != RPC_SUCCESS) {
1.5 deraadt 255: fprintf(stderr, "%s: %s", __progname,
256: clnt_sperror(rstat_clnt, host));
1.13 deraadt 257: clnt_destroy(rstat_clnt);
1.1 deraadt 258: return;
259: }
260:
1.21 moritz 261: if (sort_type != SORT_NONE)
262: remember_rup_data(host, &host_stat);
263: else
264: print_rup_data(host, &host_stat);
265:
1.1 deraadt 266: clnt_destroy(rstat_clnt);
267: }
268:
1.18 deraadt 269: static void
1.17 deraadt 270: allhosts(void)
1.1 deraadt 271: {
272: statstime host_stat;
273: enum clnt_stat clnt_stat;
1.5 deraadt 274: extern char *__progname;
1.1 deraadt 275:
276: if (sort_type != SORT_NONE) {
1.21 moritz 277: printf("collecting responses...\n");
1.1 deraadt 278: fflush(stdout);
279: }
280:
281: clnt_stat = clnt_broadcast(RSTATPROG, RSTATVERS_TIME, RSTATPROC_STATS,
1.7 deraadt 282: xdr_void, NULL, xdr_statstime, (char *)&host_stat, rstat_reply);
1.1 deraadt 283: if (clnt_stat != RPC_SUCCESS && clnt_stat != RPC_TIMEDOUT) {
1.8 deraadt 284: fprintf(stderr, "%s: %s\n", __progname, clnt_sperrno(clnt_stat));
1.1 deraadt 285: exit(1);
286: }
287: }
288:
1.7 deraadt 289: int
1.17 deraadt 290: main(int argc, char *argv[])
1.1 deraadt 291: {
292: int ch;
1.21 moritz 293: size_t i;
1.1 deraadt 294: extern int optind;
295:
296: sort_type = SORT_NONE;
297: while ((ch = getopt(argc, argv, "dhlt")) != -1)
298: switch (ch) {
299: case 'd':
300: printtime = 1;
301: break;
302: case 'h':
303: sort_type = SORT_HOST;
304: break;
305: case 'l':
306: sort_type = SORT_LDAV;
307: break;
308: case 't':
309: sort_type = SORT_UPTIME;
310: break;
311: default:
312: usage();
313: /*NOTREACHED*/
314: }
1.22 moritz 315:
1.28 millert 316: setvbuf(stdout, NULL, _IOLBF, 0);
1.1 deraadt 317:
318: if (argc == optind)
319: allhosts();
320: else {
321: for (; optind < argc; optind++)
322: onehost(argv[optind]);
1.21 moritz 323: }
324:
325: if (sort_type != SORT_NONE) {
326: qsort(rup_data, rup_data_idx, sizeof(struct rup_data),
327: compare);
328:
329: for (i = 0; i < rup_data_idx; i++) {
330: print_rup_data(rup_data[i].host,
331: &rup_data[i].statstime);
332: }
1.1 deraadt 333: }
334:
335: exit(0);
336: }
337:
338:
1.7 deraadt 339: void
1.17 deraadt 340: usage(void)
1.1 deraadt 341: {
1.23 sobrado 342: fprintf(stderr, "usage: rup [-dhlt] [host ...]\n");
1.1 deraadt 343: exit(1);
344: }