xref: /onnv-gate/usr/src/cmd/rpcsvc/rup.c (revision 631:861de40d3b19)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
230Sstevel@tonic-gate 
240Sstevel@tonic-gate /*
25*631Speteh  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
260Sstevel@tonic-gate  * Use is subject to license terms.
270Sstevel@tonic-gate  */
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include <stdio.h>
300Sstevel@tonic-gate #include <stdlib.h>
310Sstevel@tonic-gate #include <netdb.h>
320Sstevel@tonic-gate #include <sys/param.h>
330Sstevel@tonic-gate #include <sys/stat.h>
340Sstevel@tonic-gate #include <sys/time.h>
350Sstevel@tonic-gate #include <sys/socket.h>
360Sstevel@tonic-gate #include <netinet/in.h>
370Sstevel@tonic-gate #include <rpc/rpc.h>
380Sstevel@tonic-gate #include <netdir.h>
390Sstevel@tonic-gate #include <rpcsvc/rstat.h>
400Sstevel@tonic-gate #include <rpc/pmap_clnt.h>
410Sstevel@tonic-gate 
420Sstevel@tonic-gate 
430Sstevel@tonic-gate #define	MACHINELEN	15	/* length of machine name printed out */
440Sstevel@tonic-gate #define	MACHINELENMAX	128	/* maximum machine name length */
450Sstevel@tonic-gate #define	AVENSIZE	(3 * sizeof (long))
460Sstevel@tonic-gate #define	SLOTS	256
470Sstevel@tonic-gate 
480Sstevel@tonic-gate int machinecmp();
490Sstevel@tonic-gate int loadcmp();
500Sstevel@tonic-gate int uptimecmp();
51*631Speteh static int collectnames();
520Sstevel@tonic-gate int singlehost();		/* returns 1 if rup of given host fails */
530Sstevel@tonic-gate void printsinglehosts();
540Sstevel@tonic-gate void printnames();
55*631Speteh static void putline();
56*631Speteh int netbufeq(struct netbuf *ap, struct netbuf *bp);
57*631Speteh void usage(void);
580Sstevel@tonic-gate 
590Sstevel@tonic-gate struct entry {
600Sstevel@tonic-gate 	struct netconfig *nconf;
610Sstevel@tonic-gate 	struct netbuf *addr;
620Sstevel@tonic-gate 	char *machine;
630Sstevel@tonic-gate 	struct timeval boottime;
640Sstevel@tonic-gate 	time_t curtime;
650Sstevel@tonic-gate 	long avenrun[3];
660Sstevel@tonic-gate };
670Sstevel@tonic-gate 
680Sstevel@tonic-gate int total_entries;
690Sstevel@tonic-gate int curentry;
700Sstevel@tonic-gate struct entry *entry;
710Sstevel@tonic-gate int vers;			/* which version did the broadcasting */
720Sstevel@tonic-gate int lflag;			/* load: sort by load average */
730Sstevel@tonic-gate int tflag;			/* time: sort by uptime average */
740Sstevel@tonic-gate int hflag;			/* host: sort by machine name */
750Sstevel@tonic-gate int dflag;			/* debug: list only first n machines */
760Sstevel@tonic-gate int debug;
770Sstevel@tonic-gate 
78*631Speteh int
main(int argc,char * argv[])79*631Speteh main(int argc, char *argv[])
800Sstevel@tonic-gate {
810Sstevel@tonic-gate 	statsvar sv;
820Sstevel@tonic-gate 	statstime st;
830Sstevel@tonic-gate 	int single, nfailed;
840Sstevel@tonic-gate 	enum clnt_stat bstat;
850Sstevel@tonic-gate 
860Sstevel@tonic-gate 	/*
870Sstevel@tonic-gate 	 * set number of slots to be 256 to begin with,
880Sstevel@tonic-gate 	 * this is large enough for most subnets but not all
890Sstevel@tonic-gate 	 */
900Sstevel@tonic-gate 
910Sstevel@tonic-gate 	curentry = 0;
920Sstevel@tonic-gate 	total_entries = SLOTS;
930Sstevel@tonic-gate 	entry = malloc(sizeof (struct entry) * total_entries);
940Sstevel@tonic-gate 	single = nfailed = 0;
950Sstevel@tonic-gate 	while (argc > 1) {
960Sstevel@tonic-gate 		if (argv[1][0] != '-') {
970Sstevel@tonic-gate 			single++;
980Sstevel@tonic-gate 			nfailed += singlehost(argv[1]);
990Sstevel@tonic-gate 		} else {
1000Sstevel@tonic-gate 			switch (argv[1][1]) {
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate 			case 'l':
1030Sstevel@tonic-gate 				lflag++;
1040Sstevel@tonic-gate 				break;
1050Sstevel@tonic-gate 			case 't':
1060Sstevel@tonic-gate 				tflag++;
1070Sstevel@tonic-gate 				break;
1080Sstevel@tonic-gate 			case 'h':
1090Sstevel@tonic-gate 				hflag++;
1100Sstevel@tonic-gate 				break;
1110Sstevel@tonic-gate 			case 'd':
1120Sstevel@tonic-gate 				dflag++;
1130Sstevel@tonic-gate 				if (argc < 3)
1140Sstevel@tonic-gate 					usage();
1150Sstevel@tonic-gate 				debug = atoi(argv[2]);
1160Sstevel@tonic-gate 				argc--;
1170Sstevel@tonic-gate 				argv++;
1180Sstevel@tonic-gate 				break;
1190Sstevel@tonic-gate 			default:
1200Sstevel@tonic-gate 				usage();
1210Sstevel@tonic-gate 			}
1220Sstevel@tonic-gate 		}
1230Sstevel@tonic-gate 		argv++;
1240Sstevel@tonic-gate 		argc--;
1250Sstevel@tonic-gate 	}
1260Sstevel@tonic-gate 	if (single > 0) {
1270Sstevel@tonic-gate 		if (hflag || tflag || lflag)
1280Sstevel@tonic-gate 			printsinglehosts();
1290Sstevel@tonic-gate 		if (nfailed == single) {
1300Sstevel@tonic-gate 			free(entry);
1310Sstevel@tonic-gate 			exit(1);	/* all hosts we tried failed */
1320Sstevel@tonic-gate 		} else {
1330Sstevel@tonic-gate 			free(entry);
1340Sstevel@tonic-gate 			exit(0);
1350Sstevel@tonic-gate 		}
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate 	}
1380Sstevel@tonic-gate 	if (hflag || tflag || lflag) {
1390Sstevel@tonic-gate 		printf("collecting responses... ");
1400Sstevel@tonic-gate 		fflush(stdout);
1410Sstevel@tonic-gate 	}
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate 	sv.cp_time.cp_time_val = (int *)NULL;
1440Sstevel@tonic-gate 	sv.dk_xfer.dk_xfer_val = (int *)NULL;
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate 	/*
1470Sstevel@tonic-gate 	 * Null out pointers in the statsvar struct
1480Sstevel@tonic-gate 	 * so that we don't follow a random pointer
1490Sstevel@tonic-gate 	 * somewhere when we get our results back.
1500Sstevel@tonic-gate 	 * Set lengths to zero so we don't allocate
1510Sstevel@tonic-gate 	 * some random amount of space we don't need
1520Sstevel@tonic-gate 	 * (in the case where the reply was program
1530Sstevel@tonic-gate 	 *  not registered).
1540Sstevel@tonic-gate 	 */
1550Sstevel@tonic-gate 	sv.cp_time.cp_time_len = 0;
1560Sstevel@tonic-gate 	sv.cp_time.cp_time_val = (int *)NULL;
1570Sstevel@tonic-gate 	sv.dk_xfer.dk_xfer_len = 0;
1580Sstevel@tonic-gate 	sv.dk_xfer.dk_xfer_val = (int *)NULL;
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate 	vers = RSTATVERS_VAR;
1610Sstevel@tonic-gate 	bstat = rpc_broadcast(RSTATPROG, RSTATVERS_VAR, RSTATPROC_STATS,
1620Sstevel@tonic-gate 			xdr_void, NULL, xdr_statsvar, (caddr_t)&sv,
1630Sstevel@tonic-gate 			(resultproc_t)collectnames, (char *)0);
1640Sstevel@tonic-gate #ifdef TESTING
1650Sstevel@tonic-gate 	if (bstat != RPC_SUCCESS)
1660Sstevel@tonic-gate 		printf("rpc_broadcast for rstat version %d returned %s\n",
1670Sstevel@tonic-gate 			vers, clnt_sperrno(bstat));
1680Sstevel@tonic-gate 	fprintf(stderr, "starting second round of broadcasting\n");
1690Sstevel@tonic-gate #endif
1700Sstevel@tonic-gate 	vers = RSTATVERS_TIME;
1710Sstevel@tonic-gate 	bstat = rpc_broadcast(RSTATPROG, RSTATVERS_TIME, RSTATPROC_STATS,
1720Sstevel@tonic-gate 			xdr_void, NULL, xdr_statstime, (caddr_t)&st,
1730Sstevel@tonic-gate 			(resultproc_t)collectnames, (char *)0);
1740Sstevel@tonic-gate #ifdef	TESTING
1750Sstevel@tonic-gate 	if (bstat != RPC_SUCCESS)
1760Sstevel@tonic-gate 		printf("rpc_broadcast for rstat version %d returned %s\n",
1770Sstevel@tonic-gate 			vers, clnt_sperrno(bstat));
1780Sstevel@tonic-gate #endif
1790Sstevel@tonic-gate 	if (hflag || tflag || lflag)
1800Sstevel@tonic-gate 		printnames();
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate 	free(entry);
185*631Speteh 	return (0);
1860Sstevel@tonic-gate }
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate int
singlehost(host)1890Sstevel@tonic-gate singlehost(host)
1900Sstevel@tonic-gate 	char *host;
1910Sstevel@tonic-gate {
1920Sstevel@tonic-gate 	static int debugcnt;
1930Sstevel@tonic-gate 	enum clnt_stat err;
1940Sstevel@tonic-gate 	statstime st;
1950Sstevel@tonic-gate 	statsvar sw_var;
1960Sstevel@tonic-gate 	bool_t is_var_vers = FALSE;
1970Sstevel@tonic-gate 
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate 	if (curentry >= total_entries) {
2000Sstevel@tonic-gate 		struct entry *tmp;
2010Sstevel@tonic-gate 
2020Sstevel@tonic-gate 		total_entries += SLOTS;
2030Sstevel@tonic-gate 		tmp = realloc((struct entry *)entry, sizeof (struct entry)
2040Sstevel@tonic-gate 						* total_entries);
2050Sstevel@tonic-gate 		if (tmp == NULL) {
2060Sstevel@tonic-gate 			return (1);
2070Sstevel@tonic-gate 		}
2080Sstevel@tonic-gate 		entry = tmp;
2090Sstevel@tonic-gate 	}
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate 	sw_var.cp_time.cp_time_val = (int *)NULL;
2120Sstevel@tonic-gate 	sw_var.dk_xfer.dk_xfer_val = (int *)NULL;
2130Sstevel@tonic-gate 	err = (enum clnt_stat)callrpc(host, RSTATPROG, RSTATVERS_VAR,
2140Sstevel@tonic-gate 			RSTATPROC_STATS, xdr_void, 0, xdr_statsvar, &sw_var);
2150Sstevel@tonic-gate 	if (err == RPC_SUCCESS) {
2160Sstevel@tonic-gate 		is_var_vers = TRUE;
2170Sstevel@tonic-gate 	} else if (err == RPC_PROGVERSMISMATCH) {
2180Sstevel@tonic-gate 		err = (enum clnt_stat)callrpc(host, RSTATPROG, RSTATVERS_TIME,
2190Sstevel@tonic-gate 			RSTATPROC_STATS, xdr_void, 0, xdr_statstime, &st);
2200Sstevel@tonic-gate 		if (err != RPC_SUCCESS)
2210Sstevel@tonic-gate 			goto error;
2220Sstevel@tonic-gate 	} else
2230Sstevel@tonic-gate 		goto error;
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate 	debugcnt++;
2260Sstevel@tonic-gate 	if (!hflag && !lflag && !tflag) {
2270Sstevel@tonic-gate 		printf("%*.*s  ", MACHINELEN, MACHINELEN, host);
2280Sstevel@tonic-gate 		if (is_var_vers == TRUE)
2290Sstevel@tonic-gate 			putline(sw_var.curtime.tv_sec, sw_var.boottime,
2300Sstevel@tonic-gate 				sw_var.avenrun);
2310Sstevel@tonic-gate 		else
2320Sstevel@tonic-gate 			putline(st.curtime.tv_sec, st.boottime, st.avenrun);
2330Sstevel@tonic-gate 		return (0);		/* success */
2340Sstevel@tonic-gate 	} else {
2350Sstevel@tonic-gate 		entry[curentry].machine = host;
2360Sstevel@tonic-gate 		if (is_var_vers == FALSE) { /* RSTATVERS_TIME */
2370Sstevel@tonic-gate 			entry[curentry].boottime.tv_sec = st.boottime.tv_sec;
2380Sstevel@tonic-gate 			entry[curentry].boottime.tv_usec =
2390Sstevel@tonic-gate 				st.boottime.tv_usec;
2400Sstevel@tonic-gate 			entry[curentry].curtime = st.curtime.tv_sec;
2410Sstevel@tonic-gate 			memcpy(entry[curentry].avenrun, st.avenrun, AVENSIZE);
2420Sstevel@tonic-gate 		} else { /* RSTATVERS_VAR */
2430Sstevel@tonic-gate 			entry[curentry].boottime.tv_sec =
2440Sstevel@tonic-gate 				sw_var.boottime.tv_sec;
2450Sstevel@tonic-gate 			entry[curentry].boottime.tv_usec =
2460Sstevel@tonic-gate 				sw_var.boottime.tv_usec;
2470Sstevel@tonic-gate 			entry[curentry].curtime = sw_var.curtime.tv_sec;
2480Sstevel@tonic-gate 			memcpy(entry[curentry].avenrun, sw_var.avenrun,
2490Sstevel@tonic-gate 							AVENSIZE);
2500Sstevel@tonic-gate 		}
2510Sstevel@tonic-gate 	}
2520Sstevel@tonic-gate 	curentry++;
2530Sstevel@tonic-gate 	if (dflag && debugcnt >= debug)
2540Sstevel@tonic-gate 		return (1);
2550Sstevel@tonic-gate 	return (0);
2560Sstevel@tonic-gate 
2570Sstevel@tonic-gate error:
2580Sstevel@tonic-gate 	fprintf(stderr, "%*.*s: ", MACHINELEN, MACHINELEN, host);
2590Sstevel@tonic-gate 	clnt_perrno(err);
2600Sstevel@tonic-gate 	/*
2610Sstevel@tonic-gate 	 * clnt_perrno now prints a newline
2620Sstevel@tonic-gate 	 */
2630Sstevel@tonic-gate 	/* fprintf(stderr, "\n"); */
2640Sstevel@tonic-gate 	return (1);		/* a failure */
2650Sstevel@tonic-gate }
2660Sstevel@tonic-gate 
267*631Speteh static void
putline(now,boottime,avenrun)2680Sstevel@tonic-gate putline(now, boottime, avenrun)
2690Sstevel@tonic-gate 	time_t now;
2700Sstevel@tonic-gate 	struct timeval boottime;
2710Sstevel@tonic-gate 	long avenrun[];
2720Sstevel@tonic-gate {
2730Sstevel@tonic-gate 	int uptime, days, hrs, mins, i;
2740Sstevel@tonic-gate 
2750Sstevel@tonic-gate 	uptime = now - boottime.tv_sec;
2760Sstevel@tonic-gate 	uptime += 30;
2770Sstevel@tonic-gate 	if (uptime < 0)		/* unsynchronized clocks */
2780Sstevel@tonic-gate 		uptime = 0;
2790Sstevel@tonic-gate 	days = uptime / (60*60*24);
2800Sstevel@tonic-gate 	uptime %= (60*60*24);
2810Sstevel@tonic-gate 	hrs = uptime / (60*60);
2820Sstevel@tonic-gate 	uptime %= (60*60);
2830Sstevel@tonic-gate 	mins = uptime / 60;
2840Sstevel@tonic-gate 
2850Sstevel@tonic-gate 	printf("  up");
2860Sstevel@tonic-gate 	if (days > 0)
2870Sstevel@tonic-gate 		printf(" %2d day%s", days, days > 1 ? "s," : ", ");
2880Sstevel@tonic-gate 	else
2890Sstevel@tonic-gate 		printf("         ");
2900Sstevel@tonic-gate 	if (hrs > 0)
2910Sstevel@tonic-gate 		printf(" %2d:%02d,  ", hrs, mins);
2920Sstevel@tonic-gate 	else
2930Sstevel@tonic-gate 		printf(" %2d min%s", mins, mins > 1 ? "s," : ", ");
2940Sstevel@tonic-gate 
2950Sstevel@tonic-gate 	/*
2960Sstevel@tonic-gate 	 * Print 1, 5, and 15 minute load averages.
2970Sstevel@tonic-gate 	 * (Found by looking in kernel for avenrun).
2980Sstevel@tonic-gate 	 */
2990Sstevel@tonic-gate 	printf("  load average:");
3000Sstevel@tonic-gate 	for (i = 0; i < (AVENSIZE / sizeof (avenrun[0])); i++) {
3010Sstevel@tonic-gate 		if (i > 0)
3020Sstevel@tonic-gate 			printf(",");
3030Sstevel@tonic-gate 		printf(" %.2f", (double)avenrun[i]/FSCALE);
3040Sstevel@tonic-gate 	}
3050Sstevel@tonic-gate 	printf("\n");
3060Sstevel@tonic-gate }
3070Sstevel@tonic-gate 
308*631Speteh static int
collectnames(resultsp,taddr,nconf)3090Sstevel@tonic-gate collectnames(resultsp, taddr, nconf)
3100Sstevel@tonic-gate 	char *resultsp;
311*631Speteh 	struct t_bind *taddr;
312*631Speteh 	struct netconfig *nconf;
3130Sstevel@tonic-gate {
3140Sstevel@tonic-gate 	static int debugcnt;
3150Sstevel@tonic-gate 	register struct entry *entryp, *lim;
3160Sstevel@tonic-gate 	statstime *st;
3170Sstevel@tonic-gate 	statsvar *sv;
3180Sstevel@tonic-gate 	struct nd_hostservlist *hs;
3190Sstevel@tonic-gate 	extern struct netbuf *netbufdup();
3200Sstevel@tonic-gate 	extern struct netconfig *netconfigdup();
3210Sstevel@tonic-gate 	extern int netbufeq();
3220Sstevel@tonic-gate 
3230Sstevel@tonic-gate 	/*
3240Sstevel@tonic-gate 	 * need to realloc more space if we have more than 256 machines
3250Sstevel@tonic-gate 	 * that responded to the broadcast
3260Sstevel@tonic-gate 	 */
3270Sstevel@tonic-gate 
3280Sstevel@tonic-gate 	if (curentry >= total_entries) {
3290Sstevel@tonic-gate 		struct entry *tmp;
3300Sstevel@tonic-gate 
3310Sstevel@tonic-gate 		total_entries += SLOTS;
3320Sstevel@tonic-gate 		tmp = realloc((struct entry *)entry, sizeof (struct entry)
3330Sstevel@tonic-gate 						* total_entries);
3340Sstevel@tonic-gate 		if (tmp == NULL) {
3350Sstevel@tonic-gate 			return (1);
3360Sstevel@tonic-gate 		}
3370Sstevel@tonic-gate 		entry = tmp;
3380Sstevel@tonic-gate 	}
3390Sstevel@tonic-gate 	/*
3400Sstevel@tonic-gate 	 * weed out duplicates
3410Sstevel@tonic-gate 	 */
3420Sstevel@tonic-gate 	lim = entry + curentry;
3430Sstevel@tonic-gate 	for (entryp = entry; entryp < lim; entryp++)
3440Sstevel@tonic-gate 		if (netbufeq(&taddr->addr, entryp->addr))
3450Sstevel@tonic-gate 			return (0);
3460Sstevel@tonic-gate 
3470Sstevel@tonic-gate 	if (vers == RSTATVERS_TIME) {
3480Sstevel@tonic-gate 		st = (statstime *)resultsp;
3490Sstevel@tonic-gate 	} else if (vers == RSTATVERS_VAR) {
3500Sstevel@tonic-gate 		sv = (statsvar *)resultsp;
3510Sstevel@tonic-gate 	} else {
3520Sstevel@tonic-gate 		return (0);	/* we don't handle this version */
3530Sstevel@tonic-gate 	}
3540Sstevel@tonic-gate 	debugcnt++;
3550Sstevel@tonic-gate 	entry[curentry].nconf = netconfigdup(nconf);
3560Sstevel@tonic-gate 	entry[curentry].addr = netbufdup(&taddr->addr);
3570Sstevel@tonic-gate 
3580Sstevel@tonic-gate 	/*
3590Sstevel@tonic-gate 	 * if raw, print this entry out immediately
3600Sstevel@tonic-gate 	 * otherwise store for later sorting
3610Sstevel@tonic-gate 	 */
3620Sstevel@tonic-gate 	if (!hflag && !lflag && !tflag) {
3630Sstevel@tonic-gate 		if (netdir_getbyaddr(nconf, &hs, &taddr->addr) == ND_OK)
3640Sstevel@tonic-gate 			printf("%*.*s  ", MACHINELEN, MACHINELEN,
3650Sstevel@tonic-gate 				hs->h_hostservs->h_host);
3660Sstevel@tonic-gate 		else {
3670Sstevel@tonic-gate 			char *uaddr = taddr2uaddr(nconf, &taddr->addr);
3680Sstevel@tonic-gate 
3690Sstevel@tonic-gate 			if (uaddr) {
3700Sstevel@tonic-gate 				printf("  %*.*s", MACHINELEN, MACHINELEN,
3710Sstevel@tonic-gate 					uaddr);
3720Sstevel@tonic-gate 				(void) free(uaddr);
3730Sstevel@tonic-gate 			} else
3740Sstevel@tonic-gate 				printf("  %*.*s", MACHINELEN, MACHINELEN,
3750Sstevel@tonic-gate 					"unknown");
3760Sstevel@tonic-gate 		}
3770Sstevel@tonic-gate 		if (vers == RSTATVERS_TIME) {
3780Sstevel@tonic-gate 			putline(st->curtime.tv_sec, st->boottime, st->avenrun);
3790Sstevel@tonic-gate 		} else if (vers == RSTATVERS_VAR) {
3800Sstevel@tonic-gate 			putline(sv->curtime.tv_sec, sv->boottime, sv->avenrun);
3810Sstevel@tonic-gate 		}
3820Sstevel@tonic-gate 	} else {
3830Sstevel@tonic-gate 		if (vers == RSTATVERS_TIME) {
3840Sstevel@tonic-gate 			entry[curentry].boottime.tv_sec = st->boottime.tv_sec;
3850Sstevel@tonic-gate 			entry[curentry].boottime.tv_usec =
3860Sstevel@tonic-gate 				st->boottime.tv_usec;
3870Sstevel@tonic-gate 			entry[curentry].curtime = st->curtime.tv_sec;
3880Sstevel@tonic-gate 			memcpy(entry[curentry].avenrun, st->avenrun, AVENSIZE);
3890Sstevel@tonic-gate 		} else if (vers == RSTATVERS_VAR) {
3900Sstevel@tonic-gate 			entry[curentry].boottime.tv_sec = sv->boottime.tv_sec;
3910Sstevel@tonic-gate 			entry[curentry].boottime.tv_usec =
3920Sstevel@tonic-gate 				sv->boottime.tv_usec;
3930Sstevel@tonic-gate 			entry[curentry].curtime = sv->curtime.tv_sec;
3940Sstevel@tonic-gate 			memcpy(entry[curentry].avenrun, sv->avenrun, AVENSIZE);
3950Sstevel@tonic-gate 		}
3960Sstevel@tonic-gate 	}
3970Sstevel@tonic-gate 	curentry++;
3980Sstevel@tonic-gate 	if (dflag && debugcnt >= debug)
3990Sstevel@tonic-gate 		return (1);
4000Sstevel@tonic-gate 	return (0);
4010Sstevel@tonic-gate }
4020Sstevel@tonic-gate 
4030Sstevel@tonic-gate void
printsinglehosts()4040Sstevel@tonic-gate printsinglehosts()
4050Sstevel@tonic-gate {
4060Sstevel@tonic-gate 	register int i;
4070Sstevel@tonic-gate 	register struct entry *ep;
4080Sstevel@tonic-gate 
4090Sstevel@tonic-gate 
4100Sstevel@tonic-gate 	if (hflag)
4110Sstevel@tonic-gate 		qsort(entry, curentry, sizeof (struct entry), machinecmp);
4120Sstevel@tonic-gate 	else if (lflag)
4130Sstevel@tonic-gate 		qsort(entry, curentry, sizeof (struct entry), loadcmp);
4140Sstevel@tonic-gate 	else
4150Sstevel@tonic-gate 		qsort(entry, curentry, sizeof (struct entry), uptimecmp);
4160Sstevel@tonic-gate 	for (i = 0; i < curentry; i++) {
4170Sstevel@tonic-gate 		ep = &entry[i];
4180Sstevel@tonic-gate 		printf("%*.*s  ", MACHINELEN, MACHINELEN, ep->machine);
4190Sstevel@tonic-gate 		putline(ep->curtime, ep->boottime, ep->avenrun);
4200Sstevel@tonic-gate 
4210Sstevel@tonic-gate 	}
4220Sstevel@tonic-gate }
4230Sstevel@tonic-gate 
4240Sstevel@tonic-gate void
printnames()4250Sstevel@tonic-gate printnames()
4260Sstevel@tonic-gate {
4270Sstevel@tonic-gate 	char buf[MACHINELENMAX+1];
4280Sstevel@tonic-gate 	struct nd_hostservlist *hs;
4290Sstevel@tonic-gate 	register int i;
4300Sstevel@tonic-gate 	register struct entry *ep;
4310Sstevel@tonic-gate 
4320Sstevel@tonic-gate 
4330Sstevel@tonic-gate 	for (i = 0; i < curentry; i++) {
4340Sstevel@tonic-gate 		ep = &entry[i];
4350Sstevel@tonic-gate 		if (netdir_getbyaddr(ep->nconf, &hs, ep->addr) == ND_OK)
4360Sstevel@tonic-gate 			sprintf(buf, "%s", hs->h_hostservs->h_host);
4370Sstevel@tonic-gate 		else {
4380Sstevel@tonic-gate 			char *uaddr = taddr2uaddr(ep->nconf, ep->addr);
4390Sstevel@tonic-gate 
4400Sstevel@tonic-gate 			if (uaddr) {
4410Sstevel@tonic-gate 				sprintf(buf, "%s", uaddr);
4420Sstevel@tonic-gate 				(void) free(uaddr);
4430Sstevel@tonic-gate 			} else
4440Sstevel@tonic-gate 				sprintf(buf, "%s", "unknown");
4450Sstevel@tonic-gate 		}
4460Sstevel@tonic-gate 		if (ep->machine = (char *)malloc(MACHINELENMAX + 1))
4470Sstevel@tonic-gate 			strcpy(ep->machine, buf);
4480Sstevel@tonic-gate 	}
4490Sstevel@tonic-gate 	printf("\n");
4500Sstevel@tonic-gate 	printsinglehosts();
4510Sstevel@tonic-gate }
4520Sstevel@tonic-gate 
453*631Speteh int
machinecmp(struct entry * a,struct entry * b)454*631Speteh machinecmp(struct entry *a, struct entry *b)
4550Sstevel@tonic-gate {
4560Sstevel@tonic-gate 	return (strcmp(a->machine, b->machine));
4570Sstevel@tonic-gate }
4580Sstevel@tonic-gate 
459*631Speteh int
uptimecmp(struct entry * a,struct entry * b)460*631Speteh uptimecmp(struct entry *a, struct entry *b)
4610Sstevel@tonic-gate {
4620Sstevel@tonic-gate 	if (a->boottime.tv_sec != b->boottime.tv_sec)
4630Sstevel@tonic-gate 		return (a->boottime.tv_sec - b->boottime.tv_sec);
4640Sstevel@tonic-gate 	else
4650Sstevel@tonic-gate 		return (a->boottime.tv_usec - b->boottime.tv_usec);
4660Sstevel@tonic-gate }
4670Sstevel@tonic-gate 
468*631Speteh int
loadcmp(struct entry * a,struct entry * b)469*631Speteh loadcmp(struct entry *a, struct entry *b)
4700Sstevel@tonic-gate {
4710Sstevel@tonic-gate 	register int i;
4720Sstevel@tonic-gate 
4730Sstevel@tonic-gate 	for (i = 0; i < AVENSIZE / sizeof (a->avenrun[0]); i++)
4740Sstevel@tonic-gate 		if (a->avenrun[i] != b->avenrun[i])
4750Sstevel@tonic-gate 			return (a->avenrun[i] - b->avenrun[i]);
4760Sstevel@tonic-gate 
4770Sstevel@tonic-gate 	return (0);
4780Sstevel@tonic-gate }
4790Sstevel@tonic-gate 
4800Sstevel@tonic-gate struct netbuf *
netbufdup(ap)4810Sstevel@tonic-gate netbufdup(ap)
4820Sstevel@tonic-gate 	register struct netbuf	*ap;
4830Sstevel@tonic-gate {
4840Sstevel@tonic-gate 	register struct netbuf	*np;
4850Sstevel@tonic-gate 
4860Sstevel@tonic-gate 	np = (struct netbuf *) malloc(sizeof (struct netbuf) + ap->len);
4870Sstevel@tonic-gate 	if (np) {
4880Sstevel@tonic-gate 		np->maxlen = np->len = ap->len;
4890Sstevel@tonic-gate 		np->buf = ((char *)np) + sizeof (struct netbuf);
4900Sstevel@tonic-gate 		(void) memcpy(np->buf, ap->buf, ap->len);
4910Sstevel@tonic-gate 	}
4920Sstevel@tonic-gate 	return (np);
4930Sstevel@tonic-gate }
4940Sstevel@tonic-gate 
4950Sstevel@tonic-gate struct netconfig *
netconfigdup(onp)4960Sstevel@tonic-gate netconfigdup(onp)
4970Sstevel@tonic-gate 	register struct netconfig *onp;
4980Sstevel@tonic-gate {
4990Sstevel@tonic-gate 	register int nlookupdirs;
5000Sstevel@tonic-gate 	register struct netconfig *nnp;
5010Sstevel@tonic-gate 	extern char *strdup();
5020Sstevel@tonic-gate 
5030Sstevel@tonic-gate 	nnp = (struct netconfig *)malloc(sizeof (struct netconfig));
5040Sstevel@tonic-gate 	if (nnp) {
5050Sstevel@tonic-gate 		nnp->nc_netid = strdup(onp->nc_netid);
5060Sstevel@tonic-gate 		nnp->nc_semantics = onp->nc_semantics;
5070Sstevel@tonic-gate 		nnp->nc_flag = onp->nc_flag;
5080Sstevel@tonic-gate 		nnp->nc_protofmly = strdup(onp->nc_protofmly);
5090Sstevel@tonic-gate 		nnp->nc_proto = strdup(onp->nc_proto);
5100Sstevel@tonic-gate 		nnp->nc_device = strdup(onp->nc_device);
5110Sstevel@tonic-gate 		nnp->nc_nlookups = onp->nc_nlookups;
5120Sstevel@tonic-gate 		if (onp->nc_nlookups == 0)
5130Sstevel@tonic-gate 			nnp->nc_lookups = (char **)0;
5140Sstevel@tonic-gate 		else {
5150Sstevel@tonic-gate 			register int i;
5160Sstevel@tonic-gate 
5170Sstevel@tonic-gate 			nnp->nc_lookups = (char **)malloc(onp->nc_nlookups *
5180Sstevel@tonic-gate 			    sizeof (char *));
5190Sstevel@tonic-gate 			if (nnp->nc_lookups)
5200Sstevel@tonic-gate 				for (i = 0; i < onp->nc_nlookups; i++)
5210Sstevel@tonic-gate 					nnp->nc_lookups[i] =
5220Sstevel@tonic-gate 						strdup(onp->nc_lookups[i]);
5230Sstevel@tonic-gate 		}
5240Sstevel@tonic-gate 	}
5250Sstevel@tonic-gate 
5260Sstevel@tonic-gate 	return (nnp);
5270Sstevel@tonic-gate }
5280Sstevel@tonic-gate 
529*631Speteh int
netbufeq(struct netbuf * ap,struct netbuf * bp)530*631Speteh netbufeq(struct netbuf *ap, struct netbuf *bp)
5310Sstevel@tonic-gate {
5320Sstevel@tonic-gate 	return (ap->len == bp->len && !memcmp(ap->buf, bp->buf, ap->len));
5330Sstevel@tonic-gate }
5340Sstevel@tonic-gate 
535*631Speteh void
usage(void)536*631Speteh usage(void)
5370Sstevel@tonic-gate {
5380Sstevel@tonic-gate 	fprintf(stderr, "Usage: rup [-h] [-l] [-t] [host ...]\n");
5390Sstevel@tonic-gate 	free(entry);
5400Sstevel@tonic-gate 	exit(1);
5410Sstevel@tonic-gate }
542