11590Srgrimes/* 21590Srgrimes * Copyright (c) 1983, 1993, 1994 31590Srgrimes * The Regents of the University of California. All rights reserved. 41590Srgrimes * 51590Srgrimes * Redistribution and use in source and binary forms, with or without 61590Srgrimes * modification, are permitted provided that the following conditions 71590Srgrimes * are met: 81590Srgrimes * 1. Redistributions of source code must retain the above copyright 91590Srgrimes * notice, this list of conditions and the following disclaimer. 101590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 111590Srgrimes * notice, this list of conditions and the following disclaimer in the 121590Srgrimes * documentation and/or other materials provided with the distribution. 131590Srgrimes * 4. Neither the name of the University nor the names of its contributors 141590Srgrimes * may be used to endorse or promote products derived from this software 151590Srgrimes * without specific prior written permission. 161590Srgrimes * 171590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 181590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 191590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 201590Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 211590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 221590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 231590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 241590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 251590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 261590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 271590Srgrimes * SUCH DAMAGE. 281590Srgrimes */ 291590Srgrimes 301590Srgrimes#ifndef lint 3158620Scharnierstatic const char copyright[] = 321590Srgrimes"@(#) Copyright (c) 1983, 1993, 1994\n\ 331590Srgrimes The Regents of the University of California. All rights reserved.\n"; 341590Srgrimes#endif /* not lint */ 351590Srgrimes 361590Srgrimes#ifndef lint 3795617Smarkmstatic const char sccsid[] = "@(#)ruptime.c 8.2 (Berkeley) 4/5/94"; 381590Srgrimes#endif /* not lint */ 391590Srgrimes 4095617Smarkm#include <sys/cdefs.h> 4195617Smarkm__FBSDID("$FreeBSD: releng/10.3/usr.bin/ruptime/ruptime.c 227178 2011-11-06 08:16:41Z ed $"); 4295617Smarkm 431590Srgrimes#include <sys/param.h> 441590Srgrimes 451590Srgrimes#include <protocols/rwhod.h> 461590Srgrimes 471590Srgrimes#include <dirent.h> 481590Srgrimes#include <err.h> 49200462Sdelphij#include <errno.h> 501590Srgrimes#include <fcntl.h> 511590Srgrimes#include <stdio.h> 521590Srgrimes#include <stdlib.h> 531590Srgrimes#include <string.h> 541590Srgrimes#include <time.h> 551590Srgrimes#include <unistd.h> 561590Srgrimes 57227178Sedstatic struct hs { 58226419Sed struct whod hs_wd; 591590Srgrimes int hs_nusers; 601590Srgrimes} *hs; 61226419Sed#define LEFTEARTH(h) (now - (h) > 4*24*60*60) 62226419Sed#define ISDOWN(h) (now - (h)->hs_wd.wd_recvtime > 11 * 60) 63226419Sed#define WHDRSIZE __offsetof(struct whod, wd_we) 641590Srgrimes 65227178Sedstatic size_t nhosts; 66227178Sedstatic time_t now; 67227178Sedstatic int rflg = 1; 68227178Sedstatic DIR *dirp; 691590Srgrimes 70227178Sedstatic int hscmp(const void *, const void *); 71227178Sedstatic char *interval(time_t, const char *); 72227178Sedstatic int lcmp(const void *, const void *); 73227178Sedstatic void ruptime(const char *, int, int (*)(const void *, const void *)); 74227178Sedstatic int tcmp(const void *, const void *); 75227178Sedstatic int ucmp(const void *, const void *); 76227178Sedstatic void usage(void); 771590Srgrimes 781590Srgrimesint 7995617Smarkmmain(int argc, char *argv[]) 801590Srgrimes{ 8192921Simp int (*cmp)(const void *, const void *); 82111714Sjmallett int aflg, ch; 831590Srgrimes 841590Srgrimes aflg = 0; 851590Srgrimes cmp = hscmp; 8624360Simp while ((ch = getopt(argc, argv, "alrut")) != -1) 871590Srgrimes switch (ch) { 881590Srgrimes case 'a': 891590Srgrimes aflg = 1; 901590Srgrimes break; 911590Srgrimes case 'l': 921590Srgrimes cmp = lcmp; 931590Srgrimes break; 941590Srgrimes case 'r': 951590Srgrimes rflg = -1; 961590Srgrimes break; 971590Srgrimes case 't': 981590Srgrimes cmp = tcmp; 991590Srgrimes break; 1001590Srgrimes case 'u': 1011590Srgrimes cmp = ucmp; 1021590Srgrimes break; 1038874Srgrimes default: 1041590Srgrimes usage(); 1051590Srgrimes } 1061590Srgrimes argc -= optind; 1071590Srgrimes argv += optind; 1081590Srgrimes 1091590Srgrimes if (chdir(_PATH_RWHODIR) || (dirp = opendir(".")) == NULL) 1101590Srgrimes err(1, "%s", _PATH_RWHODIR); 1111590Srgrimes 112111714Sjmallett ruptime(*argv, aflg, cmp); 113111714Sjmallett while (*argv++ != NULL) { 114111714Sjmallett if (*argv == NULL) 115111714Sjmallett break; 116111714Sjmallett ruptime(*argv, aflg, cmp); 117111714Sjmallett } 118111714Sjmallett exit(0); 119111714Sjmallett} 120111714Sjmallett 121227178Sedstatic char * 122111714Sjmallettinterval(time_t tval, const char *updown) 123111714Sjmallett{ 124111714Sjmallett static char resbuf[32]; 125111714Sjmallett int days, hours, minutes; 126111714Sjmallett 127111714Sjmallett if (tval < 0) { 128226419Sed (void)snprintf(resbuf, sizeof(resbuf), "%s ??:??", updown); 129111714Sjmallett return (resbuf); 130111714Sjmallett } 131226419Sed /* Round to minutes. */ 132111714Sjmallett minutes = (tval + (60 - 1)) / 60; 133111714Sjmallett hours = minutes / 60; 134111714Sjmallett minutes %= 60; 135111714Sjmallett days = hours / 24; 136111714Sjmallett hours %= 24; 137111714Sjmallett if (days) 138111714Sjmallett (void)snprintf(resbuf, sizeof(resbuf), 139226419Sed "%s %4d+%02d:%02d", updown, days, hours, minutes); 140111714Sjmallett else 141111714Sjmallett (void)snprintf(resbuf, sizeof(resbuf), 142226419Sed "%s %2d:%02d", updown, hours, minutes); 143111714Sjmallett return (resbuf); 144111714Sjmallett} 145111714Sjmallett 146111714Sjmallett#define HS(a) ((const struct hs *)(a)) 147111714Sjmallett 148111714Sjmallett/* Alphabetical comparison. */ 149227178Sedstatic int 150111714Sjmalletthscmp(const void *a1, const void *a2) 151111714Sjmallett{ 152111714Sjmallett return (rflg * 153226419Sed strcmp(HS(a1)->hs_wd.wd_hostname, HS(a2)->hs_wd.wd_hostname)); 154111714Sjmallett} 155111714Sjmallett 156111714Sjmallett/* Load average comparison. */ 157227178Sedstatic int 158111714Sjmallettlcmp(const void *a1, const void *a2) 159111714Sjmallett{ 160111714Sjmallett if (ISDOWN(HS(a1))) 161111714Sjmallett if (ISDOWN(HS(a2))) 162111714Sjmallett return (tcmp(a1, a2)); 163111714Sjmallett else 164111714Sjmallett return (rflg); 165111714Sjmallett else if (ISDOWN(HS(a2))) 166111714Sjmallett return (-rflg); 167111714Sjmallett else 168111714Sjmallett return (rflg * 169226419Sed (HS(a2)->hs_wd.wd_loadav[0] - HS(a1)->hs_wd.wd_loadav[0])); 170111714Sjmallett} 171111714Sjmallett 172227178Sedstatic void 173111714Sjmallettruptime(const char *host, int aflg, int (*cmp)(const void *, const void *)) 174111714Sjmallett{ 175111714Sjmallett struct hs *hsp; 176111714Sjmallett struct whod *wd; 177111714Sjmallett struct whoent *we; 178111714Sjmallett struct dirent *dp; 179111714Sjmallett const char *hostname; 180111714Sjmallett int fd, i, maxloadav; 181111714Sjmallett size_t hspace; 182226419Sed ssize_t cc; 183111714Sjmallett 184111714Sjmallett rewinddir(dirp); 185111714Sjmallett hsp = NULL; 1861590Srgrimes maxloadav = -1; 187177316Santoine (void)time(&now); 1881590Srgrimes for (nhosts = hspace = 0; (dp = readdir(dirp)) != NULL;) { 189111714Sjmallett if (dp->d_ino == 0 || strncmp(dp->d_name, "whod.", 5) != 0) 1901590Srgrimes continue; 1911590Srgrimes if ((fd = open(dp->d_name, O_RDONLY, 0)) < 0) { 1921590Srgrimes warn("%s", dp->d_name); 1931590Srgrimes continue; 1941590Srgrimes } 1951590Srgrimes 1961590Srgrimes if (nhosts == hspace) { 1971590Srgrimes if ((hs = 1981590Srgrimes realloc(hs, (hspace += 40) * sizeof(*hs))) == NULL) 1991590Srgrimes err(1, NULL); 2001590Srgrimes hsp = hs + nhosts; 2011590Srgrimes } 2021590Srgrimes 203226419Sed wd = &hsp->hs_wd; 204226419Sed cc = read(fd, wd, sizeof(*wd)); 205226419Sed (void)close(fd); 206226419Sed if (cc < (ssize_t)WHDRSIZE) 207226419Sed continue; 2081590Srgrimes 209226419Sed if (host != NULL) { 210226419Sed hostname = wd->wd_hostname; 211226419Sed if (strcasecmp(hostname, host) != 0) 212226419Sed continue; 213226419Sed } 214226419Sed if (LEFTEARTH(wd->wd_recvtime)) 215226419Sed continue; 216226419Sed 217226419Sed for (i = 0; i < 2; i++) 2181590Srgrimes if (wd->wd_loadav[i] > maxloadav) 2191590Srgrimes maxloadav = wd->wd_loadav[i]; 2201590Srgrimes 221226419Sed for (hsp->hs_nusers = 0, we = &wd->wd_we[0]; 222226419Sed (char *)(we + 1) <= (char *)wd + cc; we++) 2231590Srgrimes if (aflg || we->we_idle < 3600) 2241590Srgrimes ++hsp->hs_nusers; 2251590Srgrimes ++hsp; 2261590Srgrimes ++nhosts; 2271590Srgrimes } 228111714Sjmallett if (nhosts == 0) { 229111714Sjmallett if (host == NULL) 230111714Sjmallett errx(1, "no hosts in %s", _PATH_RWHODIR); 231111714Sjmallett else 232111714Sjmallett warnx("host %s not in %s", host, _PATH_RWHODIR); 233111714Sjmallett } 2341590Srgrimes 2351590Srgrimes qsort(hs, nhosts, sizeof(hs[0]), cmp); 23695617Smarkm for (i = 0; i < (int)nhosts; i++) { 2371590Srgrimes hsp = &hs[i]; 238226419Sed wd = &hsp->hs_wd; 2391590Srgrimes if (ISDOWN(hsp)) { 240226419Sed (void)printf("%-25.25s%s\n", wd->wd_hostname, 241226419Sed interval(now - hsp->hs_wd.wd_recvtime, "down")); 2421590Srgrimes continue; 2431590Srgrimes } 2441590Srgrimes (void)printf( 245212771Sobrien "%-25.25s%s, %4d user%s load %*.2f, %*.2f, %*.2f\n", 246226419Sed wd->wd_hostname, 247226419Sed interval((time_t)wd->wd_sendtime - 248226419Sed (time_t)wd->wd_boottime, " up"), 2491590Srgrimes hsp->hs_nusers, 2501590Srgrimes hsp->hs_nusers == 1 ? ", " : "s,", 2511590Srgrimes maxloadav >= 1000 ? 5 : 4, 252226419Sed wd->wd_loadav[0] / 100.0, 2531590Srgrimes maxloadav >= 1000 ? 5 : 4, 254226419Sed wd->wd_loadav[1] / 100.0, 2551590Srgrimes maxloadav >= 1000 ? 5 : 4, 256226419Sed wd->wd_loadav[2] / 100.0); 2571590Srgrimes } 258111714Sjmallett free(hs); 259111714Sjmallett hs = NULL; 2601590Srgrimes} 2611590Srgrimes 2621590Srgrimes/* Number of users comparison. */ 263227178Sedstatic int 26495617Smarkmucmp(const void *a1, const void *a2) 2651590Srgrimes{ 2661590Srgrimes if (ISDOWN(HS(a1))) 2671590Srgrimes if (ISDOWN(HS(a2))) 2681590Srgrimes return (tcmp(a1, a2)); 2691590Srgrimes else 2701590Srgrimes return (rflg); 2711590Srgrimes else if (ISDOWN(HS(a2))) 2721590Srgrimes return (-rflg); 2731590Srgrimes else 2741590Srgrimes return (rflg * (HS(a2)->hs_nusers - HS(a1)->hs_nusers)); 2751590Srgrimes} 2761590Srgrimes 2771590Srgrimes/* Uptime comparison. */ 278227178Sedstatic int 27995617Smarkmtcmp(const void *a1, const void *a2) 2801590Srgrimes{ 2811590Srgrimes return (rflg * ( 282226419Sed (ISDOWN(HS(a2)) ? HS(a2)->hs_wd.wd_recvtime - now 283226419Sed : HS(a2)->hs_wd.wd_sendtime - HS(a2)->hs_wd.wd_boottime) 2841590Srgrimes - 285226419Sed (ISDOWN(HS(a1)) ? HS(a1)->hs_wd.wd_recvtime - now 286226419Sed : HS(a1)->hs_wd.wd_sendtime - HS(a1)->hs_wd.wd_boottime) 2871590Srgrimes )); 2881590Srgrimes} 2891590Srgrimes 290227178Sedstatic void 29195617Smarkmusage(void) 2921590Srgrimes{ 293146466Sru (void)fprintf(stderr, "usage: ruptime [-alrtu] [host ...]\n"); 2941590Srgrimes exit(1); 2951590Srgrimes} 296