11590Srgrimes/*
21590Srgrimes * Copyright (c) 1983, 1993, 1994
31590Srgrimes *	The Regents of the University of California.  All rights reserved.
41590Srgrimes *
51590Srgrimes * Redistribution and use in source and binary forms, with or without
61590Srgrimes * modification, are permitted provided that the following conditions
71590Srgrimes * are met:
81590Srgrimes * 1. Redistributions of source code must retain the above copyright
91590Srgrimes *    notice, this list of conditions and the following disclaimer.
101590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111590Srgrimes *    notice, this list of conditions and the following disclaimer in the
121590Srgrimes *    documentation and/or other materials provided with the distribution.
131590Srgrimes * 4. Neither the name of the University nor the names of its contributors
141590Srgrimes *    may be used to endorse or promote products derived from this software
151590Srgrimes *    without specific prior written permission.
161590Srgrimes *
171590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
181590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
191590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
201590Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
211590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
221590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
231590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
241590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
251590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
261590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
271590Srgrimes * SUCH DAMAGE.
281590Srgrimes */
291590Srgrimes
301590Srgrimes#ifndef lint
3158620Scharnierstatic const char copyright[] =
321590Srgrimes"@(#) Copyright (c) 1983, 1993, 1994\n\
331590Srgrimes	The Regents of the University of California.  All rights reserved.\n";
341590Srgrimes#endif /* not lint */
351590Srgrimes
361590Srgrimes#ifndef lint
3795617Smarkmstatic const char sccsid[] = "@(#)ruptime.c	8.2 (Berkeley) 4/5/94";
381590Srgrimes#endif /* not lint */
391590Srgrimes
4095617Smarkm#include <sys/cdefs.h>
4195617Smarkm__FBSDID("$FreeBSD: stable/11/usr.bin/ruptime/ruptime.c 359799 2020-04-11 07:46:38Z nyan $");
4295617Smarkm
431590Srgrimes#include <sys/param.h>
441590Srgrimes
451590Srgrimes#include <protocols/rwhod.h>
461590Srgrimes
471590Srgrimes#include <dirent.h>
481590Srgrimes#include <err.h>
49200462Sdelphij#include <errno.h>
501590Srgrimes#include <fcntl.h>
511590Srgrimes#include <stdio.h>
521590Srgrimes#include <stdlib.h>
531590Srgrimes#include <string.h>
541590Srgrimes#include <time.h>
551590Srgrimes#include <unistd.h>
561590Srgrimes
57227178Sedstatic struct hs {
58226419Sed	struct	whod hs_wd;
591590Srgrimes	int	hs_nusers;
601590Srgrimes} *hs;
61226419Sed#define	LEFTEARTH(h)	(now - (h) > 4*24*60*60)
62226419Sed#define	ISDOWN(h)	(now - (h)->hs_wd.wd_recvtime > 11 * 60)
63226419Sed#define	WHDRSIZE	__offsetof(struct whod, wd_we)
641590Srgrimes
65227178Sedstatic size_t nhosts;
66227178Sedstatic time_t now;
67227178Sedstatic int rflg = 1;
68227178Sedstatic DIR *dirp;
691590Srgrimes
70227178Sedstatic int	 hscmp(const void *, const void *);
71227178Sedstatic char	*interval(time_t, const char *);
72359798Snyanstatic int	 iwidth(int);
73227178Sedstatic int	 lcmp(const void *, const void *);
74227178Sedstatic void	 ruptime(const char *, int, int (*)(const void *, const void *));
75227178Sedstatic int	 tcmp(const void *, const void *);
76227178Sedstatic int	 ucmp(const void *, const void *);
77227178Sedstatic void	 usage(void);
781590Srgrimes
791590Srgrimesint
8095617Smarkmmain(int argc, char *argv[])
811590Srgrimes{
8292921Simp	int (*cmp)(const void *, const void *);
83111714Sjmallett	int aflg, ch;
841590Srgrimes
851590Srgrimes	aflg = 0;
861590Srgrimes	cmp = hscmp;
8724360Simp	while ((ch = getopt(argc, argv, "alrut")) != -1)
881590Srgrimes		switch (ch) {
891590Srgrimes		case 'a':
901590Srgrimes			aflg = 1;
911590Srgrimes			break;
921590Srgrimes		case 'l':
931590Srgrimes			cmp = lcmp;
941590Srgrimes			break;
951590Srgrimes		case 'r':
961590Srgrimes			rflg = -1;
971590Srgrimes			break;
981590Srgrimes		case 't':
991590Srgrimes			cmp = tcmp;
1001590Srgrimes			break;
1011590Srgrimes		case 'u':
1021590Srgrimes			cmp = ucmp;
1031590Srgrimes			break;
1048874Srgrimes		default:
1051590Srgrimes			usage();
1061590Srgrimes		}
1071590Srgrimes	argc -= optind;
1081590Srgrimes	argv += optind;
1091590Srgrimes
1101590Srgrimes	if (chdir(_PATH_RWHODIR) || (dirp = opendir(".")) == NULL)
1111590Srgrimes		err(1, "%s", _PATH_RWHODIR);
1121590Srgrimes
113111714Sjmallett	ruptime(*argv, aflg, cmp);
114111714Sjmallett	while (*argv++ != NULL) {
115111714Sjmallett		if (*argv == NULL)
116111714Sjmallett			break;
117111714Sjmallett		ruptime(*argv, aflg, cmp);
118111714Sjmallett	}
119111714Sjmallett	exit(0);
120111714Sjmallett}
121111714Sjmallett
122227178Sedstatic char *
123111714Sjmallettinterval(time_t tval, const char *updown)
124111714Sjmallett{
125111714Sjmallett	static char resbuf[32];
126111714Sjmallett	int days, hours, minutes;
127111714Sjmallett
128111714Sjmallett	if (tval < 0) {
129226419Sed		(void)snprintf(resbuf, sizeof(resbuf), "%s      ??:??", updown);
130111714Sjmallett		return (resbuf);
131111714Sjmallett	}
132226419Sed	/* Round to minutes. */
133111714Sjmallett	minutes = (tval + (60 - 1)) / 60;
134111714Sjmallett	hours = minutes / 60;
135111714Sjmallett	minutes %= 60;
136111714Sjmallett	days = hours / 24;
137111714Sjmallett	hours %= 24;
138111714Sjmallett	if (days)
139111714Sjmallett		(void)snprintf(resbuf, sizeof(resbuf),
140226419Sed		    "%s %4d+%02d:%02d", updown, days, hours, minutes);
141111714Sjmallett	else
142111714Sjmallett		(void)snprintf(resbuf, sizeof(resbuf),
143226419Sed		    "%s      %2d:%02d", updown, hours, minutes);
144111714Sjmallett	return (resbuf);
145111714Sjmallett}
146111714Sjmallett
147359798Snyan/* Width to print a small nonnegative integer. */
148359798Snyanstatic int
149359798Snyaniwidth(int w)
150359798Snyan{
151359798Snyan	if (w < 10)
152359798Snyan		return (1);
153359798Snyan	if (w < 100)
154359798Snyan		return (2);
155359798Snyan	if (w < 1000)
156359798Snyan		return (3);
157359798Snyan	if (w < 10000)
158359798Snyan		return (4);
159359798Snyan	return (5);
160359798Snyan}
161359798Snyan
162111714Sjmallett#define	HS(a)	((const struct hs *)(a))
163111714Sjmallett
164111714Sjmallett/* Alphabetical comparison. */
165227178Sedstatic int
166111714Sjmalletthscmp(const void *a1, const void *a2)
167111714Sjmallett{
168111714Sjmallett	return (rflg *
169226419Sed	    strcmp(HS(a1)->hs_wd.wd_hostname, HS(a2)->hs_wd.wd_hostname));
170111714Sjmallett}
171111714Sjmallett
172111714Sjmallett/* Load average comparison. */
173227178Sedstatic int
174111714Sjmallettlcmp(const void *a1, const void *a2)
175111714Sjmallett{
176111714Sjmallett	if (ISDOWN(HS(a1)))
177111714Sjmallett		if (ISDOWN(HS(a2)))
178111714Sjmallett			return (tcmp(a1, a2));
179111714Sjmallett		else
180111714Sjmallett			return (rflg);
181111714Sjmallett	else if (ISDOWN(HS(a2)))
182111714Sjmallett		return (-rflg);
183111714Sjmallett	else
184111714Sjmallett		return (rflg *
185226419Sed		   (HS(a2)->hs_wd.wd_loadav[0] - HS(a1)->hs_wd.wd_loadav[0]));
186111714Sjmallett}
187111714Sjmallett
188227178Sedstatic void
189111714Sjmallettruptime(const char *host, int aflg, int (*cmp)(const void *, const void *))
190111714Sjmallett{
191111714Sjmallett	struct hs *hsp;
192111714Sjmallett	struct whod *wd;
193111714Sjmallett	struct whoent *we;
194111714Sjmallett	struct dirent *dp;
195359798Snyan	int fd, hostnamewidth, i, loadavwidth[3], userswidth, w;
196111714Sjmallett	size_t hspace;
197226419Sed	ssize_t cc;
198111714Sjmallett
199111714Sjmallett	rewinddir(dirp);
200111714Sjmallett	hsp = NULL;
201359798Snyan	hostnamewidth = 0;
202359798Snyan	loadavwidth[0] = 4;
203359798Snyan	loadavwidth[1] = 4;
204359798Snyan	loadavwidth[2] = 4;
205359798Snyan	userswidth = 1;
206177316Santoine	(void)time(&now);
2071590Srgrimes	for (nhosts = hspace = 0; (dp = readdir(dirp)) != NULL;) {
208111714Sjmallett		if (dp->d_ino == 0 || strncmp(dp->d_name, "whod.", 5) != 0)
2091590Srgrimes			continue;
2101590Srgrimes		if ((fd = open(dp->d_name, O_RDONLY, 0)) < 0) {
2111590Srgrimes			warn("%s", dp->d_name);
2121590Srgrimes			continue;
2131590Srgrimes		}
2141590Srgrimes
2151590Srgrimes		if (nhosts == hspace) {
2161590Srgrimes			if ((hs =
2171590Srgrimes			    realloc(hs, (hspace += 40) * sizeof(*hs))) == NULL)
2181590Srgrimes				err(1, NULL);
2191590Srgrimes			hsp = hs + nhosts;
2201590Srgrimes		}
2211590Srgrimes
222226419Sed		wd = &hsp->hs_wd;
223226419Sed		cc = read(fd, wd, sizeof(*wd));
224226419Sed		(void)close(fd);
225226419Sed		if (cc < (ssize_t)WHDRSIZE)
226226419Sed			continue;
2271590Srgrimes
228359798Snyan		if (host != NULL && strcasecmp(wd->wd_hostname, host) != 0)
229359798Snyan			continue;
230226419Sed		if (LEFTEARTH(wd->wd_recvtime))
231226419Sed			continue;
232226419Sed
233359798Snyan		if (hostnamewidth < (int)strlen(wd->wd_hostname))
234359798Snyan			hostnamewidth = (int)strlen(wd->wd_hostname);
235359799Snyan
236359799Snyan		if (!ISDOWN(hsp)) {
237359799Snyan			for (i = 0; i < 3; i++) {
238359799Snyan				w = iwidth(wd->wd_loadav[i] / 100) + 3;
239359799Snyan				if (loadavwidth[i] < w)
240359799Snyan					loadavwidth[i] = w;
241359799Snyan			}
242359799Snyan			for (hsp->hs_nusers = 0, we = &wd->wd_we[0];
243359799Snyan			     (char *)(we + 1) <= (char *)wd + cc; we++)
244359799Snyan				if (aflg || we->we_idle < 3600)
245359799Snyan					++hsp->hs_nusers;
246359799Snyan			if (userswidth < iwidth(hsp->hs_nusers))
247359799Snyan				userswidth = iwidth(hsp->hs_nusers);
248359798Snyan		}
2491590Srgrimes
2501590Srgrimes		++hsp;
2511590Srgrimes		++nhosts;
2521590Srgrimes	}
253111714Sjmallett	if (nhosts == 0) {
254111714Sjmallett		if (host == NULL)
255111714Sjmallett			errx(1, "no hosts in %s", _PATH_RWHODIR);
256111714Sjmallett		else
257111714Sjmallett			warnx("host %s not in %s", host, _PATH_RWHODIR);
258111714Sjmallett	}
2591590Srgrimes
2601590Srgrimes	qsort(hs, nhosts, sizeof(hs[0]), cmp);
261359798Snyan	w = userswidth + loadavwidth[0] + loadavwidth[1] + loadavwidth[2];
262359798Snyan	if (hostnamewidth + w > 41)
263359798Snyan		hostnamewidth = 41 - w;	/* limit to 79 cols */
26495617Smarkm	for (i = 0; i < (int)nhosts; i++) {
2651590Srgrimes		hsp = &hs[i];
266226419Sed		wd = &hsp->hs_wd;
2671590Srgrimes		if (ISDOWN(hsp)) {
268359799Snyan			(void)printf("%-*.*s  %s\n",
269359798Snyan			    hostnamewidth, hostnamewidth, wd->wd_hostname,
270226419Sed			    interval(now - hsp->hs_wd.wd_recvtime, "down"));
2711590Srgrimes			continue;
2721590Srgrimes		}
2731590Srgrimes		(void)printf(
274359798Snyan		    "%-*.*s  %s,  %*d user%s  load %*.2f, %*.2f, %*.2f\n",
275359798Snyan		    hostnamewidth, hostnamewidth, wd->wd_hostname,
276226419Sed		    interval((time_t)wd->wd_sendtime -
277226419Sed		        (time_t)wd->wd_boottime, "  up"),
278359798Snyan		    userswidth, hsp->hs_nusers,
2791590Srgrimes		    hsp->hs_nusers == 1 ? ", " : "s,",
280359798Snyan		    loadavwidth[0], wd->wd_loadav[0] / 100.0,
281359798Snyan		    loadavwidth[1], wd->wd_loadav[1] / 100.0,
282359798Snyan		    loadavwidth[2], wd->wd_loadav[2] / 100.0);
2831590Srgrimes	}
284111714Sjmallett	free(hs);
285111714Sjmallett	hs = NULL;
2861590Srgrimes}
2871590Srgrimes
2881590Srgrimes/* Number of users comparison. */
289227178Sedstatic int
29095617Smarkmucmp(const void *a1, const void *a2)
2911590Srgrimes{
2921590Srgrimes	if (ISDOWN(HS(a1)))
2931590Srgrimes		if (ISDOWN(HS(a2)))
2941590Srgrimes			return (tcmp(a1, a2));
2951590Srgrimes		else
2961590Srgrimes			return (rflg);
2971590Srgrimes	else if (ISDOWN(HS(a2)))
2981590Srgrimes		return (-rflg);
2991590Srgrimes	else
3001590Srgrimes		return (rflg * (HS(a2)->hs_nusers - HS(a1)->hs_nusers));
3011590Srgrimes}
3021590Srgrimes
3031590Srgrimes/* Uptime comparison. */
304227178Sedstatic int
30595617Smarkmtcmp(const void *a1, const void *a2)
3061590Srgrimes{
3071590Srgrimes	return (rflg * (
308226419Sed		(ISDOWN(HS(a2)) ? HS(a2)->hs_wd.wd_recvtime - now
309226419Sed		    : HS(a2)->hs_wd.wd_sendtime - HS(a2)->hs_wd.wd_boottime)
3101590Srgrimes		-
311226419Sed		(ISDOWN(HS(a1)) ? HS(a1)->hs_wd.wd_recvtime - now
312226419Sed		    : HS(a1)->hs_wd.wd_sendtime - HS(a1)->hs_wd.wd_boottime)
3131590Srgrimes	));
3141590Srgrimes}
3151590Srgrimes
316227178Sedstatic void
31795617Smarkmusage(void)
3181590Srgrimes{
319146466Sru	(void)fprintf(stderr, "usage: ruptime [-alrtu] [host ...]\n");
3201590Srgrimes	exit(1);
3211590Srgrimes}
322