1/*
2 * Copyright (c) 1983, 1993, 1994
3 *	The Regents of the University of California.  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.
13 * 4. 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.
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.
28 */
29
30#ifndef lint
31static const char copyright[] =
32"@(#) Copyright (c) 1983, 1993, 1994\n\
33	The Regents of the University of California.  All rights reserved.\n";
34#endif /* not lint */
35
36#ifndef lint
37static const char sccsid[] = "@(#)ruptime.c	8.2 (Berkeley) 4/5/94";
38#endif /* not lint */
39
40#include <sys/cdefs.h>
41__FBSDID("$FreeBSD: stable/11/usr.bin/ruptime/ruptime.c 359799 2020-04-11 07:46:38Z nyan $");
42
43#include <sys/param.h>
44
45#include <protocols/rwhod.h>
46
47#include <dirent.h>
48#include <err.h>
49#include <errno.h>
50#include <fcntl.h>
51#include <stdio.h>
52#include <stdlib.h>
53#include <string.h>
54#include <time.h>
55#include <unistd.h>
56
57static struct hs {
58	struct	whod hs_wd;
59	int	hs_nusers;
60} *hs;
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)
64
65static size_t nhosts;
66static time_t now;
67static int rflg = 1;
68static DIR *dirp;
69
70static int	 hscmp(const void *, const void *);
71static char	*interval(time_t, const char *);
72static int	 iwidth(int);
73static int	 lcmp(const void *, const void *);
74static void	 ruptime(const char *, int, int (*)(const void *, const void *));
75static int	 tcmp(const void *, const void *);
76static int	 ucmp(const void *, const void *);
77static void	 usage(void);
78
79int
80main(int argc, char *argv[])
81{
82	int (*cmp)(const void *, const void *);
83	int aflg, ch;
84
85	aflg = 0;
86	cmp = hscmp;
87	while ((ch = getopt(argc, argv, "alrut")) != -1)
88		switch (ch) {
89		case 'a':
90			aflg = 1;
91			break;
92		case 'l':
93			cmp = lcmp;
94			break;
95		case 'r':
96			rflg = -1;
97			break;
98		case 't':
99			cmp = tcmp;
100			break;
101		case 'u':
102			cmp = ucmp;
103			break;
104		default:
105			usage();
106		}
107	argc -= optind;
108	argv += optind;
109
110	if (chdir(_PATH_RWHODIR) || (dirp = opendir(".")) == NULL)
111		err(1, "%s", _PATH_RWHODIR);
112
113	ruptime(*argv, aflg, cmp);
114	while (*argv++ != NULL) {
115		if (*argv == NULL)
116			break;
117		ruptime(*argv, aflg, cmp);
118	}
119	exit(0);
120}
121
122static char *
123interval(time_t tval, const char *updown)
124{
125	static char resbuf[32];
126	int days, hours, minutes;
127
128	if (tval < 0) {
129		(void)snprintf(resbuf, sizeof(resbuf), "%s      ??:??", updown);
130		return (resbuf);
131	}
132	/* Round to minutes. */
133	minutes = (tval + (60 - 1)) / 60;
134	hours = minutes / 60;
135	minutes %= 60;
136	days = hours / 24;
137	hours %= 24;
138	if (days)
139		(void)snprintf(resbuf, sizeof(resbuf),
140		    "%s %4d+%02d:%02d", updown, days, hours, minutes);
141	else
142		(void)snprintf(resbuf, sizeof(resbuf),
143		    "%s      %2d:%02d", updown, hours, minutes);
144	return (resbuf);
145}
146
147/* Width to print a small nonnegative integer. */
148static int
149iwidth(int w)
150{
151	if (w < 10)
152		return (1);
153	if (w < 100)
154		return (2);
155	if (w < 1000)
156		return (3);
157	if (w < 10000)
158		return (4);
159	return (5);
160}
161
162#define	HS(a)	((const struct hs *)(a))
163
164/* Alphabetical comparison. */
165static int
166hscmp(const void *a1, const void *a2)
167{
168	return (rflg *
169	    strcmp(HS(a1)->hs_wd.wd_hostname, HS(a2)->hs_wd.wd_hostname));
170}
171
172/* Load average comparison. */
173static int
174lcmp(const void *a1, const void *a2)
175{
176	if (ISDOWN(HS(a1)))
177		if (ISDOWN(HS(a2)))
178			return (tcmp(a1, a2));
179		else
180			return (rflg);
181	else if (ISDOWN(HS(a2)))
182		return (-rflg);
183	else
184		return (rflg *
185		   (HS(a2)->hs_wd.wd_loadav[0] - HS(a1)->hs_wd.wd_loadav[0]));
186}
187
188static void
189ruptime(const char *host, int aflg, int (*cmp)(const void *, const void *))
190{
191	struct hs *hsp;
192	struct whod *wd;
193	struct whoent *we;
194	struct dirent *dp;
195	int fd, hostnamewidth, i, loadavwidth[3], userswidth, w;
196	size_t hspace;
197	ssize_t cc;
198
199	rewinddir(dirp);
200	hsp = NULL;
201	hostnamewidth = 0;
202	loadavwidth[0] = 4;
203	loadavwidth[1] = 4;
204	loadavwidth[2] = 4;
205	userswidth = 1;
206	(void)time(&now);
207	for (nhosts = hspace = 0; (dp = readdir(dirp)) != NULL;) {
208		if (dp->d_ino == 0 || strncmp(dp->d_name, "whod.", 5) != 0)
209			continue;
210		if ((fd = open(dp->d_name, O_RDONLY, 0)) < 0) {
211			warn("%s", dp->d_name);
212			continue;
213		}
214
215		if (nhosts == hspace) {
216			if ((hs =
217			    realloc(hs, (hspace += 40) * sizeof(*hs))) == NULL)
218				err(1, NULL);
219			hsp = hs + nhosts;
220		}
221
222		wd = &hsp->hs_wd;
223		cc = read(fd, wd, sizeof(*wd));
224		(void)close(fd);
225		if (cc < (ssize_t)WHDRSIZE)
226			continue;
227
228		if (host != NULL && strcasecmp(wd->wd_hostname, host) != 0)
229			continue;
230		if (LEFTEARTH(wd->wd_recvtime))
231			continue;
232
233		if (hostnamewidth < (int)strlen(wd->wd_hostname))
234			hostnamewidth = (int)strlen(wd->wd_hostname);
235
236		if (!ISDOWN(hsp)) {
237			for (i = 0; i < 3; i++) {
238				w = iwidth(wd->wd_loadav[i] / 100) + 3;
239				if (loadavwidth[i] < w)
240					loadavwidth[i] = w;
241			}
242			for (hsp->hs_nusers = 0, we = &wd->wd_we[0];
243			     (char *)(we + 1) <= (char *)wd + cc; we++)
244				if (aflg || we->we_idle < 3600)
245					++hsp->hs_nusers;
246			if (userswidth < iwidth(hsp->hs_nusers))
247				userswidth = iwidth(hsp->hs_nusers);
248		}
249
250		++hsp;
251		++nhosts;
252	}
253	if (nhosts == 0) {
254		if (host == NULL)
255			errx(1, "no hosts in %s", _PATH_RWHODIR);
256		else
257			warnx("host %s not in %s", host, _PATH_RWHODIR);
258	}
259
260	qsort(hs, nhosts, sizeof(hs[0]), cmp);
261	w = userswidth + loadavwidth[0] + loadavwidth[1] + loadavwidth[2];
262	if (hostnamewidth + w > 41)
263		hostnamewidth = 41 - w;	/* limit to 79 cols */
264	for (i = 0; i < (int)nhosts; i++) {
265		hsp = &hs[i];
266		wd = &hsp->hs_wd;
267		if (ISDOWN(hsp)) {
268			(void)printf("%-*.*s  %s\n",
269			    hostnamewidth, hostnamewidth, wd->wd_hostname,
270			    interval(now - hsp->hs_wd.wd_recvtime, "down"));
271			continue;
272		}
273		(void)printf(
274		    "%-*.*s  %s,  %*d user%s  load %*.2f, %*.2f, %*.2f\n",
275		    hostnamewidth, hostnamewidth, wd->wd_hostname,
276		    interval((time_t)wd->wd_sendtime -
277		        (time_t)wd->wd_boottime, "  up"),
278		    userswidth, hsp->hs_nusers,
279		    hsp->hs_nusers == 1 ? ", " : "s,",
280		    loadavwidth[0], wd->wd_loadav[0] / 100.0,
281		    loadavwidth[1], wd->wd_loadav[1] / 100.0,
282		    loadavwidth[2], wd->wd_loadav[2] / 100.0);
283	}
284	free(hs);
285	hs = NULL;
286}
287
288/* Number of users comparison. */
289static int
290ucmp(const void *a1, const void *a2)
291{
292	if (ISDOWN(HS(a1)))
293		if (ISDOWN(HS(a2)))
294			return (tcmp(a1, a2));
295		else
296			return (rflg);
297	else if (ISDOWN(HS(a2)))
298		return (-rflg);
299	else
300		return (rflg * (HS(a2)->hs_nusers - HS(a1)->hs_nusers));
301}
302
303/* Uptime comparison. */
304static int
305tcmp(const void *a1, const void *a2)
306{
307	return (rflg * (
308		(ISDOWN(HS(a2)) ? HS(a2)->hs_wd.wd_recvtime - now
309		    : HS(a2)->hs_wd.wd_sendtime - HS(a2)->hs_wd.wd_boottime)
310		-
311		(ISDOWN(HS(a1)) ? HS(a1)->hs_wd.wd_recvtime - now
312		    : HS(a1)->hs_wd.wd_sendtime - HS(a1)->hs_wd.wd_boottime)
313	));
314}
315
316static void
317usage(void)
318{
319	(void)fprintf(stderr, "usage: ruptime [-alrtu] [host ...]\n");
320	exit(1);
321}
322