xref: /dragonfly/usr.bin/ruptime/ruptime.c (revision f9a239ec)
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  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * @(#) Copyright (c) 1983, 1993, 1994 The Regents of the University of California.  All rights reserved.
34  * @(#)ruptime.c	8.2 (Berkeley) 4/5/94
35  * $FreeBSD: src/usr.bin/ruptime/ruptime.c,v 1.12.2.1 2000/06/30 09:45:00 ps Exp $
36  * $DragonFly: src/usr.bin/ruptime/ruptime.c,v 1.8 2005/01/05 00:33:50 cpressey Exp $
37  */
38 
39 #include <sys/param.h>
40 
41 #include <protocols/rwhod.h>
42 
43 #include <dirent.h>
44 #include <err.h>
45 #include <errno.h>
46 #include <fcntl.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <time.h>
51 #include <unistd.h>
52 
53 struct hs {
54 	struct	whod *hs_wd;
55 	int	hs_nusers;
56 } *hs;
57 struct	whod awhod;
58 #define LEFTEARTH(h)		(now - (h)->hs_wd->wd_recvtime > 4*24*60*60)
59 #define	ISDOWN(h)		(now - (h)->hs_wd->wd_recvtime > 11 * 60)
60 #define	WHDRSIZE	(sizeof (awhod) - sizeof (awhod.wd_we))
61 
62 size_t nhosts;
63 time_t now;
64 int rflg = 1;
65 
66 int	 hscmp(const void *, const void *);
67 char	*interval(time_t, const char *);
68 int	 lcmp(const void *, const void *);
69 void	 morehosts(void);
70 int	 tcmp(const void *, const void *);
71 int	 ucmp(const void *, const void *);
72 void	 usage(void);
73 
74 int
75 main(int argc, char *argv[])
76 {
77 	struct dirent *dp;
78 	struct hs *hsp = NULL;
79 	struct whod *wd;
80 	struct whoent *we;
81 	DIR *dirp;
82 	size_t hspace;
83 	int aflg, ch, fd, maxloadav;
84 	char buf[sizeof(struct whod)];
85 	int (*cmp)(const void *, const void *);
86 	u_int cc, i;
87 
88 	aflg = 0;
89 	cmp = hscmp;
90 	while ((ch = getopt(argc, argv, "alrut")) != -1)
91 		switch (ch) {
92 		case 'a':
93 			aflg = 1;
94 			break;
95 		case 'l':
96 			cmp = lcmp;
97 			break;
98 		case 'r':
99 			rflg = -1;
100 			break;
101 		case 't':
102 			cmp = tcmp;
103 			break;
104 		case 'u':
105 			cmp = ucmp;
106 			break;
107 		default:
108 			usage();
109 		}
110 	argc -= optind;
111 	argv += optind;
112 
113 	if (argc != 0)
114 		usage();
115 
116 	if (chdir(_PATH_RWHODIR) || (dirp = opendir(".")) == NULL)
117 		err(1, "%s", _PATH_RWHODIR);
118 
119 	maxloadav = -1;
120 	for (nhosts = hspace = 0; (dp = readdir(dirp)) != NULL;) {
121 		if (dp->d_ino == 0 || strncmp(dp->d_name, "whod.", 5))
122 			continue;
123 		if ((fd = open(dp->d_name, O_RDONLY, 0)) < 0) {
124 			warn("%s", dp->d_name);
125 			continue;
126 		}
127 		cc = read(fd, buf, sizeof(struct whod));
128 		(void)close(fd);
129 
130 		if (cc < WHDRSIZE)
131 			continue;
132 		if (nhosts == hspace) {
133 			if ((hs =
134 			    realloc(hs, (hspace += 40) * sizeof(*hs))) == NULL)
135 				err(1, NULL);
136 			hsp = hs + nhosts;
137 		}
138 
139 		if ((hsp->hs_wd = malloc((size_t)WHDRSIZE)) == NULL)
140 			err(1, NULL);
141 		memmove(hsp->hs_wd, buf, (size_t)WHDRSIZE);
142 
143 		for (wd = (struct whod *)buf, i = 0; i < 2; ++i)
144 			if (wd->wd_loadav[i] > maxloadav)
145 				maxloadav = wd->wd_loadav[i];
146 
147 		for (hsp->hs_nusers = 0,
148 		    we = (struct whoent *)(buf + cc); --we >= wd->wd_we;)
149 			if (aflg || we->we_idle < 3600)
150 				++hsp->hs_nusers;
151 		++hsp;
152 		++nhosts;
153 	}
154 	if (nhosts == 0)
155 		errx(1, "no hosts in %s", _PATH_RWHODIR);
156 
157 	(void)time(&now);
158 	qsort(hs, nhosts, sizeof(hs[0]), cmp);
159 	for (i = 0; i < nhosts; i++) {
160 		hsp = &hs[i];
161 		if (LEFTEARTH(hsp))
162 			continue;
163 		if (ISDOWN(hsp)) {
164 			(void)printf("%-12.12s%s\n", hsp->hs_wd->wd_hostname,
165 			    interval(now - hsp->hs_wd->wd_recvtime, "down"));
166 			continue;
167 		}
168 		(void)printf(
169 		    "%-12.12s%s,  %4d user%s  load %*.2f, %*.2f, %*.2f\n",
170 		    hsp->hs_wd->wd_hostname,
171 		    interval((time_t)hsp->hs_wd->wd_sendtime -
172 			(time_t)hsp->hs_wd->wd_boottime, "  up"),
173 		    hsp->hs_nusers,
174 		    hsp->hs_nusers == 1 ? ", " : "s,",
175 		    maxloadav >= 1000 ? 5 : 4,
176 			hsp->hs_wd->wd_loadav[0] / 100.0,
177 		    maxloadav >= 1000 ? 5 : 4,
178 		        hsp->hs_wd->wd_loadav[1] / 100.0,
179 		    maxloadav >= 1000 ? 5 : 4,
180 		        hsp->hs_wd->wd_loadav[2] / 100.0);
181 	}
182 	exit(0);
183 }
184 
185 char *
186 interval(time_t tval, const char *updown)
187 {
188 	static char resbuf[32];
189 	int days, hours, minutes;
190 
191 	if (tval < 0) {
192 		(void)snprintf(resbuf, sizeof(resbuf), "   %s ??:??", updown);
193 		return (resbuf);
194 	}
195 						/* round to minutes. */
196 	minutes = (tval + (60 - 1)) / 60;
197 	hours = minutes / 60;
198 	minutes %= 60;
199 	days = hours / 24;
200 	hours %= 24;
201 	if (days)
202 		(void)snprintf(resbuf, sizeof(resbuf),
203 		    "%s %3d+%02d:%02d", updown, days, hours, minutes);
204 	else
205 		(void)snprintf(resbuf, sizeof(resbuf),
206 		    "%s     %2d:%02d", updown, hours, minutes);
207 	return (resbuf);
208 }
209 
210 #define	HS(a)	((const struct hs *)(a))
211 
212 /* Alphabetical comparison. */
213 int
214 hscmp(const void *a1, const void *a2)
215 {
216 	return (rflg *
217 	    strcmp(HS(a1)->hs_wd->wd_hostname, HS(a2)->hs_wd->wd_hostname));
218 }
219 
220 /* Load average comparison. */
221 int
222 lcmp(const void *a1, const void *a2)
223 {
224 	if (ISDOWN(HS(a1)))
225 		if (ISDOWN(HS(a2)))
226 			return (tcmp(a1, a2));
227 		else
228 			return (rflg);
229 	else if (ISDOWN(HS(a2)))
230 		return (-rflg);
231 	else
232 		return (rflg *
233 		   (HS(a2)->hs_wd->wd_loadav[0] - HS(a1)->hs_wd->wd_loadav[0]));
234 }
235 
236 /* Number of users comparison. */
237 int
238 ucmp(const void *a1, const void *a2)
239 {
240 	if (ISDOWN(HS(a1)))
241 		if (ISDOWN(HS(a2)))
242 			return (tcmp(a1, a2));
243 		else
244 			return (rflg);
245 	else if (ISDOWN(HS(a2)))
246 		return (-rflg);
247 	else
248 		return (rflg * (HS(a2)->hs_nusers - HS(a1)->hs_nusers));
249 }
250 
251 /* Uptime comparison. */
252 int
253 tcmp(const void *a1, const void *a2)
254 {
255 	return (rflg * (
256 		(ISDOWN(HS(a2)) ? HS(a2)->hs_wd->wd_recvtime - now
257 		    : HS(a2)->hs_wd->wd_sendtime - HS(a2)->hs_wd->wd_boottime)
258 		-
259 		(ISDOWN(HS(a1)) ? HS(a1)->hs_wd->wd_recvtime - now
260 		    : HS(a1)->hs_wd->wd_sendtime - HS(a1)->hs_wd->wd_boottime)
261 	));
262 }
263 
264 void
265 usage(void)
266 {
267 	(void)fprintf(stderr, "usage: ruptime [-alrut]\n");
268 	exit(1);
269 }
270