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