1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * CDDL HEADER START 3*0Sstevel@tonic-gate * 4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*0Sstevel@tonic-gate * with the License. 8*0Sstevel@tonic-gate * 9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*0Sstevel@tonic-gate * See the License for the specific language governing permissions 12*0Sstevel@tonic-gate * and limitations under the License. 13*0Sstevel@tonic-gate * 14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*0Sstevel@tonic-gate * 20*0Sstevel@tonic-gate * CDDL HEADER END 21*0Sstevel@tonic-gate */ 22*0Sstevel@tonic-gate /* 23*0Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 24*0Sstevel@tonic-gate * Use is subject to license terms. 25*0Sstevel@tonic-gate */ 26*0Sstevel@tonic-gate 27*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 28*0Sstevel@tonic-gate 29*0Sstevel@tonic-gate #include <sys/types.h> 30*0Sstevel@tonic-gate #include <sys/ctfs.h> 31*0Sstevel@tonic-gate #include <stdio.h> 32*0Sstevel@tonic-gate #include <stdlib.h> 33*0Sstevel@tonic-gate #include <unistd.h> 34*0Sstevel@tonic-gate #include <fcntl.h> 35*0Sstevel@tonic-gate #include <string.h> 36*0Sstevel@tonic-gate #include <errno.h> 37*0Sstevel@tonic-gate #include <libuutil.h> 38*0Sstevel@tonic-gate #include <sys/contract/process.h> 39*0Sstevel@tonic-gate #include <limits.h> 40*0Sstevel@tonic-gate #include <libcontract.h> 41*0Sstevel@tonic-gate #include <libcontract_priv.h> 42*0Sstevel@tonic-gate #include <dirent.h> 43*0Sstevel@tonic-gate 44*0Sstevel@tonic-gate #include <locale.h> 45*0Sstevel@tonic-gate #include <langinfo.h> 46*0Sstevel@tonic-gate 47*0Sstevel@tonic-gate static int opt_verbose = 0; 48*0Sstevel@tonic-gate static int opt_showall = 0; 49*0Sstevel@tonic-gate 50*0Sstevel@tonic-gate /* 51*0Sstevel@tonic-gate * usage 52*0Sstevel@tonic-gate * 53*0Sstevel@tonic-gate * Educate the user. 54*0Sstevel@tonic-gate */ 55*0Sstevel@tonic-gate static void 56*0Sstevel@tonic-gate usage(void) 57*0Sstevel@tonic-gate { 58*0Sstevel@tonic-gate (void) fprintf(stderr, gettext("Usage: %s [-a] [-i ctidlist] " 59*0Sstevel@tonic-gate "[-t typelist] [-v] [interval [count]]\n"), uu_getpname()); 60*0Sstevel@tonic-gate exit(UU_EXIT_USAGE); 61*0Sstevel@tonic-gate } 62*0Sstevel@tonic-gate 63*0Sstevel@tonic-gate /* 64*0Sstevel@tonic-gate * mystrtoul 65*0Sstevel@tonic-gate * 66*0Sstevel@tonic-gate * Convert a string into an int in [0, INT_MAX]. Exit if the argument 67*0Sstevel@tonic-gate * doen't fit this description. 68*0Sstevel@tonic-gate */ 69*0Sstevel@tonic-gate static int 70*0Sstevel@tonic-gate mystrtoul(const char *arg) 71*0Sstevel@tonic-gate { 72*0Sstevel@tonic-gate unsigned int result; 73*0Sstevel@tonic-gate 74*0Sstevel@tonic-gate if (uu_strtoint(arg, &result, sizeof (result), 10, 0, INT_MAX) == -1) { 75*0Sstevel@tonic-gate uu_warn(gettext("invalid numerical argument \"%s\"\n"), arg); 76*0Sstevel@tonic-gate usage(); 77*0Sstevel@tonic-gate } 78*0Sstevel@tonic-gate 79*0Sstevel@tonic-gate return (result); 80*0Sstevel@tonic-gate } 81*0Sstevel@tonic-gate 82*0Sstevel@tonic-gate /* 83*0Sstevel@tonic-gate * int_compar 84*0Sstevel@tonic-gate * 85*0Sstevel@tonic-gate * A simple integer comparator. Also used for id_ts, since they're the 86*0Sstevel@tonic-gate * same thing. 87*0Sstevel@tonic-gate */ 88*0Sstevel@tonic-gate static int 89*0Sstevel@tonic-gate int_compar(const void *a1, const void *a2) 90*0Sstevel@tonic-gate { 91*0Sstevel@tonic-gate int id1 = *(int *)a1; 92*0Sstevel@tonic-gate int id2 = *(int *)a2; 93*0Sstevel@tonic-gate 94*0Sstevel@tonic-gate if (id1 > id2) 95*0Sstevel@tonic-gate return (1); 96*0Sstevel@tonic-gate if (id2 > id1) 97*0Sstevel@tonic-gate return (-1); 98*0Sstevel@tonic-gate return (0); 99*0Sstevel@tonic-gate } 100*0Sstevel@tonic-gate 101*0Sstevel@tonic-gate typedef struct optvect { 102*0Sstevel@tonic-gate const char *option; 103*0Sstevel@tonic-gate uint_t bit; 104*0Sstevel@tonic-gate } optvect_t; 105*0Sstevel@tonic-gate 106*0Sstevel@tonic-gate static optvect_t option_params[] = { 107*0Sstevel@tonic-gate { "inherit", CT_PR_INHERIT }, 108*0Sstevel@tonic-gate { "noorphan", CT_PR_NOORPHAN }, 109*0Sstevel@tonic-gate { "pgrponly", CT_PR_PGRPONLY }, 110*0Sstevel@tonic-gate { "regent", CT_PR_REGENT }, 111*0Sstevel@tonic-gate { NULL } 112*0Sstevel@tonic-gate }; 113*0Sstevel@tonic-gate 114*0Sstevel@tonic-gate static optvect_t option_events[] = { 115*0Sstevel@tonic-gate { "core", CT_PR_EV_CORE }, 116*0Sstevel@tonic-gate { "signal", CT_PR_EV_SIGNAL }, 117*0Sstevel@tonic-gate { "hwerr", CT_PR_EV_HWERR }, 118*0Sstevel@tonic-gate { "empty", CT_PR_EV_EMPTY }, 119*0Sstevel@tonic-gate { "fork", CT_PR_EV_FORK }, 120*0Sstevel@tonic-gate { "exit", CT_PR_EV_EXIT }, 121*0Sstevel@tonic-gate { NULL } 122*0Sstevel@tonic-gate }; 123*0Sstevel@tonic-gate 124*0Sstevel@tonic-gate /* 125*0Sstevel@tonic-gate * print_bits 126*0Sstevel@tonic-gate * 127*0Sstevel@tonic-gate * Display a set whose membership is identified by a bitfield. 128*0Sstevel@tonic-gate */ 129*0Sstevel@tonic-gate static void 130*0Sstevel@tonic-gate print_bits(uint_t bits, optvect_t *desc) 131*0Sstevel@tonic-gate { 132*0Sstevel@tonic-gate int i, printed = 0; 133*0Sstevel@tonic-gate 134*0Sstevel@tonic-gate for (i = 0; desc[i].option; i++) 135*0Sstevel@tonic-gate if (desc[i].bit & bits) { 136*0Sstevel@tonic-gate if (printed) 137*0Sstevel@tonic-gate (void) putchar(' '); 138*0Sstevel@tonic-gate printed = 1; 139*0Sstevel@tonic-gate (void) fputs(desc[i].option, stdout); 140*0Sstevel@tonic-gate } 141*0Sstevel@tonic-gate if (printed) 142*0Sstevel@tonic-gate (void) putchar('\n'); 143*0Sstevel@tonic-gate else 144*0Sstevel@tonic-gate (void) puts("none"); 145*0Sstevel@tonic-gate } 146*0Sstevel@tonic-gate 147*0Sstevel@tonic-gate /* 148*0Sstevel@tonic-gate * print_ids 149*0Sstevel@tonic-gate * 150*0Sstevel@tonic-gate * Display a list of ids, sorted. 151*0Sstevel@tonic-gate */ 152*0Sstevel@tonic-gate static void 153*0Sstevel@tonic-gate print_ids(id_t *ids, uint_t nids) 154*0Sstevel@tonic-gate { 155*0Sstevel@tonic-gate int i; 156*0Sstevel@tonic-gate int first = 1; 157*0Sstevel@tonic-gate 158*0Sstevel@tonic-gate qsort(ids, nids, sizeof (int), int_compar); 159*0Sstevel@tonic-gate 160*0Sstevel@tonic-gate for (i = 0; i < nids; i++) { 161*0Sstevel@tonic-gate /*LINTED*/ 162*0Sstevel@tonic-gate (void) printf(" %d" + first, ids[i]); 163*0Sstevel@tonic-gate first = 0; 164*0Sstevel@tonic-gate } 165*0Sstevel@tonic-gate if (first) 166*0Sstevel@tonic-gate (void) puts("none"); 167*0Sstevel@tonic-gate else 168*0Sstevel@tonic-gate (void) putchar('\n'); 169*0Sstevel@tonic-gate } 170*0Sstevel@tonic-gate 171*0Sstevel@tonic-gate typedef void printfunc_t(ct_stathdl_t); 172*0Sstevel@tonic-gate 173*0Sstevel@tonic-gate /* 174*0Sstevel@tonic-gate * A structure defining a displayed field. Includes a label to be 175*0Sstevel@tonic-gate * printed along side the field value, and a function which extracts 176*0Sstevel@tonic-gate * the data from a status structure, formats it, and displays it on 177*0Sstevel@tonic-gate * stdout. 178*0Sstevel@tonic-gate */ 179*0Sstevel@tonic-gate typedef struct verbout { 180*0Sstevel@tonic-gate const char *label; /* field label */ 181*0Sstevel@tonic-gate printfunc_t *func; /* field display function */ 182*0Sstevel@tonic-gate } verbout_t; 183*0Sstevel@tonic-gate 184*0Sstevel@tonic-gate /* 185*0Sstevel@tonic-gate * verb_cookie 186*0Sstevel@tonic-gate * 187*0Sstevel@tonic-gate * Used to display an error encountered when reading a contract status 188*0Sstevel@tonic-gate * field. 189*0Sstevel@tonic-gate */ 190*0Sstevel@tonic-gate static void 191*0Sstevel@tonic-gate verb_error(int err) 192*0Sstevel@tonic-gate { 193*0Sstevel@tonic-gate (void) printf("(error: %s)\n", strerror(err)); 194*0Sstevel@tonic-gate } 195*0Sstevel@tonic-gate 196*0Sstevel@tonic-gate /* 197*0Sstevel@tonic-gate * verb_cookie 198*0Sstevel@tonic-gate * 199*0Sstevel@tonic-gate * Display the contract's cookie. 200*0Sstevel@tonic-gate */ 201*0Sstevel@tonic-gate static void 202*0Sstevel@tonic-gate verb_cookie(ct_stathdl_t hdl) 203*0Sstevel@tonic-gate { 204*0Sstevel@tonic-gate (void) printf("%#llx\n", ct_status_get_cookie(hdl)); 205*0Sstevel@tonic-gate } 206*0Sstevel@tonic-gate 207*0Sstevel@tonic-gate /* 208*0Sstevel@tonic-gate * verb_info 209*0Sstevel@tonic-gate * 210*0Sstevel@tonic-gate * Display the parameters in the parameter set. 211*0Sstevel@tonic-gate */ 212*0Sstevel@tonic-gate static void 213*0Sstevel@tonic-gate verb_param(ct_stathdl_t hdl) 214*0Sstevel@tonic-gate { 215*0Sstevel@tonic-gate uint_t param; 216*0Sstevel@tonic-gate int err; 217*0Sstevel@tonic-gate 218*0Sstevel@tonic-gate if (err = ct_pr_status_get_param(hdl, ¶m)) 219*0Sstevel@tonic-gate verb_error(err); 220*0Sstevel@tonic-gate else 221*0Sstevel@tonic-gate print_bits(param, option_params); 222*0Sstevel@tonic-gate } 223*0Sstevel@tonic-gate 224*0Sstevel@tonic-gate /* 225*0Sstevel@tonic-gate * verb_info 226*0Sstevel@tonic-gate * 227*0Sstevel@tonic-gate * Display the events in the informative event set. 228*0Sstevel@tonic-gate */ 229*0Sstevel@tonic-gate static void 230*0Sstevel@tonic-gate verb_info(ct_stathdl_t hdl) 231*0Sstevel@tonic-gate { 232*0Sstevel@tonic-gate print_bits(ct_status_get_informative(hdl), option_events); 233*0Sstevel@tonic-gate } 234*0Sstevel@tonic-gate 235*0Sstevel@tonic-gate /* 236*0Sstevel@tonic-gate * verb_crit 237*0Sstevel@tonic-gate * 238*0Sstevel@tonic-gate * Display the events in the critical event set. 239*0Sstevel@tonic-gate */ 240*0Sstevel@tonic-gate static void 241*0Sstevel@tonic-gate verb_crit(ct_stathdl_t hdl) 242*0Sstevel@tonic-gate { 243*0Sstevel@tonic-gate print_bits(ct_status_get_critical(hdl), option_events); 244*0Sstevel@tonic-gate } 245*0Sstevel@tonic-gate 246*0Sstevel@tonic-gate /* 247*0Sstevel@tonic-gate * verb_fatal 248*0Sstevel@tonic-gate * 249*0Sstevel@tonic-gate * Display the events in the fatal event set. 250*0Sstevel@tonic-gate */ 251*0Sstevel@tonic-gate static void 252*0Sstevel@tonic-gate verb_fatal(ct_stathdl_t hdl) 253*0Sstevel@tonic-gate { 254*0Sstevel@tonic-gate uint_t event; 255*0Sstevel@tonic-gate int err; 256*0Sstevel@tonic-gate 257*0Sstevel@tonic-gate if (err = ct_pr_status_get_fatal(hdl, &event)) 258*0Sstevel@tonic-gate verb_error(err); 259*0Sstevel@tonic-gate else 260*0Sstevel@tonic-gate print_bits(event, option_events); 261*0Sstevel@tonic-gate } 262*0Sstevel@tonic-gate 263*0Sstevel@tonic-gate /* 264*0Sstevel@tonic-gate * verb_members 265*0Sstevel@tonic-gate * 266*0Sstevel@tonic-gate * Display the list of member contracts. 267*0Sstevel@tonic-gate */ 268*0Sstevel@tonic-gate static void 269*0Sstevel@tonic-gate verb_members(ct_stathdl_t hdl) 270*0Sstevel@tonic-gate { 271*0Sstevel@tonic-gate pid_t *pids; 272*0Sstevel@tonic-gate uint_t npids; 273*0Sstevel@tonic-gate int err; 274*0Sstevel@tonic-gate 275*0Sstevel@tonic-gate if (err = ct_pr_status_get_members(hdl, &pids, &npids)) { 276*0Sstevel@tonic-gate verb_error(err); 277*0Sstevel@tonic-gate return; 278*0Sstevel@tonic-gate } 279*0Sstevel@tonic-gate 280*0Sstevel@tonic-gate print_ids(pids, npids); 281*0Sstevel@tonic-gate } 282*0Sstevel@tonic-gate 283*0Sstevel@tonic-gate /* 284*0Sstevel@tonic-gate * verb_inherit 285*0Sstevel@tonic-gate * 286*0Sstevel@tonic-gate * Display the list of inherited contracts. 287*0Sstevel@tonic-gate */ 288*0Sstevel@tonic-gate static void 289*0Sstevel@tonic-gate verb_inherit(ct_stathdl_t hdl) 290*0Sstevel@tonic-gate { 291*0Sstevel@tonic-gate ctid_t *ctids; 292*0Sstevel@tonic-gate uint_t nctids; 293*0Sstevel@tonic-gate int err; 294*0Sstevel@tonic-gate 295*0Sstevel@tonic-gate if (err = ct_pr_status_get_contracts(hdl, &ctids, &nctids)) 296*0Sstevel@tonic-gate verb_error(err); 297*0Sstevel@tonic-gate else 298*0Sstevel@tonic-gate print_ids(ctids, nctids); 299*0Sstevel@tonic-gate } 300*0Sstevel@tonic-gate 301*0Sstevel@tonic-gate /* 302*0Sstevel@tonic-gate * Common contract status fields. 303*0Sstevel@tonic-gate */ 304*0Sstevel@tonic-gate static verbout_t vcommon[] = { 305*0Sstevel@tonic-gate "cookie", verb_cookie, 306*0Sstevel@tonic-gate NULL, 307*0Sstevel@tonic-gate }; 308*0Sstevel@tonic-gate 309*0Sstevel@tonic-gate /* 310*0Sstevel@tonic-gate * Process contract-specific status fields. 311*0Sstevel@tonic-gate * The critical and informative event sets are here because the event 312*0Sstevel@tonic-gate * names are contract-specific. They are listed first, however, so 313*0Sstevel@tonic-gate * they are displayed adjacent to the "normal" common output. 314*0Sstevel@tonic-gate */ 315*0Sstevel@tonic-gate static verbout_t vprocess[] = { 316*0Sstevel@tonic-gate "informative event set", verb_info, 317*0Sstevel@tonic-gate "critical event set", verb_crit, 318*0Sstevel@tonic-gate "fatal event set", verb_fatal, 319*0Sstevel@tonic-gate "parameter set", verb_param, 320*0Sstevel@tonic-gate "member processes", verb_members, 321*0Sstevel@tonic-gate "inherited contracts", verb_inherit, 322*0Sstevel@tonic-gate NULL 323*0Sstevel@tonic-gate }; 324*0Sstevel@tonic-gate 325*0Sstevel@tonic-gate /* 326*0Sstevel@tonic-gate * print_verbose 327*0Sstevel@tonic-gate * 328*0Sstevel@tonic-gate * Displays a contract's verbose status, common fields first. 329*0Sstevel@tonic-gate */ 330*0Sstevel@tonic-gate static void 331*0Sstevel@tonic-gate print_verbose(ct_stathdl_t hdl, verbout_t *spec, verbout_t *common) 332*0Sstevel@tonic-gate { 333*0Sstevel@tonic-gate int i; 334*0Sstevel@tonic-gate int tmp, maxwidth = 0; 335*0Sstevel@tonic-gate 336*0Sstevel@tonic-gate /* 337*0Sstevel@tonic-gate * Compute the width of all the fields. 338*0Sstevel@tonic-gate */ 339*0Sstevel@tonic-gate for (i = 0; common[i].label; i++) 340*0Sstevel@tonic-gate if ((tmp = strlen(common[i].label)) > maxwidth) 341*0Sstevel@tonic-gate maxwidth = tmp; 342*0Sstevel@tonic-gate if (spec) 343*0Sstevel@tonic-gate for (i = 0; spec[i].label; i++) 344*0Sstevel@tonic-gate if ((tmp = strlen(spec[i].label)) > maxwidth) 345*0Sstevel@tonic-gate maxwidth = tmp; 346*0Sstevel@tonic-gate maxwidth += 2; 347*0Sstevel@tonic-gate 348*0Sstevel@tonic-gate /* 349*0Sstevel@tonic-gate * Display the data. 350*0Sstevel@tonic-gate */ 351*0Sstevel@tonic-gate for (i = 0; common[i].label; i++) { 352*0Sstevel@tonic-gate tmp = printf("\t%s", common[i].label); 353*0Sstevel@tonic-gate if (tmp < 0) 354*0Sstevel@tonic-gate tmp = 0; 355*0Sstevel@tonic-gate (void) printf("%-*s", maxwidth - tmp + 1, ":"); 356*0Sstevel@tonic-gate common[i].func(hdl); 357*0Sstevel@tonic-gate } 358*0Sstevel@tonic-gate if (spec) 359*0Sstevel@tonic-gate for (i = 0; spec[i].label; i++) { 360*0Sstevel@tonic-gate (void) printf("\t%s%n", spec[i].label, &tmp); 361*0Sstevel@tonic-gate (void) printf("%-*s", maxwidth - tmp + 1, ":"); 362*0Sstevel@tonic-gate spec[i].func(hdl); 363*0Sstevel@tonic-gate } 364*0Sstevel@tonic-gate } 365*0Sstevel@tonic-gate 366*0Sstevel@tonic-gate struct { 367*0Sstevel@tonic-gate const char *name; 368*0Sstevel@tonic-gate verbout_t *verbout; 369*0Sstevel@tonic-gate } cttypes[] = { 370*0Sstevel@tonic-gate { "process", vprocess }, 371*0Sstevel@tonic-gate { NULL } 372*0Sstevel@tonic-gate }; 373*0Sstevel@tonic-gate 374*0Sstevel@tonic-gate /* 375*0Sstevel@tonic-gate * get_type 376*0Sstevel@tonic-gate * 377*0Sstevel@tonic-gate * Given a type name, return an index into the above array of types. 378*0Sstevel@tonic-gate */ 379*0Sstevel@tonic-gate static int 380*0Sstevel@tonic-gate get_type(const char *typestr) 381*0Sstevel@tonic-gate { 382*0Sstevel@tonic-gate int i; 383*0Sstevel@tonic-gate for (i = 0; cttypes[i].name; i++) 384*0Sstevel@tonic-gate if (strcmp(cttypes[i].name, typestr) == 0) 385*0Sstevel@tonic-gate return (i); 386*0Sstevel@tonic-gate uu_die(gettext("invalid contract type: %s\n"), typestr); 387*0Sstevel@tonic-gate /* NOTREACHED */ 388*0Sstevel@tonic-gate } 389*0Sstevel@tonic-gate 390*0Sstevel@tonic-gate /* 391*0Sstevel@tonic-gate * print_header 392*0Sstevel@tonic-gate * 393*0Sstevel@tonic-gate * Display the status header. 394*0Sstevel@tonic-gate */ 395*0Sstevel@tonic-gate static void 396*0Sstevel@tonic-gate print_header(void) 397*0Sstevel@tonic-gate { 398*0Sstevel@tonic-gate (void) printf("%-8s%-8s%-8s%-8s%-8s%-8s%-8s%-8s\n", "CTID", "ZONEID", 399*0Sstevel@tonic-gate "TYPE", "STATE", "HOLDER", "EVENTS", "QTIME", "NTIME"); 400*0Sstevel@tonic-gate } 401*0Sstevel@tonic-gate 402*0Sstevel@tonic-gate /* 403*0Sstevel@tonic-gate * print_contract 404*0Sstevel@tonic-gate * 405*0Sstevel@tonic-gate * Display status for contract ID 'id' from type directory 'dir'. If 406*0Sstevel@tonic-gate * only contracts of a specific set of types should be displayed, 407*0Sstevel@tonic-gate * 'types' will be a sorted list of type indices of length 'ntypes'. 408*0Sstevel@tonic-gate */ 409*0Sstevel@tonic-gate static void 410*0Sstevel@tonic-gate print_contract(const char *dir, ctid_t id, verbout_t *spec, 411*0Sstevel@tonic-gate int *types, int ntypes) 412*0Sstevel@tonic-gate { 413*0Sstevel@tonic-gate ct_stathdl_t status; 414*0Sstevel@tonic-gate char hstr[100], qstr[20], nstr[20]; 415*0Sstevel@tonic-gate ctstate_t state; 416*0Sstevel@tonic-gate int fd = 0; 417*0Sstevel@tonic-gate int t; 418*0Sstevel@tonic-gate 419*0Sstevel@tonic-gate /* 420*0Sstevel@tonic-gate * Open and obtain status. 421*0Sstevel@tonic-gate */ 422*0Sstevel@tonic-gate if ((fd = contract_open(id, dir, "status", O_RDONLY)) == -1) { 423*0Sstevel@tonic-gate if (errno == ENOENT) 424*0Sstevel@tonic-gate return; 425*0Sstevel@tonic-gate uu_die(gettext("could not open contract status file")); 426*0Sstevel@tonic-gate } 427*0Sstevel@tonic-gate 428*0Sstevel@tonic-gate if (errno = ct_status_read(fd, opt_verbose ? CTD_ALL : CTD_COMMON, 429*0Sstevel@tonic-gate &status)) 430*0Sstevel@tonic-gate uu_die(gettext("failed to get contract status for %d"), id); 431*0Sstevel@tonic-gate (void) close(fd); 432*0Sstevel@tonic-gate 433*0Sstevel@tonic-gate /* 434*0Sstevel@tonic-gate * Unless otherwise directed, don't display dead contracts. 435*0Sstevel@tonic-gate */ 436*0Sstevel@tonic-gate state = ct_status_get_state(status); 437*0Sstevel@tonic-gate if (!opt_showall && state == CTS_DEAD) { 438*0Sstevel@tonic-gate ct_status_free(status); 439*0Sstevel@tonic-gate return; 440*0Sstevel@tonic-gate } 441*0Sstevel@tonic-gate 442*0Sstevel@tonic-gate /* 443*0Sstevel@tonic-gate * If we are only allowed to display certain contract types, 444*0Sstevel@tonic-gate * perform that filtering here. We stash a copy of spec so we 445*0Sstevel@tonic-gate * don't have to recompute it later. 446*0Sstevel@tonic-gate */ 447*0Sstevel@tonic-gate if (types) { 448*0Sstevel@tonic-gate int key = get_type(ct_status_get_type(status)); 449*0Sstevel@tonic-gate spec = cttypes[key].verbout; 450*0Sstevel@tonic-gate if (bsearch(&key, types, ntypes, sizeof (int), int_compar) == 451*0Sstevel@tonic-gate NULL) { 452*0Sstevel@tonic-gate ct_status_free(status); 453*0Sstevel@tonic-gate return; 454*0Sstevel@tonic-gate } 455*0Sstevel@tonic-gate } 456*0Sstevel@tonic-gate 457*0Sstevel@tonic-gate /* 458*0Sstevel@tonic-gate * Precompute those fields which have both textual and 459*0Sstevel@tonic-gate * numerical values. 460*0Sstevel@tonic-gate */ 461*0Sstevel@tonic-gate if ((state == CTS_OWNED) || (state == CTS_INHERITED)) 462*0Sstevel@tonic-gate (void) snprintf(hstr, sizeof (hstr), "%ld", 463*0Sstevel@tonic-gate ct_status_get_holder(status)); 464*0Sstevel@tonic-gate else 465*0Sstevel@tonic-gate (void) snprintf(hstr, sizeof (hstr), "%s", "-"); 466*0Sstevel@tonic-gate 467*0Sstevel@tonic-gate if ((t = ct_status_get_qtime(status)) == -1) { 468*0Sstevel@tonic-gate qstr[0] = nstr[0] = '-'; 469*0Sstevel@tonic-gate qstr[1] = nstr[1] = '\0'; 470*0Sstevel@tonic-gate } else { 471*0Sstevel@tonic-gate (void) snprintf(qstr, sizeof (qstr), "%d", t); 472*0Sstevel@tonic-gate (void) snprintf(nstr, sizeof (nstr), "%d", 473*0Sstevel@tonic-gate ct_status_get_ntime(status)); 474*0Sstevel@tonic-gate } 475*0Sstevel@tonic-gate 476*0Sstevel@tonic-gate /* 477*0Sstevel@tonic-gate * Emit the contract's status. 478*0Sstevel@tonic-gate */ 479*0Sstevel@tonic-gate (void) printf("%-7ld %-7ld %-7s %-7s %-7s %-7d %-7s %-8s\n", 480*0Sstevel@tonic-gate ct_status_get_id(status), 481*0Sstevel@tonic-gate ct_status_get_zoneid(status), 482*0Sstevel@tonic-gate ct_status_get_type(status), 483*0Sstevel@tonic-gate (state == CTS_OWNED) ? "owned" : 484*0Sstevel@tonic-gate (state == CTS_INHERITED) ? "inherit" : 485*0Sstevel@tonic-gate (state == CTS_ORPHAN) ? "orphan" : "dead", hstr, 486*0Sstevel@tonic-gate ct_status_get_nevents(status), qstr, nstr); 487*0Sstevel@tonic-gate 488*0Sstevel@tonic-gate /* 489*0Sstevel@tonic-gate * Emit verbose status information, if requested. If we 490*0Sstevel@tonic-gate * weren't provided a verbose output spec or didn't compute it 491*0Sstevel@tonic-gate * earlier, do it now. 492*0Sstevel@tonic-gate */ 493*0Sstevel@tonic-gate if (opt_verbose) { 494*0Sstevel@tonic-gate if (spec == NULL) 495*0Sstevel@tonic-gate spec = cttypes[get_type(ct_status_get_type(status))]. 496*0Sstevel@tonic-gate verbout; 497*0Sstevel@tonic-gate print_verbose(status, spec, vcommon); 498*0Sstevel@tonic-gate } 499*0Sstevel@tonic-gate 500*0Sstevel@tonic-gate ct_status_free(status); 501*0Sstevel@tonic-gate } 502*0Sstevel@tonic-gate 503*0Sstevel@tonic-gate /* 504*0Sstevel@tonic-gate * scan_type 505*0Sstevel@tonic-gate * 506*0Sstevel@tonic-gate * Display all contracts of the requested type. 507*0Sstevel@tonic-gate */ 508*0Sstevel@tonic-gate static void 509*0Sstevel@tonic-gate scan_type(int typeno) 510*0Sstevel@tonic-gate { 511*0Sstevel@tonic-gate DIR *dir; 512*0Sstevel@tonic-gate struct dirent64 *de; 513*0Sstevel@tonic-gate char path[PATH_MAX]; 514*0Sstevel@tonic-gate 515*0Sstevel@tonic-gate verbout_t *vo = cttypes[typeno].verbout; 516*0Sstevel@tonic-gate const char *type = cttypes[typeno].name; 517*0Sstevel@tonic-gate 518*0Sstevel@tonic-gate if (snprintf(path, PATH_MAX, CTFS_ROOT "/%s", type) >= PATH_MAX || 519*0Sstevel@tonic-gate (dir = opendir(path)) == NULL) 520*0Sstevel@tonic-gate uu_die(gettext("bad contract type: %s\n"), type); 521*0Sstevel@tonic-gate while ((de = readdir64(dir)) != NULL) { 522*0Sstevel@tonic-gate /* 523*0Sstevel@tonic-gate * Eliminate special files (e.g. '.', '..'). 524*0Sstevel@tonic-gate */ 525*0Sstevel@tonic-gate if (de->d_name[0] < '0' || de->d_name[0] > '9') 526*0Sstevel@tonic-gate continue; 527*0Sstevel@tonic-gate print_contract(type, mystrtoul(de->d_name), vo, NULL, 0); 528*0Sstevel@tonic-gate } 529*0Sstevel@tonic-gate (void) closedir(dir); 530*0Sstevel@tonic-gate } 531*0Sstevel@tonic-gate 532*0Sstevel@tonic-gate /* 533*0Sstevel@tonic-gate * scan_ids 534*0Sstevel@tonic-gate * 535*0Sstevel@tonic-gate * Display all contracts with the requested IDs. 536*0Sstevel@tonic-gate */ 537*0Sstevel@tonic-gate static void 538*0Sstevel@tonic-gate scan_ids(ctid_t *ids, int nids) 539*0Sstevel@tonic-gate { 540*0Sstevel@tonic-gate int i; 541*0Sstevel@tonic-gate for (i = 0; i < nids; i++) 542*0Sstevel@tonic-gate print_contract("all", ids[i], NULL, NULL, 0); 543*0Sstevel@tonic-gate } 544*0Sstevel@tonic-gate 545*0Sstevel@tonic-gate /* 546*0Sstevel@tonic-gate * scan_all 547*0Sstevel@tonic-gate * 548*0Sstevel@tonic-gate * Display the union of the requested IDs and types. So that the 549*0Sstevel@tonic-gate * output is sorted by contract ID, it takes the slow road by testing 550*0Sstevel@tonic-gate * each entry in /system/contract/all against its criteria. Used when 551*0Sstevel@tonic-gate * the number of types is greater than 1, when we have a mixture of 552*0Sstevel@tonic-gate * types and ids, or no lists were provided at all. 553*0Sstevel@tonic-gate */ 554*0Sstevel@tonic-gate static void 555*0Sstevel@tonic-gate scan_all(int *types, int ntypes, ctid_t *ids, int nids) 556*0Sstevel@tonic-gate { 557*0Sstevel@tonic-gate DIR *dir; 558*0Sstevel@tonic-gate struct dirent64 *de; 559*0Sstevel@tonic-gate const char *path = CTFS_ROOT "/all"; 560*0Sstevel@tonic-gate int key, test; 561*0Sstevel@tonic-gate 562*0Sstevel@tonic-gate if ((dir = opendir(path)) == NULL) 563*0Sstevel@tonic-gate uu_die(gettext("could not open %s"), path); 564*0Sstevel@tonic-gate while ((de = readdir64(dir)) != NULL) { 565*0Sstevel@tonic-gate /* 566*0Sstevel@tonic-gate * Eliminate special files (e.g. '.', '..'). 567*0Sstevel@tonic-gate */ 568*0Sstevel@tonic-gate if (de->d_name[0] < '0' || de->d_name[0] > '9') 569*0Sstevel@tonic-gate continue; 570*0Sstevel@tonic-gate key = mystrtoul(de->d_name); 571*0Sstevel@tonic-gate 572*0Sstevel@tonic-gate /* 573*0Sstevel@tonic-gate * If we are given IDs to look at and this contract 574*0Sstevel@tonic-gate * isn't in the ID list, or if we weren't given a list 575*0Sstevel@tonic-gate * if IDs but were given a list of types, provide the 576*0Sstevel@tonic-gate * list of acceptable types to print_contract. 577*0Sstevel@tonic-gate */ 578*0Sstevel@tonic-gate test = nids ? (bsearch(&key, ids, nids, sizeof (int), 579*0Sstevel@tonic-gate int_compar) == NULL) : (ntypes != 0); 580*0Sstevel@tonic-gate print_contract("all", key, NULL, (test ? types : NULL), ntypes); 581*0Sstevel@tonic-gate } 582*0Sstevel@tonic-gate (void) closedir(dir); 583*0Sstevel@tonic-gate } 584*0Sstevel@tonic-gate 585*0Sstevel@tonic-gate /* 586*0Sstevel@tonic-gate * walk_args 587*0Sstevel@tonic-gate * 588*0Sstevel@tonic-gate * Apply fp to each token in the comma- or space- separated argument 589*0Sstevel@tonic-gate * string str and store the results in the array starting at results. 590*0Sstevel@tonic-gate */ 591*0Sstevel@tonic-gate static int 592*0Sstevel@tonic-gate walk_args(const char *str, int (*fp)(const char *), int *results) 593*0Sstevel@tonic-gate { 594*0Sstevel@tonic-gate char *copy, *token; 595*0Sstevel@tonic-gate int count = 0; 596*0Sstevel@tonic-gate 597*0Sstevel@tonic-gate if ((copy = strdup(str)) == NULL) 598*0Sstevel@tonic-gate uu_die(gettext("strdup() failed")); 599*0Sstevel@tonic-gate 600*0Sstevel@tonic-gate token = strtok(copy, ", "); 601*0Sstevel@tonic-gate if (token == NULL) { 602*0Sstevel@tonic-gate free(copy); 603*0Sstevel@tonic-gate return (0); 604*0Sstevel@tonic-gate } 605*0Sstevel@tonic-gate 606*0Sstevel@tonic-gate do { 607*0Sstevel@tonic-gate if (fp) 608*0Sstevel@tonic-gate *(results++) = fp(token); 609*0Sstevel@tonic-gate count++; 610*0Sstevel@tonic-gate } while (token = strtok(NULL, ", ")); 611*0Sstevel@tonic-gate free(copy); 612*0Sstevel@tonic-gate 613*0Sstevel@tonic-gate return (count); 614*0Sstevel@tonic-gate } 615*0Sstevel@tonic-gate 616*0Sstevel@tonic-gate /* 617*0Sstevel@tonic-gate * parse 618*0Sstevel@tonic-gate * 619*0Sstevel@tonic-gate * Parse the comma- or space- separated string str, using fp to covert 620*0Sstevel@tonic-gate * the tokens to integers. Append the list of integers to the array 621*0Sstevel@tonic-gate * pointed to by *idps, growing the array if necessary. 622*0Sstevel@tonic-gate */ 623*0Sstevel@tonic-gate static int 624*0Sstevel@tonic-gate parse(const char *str, int **idsp, int nids, int (*fp)(const char *fp)) 625*0Sstevel@tonic-gate { 626*0Sstevel@tonic-gate int count; 627*0Sstevel@tonic-gate int *array; 628*0Sstevel@tonic-gate 629*0Sstevel@tonic-gate count = walk_args(str, NULL, NULL); 630*0Sstevel@tonic-gate if (count == 0) 631*0Sstevel@tonic-gate return (0); 632*0Sstevel@tonic-gate 633*0Sstevel@tonic-gate if ((array = calloc(nids + count, sizeof (int))) == NULL) 634*0Sstevel@tonic-gate uu_die(gettext("calloc() failed")); 635*0Sstevel@tonic-gate 636*0Sstevel@tonic-gate if (*idsp) { 637*0Sstevel@tonic-gate (void) memcpy(array, *idsp, nids * sizeof (int)); 638*0Sstevel@tonic-gate free(*idsp); 639*0Sstevel@tonic-gate } 640*0Sstevel@tonic-gate 641*0Sstevel@tonic-gate (void) walk_args(str, fp, array + nids); 642*0Sstevel@tonic-gate 643*0Sstevel@tonic-gate *idsp = array; 644*0Sstevel@tonic-gate return (count + nids); 645*0Sstevel@tonic-gate } 646*0Sstevel@tonic-gate 647*0Sstevel@tonic-gate /* 648*0Sstevel@tonic-gate * parse_ids 649*0Sstevel@tonic-gate * 650*0Sstevel@tonic-gate * Extract a list of ids from the comma- or space- separated string str 651*0Sstevel@tonic-gate * and append them to the array *idsp, growing it if necessary. 652*0Sstevel@tonic-gate */ 653*0Sstevel@tonic-gate static int 654*0Sstevel@tonic-gate parse_ids(const char *arg, int **idsp, int nids) 655*0Sstevel@tonic-gate { 656*0Sstevel@tonic-gate return (parse(arg, idsp, nids, mystrtoul)); 657*0Sstevel@tonic-gate } 658*0Sstevel@tonic-gate 659*0Sstevel@tonic-gate /* 660*0Sstevel@tonic-gate * parse_types 661*0Sstevel@tonic-gate * 662*0Sstevel@tonic-gate * Extract a list of types from the comma- or space- separated string 663*0Sstevel@tonic-gate * str and append them to the array *idsp, growing it if necessary. 664*0Sstevel@tonic-gate */ 665*0Sstevel@tonic-gate static int 666*0Sstevel@tonic-gate parse_types(const char *arg, int **typesp, int ntypes) 667*0Sstevel@tonic-gate { 668*0Sstevel@tonic-gate return (parse(arg, typesp, ntypes, get_type)); 669*0Sstevel@tonic-gate } 670*0Sstevel@tonic-gate 671*0Sstevel@tonic-gate /* 672*0Sstevel@tonic-gate * compact 673*0Sstevel@tonic-gate * 674*0Sstevel@tonic-gate * Sorts and removes duplicates from array. Initial size of array is 675*0Sstevel@tonic-gate * in *size; final size is stored in *size. 676*0Sstevel@tonic-gate */ 677*0Sstevel@tonic-gate static void 678*0Sstevel@tonic-gate compact(int *array, int *size) 679*0Sstevel@tonic-gate { 680*0Sstevel@tonic-gate int i, j, last = -1; 681*0Sstevel@tonic-gate 682*0Sstevel@tonic-gate qsort(array, *size, sizeof (int), int_compar); 683*0Sstevel@tonic-gate for (i = j = 0; i < *size; i++) { 684*0Sstevel@tonic-gate if (array[i] != last) { 685*0Sstevel@tonic-gate last = array[i]; 686*0Sstevel@tonic-gate array[j++] = array[i]; 687*0Sstevel@tonic-gate } 688*0Sstevel@tonic-gate } 689*0Sstevel@tonic-gate *size = j; 690*0Sstevel@tonic-gate } 691*0Sstevel@tonic-gate 692*0Sstevel@tonic-gate int 693*0Sstevel@tonic-gate main(int argc, char **argv) 694*0Sstevel@tonic-gate { 695*0Sstevel@tonic-gate unsigned int interval = 0, count = 1; 696*0Sstevel@tonic-gate ctid_t *ids = NULL; 697*0Sstevel@tonic-gate int *types = NULL; 698*0Sstevel@tonic-gate int nids = 0, ntypes = 0; 699*0Sstevel@tonic-gate int i, s; 700*0Sstevel@tonic-gate 701*0Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 702*0Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 703*0Sstevel@tonic-gate 704*0Sstevel@tonic-gate (void) uu_setpname(argv[0]); 705*0Sstevel@tonic-gate 706*0Sstevel@tonic-gate while ((s = getopt(argc, argv, "ai:t:v")) != EOF) { 707*0Sstevel@tonic-gate switch (s) { 708*0Sstevel@tonic-gate case 'a': 709*0Sstevel@tonic-gate opt_showall = 1; 710*0Sstevel@tonic-gate break; 711*0Sstevel@tonic-gate case 'i': 712*0Sstevel@tonic-gate nids = parse_ids(optarg, (int **)&ids, nids); 713*0Sstevel@tonic-gate break; 714*0Sstevel@tonic-gate case 't': 715*0Sstevel@tonic-gate ntypes = parse_types(optarg, &types, ntypes); 716*0Sstevel@tonic-gate break; 717*0Sstevel@tonic-gate case 'v': 718*0Sstevel@tonic-gate opt_verbose = 1; 719*0Sstevel@tonic-gate break; 720*0Sstevel@tonic-gate default: 721*0Sstevel@tonic-gate usage(); 722*0Sstevel@tonic-gate } 723*0Sstevel@tonic-gate } 724*0Sstevel@tonic-gate 725*0Sstevel@tonic-gate argc -= optind; 726*0Sstevel@tonic-gate argv += optind; 727*0Sstevel@tonic-gate 728*0Sstevel@tonic-gate if (argc > 2 || argc < 0) 729*0Sstevel@tonic-gate usage(); 730*0Sstevel@tonic-gate 731*0Sstevel@tonic-gate if (argc > 0) { 732*0Sstevel@tonic-gate interval = mystrtoul(argv[0]); 733*0Sstevel@tonic-gate count = 0; 734*0Sstevel@tonic-gate } 735*0Sstevel@tonic-gate 736*0Sstevel@tonic-gate if (argc > 1) { 737*0Sstevel@tonic-gate count = mystrtoul(argv[1]); 738*0Sstevel@tonic-gate if (count == 0) 739*0Sstevel@tonic-gate return (0); 740*0Sstevel@tonic-gate } 741*0Sstevel@tonic-gate 742*0Sstevel@tonic-gate if (nids) 743*0Sstevel@tonic-gate compact((int *)ids, &nids); 744*0Sstevel@tonic-gate if (ntypes) 745*0Sstevel@tonic-gate compact(types, &ntypes); 746*0Sstevel@tonic-gate 747*0Sstevel@tonic-gate for (i = 0; count == 0 || i < count; i++) { 748*0Sstevel@tonic-gate if (i) 749*0Sstevel@tonic-gate (void) sleep(interval); 750*0Sstevel@tonic-gate print_header(); 751*0Sstevel@tonic-gate if (nids && ntypes) 752*0Sstevel@tonic-gate scan_all(types, ntypes, ids, nids); 753*0Sstevel@tonic-gate else if (ntypes == 1) 754*0Sstevel@tonic-gate scan_type(*types); 755*0Sstevel@tonic-gate else if (nids) 756*0Sstevel@tonic-gate scan_ids(ids, nids); 757*0Sstevel@tonic-gate else 758*0Sstevel@tonic-gate scan_all(types, ntypes, ids, nids); 759*0Sstevel@tonic-gate } 760*0Sstevel@tonic-gate 761*0Sstevel@tonic-gate return (0); 762*0Sstevel@tonic-gate } 763