17c478bdstevel@tonic-gate/*
27c478bdstevel@tonic-gate * CDDL HEADER START
37c478bdstevel@tonic-gate *
47c478bdstevel@tonic-gate * The contents of this file are subject to the terms of the
57c478bdstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
67c478bdstevel@tonic-gate * (the "License").  You may not use this file except in compliance
77c478bdstevel@tonic-gate * with the License.
87c478bdstevel@tonic-gate *
97c478bdstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bdstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
117c478bdstevel@tonic-gate * See the License for the specific language governing permissions
127c478bdstevel@tonic-gate * and limitations under the License.
137c478bdstevel@tonic-gate *
147c478bdstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
157c478bdstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bdstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
177c478bdstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
187c478bdstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bdstevel@tonic-gate *
207c478bdstevel@tonic-gate * CDDL HEADER END
217c478bdstevel@tonic-gate */
227c478bdstevel@tonic-gate
237c478bdstevel@tonic-gate/*
2449e7ca4peteh * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
257c478bdstevel@tonic-gate * Use is subject to license terms.
267c478bdstevel@tonic-gate */
277c478bdstevel@tonic-gate
287c478bdstevel@tonic-gate#include <stdio.h>
297c478bdstevel@tonic-gate#include <stdlib.h>
307c478bdstevel@tonic-gate#include <netdb.h>
317c478bdstevel@tonic-gate#include <sys/param.h>
327c478bdstevel@tonic-gate#include <sys/stat.h>
337c478bdstevel@tonic-gate#include <sys/time.h>
347c478bdstevel@tonic-gate#include <sys/socket.h>
357c478bdstevel@tonic-gate#include <netinet/in.h>
367c478bdstevel@tonic-gate#include <rpc/rpc.h>
377c478bdstevel@tonic-gate#include <netdir.h>
387c478bdstevel@tonic-gate#include <rpcsvc/rstat.h>
397c478bdstevel@tonic-gate#include <rpc/pmap_clnt.h>
407c478bdstevel@tonic-gate
417c478bdstevel@tonic-gate
427c478bdstevel@tonic-gate#define	MACHINELEN	15	/* length of machine name printed out */
437c478bdstevel@tonic-gate#define	MACHINELENMAX	128	/* maximum machine name length */
447c478bdstevel@tonic-gate#define	AVENSIZE	(3 * sizeof (long))
457c478bdstevel@tonic-gate#define	SLOTS	256
467c478bdstevel@tonic-gate
477c478bdstevel@tonic-gateint machinecmp();
487c478bdstevel@tonic-gateint loadcmp();
497c478bdstevel@tonic-gateint uptimecmp();
5049e7ca4petehstatic int collectnames();
517c478bdstevel@tonic-gateint singlehost();		/* returns 1 if rup of given host fails */
527c478bdstevel@tonic-gatevoid printsinglehosts();
537c478bdstevel@tonic-gatevoid printnames();
5449e7ca4petehstatic void putline();
5549e7ca4petehint netbufeq(struct netbuf *ap, struct netbuf *bp);
5649e7ca4petehvoid usage(void);
577c478bdstevel@tonic-gate
587c478bdstevel@tonic-gatestruct entry {
597c478bdstevel@tonic-gate	struct netconfig *nconf;
607c478bdstevel@tonic-gate	struct netbuf *addr;
617c478bdstevel@tonic-gate	char *machine;
627c478bdstevel@tonic-gate	struct timeval boottime;
637c478bdstevel@tonic-gate	time_t curtime;
647c478bdstevel@tonic-gate	long avenrun[3];
657c478bdstevel@tonic-gate};
667c478bdstevel@tonic-gate
677c478bdstevel@tonic-gateint total_entries;
687c478bdstevel@tonic-gateint curentry;
697c478bdstevel@tonic-gatestruct entry *entry;
707c478bdstevel@tonic-gateint vers;			/* which version did the broadcasting */
717c478bdstevel@tonic-gateint lflag;			/* load: sort by load average */
727c478bdstevel@tonic-gateint tflag;			/* time: sort by uptime average */
737c478bdstevel@tonic-gateint hflag;			/* host: sort by machine name */
747c478bdstevel@tonic-gateint dflag;			/* debug: list only first n machines */
757c478bdstevel@tonic-gateint debug;
767c478bdstevel@tonic-gate
7749e7ca4petehint
7849e7ca4petehmain(int argc, char *argv[])
797c478bdstevel@tonic-gate{
807c478bdstevel@tonic-gate	statsvar sv;
817c478bdstevel@tonic-gate	statstime st;
827c478bdstevel@tonic-gate	int single, nfailed;
837c478bdstevel@tonic-gate
847c478bdstevel@tonic-gate	/*
857c478bdstevel@tonic-gate	 * set number of slots to be 256 to begin with,
867c478bdstevel@tonic-gate	 * this is large enough for most subnets but not all
877c478bdstevel@tonic-gate	 */
887c478bdstevel@tonic-gate
897c478bdstevel@tonic-gate	curentry = 0;
907c478bdstevel@tonic-gate	total_entries = SLOTS;
917c478bdstevel@tonic-gate	entry = malloc(sizeof (struct entry) * total_entries);
927c478bdstevel@tonic-gate	single = nfailed = 0;
937c478bdstevel@tonic-gate	while (argc > 1) {
947c478bdstevel@tonic-gate		if (argv[1][0] != '-') {
957c478bdstevel@tonic-gate			single++;
967c478bdstevel@tonic-gate			nfailed += singlehost(argv[1]);
977c478bdstevel@tonic-gate		} else {
987c478bdstevel@tonic-gate			switch (argv[1][1]) {
997c478bdstevel@tonic-gate
1007c478bdstevel@tonic-gate			case 'l':
1017c478bdstevel@tonic-gate				lflag++;
1027c478bdstevel@tonic-gate				break;
1037c478bdstevel@tonic-gate			case 't':
1047c478bdstevel@tonic-gate				tflag++;
1057c478bdstevel@tonic-gate				break;
1067c478bdstevel@tonic-gate			case 'h':
1077c478bdstevel@tonic-gate				hflag++;
1087c478bdstevel@tonic-gate				break;
1097c478bdstevel@tonic-gate			case 'd':
1107c478bdstevel@tonic-gate				dflag++;
1117c478bdstevel@tonic-gate				if (argc < 3)
1127c478bdstevel@tonic-gate					usage();
1137c478bdstevel@tonic-gate				debug = atoi(argv[2]);
1147c478bdstevel@tonic-gate				argc--;
1157c478bdstevel@tonic-gate				argv++;
1167c478bdstevel@tonic-gate				break;
1177c478bdstevel@tonic-gate			default:
1187c478bdstevel@tonic-gate				usage();
1197c478bdstevel@tonic-gate			}
1207c478bdstevel@tonic-gate		}
1217c478bdstevel@tonic-gate		argv++;
1227c478bdstevel@tonic-gate		argc--;
1237c478bdstevel@tonic-gate	}
1247c478bdstevel@tonic-gate	if (single > 0) {
1257c478bdstevel@tonic-gate		if (hflag || tflag || lflag)
1267c478bdstevel@tonic-gate			printsinglehosts();
1277c478bdstevel@tonic-gate		if (nfailed == single) {
1287c478bdstevel@tonic-gate			free(entry);
1297c478bdstevel@tonic-gate			exit(1);	/* all hosts we tried failed */
1307c478bdstevel@tonic-gate		} else {
1317c478bdstevel@tonic-gate			free(entry);
1327c478bdstevel@tonic-gate			exit(0);
1337c478bdstevel@tonic-gate		}
1347c478bdstevel@tonic-gate
1357c478bdstevel@tonic-gate	}
1367c478bdstevel@tonic-gate	if (hflag || tflag || lflag) {
1377c478bdstevel@tonic-gate		printf("collecting responses... ");
1387c478bdstevel@tonic-gate		fflush(stdout);
1397c478bdstevel@tonic-gate	}
1407c478bdstevel@tonic-gate
1417c478bdstevel@tonic-gate	sv.cp_time.cp_time_val = (int *)NULL;
1427c478bdstevel@tonic-gate	sv.dk_xfer.dk_xfer_val = (int *)NULL;
1437c478bdstevel@tonic-gate
1447c478bdstevel@tonic-gate	/*
1457c478bdstevel@tonic-gate	 * Null out pointers in the statsvar struct
1467c478bdstevel@tonic-gate	 * so that we don't follow a random pointer
1477c478bdstevel@tonic-gate	 * somewhere when we get our results back.
1487c478bdstevel@tonic-gate	 * Set lengths to zero so we don't allocate
1497c478bdstevel@tonic-gate	 * some random amount of space we don't need
1507c478bdstevel@tonic-gate	 * (in the case where the reply was program
1517c478bdstevel@tonic-gate	 *  not registered).
1527c478bdstevel@tonic-gate	 */
1537c478bdstevel@tonic-gate	sv.cp_time.cp_time_len = 0;
1547c478bdstevel@tonic-gate	sv.cp_time.cp_time_val = (int *)NULL;
1557c478bdstevel@tonic-gate	sv.dk_xfer.dk_xfer_len = 0;
1567c478bdstevel@tonic-gate	sv.dk_xfer.dk_xfer_val = (int *)NULL;
1577c478bdstevel@tonic-gate
1587c478bdstevel@tonic-gate	vers = RSTATVERS_VAR;
159d7dc203Toomas Soome	(void) rpc_broadcast(RSTATPROG, RSTATVERS_VAR, RSTATPROC_STATS,
1607c478bdstevel@tonic-gate			xdr_void, NULL, xdr_statsvar, (caddr_t)&sv,
1617c478bdstevel@tonic-gate			(resultproc_t)collectnames, (char *)0);
1627c478bdstevel@tonic-gate	vers = RSTATVERS_TIME;
163d7dc203Toomas Soome	(void) rpc_broadcast(RSTATPROG, RSTATVERS_TIME, RSTATPROC_STATS,
1647c478bdstevel@tonic-gate			xdr_void, NULL, xdr_statstime, (caddr_t)&st,
1657c478bdstevel@tonic-gate			(resultproc_t)collectnames, (char *)0);
1667c478bdstevel@tonic-gate	if (hflag || tflag || lflag)
1677c478bdstevel@tonic-gate		printnames();
1687c478bdstevel@tonic-gate
1697c478bdstevel@tonic-gate
1707c478bdstevel@tonic-gate
1717c478bdstevel@tonic-gate	free(entry);
17249e7ca4peteh	return (0);
1737c478bdstevel@tonic-gate}
1747c478bdstevel@tonic-gate
1757c478bdstevel@tonic-gateint
1767c478bdstevel@tonic-gatesinglehost(host)
1777c478bdstevel@tonic-gate	char *host;
1787c478bdstevel@tonic-gate{
1797c478bdstevel@tonic-gate	static int debugcnt;
1807c478bdstevel@tonic-gate	enum clnt_stat err;
1817c478bdstevel@tonic-gate	statstime st;
1827c478bdstevel@tonic-gate	statsvar sw_var;
1837c478bdstevel@tonic-gate	bool_t is_var_vers = FALSE;
1847c478bdstevel@tonic-gate
1857c478bdstevel@tonic-gate
1867c478bdstevel@tonic-gate	if (curentry >= total_entries) {
1877c478bdstevel@tonic-gate		struct entry *tmp;
1887c478bdstevel@tonic-gate
1897c478bdstevel@tonic-gate		total_entries += SLOTS;
1907c478bdstevel@tonic-gate		tmp = realloc((struct entry *)entry, sizeof (struct entry)
1917c478bdstevel@tonic-gate						* total_entries);
1927c478bdstevel@tonic-gate		if (tmp == NULL) {
1937c478bdstevel@tonic-gate			return (1);
1947c478bdstevel@tonic-gate		}
1957c478bdstevel@tonic-gate		entry = tmp;
1967c478bdstevel@tonic-gate	}
1977c478bdstevel@tonic-gate
1987c478bdstevel@tonic-gate	sw_var.cp_time.cp_time_val = (int *)NULL;
1997c478bdstevel@tonic-gate	sw_var.dk_xfer.dk_xfer_val = (int *)NULL;
2007c478bdstevel@tonic-gate	err = (enum clnt_stat)callrpc(host, RSTATPROG, RSTATVERS_VAR,
2017c478bdstevel@tonic-gate			RSTATPROC_STATS, xdr_void, 0, xdr_statsvar, &sw_var);
2027c478bdstevel@tonic-gate	if (err == RPC_SUCCESS) {
2037c478bdstevel@tonic-gate		is_var_vers = TRUE;
2047c478bdstevel@tonic-gate	} else if (err == RPC_PROGVERSMISMATCH) {
2057c478bdstevel@tonic-gate		err = (enum clnt_stat)callrpc(host, RSTATPROG, RSTATVERS_TIME,
2067c478bdstevel@tonic-gate			RSTATPROC_STATS, xdr_void, 0, xdr_statstime, &st);
2077c478bdstevel@tonic-gate		if (err != RPC_SUCCESS)
2087c478bdstevel@tonic-gate			goto error;
2097c478bdstevel@tonic-gate	} else
2107c478bdstevel@tonic-gate		goto error;
2117c478bdstevel@tonic-gate
2127c478bdstevel@tonic-gate	debugcnt++;
2137c478bdstevel@tonic-gate	if (!hflag && !lflag && !tflag) {
2147c478bdstevel@tonic-gate		printf("%*.*s  ", MACHINELEN, MACHINELEN, host);
2157c478bdstevel@tonic-gate		if (is_var_vers == TRUE)
2167c478bdstevel@tonic-gate			putline(sw_var.curtime.tv_sec, sw_var.boottime,
2177c478bdstevel@tonic-gate				sw_var.avenrun);
2187c478bdstevel@tonic-gate		else
2197c478bdstevel@tonic-gate			putline(st.curtime.tv_sec, st.boottime, st.avenrun);
2207c478bdstevel@tonic-gate		return (0);		/* success */
2217c478bdstevel@tonic-gate	} else {
2227c478bdstevel@tonic-gate		entry[curentry].machine = host;
2237c478bdstevel@tonic-gate		if (is_var_vers == FALSE) { /* RSTATVERS_TIME */
2247c478bdstevel@tonic-gate			entry[curentry].boottime.tv_sec = st.boottime.tv_sec;
2257c478bdstevel@tonic-gate			entry[curentry].boottime.tv_usec =
2267c478bdstevel@tonic-gate				st.boottime.tv_usec;
2277c478bdstevel@tonic-gate			entry[curentry].curtime = st.curtime.tv_sec;
2287c478bdstevel@tonic-gate			memcpy(entry[curentry].avenrun, st.avenrun, AVENSIZE);
2297c478bdstevel@tonic-gate		} else { /* RSTATVERS_VAR */
2307c478bdstevel@tonic-gate			entry[curentry].boottime.tv_sec =
2317c478bdstevel@tonic-gate				sw_var.boottime.tv_sec;
2327c478bdstevel@tonic-gate			entry[curentry].boottime.tv_usec =
2337c478bdstevel@tonic-gate				sw_var.boottime.tv_usec;
2347c478bdstevel@tonic-gate			entry[curentry].curtime = sw_var.curtime.tv_sec;
2357c478bdstevel@tonic-gate			memcpy(entry[curentry].avenrun, sw_var.avenrun,
2367c478bdstevel@tonic-gate							AVENSIZE);
2377c478bdstevel@tonic-gate		}
2387c478bdstevel@tonic-gate	}
2397c478bdstevel@tonic-gate	curentry++;
2407c478bdstevel@tonic-gate	if (dflag && debugcnt >= debug)
2417c478bdstevel@tonic-gate		return (1);
2427c478bdstevel@tonic-gate	return (0);
2437c478bdstevel@tonic-gate
2447c478bdstevel@tonic-gateerror:
2457c478bdstevel@tonic-gate	fprintf(stderr, "%*.*s: ", MACHINELEN, MACHINELEN, host);
2467c478bdstevel@tonic-gate	clnt_perrno(err);
2477c478bdstevel@tonic-gate	/*
2487c478bdstevel@tonic-gate	 * clnt_perrno now prints a newline
2497c478bdstevel@tonic-gate	 */
2507c478bdstevel@tonic-gate	/* fprintf(stderr, "\n"); */
2517c478bdstevel@tonic-gate	return (1);		/* a failure */
2527c478bdstevel@tonic-gate}
2537c478bdstevel@tonic-gate
25449e7ca4petehstatic void
2557c478bdstevel@tonic-gateputline(now, boottime, avenrun)
2567c478bdstevel@tonic-gate	time_t now;
2577c478bdstevel@tonic-gate	struct timeval boottime;
2587c478bdstevel@tonic-gate	long avenrun[];
2597c478bdstevel@tonic-gate{
2607c478bdstevel@tonic-gate	int uptime, days, hrs, mins, i;
2617c478bdstevel@tonic-gate
2627c478bdstevel@tonic-gate	uptime = now - boottime.tv_sec;
2637c478bdstevel@tonic-gate	uptime += 30;
2647c478bdstevel@tonic-gate	if (uptime < 0)		/* unsynchronized clocks */
2657c478bdstevel@tonic-gate		uptime = 0;
2667c478bdstevel@tonic-gate	days = uptime / (60*60*24);
2677c478bdstevel@tonic-gate	uptime %= (60*60*24);
2687c478bdstevel@tonic-gate	hrs = uptime / (60*60);
2697c478bdstevel@tonic-gate	uptime %= (60*60);
2707c478bdstevel@tonic-gate	mins = uptime / 60;
2717c478bdstevel@tonic-gate
2727c478bdstevel@tonic-gate	printf("  up");
2737c478bdstevel@tonic-gate	if (days > 0)
2747c478bdstevel@tonic-gate		printf(" %2d day%s", days, days > 1 ? "s," : ", ");
2757c478bdstevel@tonic-gate	else
2767c478bdstevel@tonic-gate		printf("         ");
2777c478bdstevel@tonic-gate	if (hrs > 0)
2787c478bdstevel@tonic-gate		printf(" %2d:%02d,  ", hrs, mins);
2797c478bdstevel@tonic-gate	else
2807c478bdstevel@tonic-gate		printf(" %2d min%s", mins, mins > 1 ? "s," : ", ");
2817c478bdstevel@tonic-gate
2827c478bdstevel@tonic-gate	/*
2837c478bdstevel@tonic-gate	 * Print 1, 5, and 15 minute load averages.
2847c478bdstevel@tonic-gate	 * (Found by looking in kernel for avenrun).
2857c478bdstevel@tonic-gate	 */
2867c478bdstevel@tonic-gate	printf("  load average:");
2877c478bdstevel@tonic-gate	for (i = 0; i < (AVENSIZE / sizeof (avenrun[0])); i++) {
2887c478bdstevel@tonic-gate		if (i > 0)
2897c478bdstevel@tonic-gate			printf(",");
2907c478bdstevel@tonic-gate		printf(" %.2f", (double)avenrun[i]/FSCALE);
2917c478bdstevel@tonic-gate	}
2927c478bdstevel@tonic-gate	printf("\n");
2937c478bdstevel@tonic-gate}
2947c478bdstevel@tonic-gate
29549e7ca4petehstatic int
2967c478bdstevel@tonic-gatecollectnames(resultsp, taddr, nconf)
2977c478bdstevel@tonic-gate	char *resultsp;
29849e7ca4peteh	struct t_bind *taddr;
29949e7ca4peteh	struct netconfig *nconf;
3007c478bdstevel@tonic-gate{
3017c478bdstevel@tonic-gate	static int debugcnt;
3027c478bdstevel@tonic-gate	register struct entry *entryp, *lim;
3037c478bdstevel@tonic-gate	statstime *st;
3047c478bdstevel@tonic-gate	statsvar *sv;
3057c478bdstevel@tonic-gate	struct nd_hostservlist *hs;
3067c478bdstevel@tonic-gate	extern struct netbuf *netbufdup();
3077c478bdstevel@tonic-gate	extern struct netconfig *netconfigdup();
3087c478bdstevel@tonic-gate	extern int netbufeq();
3097c478bdstevel@tonic-gate
3107c478bdstevel@tonic-gate	/*
3117c478bdstevel@tonic-gate	 * need to realloc more space if we have more than 256 machines
3127c478bdstevel@tonic-gate	 * that responded to the broadcast
3137c478bdstevel@tonic-gate	 */
3147c478bdstevel@tonic-gate
3157c478bdstevel@tonic-gate	if (curentry >= total_entries) {
3167c478bdstevel@tonic-gate		struct entry *tmp;
3177c478bdstevel@tonic-gate
3187c478bdstevel@tonic-gate		total_entries += SLOTS;
3197c478bdstevel@tonic-gate		tmp = realloc((struct entry *)entry, sizeof (struct entry)
3207c478bdstevel@tonic-gate						* total_entries);
3217c478bdstevel@tonic-gate		if (tmp == NULL) {
3227c478bdstevel@tonic-gate			return (1);
3237c478bdstevel@tonic-gate		}
3247c478bdstevel@tonic-gate		entry = tmp;
3257c478bdstevel@tonic-gate	}
3267c478bdstevel@tonic-gate	/*
3277c478bdstevel@tonic-gate	 * weed out duplicates
3287c478bdstevel@tonic-gate	 */
3297c478bdstevel@tonic-gate	lim = entry + curentry;
3307c478bdstevel@tonic-gate	for (entryp = entry; entryp < lim; entryp++)
3317c478bdstevel@tonic-gate		if (netbufeq(&taddr->addr, entryp->addr))
3327c478bdstevel@tonic-gate			return (0);
3337c478bdstevel@tonic-gate
3347c478bdstevel@tonic-gate	if (vers == RSTATVERS_TIME) {
3357c478bdstevel@tonic-gate		st = (statstime *)resultsp;
3367c478bdstevel@tonic-gate	} else if (vers == RSTATVERS_VAR) {
3377c478bdstevel@tonic-gate		sv = (statsvar *)resultsp;
3387c478bdstevel@tonic-gate	} else {
3397c478bdstevel@tonic-gate		return (0);	/* we don't handle this version */
3407c478bdstevel@tonic-gate	}
3417c478bdstevel@tonic-gate	debugcnt++;
3427c478bdstevel@tonic-gate	entry[curentry].nconf = netconfigdup(nconf);
3437c478bdstevel@tonic-gate	entry[curentry].addr = netbufdup(&taddr->addr);
3447c478bdstevel@tonic-gate
3457c478bdstevel@tonic-gate	/*
3467c478bdstevel@tonic-gate	 * if raw, print this entry out immediately
3477c478bdstevel@tonic-gate	 * otherwise store for later sorting
3487c478bdstevel@tonic-gate	 */
3497c478bdstevel@tonic-gate	if (!hflag && !lflag && !tflag) {
3507c478bdstevel@tonic-gate		if (netdir_getbyaddr(nconf, &hs, &taddr->addr) == ND_OK)
3517c478bdstevel@tonic-gate			printf("%*.*s  ", MACHINELEN, MACHINELEN,
3527c478bdstevel@tonic-gate				hs->h_hostservs->h_host);
3537c478bdstevel@tonic-gate		else {
3547c478bdstevel@tonic-gate			char *uaddr = taddr2uaddr(nconf, &taddr->addr);
3557c478bdstevel@tonic-gate
3567c478bdstevel@tonic-gate			if (uaddr) {
3577c478bdstevel@tonic-gate				printf("  %*.*s", MACHINELEN, MACHINELEN,
3587c478bdstevel@tonic-gate					uaddr);
3597c478bdstevel@tonic-gate				(void) free(uaddr);
3607c478bdstevel@tonic-gate			} else
3617c478bdstevel@tonic-gate				printf("  %*.*s", MACHINELEN, MACHINELEN,
3627c478bdstevel@tonic-gate					"unknown");
3637c478bdstevel@tonic-gate		}
3647c478bdstevel@tonic-gate		if (vers == RSTATVERS_TIME) {
3657c478bdstevel@tonic-gate			putline(st->curtime.tv_sec, st->boottime, st->avenrun);
3667c478bdstevel@tonic-gate		} else if (vers == RSTATVERS_VAR) {
3677c478bdstevel@tonic-gate			putline(sv->curtime.tv_sec, sv->boottime, sv->avenrun);
3687c478bdstevel@tonic-gate		}
3697c478bdstevel@tonic-gate	} else {
3707c478bdstevel@tonic-gate		if (vers == RSTATVERS_TIME) {
3717c478bdstevel@tonic-gate			entry[curentry].boottime.tv_sec = st->boottime.tv_sec;
3727c478bdstevel@tonic-gate			entry[curentry].boottime.tv_usec =
3737c478bdstevel@tonic-gate				st->boottime.tv_usec;
3747c478bdstevel@tonic-gate			entry[curentry].curtime = st->curtime.tv_sec;
3757c478bdstevel@tonic-gate			memcpy(entry[curentry].avenrun, st->avenrun, AVENSIZE);
3767c478bdstevel@tonic-gate		} else if (vers == RSTATVERS_VAR) {
3777c478bdstevel@tonic-gate			entry[curentry].boottime.tv_sec = sv->boottime.tv_sec;
3787c478bdstevel@tonic-gate			entry[curentry].boottime.tv_usec =
3797c478bdstevel@tonic-gate				sv->boottime.tv_usec;
3807c478bdstevel@tonic-gate			entry[curentry].curtime = sv->curtime.tv_sec;
3817c478bdstevel@tonic-gate			memcpy(entry[curentry].avenrun, sv->avenrun, AVENSIZE);
3827c478bdstevel@tonic-gate		}
3837c478bdstevel@tonic-gate	}
3847c478bdstevel@tonic-gate	curentry++;
3857c478bdstevel@tonic-gate	if (dflag && debugcnt >= debug)
3867c478bdstevel@tonic-gate		return (1);
3877c478bdstevel@tonic-gate	return (0);
3887c478bdstevel@tonic-gate}
3897c478bdstevel@tonic-gate
3907c478bdstevel@tonic-gatevoid
3917c478bdstevel@tonic-gateprintsinglehosts()
3927c478bdstevel@tonic-gate{
3937c478bdstevel@tonic-gate	register int i;
3947c478bdstevel@tonic-gate	register struct entry *ep;
3957c478bdstevel@tonic-gate
3967c478bdstevel@tonic-gate
3977c478bdstevel@tonic-gate	if (hflag)
3987c478bdstevel@tonic-gate		qsort(entry, curentry, sizeof (struct entry), machinecmp);
3997c478bdstevel@tonic-gate	else if (lflag)
4007c478bdstevel@tonic-gate		qsort(entry, curentry, sizeof (struct entry), loadcmp);
4017c478bdstevel@tonic-gate	else
4027c478bdstevel@tonic-gate		qsort(entry, curentry, sizeof (struct entry), uptimecmp);
4037c478bdstevel@tonic-gate	for (i = 0; i < curentry; i++) {
4047c478bdstevel@tonic-gate		ep = &entry[i];
4057c478bdstevel@tonic-gate		printf("%*.*s  ", MACHINELEN, MACHINELEN, ep->machine);
4067c478bdstevel@tonic-gate		putline(ep->curtime, ep->boottime, ep->avenrun);
4077c478bdstevel@tonic-gate
4087c478bdstevel@tonic-gate	}
4097c478bdstevel@tonic-gate}
4107c478bdstevel@tonic-gate
4117c478bdstevel@tonic-gatevoid
4127c478bdstevel@tonic-gateprintnames()
4137c478bdstevel@tonic-gate{
4147c478bdstevel@tonic-gate	char buf[MACHINELENMAX+1];
4157c478bdstevel@tonic-gate	struct nd_hostservlist *hs;
4167c478bdstevel@tonic-gate	register int i;
4177c478bdstevel@tonic-gate	register struct entry *ep;
4187c478bdstevel@tonic-gate
4197c478bdstevel@tonic-gate
4207c478bdstevel@tonic-gate	for (i = 0; i < curentry; i++) {
4217c478bdstevel@tonic-gate		ep = &entry[i];
4227c478bdstevel@tonic-gate		if (netdir_getbyaddr(ep->nconf, &hs, ep->addr) == ND_OK)
4237c478bdstevel@tonic-gate			sprintf(buf, "%s", hs->h_hostservs->h_host);
4247c478bdstevel@tonic-gate		else {
4257c478bdstevel@tonic-gate			char *uaddr = taddr2uaddr(ep->nconf, ep->addr);
4267c478bdstevel@tonic-gate
4277c478bdstevel@tonic-gate			if (uaddr) {
4287c478bdstevel@tonic-gate				sprintf(buf, "%s", uaddr);
4297c478bdstevel@tonic-gate				(void) free(uaddr);
4307c478bdstevel@tonic-gate			} else
4317c478bdstevel@tonic-gate				sprintf(buf, "%s", "unknown");
4327c478bdstevel@tonic-gate		}
4337c478bdstevel@tonic-gate		if (ep->machine = (char *)malloc(MACHINELENMAX + 1))
4347c478bdstevel@tonic-gate			strcpy(ep->machine, buf);
4357c478bdstevel@tonic-gate	}
4367c478bdstevel@tonic-gate	printf("\n");
4377c478bdstevel@tonic-gate	printsinglehosts();
4387c478bdstevel@tonic-gate}
4397c478bdstevel@tonic-gate
44049e7ca4petehint
44149e7ca4petehmachinecmp(struct entry *a, struct entry *b)
4427c478bdstevel@tonic-gate{
4437c478bdstevel@tonic-gate	return (strcmp(a->machine, b->machine));
4447c478bdstevel@tonic-gate}
4457c478bdstevel@tonic-gate
44649e7ca4petehint
44749e7ca4petehuptimecmp(struct entry *a, struct entry *b)
4487c478bdstevel@tonic-gate{
4497c478bdstevel@tonic-gate	if (a->boottime.tv_sec != b->boottime.tv_sec)
4507c478bdstevel@tonic-gate		return (a->boottime.tv_sec - b->boottime.tv_sec);
4517c478bdstevel@tonic-gate	else
4527c478bdstevel@tonic-gate		return (a->boottime.tv_usec - b->boottime.tv_usec);
4537c478bdstevel@tonic-gate}
4547c478bdstevel@tonic-gate
45549e7ca4petehint
45649e7ca4petehloadcmp(struct entry *a, struct entry *b)
4577c478bdstevel@tonic-gate{
4587c478bdstevel@tonic-gate	register int i;
4597c478bdstevel@tonic-gate
4607c478bdstevel@tonic-gate	for (i = 0; i < AVENSIZE / sizeof (a->avenrun[0]); i++)
4617c478bdstevel@tonic-gate		if (a->avenrun[i] != b->avenrun[i])
4627c478bdstevel@tonic-gate			return (a->avenrun[i] - b->avenrun[i]);
4637c478bdstevel@tonic-gate
4647c478bdstevel@tonic-gate	return (0);
4657c478bdstevel@tonic-gate}
4667c478bdstevel@tonic-gate
4677c478bdstevel@tonic-gatestruct netbuf *
4687c478bdstevel@tonic-gatenetbufdup(ap)
4697c478bdstevel@tonic-gate	register struct netbuf	*ap;
4707c478bdstevel@tonic-gate{
4717c478bdstevel@tonic-gate	register struct netbuf	*np;
4727c478bdstevel@tonic-gate
4737c478bdstevel@tonic-gate	np = (struct netbuf *) malloc(sizeof (struct netbuf) + ap->len);
4747c478bdstevel@tonic-gate	if (np) {
4757c478bdstevel@tonic-gate		np->maxlen = np->len = ap->len;
4767c478bdstevel@tonic-gate		np->buf = ((char *)np) + sizeof (struct netbuf);
4777c478bdstevel@tonic-gate		(void) memcpy(np->buf, ap->buf, ap->len);
4787c478bdstevel@tonic-gate	}
4797c478bdstevel@tonic-gate	return (np);
4807c478bdstevel@tonic-gate}
4817c478bdstevel@tonic-gate
4827c478bdstevel@tonic-gatestruct netconfig *
4837c478bdstevel@tonic-gatenetconfigdup(onp)
4847c478bdstevel@tonic-gate	register struct netconfig *onp;
4857c478bdstevel@tonic-gate{
4867c478bdstevel@tonic-gate	register int nlookupdirs;
4877c478bdstevel@tonic-gate	register struct netconfig *nnp;
4887c478bdstevel@tonic-gate	extern char *strdup();
4897c478bdstevel@tonic-gate
4907c478bdstevel@tonic-gate	nnp = (struct netconfig *)malloc(sizeof (struct netconfig));
4917c478bdstevel@tonic-gate	if (nnp) {
4927c478bdstevel@tonic-gate		nnp->nc_netid = strdup(onp->nc_netid);
4937c478bdstevel@tonic-gate		nnp->nc_semantics = onp->nc_semantics;
4947c478bdstevel@tonic-gate		nnp->nc_flag = onp->nc_flag;
4957c478bdstevel@tonic-gate		nnp->nc_protofmly = strdup(onp->nc_protofmly);
4967c478bdstevel@tonic-gate		nnp->nc_proto = strdup(onp->nc_proto);
4977c478bdstevel@tonic-gate		nnp->nc_device = strdup(onp->nc_device);
4987c478bdstevel@tonic-gate		nnp->nc_nlookups = onp->nc_nlookups;
4997c478bdstevel@tonic-gate		if (onp->nc_nlookups == 0)
5007c478bdstevel@tonic-gate			nnp->nc_lookups = (char **)0;
5017c478bdstevel@tonic-gate		else {
5027c478bdstevel@tonic-gate			register int i;
5037c478bdstevel@tonic-gate
5047c478bdstevel@tonic-gate			nnp->nc_lookups = (char **)malloc(onp->nc_nlookups *
5057c478bdstevel@tonic-gate			    sizeof (char *));
5067c478bdstevel@tonic-gate			if (nnp->nc_lookups)
5077c478bdstevel@tonic-gate				for (i = 0; i < onp->nc_nlookups; i++)
5087c478bdstevel@tonic-gate					nnp->nc_lookups[i] =
5097c478bdstevel@tonic-gate						strdup(onp->nc_lookups[i]);
5107c478bdstevel@tonic-gate		}
5117c478bdstevel@tonic-gate	}
5127c478bdstevel@tonic-gate
5137c478bdstevel@tonic-gate	return (nnp);
5147c478bdstevel@tonic-gate}
5157c478bdstevel@tonic-gate
51649e7ca4petehint
51749e7ca4petehnetbufeq(struct netbuf *ap, struct netbuf *bp)
5187c478bdstevel@tonic-gate{
5197c478bdstevel@tonic-gate	return (ap->len == bp->len && !memcmp(ap->buf, bp->buf, ap->len));
5207c478bdstevel@tonic-gate}
5217c478bdstevel@tonic-gate
52249e7ca4petehvoid
52349e7ca4petehusage(void)
5247c478bdstevel@tonic-gate{
5257c478bdstevel@tonic-gate	fprintf(stderr, "Usage: rup [-h] [-l] [-t] [host ...]\n");
5267c478bdstevel@tonic-gate	free(entry);
5277c478bdstevel@tonic-gate	exit(1);
5287c478bdstevel@tonic-gate}
529