xref: /illumos-gate/usr/src/cmd/fm/fmtopo/common/fmtopo.c (revision 0eb822a1c0c2bea495647510b75f77f0e57633eb)
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 2006 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <sys/fm/protocol.h>
30 #include <fm/libtopo.h>
31 #include <ctype.h>
32 #include <limits.h>
33 #include <strings.h>
34 #include <stdio.h>
35 #include <errno.h>
36 #include <sys/param.h>
37 
38 #define	FMTOPO_EXIT_SUCCESS	0
39 #define	FMTOPO_EXIT_ERROR	1
40 #define	FMTOPO_EXIT_USAGE	2
41 
42 #define	STDERR	"stderr"
43 #define	DOTS	"..."
44 #define	ALL	"all"
45 
46 static const char *g_pname;
47 
48 static const char *opt_R = "/";
49 static const char *opt_P = NULL;
50 static const char *opt_s = FM_FMRI_SCHEME_HC;
51 
52 static int opt_e = 0;
53 static int opt_d = 0;
54 static int opt_V = 0;
55 static int opt_p = 0;
56 static int opt_x = 0;
57 
58 static int
59 usage(FILE *fp)
60 {
61 	(void) fprintf(fp,
62 	    "Usage: %s [-edpvVx] [-Cdev] [-P properties] [-R root] "
63 		    "[-s scheme]\n", g_pname);
64 
65 	(void) fprintf(fp,
66 	    "\t-C  dump core after completing execution\n"
67 	    "\t-d  set debug mode for libtopo modules\n"
68 	    "\t-e  display FMRIs as paths using esc/eft notation\n"
69 	    "\t-P  display of FMRI with the specified properties\n"
70 	    "\t-p  display of FMRI protocol properties\n"
71 	    "\t-R  set root directory for libtopo plug-ins and other files\n"
72 	    "\t-s  display topology for the specified FMRI scheme\n"
73 	    "\t-V  set verbose mode\n"
74 	    "\t-x  display a xml formatted topology\n");
75 
76 	return (FMTOPO_EXIT_USAGE);
77 }
78 
79 static void
80 print_fmri(topo_hdl_t *thp, tnode_t *node)
81 {
82 	int err;
83 	char *name;
84 	nvlist_t *fmri;
85 
86 	if (topo_node_resource(node, &fmri, &err) < 0) {
87 		(void) fprintf(stderr, "%s: failed to get fmri for %s=%d: %s\n",
88 		    g_pname, topo_node_name(node),
89 		    topo_node_instance(node), topo_strerror(err));
90 		return;
91 	}
92 
93 	if (topo_fmri_nvl2str(thp, fmri, &name, &err) < 0) {
94 		(void) fprintf(stderr, "%s: failed to convert fmri for %s=%d "
95 		    "to a string: %s\n", g_pname, topo_node_name(node),
96 		    topo_node_instance(node), topo_strerror(err));
97 		nvlist_free(fmri);
98 		return;
99 	}
100 
101 	(void) printf("%s\n", name);
102 
103 	if (opt_p) {
104 		char *aname = NULL, *fname = NULL, *lname = NULL;
105 		nvlist_t *asru = NULL;
106 		nvlist_t *fru = NULL;
107 
108 		if (topo_node_asru(node, &asru, NULL, &err) == 0)
109 		    (void) topo_fmri_nvl2str(thp, asru, &aname, &err);
110 		if (topo_node_fru(node, &fru, NULL, &err) == 0)
111 		    (void) topo_fmri_nvl2str(thp, fru, &fname, &err);
112 		(void) topo_node_label(node, &lname, &err);
113 		if (aname != NULL) {
114 			nvlist_free(asru);
115 			(void) printf("\tASRU: %s\n", aname);
116 			topo_hdl_strfree(thp, aname);
117 		} else {
118 			(void) printf("\tASRU: -\n");
119 		}
120 		if (fname != NULL) {
121 			nvlist_free(fru);
122 			(void) printf("\tFRU: %s\n", fname);
123 			topo_hdl_strfree(thp, fname);
124 		} else {
125 			(void) printf("\tFRU: -\n");
126 		}
127 		if (lname != NULL) {
128 			(void) printf("\tLabel: %s\n", lname);
129 			topo_hdl_strfree(thp, lname);
130 		} else {
131 			(void) printf("\tLabel: -\n");
132 		}
133 	}
134 	nvlist_free(fmri);
135 
136 	topo_hdl_strfree(thp, name);
137 }
138 
139 static void
140 print_everstyle(tnode_t *node)
141 {
142 	char buf[PATH_MAX], numbuf[64];
143 	nvlist_t *fmri, **hcl;
144 	int i, err;
145 	uint_t n;
146 
147 	if (topo_prop_get_fmri(node, TOPO_PGROUP_PROTOCOL,
148 	    TOPO_PROP_RESOURCE, &fmri, &err) < 0) {
149 		(void) fprintf(stderr, "%s: failed to get fmri for %s=%d: %s\n",
150 		    g_pname, topo_node_name(node),
151 		    topo_node_instance(node), topo_strerror(err));
152 		return;
153 	}
154 
155 	if (nvlist_lookup_nvlist_array(fmri, FM_FMRI_HC_LIST, &hcl, &n) != 0) {
156 		(void) fprintf(stderr, "%s: failed to find %s for %s=%d\n",
157 		    g_pname, FM_FMRI_HC_LIST, topo_node_name(node),
158 		    topo_node_instance(node));
159 		return;
160 	}
161 
162 	buf[0] = '\0';
163 
164 	for (i = 0; i < n; i++) {
165 		char *name, *inst, *estr;
166 		ulong_t ul;
167 
168 		if (nvlist_lookup_string(hcl[i], FM_FMRI_HC_NAME, &name) != 0 ||
169 		    nvlist_lookup_string(hcl[i], FM_FMRI_HC_ID, &inst) != 0) {
170 			(void) fprintf(stderr, "%s: failed to get "
171 			    "name-instance for %s=%d\n", g_pname,
172 			    topo_node_name(node), topo_node_instance(node));
173 			return;
174 		}
175 
176 		errno = 0;
177 		ul = strtoul(inst, &estr, 10);
178 
179 		if (errno != 0 || estr == inst) {
180 			(void) fprintf(stderr, "%s: instance %s does not "
181 			    "convert to an unsigned integer\n", g_pname, inst);
182 		}
183 
184 		(void) strlcat(buf, "/", sizeof (buf));
185 		(void) strlcat(buf, name, sizeof (buf));
186 		(void) snprintf(numbuf, sizeof (numbuf), "%u", ul);
187 		(void) strlcat(buf, numbuf, sizeof (buf));
188 	}
189 
190 	(void) printf("%s\n", buf);
191 }
192 
193 static void
194 print_prop_nameval(topo_hdl_t *thp, nvlist_t *nvl, int skip)
195 {
196 	int err;
197 	topo_type_t type;
198 	char *tstr, *propn, buf[48];
199 	nvpair_t *pv_nvp;
200 
201 	if ((pv_nvp = nvlist_next_nvpair(nvl, NULL)) == NULL)
202 		return;
203 
204 	/* Print property name */
205 	if ((pv_nvp = nvlist_next_nvpair(nvl, NULL)) == NULL ||
206 	    nvpair_name(pv_nvp) == NULL ||
207 	    strcmp(TOPO_PROP_VAL_NAME, nvpair_name(pv_nvp)) != 0) {
208 		(void) fprintf(stderr, "%s: malformed property name\n",
209 		    g_pname);
210 		return;
211 	} else {
212 	    (void) nvpair_value_string(pv_nvp, &propn);
213 	}
214 
215 	if ((pv_nvp = nvlist_next_nvpair(nvl, pv_nvp)) == NULL ||
216 	    nvpair_name(pv_nvp) == NULL ||
217 	    strcmp(nvpair_name(pv_nvp), TOPO_PROP_VAL_TYPE) != 0 ||
218 	    nvpair_type(pv_nvp) != DATA_TYPE_INT32)  {
219 		(void) fprintf(stderr, "%s: malformed property type for %s\n",
220 		    g_pname, propn);
221 		return;
222 	} else {
223 		(void) nvpair_value_int32(pv_nvp, (int32_t *)&type);
224 	}
225 
226 	switch (type) {
227 		case TOPO_TYPE_BOOLEAN: tstr = "boolean"; break;
228 		case TOPO_TYPE_INT32: tstr = "int32"; break;
229 		case TOPO_TYPE_UINT32: tstr = "uint32"; break;
230 		case TOPO_TYPE_INT64: tstr = "int64"; break;
231 		case TOPO_TYPE_UINT64: tstr = "uint64"; break;
232 		case TOPO_TYPE_STRING: tstr = "string"; break;
233 		case TOPO_TYPE_FMRI: tstr = "fmri"; break;
234 		case TOPO_TYPE_INT32_ARRAY: tstr = "int32[]"; break;
235 		case TOPO_TYPE_UINT32_ARRAY: tstr = "uint32[]"; break;
236 		case TOPO_TYPE_INT64_ARRAY: tstr = "int64[]"; break;
237 		case TOPO_TYPE_UINT64_ARRAY: tstr = "uint64[]"; break;
238 		case TOPO_TYPE_STRING_ARRAY: tstr = "string[]"; break;
239 		case TOPO_TYPE_FMRI_ARRAY: tstr = "fmri[]"; break;
240 		default: tstr = "unknown type";
241 	}
242 
243 	if (!skip)
244 		printf("    %-17s %-8s ", propn, tstr);
245 
246 	/*
247 	 * Get property value
248 	 */
249 	if (nvpair_name(pv_nvp) == NULL ||
250 	    (pv_nvp = nvlist_next_nvpair(nvl, pv_nvp)) == NULL) {
251 		(void) fprintf(stderr, "%s: malformed property value\n",
252 		    g_pname);
253 		return;
254 	}
255 
256 	if (skip)
257 		return;
258 
259 	switch (nvpair_type(pv_nvp)) {
260 		case DATA_TYPE_INT32: {
261 			int32_t val;
262 			(void) nvpair_value_int32(pv_nvp, &val);
263 			(void) printf(" %d", val);
264 			break;
265 		}
266 		case DATA_TYPE_UINT32: {
267 			uint32_t val;
268 			(void) nvpair_value_uint32(pv_nvp, &val);
269 			(void) printf(" 0x%x", val);
270 			break;
271 		}
272 		case DATA_TYPE_INT64: {
273 			int64_t val;
274 			(void) nvpair_value_int64(pv_nvp, &val);
275 			(void) printf(" %lld", (longlong_t)val);
276 			break;
277 		}
278 		case DATA_TYPE_UINT64: {
279 			uint64_t val;
280 			(void) nvpair_value_uint64(pv_nvp, &val);
281 			(void) printf(" 0x%llx", (u_longlong_t)val);
282 			break;
283 		}
284 		case DATA_TYPE_STRING: {
285 			char *val;
286 			(void) nvpair_value_string(pv_nvp, &val);
287 			if (!opt_V && strlen(val) > 48) {
288 				(void) snprintf(buf, 48, "%s...", val);
289 				(void) printf(" %s", buf);
290 			} else {
291 				(void) printf(" %s", val);
292 			}
293 			break;
294 		}
295 		case DATA_TYPE_NVLIST: {
296 			nvlist_t *val;
297 			char *fmri;
298 			(void) nvpair_value_nvlist(pv_nvp, &val);
299 			if (topo_fmri_nvl2str(thp, val, &fmri, &err) != 0) {
300 				if (opt_V)
301 					nvlist_print(stdout, nvl);
302 				break;
303 			}
304 
305 			if (!opt_V && strlen(fmri) > 48) {
306 				(void) snprintf(buf, 48, "%s", fmri);
307 				(void) snprintf(&buf[45], 4, "%s", DOTS);
308 				(void) printf(" %s", buf);
309 			} else {
310 				(void) printf(" %s", fmri);
311 			}
312 
313 			topo_hdl_strfree(thp, fmri);
314 			break;
315 		}
316 		default:
317 			(void) fprintf(stderr, " unknown data type (%d)",
318 			    nvpair_type(pv_nvp));
319 			break;
320 		}
321 		(void) printf("\n");
322 }
323 
324 static void
325 print_pgroup(char *pgn, char *dstab, char *nstab, int32_t version, int skip)
326 {
327 	char buf[30];
328 
329 	if (skip)
330 		return;
331 
332 	if (!opt_V && strlen(pgn) > 30) {
333 		(void) snprintf(buf, 26, "%s", pgn);
334 		(void) snprintf(&buf[27], 4, "%s", DOTS);
335 		printf("  group: %-30s version: %-3d stability: %s/%s\n",
336 		    buf, version, nstab, dstab);
337 	} else {
338 		printf("  group: %-30s version: %-3d stability: %s/%s\n",
339 		    pgn, version, nstab, dstab);
340 	}
341 }
342 
343 static int
344 cmp_name(const char *props, char *pgn)
345 {
346 	char buf[MAXNAMELEN];
347 	size_t count;
348 	char *begin, *end, *value, *next;
349 	char *np;
350 
351 	if (props == NULL)
352 		return (0);
353 
354 	if (strcmp(props, ALL) == 0)
355 		return (0);
356 
357 
358 	value = np = strdup(props);
359 
360 	for (end = np; *end != '\0'; value = next) {
361 		end = strchr(value, ',');
362 		if (end != NULL)
363 			next = end + 1; /* skip the comma */
364 		else
365 			next = end = value + strlen(value);
366 
367 		/*
368 		 * Eat up white space at beginning or end of the
369 		 * property group name
370 		 */
371 		begin = value;
372 		while (begin < end && isspace(*begin))
373 			begin++;
374 		while (begin < end && isspace(*(end - 1)))
375 			end--;
376 
377 		if (begin >= end)
378 			return (1);
379 
380 		count = end - begin;
381 		count += 1;
382 
383 		if (count > sizeof (buf))
384 			return (1);
385 
386 		(void) snprintf(buf, count, "%s", begin);
387 		if (strcmp(pgn, buf) == 0) {
388 			free(np);
389 			return (0);
390 		}
391 	}
392 
393 	free(np);
394 	return (1);
395 }
396 
397 static void
398 print_props(topo_hdl_t *thp, nvlist_t *p_nv, const char *props)
399 {
400 	char *pgn = NULL, *dstab = NULL, *nstab = NULL;
401 	int32_t version = 0;
402 	nvlist_t *pg_nv, *pv_nv;
403 	nvpair_t *nvp, *pg_nvp;
404 	int pg_done = 0, skip = 0;
405 
406 	for (nvp = nvlist_next_nvpair(p_nv, NULL); nvp != NULL;
407 	    nvp = nvlist_next_nvpair(p_nv, nvp)) {
408 		if (strcmp(TOPO_PROP_GROUP, nvpair_name(nvp)) != 0 ||
409 		    nvpair_type(nvp) != DATA_TYPE_NVLIST)
410 			continue;
411 
412 		(void) nvpair_value_nvlist(nvp, &pg_nv);
413 		for (pg_nvp = nvlist_next_nvpair(pg_nv, NULL); pg_nvp != NULL;
414 		    pg_nvp = nvlist_next_nvpair(pg_nv, pg_nvp)) {
415 			/*
416 			 * Print property group name and stability levels
417 			 */
418 			if (strcmp(TOPO_PROP_GROUP_NAME, nvpair_name(pg_nvp))
419 			    == 0 && nvpair_type(pg_nvp) == DATA_TYPE_STRING) {
420 				(void) nvpair_value_string(pg_nvp, &pgn);
421 
422 				skip = cmp_name(props, pgn);
423 
424 			} else if (strcmp(TOPO_PROP_GROUP_NSTAB,
425 			    nvpair_name(pg_nvp)) == 0 &&
426 			    nvpair_type(pg_nvp) == DATA_TYPE_STRING) {
427 				(void) nvpair_value_string(pg_nvp, &nstab);
428 			} else if (strcmp(TOPO_PROP_GROUP_DSTAB,
429 			    nvpair_name(pg_nvp)) == 0 &&
430 			    nvpair_type(pg_nvp) == DATA_TYPE_STRING) {
431 				(void) nvpair_value_string(pg_nvp, &dstab);
432 			} else if (strcmp(TOPO_PROP_GROUP_VERSION,
433 			    nvpair_name(pg_nvp)) == 0 &&
434 			    nvpair_type(pg_nvp) == DATA_TYPE_INT32) {
435 				(void) nvpair_value_int32(pg_nvp, &version);
436 			}
437 
438 			if (!pg_done) {
439 				if (pgn && dstab && nstab && version) {
440 					print_pgroup(pgn, dstab, nstab,
441 					    version, skip);
442 					pg_done++;
443 				} else {
444 					continue;
445 				}
446 			/*
447 			 * Print property name-value pair
448 			 */
449 			} else if (strcmp(TOPO_PROP_VAL, nvpair_name(pg_nvp))
450 			    == 0 && nvpair_type(pg_nvp) == DATA_TYPE_NVLIST) {
451 				(void) nvpair_value_nvlist(pg_nvp, &pv_nv);
452 				print_prop_nameval(thp, pv_nv, skip);
453 
454 			}
455 		}
456 		pg_done = 0;
457 		skip = 0;
458 	}
459 }
460 
461 /*ARGSUSED*/
462 static int
463 print_tnode(topo_hdl_t *thp, tnode_t *node, void *arg)
464 {
465 	int err;
466 	nvlist_t *nvl;
467 
468 	if (opt_e && strcmp(opt_s, FM_FMRI_SCHEME_HC) == 0) {
469 		print_everstyle(node);
470 		return (TOPO_WALK_NEXT);
471 	}
472 
473 	print_fmri(thp, node);
474 
475 	if (opt_V || opt_P) {
476 		if ((nvl = topo_prop_getprops(node, &err)) == NULL) {
477 			(void) fprintf(stderr, "%s: failed to get "
478 			    "properties for %s=%d: %s\n", g_pname,
479 			    topo_node_name(node), topo_node_instance(node),
480 			    topo_strerror(err));
481 		} else {
482 			print_props(thp, nvl, opt_P);
483 			nvlist_free(nvl);
484 		}
485 	}
486 
487 	printf("\n");
488 
489 	return (TOPO_WALK_NEXT);
490 }
491 
492 int
493 main(int argc, char *argv[])
494 {
495 	topo_hdl_t *thp;
496 	topo_walk_t *twp;
497 	char *uuid;
498 	int c, err = 0;
499 
500 	g_pname = argv[0];
501 
502 	while (optind < argc) {
503 		while ((c = getopt(argc, argv, "aCdeP:pR:s:vVx")) != -1) {
504 			switch (c) {
505 			case 'C':
506 				atexit(abort);
507 				break;
508 			case 'd':
509 				opt_d++;
510 				break;
511 			case 'e':
512 				opt_e++;
513 				break;
514 			case 'P':
515 				opt_P = optarg;
516 				break;
517 			case 'p':
518 				opt_p++;
519 				break;
520 			case 'V':
521 				opt_V++;
522 				break;
523 			case 'R':
524 				opt_R = optarg;
525 				break;
526 			case 's':
527 				opt_s = optarg;
528 				break;
529 			case 'x':
530 				opt_x++;
531 				break;
532 			default:
533 				return (usage(stderr));
534 			}
535 		}
536 
537 		if (optind < argc) {
538 			(void) fprintf(stderr, "%s: illegal argument -- %s\n",
539 			    g_pname, argv[optind]);
540 			return (FMTOPO_EXIT_USAGE);
541 		}
542 	}
543 
544 	if ((thp = topo_open(TOPO_VERSION, opt_R, &err)) == NULL) {
545 		(void) fprintf(stderr, "%s: failed to open topology tree: %s\n",
546 		    g_pname, topo_strerror(err));
547 		return (FMTOPO_EXIT_ERROR);
548 	}
549 
550 	if (opt_d)
551 		topo_debug_set(thp, "module", "stderr");
552 
553 	if ((uuid = topo_snap_hold(thp, NULL, &err)) == NULL) {
554 		(void) fprintf(stderr, "%s: failed to snapshot topology: %s\n",
555 		    g_pname, topo_strerror(err));
556 		topo_close(thp);
557 		return (FMTOPO_EXIT_ERROR);
558 	} else if (err != 0) {
559 		(void) fprintf(stderr, "%s: topology snapshot incomplete\n",
560 		    g_pname);
561 	}
562 
563 
564 	if (opt_x) {
565 		err = 0;
566 		if (topo_xml_print(thp, stdout, opt_s, &err) < 0)
567 			(void) fprintf(stderr, "%s: failed to print xml "
568 			    "formatted topology:%s",  g_pname,
569 			    topo_strerror(err));
570 
571 		topo_hdl_strfree(thp, uuid);
572 		topo_snap_release(thp);
573 		topo_close(thp);
574 		return (err ? FMTOPO_EXIT_ERROR : FMTOPO_EXIT_SUCCESS);
575 	}
576 	if ((twp = topo_walk_init(thp, opt_s, print_tnode, NULL, &err))
577 	    == NULL) {
578 		(void) fprintf(stderr, "%s: failed to walk %s topology:"
579 		    " %s\n", g_pname, opt_s, topo_strerror(err));
580 
581 		topo_hdl_strfree(thp, uuid);
582 		topo_snap_release(thp);
583 		topo_close(thp);
584 
585 		return (err ? FMTOPO_EXIT_ERROR : FMTOPO_EXIT_SUCCESS);
586 	}
587 
588 	/*
589 	 * Print standard header
590 	 */
591 	if (!opt_e) {
592 		char buf[32];
593 		time_t tod = time(NULL);
594 
595 		printf("TIME                 UUID\n");
596 		(void) strftime(buf, sizeof (buf), "%b %d %T", localtime(&tod));
597 		(void) printf("%-15s %-32s\n", buf, uuid);
598 		(void) printf("\n");
599 	}
600 
601 	topo_hdl_strfree(thp, uuid);
602 
603 	if (topo_walk_step(twp, TOPO_WALK_CHILD) == TOPO_WALK_ERR) {
604 		(void) fprintf(stderr, "%s: failed to walk topology\n",
605 		    g_pname);
606 		topo_walk_fini(twp);
607 		topo_snap_release(thp);
608 		topo_close(thp);
609 		return (FMTOPO_EXIT_ERROR);
610 	}
611 
612 	topo_walk_fini(twp);
613 	topo_snap_release(thp);
614 	topo_close(thp);
615 
616 	return (FMTOPO_EXIT_SUCCESS);
617 }
618