| 140 |
|
++num_stats; |
| 141 |
|
} |
| 142 |
|
|
| 143 |
< |
/* Compare two stats by name, for sorting purposes. */ |
| 143 |
> |
/* Compare two stats by name for qsort and bsearch. */ |
| 144 |
|
int stats_compare(const void *a, const void *b) { |
| 145 |
|
return strcmp(((stat *)a)->name, ((stat *)b)->name); |
| 146 |
|
} |
| 147 |
|
|
| 148 |
+ |
/* Compare up to the length of the key for bsearch. */ |
| 149 |
+ |
int stats_compare_prefix(const void *key, const void *item) { |
| 150 |
+ |
const char *kn = ((stat *)key)->name; |
| 151 |
+ |
const char *in = ((stat *)item)->name; |
| 152 |
+ |
|
| 153 |
+ |
return strncmp(kn, in, strlen(kn)); |
| 154 |
+ |
} |
| 155 |
+ |
|
| 156 |
|
void populate_const() { |
| 157 |
|
static int zero = 0; |
| 158 |
|
|
| 268 |
|
if (disk != NULL) { |
| 269 |
|
for (i = 0; i < n; i++) { |
| 270 |
|
/* FIXME it'd be nicer if libstatgrab did this */ |
| 271 |
< |
const char *name = disk[i].device_name, |
| 272 |
< |
*p = strrchr(name, '/'); |
| 273 |
< |
if (p != NULL) |
| 274 |
< |
name = p + 1; |
| 275 |
< |
if (*name == '\0') |
| 276 |
< |
name = "root"; |
| 277 |
< |
|
| 271 |
> |
char *buf, *name, *p; |
| 272 |
> |
const char *device = disk[i].device_name; |
| 273 |
> |
|
| 274 |
> |
if (strcmp(device, "/") == 0) |
| 275 |
> |
device = "root"; |
| 276 |
> |
|
| 277 |
> |
buf = strdup(device); |
| 278 |
> |
if (buf == NULL) |
| 279 |
> |
die("out of memory"); |
| 280 |
> |
|
| 281 |
> |
name = buf; |
| 282 |
> |
if (strlen(name) == 2 && name[1] == ':') |
| 283 |
> |
name[1] = '\0'; |
| 284 |
> |
if (strncmp(name, "/dev/", 5) == 0) |
| 285 |
> |
name += 5; |
| 286 |
> |
while ((p = strchr(name, '/')) != NULL) |
| 287 |
> |
*p = '_'; |
| 288 |
> |
|
| 289 |
|
add_stat(STRING, &disk[i].device_name, |
| 290 |
|
"fs", name, "device_name", NULL); |
| 291 |
|
add_stat(STRING, &disk[i].fs_type, |
| 304 |
|
"fs", name, "used_inodes", NULL); |
| 305 |
|
add_stat(LONG_LONG, &disk[i].free_inodes, |
| 306 |
|
"fs", name, "free_inodes", NULL); |
| 307 |
+ |
|
| 308 |
+ |
free(buf); |
| 309 |
|
} |
| 310 |
|
} |
| 311 |
|
} |
| 371 |
|
if (page != NULL) { |
| 372 |
|
add_stat(LONG_LONG, &page->pages_pagein, "page", "in", NULL); |
| 373 |
|
add_stat(LONG_LONG, &page->pages_pageout, "page", "out", NULL); |
| 374 |
< |
add_stat(LONG_LONG, &page->systime, "page", "systime", NULL); |
| 374 |
> |
add_stat(TIME_T, &page->systime, "page", "systime", NULL); |
| 375 |
|
} |
| 376 |
|
} |
| 377 |
|
|
| 489 |
|
} else { |
| 490 |
|
/* Print selected stats. */ |
| 491 |
|
for (i = optind; i < argc; i++) { |
| 492 |
+ |
char *name = argv[i]; |
| 493 |
|
stat key; |
| 494 |
< |
const stat *s; |
| 494 |
> |
const stat *s, *end; |
| 495 |
> |
int (*compare)(const void *, const void *); |
| 496 |
|
|
| 497 |
< |
key.name = argv[i]; |
| 497 |
> |
key.name = name; |
| 498 |
> |
if (name[strlen(name) - 1] == '.') |
| 499 |
> |
compare = stats_compare_prefix; |
| 500 |
> |
else |
| 501 |
> |
compare = stats_compare; |
| 502 |
> |
|
| 503 |
|
s = (const stat *)bsearch(&key, stats, num_stats, |
| 504 |
< |
sizeof *stats, |
| 505 |
< |
stats_compare); |
| 506 |
< |
if (s != NULL) { |
| 504 |
> |
sizeof *stats, compare); |
| 505 |
> |
if (s == NULL) { |
| 506 |
> |
printf("Unknown stat %s\n", name); |
| 507 |
> |
continue; |
| 508 |
> |
} |
| 509 |
> |
|
| 510 |
> |
/* Find the range of stats the user wanted. */ |
| 511 |
> |
for (; s >= stats; s--) { |
| 512 |
> |
if (compare(&key, s) != 0) |
| 513 |
> |
break; |
| 514 |
> |
} |
| 515 |
> |
s++; |
| 516 |
> |
for (end = s; end < &stats[num_stats]; end++) { |
| 517 |
> |
if (compare(&key, end) != 0) |
| 518 |
> |
break; |
| 519 |
> |
} |
| 520 |
> |
|
| 521 |
> |
/* And print them. */ |
| 522 |
> |
for (; s < end; s++) { |
| 523 |
|
print_stat(s); |
| 524 |
|
} |
| 525 |
|
} |
| 528 |
|
|
| 529 |
|
void usage() { |
| 530 |
|
printf("Usage: statgrab [OPTION]... [STAT]...\n" |
| 531 |
< |
"Display system statistics (all statistics by default).\n" |
| 531 |
> |
"Display system statistics.\n" |
| 532 |
> |
"\n" |
| 533 |
> |
"If no STATs are given, all will be displayed. Specify 'STAT.' to display all\n" |
| 534 |
> |
"statistics starting with that prefix.\n" |
| 535 |
|
"\n"); |
| 536 |
|
printf(" -l Linux sysctl-style output (default)\n" |
| 537 |
|
" -b BSD sysctl-style output\n" |
| 604 |
|
use_diffs = 1; |
| 605 |
|
|
| 606 |
|
select_interesting(argc - optind, &argv[optind]); |
| 607 |
+ |
|
| 608 |
+ |
/* We don't care if statgrab_init fails, because we can just display |
| 609 |
+ |
the statistics that can be read as non-root. */ |
| 610 |
+ |
statgrab_init(); |
| 611 |
+ |
#ifdef ALLBSD |
| 612 |
+ |
if (setegid(getgid()) != 0) |
| 613 |
+ |
die("Failed to lose effective group"); |
| 614 |
+ |
#endif |
| 615 |
+ |
#ifdef SOLARIS |
| 616 |
+ |
if (seteuid(getuid()) != 0) |
| 617 |
+ |
die("Failed to lose effective user"); |
| 618 |
+ |
#endif |
| 619 |
|
|
| 620 |
|
switch (repeat_mode) { |
| 621 |
|
case REPEAT_NONE: |