2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1983, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 static const char copyright[] =
34 "@(#) Copyright (c) 1983, 1993, 1994\n\
35 The Regents of the University of California. All rights reserved.\n";
39 static const char sccsid[] = "@(#)ruptime.c 8.2 (Berkeley) 4/5/94";
42 #include <sys/cdefs.h>
43 __FBSDID("$FreeBSD$");
45 #include <sys/param.h>
47 #include <protocols/rwhod.h>
63 #define LEFTEARTH(h) (now - (h) > 4*24*60*60)
64 #define ISDOWN(h) (now - (h)->hs_wd.wd_recvtime > 11 * 60)
65 #define WHDRSIZE __offsetof(struct whod, wd_we)
72 static int hscmp(const void *, const void *);
73 static char *interval(time_t, const char *);
74 static int iwidth(int);
75 static int lcmp(const void *, const void *);
76 static void ruptime(const char *, int, int (*)(const void *, const void *));
77 static int tcmp(const void *, const void *);
78 static int ucmp(const void *, const void *);
79 static void usage(void);
82 main(int argc, char *argv[])
84 int (*cmp)(const void *, const void *);
89 while ((ch = getopt(argc, argv, "alrut")) != -1)
112 if (chdir(_PATH_RWHODIR) || (dirp = opendir(".")) == NULL)
113 err(1, "%s", _PATH_RWHODIR);
115 ruptime(*argv, aflg, cmp);
116 while (*argv++ != NULL) {
119 ruptime(*argv, aflg, cmp);
125 interval(time_t tval, const char *updown)
127 static char resbuf[32];
128 int days, hours, minutes;
131 (void)snprintf(resbuf, sizeof(resbuf), "%s ??:??", updown);
134 /* Round to minutes. */
135 minutes = (tval + (60 - 1)) / 60;
136 hours = minutes / 60;
141 (void)snprintf(resbuf, sizeof(resbuf),
142 "%s %4d+%02d:%02d", updown, days, hours, minutes);
144 (void)snprintf(resbuf, sizeof(resbuf),
145 "%s %2d:%02d", updown, hours, minutes);
149 /* Width to print a small nonnegative integer. */
164 #define HS(a) ((const struct hs *)(a))
166 /* Alphabetical comparison. */
168 hscmp(const void *a1, const void *a2)
171 strcmp(HS(a1)->hs_wd.wd_hostname, HS(a2)->hs_wd.wd_hostname));
174 /* Load average comparison. */
176 lcmp(const void *a1, const void *a2)
180 return (tcmp(a1, a2));
183 else if (ISDOWN(HS(a2)))
187 (HS(a2)->hs_wd.wd_loadav[0] - HS(a1)->hs_wd.wd_loadav[0]));
191 ruptime(const char *host, int aflg, int (*cmp)(const void *, const void *))
197 int fd, hostnamewidth, i, loadavwidth[3], userswidth, w;
209 for (nhosts = hspace = 0; (dp = readdir(dirp)) != NULL;) {
210 if (dp->d_ino == 0 || strncmp(dp->d_name, "whod.", 5) != 0)
212 if ((fd = open(dp->d_name, O_RDONLY, 0)) < 0) {
213 warn("%s", dp->d_name);
217 if (nhosts == hspace) {
219 realloc(hs, (hspace += 40) * sizeof(*hs))) == NULL)
225 cc = read(fd, wd, sizeof(*wd));
227 if (cc < (ssize_t)WHDRSIZE)
230 if (host != NULL && strcasecmp(wd->wd_hostname, host) != 0)
232 if (LEFTEARTH(wd->wd_recvtime))
235 if (hostnamewidth < (int)strlen(wd->wd_hostname))
236 hostnamewidth = (int)strlen(wd->wd_hostname);
239 for (i = 0; i < 3; i++) {
240 w = iwidth(wd->wd_loadav[i] / 100) + 3;
241 if (loadavwidth[i] < w)
244 for (hsp->hs_nusers = 0, we = &wd->wd_we[0];
245 (char *)(we + 1) <= (char *)wd + cc; we++)
246 if (aflg || we->we_idle < 3600)
248 if (userswidth < iwidth(hsp->hs_nusers))
249 userswidth = iwidth(hsp->hs_nusers);
257 errx(1, "no hosts in %s", _PATH_RWHODIR);
259 warnx("host %s not in %s", host, _PATH_RWHODIR);
262 qsort(hs, nhosts, sizeof(hs[0]), cmp);
263 w = userswidth + loadavwidth[0] + loadavwidth[1] + loadavwidth[2];
264 if (hostnamewidth + w > 41)
265 hostnamewidth = 41 - w; /* limit to 79 cols */
266 for (i = 0; i < (int)nhosts; i++) {
270 (void)printf("%-*.*s %s\n",
271 hostnamewidth, hostnamewidth, wd->wd_hostname,
272 interval(now - hsp->hs_wd.wd_recvtime, "down"));
276 "%-*.*s %s, %*d user%s load %*.2f, %*.2f, %*.2f\n",
277 hostnamewidth, hostnamewidth, wd->wd_hostname,
278 interval((time_t)wd->wd_sendtime -
279 (time_t)wd->wd_boottime, " up"),
280 userswidth, hsp->hs_nusers,
281 hsp->hs_nusers == 1 ? ", " : "s,",
282 loadavwidth[0], wd->wd_loadav[0] / 100.0,
283 loadavwidth[1], wd->wd_loadav[1] / 100.0,
284 loadavwidth[2], wd->wd_loadav[2] / 100.0);
290 /* Number of users comparison. */
292 ucmp(const void *a1, const void *a2)
296 return (tcmp(a1, a2));
299 else if (ISDOWN(HS(a2)))
302 return (rflg * (HS(a2)->hs_nusers - HS(a1)->hs_nusers));
305 /* Uptime comparison. */
307 tcmp(const void *a1, const void *a2)
310 (ISDOWN(HS(a2)) ? HS(a2)->hs_wd.wd_recvtime - now
311 : HS(a2)->hs_wd.wd_sendtime - HS(a2)->hs_wd.wd_boottime)
313 (ISDOWN(HS(a1)) ? HS(a1)->hs_wd.wd_recvtime - now
314 : HS(a1)->hs_wd.wd_sendtime - HS(a1)->hs_wd.wd_boottime)
321 (void)fprintf(stderr, "usage: ruptime [-alrtu] [host ...]\n");