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