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