fmtopo.c revision c40d7343efa60b18ad1ceb316eb337caeea79046
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * CDDL HEADER START
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * The contents of this file are subject to the terms of the
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * Common Development and Distribution License (the "License").
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * You may not use this file except in compliance with the License.
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * See the License for the specific language governing permissions
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * and limitations under the License.
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * When distributing Covered Code, include this CDDL HEADER in each
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * If applicable, add the following below this CDDL HEADER, with the
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * fields enclosed by brackets "[]" replaced with your own identifying
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * information: Portions Copyright [yyyy] [name of copyright owner]
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * CDDL HEADER END
c40d7343efa60b18ad1ceb316eb337caeea79046cindi * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * Use is subject to license terms.
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi#pragma ident "%Z%%M% %I% %E% SMI"
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindistatic const char *g_pname;
0eb822a1c0c2bea495647510b75f77f0e57633ebcindistatic int opt_e = 0;
0eb822a1c0c2bea495647510b75f77f0e57633ebcindistatic int opt_d = 0;
0eb822a1c0c2bea495647510b75f77f0e57633ebcindistatic int opt_V = 0;
0eb822a1c0c2bea495647510b75f77f0e57633ebcindistatic int opt_p = 0;
c40d7343efa60b18ad1ceb316eb337caeea79046cindistatic int opt_S = 0;
c40d7343efa60b18ad1ceb316eb337caeea79046cindistatic int opt_t = 0;
0eb822a1c0c2bea495647510b75f77f0e57633ebcindistatic int opt_x = 0;
c40d7343efa60b18ad1ceb316eb337caeea79046cindistatic int opt_all = 0;
c40d7343efa60b18ad1ceb316eb337caeea79046cindi const char *group;
c40d7343efa60b18ad1ceb316eb337caeea79046cindi const char *prop;
c40d7343efa60b18ad1ceb316eb337caeea79046cindi const char *type;
c40d7343efa60b18ad1ceb316eb337caeea79046cindi const char *value;
c40d7343efa60b18ad1ceb316eb337caeea79046cindistatic int pcnt = 0;
c40d7343efa60b18ad1ceb316eb337caeea79046cindi "Usage: %s [-CedpSVx] [-P group.property[=type:value]] "
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi "\t-C dump core after completing execution\n"
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi "\t-d set debug mode for libtopo modules\n"
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi "\t-e display FMRIs as paths using esc/eft notation\n"
c40d7343efa60b18ad1ceb316eb337caeea79046cindi "\t-P get/set specified properties\n"
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi "\t-p display of FMRI protocol properties\n"
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi "\t-R set root directory for libtopo plug-ins and other files\n"
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi "\t-s display topology for the specified FMRI scheme\n"
c40d7343efa60b18ad1ceb316eb337caeea79046cindi "\t-S display FMRI status (present/usable)\n"
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi "\t-V set verbose mode\n"
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi "\t-x display a xml formatted topology\n");
c40d7343efa60b18ad1ceb316eb337caeea79046cindiprint_node(topo_hdl_t *thp, tnode_t *node, nvlist_t *nvl, const char *fmri)
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi (void) fprintf(stderr, "%s: failed to get fmri for %s=%d: %s\n",
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi if (nvlist_lookup_nvlist_array(fmri, FM_FMRI_HC_LIST, &hcl, &n) != 0) {
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi (void) fprintf(stderr, "%s: failed to find %s for %s=%d\n",
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi for (i = 0; i < n; i++) {
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi if (nvlist_lookup_string(hcl[i], FM_FMRI_HC_NAME, &name) != 0 ||
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi nvlist_lookup_string(hcl[i], FM_FMRI_HC_ID, &inst) != 0) {
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi /* Print property name */
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi if ((pv_nvp = nvlist_next_nvpair(nvl, NULL)) == NULL ||
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi strcmp(TOPO_PROP_VAL_NAME, nvpair_name(pv_nvp)) != 0) {
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi (void) fprintf(stderr, "%s: malformed property name\n",
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi if ((pv_nvp = nvlist_next_nvpair(nvl, pv_nvp)) == NULL ||
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi strcmp(nvpair_name(pv_nvp), TOPO_PROP_VAL_TYPE) != 0 ||
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi (void) fprintf(stderr, "%s: malformed property type for %s\n",
c40d7343efa60b18ad1ceb316eb337caeea79046cindi (void) nvpair_value_uint32(pv_nvp, (uint32_t *)&type);
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi case TOPO_TYPE_UINT32_ARRAY: tstr = "uint32[]"; break;
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi case TOPO_TYPE_UINT64_ARRAY: tstr = "uint64[]"; break;
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi case TOPO_TYPE_STRING_ARRAY: tstr = "string[]"; break;
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi * Get property value
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi (void) fprintf(stderr, "%s: malformed property value\n",
9dd0f810214fdc8e1af881a9a5c4b6927629ff9ecindi (void) nvpair_value_uint32_array(pv_nvp, &val, &nelem);
9dd0f810214fdc8e1af881a9a5c4b6927629ff9ecindi for (i = 0; i < nelem; i++)
c40d7343efa60b18ad1ceb316eb337caeea79046cindiprint_pgroup(topo_hdl_t *thp, tnode_t *node, const char *pgn, char *dstab,
c40d7343efa60b18ad1ceb316eb337caeea79046cindi if (node != NULL && (dstab == NULL || nstab == NULL || version == -1)) {
c40d7343efa60b18ad1ceb316eb337caeea79046cindi if ((pgi = topo_pgroup_info(node, pgn, &err)) != NULL) {
c40d7343efa60b18ad1ceb316eb337caeea79046cindi dstab = (char *)topo_stability2name(pgi->tpi_datastab);
c40d7343efa60b18ad1ceb316eb337caeea79046cindi nstab = (char *)topo_stability2name(pgi->tpi_namestab);
c40d7343efa60b18ad1ceb316eb337caeea79046cindi if (dstab == NULL || nstab == NULL || version == -1) {
c40d7343efa60b18ad1ceb316eb337caeea79046cindi printf(" group: %-30s version: - stability: -/-\n", pgn);
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi printf(" group: %-30s version: %-3d stability: %s/%s\n",
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi printf(" group: %-30s version: %-3d stability: %s/%s\n",
c40d7343efa60b18ad1ceb316eb337caeea79046cindiprint_all_props(topo_hdl_t *thp, tnode_t *node, nvlist_t *p_nv,
c40d7343efa60b18ad1ceb316eb337caeea79046cindi const char *group)
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi for (nvp = nvlist_next_nvpair(p_nv, NULL); nvp != NULL;
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi for (pg_nvp = nvlist_next_nvpair(pg_nv, NULL); pg_nvp != NULL;
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi * Print property group name and stability levels
c40d7343efa60b18ad1ceb316eb337caeea79046cindi * Print property group and property name-value pair
c40d7343efa60b18ad1ceb316eb337caeea79046cindiset_prop(topo_hdl_t *thp, tnode_t *node, nvlist_t *fmri, struct prop_args *pp)
c40d7343efa60b18ad1ceb316eb337caeea79046cindi if (pp->prop == NULL || pp->type == NULL || pp->value == NULL)
c40d7343efa60b18ad1ceb316eb337caeea79046cindi if ((type = str2type(pp->type)) == TOPO_TYPE_INVALID) {
c40d7343efa60b18ad1ceb316eb337caeea79046cindi (void) fprintf(stderr, "%s: invalid property type %s for %s\n",
c40d7343efa60b18ad1ceb316eb337caeea79046cindi (void) fprintf(stderr, "%s: nvlist allocation failed for "
c40d7343efa60b18ad1ceb316eb337caeea79046cindi "%s=%s:%s\n", g_pname, pp->prop, pp->type, pp->value);
c40d7343efa60b18ad1ceb316eb337caeea79046cindi ret = nvlist_add_string(nvl, TOPO_PROP_VAL_NAME, pp->prop);
c40d7343efa60b18ad1ceb316eb337caeea79046cindi ret |= nvlist_add_uint32(nvl, TOPO_PROP_VAL_TYPE, type);
c40d7343efa60b18ad1ceb316eb337caeea79046cindi if (ret != 0) {
c40d7343efa60b18ad1ceb316eb337caeea79046cindi (void) fprintf(stderr, "%s: invalid property type %s for %s\n",
c40d7343efa60b18ad1ceb316eb337caeea79046cindi if ((ret = topo_fmri_str2nvl(thp, pp->value, &f, &err))
c40d7343efa60b18ad1ceb316eb337caeea79046cindi if (ret != 0) {
c40d7343efa60b18ad1ceb316eb337caeea79046cindi (void) fprintf(stderr, "%s: unable to set property value for "
c40d7343efa60b18ad1ceb316eb337caeea79046cindi if (topo_prop_setprop(node, pp->group, nvl, TOPO_PROP_MUTABLE,
c40d7343efa60b18ad1ceb316eb337caeea79046cindi f, &ret) < 0) {
c40d7343efa60b18ad1ceb316eb337caeea79046cindi * Now, get the property back for printing
c40d7343efa60b18ad1ceb316eb337caeea79046cindi if (topo_prop_getprop(node, pp->group, pp->prop, f, &nvl,
c40d7343efa60b18ad1ceb316eb337caeea79046cindi (void) fprintf(stderr, "%s: failed to get %s.%s: %s\n",
c40d7343efa60b18ad1ceb316eb337caeea79046cindi (void) fprintf(stderr, "%s: failed to get %s.%s: %s\n",
c40d7343efa60b18ad1ceb316eb337caeea79046cindi for (i = 0; i < pcnt; ++i) {
c40d7343efa60b18ad1ceb316eb337caeea79046cindi * If we have a valid value, this is a request to
c40d7343efa60b18ad1ceb316eb337caeea79046cindi * set a property. Otherwise, just print the property
c40d7343efa60b18ad1ceb316eb337caeea79046cindi * group and any specified properties.
c40d7343efa60b18ad1ceb316eb337caeea79046cindi * Print all properties in this group
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi/*ARGSUSED*/
c40d7343efa60b18ad1ceb316eb337caeea79046cindi } else if (pcnt > 0) {
c40d7343efa60b18ad1ceb316eb337caeea79046cindi char c, *s, *p;
c40d7343efa60b18ad1ceb316eb337caeea79046cindi if ((pargs = malloc(sizeof (struct prop_args *) * pcnt)) == NULL) {
c40d7343efa60b18ad1ceb316eb337caeea79046cindi (void) fprintf(stderr, "%s: failed to allocate property "
c40d7343efa60b18ad1ceb316eb337caeea79046cindi for (optind = 1; (c = getopt(argc, argv, optstr)) != EOF; ) {
c40d7343efa60b18ad1ceb316eb337caeea79046cindi if (c == 'P') {
c40d7343efa60b18ad1ceb316eb337caeea79046cindi if ((pp = pargs[i] = malloc(sizeof (struct prop_args)))
c40d7343efa60b18ad1ceb316eb337caeea79046cindi *s++ = '\0';
c40d7343efa60b18ad1ceb316eb337caeea79046cindi "property type not "
c40d7343efa60b18ad1ceb316eb337caeea79046cindi "specified for assignment "
c40d7343efa60b18ad1ceb316eb337caeea79046cindi for (j = 0; j < i; ++j)
c40d7343efa60b18ad1ceb316eb337caeea79046cindi if ((twp = topo_walk_init(thp, opt_s, walk_node, NULL, &err))
c40d7343efa60b18ad1ceb316eb337caeea79046cindi (void) fprintf(stderr, "%s: failed to walk %s topology:"
c40d7343efa60b18ad1ceb316eb337caeea79046cindi return (-1);
c40d7343efa60b18ad1ceb316eb337caeea79046cindi * Print standard header
c40d7343efa60b18ad1ceb316eb337caeea79046cindi (void) strftime(buf, sizeof (buf), "%b %d %T", localtime(&tod));
c40d7343efa60b18ad1ceb316eb337caeea79046cindi if (topo_walk_step(twp, TOPO_WALK_CHILD) == TOPO_WALK_ERR) {
c40d7343efa60b18ad1ceb316eb337caeea79046cindi (void) fprintf(stderr, "%s: failed to walk topology\n",
c40d7343efa60b18ad1ceb316eb337caeea79046cindi return (-1);
c40d7343efa60b18ad1ceb316eb337caeea79046cindi return (0);
c40d7343efa60b18ad1ceb316eb337caeea79046cindiprint_fmri_pgroup(topo_hdl_t *thp, const char *pgn, nvlist_t *nvl)
c40d7343efa60b18ad1ceb316eb337caeea79046cindi (void) nvlist_lookup_string(nvl, TOPO_PROP_GROUP_NSTAB, &nstab);
c40d7343efa60b18ad1ceb316eb337caeea79046cindi (void) nvlist_lookup_string(nvl, TOPO_PROP_GROUP_DSTAB, &dstab);
c40d7343efa60b18ad1ceb316eb337caeea79046cindi (void) nvlist_lookup_int32(nvl, TOPO_PROP_GROUP_VERSION, &version);
c40d7343efa60b18ad1ceb316eb337caeea79046cindi for (pnvp = nvlist_next_nvpair(nvl, NULL); pnvp != NULL;
c40d7343efa60b18ad1ceb316eb337caeea79046cindi * Print property group and property name-value pair
c40d7343efa60b18ad1ceb316eb337caeea79046cindi for (i = 0; i < pcnt; ++i) {
c40d7343efa60b18ad1ceb316eb337caeea79046cindi * If we have a valid value, this is a request to
c40d7343efa60b18ad1ceb316eb337caeea79046cindi * set a property. Otherwise, just print the property
c40d7343efa60b18ad1ceb316eb337caeea79046cindi * group and any specified properties.
c40d7343efa60b18ad1ceb316eb337caeea79046cindi * Print all properties in this group
c40d7343efa60b18ad1ceb316eb337caeea79046cindi (void) fprintf(stderr, "%s: failed to convert %s to nvlist: "
c40d7343efa60b18ad1ceb316eb337caeea79046cindi (void) strftime(buf, sizeof (buf), "%b %d %T", localtime(&tod));
c40d7343efa60b18ad1ceb316eb337caeea79046cindi for (i = 0; i < pcnt; ++i)
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi int c, err = 0;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi switch (c) {
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi if ((thp = topo_open(TOPO_VERSION, opt_R, &err)) == NULL) {
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi (void) fprintf(stderr, "%s: failed to open topology tree: %s\n",
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi if ((uuid = topo_snap_hold(thp, NULL, &err)) == NULL) {
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi (void) fprintf(stderr, "%s: failed to snapshot topology: %s\n",
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi } else if (err != 0) {
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi (void) fprintf(stderr, "%s: topology snapshot incomplete\n",
c40d7343efa60b18ad1ceb316eb337caeea79046cindi return (fmtopo_exit(thp, uuid, err ? FMTOPO_EXIT_ERROR :
c40d7343efa60b18ad1ceb316eb337caeea79046cindi * Try getting some useful information