10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
70Sstevel@tonic-gate * with the License.
80Sstevel@tonic-gate *
90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate * See the License for the specific language governing permissions
120Sstevel@tonic-gate * and limitations under the License.
130Sstevel@tonic-gate *
140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate *
200Sstevel@tonic-gate * CDDL HEADER END
210Sstevel@tonic-gate */
220Sstevel@tonic-gate /*
23*473Sbw * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
240Sstevel@tonic-gate * Use is subject to license terms.
250Sstevel@tonic-gate */
260Sstevel@tonic-gate
270Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
280Sstevel@tonic-gate /* All Rights Reserved */
290Sstevel@tonic-gate
300Sstevel@tonic-gate /*
310Sstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988
320Sstevel@tonic-gate * The Regents of the University of California
330Sstevel@tonic-gate * All Rights Reserved
340Sstevel@tonic-gate *
350Sstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from
360Sstevel@tonic-gate * software developed by the University of California, Berkeley, and its
370Sstevel@tonic-gate * contributors.
380Sstevel@tonic-gate */
390Sstevel@tonic-gate
400Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
410Sstevel@tonic-gate
420Sstevel@tonic-gate #include <sys/param.h>
430Sstevel@tonic-gate #include <stdio.h>
440Sstevel@tonic-gate #include <dirent.h>
450Sstevel@tonic-gate #include <stdlib.h>
460Sstevel@tonic-gate #include <strings.h>
470Sstevel@tonic-gate #include <unistd.h>
480Sstevel@tonic-gate #include <fcntl.h>
490Sstevel@tonic-gate #include <protocols/rwhod.h>
500Sstevel@tonic-gate
510Sstevel@tonic-gate static DIR *dirp;
520Sstevel@tonic-gate
530Sstevel@tonic-gate #define HOSTLIM 100
540Sstevel@tonic-gate static int hostslim = HOSTLIM;
550Sstevel@tonic-gate static int nhosts;
560Sstevel@tonic-gate struct hs {
570Sstevel@tonic-gate struct whod *hs_wd;
580Sstevel@tonic-gate int hs_nusers;
590Sstevel@tonic-gate };
600Sstevel@tonic-gate static int hscmp(), ucmp(), lcmp(), tcmp();
610Sstevel@tonic-gate
620Sstevel@tonic-gate #define RWHODIR "/var/spool/rwho"
630Sstevel@tonic-gate
640Sstevel@tonic-gate static char *interval();
650Sstevel@tonic-gate static time_t now;
660Sstevel@tonic-gate static int aflg;
670Sstevel@tonic-gate static int rflg = 1;
680Sstevel@tonic-gate
690Sstevel@tonic-gate #define down(h) (now - (h)->hs_wd->wd_recvtime > 11 * 60)
700Sstevel@tonic-gate
710Sstevel@tonic-gate /* ARGSUSED */
72*473Sbw int
main(int argc,char ** argv)730Sstevel@tonic-gate main(int argc, char **argv)
740Sstevel@tonic-gate {
750Sstevel@tonic-gate struct dirent *dp;
760Sstevel@tonic-gate int f, i;
770Sstevel@tonic-gate struct whod *buf;
780Sstevel@tonic-gate int cc;
790Sstevel@tonic-gate char *name;
800Sstevel@tonic-gate struct hs *hs;
810Sstevel@tonic-gate struct hs *hsp;
820Sstevel@tonic-gate struct whod *wd;
830Sstevel@tonic-gate struct whoent *we;
840Sstevel@tonic-gate int maxloadav = 0;
850Sstevel@tonic-gate int (*cmp)() = hscmp;
860Sstevel@tonic-gate ptrdiff_t hoff;
870Sstevel@tonic-gate
880Sstevel@tonic-gate name = *argv;
890Sstevel@tonic-gate while (*++argv)
900Sstevel@tonic-gate while (**argv)
910Sstevel@tonic-gate switch (*(*argv)++) {
920Sstevel@tonic-gate case 'a':
930Sstevel@tonic-gate aflg++;
940Sstevel@tonic-gate break;
950Sstevel@tonic-gate case 'l':
960Sstevel@tonic-gate cmp = lcmp;
970Sstevel@tonic-gate break;
980Sstevel@tonic-gate case 'u':
990Sstevel@tonic-gate cmp = ucmp;
1000Sstevel@tonic-gate break;
1010Sstevel@tonic-gate case 't':
1020Sstevel@tonic-gate cmp = tcmp;
1030Sstevel@tonic-gate break;
1040Sstevel@tonic-gate case 'r':
1050Sstevel@tonic-gate rflg = -rflg;
1060Sstevel@tonic-gate break;
1070Sstevel@tonic-gate case '-':
1080Sstevel@tonic-gate break;
1090Sstevel@tonic-gate default:
1100Sstevel@tonic-gate (void) fprintf(stderr, "Usage: %s [ -alrtu ]"
1110Sstevel@tonic-gate " (choose at most one of l, t, or u)\n",
1120Sstevel@tonic-gate name);
1130Sstevel@tonic-gate exit(1);
1140Sstevel@tonic-gate }
1150Sstevel@tonic-gate
1160Sstevel@tonic-gate if ((hs = malloc(hostslim * sizeof (struct hs))) == NULL) {
1170Sstevel@tonic-gate (void) fprintf(stderr, "initial hs malloc failed\n");
1180Sstevel@tonic-gate exit(1);
1190Sstevel@tonic-gate }
1200Sstevel@tonic-gate hsp = hs;
1210Sstevel@tonic-gate if ((buf = malloc(sizeof (struct whod))) == NULL) {
1220Sstevel@tonic-gate (void) fprintf(stderr, "initial buf malloc failed\n");
1230Sstevel@tonic-gate exit(1);
1240Sstevel@tonic-gate }
1250Sstevel@tonic-gate
1260Sstevel@tonic-gate if (chdir(RWHODIR) < 0) {
1270Sstevel@tonic-gate perror(RWHODIR);
1280Sstevel@tonic-gate exit(1);
1290Sstevel@tonic-gate }
1300Sstevel@tonic-gate dirp = opendir(".");
1310Sstevel@tonic-gate if (dirp == NULL) {
1320Sstevel@tonic-gate perror(RWHODIR);
1330Sstevel@tonic-gate exit(1);
1340Sstevel@tonic-gate }
1350Sstevel@tonic-gate while (dp = readdir(dirp)) {
1360Sstevel@tonic-gate if (dp->d_ino == 0)
1370Sstevel@tonic-gate continue;
1380Sstevel@tonic-gate if (strncmp(dp->d_name, "whod.", 5))
1390Sstevel@tonic-gate continue;
1400Sstevel@tonic-gate if (nhosts == hostslim) {
1410Sstevel@tonic-gate /*
1420Sstevel@tonic-gate * We trust that the file system's limit on the number
1430Sstevel@tonic-gate * of files in a directory will kick in long before
1440Sstevel@tonic-gate * integer overflow.
1450Sstevel@tonic-gate */
1460Sstevel@tonic-gate hostslim = hostslim << 1;
1470Sstevel@tonic-gate
1480Sstevel@tonic-gate /*
1490Sstevel@tonic-gate * hsp points into an area about to be moved,
1500Sstevel@tonic-gate * so we first remember its offset into hs[],
1510Sstevel@tonic-gate * then restore it after realloc() has moved
1520Sstevel@tonic-gate * the data.
1530Sstevel@tonic-gate */
1540Sstevel@tonic-gate hoff = hsp - hs;
1550Sstevel@tonic-gate hs = realloc(hs, hostslim * sizeof (struct hs));
1560Sstevel@tonic-gate if (hs == NULL) {
1570Sstevel@tonic-gate (void) fprintf(stderr, "too many hosts\n");
1580Sstevel@tonic-gate exit(1);
1590Sstevel@tonic-gate }
1600Sstevel@tonic-gate hsp = hs + hoff;
1610Sstevel@tonic-gate }
1620Sstevel@tonic-gate f = open(dp->d_name, 0);
1630Sstevel@tonic-gate if (f > 0) {
1640Sstevel@tonic-gate int whdrsize = sizeof (*buf) - sizeof (buf->wd_we);
1650Sstevel@tonic-gate
1660Sstevel@tonic-gate cc = read(f, buf, sizeof (struct whod));
1670Sstevel@tonic-gate if (cc >= whdrsize) {
1680Sstevel@tonic-gate hsp->hs_wd = malloc(whdrsize);
1690Sstevel@tonic-gate wd = buf;
1700Sstevel@tonic-gate bcopy((char *)buf, (char *)hsp->hs_wd,
1710Sstevel@tonic-gate whdrsize);
1720Sstevel@tonic-gate hsp->hs_nusers = 0;
1730Sstevel@tonic-gate for (i = 0; i < 2; i++)
1740Sstevel@tonic-gate if (wd->wd_loadav[i] > maxloadav)
1750Sstevel@tonic-gate maxloadav = wd->wd_loadav[i];
1760Sstevel@tonic-gate /* LINTED: pointer alignment */
1770Sstevel@tonic-gate we = (struct whoent *)(((char *)buf)+cc);
1780Sstevel@tonic-gate while (--we >= wd->wd_we)
1790Sstevel@tonic-gate if (aflg || we->we_idle < 3600)
1800Sstevel@tonic-gate hsp->hs_nusers++;
1810Sstevel@tonic-gate nhosts++; hsp++;
1820Sstevel@tonic-gate }
1830Sstevel@tonic-gate }
1840Sstevel@tonic-gate (void) close(f);
1850Sstevel@tonic-gate }
1860Sstevel@tonic-gate (void) time(&now);
1870Sstevel@tonic-gate qsort((char *)hs, nhosts, sizeof (hs[0]), cmp);
1880Sstevel@tonic-gate if (nhosts == 0) {
1890Sstevel@tonic-gate (void) printf("no hosts!?!\n");
1900Sstevel@tonic-gate exit(1);
1910Sstevel@tonic-gate }
1920Sstevel@tonic-gate for (i = 0; i < nhosts; i++) {
1930Sstevel@tonic-gate hsp = &hs[i];
1940Sstevel@tonic-gate if (down(hsp)) {
1950Sstevel@tonic-gate (void) printf("%-12s%s\n", hsp->hs_wd->wd_hostname,
1960Sstevel@tonic-gate interval((int)(now - hsp->hs_wd->wd_recvtime),
1970Sstevel@tonic-gate "down"));
1980Sstevel@tonic-gate continue;
1990Sstevel@tonic-gate }
2000Sstevel@tonic-gate (void) printf("%-12s%s, %4d user%s load %*.2f,"
2010Sstevel@tonic-gate " %*.2f, %*.2f\n",
2020Sstevel@tonic-gate hsp->hs_wd->wd_hostname,
2030Sstevel@tonic-gate interval(hsp->hs_wd->wd_sendtime -
2040Sstevel@tonic-gate hsp->hs_wd->wd_boottime, " up"),
2050Sstevel@tonic-gate hsp->hs_nusers,
2060Sstevel@tonic-gate hsp->hs_nusers == 1 ? ", " : "s,",
2070Sstevel@tonic-gate maxloadav >= 1000 ? 5 : 4,
2080Sstevel@tonic-gate hsp->hs_wd->wd_loadav[0] / 100.0,
2090Sstevel@tonic-gate maxloadav >= 1000 ? 5 : 4,
2100Sstevel@tonic-gate hsp->hs_wd->wd_loadav[1] / 100.0,
2110Sstevel@tonic-gate maxloadav >= 1000 ? 5 : 4,
2120Sstevel@tonic-gate hsp->hs_wd->wd_loadav[2] / 100.0);
2130Sstevel@tonic-gate free(hsp->hs_wd);
2140Sstevel@tonic-gate }
2150Sstevel@tonic-gate
2160Sstevel@tonic-gate return (0);
2170Sstevel@tonic-gate }
2180Sstevel@tonic-gate
2190Sstevel@tonic-gate static char *
interval(int time,char * updown)2200Sstevel@tonic-gate interval(int time, char *updown)
2210Sstevel@tonic-gate {
2220Sstevel@tonic-gate static char resbuf[32];
2230Sstevel@tonic-gate int days, hours, minutes;
2240Sstevel@tonic-gate
2250Sstevel@tonic-gate if (time < 0 || time > 10*365*24*60*60) {
2260Sstevel@tonic-gate (void) sprintf(resbuf, " %s ??:??", updown);
2270Sstevel@tonic-gate return (resbuf);
2280Sstevel@tonic-gate }
2290Sstevel@tonic-gate minutes = (time + 59) / 60; /* round to minutes */
2300Sstevel@tonic-gate hours = minutes / 60; minutes %= 60;
2310Sstevel@tonic-gate days = hours / 24; hours %= 24;
2320Sstevel@tonic-gate if (days)
2330Sstevel@tonic-gate (void) sprintf(resbuf, "%s %2d+%02d:%02d",
2340Sstevel@tonic-gate updown, days, hours, minutes);
2350Sstevel@tonic-gate else
2360Sstevel@tonic-gate (void) sprintf(resbuf, "%s %2d:%02d",
2370Sstevel@tonic-gate updown, hours, minutes);
2380Sstevel@tonic-gate return (resbuf);
2390Sstevel@tonic-gate }
2400Sstevel@tonic-gate
2410Sstevel@tonic-gate static int
hscmp(struct hs * h1,struct hs * h2)2420Sstevel@tonic-gate hscmp(struct hs *h1, struct hs *h2)
2430Sstevel@tonic-gate {
2440Sstevel@tonic-gate
2450Sstevel@tonic-gate return (rflg * strcmp(h1->hs_wd->wd_hostname, h2->hs_wd->wd_hostname));
2460Sstevel@tonic-gate }
2470Sstevel@tonic-gate
2480Sstevel@tonic-gate /*
2490Sstevel@tonic-gate * Compare according to load average.
2500Sstevel@tonic-gate */
2510Sstevel@tonic-gate static int
lcmp(struct hs * h1,struct hs * h2)2520Sstevel@tonic-gate lcmp(struct hs *h1, struct hs *h2)
2530Sstevel@tonic-gate {
2540Sstevel@tonic-gate
2550Sstevel@tonic-gate if (down(h1))
2560Sstevel@tonic-gate if (down(h2))
2570Sstevel@tonic-gate return (tcmp(h1, h2));
2580Sstevel@tonic-gate else
2590Sstevel@tonic-gate return (rflg);
2600Sstevel@tonic-gate else if (down(h2))
2610Sstevel@tonic-gate return (-rflg);
2620Sstevel@tonic-gate else
2630Sstevel@tonic-gate return (rflg *
2640Sstevel@tonic-gate (h2->hs_wd->wd_loadav[0] - h1->hs_wd->wd_loadav[0]));
2650Sstevel@tonic-gate }
2660Sstevel@tonic-gate
2670Sstevel@tonic-gate /*
2680Sstevel@tonic-gate * Compare according to number of users.
2690Sstevel@tonic-gate */
2700Sstevel@tonic-gate static int
ucmp(struct hs * h1,struct hs * h2)2710Sstevel@tonic-gate ucmp(struct hs *h1, struct hs *h2)
2720Sstevel@tonic-gate {
2730Sstevel@tonic-gate
2740Sstevel@tonic-gate if (down(h1))
2750Sstevel@tonic-gate if (down(h2))
2760Sstevel@tonic-gate return (tcmp(h1, h2));
2770Sstevel@tonic-gate else
2780Sstevel@tonic-gate return (rflg);
2790Sstevel@tonic-gate else if (down(h2))
2800Sstevel@tonic-gate return (-rflg);
2810Sstevel@tonic-gate else
2820Sstevel@tonic-gate return (rflg * (h2->hs_nusers - h1->hs_nusers));
2830Sstevel@tonic-gate }
2840Sstevel@tonic-gate
2850Sstevel@tonic-gate /*
2860Sstevel@tonic-gate * Compare according to uptime.
2870Sstevel@tonic-gate */
2880Sstevel@tonic-gate static int
tcmp(struct hs * h1,struct hs * h2)2890Sstevel@tonic-gate tcmp(struct hs *h1, struct hs *h2)
2900Sstevel@tonic-gate {
2910Sstevel@tonic-gate
2920Sstevel@tonic-gate return (rflg * (
2930Sstevel@tonic-gate (down(h2) ? h2->hs_wd->wd_recvtime - now :
2940Sstevel@tonic-gate h2->hs_wd->wd_sendtime - h2->hs_wd->wd_boottime)
2950Sstevel@tonic-gate -
2960Sstevel@tonic-gate (down(h1) ? h1->hs_wd->wd_recvtime - now :
2970Sstevel@tonic-gate h1->hs_wd->wd_sendtime - h1->hs_wd->wd_boottime)));
2980Sstevel@tonic-gate }
299