Annotation of src/usr.bin/ruptime/ruptime.c, Revision 1.13
1.13 ! deraadt 1: /* $OpenBSD: ruptime.c,v 1.12 2003/08/04 17:06:45 deraadt Exp $ */
1.2 deraadt 2:
1.1 deraadt 3: /*
4: * Copyright (c) 1983 The Regents of the University of California.
5: * All rights reserved.
6: *
7: * Redistribution and use in source and binary forms, with or without
8: * modification, are permitted provided that the following conditions
9: * are met:
10: * 1. Redistributions of source code must retain the above copyright
11: * notice, this list of conditions and the following disclaimer.
12: * 2. Redistributions in binary form must reproduce the above copyright
13: * notice, this list of conditions and the following disclaimer in the
14: * documentation and/or other materials provided with the distribution.
1.9 millert 15: * 3. Neither the name of the University nor the names of its contributors
1.1 deraadt 16: * may be used to endorse or promote products derived from this software
17: * without specific prior written permission.
18: *
19: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29: * SUCH DAMAGE.
30: */
31:
32: #ifndef lint
33: char copyright[] =
34: "@(#) Copyright (c) 1983 The Regents of the University of California.\n\
35: All rights reserved.\n";
36: #endif /* not lint */
37:
38: #ifndef lint
39: /*static char sccsid[] = "from: @(#)ruptime.c 5.8 (Berkeley) 7/21/90";*/
1.13 ! deraadt 40: static char rcsid[] = "$OpenBSD: ruptime.c,v 1.12 2003/08/04 17:06:45 deraadt Exp $";
1.1 deraadt 41: #endif /* not lint */
42:
43: #include <sys/param.h>
44: #include <sys/file.h>
45: #include <dirent.h>
46: #include <protocols/rwhod.h>
47: #include <stdio.h>
1.4 deraadt 48: #include <unistd.h>
1.1 deraadt 49: #include <stdlib.h>
50: #include <string.h>
51: #include <errno.h>
52:
53: size_t nhosts, hspace = 20;
54: struct hs {
55: struct whod *hs_wd;
56: int hs_nusers;
57: } *hs;
58: struct whod awhod;
59:
60: #define ISDOWN(h) (now - (h)->hs_wd->wd_recvtime > 11 * 60)
61: #define WHDRSIZE (sizeof (awhod) - sizeof (awhod.wd_we))
62:
63: time_t now;
64: int rflg = 1;
1.11 deraadt 65: int hscmp(const void *, const void *);
66: int ucmp(const void *, const void *);
67: int lcmp(const void *, const void *);
68: int tcmp(const void *, const void *);
1.12 deraadt 69: char *interval(time_t, char *);
1.1 deraadt 70:
1.7 millert 71: void morehosts(void);
1.4 deraadt 72:
73: int
1.10 deraadt 74: main(int argc, char *argv[])
1.1 deraadt 75: {
1.6 mpech 76: struct hs *hsp;
77: struct whod *wd;
78: struct whoent *we;
79: DIR *dirp;
1.1 deraadt 80: struct dirent *dp;
81: int aflg, cc, ch, f, i, maxloadav;
82: char buf[sizeof(struct whod)];
1.11 deraadt 83: int (*cmp)(const void *, const void *) = hscmp;
1.1 deraadt 84:
85: aflg = 0;
1.3 millert 86: while ((ch = getopt(argc, argv, "alrut")) != -1)
1.1 deraadt 87: switch((char)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: (void)fprintf(stderr, "usage: ruptime [-alrut]\n");
105: exit(1);
106: }
107:
108: if (chdir(_PATH_RWHODIR) || (dirp = opendir(".")) == NULL) {
109: (void)fprintf(stderr, "ruptime: %s: %s.\n",
110: _PATH_RWHODIR, strerror(errno));
111: exit(1);
112: }
113: morehosts();
114: hsp = hs;
115: maxloadav = -1;
1.4 deraadt 116: while ((dp = readdir(dirp))) {
1.1 deraadt 117: if (dp->d_ino == 0 || strncmp(dp->d_name, "whod.", 5))
118: continue;
119: if ((f = open(dp->d_name, O_RDONLY, 0)) < 0) {
120: (void)fprintf(stderr, "ruptime: %s: %s\n",
121: dp->d_name, strerror(errno));
122: continue;
123: }
124: cc = read(f, buf, sizeof(struct whod));
125: (void)close(f);
126: if (cc < WHDRSIZE)
127: continue;
128: if (nhosts == hspace) {
129: morehosts();
130: hsp = hs + nhosts;
131: }
132: /* NOSTRICT */
133: hsp->hs_wd = malloc((size_t)WHDRSIZE);
134: wd = (struct whod *)buf;
135: bcopy((char *)wd, (char *)hsp->hs_wd, (size_t)WHDRSIZE);
136: hsp->hs_nusers = 0;
137: for (i = 0; i < 2; i++)
138: if (wd->wd_loadav[i] > maxloadav)
139: maxloadav = wd->wd_loadav[i];
140: we = (struct whoent *)(buf+cc);
141: while (--we >= wd->wd_we)
142: if (aflg || we->we_idle < 3600)
143: hsp->hs_nusers++;
144: nhosts++;
145: hsp++;
146: }
147: if (!nhosts) {
148: (void)printf("ruptime: no hosts in %s.\n", _PATH_RWHODIR);
149: exit(1);
150: }
151: (void)time(&now);
152: qsort((char *)hs, nhosts, sizeof (hs[0]), cmp);
153: for (i = 0; i < nhosts; i++) {
154: hsp = &hs[i];
155: if (ISDOWN(hsp)) {
156: (void)printf("%-12.12s%s\n", hsp->hs_wd->wd_hostname,
157: interval(now - hsp->hs_wd->wd_recvtime, "down"));
158: continue;
159: }
160: (void)printf(
161: "%-12.12s%s, %4d user%s load %*.2f, %*.2f, %*.2f\n",
162: hsp->hs_wd->wd_hostname,
163: interval((time_t)hsp->hs_wd->wd_sendtime -
164: (time_t)hsp->hs_wd->wd_boottime, " up"),
165: hsp->hs_nusers,
166: hsp->hs_nusers == 1 ? ", " : "s,",
167: maxloadav >= 1000 ? 5 : 4,
168: hsp->hs_wd->wd_loadav[0] / 100.0,
169: maxloadav >= 1000 ? 5 : 4,
170: hsp->hs_wd->wd_loadav[1] / 100.0,
171: maxloadav >= 1000 ? 5 : 4,
172: hsp->hs_wd->wd_loadav[2] / 100.0);
173: free((void *)hsp->hs_wd);
174: }
175: exit(0);
176: }
177:
178: char *
1.10 deraadt 179: interval(time_t tval, char *updown)
1.1 deraadt 180: {
181: static char resbuf[32];
182: int days, hours, minutes;
183:
1.5 millert 184: if (tval < 0 || tval > 999*24*60*60) {
1.8 deraadt 185: (void)snprintf(resbuf, sizeof resbuf, "%s ??:??", updown);
1.1 deraadt 186: return(resbuf);
187: }
188: minutes = (tval + 59) / 60; /* round to minutes */
189: hours = minutes / 60; minutes %= 60;
190: days = hours / 24; hours %= 24;
191: if (days)
1.8 deraadt 192: (void)snprintf(resbuf, sizeof resbuf, "%s %3d+%02d:%02d",
1.1 deraadt 193: updown, days, hours, minutes);
194: else
1.8 deraadt 195: (void)snprintf(resbuf, sizeof resbuf, "%s %2d:%02d",
1.1 deraadt 196: updown, hours, minutes);
197: return(resbuf);
198: }
199:
200: /* alphabetical comparison */
1.4 deraadt 201: int
1.11 deraadt 202: hscmp(const void *a1, const void *a2)
1.1 deraadt 203: {
1.11 deraadt 204: const struct hs *h1 = a1, *h2 = a2;
1.1 deraadt 205:
206: return(rflg * strcmp(h1->hs_wd->wd_hostname, h2->hs_wd->wd_hostname));
207: }
208:
209: /* load average comparison */
1.4 deraadt 210: int
1.11 deraadt 211: lcmp(const void *a1, const void *a2)
1.1 deraadt 212: {
1.11 deraadt 213: const struct hs *h1 = a1, *h2 = a2;
1.1 deraadt 214:
215: if (ISDOWN(h1))
216: if (ISDOWN(h2))
217: return(tcmp(a1, a2));
218: else
219: return(rflg);
220: else if (ISDOWN(h2))
221: return(-rflg);
222: else
223: return(rflg *
224: (h2->hs_wd->wd_loadav[0] - h1->hs_wd->wd_loadav[0]));
225: }
226:
227: /* number of users comparison */
1.4 deraadt 228: int
1.11 deraadt 229: ucmp(const void *a1, const void *a2)
1.1 deraadt 230: {
1.11 deraadt 231: const struct hs *h1 = a1, *h2 = a2;
1.1 deraadt 232:
233: if (ISDOWN(h1))
234: if (ISDOWN(h2))
235: return(tcmp(a1, a2));
236: else
237: return(rflg);
238: else if (ISDOWN(h2))
239: return(-rflg);
240: else
241: return(rflg * (h2->hs_nusers - h1->hs_nusers));
242: }
243:
244: /* uptime comparison */
1.4 deraadt 245: int
1.11 deraadt 246: tcmp(const void *a1, const void *a2)
1.1 deraadt 247: {
1.11 deraadt 248: const struct hs *h1 = a1, *h2 = a2;
1.1 deraadt 249:
250: return(rflg * (
251: (ISDOWN(h2) ? h2->hs_wd->wd_recvtime - now
252: : h2->hs_wd->wd_sendtime - h2->hs_wd->wd_boottime)
253: -
254: (ISDOWN(h1) ? h1->hs_wd->wd_recvtime - now
255: : h1->hs_wd->wd_sendtime - h1->hs_wd->wd_boottime)
256: ));
257: }
258:
1.4 deraadt 259: void
1.10 deraadt 260: morehosts(void)
1.1 deraadt 261: {
262: hs = realloc((char *)hs, (hspace *= 2) * sizeof(*hs));
263: if (hs == NULL) {
264: (void)fprintf(stderr, "ruptime: %s.\n", strerror(ENOMEM));
265: exit(1);
266: }
267: }