1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START 3*7c478bd9Sstevel@tonic-gate * 4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*7c478bd9Sstevel@tonic-gate * with the License. 8*7c478bd9Sstevel@tonic-gate * 9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 12*7c478bd9Sstevel@tonic-gate * and limitations under the License. 13*7c478bd9Sstevel@tonic-gate * 14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*7c478bd9Sstevel@tonic-gate * 20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END 21*7c478bd9Sstevel@tonic-gate */ 22*7c478bd9Sstevel@tonic-gate /* 23*7c478bd9Sstevel@tonic-gate * Copyright 2001 Sun Microsystems, Inc. All rights reserved. 24*7c478bd9Sstevel@tonic-gate * Use is subject to license terms. 25*7c478bd9Sstevel@tonic-gate */ 26*7c478bd9Sstevel@tonic-gate 27*7c478bd9Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */ 28*7c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 29*7c478bd9Sstevel@tonic-gate 30*7c478bd9Sstevel@tonic-gate /* 31*7c478bd9Sstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988 32*7c478bd9Sstevel@tonic-gate * The Regents of the University of California 33*7c478bd9Sstevel@tonic-gate * All Rights Reserved 34*7c478bd9Sstevel@tonic-gate * 35*7c478bd9Sstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from 36*7c478bd9Sstevel@tonic-gate * software developed by the University of California, Berkeley, and its 37*7c478bd9Sstevel@tonic-gate * contributors. 38*7c478bd9Sstevel@tonic-gate */ 39*7c478bd9Sstevel@tonic-gate 40*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 41*7c478bd9Sstevel@tonic-gate 42*7c478bd9Sstevel@tonic-gate #include <sys/param.h> 43*7c478bd9Sstevel@tonic-gate #include <stdio.h> 44*7c478bd9Sstevel@tonic-gate #include <dirent.h> 45*7c478bd9Sstevel@tonic-gate #include <stdlib.h> 46*7c478bd9Sstevel@tonic-gate #include <strings.h> 47*7c478bd9Sstevel@tonic-gate #include <unistd.h> 48*7c478bd9Sstevel@tonic-gate #include <fcntl.h> 49*7c478bd9Sstevel@tonic-gate #include <protocols/rwhod.h> 50*7c478bd9Sstevel@tonic-gate 51*7c478bd9Sstevel@tonic-gate static DIR *dirp; 52*7c478bd9Sstevel@tonic-gate 53*7c478bd9Sstevel@tonic-gate #define HOSTLIM 100 54*7c478bd9Sstevel@tonic-gate static int hostslim = HOSTLIM; 55*7c478bd9Sstevel@tonic-gate static int nhosts; 56*7c478bd9Sstevel@tonic-gate struct hs { 57*7c478bd9Sstevel@tonic-gate struct whod *hs_wd; 58*7c478bd9Sstevel@tonic-gate int hs_nusers; 59*7c478bd9Sstevel@tonic-gate }; 60*7c478bd9Sstevel@tonic-gate static int hscmp(), ucmp(), lcmp(), tcmp(); 61*7c478bd9Sstevel@tonic-gate 62*7c478bd9Sstevel@tonic-gate #define RWHODIR "/var/spool/rwho" 63*7c478bd9Sstevel@tonic-gate 64*7c478bd9Sstevel@tonic-gate static char *interval(); 65*7c478bd9Sstevel@tonic-gate static time_t now; 66*7c478bd9Sstevel@tonic-gate static int aflg; 67*7c478bd9Sstevel@tonic-gate static int rflg = 1; 68*7c478bd9Sstevel@tonic-gate 69*7c478bd9Sstevel@tonic-gate #define down(h) (now - (h)->hs_wd->wd_recvtime > 11 * 60) 70*7c478bd9Sstevel@tonic-gate 71*7c478bd9Sstevel@tonic-gate /* ARGSUSED */ 72*7c478bd9Sstevel@tonic-gate main(int argc, char **argv) 73*7c478bd9Sstevel@tonic-gate { 74*7c478bd9Sstevel@tonic-gate struct dirent *dp; 75*7c478bd9Sstevel@tonic-gate int f, i; 76*7c478bd9Sstevel@tonic-gate struct whod *buf; 77*7c478bd9Sstevel@tonic-gate int cc; 78*7c478bd9Sstevel@tonic-gate char *name; 79*7c478bd9Sstevel@tonic-gate struct hs *hs; 80*7c478bd9Sstevel@tonic-gate struct hs *hsp; 81*7c478bd9Sstevel@tonic-gate struct whod *wd; 82*7c478bd9Sstevel@tonic-gate struct whoent *we; 83*7c478bd9Sstevel@tonic-gate int maxloadav = 0; 84*7c478bd9Sstevel@tonic-gate int (*cmp)() = hscmp; 85*7c478bd9Sstevel@tonic-gate ptrdiff_t hoff; 86*7c478bd9Sstevel@tonic-gate 87*7c478bd9Sstevel@tonic-gate name = *argv; 88*7c478bd9Sstevel@tonic-gate while (*++argv) 89*7c478bd9Sstevel@tonic-gate while (**argv) 90*7c478bd9Sstevel@tonic-gate switch (*(*argv)++) { 91*7c478bd9Sstevel@tonic-gate case 'a': 92*7c478bd9Sstevel@tonic-gate aflg++; 93*7c478bd9Sstevel@tonic-gate break; 94*7c478bd9Sstevel@tonic-gate case 'l': 95*7c478bd9Sstevel@tonic-gate cmp = lcmp; 96*7c478bd9Sstevel@tonic-gate break; 97*7c478bd9Sstevel@tonic-gate case 'u': 98*7c478bd9Sstevel@tonic-gate cmp = ucmp; 99*7c478bd9Sstevel@tonic-gate break; 100*7c478bd9Sstevel@tonic-gate case 't': 101*7c478bd9Sstevel@tonic-gate cmp = tcmp; 102*7c478bd9Sstevel@tonic-gate break; 103*7c478bd9Sstevel@tonic-gate case 'r': 104*7c478bd9Sstevel@tonic-gate rflg = -rflg; 105*7c478bd9Sstevel@tonic-gate break; 106*7c478bd9Sstevel@tonic-gate case '-': 107*7c478bd9Sstevel@tonic-gate break; 108*7c478bd9Sstevel@tonic-gate default: 109*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "Usage: %s [ -alrtu ]" 110*7c478bd9Sstevel@tonic-gate " (choose at most one of l, t, or u)\n", 111*7c478bd9Sstevel@tonic-gate name); 112*7c478bd9Sstevel@tonic-gate exit(1); 113*7c478bd9Sstevel@tonic-gate } 114*7c478bd9Sstevel@tonic-gate 115*7c478bd9Sstevel@tonic-gate if ((hs = malloc(hostslim * sizeof (struct hs))) == NULL) { 116*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "initial hs malloc failed\n"); 117*7c478bd9Sstevel@tonic-gate exit(1); 118*7c478bd9Sstevel@tonic-gate } 119*7c478bd9Sstevel@tonic-gate hsp = hs; 120*7c478bd9Sstevel@tonic-gate if ((buf = malloc(sizeof (struct whod))) == NULL) { 121*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "initial buf malloc failed\n"); 122*7c478bd9Sstevel@tonic-gate exit(1); 123*7c478bd9Sstevel@tonic-gate } 124*7c478bd9Sstevel@tonic-gate 125*7c478bd9Sstevel@tonic-gate if (chdir(RWHODIR) < 0) { 126*7c478bd9Sstevel@tonic-gate perror(RWHODIR); 127*7c478bd9Sstevel@tonic-gate exit(1); 128*7c478bd9Sstevel@tonic-gate } 129*7c478bd9Sstevel@tonic-gate dirp = opendir("."); 130*7c478bd9Sstevel@tonic-gate if (dirp == NULL) { 131*7c478bd9Sstevel@tonic-gate perror(RWHODIR); 132*7c478bd9Sstevel@tonic-gate exit(1); 133*7c478bd9Sstevel@tonic-gate } 134*7c478bd9Sstevel@tonic-gate while (dp = readdir(dirp)) { 135*7c478bd9Sstevel@tonic-gate if (dp->d_ino == 0) 136*7c478bd9Sstevel@tonic-gate continue; 137*7c478bd9Sstevel@tonic-gate if (strncmp(dp->d_name, "whod.", 5)) 138*7c478bd9Sstevel@tonic-gate continue; 139*7c478bd9Sstevel@tonic-gate if (nhosts == hostslim) { 140*7c478bd9Sstevel@tonic-gate /* 141*7c478bd9Sstevel@tonic-gate * We trust that the file system's limit on the number 142*7c478bd9Sstevel@tonic-gate * of files in a directory will kick in long before 143*7c478bd9Sstevel@tonic-gate * integer overflow. 144*7c478bd9Sstevel@tonic-gate */ 145*7c478bd9Sstevel@tonic-gate hostslim = hostslim << 1; 146*7c478bd9Sstevel@tonic-gate 147*7c478bd9Sstevel@tonic-gate /* 148*7c478bd9Sstevel@tonic-gate * hsp points into an area about to be moved, 149*7c478bd9Sstevel@tonic-gate * so we first remember its offset into hs[], 150*7c478bd9Sstevel@tonic-gate * then restore it after realloc() has moved 151*7c478bd9Sstevel@tonic-gate * the data. 152*7c478bd9Sstevel@tonic-gate */ 153*7c478bd9Sstevel@tonic-gate hoff = hsp - hs; 154*7c478bd9Sstevel@tonic-gate hs = realloc(hs, hostslim * sizeof (struct hs)); 155*7c478bd9Sstevel@tonic-gate if (hs == NULL) { 156*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "too many hosts\n"); 157*7c478bd9Sstevel@tonic-gate exit(1); 158*7c478bd9Sstevel@tonic-gate } 159*7c478bd9Sstevel@tonic-gate hsp = hs + hoff; 160*7c478bd9Sstevel@tonic-gate } 161*7c478bd9Sstevel@tonic-gate f = open(dp->d_name, 0); 162*7c478bd9Sstevel@tonic-gate if (f > 0) { 163*7c478bd9Sstevel@tonic-gate int whdrsize = sizeof (*buf) - sizeof (buf->wd_we); 164*7c478bd9Sstevel@tonic-gate 165*7c478bd9Sstevel@tonic-gate cc = read(f, buf, sizeof (struct whod)); 166*7c478bd9Sstevel@tonic-gate if (cc >= whdrsize) { 167*7c478bd9Sstevel@tonic-gate hsp->hs_wd = malloc(whdrsize); 168*7c478bd9Sstevel@tonic-gate wd = buf; 169*7c478bd9Sstevel@tonic-gate bcopy((char *)buf, (char *)hsp->hs_wd, 170*7c478bd9Sstevel@tonic-gate whdrsize); 171*7c478bd9Sstevel@tonic-gate hsp->hs_nusers = 0; 172*7c478bd9Sstevel@tonic-gate for (i = 0; i < 2; i++) 173*7c478bd9Sstevel@tonic-gate if (wd->wd_loadav[i] > maxloadav) 174*7c478bd9Sstevel@tonic-gate maxloadav = wd->wd_loadav[i]; 175*7c478bd9Sstevel@tonic-gate /* LINTED: pointer alignment */ 176*7c478bd9Sstevel@tonic-gate we = (struct whoent *)(((char *)buf)+cc); 177*7c478bd9Sstevel@tonic-gate while (--we >= wd->wd_we) 178*7c478bd9Sstevel@tonic-gate if (aflg || we->we_idle < 3600) 179*7c478bd9Sstevel@tonic-gate hsp->hs_nusers++; 180*7c478bd9Sstevel@tonic-gate nhosts++; hsp++; 181*7c478bd9Sstevel@tonic-gate } 182*7c478bd9Sstevel@tonic-gate } 183*7c478bd9Sstevel@tonic-gate (void) close(f); 184*7c478bd9Sstevel@tonic-gate } 185*7c478bd9Sstevel@tonic-gate (void) time(&now); 186*7c478bd9Sstevel@tonic-gate qsort((char *)hs, nhosts, sizeof (hs[0]), cmp); 187*7c478bd9Sstevel@tonic-gate if (nhosts == 0) { 188*7c478bd9Sstevel@tonic-gate (void) printf("no hosts!?!\n"); 189*7c478bd9Sstevel@tonic-gate exit(1); 190*7c478bd9Sstevel@tonic-gate } 191*7c478bd9Sstevel@tonic-gate for (i = 0; i < nhosts; i++) { 192*7c478bd9Sstevel@tonic-gate hsp = &hs[i]; 193*7c478bd9Sstevel@tonic-gate if (down(hsp)) { 194*7c478bd9Sstevel@tonic-gate (void) printf("%-12s%s\n", hsp->hs_wd->wd_hostname, 195*7c478bd9Sstevel@tonic-gate interval((int)(now - hsp->hs_wd->wd_recvtime), 196*7c478bd9Sstevel@tonic-gate "down")); 197*7c478bd9Sstevel@tonic-gate continue; 198*7c478bd9Sstevel@tonic-gate } 199*7c478bd9Sstevel@tonic-gate (void) printf("%-12s%s, %4d user%s load %*.2f," 200*7c478bd9Sstevel@tonic-gate " %*.2f, %*.2f\n", 201*7c478bd9Sstevel@tonic-gate hsp->hs_wd->wd_hostname, 202*7c478bd9Sstevel@tonic-gate interval(hsp->hs_wd->wd_sendtime - 203*7c478bd9Sstevel@tonic-gate hsp->hs_wd->wd_boottime, " up"), 204*7c478bd9Sstevel@tonic-gate hsp->hs_nusers, 205*7c478bd9Sstevel@tonic-gate hsp->hs_nusers == 1 ? ", " : "s,", 206*7c478bd9Sstevel@tonic-gate maxloadav >= 1000 ? 5 : 4, 207*7c478bd9Sstevel@tonic-gate hsp->hs_wd->wd_loadav[0] / 100.0, 208*7c478bd9Sstevel@tonic-gate maxloadav >= 1000 ? 5 : 4, 209*7c478bd9Sstevel@tonic-gate hsp->hs_wd->wd_loadav[1] / 100.0, 210*7c478bd9Sstevel@tonic-gate maxloadav >= 1000 ? 5 : 4, 211*7c478bd9Sstevel@tonic-gate hsp->hs_wd->wd_loadav[2] / 100.0); 212*7c478bd9Sstevel@tonic-gate free(hsp->hs_wd); 213*7c478bd9Sstevel@tonic-gate } 214*7c478bd9Sstevel@tonic-gate 215*7c478bd9Sstevel@tonic-gate return (0); 216*7c478bd9Sstevel@tonic-gate } 217*7c478bd9Sstevel@tonic-gate 218*7c478bd9Sstevel@tonic-gate static char * 219*7c478bd9Sstevel@tonic-gate interval(int time, char *updown) 220*7c478bd9Sstevel@tonic-gate { 221*7c478bd9Sstevel@tonic-gate static char resbuf[32]; 222*7c478bd9Sstevel@tonic-gate int days, hours, minutes; 223*7c478bd9Sstevel@tonic-gate 224*7c478bd9Sstevel@tonic-gate if (time < 0 || time > 10*365*24*60*60) { 225*7c478bd9Sstevel@tonic-gate (void) sprintf(resbuf, " %s ??:??", updown); 226*7c478bd9Sstevel@tonic-gate return (resbuf); 227*7c478bd9Sstevel@tonic-gate } 228*7c478bd9Sstevel@tonic-gate minutes = (time + 59) / 60; /* round to minutes */ 229*7c478bd9Sstevel@tonic-gate hours = minutes / 60; minutes %= 60; 230*7c478bd9Sstevel@tonic-gate days = hours / 24; hours %= 24; 231*7c478bd9Sstevel@tonic-gate if (days) 232*7c478bd9Sstevel@tonic-gate (void) sprintf(resbuf, "%s %2d+%02d:%02d", 233*7c478bd9Sstevel@tonic-gate updown, days, hours, minutes); 234*7c478bd9Sstevel@tonic-gate else 235*7c478bd9Sstevel@tonic-gate (void) sprintf(resbuf, "%s %2d:%02d", 236*7c478bd9Sstevel@tonic-gate updown, hours, minutes); 237*7c478bd9Sstevel@tonic-gate return (resbuf); 238*7c478bd9Sstevel@tonic-gate } 239*7c478bd9Sstevel@tonic-gate 240*7c478bd9Sstevel@tonic-gate static int 241*7c478bd9Sstevel@tonic-gate hscmp(struct hs *h1, struct hs *h2) 242*7c478bd9Sstevel@tonic-gate { 243*7c478bd9Sstevel@tonic-gate 244*7c478bd9Sstevel@tonic-gate return (rflg * strcmp(h1->hs_wd->wd_hostname, h2->hs_wd->wd_hostname)); 245*7c478bd9Sstevel@tonic-gate } 246*7c478bd9Sstevel@tonic-gate 247*7c478bd9Sstevel@tonic-gate /* 248*7c478bd9Sstevel@tonic-gate * Compare according to load average. 249*7c478bd9Sstevel@tonic-gate */ 250*7c478bd9Sstevel@tonic-gate static int 251*7c478bd9Sstevel@tonic-gate lcmp(struct hs *h1, struct hs *h2) 252*7c478bd9Sstevel@tonic-gate { 253*7c478bd9Sstevel@tonic-gate 254*7c478bd9Sstevel@tonic-gate if (down(h1)) 255*7c478bd9Sstevel@tonic-gate if (down(h2)) 256*7c478bd9Sstevel@tonic-gate return (tcmp(h1, h2)); 257*7c478bd9Sstevel@tonic-gate else 258*7c478bd9Sstevel@tonic-gate return (rflg); 259*7c478bd9Sstevel@tonic-gate else if (down(h2)) 260*7c478bd9Sstevel@tonic-gate return (-rflg); 261*7c478bd9Sstevel@tonic-gate else 262*7c478bd9Sstevel@tonic-gate return (rflg * 263*7c478bd9Sstevel@tonic-gate (h2->hs_wd->wd_loadav[0] - h1->hs_wd->wd_loadav[0])); 264*7c478bd9Sstevel@tonic-gate } 265*7c478bd9Sstevel@tonic-gate 266*7c478bd9Sstevel@tonic-gate /* 267*7c478bd9Sstevel@tonic-gate * Compare according to number of users. 268*7c478bd9Sstevel@tonic-gate */ 269*7c478bd9Sstevel@tonic-gate static int 270*7c478bd9Sstevel@tonic-gate ucmp(struct hs *h1, struct hs *h2) 271*7c478bd9Sstevel@tonic-gate { 272*7c478bd9Sstevel@tonic-gate 273*7c478bd9Sstevel@tonic-gate if (down(h1)) 274*7c478bd9Sstevel@tonic-gate if (down(h2)) 275*7c478bd9Sstevel@tonic-gate return (tcmp(h1, h2)); 276*7c478bd9Sstevel@tonic-gate else 277*7c478bd9Sstevel@tonic-gate return (rflg); 278*7c478bd9Sstevel@tonic-gate else if (down(h2)) 279*7c478bd9Sstevel@tonic-gate return (-rflg); 280*7c478bd9Sstevel@tonic-gate else 281*7c478bd9Sstevel@tonic-gate return (rflg * (h2->hs_nusers - h1->hs_nusers)); 282*7c478bd9Sstevel@tonic-gate } 283*7c478bd9Sstevel@tonic-gate 284*7c478bd9Sstevel@tonic-gate /* 285*7c478bd9Sstevel@tonic-gate * Compare according to uptime. 286*7c478bd9Sstevel@tonic-gate */ 287*7c478bd9Sstevel@tonic-gate static int 288*7c478bd9Sstevel@tonic-gate tcmp(struct hs *h1, struct hs *h2) 289*7c478bd9Sstevel@tonic-gate { 290*7c478bd9Sstevel@tonic-gate 291*7c478bd9Sstevel@tonic-gate return (rflg * ( 292*7c478bd9Sstevel@tonic-gate (down(h2) ? h2->hs_wd->wd_recvtime - now : 293*7c478bd9Sstevel@tonic-gate h2->hs_wd->wd_sendtime - h2->hs_wd->wd_boottime) 294*7c478bd9Sstevel@tonic-gate - 295*7c478bd9Sstevel@tonic-gate (down(h1) ? h1->hs_wd->wd_recvtime - now : 296*7c478bd9Sstevel@tonic-gate h1->hs_wd->wd_sendtime - h1->hs_wd->wd_boottime))); 297*7c478bd9Sstevel@tonic-gate } 298