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