xref: /titanic_44/usr/src/cmd/rpcsvc/rup.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
23*7c478bd9Sstevel@tonic-gate 
24*7c478bd9Sstevel@tonic-gate /*
25*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
26*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
27*7c478bd9Sstevel@tonic-gate  */
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #include <stdio.h>
30*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
31*7c478bd9Sstevel@tonic-gate #include <netdb.h>
32*7c478bd9Sstevel@tonic-gate #include <sys/param.h>
33*7c478bd9Sstevel@tonic-gate #include <sys/stat.h>
34*7c478bd9Sstevel@tonic-gate #include <sys/time.h>
35*7c478bd9Sstevel@tonic-gate #include <sys/socket.h>
36*7c478bd9Sstevel@tonic-gate #include <netinet/in.h>
37*7c478bd9Sstevel@tonic-gate #include <rpc/rpc.h>
38*7c478bd9Sstevel@tonic-gate #include <netdir.h>
39*7c478bd9Sstevel@tonic-gate #include <rpcsvc/rstat.h>
40*7c478bd9Sstevel@tonic-gate #include <rpc/pmap_clnt.h>
41*7c478bd9Sstevel@tonic-gate 
42*7c478bd9Sstevel@tonic-gate 
43*7c478bd9Sstevel@tonic-gate #define	MACHINELEN	15	/* length of machine name printed out */
44*7c478bd9Sstevel@tonic-gate #define	MACHINELENMAX	128	/* maximum machine name length */
45*7c478bd9Sstevel@tonic-gate #define	AVENSIZE	(3 * sizeof (long))
46*7c478bd9Sstevel@tonic-gate #define	SLOTS	256
47*7c478bd9Sstevel@tonic-gate 
48*7c478bd9Sstevel@tonic-gate int machinecmp();
49*7c478bd9Sstevel@tonic-gate int loadcmp();
50*7c478bd9Sstevel@tonic-gate int uptimecmp();
51*7c478bd9Sstevel@tonic-gate int collectnames();
52*7c478bd9Sstevel@tonic-gate int singlehost();		/* returns 1 if rup of given host fails */
53*7c478bd9Sstevel@tonic-gate void printsinglehosts();
54*7c478bd9Sstevel@tonic-gate void printnames();
55*7c478bd9Sstevel@tonic-gate 
56*7c478bd9Sstevel@tonic-gate 
57*7c478bd9Sstevel@tonic-gate struct entry {
58*7c478bd9Sstevel@tonic-gate 	struct netconfig *nconf;
59*7c478bd9Sstevel@tonic-gate 	struct netbuf *addr;
60*7c478bd9Sstevel@tonic-gate 	char *machine;
61*7c478bd9Sstevel@tonic-gate 	struct timeval boottime;
62*7c478bd9Sstevel@tonic-gate 	time_t curtime;
63*7c478bd9Sstevel@tonic-gate 	long avenrun[3];
64*7c478bd9Sstevel@tonic-gate };
65*7c478bd9Sstevel@tonic-gate 
66*7c478bd9Sstevel@tonic-gate int total_entries;
67*7c478bd9Sstevel@tonic-gate int curentry;
68*7c478bd9Sstevel@tonic-gate struct entry *entry;
69*7c478bd9Sstevel@tonic-gate int vers;			/* which version did the broadcasting */
70*7c478bd9Sstevel@tonic-gate int lflag;			/* load: sort by load average */
71*7c478bd9Sstevel@tonic-gate int tflag;			/* time: sort by uptime average */
72*7c478bd9Sstevel@tonic-gate int hflag;			/* host: sort by machine name */
73*7c478bd9Sstevel@tonic-gate int dflag;			/* debug: list only first n machines */
74*7c478bd9Sstevel@tonic-gate int debug;
75*7c478bd9Sstevel@tonic-gate 
76*7c478bd9Sstevel@tonic-gate main(argc, argv)
77*7c478bd9Sstevel@tonic-gate 	char **argv;
78*7c478bd9Sstevel@tonic-gate {
79*7c478bd9Sstevel@tonic-gate 	statsvar sv;
80*7c478bd9Sstevel@tonic-gate 	statstime st;
81*7c478bd9Sstevel@tonic-gate 	int single, nfailed;
82*7c478bd9Sstevel@tonic-gate 	enum clnt_stat bstat;
83*7c478bd9Sstevel@tonic-gate 
84*7c478bd9Sstevel@tonic-gate 	/*
85*7c478bd9Sstevel@tonic-gate 	 * set number of slots to be 256 to begin with,
86*7c478bd9Sstevel@tonic-gate 	 * this is large enough for most subnets but not all
87*7c478bd9Sstevel@tonic-gate 	 */
88*7c478bd9Sstevel@tonic-gate 
89*7c478bd9Sstevel@tonic-gate 	curentry = 0;
90*7c478bd9Sstevel@tonic-gate 	total_entries = SLOTS;
91*7c478bd9Sstevel@tonic-gate 	entry = malloc(sizeof (struct entry) * total_entries);
92*7c478bd9Sstevel@tonic-gate 	single = nfailed = 0;
93*7c478bd9Sstevel@tonic-gate 	while (argc > 1) {
94*7c478bd9Sstevel@tonic-gate 		if (argv[1][0] != '-') {
95*7c478bd9Sstevel@tonic-gate 			single++;
96*7c478bd9Sstevel@tonic-gate 			nfailed += singlehost(argv[1]);
97*7c478bd9Sstevel@tonic-gate 		} else {
98*7c478bd9Sstevel@tonic-gate 			switch (argv[1][1]) {
99*7c478bd9Sstevel@tonic-gate 
100*7c478bd9Sstevel@tonic-gate 			case 'l':
101*7c478bd9Sstevel@tonic-gate 				lflag++;
102*7c478bd9Sstevel@tonic-gate 				break;
103*7c478bd9Sstevel@tonic-gate 			case 't':
104*7c478bd9Sstevel@tonic-gate 				tflag++;
105*7c478bd9Sstevel@tonic-gate 				break;
106*7c478bd9Sstevel@tonic-gate 			case 'h':
107*7c478bd9Sstevel@tonic-gate 				hflag++;
108*7c478bd9Sstevel@tonic-gate 				break;
109*7c478bd9Sstevel@tonic-gate 			case 'd':
110*7c478bd9Sstevel@tonic-gate 				dflag++;
111*7c478bd9Sstevel@tonic-gate 				if (argc < 3)
112*7c478bd9Sstevel@tonic-gate 					usage();
113*7c478bd9Sstevel@tonic-gate 				debug = atoi(argv[2]);
114*7c478bd9Sstevel@tonic-gate 				argc--;
115*7c478bd9Sstevel@tonic-gate 				argv++;
116*7c478bd9Sstevel@tonic-gate 				break;
117*7c478bd9Sstevel@tonic-gate 			default:
118*7c478bd9Sstevel@tonic-gate 				usage();
119*7c478bd9Sstevel@tonic-gate 			}
120*7c478bd9Sstevel@tonic-gate 		}
121*7c478bd9Sstevel@tonic-gate 		argv++;
122*7c478bd9Sstevel@tonic-gate 		argc--;
123*7c478bd9Sstevel@tonic-gate 	}
124*7c478bd9Sstevel@tonic-gate 	if (single > 0) {
125*7c478bd9Sstevel@tonic-gate 		if (hflag || tflag || lflag)
126*7c478bd9Sstevel@tonic-gate 			printsinglehosts();
127*7c478bd9Sstevel@tonic-gate 		if (nfailed == single) {
128*7c478bd9Sstevel@tonic-gate 			free(entry);
129*7c478bd9Sstevel@tonic-gate 			exit(1);	/* all hosts we tried failed */
130*7c478bd9Sstevel@tonic-gate 		} else {
131*7c478bd9Sstevel@tonic-gate 			free(entry);
132*7c478bd9Sstevel@tonic-gate 			exit(0);
133*7c478bd9Sstevel@tonic-gate 		}
134*7c478bd9Sstevel@tonic-gate 
135*7c478bd9Sstevel@tonic-gate 	}
136*7c478bd9Sstevel@tonic-gate 	if (hflag || tflag || lflag) {
137*7c478bd9Sstevel@tonic-gate 		printf("collecting responses... ");
138*7c478bd9Sstevel@tonic-gate 		fflush(stdout);
139*7c478bd9Sstevel@tonic-gate 	}
140*7c478bd9Sstevel@tonic-gate 
141*7c478bd9Sstevel@tonic-gate 	sv.cp_time.cp_time_val = (int *)NULL;
142*7c478bd9Sstevel@tonic-gate 	sv.dk_xfer.dk_xfer_val = (int *)NULL;
143*7c478bd9Sstevel@tonic-gate 
144*7c478bd9Sstevel@tonic-gate 	/*
145*7c478bd9Sstevel@tonic-gate 	 * Null out pointers in the statsvar struct
146*7c478bd9Sstevel@tonic-gate 	 * so that we don't follow a random pointer
147*7c478bd9Sstevel@tonic-gate 	 * somewhere when we get our results back.
148*7c478bd9Sstevel@tonic-gate 	 * Set lengths to zero so we don't allocate
149*7c478bd9Sstevel@tonic-gate 	 * some random amount of space we don't need
150*7c478bd9Sstevel@tonic-gate 	 * (in the case where the reply was program
151*7c478bd9Sstevel@tonic-gate 	 *  not registered).
152*7c478bd9Sstevel@tonic-gate 	 */
153*7c478bd9Sstevel@tonic-gate 	sv.cp_time.cp_time_len = 0;
154*7c478bd9Sstevel@tonic-gate 	sv.cp_time.cp_time_val = (int *)NULL;
155*7c478bd9Sstevel@tonic-gate 	sv.dk_xfer.dk_xfer_len = 0;
156*7c478bd9Sstevel@tonic-gate 	sv.dk_xfer.dk_xfer_val = (int *)NULL;
157*7c478bd9Sstevel@tonic-gate 
158*7c478bd9Sstevel@tonic-gate 	vers = RSTATVERS_VAR;
159*7c478bd9Sstevel@tonic-gate 	bstat = rpc_broadcast(RSTATPROG, RSTATVERS_VAR, RSTATPROC_STATS,
160*7c478bd9Sstevel@tonic-gate 			xdr_void, NULL, xdr_statsvar, (caddr_t)&sv,
161*7c478bd9Sstevel@tonic-gate 			(resultproc_t)collectnames, (char *)0);
162*7c478bd9Sstevel@tonic-gate #ifdef TESTING
163*7c478bd9Sstevel@tonic-gate 	if (bstat != RPC_SUCCESS)
164*7c478bd9Sstevel@tonic-gate 		printf("rpc_broadcast for rstat version %d returned %s\n",
165*7c478bd9Sstevel@tonic-gate 			vers, clnt_sperrno(bstat));
166*7c478bd9Sstevel@tonic-gate 	fprintf(stderr, "starting second round of broadcasting\n");
167*7c478bd9Sstevel@tonic-gate #endif
168*7c478bd9Sstevel@tonic-gate 	vers = RSTATVERS_TIME;
169*7c478bd9Sstevel@tonic-gate 	bstat = rpc_broadcast(RSTATPROG, RSTATVERS_TIME, RSTATPROC_STATS,
170*7c478bd9Sstevel@tonic-gate 			xdr_void, NULL, xdr_statstime, (caddr_t)&st,
171*7c478bd9Sstevel@tonic-gate 			(resultproc_t)collectnames, (char *)0);
172*7c478bd9Sstevel@tonic-gate #ifdef	TESTING
173*7c478bd9Sstevel@tonic-gate 	if (bstat != RPC_SUCCESS)
174*7c478bd9Sstevel@tonic-gate 		printf("rpc_broadcast for rstat version %d returned %s\n",
175*7c478bd9Sstevel@tonic-gate 			vers, clnt_sperrno(bstat));
176*7c478bd9Sstevel@tonic-gate #endif
177*7c478bd9Sstevel@tonic-gate 	if (hflag || tflag || lflag)
178*7c478bd9Sstevel@tonic-gate 		printnames();
179*7c478bd9Sstevel@tonic-gate 
180*7c478bd9Sstevel@tonic-gate 
181*7c478bd9Sstevel@tonic-gate 
182*7c478bd9Sstevel@tonic-gate 	free(entry);
183*7c478bd9Sstevel@tonic-gate 	exit(0);
184*7c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
185*7c478bd9Sstevel@tonic-gate }
186*7c478bd9Sstevel@tonic-gate 
187*7c478bd9Sstevel@tonic-gate int
188*7c478bd9Sstevel@tonic-gate singlehost(host)
189*7c478bd9Sstevel@tonic-gate 	char *host;
190*7c478bd9Sstevel@tonic-gate {
191*7c478bd9Sstevel@tonic-gate 	static int debugcnt;
192*7c478bd9Sstevel@tonic-gate 	enum clnt_stat err;
193*7c478bd9Sstevel@tonic-gate 	statstime st;
194*7c478bd9Sstevel@tonic-gate 	statsvar sw_var;
195*7c478bd9Sstevel@tonic-gate 	bool_t is_var_vers = FALSE;
196*7c478bd9Sstevel@tonic-gate 
197*7c478bd9Sstevel@tonic-gate 
198*7c478bd9Sstevel@tonic-gate 	if (curentry >= total_entries) {
199*7c478bd9Sstevel@tonic-gate 		struct entry *tmp;
200*7c478bd9Sstevel@tonic-gate 
201*7c478bd9Sstevel@tonic-gate 		total_entries += SLOTS;
202*7c478bd9Sstevel@tonic-gate 		tmp = realloc((struct entry *)entry, sizeof (struct entry)
203*7c478bd9Sstevel@tonic-gate 						* total_entries);
204*7c478bd9Sstevel@tonic-gate 		if (tmp == NULL) {
205*7c478bd9Sstevel@tonic-gate 			return (1);
206*7c478bd9Sstevel@tonic-gate 		}
207*7c478bd9Sstevel@tonic-gate 		entry = tmp;
208*7c478bd9Sstevel@tonic-gate 	}
209*7c478bd9Sstevel@tonic-gate 
210*7c478bd9Sstevel@tonic-gate 	sw_var.cp_time.cp_time_val = (int *)NULL;
211*7c478bd9Sstevel@tonic-gate 	sw_var.dk_xfer.dk_xfer_val = (int *)NULL;
212*7c478bd9Sstevel@tonic-gate 	err = (enum clnt_stat)callrpc(host, RSTATPROG, RSTATVERS_VAR,
213*7c478bd9Sstevel@tonic-gate 			RSTATPROC_STATS, xdr_void, 0, xdr_statsvar, &sw_var);
214*7c478bd9Sstevel@tonic-gate 	if (err == RPC_SUCCESS) {
215*7c478bd9Sstevel@tonic-gate 		is_var_vers = TRUE;
216*7c478bd9Sstevel@tonic-gate 	} else if (err == RPC_PROGVERSMISMATCH) {
217*7c478bd9Sstevel@tonic-gate 		err = (enum clnt_stat)callrpc(host, RSTATPROG, RSTATVERS_TIME,
218*7c478bd9Sstevel@tonic-gate 			RSTATPROC_STATS, xdr_void, 0, xdr_statstime, &st);
219*7c478bd9Sstevel@tonic-gate 		if (err != RPC_SUCCESS)
220*7c478bd9Sstevel@tonic-gate 			goto error;
221*7c478bd9Sstevel@tonic-gate 	} else
222*7c478bd9Sstevel@tonic-gate 		goto error;
223*7c478bd9Sstevel@tonic-gate 
224*7c478bd9Sstevel@tonic-gate 	debugcnt++;
225*7c478bd9Sstevel@tonic-gate 	if (!hflag && !lflag && !tflag) {
226*7c478bd9Sstevel@tonic-gate 		printf("%*.*s  ", MACHINELEN, MACHINELEN, host);
227*7c478bd9Sstevel@tonic-gate 		if (is_var_vers == TRUE)
228*7c478bd9Sstevel@tonic-gate 			putline(sw_var.curtime.tv_sec, sw_var.boottime,
229*7c478bd9Sstevel@tonic-gate 				sw_var.avenrun);
230*7c478bd9Sstevel@tonic-gate 		else
231*7c478bd9Sstevel@tonic-gate 			putline(st.curtime.tv_sec, st.boottime, st.avenrun);
232*7c478bd9Sstevel@tonic-gate 		return (0);		/* success */
233*7c478bd9Sstevel@tonic-gate 	} else {
234*7c478bd9Sstevel@tonic-gate 		entry[curentry].machine = host;
235*7c478bd9Sstevel@tonic-gate 		if (is_var_vers == FALSE) { /* RSTATVERS_TIME */
236*7c478bd9Sstevel@tonic-gate 			entry[curentry].boottime.tv_sec = st.boottime.tv_sec;
237*7c478bd9Sstevel@tonic-gate 			entry[curentry].boottime.tv_usec =
238*7c478bd9Sstevel@tonic-gate 				st.boottime.tv_usec;
239*7c478bd9Sstevel@tonic-gate 			entry[curentry].curtime = st.curtime.tv_sec;
240*7c478bd9Sstevel@tonic-gate 			memcpy(entry[curentry].avenrun, st.avenrun, AVENSIZE);
241*7c478bd9Sstevel@tonic-gate 		} else { /* RSTATVERS_VAR */
242*7c478bd9Sstevel@tonic-gate 			entry[curentry].boottime.tv_sec =
243*7c478bd9Sstevel@tonic-gate 				sw_var.boottime.tv_sec;
244*7c478bd9Sstevel@tonic-gate 			entry[curentry].boottime.tv_usec =
245*7c478bd9Sstevel@tonic-gate 				sw_var.boottime.tv_usec;
246*7c478bd9Sstevel@tonic-gate 			entry[curentry].curtime = sw_var.curtime.tv_sec;
247*7c478bd9Sstevel@tonic-gate 			memcpy(entry[curentry].avenrun, sw_var.avenrun,
248*7c478bd9Sstevel@tonic-gate 							AVENSIZE);
249*7c478bd9Sstevel@tonic-gate 		}
250*7c478bd9Sstevel@tonic-gate 	}
251*7c478bd9Sstevel@tonic-gate 	curentry++;
252*7c478bd9Sstevel@tonic-gate 	if (dflag && debugcnt >= debug)
253*7c478bd9Sstevel@tonic-gate 		return (1);
254*7c478bd9Sstevel@tonic-gate 	return (0);
255*7c478bd9Sstevel@tonic-gate 
256*7c478bd9Sstevel@tonic-gate error:
257*7c478bd9Sstevel@tonic-gate 	fprintf(stderr, "%*.*s: ", MACHINELEN, MACHINELEN, host);
258*7c478bd9Sstevel@tonic-gate 	clnt_perrno(err);
259*7c478bd9Sstevel@tonic-gate 	/*
260*7c478bd9Sstevel@tonic-gate 	 * clnt_perrno now prints a newline
261*7c478bd9Sstevel@tonic-gate 	 */
262*7c478bd9Sstevel@tonic-gate 	/* fprintf(stderr, "\n"); */
263*7c478bd9Sstevel@tonic-gate 	return (1);		/* a failure */
264*7c478bd9Sstevel@tonic-gate }
265*7c478bd9Sstevel@tonic-gate 
266*7c478bd9Sstevel@tonic-gate putline(now, boottime, avenrun)
267*7c478bd9Sstevel@tonic-gate 	time_t now;
268*7c478bd9Sstevel@tonic-gate 	struct timeval boottime;
269*7c478bd9Sstevel@tonic-gate 	long avenrun[];
270*7c478bd9Sstevel@tonic-gate {
271*7c478bd9Sstevel@tonic-gate 	int uptime, days, hrs, mins, i;
272*7c478bd9Sstevel@tonic-gate 
273*7c478bd9Sstevel@tonic-gate 	uptime = now - boottime.tv_sec;
274*7c478bd9Sstevel@tonic-gate 	uptime += 30;
275*7c478bd9Sstevel@tonic-gate 	if (uptime < 0)		/* unsynchronized clocks */
276*7c478bd9Sstevel@tonic-gate 		uptime = 0;
277*7c478bd9Sstevel@tonic-gate 	days = uptime / (60*60*24);
278*7c478bd9Sstevel@tonic-gate 	uptime %= (60*60*24);
279*7c478bd9Sstevel@tonic-gate 	hrs = uptime / (60*60);
280*7c478bd9Sstevel@tonic-gate 	uptime %= (60*60);
281*7c478bd9Sstevel@tonic-gate 	mins = uptime / 60;
282*7c478bd9Sstevel@tonic-gate 
283*7c478bd9Sstevel@tonic-gate 	printf("  up");
284*7c478bd9Sstevel@tonic-gate 	if (days > 0)
285*7c478bd9Sstevel@tonic-gate 		printf(" %2d day%s", days, days > 1 ? "s," : ", ");
286*7c478bd9Sstevel@tonic-gate 	else
287*7c478bd9Sstevel@tonic-gate 		printf("         ");
288*7c478bd9Sstevel@tonic-gate 	if (hrs > 0)
289*7c478bd9Sstevel@tonic-gate 		printf(" %2d:%02d,  ", hrs, mins);
290*7c478bd9Sstevel@tonic-gate 	else
291*7c478bd9Sstevel@tonic-gate 		printf(" %2d min%s", mins, mins > 1 ? "s," : ", ");
292*7c478bd9Sstevel@tonic-gate 
293*7c478bd9Sstevel@tonic-gate 	/*
294*7c478bd9Sstevel@tonic-gate 	 * Print 1, 5, and 15 minute load averages.
295*7c478bd9Sstevel@tonic-gate 	 * (Found by looking in kernel for avenrun).
296*7c478bd9Sstevel@tonic-gate 	 */
297*7c478bd9Sstevel@tonic-gate 	printf("  load average:");
298*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < (AVENSIZE / sizeof (avenrun[0])); i++) {
299*7c478bd9Sstevel@tonic-gate 		if (i > 0)
300*7c478bd9Sstevel@tonic-gate 			printf(",");
301*7c478bd9Sstevel@tonic-gate 		printf(" %.2f", (double)avenrun[i]/FSCALE);
302*7c478bd9Sstevel@tonic-gate 	}
303*7c478bd9Sstevel@tonic-gate 	printf("\n");
304*7c478bd9Sstevel@tonic-gate }
305*7c478bd9Sstevel@tonic-gate 
306*7c478bd9Sstevel@tonic-gate collectnames(resultsp, taddr, nconf)
307*7c478bd9Sstevel@tonic-gate 	char *resultsp;
308*7c478bd9Sstevel@tonic-gate 	struct t_bind	*taddr;
309*7c478bd9Sstevel@tonic-gate 	struct netconfig	*nconf;
310*7c478bd9Sstevel@tonic-gate {
311*7c478bd9Sstevel@tonic-gate 	static int debugcnt;
312*7c478bd9Sstevel@tonic-gate 	register struct entry *entryp, *lim;
313*7c478bd9Sstevel@tonic-gate 	statstime *st;
314*7c478bd9Sstevel@tonic-gate 	statsvar *sv;
315*7c478bd9Sstevel@tonic-gate 	struct nd_hostservlist *hs;
316*7c478bd9Sstevel@tonic-gate 	extern struct netbuf *netbufdup();
317*7c478bd9Sstevel@tonic-gate 	extern struct netconfig *netconfigdup();
318*7c478bd9Sstevel@tonic-gate 	extern int netbufeq();
319*7c478bd9Sstevel@tonic-gate 
320*7c478bd9Sstevel@tonic-gate 	/*
321*7c478bd9Sstevel@tonic-gate 	 * need to realloc more space if we have more than 256 machines
322*7c478bd9Sstevel@tonic-gate 	 * that responded to the broadcast
323*7c478bd9Sstevel@tonic-gate 	 */
324*7c478bd9Sstevel@tonic-gate 
325*7c478bd9Sstevel@tonic-gate 	if (curentry >= total_entries) {
326*7c478bd9Sstevel@tonic-gate 		struct entry *tmp;
327*7c478bd9Sstevel@tonic-gate 
328*7c478bd9Sstevel@tonic-gate 		total_entries += SLOTS;
329*7c478bd9Sstevel@tonic-gate 		tmp = realloc((struct entry *)entry, sizeof (struct entry)
330*7c478bd9Sstevel@tonic-gate 						* total_entries);
331*7c478bd9Sstevel@tonic-gate 		if (tmp == NULL) {
332*7c478bd9Sstevel@tonic-gate 			return (1);
333*7c478bd9Sstevel@tonic-gate 		}
334*7c478bd9Sstevel@tonic-gate 		entry = tmp;
335*7c478bd9Sstevel@tonic-gate 	}
336*7c478bd9Sstevel@tonic-gate 	/*
337*7c478bd9Sstevel@tonic-gate 	 * weed out duplicates
338*7c478bd9Sstevel@tonic-gate 	 */
339*7c478bd9Sstevel@tonic-gate 	lim = entry + curentry;
340*7c478bd9Sstevel@tonic-gate 	for (entryp = entry; entryp < lim; entryp++)
341*7c478bd9Sstevel@tonic-gate 		if (netbufeq(&taddr->addr, entryp->addr))
342*7c478bd9Sstevel@tonic-gate 			return (0);
343*7c478bd9Sstevel@tonic-gate 
344*7c478bd9Sstevel@tonic-gate 	if (vers == RSTATVERS_TIME) {
345*7c478bd9Sstevel@tonic-gate 		st = (statstime *)resultsp;
346*7c478bd9Sstevel@tonic-gate 	} else if (vers == RSTATVERS_VAR) {
347*7c478bd9Sstevel@tonic-gate 		sv = (statsvar *)resultsp;
348*7c478bd9Sstevel@tonic-gate 	} else {
349*7c478bd9Sstevel@tonic-gate 		return (0);	/* we don't handle this version */
350*7c478bd9Sstevel@tonic-gate 	}
351*7c478bd9Sstevel@tonic-gate 	debugcnt++;
352*7c478bd9Sstevel@tonic-gate 	entry[curentry].nconf = netconfigdup(nconf);
353*7c478bd9Sstevel@tonic-gate 	entry[curentry].addr = netbufdup(&taddr->addr);
354*7c478bd9Sstevel@tonic-gate 
355*7c478bd9Sstevel@tonic-gate 	/*
356*7c478bd9Sstevel@tonic-gate 	 * if raw, print this entry out immediately
357*7c478bd9Sstevel@tonic-gate 	 * otherwise store for later sorting
358*7c478bd9Sstevel@tonic-gate 	 */
359*7c478bd9Sstevel@tonic-gate 	if (!hflag && !lflag && !tflag) {
360*7c478bd9Sstevel@tonic-gate 		if (netdir_getbyaddr(nconf, &hs, &taddr->addr) == ND_OK)
361*7c478bd9Sstevel@tonic-gate 			printf("%*.*s  ", MACHINELEN, MACHINELEN,
362*7c478bd9Sstevel@tonic-gate 				hs->h_hostservs->h_host);
363*7c478bd9Sstevel@tonic-gate 		else {
364*7c478bd9Sstevel@tonic-gate 			char *uaddr = taddr2uaddr(nconf, &taddr->addr);
365*7c478bd9Sstevel@tonic-gate 
366*7c478bd9Sstevel@tonic-gate 			if (uaddr) {
367*7c478bd9Sstevel@tonic-gate 				printf("  %*.*s", MACHINELEN, MACHINELEN,
368*7c478bd9Sstevel@tonic-gate 					uaddr);
369*7c478bd9Sstevel@tonic-gate 				(void) free(uaddr);
370*7c478bd9Sstevel@tonic-gate 			} else
371*7c478bd9Sstevel@tonic-gate 				printf("  %*.*s", MACHINELEN, MACHINELEN,
372*7c478bd9Sstevel@tonic-gate 					"unknown");
373*7c478bd9Sstevel@tonic-gate 		}
374*7c478bd9Sstevel@tonic-gate 		if (vers == RSTATVERS_TIME) {
375*7c478bd9Sstevel@tonic-gate 			putline(st->curtime.tv_sec, st->boottime, st->avenrun);
376*7c478bd9Sstevel@tonic-gate 		} else if (vers == RSTATVERS_VAR) {
377*7c478bd9Sstevel@tonic-gate 			putline(sv->curtime.tv_sec, sv->boottime, sv->avenrun);
378*7c478bd9Sstevel@tonic-gate 		}
379*7c478bd9Sstevel@tonic-gate 	} else {
380*7c478bd9Sstevel@tonic-gate 		if (vers == RSTATVERS_TIME) {
381*7c478bd9Sstevel@tonic-gate 			entry[curentry].boottime.tv_sec = st->boottime.tv_sec;
382*7c478bd9Sstevel@tonic-gate 			entry[curentry].boottime.tv_usec =
383*7c478bd9Sstevel@tonic-gate 				st->boottime.tv_usec;
384*7c478bd9Sstevel@tonic-gate 			entry[curentry].curtime = st->curtime.tv_sec;
385*7c478bd9Sstevel@tonic-gate 			memcpy(entry[curentry].avenrun, st->avenrun, AVENSIZE);
386*7c478bd9Sstevel@tonic-gate 		} else if (vers == RSTATVERS_VAR) {
387*7c478bd9Sstevel@tonic-gate 			entry[curentry].boottime.tv_sec = sv->boottime.tv_sec;
388*7c478bd9Sstevel@tonic-gate 			entry[curentry].boottime.tv_usec =
389*7c478bd9Sstevel@tonic-gate 				sv->boottime.tv_usec;
390*7c478bd9Sstevel@tonic-gate 			entry[curentry].curtime = sv->curtime.tv_sec;
391*7c478bd9Sstevel@tonic-gate 			memcpy(entry[curentry].avenrun, sv->avenrun, AVENSIZE);
392*7c478bd9Sstevel@tonic-gate 		}
393*7c478bd9Sstevel@tonic-gate 	}
394*7c478bd9Sstevel@tonic-gate 	curentry++;
395*7c478bd9Sstevel@tonic-gate 	if (dflag && debugcnt >= debug)
396*7c478bd9Sstevel@tonic-gate 		return (1);
397*7c478bd9Sstevel@tonic-gate 	return (0);
398*7c478bd9Sstevel@tonic-gate }
399*7c478bd9Sstevel@tonic-gate 
400*7c478bd9Sstevel@tonic-gate void
401*7c478bd9Sstevel@tonic-gate printsinglehosts()
402*7c478bd9Sstevel@tonic-gate {
403*7c478bd9Sstevel@tonic-gate 	register int i;
404*7c478bd9Sstevel@tonic-gate 	register struct entry *ep;
405*7c478bd9Sstevel@tonic-gate 
406*7c478bd9Sstevel@tonic-gate 
407*7c478bd9Sstevel@tonic-gate 	if (hflag)
408*7c478bd9Sstevel@tonic-gate 		qsort(entry, curentry, sizeof (struct entry), machinecmp);
409*7c478bd9Sstevel@tonic-gate 	else if (lflag)
410*7c478bd9Sstevel@tonic-gate 		qsort(entry, curentry, sizeof (struct entry), loadcmp);
411*7c478bd9Sstevel@tonic-gate 	else
412*7c478bd9Sstevel@tonic-gate 		qsort(entry, curentry, sizeof (struct entry), uptimecmp);
413*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < curentry; i++) {
414*7c478bd9Sstevel@tonic-gate 		ep = &entry[i];
415*7c478bd9Sstevel@tonic-gate 		printf("%*.*s  ", MACHINELEN, MACHINELEN, ep->machine);
416*7c478bd9Sstevel@tonic-gate 		putline(ep->curtime, ep->boottime, ep->avenrun);
417*7c478bd9Sstevel@tonic-gate 
418*7c478bd9Sstevel@tonic-gate 	}
419*7c478bd9Sstevel@tonic-gate }
420*7c478bd9Sstevel@tonic-gate 
421*7c478bd9Sstevel@tonic-gate void
422*7c478bd9Sstevel@tonic-gate printnames()
423*7c478bd9Sstevel@tonic-gate {
424*7c478bd9Sstevel@tonic-gate 	char buf[MACHINELENMAX+1];
425*7c478bd9Sstevel@tonic-gate 	struct nd_hostservlist *hs;
426*7c478bd9Sstevel@tonic-gate 	register int i;
427*7c478bd9Sstevel@tonic-gate 	register struct entry *ep;
428*7c478bd9Sstevel@tonic-gate 
429*7c478bd9Sstevel@tonic-gate 
430*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < curentry; i++) {
431*7c478bd9Sstevel@tonic-gate 		ep = &entry[i];
432*7c478bd9Sstevel@tonic-gate 		if (netdir_getbyaddr(ep->nconf, &hs, ep->addr) == ND_OK)
433*7c478bd9Sstevel@tonic-gate 			sprintf(buf, "%s", hs->h_hostservs->h_host);
434*7c478bd9Sstevel@tonic-gate 		else {
435*7c478bd9Sstevel@tonic-gate 			char *uaddr = taddr2uaddr(ep->nconf, ep->addr);
436*7c478bd9Sstevel@tonic-gate 
437*7c478bd9Sstevel@tonic-gate 			if (uaddr) {
438*7c478bd9Sstevel@tonic-gate 				sprintf(buf, "%s", uaddr);
439*7c478bd9Sstevel@tonic-gate 				(void) free(uaddr);
440*7c478bd9Sstevel@tonic-gate 			} else
441*7c478bd9Sstevel@tonic-gate 				sprintf(buf, "%s", "unknown");
442*7c478bd9Sstevel@tonic-gate 		}
443*7c478bd9Sstevel@tonic-gate 		if (ep->machine = (char *)malloc(MACHINELENMAX + 1))
444*7c478bd9Sstevel@tonic-gate 			strcpy(ep->machine, buf);
445*7c478bd9Sstevel@tonic-gate 	}
446*7c478bd9Sstevel@tonic-gate 	printf("\n");
447*7c478bd9Sstevel@tonic-gate 	printsinglehosts();
448*7c478bd9Sstevel@tonic-gate }
449*7c478bd9Sstevel@tonic-gate 
450*7c478bd9Sstevel@tonic-gate machinecmp(a, b)
451*7c478bd9Sstevel@tonic-gate 	struct entry *a, *b;
452*7c478bd9Sstevel@tonic-gate {
453*7c478bd9Sstevel@tonic-gate 	return (strcmp(a->machine, b->machine));
454*7c478bd9Sstevel@tonic-gate }
455*7c478bd9Sstevel@tonic-gate 
456*7c478bd9Sstevel@tonic-gate uptimecmp(a, b)
457*7c478bd9Sstevel@tonic-gate 	struct entry *a, *b;
458*7c478bd9Sstevel@tonic-gate {
459*7c478bd9Sstevel@tonic-gate 	if (a->boottime.tv_sec != b->boottime.tv_sec)
460*7c478bd9Sstevel@tonic-gate 		return (a->boottime.tv_sec - b->boottime.tv_sec);
461*7c478bd9Sstevel@tonic-gate 	else
462*7c478bd9Sstevel@tonic-gate 		return (a->boottime.tv_usec - b->boottime.tv_usec);
463*7c478bd9Sstevel@tonic-gate }
464*7c478bd9Sstevel@tonic-gate 
465*7c478bd9Sstevel@tonic-gate loadcmp(a, b)
466*7c478bd9Sstevel@tonic-gate 	struct entry *a, *b;
467*7c478bd9Sstevel@tonic-gate {
468*7c478bd9Sstevel@tonic-gate 	register int i;
469*7c478bd9Sstevel@tonic-gate 
470*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < AVENSIZE / sizeof (a->avenrun[0]); i++)
471*7c478bd9Sstevel@tonic-gate 		if (a->avenrun[i] != b->avenrun[i])
472*7c478bd9Sstevel@tonic-gate 			return (a->avenrun[i] - b->avenrun[i]);
473*7c478bd9Sstevel@tonic-gate 
474*7c478bd9Sstevel@tonic-gate 	return (0);
475*7c478bd9Sstevel@tonic-gate }
476*7c478bd9Sstevel@tonic-gate 
477*7c478bd9Sstevel@tonic-gate struct netbuf *
478*7c478bd9Sstevel@tonic-gate netbufdup(ap)
479*7c478bd9Sstevel@tonic-gate 	register struct netbuf	*ap;
480*7c478bd9Sstevel@tonic-gate {
481*7c478bd9Sstevel@tonic-gate 	register struct netbuf	*np;
482*7c478bd9Sstevel@tonic-gate 
483*7c478bd9Sstevel@tonic-gate 	np = (struct netbuf *) malloc(sizeof (struct netbuf) + ap->len);
484*7c478bd9Sstevel@tonic-gate 	if (np) {
485*7c478bd9Sstevel@tonic-gate 		np->maxlen = np->len = ap->len;
486*7c478bd9Sstevel@tonic-gate 		np->buf = ((char *)np) + sizeof (struct netbuf);
487*7c478bd9Sstevel@tonic-gate 		(void) memcpy(np->buf, ap->buf, ap->len);
488*7c478bd9Sstevel@tonic-gate 	}
489*7c478bd9Sstevel@tonic-gate 	return (np);
490*7c478bd9Sstevel@tonic-gate }
491*7c478bd9Sstevel@tonic-gate 
492*7c478bd9Sstevel@tonic-gate struct netconfig *
493*7c478bd9Sstevel@tonic-gate netconfigdup(onp)
494*7c478bd9Sstevel@tonic-gate 	register struct netconfig *onp;
495*7c478bd9Sstevel@tonic-gate {
496*7c478bd9Sstevel@tonic-gate 	register int nlookupdirs;
497*7c478bd9Sstevel@tonic-gate 	register struct netconfig *nnp;
498*7c478bd9Sstevel@tonic-gate 	extern char *strdup();
499*7c478bd9Sstevel@tonic-gate 
500*7c478bd9Sstevel@tonic-gate 	nnp = (struct netconfig *)malloc(sizeof (struct netconfig));
501*7c478bd9Sstevel@tonic-gate 	if (nnp) {
502*7c478bd9Sstevel@tonic-gate 		nnp->nc_netid = strdup(onp->nc_netid);
503*7c478bd9Sstevel@tonic-gate 		nnp->nc_semantics = onp->nc_semantics;
504*7c478bd9Sstevel@tonic-gate 		nnp->nc_flag = onp->nc_flag;
505*7c478bd9Sstevel@tonic-gate 		nnp->nc_protofmly = strdup(onp->nc_protofmly);
506*7c478bd9Sstevel@tonic-gate 		nnp->nc_proto = strdup(onp->nc_proto);
507*7c478bd9Sstevel@tonic-gate 		nnp->nc_device = strdup(onp->nc_device);
508*7c478bd9Sstevel@tonic-gate 		nnp->nc_nlookups = onp->nc_nlookups;
509*7c478bd9Sstevel@tonic-gate 		if (onp->nc_nlookups == 0)
510*7c478bd9Sstevel@tonic-gate 			nnp->nc_lookups = (char **)0;
511*7c478bd9Sstevel@tonic-gate 		else {
512*7c478bd9Sstevel@tonic-gate 			register int i;
513*7c478bd9Sstevel@tonic-gate 
514*7c478bd9Sstevel@tonic-gate 			nnp->nc_lookups = (char **)malloc(onp->nc_nlookups *
515*7c478bd9Sstevel@tonic-gate 			    sizeof (char *));
516*7c478bd9Sstevel@tonic-gate 			if (nnp->nc_lookups)
517*7c478bd9Sstevel@tonic-gate 				for (i = 0; i < onp->nc_nlookups; i++)
518*7c478bd9Sstevel@tonic-gate 					nnp->nc_lookups[i] =
519*7c478bd9Sstevel@tonic-gate 						strdup(onp->nc_lookups[i]);
520*7c478bd9Sstevel@tonic-gate 		}
521*7c478bd9Sstevel@tonic-gate 	}
522*7c478bd9Sstevel@tonic-gate 
523*7c478bd9Sstevel@tonic-gate 	return (nnp);
524*7c478bd9Sstevel@tonic-gate }
525*7c478bd9Sstevel@tonic-gate 
526*7c478bd9Sstevel@tonic-gate netbufeq(ap, bp)
527*7c478bd9Sstevel@tonic-gate 	register struct netbuf *ap, *bp;
528*7c478bd9Sstevel@tonic-gate {
529*7c478bd9Sstevel@tonic-gate 	return (ap->len == bp->len && !memcmp(ap->buf, bp->buf, ap->len));
530*7c478bd9Sstevel@tonic-gate }
531*7c478bd9Sstevel@tonic-gate 
532*7c478bd9Sstevel@tonic-gate usage()
533*7c478bd9Sstevel@tonic-gate {
534*7c478bd9Sstevel@tonic-gate 	fprintf(stderr, "Usage: rup [-h] [-l] [-t] [host ...]\n");
535*7c478bd9Sstevel@tonic-gate 	free(entry);
536*7c478bd9Sstevel@tonic-gate 	exit(1);
537*7c478bd9Sstevel@tonic-gate }
538