aboutsummaryrefslogtreecommitdiff
path: root/usr.bin/ruptime/ruptime.c
blob: 62a771b02d39d3ea9b9412566fad38f460515898 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/*
 * SPDX-License-Identifier: BSD-3-Clause
 *
 * Copyright (c) 1983, 1993, 1994
 *	The Regents of the University of California.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#ifndef lint
static const char copyright[] =
"@(#) Copyright (c) 1983, 1993, 1994\n\
	The Regents of the University of California.  All rights reserved.\n";
#endif /* not lint */

#ifndef lint
static const char sccsid[] = "@(#)ruptime.c	8.2 (Berkeley) 4/5/94";
#endif /* not lint */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <sys/param.h>

#include <protocols/rwhod.h>

#include <dirent.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>

static struct hs {
	struct	whod hs_wd;
	int	hs_nusers;
} *hs;
#define	LEFTEARTH(h)	(now - (h) > 4*24*60*60)
#define	ISDOWN(h)	(now - (h)->hs_wd.wd_recvtime > 11 * 60)
#define	WHDRSIZE	__offsetof(struct whod, wd_we)

static size_t nhosts;
static time_t now;
static int rflg = 1;
static DIR *dirp;

static int	 hscmp(const void *, const void *);
static char	*interval(time_t, const char *);
static int	 iwidth(int);
static int	 lcmp(const void *, const void *);
static void	 ruptime(const char *, int, int (*)(const void *, const void *));
static int	 tcmp(const void *, const void *);
static int	 ucmp(const void *, const void *);
static void	 usage(void);

int
main(int argc, char *argv[])
{
	int (*cmp)(const void *, const void *);
	int aflg, ch;

	aflg = 0;
	cmp = hscmp;
	while ((ch = getopt(argc, argv, "alrut")) != -1)
		switch (ch) {
		case 'a':
			aflg = 1;
			break;
		case 'l':
			cmp = lcmp;
			break;
		case 'r':
			rflg = -1;
			break;
		case 't':
			cmp = tcmp;
			break;
		case 'u':
			cmp = ucmp;
			break;
		default:
			usage();
		}
	argc -= optind;
	argv += optind;

	if (chdir(_PATH_RWHODIR) || (dirp = opendir(".")) == NULL)
		err(1, "%s", _PATH_RWHODIR);

	ruptime(*argv, aflg, cmp);
	while (*argv++ != NULL) {
		if (*argv == NULL)
			break;
		ruptime(*argv, aflg, cmp);
	}
	exit(0);
}

static char *
interval(time_t tval, const char *updown)
{
	static char resbuf[32];
	int days, hours, minutes;

	if (tval < 0) {
		(void)snprintf(resbuf, sizeof(resbuf), "%s      ??:??", updown);
		return (resbuf);
	}
	/* Round to minutes. */
	minutes = (tval + (60 - 1)) / 60;
	hours = minutes / 60;
	minutes %= 60;
	days = hours / 24;
	hours %= 24;
	if (days)
		(void)snprintf(resbuf, sizeof(resbuf),
		    "%s %4d+%02d:%02d", updown, days, hours, minutes);
	else
		(void)snprintf(resbuf, sizeof(resbuf),
		    "%s      %2d:%02d", updown, hours, minutes);
	return (resbuf);
}

/* Width to print a small nonnegative integer. */
static int
iwidth(int w)
{
	if (w < 10)
		return (1);
	if (w < 100)
		return (2);
	if (w < 1000)
		return (3);
	if (w < 10000)
		return (4);
	return (5);
}

#define	HS(a)	((const struct hs *)(a))

/* Alphabetical comparison. */
static int
hscmp(const void *a1, const void *a2)
{
	return (rflg *
	    strcmp(HS(a1)->hs_wd.wd_hostname, HS(a2)->hs_wd.wd_hostname));
}

/* Load average comparison. */
static int
lcmp(const void *a1, const void *a2)
{
	if (ISDOWN(HS(a1)))
		if (ISDOWN(HS(a2)))
			return (tcmp(a1, a2));
		else
			return (rflg);
	else if (ISDOWN(HS(a2)))
		return (-rflg);
	else
		return (rflg *
		   (HS(a2)->hs_wd.wd_loadav[0] - HS(a1)->hs_wd.wd_loadav[0]));
}

static void
ruptime(const char *host, int aflg, int (*cmp)(const void *, const void *))
{
	struct hs *hsp;
	struct whod *wd;
	struct whoent *we;
	struct dirent *dp;
	int fd, hostnamewidth, i, loadavwidth[3], userswidth, w;
	size_t hspace;
	ssize_t cc;

	rewinddir(dirp);
	hsp = NULL;
	hostnamewidth = 0;
	loadavwidth[0] = 4;
	loadavwidth[1] = 4;
	loadavwidth[2] = 4;
	userswidth = 1;
	(void)time(&now);
	for (nhosts = hspace = 0; (dp = readdir(dirp)) != NULL;) {
		if (dp->d_ino == 0 || strncmp(dp->d_name, "whod.", 5) != 0)
			continue;
		if ((fd = open(dp->d_name, O_RDONLY, 0)) < 0) {
			warn("%s", dp->d_name);
			continue;
		}

		if (nhosts == hspace) {
			if ((hs =
			    realloc(hs, (hspace += 40) * sizeof(*hs))) == NULL)
				err(1, NULL);
			hsp = hs + nhosts;
		}

		wd = &hsp->hs_wd;
		cc = read(fd, wd, sizeof(*wd));
		(void)close(fd);
		if (cc < (ssize_t)WHDRSIZE)
			continue;

		if (host != NULL && strcasecmp(wd->wd_hostname, host) != 0)
			continue;
		if (LEFTEARTH(wd->wd_recvtime))
			continue;

		if (hostnamewidth < (int)strlen(wd->wd_hostname))
			hostnamewidth = (int)strlen(wd->wd_hostname);

		if (!ISDOWN(hsp)) {
			for (i = 0; i < 3; i++) {
				w = iwidth(wd->wd_loadav[i] / 100) + 3;
				if (loadavwidth[i] < w)
					loadavwidth[i] = w;
			}
			for (hsp->hs_nusers = 0, we = &wd->wd_we[0];
			     (char *)(we + 1) <= (char *)wd + cc; we++)
				if (aflg || we->we_idle < 3600)
					++hsp->hs_nusers;
			if (userswidth < iwidth(hsp->hs_nusers))
				userswidth = iwidth(hsp->hs_nusers);
		}

		++hsp;
		++nhosts;
	}
	if (nhosts == 0) {
		if (host == NULL)
			errx(1, "no hosts in %s", _PATH_RWHODIR);
		else
			warnx("host %s not in %s", host, _PATH_RWHODIR);
	}

	qsort(hs, nhosts, sizeof(hs[0]), cmp);
	w = userswidth + loadavwidth[0] + loadavwidth[1] + loadavwidth[2];
	if (hostnamewidth + w > 41)
		hostnamewidth = 41 - w;	/* limit to 79 cols */
	for (i = 0; i < (int)nhosts; i++) {
		hsp = &hs[i];
		wd = &hsp->hs_wd;
		if (ISDOWN(hsp)) {
			(void)printf("%-*.*s  %s\n",
			    hostnamewidth, hostnamewidth, wd->wd_hostname,
			    interval(now - hsp->hs_wd.wd_recvtime, "down"));
			continue;
		}
		(void)printf(
		    "%-*.*s  %s,  %*d user%s  load %*.2f, %*.2f, %*.2f\n",
		    hostnamewidth, hostnamewidth, wd->wd_hostname,
		    interval((time_t)wd->wd_sendtime -
		        (time_t)wd->wd_boottime, "  up"),
		    userswidth, hsp->hs_nusers,
		    hsp->hs_nusers == 1 ? ", " : "s,",
		    loadavwidth[0], wd->wd_loadav[0] / 100.0,
		    loadavwidth[1], wd->wd_loadav[1] / 100.0,
		    loadavwidth[2], wd->wd_loadav[2] / 100.0);
	}
	free(hs);
	hs = NULL;
}

/* Number of users comparison. */
static int
ucmp(const void *a1, const void *a2)
{
	if (ISDOWN(HS(a1)))
		if (ISDOWN(HS(a2)))
			return (tcmp(a1, a2));
		else
			return (rflg);
	else if (ISDOWN(HS(a2)))
		return (-rflg);
	else
		return (rflg * (HS(a2)->hs_nusers - HS(a1)->hs_nusers));
}

/* Uptime comparison. */
static int
tcmp(const void *a1, const void *a2)
{
	return (rflg * (
		(ISDOWN(HS(a2)) ? HS(a2)->hs_wd.wd_recvtime - now
		    : HS(a2)->hs_wd.wd_sendtime - HS(a2)->hs_wd.wd_boottime)
		-
		(ISDOWN(HS(a1)) ? HS(a1)->hs_wd.wd_recvtime - now
		    : HS(a1)->hs_wd.wd_sendtime - HS(a1)->hs_wd.wd_boottime)
	));
}

static void
usage(void)
{
	(void)fprintf(stderr, "usage: ruptime [-alrtu] [host ...]\n");
	exit(1);
}