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