xref: /freebsd/usr.bin/rup/rup.c (revision 00e9473388d3f7ac84d8e51829b532ca476ab14e)
1 /*-
2  * SPDX-License-Identifier: BSD-4-Clause
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.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #include <sys/cdefs.h>
37 
38 __FBSDID("$FreeBSD$");
39 
40 #include <sys/param.h>
41 #include <sys/socket.h>
42 
43 #include <rpc/rpc.h>
44 #include <rpc/pmap_clnt.h>
45 
46 #undef FSHIFT			/* Use protocol's shift and scale values */
47 #undef FSCALE
48 
49 #include <rpcsvc/rstat.h>
50 
51 #include <arpa/inet.h>
52 
53 #include <err.h>
54 #include <netdb.h>
55 #include <stdio.h>
56 #include <string.h>
57 #include <stdlib.h>
58 #include <time.h>
59 #include <unistd.h>
60 
61 #define HOST_WIDTH 15
62 
63 static struct host_list {
64 	struct host_list *next;
65 	struct in_addr addr;
66 } *hosts;
67 
68 static int
69 search_host(struct in_addr addr)
70 {
71 	struct host_list *hp;
72 
73 	if (!hosts)
74 		return(0);
75 
76 	for (hp = hosts; hp != NULL; hp = hp->next) {
77 		if (hp->addr.s_addr == addr.s_addr)
78 			return(1);
79 	}
80 	return(0);
81 }
82 
83 static void
84 remember_host(struct in_addr addr)
85 {
86 	struct host_list *hp;
87 
88 	if (!(hp = (struct host_list *)malloc(sizeof(struct host_list))))
89 		errx(1, "no memory");
90 	hp->addr.s_addr = addr.s_addr;
91 	hp->next = hosts;
92 	hosts = hp;
93 }
94 
95 static bool_t
96 rstat_reply(statstime *host_stat, struct sockaddr_in *raddrp)
97 {
98 	struct tm *tmp_time;
99 	struct tm host_time;
100 	struct tm host_uptime;
101 	char days_buf[16];
102 	char hours_buf[16];
103 	struct hostent *hp;
104 	char *host;
105 	time_t tmp_time_t;
106 
107 	if (search_host(raddrp->sin_addr))
108 		return(0);
109 
110 	hp = gethostbyaddr((char *)&raddrp->sin_addr.s_addr,
111 			   sizeof(struct in_addr), AF_INET);
112 	if (hp)
113 		host = hp->h_name;
114 	else
115 		host = inet_ntoa(raddrp->sin_addr);
116 
117 	/* truncate hostname to fit nicely into field */
118 	if (strlen(host) > HOST_WIDTH)
119 		host[HOST_WIDTH] = '\0';
120 
121 	printf("%-*s\t", HOST_WIDTH, host);
122 
123 	if (sizeof(time_t) == sizeof(host_stat->curtime.tv_sec)) {
124 		tmp_time = localtime((time_t *)&host_stat->curtime.tv_sec);
125 		host_time = *tmp_time;
126 
127 		host_stat->curtime.tv_sec -= host_stat->boottime.tv_sec;
128 
129 		tmp_time = gmtime((time_t *)&host_stat->curtime.tv_sec);
130 		host_uptime = *tmp_time;
131 	}
132 	else {			/* non-32-bit time_t */
133 		tmp_time_t = host_stat->curtime.tv_sec;
134 		tmp_time = localtime(&tmp_time_t);
135 		host_time = *tmp_time;
136 
137 		host_stat->curtime.tv_sec -= host_stat->boottime.tv_sec;
138 
139 		tmp_time_t = host_stat->curtime.tv_sec;
140 		tmp_time = gmtime(&tmp_time_t);
141 		host_uptime = *tmp_time;
142 	}
143 
144 	#define updays (host_stat->curtime.tv_sec  / 86400)
145 	if (host_uptime.tm_yday != 0)
146 		sprintf(days_buf, "%3d day%s, ", updays,
147 			(updays > 1) ? "s" : "");
148 	else
149 		days_buf[0] = '\0';
150 
151 	if (host_uptime.tm_hour != 0)
152 		sprintf(hours_buf, "%2d:%02d, ",
153 			host_uptime.tm_hour, host_uptime.tm_min);
154 	else
155 		if (host_uptime.tm_min != 0)
156 			sprintf(hours_buf, "%2d mins, ", host_uptime.tm_min);
157 		else if (host_stat->curtime.tv_sec < 60)
158 			sprintf(hours_buf, "%2d secs, ", host_uptime.tm_sec);
159 		else
160 			hours_buf[0] = '\0';
161 
162 	printf(" %2d:%02d%cm  up %9.9s%9.9s load average: %.2f %.2f %.2f\n",
163 		(host_time.tm_hour % 12) ? host_time.tm_hour % 12 : 12,
164 		host_time.tm_min,
165 		(host_time.tm_hour >= 12) ? 'p' : 'a',
166 		days_buf,
167 		hours_buf,
168 		(double)host_stat->avenrun[0]/FSCALE,
169 		(double)host_stat->avenrun[1]/FSCALE,
170 		(double)host_stat->avenrun[2]/FSCALE);
171 
172 	remember_host(raddrp->sin_addr);
173 	return(0);
174 }
175 
176 static int
177 onehost(char *host)
178 {
179 	CLIENT *rstat_clnt;
180 	statstime host_stat;
181 	struct sockaddr_in addr;
182 	struct hostent *hp;
183 	struct timeval tv;
184 
185 	hp = gethostbyname(host);
186 	if (hp == NULL) {
187 		warnx("unknown host \"%s\"", host);
188 		return(-1);
189 	}
190 
191 	rstat_clnt = clnt_create(host, RSTATPROG, RSTATVERS_TIME, "udp");
192 	if (rstat_clnt == NULL) {
193 		warnx("%s %s", host, clnt_spcreateerror(""));
194 		return(-1);
195 	}
196 
197 	bzero((char *)&host_stat, sizeof(host_stat));
198 	tv.tv_sec = 15;	/* XXX ??? */
199 	tv.tv_usec = 0;
200 	if (clnt_call(rstat_clnt, RSTATPROC_STATS,
201 	    (xdrproc_t)xdr_void, NULL,
202 	    (xdrproc_t)xdr_statstime, &host_stat, tv) != RPC_SUCCESS) {
203 		warnx("%s: %s", host, clnt_sperror(rstat_clnt, host));
204 		clnt_destroy(rstat_clnt);
205 		return(-1);
206 	}
207 
208 	addr.sin_addr.s_addr = *(int *)hp->h_addr;
209 	rstat_reply(&host_stat, &addr);
210 	clnt_destroy(rstat_clnt);
211 	return (0);
212 }
213 
214 static void
215 allhosts(void)
216 {
217 	statstime host_stat;
218 	enum clnt_stat clnt_stat;
219 
220 	clnt_stat = clnt_broadcast(RSTATPROG, RSTATVERS_TIME, RSTATPROC_STATS,
221 				   (xdrproc_t)xdr_void, NULL,
222 				   (xdrproc_t)xdr_statstime, &host_stat,
223 				   (resultproc_t)rstat_reply);
224 	if (clnt_stat != RPC_SUCCESS && clnt_stat != RPC_TIMEDOUT)
225 		errx(1, "%s", clnt_sperrno(clnt_stat));
226 }
227 
228 static void
229 usage(void)
230 {
231 	fprintf(stderr, "usage: rup [host ...]\n");
232 	exit(1);
233 }
234 
235 int
236 main(int argc, char *argv[])
237 {
238 	int ch;
239 
240 	while ((ch = getopt(argc, argv, "?")) != -1)
241 		switch (ch) {
242 		default:
243 			usage();
244 		}
245 
246 	setlinebuf(stdout);
247 	if (argc == optind)
248 		allhosts();
249 	else {
250 		for (; optind < argc; optind++)
251 			(void) onehost(argv[optind]);
252 	}
253 	exit(0);
254 }
255