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