xref: /illumos-gate/usr/src/cmd/cmd-inet/usr.bin/ruptime.c (revision 2a8bcb4efb45d99ac41c94a75c396b362c414f7f)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
23*740638c8Sbw  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved	*/
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
327c478bd9Sstevel@tonic-gate  * The Regents of the University of California
337c478bd9Sstevel@tonic-gate  * All Rights Reserved
347c478bd9Sstevel@tonic-gate  *
357c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
367c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
377c478bd9Sstevel@tonic-gate  * contributors.
387c478bd9Sstevel@tonic-gate  */
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate #include <sys/param.h>
417c478bd9Sstevel@tonic-gate #include <stdio.h>
427c478bd9Sstevel@tonic-gate #include <dirent.h>
437c478bd9Sstevel@tonic-gate #include <stdlib.h>
447c478bd9Sstevel@tonic-gate #include <strings.h>
457c478bd9Sstevel@tonic-gate #include <unistd.h>
467c478bd9Sstevel@tonic-gate #include <fcntl.h>
477c478bd9Sstevel@tonic-gate #include <protocols/rwhod.h>
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate static DIR	*dirp;
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate #define	HOSTLIM	100
527c478bd9Sstevel@tonic-gate static int	hostslim = HOSTLIM;
537c478bd9Sstevel@tonic-gate static int	nhosts;
547c478bd9Sstevel@tonic-gate struct	hs {
557c478bd9Sstevel@tonic-gate 	struct	whod *hs_wd;
567c478bd9Sstevel@tonic-gate 	int	hs_nusers;
577c478bd9Sstevel@tonic-gate };
587c478bd9Sstevel@tonic-gate static int	hscmp(), ucmp(), lcmp(), tcmp();
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate #define	RWHODIR		"/var/spool/rwho"
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate static char	*interval();
637c478bd9Sstevel@tonic-gate static time_t	now;
647c478bd9Sstevel@tonic-gate static int	aflg;
657c478bd9Sstevel@tonic-gate static int	rflg = 1;
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate #define	down(h)		(now - (h)->hs_wd->wd_recvtime > 11 * 60)
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate /* ARGSUSED */
70*740638c8Sbw int
main(int argc,char ** argv)717c478bd9Sstevel@tonic-gate main(int argc, char **argv)
727c478bd9Sstevel@tonic-gate {
737c478bd9Sstevel@tonic-gate 	struct dirent *dp;
747c478bd9Sstevel@tonic-gate 	int f, i;
757c478bd9Sstevel@tonic-gate 	struct whod *buf;
767c478bd9Sstevel@tonic-gate 	int cc;
777c478bd9Sstevel@tonic-gate 	char *name;
787c478bd9Sstevel@tonic-gate 	struct hs *hs;
797c478bd9Sstevel@tonic-gate 	struct hs *hsp;
807c478bd9Sstevel@tonic-gate 	struct whod *wd;
817c478bd9Sstevel@tonic-gate 	struct whoent *we;
827c478bd9Sstevel@tonic-gate 	int maxloadav = 0;
837c478bd9Sstevel@tonic-gate 	int (*cmp)() = hscmp;
847c478bd9Sstevel@tonic-gate 	ptrdiff_t hoff;
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate 	name = *argv;
877c478bd9Sstevel@tonic-gate 	while (*++argv)
887c478bd9Sstevel@tonic-gate 		while (**argv)
897c478bd9Sstevel@tonic-gate 			switch (*(*argv)++) {
907c478bd9Sstevel@tonic-gate 			case 'a':
917c478bd9Sstevel@tonic-gate 				aflg++;
927c478bd9Sstevel@tonic-gate 				break;
937c478bd9Sstevel@tonic-gate 			case 'l':
947c478bd9Sstevel@tonic-gate 				cmp = lcmp;
957c478bd9Sstevel@tonic-gate 				break;
967c478bd9Sstevel@tonic-gate 			case 'u':
977c478bd9Sstevel@tonic-gate 				cmp = ucmp;
987c478bd9Sstevel@tonic-gate 				break;
997c478bd9Sstevel@tonic-gate 			case 't':
1007c478bd9Sstevel@tonic-gate 				cmp = tcmp;
1017c478bd9Sstevel@tonic-gate 				break;
1027c478bd9Sstevel@tonic-gate 			case 'r':
1037c478bd9Sstevel@tonic-gate 				rflg = -rflg;
1047c478bd9Sstevel@tonic-gate 				break;
1057c478bd9Sstevel@tonic-gate 			case '-':
1067c478bd9Sstevel@tonic-gate 				break;
1077c478bd9Sstevel@tonic-gate 			default:
1087c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "Usage: %s [ -alrtu ]"
1097c478bd9Sstevel@tonic-gate 				    " (choose at most one of l, t, or u)\n",
1107c478bd9Sstevel@tonic-gate 				    name);
1117c478bd9Sstevel@tonic-gate 				exit(1);
1127c478bd9Sstevel@tonic-gate 			}
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate 	if ((hs = malloc(hostslim * sizeof (struct hs))) == NULL) {
1157c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "initial hs malloc failed\n");
1167c478bd9Sstevel@tonic-gate 		exit(1);
1177c478bd9Sstevel@tonic-gate 	}
1187c478bd9Sstevel@tonic-gate 	hsp = hs;
1197c478bd9Sstevel@tonic-gate 	if ((buf = malloc(sizeof (struct whod))) == NULL) {
1207c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "initial buf malloc failed\n");
1217c478bd9Sstevel@tonic-gate 		exit(1);
1227c478bd9Sstevel@tonic-gate 	}
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate 	if (chdir(RWHODIR) < 0) {
1257c478bd9Sstevel@tonic-gate 		perror(RWHODIR);
1267c478bd9Sstevel@tonic-gate 		exit(1);
1277c478bd9Sstevel@tonic-gate 	}
1287c478bd9Sstevel@tonic-gate 	dirp = opendir(".");
1297c478bd9Sstevel@tonic-gate 	if (dirp == NULL) {
1307c478bd9Sstevel@tonic-gate 		perror(RWHODIR);
1317c478bd9Sstevel@tonic-gate 		exit(1);
1327c478bd9Sstevel@tonic-gate 	}
1337c478bd9Sstevel@tonic-gate 	while (dp = readdir(dirp)) {
1347c478bd9Sstevel@tonic-gate 		if (dp->d_ino == 0)
1357c478bd9Sstevel@tonic-gate 			continue;
1367c478bd9Sstevel@tonic-gate 		if (strncmp(dp->d_name, "whod.", 5))
1377c478bd9Sstevel@tonic-gate 			continue;
1387c478bd9Sstevel@tonic-gate 		if (nhosts == hostslim) {
1397c478bd9Sstevel@tonic-gate 			/*
1407c478bd9Sstevel@tonic-gate 			 * We trust that the file system's limit on the number
1417c478bd9Sstevel@tonic-gate 			 * of files in a directory will kick in long before
1427c478bd9Sstevel@tonic-gate 			 * integer overflow.
1437c478bd9Sstevel@tonic-gate 			 */
1447c478bd9Sstevel@tonic-gate 			hostslim = hostslim << 1;
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 			/*
1477c478bd9Sstevel@tonic-gate 			 * hsp points into an area about to be moved,
1487c478bd9Sstevel@tonic-gate 			 * so we first remember its offset into hs[],
1497c478bd9Sstevel@tonic-gate 			 * then restore it after realloc() has moved
1507c478bd9Sstevel@tonic-gate 			 * the data.
1517c478bd9Sstevel@tonic-gate 			 */
1527c478bd9Sstevel@tonic-gate 			hoff = hsp - hs;
1537c478bd9Sstevel@tonic-gate 			hs = realloc(hs, hostslim * sizeof (struct hs));
1547c478bd9Sstevel@tonic-gate 			if (hs == NULL) {
1557c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "too many hosts\n");
1567c478bd9Sstevel@tonic-gate 				exit(1);
1577c478bd9Sstevel@tonic-gate 			}
1587c478bd9Sstevel@tonic-gate 			hsp = hs + hoff;
1597c478bd9Sstevel@tonic-gate 		}
1607c478bd9Sstevel@tonic-gate 		f = open(dp->d_name, 0);
1617c478bd9Sstevel@tonic-gate 		if (f > 0) {
1627c478bd9Sstevel@tonic-gate 			int whdrsize = sizeof (*buf) - sizeof (buf->wd_we);
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 			cc = read(f, buf, sizeof (struct whod));
1657c478bd9Sstevel@tonic-gate 			if (cc >= whdrsize) {
1667c478bd9Sstevel@tonic-gate 				hsp->hs_wd = malloc(whdrsize);
1677c478bd9Sstevel@tonic-gate 				wd = buf;
1687c478bd9Sstevel@tonic-gate 				bcopy((char *)buf, (char *)hsp->hs_wd,
1697c478bd9Sstevel@tonic-gate 				    whdrsize);
1707c478bd9Sstevel@tonic-gate 				hsp->hs_nusers = 0;
1717c478bd9Sstevel@tonic-gate 				for (i = 0; i < 2; i++)
1727c478bd9Sstevel@tonic-gate 					if (wd->wd_loadav[i] > maxloadav)
1737c478bd9Sstevel@tonic-gate 						maxloadav = wd->wd_loadav[i];
1747c478bd9Sstevel@tonic-gate 				/* LINTED:  pointer alignment */
1757c478bd9Sstevel@tonic-gate 				we = (struct whoent *)(((char *)buf)+cc);
1767c478bd9Sstevel@tonic-gate 				while (--we >= wd->wd_we)
1777c478bd9Sstevel@tonic-gate 					if (aflg || we->we_idle < 3600)
1787c478bd9Sstevel@tonic-gate 						hsp->hs_nusers++;
1797c478bd9Sstevel@tonic-gate 				nhosts++; hsp++;
1807c478bd9Sstevel@tonic-gate 			}
1817c478bd9Sstevel@tonic-gate 		}
1827c478bd9Sstevel@tonic-gate 		(void) close(f);
1837c478bd9Sstevel@tonic-gate 	}
1847c478bd9Sstevel@tonic-gate 	(void) time(&now);
1857c478bd9Sstevel@tonic-gate 	qsort((char *)hs, nhosts, sizeof (hs[0]), cmp);
1867c478bd9Sstevel@tonic-gate 	if (nhosts == 0) {
1877c478bd9Sstevel@tonic-gate 		(void) printf("no hosts!?!\n");
1887c478bd9Sstevel@tonic-gate 		exit(1);
1897c478bd9Sstevel@tonic-gate 	}
1907c478bd9Sstevel@tonic-gate 	for (i = 0; i < nhosts; i++) {
1917c478bd9Sstevel@tonic-gate 		hsp = &hs[i];
1927c478bd9Sstevel@tonic-gate 		if (down(hsp)) {
1937c478bd9Sstevel@tonic-gate 			(void) printf("%-12s%s\n", hsp->hs_wd->wd_hostname,
1947c478bd9Sstevel@tonic-gate 			    interval((int)(now - hsp->hs_wd->wd_recvtime),
1957c478bd9Sstevel@tonic-gate 				"down"));
1967c478bd9Sstevel@tonic-gate 			continue;
1977c478bd9Sstevel@tonic-gate 		}
1987c478bd9Sstevel@tonic-gate 		(void) printf("%-12s%s,  %4d user%s  load %*.2f,"
1997c478bd9Sstevel@tonic-gate 		    " %*.2f, %*.2f\n",
2007c478bd9Sstevel@tonic-gate 		    hsp->hs_wd->wd_hostname,
2017c478bd9Sstevel@tonic-gate 		    interval(hsp->hs_wd->wd_sendtime -
2027c478bd9Sstevel@tonic-gate 			hsp->hs_wd->wd_boottime, "  up"),
2037c478bd9Sstevel@tonic-gate 		    hsp->hs_nusers,
2047c478bd9Sstevel@tonic-gate 		    hsp->hs_nusers == 1 ? ", " : "s,",
2057c478bd9Sstevel@tonic-gate 		    maxloadav >= 1000 ? 5 : 4,
2067c478bd9Sstevel@tonic-gate 			hsp->hs_wd->wd_loadav[0] / 100.0,
2077c478bd9Sstevel@tonic-gate 		    maxloadav >= 1000 ? 5 : 4,
2087c478bd9Sstevel@tonic-gate 			hsp->hs_wd->wd_loadav[1] / 100.0,
2097c478bd9Sstevel@tonic-gate 		    maxloadav >= 1000 ? 5 : 4,
2107c478bd9Sstevel@tonic-gate 			hsp->hs_wd->wd_loadav[2] / 100.0);
2117c478bd9Sstevel@tonic-gate 		free(hsp->hs_wd);
2127c478bd9Sstevel@tonic-gate 	}
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 	return (0);
2157c478bd9Sstevel@tonic-gate }
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate static char *
interval(int time,char * updown)2187c478bd9Sstevel@tonic-gate interval(int time, char *updown)
2197c478bd9Sstevel@tonic-gate {
2207c478bd9Sstevel@tonic-gate 	static char resbuf[32];
2217c478bd9Sstevel@tonic-gate 	int days, hours, minutes;
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 	if (time < 0 || time > 10*365*24*60*60) {
2247c478bd9Sstevel@tonic-gate 		(void) sprintf(resbuf, "   %s ??:??", updown);
2257c478bd9Sstevel@tonic-gate 		return (resbuf);
2267c478bd9Sstevel@tonic-gate 	}
2277c478bd9Sstevel@tonic-gate 	minutes = (time + 59) / 60;		/* round to minutes */
2287c478bd9Sstevel@tonic-gate 	hours = minutes / 60; minutes %= 60;
2297c478bd9Sstevel@tonic-gate 	days = hours / 24; hours %= 24;
2307c478bd9Sstevel@tonic-gate 	if (days)
2317c478bd9Sstevel@tonic-gate 		(void) sprintf(resbuf, "%s %2d+%02d:%02d",
2327c478bd9Sstevel@tonic-gate 		    updown, days, hours, minutes);
2337c478bd9Sstevel@tonic-gate 	else
2347c478bd9Sstevel@tonic-gate 		(void) sprintf(resbuf, "%s    %2d:%02d",
2357c478bd9Sstevel@tonic-gate 		    updown, hours, minutes);
2367c478bd9Sstevel@tonic-gate 	return (resbuf);
2377c478bd9Sstevel@tonic-gate }
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate static int
hscmp(struct hs * h1,struct hs * h2)2407c478bd9Sstevel@tonic-gate hscmp(struct hs *h1, struct hs *h2)
2417c478bd9Sstevel@tonic-gate {
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate 	return (rflg * strcmp(h1->hs_wd->wd_hostname, h2->hs_wd->wd_hostname));
2447c478bd9Sstevel@tonic-gate }
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate /*
2477c478bd9Sstevel@tonic-gate  * Compare according to load average.
2487c478bd9Sstevel@tonic-gate  */
2497c478bd9Sstevel@tonic-gate static int
lcmp(struct hs * h1,struct hs * h2)2507c478bd9Sstevel@tonic-gate lcmp(struct hs *h1, struct hs *h2)
2517c478bd9Sstevel@tonic-gate {
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate 	if (down(h1))
2547c478bd9Sstevel@tonic-gate 		if (down(h2))
2557c478bd9Sstevel@tonic-gate 			return (tcmp(h1, h2));
2567c478bd9Sstevel@tonic-gate 		else
2577c478bd9Sstevel@tonic-gate 			return (rflg);
2587c478bd9Sstevel@tonic-gate 	else if (down(h2))
2597c478bd9Sstevel@tonic-gate 		return (-rflg);
2607c478bd9Sstevel@tonic-gate 	else
2617c478bd9Sstevel@tonic-gate 		return (rflg *
2627c478bd9Sstevel@tonic-gate 			(h2->hs_wd->wd_loadav[0] - h1->hs_wd->wd_loadav[0]));
2637c478bd9Sstevel@tonic-gate }
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate /*
2667c478bd9Sstevel@tonic-gate  * Compare according to number of users.
2677c478bd9Sstevel@tonic-gate  */
2687c478bd9Sstevel@tonic-gate static int
ucmp(struct hs * h1,struct hs * h2)2697c478bd9Sstevel@tonic-gate ucmp(struct hs *h1, struct hs *h2)
2707c478bd9Sstevel@tonic-gate {
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate 	if (down(h1))
2737c478bd9Sstevel@tonic-gate 		if (down(h2))
2747c478bd9Sstevel@tonic-gate 			return (tcmp(h1, h2));
2757c478bd9Sstevel@tonic-gate 		else
2767c478bd9Sstevel@tonic-gate 			return (rflg);
2777c478bd9Sstevel@tonic-gate 	else if (down(h2))
2787c478bd9Sstevel@tonic-gate 		return (-rflg);
2797c478bd9Sstevel@tonic-gate 	else
2807c478bd9Sstevel@tonic-gate 		return (rflg * (h2->hs_nusers - h1->hs_nusers));
2817c478bd9Sstevel@tonic-gate }
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate /*
2847c478bd9Sstevel@tonic-gate  * Compare according to uptime.
2857c478bd9Sstevel@tonic-gate  */
2867c478bd9Sstevel@tonic-gate static int
tcmp(struct hs * h1,struct hs * h2)2877c478bd9Sstevel@tonic-gate tcmp(struct hs *h1, struct hs *h2)
2887c478bd9Sstevel@tonic-gate {
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate 	return (rflg * (
2917c478bd9Sstevel@tonic-gate 		(down(h2) ? h2->hs_wd->wd_recvtime - now :
2927c478bd9Sstevel@tonic-gate 		    h2->hs_wd->wd_sendtime - h2->hs_wd->wd_boottime)
2937c478bd9Sstevel@tonic-gate 		-
2947c478bd9Sstevel@tonic-gate 		(down(h1) ? h1->hs_wd->wd_recvtime - now :
2957c478bd9Sstevel@tonic-gate 		    h1->hs_wd->wd_sendtime - h1->hs_wd->wd_boottime)));
2967c478bd9Sstevel@tonic-gate }
297