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