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