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