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