kgdb - Fix kernel trapframe backtraces for x86_64
[dragonfly.git] / usr.bin / ruptime / ruptime.c
blob87863918937e27af9e726d2bc25f27355e87704a
1 /*
2 * Copyright (c) 1983, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
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.
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.
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.11 2005/09/12 14:35:51 liamfoy Exp $
39 #include <sys/param.h>
41 #include <protocols/rwhod.h>
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>
53 struct hs {
54 struct whod *hs_wd;
55 int hs_nusers;
56 } *hs;
58 #define LEFTEARTH(h) (now - (h) > 4*24*60*60)
59 #define ISDOWN(h) (now - (h)->hs_wd->wd_recvtime > 11 * 60)
60 #define WHDRSIZE (sizeof(struct whod) - \
61 sizeof(((struct whod *)NULL)->wd_we))
63 size_t nhosts;
64 time_t now;
65 int rflg = 1;
67 int hscmp(const void *, const void *);
68 char *interval(time_t, const char *);
69 int lcmp(const void *, const void *);
70 void morehosts(void);
71 int tcmp(const void *, const void *);
72 int ucmp(const void *, const void *);
73 void usage(void);
75 int
76 main(int argc, char *argv[])
78 struct dirent *dp;
79 struct hs *hsp = NULL;
80 struct whod *wd;
81 struct whoent *we;
82 DIR *dirp;
83 size_t hspace;
84 int aflg, ch, fd, maxloadav;
85 char buf[sizeof(struct whod)];
86 int (*cmp)(const void *, const void *);
87 u_int cc, i;
89 aflg = 0;
90 cmp = hscmp;
91 while ((ch = getopt(argc, argv, "alrut")) != -1)
92 switch (ch) {
93 case 'a':
94 aflg = 1;
95 break;
96 case 'l':
97 cmp = lcmp;
98 break;
99 case 'r':
100 rflg = -1;
101 break;
102 case 't':
103 cmp = tcmp;
104 break;
105 case 'u':
106 cmp = ucmp;
107 break;
108 default:
109 usage();
111 argc -= optind;
112 argv += optind;
114 if (argc != 0)
115 usage();
117 if (chdir(_PATH_RWHODIR) || (dirp = opendir(".")) == NULL)
118 err(1, "%s", _PATH_RWHODIR);
120 maxloadav = -1;
121 for (nhosts = hspace = 0; (dp = readdir(dirp)) != NULL;) {
122 if (dp->d_ino == 0 || strncmp(dp->d_name, "whod.", 5))
123 continue;
124 if ((fd = open(dp->d_name, O_RDONLY, 0)) < 0) {
125 warn("%s", dp->d_name);
126 continue;
128 cc = read(fd, buf, sizeof(struct whod));
129 (void)close(fd);
131 if (cc < WHDRSIZE)
132 continue;
133 if (LEFTEARTH(((struct whod *)buf)->wd_recvtime))
134 continue;
135 if (nhosts == hspace) {
136 hspace += 40;
137 if ((hs = realloc(hs, hspace * sizeof(*hs))) == NULL)
138 err(1, NULL);
139 hsp = hs + nhosts;
142 if ((hsp->hs_wd = malloc((size_t)WHDRSIZE)) == NULL)
143 err(1, NULL);
144 memmove(hsp->hs_wd, buf, (size_t)WHDRSIZE);
146 for (wd = (struct whod *)buf, i = 0; i < 2; ++i)
147 if (wd->wd_loadav[i] > maxloadav)
148 maxloadav = wd->wd_loadav[i];
150 for (hsp->hs_nusers = 0,
151 we = (struct whoent *)(buf + cc); --we >= wd->wd_we;)
152 if (aflg || we->we_idle < 3600)
153 ++hsp->hs_nusers;
154 ++hsp;
155 ++nhosts;
157 if (nhosts == 0)
158 errx(1, "no hosts in %s", _PATH_RWHODIR);
160 (void)time(&now);
161 qsort(hs, nhosts, sizeof(hs[0]), cmp);
162 for (i = 0; i < nhosts; i++) {
163 hsp = &hs[i];
164 if (ISDOWN(hsp)) {
165 (void)printf("%-12.12s%s\n", hsp->hs_wd->wd_hostname,
166 interval(now - hsp->hs_wd->wd_recvtime, "down"));
167 continue;
169 (void)printf(
170 "%-12.12s%s, %4d user%s load %*.2f, %*.2f, %*.2f\n",
171 hsp->hs_wd->wd_hostname,
172 interval((time_t)hsp->hs_wd->wd_sendtime -
173 (time_t)hsp->hs_wd->wd_boottime, " up"),
174 hsp->hs_nusers,
175 hsp->hs_nusers == 1 ? ", " : "s,",
176 maxloadav >= 1000 ? 5 : 4,
177 hsp->hs_wd->wd_loadav[0] / 100.0,
178 maxloadav >= 1000 ? 5 : 4,
179 hsp->hs_wd->wd_loadav[1] / 100.0,
180 maxloadav >= 1000 ? 5 : 4,
181 hsp->hs_wd->wd_loadav[2] / 100.0);
183 exit(0);
186 char *
187 interval(time_t tval, const char *updown)
189 static char resbuf[32];
190 int days, hours, minutes;
192 if (tval < 0) {
193 (void)snprintf(resbuf, sizeof(resbuf), " %s ??:??", updown);
194 return (resbuf);
196 /* round to minutes. */
197 minutes = (tval + (60 - 1)) / 60;
198 hours = minutes / 60;
199 minutes %= 60;
200 days = hours / 24;
201 hours %= 24;
202 if (days)
203 (void)snprintf(resbuf, sizeof(resbuf),
204 "%s %3d+%02d:%02d", updown, days, hours, minutes);
205 else
206 (void)snprintf(resbuf, sizeof(resbuf),
207 "%s %2d:%02d", updown, hours, minutes);
208 return (resbuf);
211 #define HS(a) ((const struct hs *)(a))
213 /* Alphabetical comparison. */
215 hscmp(const void *a1, const void *a2)
217 return (rflg *
218 strcmp(HS(a1)->hs_wd->wd_hostname, HS(a2)->hs_wd->wd_hostname));
221 /* Load average comparison. */
223 lcmp(const void *a1, const void *a2)
225 if (ISDOWN(HS(a1)))
226 if (ISDOWN(HS(a2)))
227 return (tcmp(a1, a2));
228 else
229 return (rflg);
230 else if (ISDOWN(HS(a2)))
231 return (-rflg);
232 else
233 return (rflg *
234 (HS(a2)->hs_wd->wd_loadav[0] - HS(a1)->hs_wd->wd_loadav[0]));
237 /* Number of users comparison. */
239 ucmp(const void *a1, const void *a2)
241 if (ISDOWN(HS(a1)))
242 if (ISDOWN(HS(a2)))
243 return (tcmp(a1, a2));
244 else
245 return (rflg);
246 else if (ISDOWN(HS(a2)))
247 return (-rflg);
248 else
249 return (rflg * (HS(a2)->hs_nusers - HS(a1)->hs_nusers));
252 /* Uptime comparison. */
254 tcmp(const void *a1, const void *a2)
256 return (rflg * (
257 (ISDOWN(HS(a2)) ? HS(a2)->hs_wd->wd_recvtime - now
258 : HS(a2)->hs_wd->wd_sendtime - HS(a2)->hs_wd->wd_boottime)
260 (ISDOWN(HS(a1)) ? HS(a1)->hs_wd->wd_recvtime - now
261 : HS(a1)->hs_wd->wd_sendtime - HS(a1)->hs_wd->wd_boottime)
265 void
266 usage(void)
268 (void)fprintf(stderr, "usage: ruptime [-alrut]\n");
269 exit(1);