Bring in a transport-independent RPC (TI-RPC).
[dragonfly.git] / usr.bin / rup / rup.c
blob93a7725cbb30d2b988f097b2e6393124f418a0e6
1 /*-
2 * Copyright (c) 1993, John Brezak
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
33 * $FreeBSD: src/usr.bin/rup/rup.c,v 1.19 2005/12/04 18:25:26 philip Exp $
34 * $DragonFly: src/usr.bin/rup/rup.c,v 1.5 2005/08/04 18:21:25 liamfoy Exp $
36 #include <sys/param.h>
37 #include <sys/socket.h>
39 #include <arpa/inet.h>
40 #include <rpc/rpc.h>
41 #include <rpc/pmap_clnt.h>
42 #undef FSHIFT /* Use protocol's shift and scale values */
43 #undef FSCALE
44 #include <rpcsvc/rstat.h>
46 #include <err.h>
47 #include <netdb.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include <time.h>
52 #include <unistd.h>
54 #define HOST_WIDTH 15
56 struct host_list {
57 struct host_list *next;
58 struct in_addr addr;
59 } *hosts;
61 static int
62 search_host(struct in_addr addr)
64 struct host_list *hp;
66 if (hosts == NULL)
67 return(0);
69 for (hp = hosts; hp != NULL; hp = hp->next) {
70 if (hp->addr.s_addr == addr.s_addr)
71 return(1);
73 return(0);
76 static void
77 remember_host(struct in_addr addr)
79 struct host_list *hp;
81 if ((hp = malloc(sizeof(struct host_list))) == NULL)
82 err(1, "malloc failed");
83 hp->addr.s_addr = addr.s_addr;
84 hp->next = hosts;
85 hosts = hp;
88 static bool_t
89 rstat_reply(caddr_t replyp, struct sockaddr_in *raddrp)
91 struct tm *tmp_time;
92 struct tm host_time;
93 struct tm host_uptime;
94 char days_buf[16];
95 char hours_buf[16];
96 struct hostent *hp;
97 char *host;
98 statstime *host_stat = (statstime *)replyp;
99 time_t tmp_time_t;
101 if (search_host(raddrp->sin_addr))
102 return(0);
104 hp = gethostbyaddr(&raddrp->sin_addr.s_addr, sizeof(struct in_addr),
105 AF_INET);
106 if (hp)
107 host = hp->h_name;
108 else
109 host = inet_ntoa(raddrp->sin_addr);
111 /* truncate hostname to fit nicely into field */
112 if (strlen(host) > HOST_WIDTH)
113 host[HOST_WIDTH] = '\0';
115 printf("%-*s\t", HOST_WIDTH, host);
117 if (sizeof(time_t) == sizeof(host_stat->curtime.tv_sec)) {
118 tmp_time = localtime((time_t *)&host_stat->curtime.tv_sec);
119 host_time = *tmp_time;
121 host_stat->curtime.tv_sec -= host_stat->boottime.tv_sec;
123 tmp_time = gmtime((time_t *)&host_stat->curtime.tv_sec);
124 host_uptime = *tmp_time;
126 else { /* non-32-bit time_t */
127 tmp_time_t = host_stat->curtime.tv_sec;
128 tmp_time = localtime(&tmp_time_t);
129 host_time = *tmp_time;
131 host_stat->curtime.tv_sec -= host_stat->boottime.tv_sec;
133 tmp_time_t = host_stat->curtime.tv_sec;
134 tmp_time = gmtime(&tmp_time_t);
135 host_uptime = *tmp_time;
138 #define updays (host_stat->curtime.tv_sec / 86400)
139 if (host_uptime.tm_yday != 0)
140 snprintf(days_buf, sizeof days_buf, "%3d day%s, ", updays,
141 (updays > 1) ? "s" : "");
142 else
143 days_buf[0] = '\0';
145 if (host_uptime.tm_hour != 0)
146 snprintf(hours_buf, sizeof hours_buf, "%2d:%02d, ",
147 host_uptime.tm_hour, host_uptime.tm_min);
148 else if (host_uptime.tm_min != 0)
149 snprintf(hours_buf, sizeof hours_buf, "%2d mins, ",
150 host_uptime.tm_min);
151 else if (host_stat->curtime.tv_sec < 60)
152 snprintf(hours_buf, sizeof hours_buf, "%2d secs, ",
153 host_uptime.tm_sec);
154 else
155 hours_buf[0] = '\0';
157 printf(" %2d:%02d%cm up %9.9s%9.9s load average: %.2f %.2f %.2f\n",
158 (host_time.tm_hour % 12) ? host_time.tm_hour % 12 : 12,
159 host_time.tm_min,
160 (host_time.tm_hour >= 12) ? 'p' : 'a',
161 days_buf,
162 hours_buf,
163 (double)host_stat->avenrun[0]/FSCALE,
164 (double)host_stat->avenrun[1]/FSCALE,
165 (double)host_stat->avenrun[2]/FSCALE);
167 remember_host(raddrp->sin_addr);
168 return(0);
171 static int
172 onehost(const char *host)
174 CLIENT *rstat_clnt;
175 statstime host_stat;
176 struct sockaddr_in addr;
177 struct hostent *hp;
178 struct timeval tv;
180 hp = gethostbyname(host);
181 if (hp == NULL) {
182 herror("rup");
183 return(1);
186 rstat_clnt = clnt_create(host, RSTATPROG, RSTATVERS_TIME, "udp");
187 if (rstat_clnt == NULL) {
188 clnt_pcreateerror(host);
189 return(1);
192 bzero(&host_stat, sizeof(host_stat));
193 tv.tv_sec = 15; /* XXX ??? */
194 tv.tv_usec = 0;
195 if (clnt_call(rstat_clnt, RSTATPROC_STATS, (xdrproc_t)xdr_void, NULL,
196 (xdrproc_t)xdr_statstime, &host_stat, tv) != RPC_SUCCESS) {
197 clnt_perror(rstat_clnt, host);
198 clnt_destroy(rstat_clnt);
199 return(1);
202 addr.sin_addr.s_addr = *(int *)hp->h_addr;
203 rstat_reply((caddr_t)&host_stat, &addr);
204 clnt_destroy(rstat_clnt);
205 return (0);
208 static void
209 allhosts(void)
211 statstime host_stat;
212 enum clnt_stat clnt_stat;
214 clnt_stat = clnt_broadcast(RSTATPROG, RSTATVERS_TIME, RSTATPROC_STATS,
215 (xdrproc_t)xdr_void, NULL,
216 (xdrproc_t)xdr_statstime, &host_stat,
217 (resultproc_t)rstat_reply);
218 if (clnt_stat != RPC_SUCCESS && clnt_stat != RPC_TIMEDOUT)
219 errx(1, "%s", clnt_sperrno(clnt_stat));
222 static void
223 usage(void)
225 fprintf(stderr, "usage: rup [host ...]\n");
226 exit(1);
230 main(int argc, char *argv[])
232 int ch, retval;
234 retval = 0;
235 while ((ch = getopt(argc, argv, "")) != -1)
236 switch (ch) {
237 default:
238 usage();
239 /*NOTREACHED*/
242 setlinebuf(stdout);
243 if (argc == optind)
244 allhosts();
245 else {
246 for (; optind < argc; optind++)
247 retval += onehost(argv[optind]);
249 exit(retval ? 1 : 0);