xref: /illumos-gate/usr/src/cmd/fm/fmtopo/common/fmtopo.c (revision 55fea89dcaa64928bed4327112404dcb3e07b79f)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Copyright (c) 2019, Joyent, Inc. All rights reserved.
25  * Copyright 2023 Oxide Computer Company
26  */
27 
28 
29 #include <sys/fm/protocol.h>
30 #include <fm/libtopo.h>
31 #include <ctype.h>
32 #include <fnmatch.h>
33 #include <limits.h>
34 #include <strings.h>
35 #include <stdio.h>
36 #include <errno.h>
37 #include <umem.h>
38 #include <zone.h>
39 #include <sys/param.h>
40 
41 #define	FMTOPO_EXIT_SUCCESS	0
42 #define	FMTOPO_EXIT_ERROR	1
43 #define	FMTOPO_EXIT_USAGE	2
44 
45 #define	STDERR	"stderr"
46 #define	ALL	"all"
47 
48 static const char *g_pname;
49 static const char *g_fmri = NULL;
50 
51 static const char *opt_R = "/";
52 static const char *opt_s = FM_FMRI_SCHEME_HC;
53 static const char optstr[] = "bCdelm:P:pR:s:StVx";
54 static const char *opt_m;
55 
56 static int opt_b = 0;
57 static int opt_d = 0;
58 static int opt_e = 0;
59 static int opt_l = 0;
60 static int opt_p = 0;
61 static int opt_S = 0;
62 static int opt_t = 0;
63 static int opt_V = 0;
64 static int opt_x = 0;
65 static int opt_all = 0;
66 
67 struct prop_args {
68 	const char *group;
69 	const char *prop;
70 	const char *type;
71 	const char *value;
72 };
73 
74 static struct prop_args **pargs = NULL;
75 static int pcnt = 0;
76 
77 static int
78 usage(FILE *fp)
79 {
80 	(void) fprintf(fp,
81 	    "Usage: %s [-bCedlpSVx] [-P group.property[=type:value]] "
82 	    "[-R root] [-m method] [-s scheme] [fmri]\n", g_pname);
83 
84 	(void) fprintf(fp,
85 	    "\t-b  walk in sibling-first order (default is child-first)\n"
86 	    "\t-C  dump core after completing execution\n"
87 	    "\t-d  set debug mode for libtopo modules\n"
88 	    "\t-e  display FMRIs as paths using esc/eft notation\n"
89 	    "\t-l  list available schemes rather than print a tree\n"
90 	    "\t-m  execute given method\n"
91 	    "\t-P  get/set specified properties\n"
92 	    "\t-p  display of FMRI protocol properties\n"
93 	    "\t-R  set root directory for libtopo plug-ins and other files\n"
94 	    "\t-s  display topology for the specified FMRI scheme\n"
95 	    "\t-S  display FMRI status (present/usable/occupied)\n"
96 	    "\t-V  set verbose mode\n"
97 	    "\t-x  display a xml formatted topology\n");
98 
99 	return (FMTOPO_EXIT_USAGE);
100 }
101 
102 static topo_type_t
103 str2type(const char *tstr)
104 {
105 	topo_type_t type;
106 
107 	if (tstr == NULL)
108 		return (TOPO_TYPE_INVALID);
109 
110 	if (strcmp(tstr, "int32") == 0)
111 		type = TOPO_TYPE_INT32;
112 	else if (strcmp(tstr, "uint32") == 0)
113 		type = TOPO_TYPE_UINT32;
114 	else if (strcmp(tstr, "int64") == 0)
115 		type = TOPO_TYPE_INT64;
116 	else if (strcmp(tstr, "uint64") == 0)
117 		type = TOPO_TYPE_UINT64;
118 	else if (strcmp(tstr, "string") == 0)
119 		type = TOPO_TYPE_STRING;
120 	else if (strcmp(tstr, "fmri") == 0)
121 		type = TOPO_TYPE_FMRI;
122 	else {
123 		type = TOPO_TYPE_INVALID;
124 	}
125 
126 	return (type);
127 }
128 
129 static void
130 print_node(topo_hdl_t *thp, tnode_t *node, nvlist_t *nvl, const char *fmri)
131 {
132 	int err, ret;
133 	boolean_t is_occupied;
134 
135 	(void) printf("%s\n", (char *)fmri);
136 
137 	if (opt_p && !(pcnt > 0 || opt_V || opt_all)) {
138 		char *aname = NULL, *fname = NULL, *lname = NULL;
139 		nvlist_t *asru = NULL;
140 		nvlist_t *fru = NULL;
141 
142 		if (topo_node_asru(node, &asru, NULL, &err) == 0)
143 			(void) topo_fmri_nvl2str(thp, asru, &aname, &err);
144 		if (topo_node_fru(node, &fru, NULL, &err) == 0)
145 			(void) topo_fmri_nvl2str(thp, fru, &fname, &err);
146 		(void) topo_node_label(node, &lname, &err);
147 		if (aname != NULL) {
148 			nvlist_free(asru);
149 			(void) printf("\tASRU: %s\n", aname);
150 			topo_hdl_strfree(thp, aname);
151 		} else {
152 			(void) printf("\tASRU: -\n");
153 		}
154 		if (fname != NULL) {
155 			nvlist_free(fru);
156 			(void) printf("\tFRU: %s\n", fname);
157 			topo_hdl_strfree(thp, fname);
158 		} else {
159 			(void) printf("\tFRU: -\n");
160 		}
161 		if (lname != NULL) {
162 			(void) printf("\tLabel: %s\n", lname);
163 			topo_hdl_strfree(thp, lname);
164 		} else {
165 			(void) printf("\tLabel: -\n");
166 		}
167 	}
168 
169 	if (opt_S) {
170 		if ((ret = topo_fmri_present(thp, nvl, &err)) < 0)
171 			(void) printf("\tPresent: -\n");
172 		else
173 			(void) printf("\tPresent: %s\n",
174 			    ret ? "true" : "false");
175 
176 		if ((ret = topo_fmri_unusable(thp, nvl, &err)) < 0)
177 			(void) printf("\tUnusable: -\n");
178 		else
179 			(void) printf("\tUnusable: %s\n",
180 			    ret ? "true" : "false");
181 
182 		ret = topo_node_occupied(node, &is_occupied);
183 		if (ret == 0)
184 			(void) printf("\tOccupied: %s\n",
185 			    is_occupied ? "true" : "false");
186 		else if (ret != ETOPO_METHOD_NOTSUP)
187 			(void) printf("\tOccupied: -\n");
188 	}
189 }
190 
191 static void
192 print_everstyle(tnode_t *node)
193 {
194 	char buf[PATH_MAX], numbuf[64];
195 	nvlist_t *fmri, **hcl;
196 	int i, err;
197 	uint_t n;
198 
199 	if (topo_prop_get_fmri(node, TOPO_PGROUP_PROTOCOL,
200 	    TOPO_PROP_RESOURCE, &fmri, &err) < 0) {
201 		(void) fprintf(stderr, "%s: failed to get fmri for %s=%d: %s\n",
202 		    g_pname, topo_node_name(node),
203 		    topo_node_instance(node), topo_strerror(err));
204 		return;
205 	}
206 
207 	if (nvlist_lookup_nvlist_array(fmri, FM_FMRI_HC_LIST, &hcl, &n) != 0) {
208 		(void) fprintf(stderr, "%s: failed to find %s for %s=%d\n",
209 		    g_pname, FM_FMRI_HC_LIST, topo_node_name(node),
210 		    topo_node_instance(node));
211 		nvlist_free(fmri);
212 		return;
213 	}
214 
215 	buf[0] = '\0';
216 
217 	for (i = 0; i < n; i++) {
218 		char *name, *inst, *estr;
219 		ulong_t ul;
220 
221 		if (nvlist_lookup_string(hcl[i], FM_FMRI_HC_NAME, &name) != 0 ||
222 		    nvlist_lookup_string(hcl[i], FM_FMRI_HC_ID, &inst) != 0) {
223 			(void) fprintf(stderr, "%s: failed to get "
224 			    "name-instance for %s=%d\n", g_pname,
225 			    topo_node_name(node), topo_node_instance(node));
226 			nvlist_free(fmri);
227 			return;
228 		}
229 
230 		errno = 0;
231 		ul = strtoul(inst, &estr, 10);
232 
233 		if (errno != 0 || estr == inst) {
234 			(void) fprintf(stderr, "%s: instance %s does not "
235 			    "convert to an unsigned integer\n", g_pname, inst);
236 		}
237 
238 		(void) strlcat(buf, "/", sizeof (buf));
239 		(void) strlcat(buf, name, sizeof (buf));
240 		(void) snprintf(numbuf, sizeof (numbuf), "%u", ul);
241 		(void) strlcat(buf, numbuf, sizeof (buf));
242 	}
243 	nvlist_free(fmri);
244 
245 	(void) printf("%s\n", buf);
246 }
247 
248 static void
249 print_prop_nameval(topo_hdl_t *thp, tnode_t *node, nvlist_t *nvl)
250 {
251 	int err;
252 	topo_type_t type;
253 	char *tstr, *propn, *factype;
254 	nvpair_t *pv_nvp;
255 	int i;
256 	uint_t nelem;
257 
258 	if ((pv_nvp = nvlist_next_nvpair(nvl, NULL)) == NULL)
259 		return;
260 
261 	/* Print property name */
262 	if ((pv_nvp = nvlist_next_nvpair(nvl, NULL)) == NULL ||
263 	    nvpair_name(pv_nvp) == NULL ||
264 	    strcmp(TOPO_PROP_VAL_NAME, nvpair_name(pv_nvp)) != 0) {
265 		(void) fprintf(stderr, "%s: malformed property name\n",
266 		    g_pname);
267 		return;
268 	} else {
269 		(void) nvpair_value_string(pv_nvp, &propn);
270 	}
271 
272 	if ((pv_nvp = nvlist_next_nvpair(nvl, pv_nvp)) == NULL ||
273 	    nvpair_name(pv_nvp) == NULL ||
274 	    strcmp(nvpair_name(pv_nvp), TOPO_PROP_VAL_TYPE) != 0 ||
275 	    nvpair_type(pv_nvp) != DATA_TYPE_UINT32)  {
276 		(void) fprintf(stderr, "%s: malformed property type for %s\n",
277 		    g_pname, propn);
278 		return;
279 	} else {
280 		(void) nvpair_value_uint32(pv_nvp, (uint32_t *)&type);
281 	}
282 
283 	switch (type) {
284 		case TOPO_TYPE_BOOLEAN: tstr = "boolean"; break;
285 		case TOPO_TYPE_INT32: tstr = "int32"; break;
286 		case TOPO_TYPE_UINT32: tstr = "uint32"; break;
287 		case TOPO_TYPE_INT64: tstr = "int64"; break;
288 		case TOPO_TYPE_UINT64: tstr = "uint64"; break;
289 		case TOPO_TYPE_DOUBLE: tstr = "double"; break;
290 		case TOPO_TYPE_STRING: tstr = "string"; break;
291 		case TOPO_TYPE_FMRI: tstr = "fmri"; break;
292 		case TOPO_TYPE_INT32_ARRAY: tstr = "int32[]"; break;
293 		case TOPO_TYPE_UINT32_ARRAY: tstr = "uint32[]"; break;
294 		case TOPO_TYPE_INT64_ARRAY: tstr = "int64[]"; break;
295 		case TOPO_TYPE_UINT64_ARRAY: tstr = "uint64[]"; break;
296 		case TOPO_TYPE_STRING_ARRAY: tstr = "string[]"; break;
297 		case TOPO_TYPE_FMRI_ARRAY: tstr = "fmri[]"; break;
298 		default: tstr = "unknown type";
299 	}
300 
301 	(void) printf("    %-17s %-8s ", propn, tstr);
302 
303 	/*
304 	 * Get property value
305 	 */
306 	if (nvpair_name(pv_nvp) == NULL ||
307 	    (pv_nvp = nvlist_next_nvpair(nvl, pv_nvp)) == NULL) {
308 		(void) fprintf(stderr, "%s: malformed property value\n",
309 		    g_pname);
310 		return;
311 	}
312 
313 	switch (nvpair_type(pv_nvp)) {
314 		case DATA_TYPE_INT32: {
315 			int32_t val;
316 			(void) nvpair_value_int32(pv_nvp, &val);
317 			(void) printf(" %d", val);
318 			break;
319 		}
320 		case DATA_TYPE_UINT32: {
321 			uint32_t val, type;
322 			char val_str[49];
323 			nvlist_t *fac, *rsrc = NULL;
324 
325 			(void) nvpair_value_uint32(pv_nvp, &val);
326 			if (node == NULL || topo_node_flags(node) !=
327 			    TOPO_NODE_FACILITY)
328 				goto uint32_def;
329 
330 			if (topo_node_resource(node, &rsrc, &err) != 0)
331 				goto uint32_def;
332 
333 			if (nvlist_lookup_nvlist(rsrc, "facility", &fac) != 0)
334 				goto uint32_def;
335 
336 			if (nvlist_lookup_string(fac, FM_FMRI_FACILITY_TYPE,
337 			    &factype) != 0)
338 				goto uint32_def;
339 
340 			nvlist_free(rsrc);
341 			rsrc = NULL;
342 
343 			/*
344 			 * Special case code to do friendlier printing of
345 			 * facility node properties
346 			 */
347 			if ((strcmp(propn, TOPO_FACILITY_TYPE) == 0) &&
348 			    (strcmp(factype, TOPO_FAC_TYPE_SENSOR) == 0)) {
349 				topo_sensor_type_name(val, val_str, 48);
350 				(void) printf(" 0x%x (%s)", val, val_str);
351 				break;
352 			} else if ((strcmp(propn, TOPO_FACILITY_TYPE) == 0) &&
353 			    (strcmp(factype, TOPO_FAC_TYPE_INDICATOR) == 0)) {
354 				topo_led_type_name(val, val_str, 48);
355 				(void) printf(" 0x%x (%s)", val, val_str);
356 				break;
357 			} else if (strcmp(propn, TOPO_SENSOR_UNITS) == 0) {
358 				topo_sensor_units_name(val, val_str, 48);
359 				(void) printf(" 0x%x (%s)", val, val_str);
360 				break;
361 			} else if (strcmp(propn, TOPO_LED_MODE) == 0) {
362 				topo_led_state_name(val, val_str, 48);
363 				(void) printf(" 0x%x (%s)", val, val_str);
364 				break;
365 			} else if ((strcmp(propn, TOPO_SENSOR_STATE) == 0) &&
366 			    (strcmp(factype, TOPO_FAC_TYPE_SENSOR) == 0)) {
367 				if (topo_prop_get_uint32(node,
368 				    TOPO_PGROUP_FACILITY, TOPO_FACILITY_TYPE,
369 				    &type, &err) != 0) {
370 					goto uint32_def;
371 				}
372 				topo_sensor_state_name(type, val, val_str, 48);
373 				(void) printf(" 0x%x (%s)", val, val_str);
374 				break;
375 			}
376 uint32_def:
377 			(void) printf(" 0x%x", val);
378 			nvlist_free(rsrc);
379 			break;
380 		}
381 		case DATA_TYPE_INT64: {
382 			int64_t val;
383 			(void) nvpair_value_int64(pv_nvp, &val);
384 			(void) printf(" %lld", (longlong_t)val);
385 			break;
386 		}
387 		case DATA_TYPE_UINT64: {
388 			uint64_t val;
389 			(void) nvpair_value_uint64(pv_nvp, &val);
390 			(void) printf(" 0x%llx", (u_longlong_t)val);
391 			break;
392 		}
393 		case DATA_TYPE_DOUBLE: {
394 			double val;
395 			(void) nvpair_value_double(pv_nvp, &val);
396 			(void) printf(" %lf", (double)val);
397 			break;
398 		}
399 		case DATA_TYPE_STRING: {
400 			char *val;
401 			(void) nvpair_value_string(pv_nvp, &val);
402 			(void) printf(" %s", val);
403 			break;
404 		}
405 		case DATA_TYPE_NVLIST: {
406 			nvlist_t *val;
407 			char *fmri;
408 			(void) nvpair_value_nvlist(pv_nvp, &val);
409 			if (topo_fmri_nvl2str(thp, val, &fmri, &err) != 0) {
410 				(void) fprintf(stderr, "failed to convert "
411 				    "FMRI to string: (%s)\n",
412 				    topo_strerror(err));
413 				nvlist_print(stdout, nvl);
414 				break;
415 			}
416 			(void) printf(" %s", fmri);
417 			topo_hdl_strfree(thp, fmri);
418 			break;
419 		}
420 		case DATA_TYPE_INT32_ARRAY: {
421 			int32_t *val;
422 
423 			(void) nvpair_value_int32_array(pv_nvp, &val, &nelem);
424 			(void) printf(" [ ");
425 			for (i = 0; i < nelem; i++)
426 				(void) printf("%d ", val[i]);
427 			(void) printf("]");
428 			break;
429 		}
430 		case DATA_TYPE_UINT32_ARRAY: {
431 			uint32_t *val;
432 
433 			(void) nvpair_value_uint32_array(pv_nvp, &val, &nelem);
434 			(void) printf(" [ ");
435 			for (i = 0; i < nelem; i++)
436 				(void) printf("%u ", val[i]);
437 			(void) printf("]");
438 			break;
439 		}
440 		case DATA_TYPE_INT64_ARRAY: {
441 			int64_t *val;
442 
443 			(void) nvpair_value_int64_array(pv_nvp, &val, &nelem);
444 			(void) printf(" [ ");
445 			for (i = 0; i < nelem; i++)
446 				(void) printf("%lld ", val[i]);
447 			(void) printf("]");
448 			break;
449 		}
450 		case DATA_TYPE_UINT64_ARRAY: {
451 			uint64_t *val;
452 
453 			(void) nvpair_value_uint64_array(pv_nvp, &val, &nelem);
454 			(void) printf(" [ ");
455 			for (i = 0; i < nelem; i++)
456 				(void) printf("%llu ", val[i]);
457 			(void) printf("]");
458 			break;
459 		}
460 		case DATA_TYPE_STRING_ARRAY: {
461 			char **val;
462 
463 			(void) nvpair_value_string_array(pv_nvp, &val, &nelem);
464 			(void) printf(" [ ");
465 			for (i = 0; i < nelem; i++)
466 				(void) printf("\"%s\" ", val[i]);
467 			(void) printf("]");
468 			break;
469 		}
470 		case DATA_TYPE_NVLIST_ARRAY: {
471 			nvlist_t **val;
472 			char *fmri;
473 			int ret;
474 
475 			(void) nvpair_value_nvlist_array(pv_nvp, &val, &nelem);
476 			(void) printf(" [ ");
477 			for (i = 0; i < nelem; i++) {
478 				ret = topo_fmri_nvl2str(thp, val[i], &fmri,
479 				    &err);
480 				if (ret != 0) {
481 					(void) fprintf(stderr, "failed to "
482 					    "convert FMRI to string (%s)\n",
483 					    topo_strerror(err));
484 					nvlist_print(stdout, val[i]);
485 					break;
486 				}
487 				(void) printf("\"%s\" ", fmri);
488 				topo_hdl_strfree(thp, fmri);
489 			}
490 			(void) printf("]");
491 			break;
492 		}
493 		default:
494 			(void) fprintf(stderr, " unknown data type (%d)",
495 			    nvpair_type(pv_nvp));
496 			break;
497 		}
498 		(void) printf("\n");
499 }
500 
501 static void
502 print_pgroup(topo_hdl_t *thp, tnode_t *node, const char *pgn, char *dstab,
503     char *nstab, int32_t version)
504 {
505 	int err;
506 	topo_pgroup_info_t *pgi = NULL;
507 
508 	if (pgn == NULL)
509 		return;
510 
511 	if (node != NULL && (dstab == NULL || nstab == NULL || version == -1)) {
512 		if ((pgi = topo_pgroup_info(node, pgn, &err)) != NULL) {
513 			dstab = (char *)topo_stability2name(pgi->tpi_datastab);
514 			nstab = (char *)topo_stability2name(pgi->tpi_namestab);
515 			version = pgi->tpi_version;
516 		}
517 	}
518 
519 	if (dstab == NULL || nstab == NULL || version == -1) {
520 		(void) printf("  group: %-30s version: - stability: -/-\n",
521 		    pgn);
522 	} else {
523 		(void) printf("  group: %-30s version: %-3d stability: %s/%s\n",
524 		    pgn, version, nstab, dstab);
525 	}
526 
527 	if (pgi != NULL) {
528 		topo_hdl_strfree(thp, (char *)pgi->tpi_name);
529 		topo_hdl_free(thp, pgi, sizeof (topo_pgroup_info_t));
530 	}
531 }
532 
533 static void
534 print_all_props(topo_hdl_t *thp, tnode_t *node, nvlist_t *p_nv,
535     const char *group)
536 {
537 	char *pgn = NULL, *dstab = NULL, *nstab = NULL;
538 	int32_t version;
539 	nvlist_t *pg_nv, *pv_nv;
540 	nvpair_t *nvp, *pg_nvp;
541 	int pg_done, match, all = strcmp(group, ALL) == 0;
542 
543 	for (nvp = nvlist_next_nvpair(p_nv, NULL); nvp != NULL;
544 	    nvp = nvlist_next_nvpair(p_nv, nvp)) {
545 		if (strcmp(TOPO_PROP_GROUP, nvpair_name(nvp)) != 0 ||
546 		    nvpair_type(nvp) != DATA_TYPE_NVLIST)
547 			continue;
548 
549 		nstab = NULL;
550 		dstab = NULL;
551 		version = -1;
552 		pg_done = match = 0;
553 		(void) nvpair_value_nvlist(nvp, &pg_nv);
554 		for (pg_nvp = nvlist_next_nvpair(pg_nv, NULL); pg_nvp != NULL;
555 		    pg_nvp = nvlist_next_nvpair(pg_nv, pg_nvp)) {
556 			/*
557 			 * Print property group name and stability levels
558 			 */
559 			if (strcmp(TOPO_PROP_GROUP_NAME, nvpair_name(pg_nvp))
560 			    == 0 && nvpair_type(pg_nvp) == DATA_TYPE_STRING) {
561 				(void) nvpair_value_string(pg_nvp, &pgn);
562 				match = strcmp(group, pgn) == 0;
563 				continue;
564 			}
565 
566 			if (strcmp(TOPO_PROP_GROUP_NSTAB,
567 			    nvpair_name(pg_nvp)) == 0 &&
568 			    nvpair_type(pg_nvp) == DATA_TYPE_STRING) {
569 				(void) nvpair_value_string(pg_nvp, &nstab);
570 				continue;
571 			}
572 
573 			if (strcmp(TOPO_PROP_GROUP_DSTAB,
574 			    nvpair_name(pg_nvp)) == 0 &&
575 			    nvpair_type(pg_nvp) == DATA_TYPE_STRING) {
576 				(void) nvpair_value_string(pg_nvp, &dstab);
577 				continue;
578 			}
579 
580 			if (strcmp(TOPO_PROP_GROUP_VERSION,
581 			    nvpair_name(pg_nvp)) == 0 &&
582 			    nvpair_type(pg_nvp) == DATA_TYPE_INT32) {
583 				(void) nvpair_value_int32(pg_nvp, &version);
584 				continue;
585 			}
586 
587 			if ((match || all) && !pg_done) {
588 				print_pgroup(thp, node, pgn, dstab, nstab,
589 				    version);
590 				pg_done++;
591 			}
592 
593 			/*
594 			 * Print property group and property name-value pair
595 			 */
596 			if (strcmp(TOPO_PROP_VAL, nvpair_name(pg_nvp))
597 			    == 0 && nvpair_type(pg_nvp) == DATA_TYPE_NVLIST) {
598 				(void) nvpair_value_nvlist(pg_nvp, &pv_nv);
599 				if ((match || all) && pg_done) {
600 					print_prop_nameval(thp, node, pv_nv);
601 				}
602 
603 			}
604 
605 		}
606 		if (match && !all)
607 			return;
608 	}
609 }
610 
611 static void
612 set_prop(topo_hdl_t *thp, tnode_t *node, nvlist_t *fmri, struct prop_args *pp)
613 {
614 	int ret, err = 0;
615 	topo_type_t type;
616 	nvlist_t *nvl = NULL;
617 	char *end;
618 
619 	if (pp->prop == NULL || pp->type == NULL || pp->value == NULL)
620 		goto out;
621 
622 	if ((type = str2type(pp->type)) == TOPO_TYPE_INVALID) {
623 		(void) fprintf(stderr, "%s: invalid property type %s for %s\n",
624 		    g_pname, pp->type, pp->prop);
625 		goto out;
626 	}
627 
628 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
629 		(void) fprintf(stderr, "%s: nvlist allocation failed for "
630 		    "%s=%s:%s\n", g_pname, pp->prop, pp->type, pp->value);
631 		goto out;
632 	}
633 	ret = nvlist_add_string(nvl, TOPO_PROP_VAL_NAME, pp->prop);
634 	ret |= nvlist_add_uint32(nvl, TOPO_PROP_VAL_TYPE, type);
635 	if (ret != 0) {
636 		(void) fprintf(stderr, "%s: invalid property type %s for %s\n",
637 		    g_pname, pp->type, pp->prop);
638 		goto out;
639 	}
640 
641 	errno = 0;
642 	switch (type) {
643 		case TOPO_TYPE_INT32:
644 		{
645 			int32_t val;
646 
647 			val = strtol(pp->value, &end, 0);
648 			if (errno == ERANGE) {
649 				ret = -1;
650 				break;
651 			}
652 			ret = nvlist_add_int32(nvl, TOPO_PROP_VAL_VAL, val);
653 			break;
654 		}
655 		case TOPO_TYPE_UINT32:
656 		{
657 			uint32_t val;
658 
659 			val = strtoul(pp->value, &end, 0);
660 			if (errno == ERANGE) {
661 				ret = -1;
662 				break;
663 			}
664 			ret = nvlist_add_uint32(nvl, TOPO_PROP_VAL_VAL, val);
665 			break;
666 		}
667 		case TOPO_TYPE_INT64:
668 		{
669 			int64_t val;
670 
671 			val = strtoll(pp->value, &end, 0);
672 			if (errno == ERANGE) {
673 				ret = -1;
674 				break;
675 			}
676 			ret = nvlist_add_int64(nvl, TOPO_PROP_VAL_VAL, val);
677 			break;
678 		}
679 		case TOPO_TYPE_UINT64:
680 		{
681 			uint64_t val;
682 
683 			val = strtoull(pp->value, &end, 0);
684 			if (errno == ERANGE) {
685 				ret = -1;
686 				break;
687 			}
688 			ret = nvlist_add_uint64(nvl, TOPO_PROP_VAL_VAL, val);
689 			break;
690 		}
691 		case TOPO_TYPE_STRING:
692 		{
693 			ret = nvlist_add_string(nvl, TOPO_PROP_VAL_VAL,
694 			    pp->value);
695 			break;
696 		}
697 		case TOPO_TYPE_FMRI:
698 		{
699 			nvlist_t *val = NULL;
700 
701 			if ((ret = topo_fmri_str2nvl(thp, pp->value, &val,
702 			    &err)) < 0)
703 				break;
704 
705 			if ((ret = nvlist_add_nvlist(nvl, TOPO_PROP_VAL_VAL,
706 			    val)) != 0)
707 				err = ETOPO_PROP_NVL;
708 
709 			nvlist_free(val);
710 			break;
711 		}
712 		default:
713 			ret = -1;
714 	}
715 
716 	if (ret != 0) {
717 		(void) fprintf(stderr, "%s: unable to set property value for "
718 		    "%s: %s\n", g_pname, pp->prop,  topo_strerror(err));
719 		goto out;
720 	}
721 
722 	if (node != NULL) {
723 		if ((ret = topo_prop_setprop(node, pp->group, nvl,
724 		    TOPO_PROP_MUTABLE, nvl, &err)) < 0) {
725 			(void) fprintf(stderr, "%s: unable to set property "
726 			    "value for " "%s=%s:%s: %s\n", g_pname, pp->prop,
727 			    pp->type, pp->value, topo_strerror(err));
728 			goto out;
729 		}
730 	} else {
731 		if ((ret = topo_fmri_setprop(thp, fmri,  pp->group, nvl,
732 		    TOPO_PROP_MUTABLE, nvl, &err)) < 0) {
733 			(void) fprintf(stderr, "%s: unable to set property "
734 			    "value for " "%s=%s:%s: %s\n", g_pname, pp->prop,
735 			    pp->type, pp->value, topo_strerror(err));
736 			goto out;
737 		}
738 	}
739 
740 	nvlist_free(nvl);
741 	nvl = NULL;
742 
743 	/*
744 	 * Now, get the property back for printing
745 	 */
746 	if (node != NULL) {
747 		if ((ret = topo_prop_getprop(node, pp->group, pp->prop, NULL,
748 		    &nvl, &err)) < 0) {
749 			(void) fprintf(stderr, "%s: failed to get %s.%s: %s\n",
750 			    g_pname, pp->group, pp->prop, topo_strerror(err));
751 			goto out;
752 		}
753 	} else {
754 		if ((ret = topo_fmri_getprop(thp, fmri, pp->group, pp->prop,
755 		    NULL, &nvl, &err)) < 0) {
756 			(void) fprintf(stderr, "%s: failed to get %s.%s: %s\n",
757 			    g_pname, pp->group, pp->prop, topo_strerror(err));
758 			goto out;
759 		}
760 	}
761 
762 	print_pgroup(thp, node, pp->group, NULL, NULL, 0);
763 	print_prop_nameval(thp, node, nvl);
764 
765 out:
766 	nvlist_free(nvl);
767 }
768 
769 static void
770 print_props(topo_hdl_t *thp, tnode_t *node)
771 {
772 	int i, err;
773 	nvlist_t *nvl;
774 	struct prop_args *pp;
775 
776 	if (pcnt == 0)
777 		return;
778 
779 	for (i = 0; i < pcnt; ++i) {
780 		pp = pargs[i];
781 
782 		if (pp->group == NULL)
783 			continue;
784 
785 		/*
786 		 * If we have a valid value, this is a request to
787 		 * set a property.  Otherwise, just print the property
788 		 * group and any specified properties.
789 		 */
790 		if (pp->value == NULL) {
791 			if (pp->prop == NULL) {
792 
793 				/*
794 				 * Print all properties in this group
795 				 */
796 				if ((nvl = topo_prop_getprops(node, &err))
797 				    == NULL) {
798 					(void) fprintf(stderr, "%s: failed to "
799 					    "get %s: %s\n", g_pname,
800 					    pp->group,
801 					    topo_strerror(err));
802 					continue;
803 				} else {
804 					print_all_props(thp, node, nvl,
805 					    pp->group);
806 					nvlist_free(nvl);
807 					continue;
808 				}
809 			}
810 			if (topo_prop_getprop(node, pp->group, pp->prop,
811 			    NULL, &nvl, &err) < 0) {
812 				(void) fprintf(stderr, "%s: failed to get "
813 				    "%s.%s: %s\n", g_pname,
814 				    pp->group, pp->prop,
815 				    topo_strerror(err));
816 				continue;
817 			} else {
818 				print_pgroup(thp, node, pp->group, NULL,
819 				    NULL, 0);
820 				print_prop_nameval(thp, node, nvl);
821 				nvlist_free(nvl);
822 			}
823 		} else {
824 			set_prop(thp, node, NULL, pp);
825 		}
826 	}
827 }
828 
829 /*ARGSUSED*/
830 static int
831 walk_node(topo_hdl_t *thp, tnode_t *node, void *arg)
832 {
833 	int err;
834 	nvlist_t *nvl;
835 	nvlist_t *rsrc, *out;
836 	char *s;
837 
838 	if (opt_e && strcmp(opt_s, FM_FMRI_SCHEME_HC) == 0) {
839 		print_everstyle(node);
840 		return (TOPO_WALK_NEXT);
841 	}
842 
843 	if (topo_node_resource(node, &rsrc, &err) < 0) {
844 		(void) fprintf(stderr, "%s: failed to get resource: "
845 		    "%s", g_pname, topo_strerror(err));
846 		return (TOPO_WALK_NEXT);
847 	}
848 	if (topo_fmri_nvl2str(thp, rsrc, &s, &err) < 0) {
849 		(void) fprintf(stderr, "%s: failed to convert "
850 		    "resource to FMRI string: %s", g_pname,
851 		    topo_strerror(err));
852 		nvlist_free(rsrc);
853 		return (TOPO_WALK_NEXT);
854 	}
855 
856 	if (g_fmri != NULL && fnmatch(g_fmri, s, 0) != 0) {
857 		nvlist_free(rsrc);
858 		topo_hdl_strfree(thp, s);
859 		return (TOPO_WALK_NEXT);
860 	}
861 
862 	print_node(thp, node, rsrc, s);
863 	topo_hdl_strfree(thp, s);
864 	nvlist_free(rsrc);
865 
866 	if (opt_m != NULL) {
867 		if (topo_method_invoke(node, opt_m, 0, NULL, &out, &err) == 0) {
868 			nvlist_print(stdout, out);
869 			nvlist_free(out);
870 		} else if (err != ETOPO_METHOD_NOTSUP)
871 			(void) fprintf(stderr, "%s: method failed unexpectedly "
872 			    "on %s=%d (%s)\n", g_pname, topo_node_name(node),
873 			    topo_node_instance(node), topo_strerror(err));
874 	}
875 
876 	if (opt_V || opt_all) {
877 		if ((nvl = topo_prop_getprops(node, &err)) == NULL) {
878 			(void) fprintf(stderr, "%s: failed to get "
879 			    "properties for %s=%d: %s\n", g_pname,
880 			    topo_node_name(node), topo_node_instance(node),
881 			    topo_strerror(err));
882 		} else {
883 			print_all_props(thp, node, nvl, ALL);
884 			nvlist_free(nvl);
885 		}
886 	} else if (pcnt > 0)
887 		print_props(thp, node);
888 
889 	(void) printf("\n");
890 
891 	return (TOPO_WALK_NEXT);
892 }
893 
894 static void
895 get_pargs(int argc, char *argv[])
896 {
897 	struct prop_args *pp;
898 	char c, *s, *p;
899 	int i = 0;
900 
901 	if ((pargs = malloc(sizeof (struct prop_args *) * pcnt)) == NULL) {
902 		(void) fprintf(stderr, "%s: failed to allocate property "
903 		    "arguments\n", g_pname);
904 		return;
905 	}
906 
907 	for (optind = 1; (c = getopt(argc, argv, optstr)) != EOF; ) {
908 		if (c == 'P') {
909 
910 			if (strcmp(optarg, ALL) == 0) {
911 				opt_all++;
912 				break;
913 			}
914 
915 			if ((pp = pargs[i] = malloc(sizeof (struct prop_args)))
916 			    == NULL) {
917 				(void) fprintf(stderr, "%s: failed to "
918 				    "allocate propertyarguments\n", g_pname);
919 				return;
920 			}
921 			++i;
922 			pp->group = NULL;
923 			pp->prop = NULL;
924 			pp->type = NULL;
925 			pp->value = NULL;
926 
927 			p = optarg;
928 			if ((s = strchr(p, '.')) != NULL) {
929 				*s++ = '\0'; /* strike out delimiter */
930 				pp->group = p;
931 				p = s;
932 				if ((s = strchr(p, '=')) != NULL) {
933 					*s++ = '\0'; /* strike out delimiter */
934 					pp->prop = p;
935 					p = s;
936 					if ((s = strchr(p, ':')) != NULL) {
937 						*s++ = '\0';
938 						pp->type = p;
939 						pp->value = s;
940 					} else {
941 						(void) fprintf(stderr, "%s: "
942 						    "property type not "
943 						    "specified for assignment "
944 						    " of %s.%s\n", g_pname,
945 						    pp->group, pp->prop);
946 						break;
947 					}
948 				} else {
949 					pp->prop = p;
950 				}
951 			} else {
952 				pp->group = p;
953 			}
954 			if (i >= pcnt)
955 				break;
956 		}
957 	}
958 
959 	if (opt_all > 0) {
960 		int j;
961 
962 		for (j = 0; j < i; ++j)
963 			free(pargs[i]);
964 		free(pargs);
965 		pargs = NULL;
966 	}
967 }
968 
969 static int
970 walk_schemes_cb(topo_hdl_t *thp, const topo_scheme_info_t *info, void *arg)
971 {
972 	const char *type;
973 	char unknown[32];
974 
975 	if (g_fmri != NULL && fnmatch(g_fmri, info->tsi_scheme, 0) != 0) {
976 		return (TOPO_WALK_NEXT);
977 	}
978 
979 	switch (info->tsi_type) {
980 	case TOPO_SCHEME_TREE:
981 		type = "tree";
982 		break;
983 	case TOPO_SCHEME_DIGRAPH:
984 		type = "directed graph";
985 		break;
986 	default:
987 		(void) snprintf(unknown, sizeof (unknown), "unknown (0x%x)",
988 		    info->tsi_type);
989 		type = unknown;
990 		break;
991 	}
992 
993 	(void) printf("%-15s %s\n", type, info->tsi_scheme);
994 	return (TOPO_WALK_NEXT);
995 }
996 
997 static int
998 walk_topo(topo_hdl_t *thp, char *uuid)
999 {
1000 	int err;
1001 	topo_walk_t *twp;
1002 	int flag;
1003 
1004 	if (getzoneid() != GLOBAL_ZONEID &&
1005 	    strcmp(opt_s, FM_FMRI_SCHEME_HC) == 0) {
1006 		return (0);
1007 	}
1008 
1009 	if ((twp = topo_walk_init(thp, opt_s, walk_node, NULL, &err))
1010 	    == NULL) {
1011 		(void) fprintf(stderr, "%s: failed to walk %s topology:"
1012 		    " %s\n", g_pname, opt_s, topo_strerror(err));
1013 
1014 		return (-1);
1015 	}
1016 
1017 	/*
1018 	 * Print standard header
1019 	 */
1020 	if (!opt_e) {
1021 		char buf[32];
1022 		time_t tod = time(NULL);
1023 
1024 		(void) printf("TIME                 UUID\n");
1025 		(void) strftime(buf, sizeof (buf), "%b %d %T", localtime(&tod));
1026 		(void) printf("%-15s %-32s\n", buf, uuid);
1027 		(void) printf("\n");
1028 	}
1029 
1030 	flag = opt_b != 0 ? TOPO_WALK_SIBLING : TOPO_WALK_CHILD;
1031 
1032 	if (topo_walk_step(twp, flag) == TOPO_WALK_ERR) {
1033 		(void) fprintf(stderr, "%s: failed to walk topology\n",
1034 		    g_pname);
1035 		topo_walk_fini(twp);
1036 		return (-1);
1037 	}
1038 
1039 	topo_walk_fini(twp);
1040 
1041 	return (0);
1042 }
1043 
1044 static void
1045 print_fmri_pgroup(topo_hdl_t *thp, const char *pgn, nvlist_t *nvl)
1046 {
1047 	char *dstab = NULL, *nstab = NULL;
1048 	int32_t version = -1;
1049 	nvlist_t *pnvl;
1050 	nvpair_t *pnvp;
1051 
1052 	(void) nvlist_lookup_string(nvl, TOPO_PROP_GROUP_NSTAB, &nstab);
1053 	(void) nvlist_lookup_string(nvl, TOPO_PROP_GROUP_DSTAB, &dstab);
1054 	(void) nvlist_lookup_int32(nvl, TOPO_PROP_GROUP_VERSION, &version);
1055 
1056 	print_pgroup(thp, NULL, pgn, dstab, nstab, version);
1057 
1058 	for (pnvp = nvlist_next_nvpair(nvl, NULL); pnvp != NULL;
1059 	    pnvp = nvlist_next_nvpair(nvl, pnvp)) {
1060 
1061 		/*
1062 		 * Print property group and property name-value pair
1063 		 */
1064 		if (strcmp(TOPO_PROP_VAL, nvpair_name(pnvp))
1065 		    == 0 && nvpair_type(pnvp) == DATA_TYPE_NVLIST) {
1066 			(void) nvpair_value_nvlist(pnvp, &pnvl);
1067 				print_prop_nameval(thp, NULL, pnvl);
1068 
1069 		}
1070 
1071 	}
1072 }
1073 
1074 static void
1075 print_fmri_props(topo_hdl_t *thp, nvlist_t *nvl)
1076 {
1077 	int i, err;
1078 	struct prop_args *pp;
1079 	nvlist_t *pnvl;
1080 
1081 	for (i = 0; i < pcnt; ++i) {
1082 		pp = pargs[i];
1083 
1084 		if (pp->group == NULL)
1085 			continue;
1086 
1087 		pnvl = NULL;
1088 
1089 		/*
1090 		 * If we have a valid value, this is a request to
1091 		 * set a property.  Otherwise, just print the property
1092 		 * group and any specified properties.
1093 		 */
1094 		if (pp->value == NULL) {
1095 			if (pp->prop == NULL) {
1096 
1097 				/*
1098 				 * Print all properties in this group
1099 				 */
1100 				if (topo_fmri_getpgrp(thp, nvl, pp->group,
1101 				    &pnvl, &err) < 0) {
1102 					(void) fprintf(stderr, "%s: failed to "
1103 					    "get group %s: %s\n", g_pname,
1104 					    pp->group, topo_strerror(err));
1105 					continue;
1106 				} else {
1107 					print_fmri_pgroup(thp, pp->group,
1108 					    pnvl);
1109 					nvlist_free(pnvl);
1110 					continue;
1111 				}
1112 			}
1113 			if (topo_fmri_getprop(thp, nvl, pp->group, pp->prop,
1114 			    NULL, &pnvl, &err) < 0) {
1115 				(void) fprintf(stderr, "%s: failed to get "
1116 				    "%s.%s: %s\n", g_pname,
1117 				    pp->group, pp->prop,
1118 				    topo_strerror(err));
1119 				continue;
1120 			} else {
1121 				print_fmri_pgroup(thp, pp->group, pnvl);
1122 				print_prop_nameval(thp, NULL, pnvl);
1123 				nvlist_free(nvl);
1124 			}
1125 		} else {
1126 			set_prop(thp, NULL, nvl, pp);
1127 		}
1128 	}
1129 }
1130 
1131 void
1132 print_fmri(topo_hdl_t *thp, char *uuid)
1133 {
1134 	int ret, err;
1135 	nvlist_t *nvl;
1136 	char buf[32];
1137 	time_t tod = time(NULL);
1138 
1139 	if (topo_fmri_str2nvl(thp, g_fmri, &nvl, &err) < 0) {
1140 		(void) fprintf(stderr, "%s: failed to convert %s to nvlist: "
1141 		    "%s\n", g_pname, g_fmri, topo_strerror(err));
1142 		return;
1143 	}
1144 
1145 	(void) printf("TIME                 UUID\n");
1146 	(void) strftime(buf, sizeof (buf), "%b %d %T", localtime(&tod));
1147 	(void) printf("%-15s %-32s\n", buf, uuid);
1148 	(void) printf("\n");
1149 
1150 	(void) printf("%s\n", (char *)g_fmri);
1151 
1152 	if (opt_p && !(pcnt > 0 || opt_V || opt_all)) {
1153 		char *aname = NULL, *fname = NULL, *lname = NULL;
1154 		nvlist_t *asru = NULL;
1155 		nvlist_t *fru = NULL;
1156 
1157 		if (topo_fmri_asru(thp, nvl, &asru, &err) == 0)
1158 			(void) topo_fmri_nvl2str(thp, asru, &aname, &err);
1159 		if (topo_fmri_fru(thp, nvl, &fru, &err) == 0)
1160 			(void) topo_fmri_nvl2str(thp, fru, &fname, &err);
1161 		(void) topo_fmri_label(thp, nvl, &lname, &err);
1162 
1163 		nvlist_free(fru);
1164 		nvlist_free(asru);
1165 
1166 		if (aname != NULL) {
1167 			(void) printf("\tASRU: %s\n", aname);
1168 			topo_hdl_strfree(thp, aname);
1169 		} else {
1170 			(void) printf("\tASRU: -\n");
1171 		}
1172 		if (fname != NULL) {
1173 			(void) printf("\tFRU: %s\n", fname);
1174 			topo_hdl_strfree(thp, fname);
1175 		} else {
1176 			(void) printf("\tFRU: -\n");
1177 		}
1178 		if (lname != NULL) {
1179 			(void) printf("\tLabel: %s\n", lname);
1180 			topo_hdl_strfree(thp, lname);
1181 		} else {
1182 			(void) printf("\tLabel: -\n");
1183 		}
1184 	}
1185 
1186 	if (opt_S) {
1187 		if (topo_fmri_str2nvl(thp, g_fmri, &nvl, &err) < 0) {
1188 			(void) printf("\tPresent: -\n");
1189 			(void) printf("\tUnusable: -\n");
1190 			return;
1191 		}
1192 
1193 		if ((ret = topo_fmri_present(thp, nvl, &err)) < 0)
1194 			(void) printf("\tPresent: -\n");
1195 		else
1196 			(void) printf("\tPresent: %s\n",
1197 			    ret ? "true" : "false");
1198 
1199 		if ((ret = topo_fmri_unusable(thp, nvl, &err)) < 0)
1200 			(void) printf("\tUnusable: -\n");
1201 		else
1202 			(void) printf("\tUnusable: %s\n",
1203 			    ret ? "true" : "false");
1204 
1205 		nvlist_free(nvl);
1206 	}
1207 
1208 	if (pargs && pcnt > 0)
1209 		print_fmri_props(thp, nvl);
1210 }
1211 
1212 int
1213 fmtopo_exit(topo_hdl_t *thp, char *uuid, int err)
1214 {
1215 	if (uuid != NULL)
1216 		topo_hdl_strfree(thp, uuid);
1217 
1218 	if (thp != NULL) {
1219 		topo_snap_release(thp);
1220 		topo_close(thp);
1221 	}
1222 
1223 	if (pargs) {
1224 		int i;
1225 		for (i = 0; i < pcnt; ++i)
1226 			free(pargs[i]);
1227 		free(pargs);
1228 	}
1229 
1230 	return (err);
1231 }
1232 
1233 int
1234 main(int argc, char *argv[])
1235 {
1236 	topo_hdl_t *thp = NULL;
1237 	char *uuid = NULL;
1238 	int c, err = 0;
1239 
1240 	g_pname = argv[0];
1241 
1242 	while (optind < argc) {
1243 		while ((c = getopt(argc, argv, optstr)) != -1) {
1244 			switch (c) {
1245 			case 'b':
1246 				opt_b++;
1247 				break;
1248 			case 'C':
1249 				(void) atexit(abort);
1250 				break;
1251 			case 'd':
1252 				opt_d++;
1253 				break;
1254 			case 'e':
1255 				opt_e++;
1256 				break;
1257 			case 'l':
1258 				opt_l++;
1259 				break;
1260 			case 'm':
1261 				opt_m = optarg;
1262 				break;
1263 			case 'P':
1264 				pcnt++;
1265 				break;
1266 			case 'p':
1267 				opt_p++;
1268 				break;
1269 			case 'V':
1270 				opt_V++;
1271 				break;
1272 			case 'R':
1273 				opt_R = optarg;
1274 				break;
1275 			case 's':
1276 				opt_s = optarg;
1277 				break;
1278 			case 'S':
1279 				opt_S++;
1280 				break;
1281 			case 't':
1282 				opt_t++;
1283 				break;
1284 			case 'x':
1285 				opt_x++;
1286 				break;
1287 			default:
1288 				return (usage(stderr));
1289 			}
1290 		}
1291 
1292 		if (optind < argc) {
1293 			if (g_fmri != NULL) {
1294 				(void) fprintf(stderr, "%s: illegal argument "
1295 				    "-- %s\n", g_pname, argv[optind]);
1296 				return (FMTOPO_EXIT_USAGE);
1297 			} else {
1298 				g_fmri = argv[optind++];
1299 			}
1300 		}
1301 	}
1302 
1303 	if (pcnt > 0)
1304 		get_pargs(argc, argv);
1305 
1306 	if ((thp = topo_open(TOPO_VERSION, opt_R, &err)) == NULL) {
1307 		(void) fprintf(stderr, "%s: failed to open topology tree: %s\n",
1308 		    g_pname, topo_strerror(err));
1309 		return (fmtopo_exit(thp, uuid, FMTOPO_EXIT_ERROR));
1310 	}
1311 
1312 	if (opt_d)
1313 		topo_debug_set(thp, "module", "stderr");
1314 
1315 	if ((uuid = topo_snap_hold(thp, NULL, &err)) == NULL) {
1316 		(void) fprintf(stderr, "%s: failed to snapshot topology: %s\n",
1317 		    g_pname, topo_strerror(err));
1318 		return (fmtopo_exit(thp, uuid, FMTOPO_EXIT_ERROR));
1319 	} else if (err != 0) {
1320 		(void) fprintf(stderr, "%s: topology snapshot incomplete%s\n",
1321 		    g_pname, getzoneid() != GLOBAL_ZONEID &&
1322 		    strcmp(opt_s, FM_FMRI_SCHEME_HC) == 0 ?
1323 		    " (" FM_FMRI_SCHEME_HC " scheme does not enumerate "
1324 		    "in a non-global zone)": "");
1325 	}
1326 
1327 	if (opt_x) {
1328 		if (opt_b) {
1329 			(void) fprintf(stderr,
1330 			    "%s: -b and -x cannot be specified together\n",
1331 			    g_pname);
1332 			return (fmtopo_exit(thp, uuid, FMTOPO_EXIT_USAGE));
1333 		}
1334 
1335 		if (opt_l) {
1336 			(void) fprintf(stderr,
1337 			    "%s: -l and -x cannot be specified together\n",
1338 			    g_pname);
1339 			return (fmtopo_exit(thp, uuid, FMTOPO_EXIT_USAGE));
1340 		}
1341 
1342 		err = 0;
1343 		if (topo_xml_print(thp, stdout, opt_s, &err) < 0)
1344 			(void) fprintf(stderr, "%s: failed to print xml "
1345 			    "formatted topology:%s",  g_pname,
1346 			    topo_strerror(err));
1347 
1348 		return (fmtopo_exit(thp, uuid, err ? FMTOPO_EXIT_ERROR :
1349 		    FMTOPO_EXIT_SUCCESS));
1350 	}
1351 
1352 	if (opt_l) {
1353 		if (opt_b || opt_e || opt_m || pcnt > 0 || opt_p || opt_V ||
1354 		    opt_S || opt_x) {
1355 			(void) fprintf(stderr,
1356 			    "%s: -l may only be used with -c, -d, and -R\n",
1357 			    g_pname);
1358 			return (fmtopo_exit(thp, uuid, FMTOPO_EXIT_USAGE));
1359 		}
1360 
1361 		(void) printf("%-15s %s\n", "TYPE", "NAME");
1362 		(void) topo_scheme_walk(thp, walk_schemes_cb, NULL);
1363 		return (fmtopo_exit(thp, uuid, FMTOPO_EXIT_SUCCESS));
1364 	}
1365 
1366 	if (opt_t || walk_topo(thp, uuid) < 0) {
1367 		if (g_fmri != NULL)
1368 			/*
1369 			 * Try getting some useful information
1370 			 */
1371 			print_fmri(thp, uuid);
1372 
1373 		return (fmtopo_exit(thp, uuid, FMTOPO_EXIT_ERROR));
1374 	}
1375 
1376 	return (fmtopo_exit(thp, uuid, FMTOPO_EXIT_SUCCESS));
1377 }
1378