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$");
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
57struct hs {
58	struct	whod *hs_wd;
59	int	hs_nusers;
60} *hs;
61struct	whod awhod;
62#define LEFTEARTH(h)		(now - (h) > 4*24*60*60)
63#define	ISDOWN(h)		(now - (h)->hs_wd->wd_recvtime > 11 * 60)
64#define	WHDRSIZE	(sizeof (awhod) - sizeof (awhod.wd_we))
65
66size_t nhosts;
67time_t now;
68int rflg = 1;
69DIR *dirp;
70
71int	 hscmp(const void *, const void *);
72char	*interval(time_t, const char *);
73int	 lcmp(const void *, const void *);
74void	 morehosts(void);
75void	 ruptime(const char *, int, int (*)(const void *, const void *));
76int	 tcmp(const void *, const void *);
77int	 ucmp(const void *, const void *);
78void	 usage(void);
79
80int
81main(int argc, char *argv[])
82{
83	int (*cmp)(const void *, const void *);
84	int aflg, ch;
85
86	aflg = 0;
87	cmp = hscmp;
88	while ((ch = getopt(argc, argv, "alrut")) != -1)
89		switch (ch) {
90		case 'a':
91			aflg = 1;
92			break;
93		case 'l':
94			cmp = lcmp;
95			break;
96		case 'r':
97			rflg = -1;
98			break;
99		case 't':
100			cmp = tcmp;
101			break;
102		case 'u':
103			cmp = ucmp;
104			break;
105		default:
106			usage();
107		}
108	argc -= optind;
109	argv += optind;
110
111	if (chdir(_PATH_RWHODIR) || (dirp = opendir(".")) == NULL)
112		err(1, "%s", _PATH_RWHODIR);
113
114	ruptime(*argv, aflg, cmp);
115	while (*argv++ != NULL) {
116		if (*argv == NULL)
117			break;
118		ruptime(*argv, aflg, cmp);
119	}
120	exit(0);
121}
122
123char *
124interval(time_t tval, const char *updown)
125{
126	static char resbuf[32];
127	int days, hours, minutes;
128
129	if (tval < 0) {
130		(void)snprintf(resbuf, sizeof(resbuf), "   %s ??:??", updown);
131		return (resbuf);
132	}
133						/* round to minutes. */
134	minutes = (tval + (60 - 1)) / 60;
135	hours = minutes / 60;
136	minutes %= 60;
137	days = hours / 24;
138	hours %= 24;
139	if (days)
140		(void)snprintf(resbuf, sizeof(resbuf),
141		    "%s %3d+%02d:%02d", updown, days, hours, minutes);
142	else
143		(void)snprintf(resbuf, sizeof(resbuf),
144		    "%s     %2d:%02d", updown, hours, minutes);
145	return (resbuf);
146}
147
148#define	HS(a)	((const struct hs *)(a))
149
150/* Alphabetical comparison. */
151int
152hscmp(const void *a1, const void *a2)
153{
154	return (rflg *
155	    strcmp(HS(a1)->hs_wd->wd_hostname, HS(a2)->hs_wd->wd_hostname));
156}
157
158/* Load average comparison. */
159int
160lcmp(const void *a1, const void *a2)
161{
162	if (ISDOWN(HS(a1)))
163		if (ISDOWN(HS(a2)))
164			return (tcmp(a1, a2));
165		else
166			return (rflg);
167	else if (ISDOWN(HS(a2)))
168		return (-rflg);
169	else
170		return (rflg *
171		   (HS(a2)->hs_wd->wd_loadav[0] - HS(a1)->hs_wd->wd_loadav[0]));
172}
173
174void
175ruptime(const char *host, int aflg, int (*cmp)(const void *, const void *))
176{
177	struct hs *hsp;
178	struct whod *wd;
179	struct whoent *we;
180	struct dirent *dp;
181	const char *hostname;
182	char buf[sizeof(struct whod)];
183	int fd, i, maxloadav;
184	size_t hspace;
185	u_int cc;
186
187	rewinddir(dirp);
188	hsp = NULL;
189	maxloadav = -1;
190	(void)time(&now);
191	for (nhosts = hspace = 0; (dp = readdir(dirp)) != NULL;) {
192		if (dp->d_ino == 0 || strncmp(dp->d_name, "whod.", 5) != 0)
193			continue;
194		if ((fd = open(dp->d_name, O_RDONLY, 0)) < 0) {
195			warn("%s", dp->d_name);
196			continue;
197		}
198		cc = read(fd, buf, sizeof(struct whod));
199		(void)close(fd);
200		if (host != NULL) {
201			hostname = ((struct whod *)buf)->wd_hostname;
202			if (strcasecmp(hostname, host) != 0)
203				continue;
204		}
205
206		if (cc < WHDRSIZE)
207			continue;
208		if (LEFTEARTH(((struct whod *)buf)->wd_recvtime))
209			continue;
210		if (nhosts == hspace) {
211			if ((hs =
212			    realloc(hs, (hspace += 40) * sizeof(*hs))) == NULL)
213				err(1, NULL);
214			hsp = hs + nhosts;
215		}
216
217		if ((hsp->hs_wd = malloc((size_t)WHDRSIZE)) == NULL)
218			err(1, NULL);
219		memmove(hsp->hs_wd, buf, (size_t)WHDRSIZE);
220
221		for (wd = (struct whod *)buf, i = 0; i < 2; ++i)
222			if (wd->wd_loadav[i] > maxloadav)
223				maxloadav = wd->wd_loadav[i];
224
225		for (hsp->hs_nusers = 0,
226		    we = (struct whoent *)(buf + cc); --we >= wd->wd_we;)
227			if (aflg || we->we_idle < 3600)
228				++hsp->hs_nusers;
229		++hsp;
230		++nhosts;
231	}
232	if (nhosts == 0) {
233		if (host == NULL)
234			errx(1, "no hosts in %s", _PATH_RWHODIR);
235		else
236			warnx("host %s not in %s", host, _PATH_RWHODIR);
237	}
238
239	qsort(hs, nhosts, sizeof(hs[0]), cmp);
240	for (i = 0; i < (int)nhosts; i++) {
241		hsp = &hs[i];
242		if (ISDOWN(hsp)) {
243			(void)printf("%-25.25s%s\n", hsp->hs_wd->wd_hostname,
244			    interval(now - hsp->hs_wd->wd_recvtime, "down"));
245			continue;
246		}
247		(void)printf(
248		    "%-25.25s%s,  %4d user%s  load %*.2f, %*.2f, %*.2f\n",
249		    hsp->hs_wd->wd_hostname,
250		    interval((time_t)hsp->hs_wd->wd_sendtime -
251			(time_t)hsp->hs_wd->wd_boottime, "  up"),
252		    hsp->hs_nusers,
253		    hsp->hs_nusers == 1 ? ", " : "s,",
254		    maxloadav >= 1000 ? 5 : 4,
255			hsp->hs_wd->wd_loadav[0] / 100.0,
256		    maxloadav >= 1000 ? 5 : 4,
257		        hsp->hs_wd->wd_loadav[1] / 100.0,
258		    maxloadav >= 1000 ? 5 : 4,
259		        hsp->hs_wd->wd_loadav[2] / 100.0);
260		free(hsp->hs_wd);
261	}
262	free(hs);
263	hs = NULL;
264}
265
266/* Number of users comparison. */
267int
268ucmp(const void *a1, const void *a2)
269{
270	if (ISDOWN(HS(a1)))
271		if (ISDOWN(HS(a2)))
272			return (tcmp(a1, a2));
273		else
274			return (rflg);
275	else if (ISDOWN(HS(a2)))
276		return (-rflg);
277	else
278		return (rflg * (HS(a2)->hs_nusers - HS(a1)->hs_nusers));
279}
280
281/* Uptime comparison. */
282int
283tcmp(const void *a1, const void *a2)
284{
285	return (rflg * (
286		(ISDOWN(HS(a2)) ? HS(a2)->hs_wd->wd_recvtime - now
287		    : HS(a2)->hs_wd->wd_sendtime - HS(a2)->hs_wd->wd_boottime)
288		-
289		(ISDOWN(HS(a1)) ? HS(a1)->hs_wd->wd_recvtime - now
290		    : HS(a1)->hs_wd->wd_sendtime - HS(a1)->hs_wd->wd_boottime)
291	));
292}
293
294void
295usage(void)
296{
297	(void)fprintf(stderr, "usage: ruptime [-alrtu] [host ...]\n");
298	exit(1);
299}
300