2 * Copyright (c) 1983, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
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
31 static const char copyright[] =
32 "@(#) Copyright (c) 1983, 1993, 1994\n\
33 The Regents of the University of California. All rights reserved.\n";
37 static const char sccsid[] = "@(#)ruptime.c 8.2 (Berkeley) 4/5/94";
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
43 #include <sys/param.h>
45 #include <protocols/rwhod.h>
61 #define LEFTEARTH(h) (now - (h) > 4*24*60*60)
62 #define ISDOWN(h) (now - (h)->hs_wd.wd_recvtime > 11 * 60)
63 #define WHDRSIZE __offsetof(struct whod, wd_we)
70 static int hscmp(const void *, const void *);
71 static char *interval(time_t, const char *);
72 static int iwidth(int);
73 static int lcmp(const void *, const void *);
74 static void ruptime(const char *, int, int (*)(const void *, const void *));
75 static int tcmp(const void *, const void *);
76 static int ucmp(const void *, const void *);
77 static void usage(void);
80 main(int argc, char *argv[])
82 int (*cmp)(const void *, const void *);
87 while ((ch = getopt(argc, argv, "alrut")) != -1)
110 if (chdir(_PATH_RWHODIR) || (dirp = opendir(".")) == NULL)
111 err(1, "%s", _PATH_RWHODIR);
113 ruptime(*argv, aflg, cmp);
114 while (*argv++ != NULL) {
117 ruptime(*argv, aflg, cmp);
123 interval(time_t tval, const char *updown)
125 static char resbuf[32];
126 int days, hours, minutes;
129 (void)snprintf(resbuf, sizeof(resbuf), "%s ??:??", updown);
132 /* Round to minutes. */
133 minutes = (tval + (60 - 1)) / 60;
134 hours = minutes / 60;
139 (void)snprintf(resbuf, sizeof(resbuf),
140 "%s %4d+%02d:%02d", updown, days, hours, minutes);
142 (void)snprintf(resbuf, sizeof(resbuf),
143 "%s %2d:%02d", updown, hours, minutes);
147 /* Width to print a small nonnegative integer. */
162 #define HS(a) ((const struct hs *)(a))
164 /* Alphabetical comparison. */
166 hscmp(const void *a1, const void *a2)
169 strcmp(HS(a1)->hs_wd.wd_hostname, HS(a2)->hs_wd.wd_hostname));
172 /* Load average comparison. */
174 lcmp(const void *a1, const void *a2)
178 return (tcmp(a1, a2));
181 else if (ISDOWN(HS(a2)))
185 (HS(a2)->hs_wd.wd_loadav[0] - HS(a1)->hs_wd.wd_loadav[0]));
189 ruptime(const char *host, int aflg, int (*cmp)(const void *, const void *))
195 int fd, hostnamewidth, i, loadavwidth[3], userswidth, w;
207 for (nhosts = hspace = 0; (dp = readdir(dirp)) != NULL;) {
208 if (dp->d_ino == 0 || strncmp(dp->d_name, "whod.", 5) != 0)
210 if ((fd = open(dp->d_name, O_RDONLY, 0)) < 0) {
211 warn("%s", dp->d_name);
215 if (nhosts == hspace) {
217 realloc(hs, (hspace += 40) * sizeof(*hs))) == NULL)
223 cc = read(fd, wd, sizeof(*wd));
225 if (cc < (ssize_t)WHDRSIZE)
228 if (host != NULL && strcasecmp(wd->wd_hostname, host) != 0)
230 if (LEFTEARTH(wd->wd_recvtime))
233 if (hostnamewidth < (int)strlen(wd->wd_hostname))
234 hostnamewidth = (int)strlen(wd->wd_hostname);
235 for (i = 0; i < 3; i++) {
236 w = iwidth(wd->wd_loadav[i] / 100) + 3;
237 if (loadavwidth[i] < w)
241 for (hsp->hs_nusers = 0, we = &wd->wd_we[0];
242 (char *)(we + 1) <= (char *)wd + cc; we++)
243 if (aflg || we->we_idle < 3600)
245 if (userswidth < iwidth(hsp->hs_nusers))
246 userswidth = iwidth(hsp->hs_nusers);
252 errx(1, "no hosts in %s", _PATH_RWHODIR);
254 warnx("host %s not in %s", host, _PATH_RWHODIR);
257 qsort(hs, nhosts, sizeof(hs[0]), cmp);
258 w = userswidth + loadavwidth[0] + loadavwidth[1] + loadavwidth[2];
259 if (hostnamewidth + w > 41)
260 hostnamewidth = 41 - w; /* limit to 79 cols */
261 for (i = 0; i < (int)nhosts; i++) {
265 (void)printf("%-*.*s%s\n",
266 hostnamewidth, hostnamewidth, wd->wd_hostname,
267 interval(now - hsp->hs_wd.wd_recvtime, "down"));
271 "%-*.*s %s, %*d user%s load %*.2f, %*.2f, %*.2f\n",
272 hostnamewidth, hostnamewidth, wd->wd_hostname,
273 interval((time_t)wd->wd_sendtime -
274 (time_t)wd->wd_boottime, " up"),
275 userswidth, hsp->hs_nusers,
276 hsp->hs_nusers == 1 ? ", " : "s,",
277 loadavwidth[0], wd->wd_loadav[0] / 100.0,
278 loadavwidth[1], wd->wd_loadav[1] / 100.0,
279 loadavwidth[2], wd->wd_loadav[2] / 100.0);
285 /* Number of users comparison. */
287 ucmp(const void *a1, const void *a2)
291 return (tcmp(a1, a2));
294 else if (ISDOWN(HS(a2)))
297 return (rflg * (HS(a2)->hs_nusers - HS(a1)->hs_nusers));
300 /* Uptime comparison. */
302 tcmp(const void *a1, const void *a2)
305 (ISDOWN(HS(a2)) ? HS(a2)->hs_wd.wd_recvtime - now
306 : HS(a2)->hs_wd.wd_sendtime - HS(a2)->hs_wd.wd_boottime)
308 (ISDOWN(HS(a1)) ? HS(a1)->hs_wd.wd_recvtime - now
309 : HS(a1)->hs_wd.wd_sendtime - HS(a1)->hs_wd.wd_boottime)
316 (void)fprintf(stderr, "usage: ruptime [-alrtu] [host ...]\n");