4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21
22 /*
23 * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
24 */
25
26
27 #include <sys/fm/protocol.h>
28 #include <fm/libtopo.h>
29 #include <ctype.h>
30 #include <fnmatch.h>
31 #include <limits.h>
32 #include <strings.h>
33 #include <stdio.h>
34 #include <errno.h>
35 #include <umem.h>
36 #include <zone.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"
580 if (strcmp(TOPO_PROP_VAL, nvpair_name(pg_nvp))
581 == 0 && nvpair_type(pg_nvp) == DATA_TYPE_NVLIST) {
582 (void) nvpair_value_nvlist(pg_nvp, &pv_nv);
583 if ((match || all) && pg_done) {
584 print_prop_nameval(thp, node, pv_nv);
585 }
586
587 }
588
589 }
590 if (match && !all)
591 return;
592 }
593 }
594
595 static void
596 set_prop(topo_hdl_t *thp, tnode_t *node, nvlist_t *fmri, struct prop_args *pp)
597 {
598 int ret, err = 0;
599 topo_type_t type;
600 nvlist_t *nvl, *f = NULL;
601 char *end;
602
603 if (pp->prop == NULL || pp->type == NULL || pp->value == NULL)
604 return;
605
606 if ((type = str2type(pp->type)) == TOPO_TYPE_INVALID) {
607 (void) fprintf(stderr, "%s: invalid property type %s for %s\n",
608 g_pname, pp->type, pp->prop);
609 return;
610 }
611
612 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
613 (void) fprintf(stderr, "%s: nvlist allocation failed for "
614 "%s=%s:%s\n", g_pname, pp->prop, pp->type, pp->value);
615 return;
616 }
617 ret = nvlist_add_string(nvl, TOPO_PROP_VAL_NAME, pp->prop);
618 ret |= nvlist_add_uint32(nvl, TOPO_PROP_VAL_TYPE, type);
619 if (ret != 0) {
620 (void) fprintf(stderr, "%s: invalid property type %s for %s\n",
621 g_pname, pp->type, pp->prop);
622 nvlist_free(nvl);
623 return;
624 }
625
626 errno = 0;
627 switch (type) {
628 case TOPO_TYPE_INT32:
629 {
630 int32_t val;
631
632 val = strtol(pp->value, &end, 0);
633 if (errno == ERANGE) {
634 ret = -1;
635 break;
636 }
637 ret = nvlist_add_int32(nvl, TOPO_PROP_VAL_VAL, val);
638 break;
639 }
640 case TOPO_TYPE_UINT32:
641 {
642 uint32_t val;
643
664 case TOPO_TYPE_UINT64:
665 {
666 uint64_t val;
667
668 val = strtoull(pp->value, &end, 0);
669 if (errno == ERANGE) {
670 ret = -1;
671 break;
672 }
673 ret = nvlist_add_uint64(nvl, TOPO_PROP_VAL_VAL, val);
674 break;
675 }
676 case TOPO_TYPE_STRING:
677 {
678 ret = nvlist_add_string(nvl, TOPO_PROP_VAL_VAL,
679 pp->value);
680 break;
681 }
682 case TOPO_TYPE_FMRI:
683 {
684 if ((ret = topo_fmri_str2nvl(thp, pp->value, &f, &err))
685 < 0)
686 break;
687
688 if ((ret = nvlist_add_nvlist(nvl, TOPO_PROP_VAL_VAL,
689 f)) != 0)
690 err = ETOPO_PROP_NVL;
691 break;
692 }
693 default:
694 ret = -1;
695 }
696
697 if (ret != 0) {
698 (void) fprintf(stderr, "%s: unable to set property value for "
699 "%s: %s\n", g_pname, pp->prop, topo_strerror(err));
700 nvlist_free(nvl);
701 return;
702 }
703
704 if (node != NULL) {
705 if (topo_prop_setprop(node, pp->group, nvl, TOPO_PROP_MUTABLE,
706 f, &ret) < 0) {
707 (void) fprintf(stderr, "%s: unable to set property "
708 "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;
713 }
714 } else {
715 if (topo_fmri_setprop(thp, fmri, pp->group, nvl,
716 TOPO_PROP_MUTABLE, f, &ret) < 0) {
717 (void) fprintf(stderr, "%s: unable to set property "
718 "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;
723 }
724 }
725
726 nvlist_free(nvl);
727
728 /*
729 * Now, get the property back for printing
730 */
731 if (node != NULL) {
732 if (topo_prop_getprop(node, pp->group, pp->prop, f, &nvl,
733 &err) < 0) {
734 (void) fprintf(stderr, "%s: failed to get %s.%s: %s\n",
735 g_pname, pp->group, pp->prop, topo_strerror(err));
736 nvlist_free(f);
737 return;
738 }
739 } else {
740 if (topo_fmri_getprop(thp, fmri, pp->group, pp->prop,
741 f, &nvl, &err) < 0) {
742 (void) fprintf(stderr, "%s: failed to get %s.%s: %s\n",
743 g_pname, pp->group, pp->prop, topo_strerror(err));
744 nvlist_free(f);
745 return;
746 }
747 }
748
749 print_pgroup(thp, node, pp->group, NULL, NULL, 0);
750 print_prop_nameval(thp, node, nvl);
751 nvlist_free(nvl);
752
753 nvlist_free(f);
754 }
755
756 static void
757 print_props(topo_hdl_t *thp, tnode_t *node)
758 {
759 int i, err;
760 nvlist_t *nvl;
761 struct prop_args *pp;
762
763 if (pcnt == 0)
764 return;
765
766 for (i = 0; i < pcnt; ++i) {
767 pp = pargs[i];
768
769 if (pp->group == NULL)
770 continue;
771
772 /*
773 * If we have a valid value, this is a request to
|
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21
22 /*
23 * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2013, Joyent, Inc. All rights reserved.
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 <zone.h>
38 #include <sys/param.h>
39
40 #define FMTOPO_EXIT_SUCCESS 0
41 #define FMTOPO_EXIT_ERROR 1
42 #define FMTOPO_EXIT_USAGE 2
43
44 #define STDERR "stderr"
581 if (strcmp(TOPO_PROP_VAL, nvpair_name(pg_nvp))
582 == 0 && nvpair_type(pg_nvp) == DATA_TYPE_NVLIST) {
583 (void) nvpair_value_nvlist(pg_nvp, &pv_nv);
584 if ((match || all) && pg_done) {
585 print_prop_nameval(thp, node, pv_nv);
586 }
587
588 }
589
590 }
591 if (match && !all)
592 return;
593 }
594 }
595
596 static void
597 set_prop(topo_hdl_t *thp, tnode_t *node, nvlist_t *fmri, struct prop_args *pp)
598 {
599 int ret, err = 0;
600 topo_type_t type;
601 nvlist_t *nvl = NULL;
602 char *end;
603
604 if (pp->prop == NULL || pp->type == NULL || pp->value == NULL)
605 goto out;
606
607 if ((type = str2type(pp->type)) == TOPO_TYPE_INVALID) {
608 (void) fprintf(stderr, "%s: invalid property type %s for %s\n",
609 g_pname, pp->type, pp->prop);
610 goto out;
611 }
612
613 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
614 (void) fprintf(stderr, "%s: nvlist allocation failed for "
615 "%s=%s:%s\n", g_pname, pp->prop, pp->type, pp->value);
616 goto out;
617 }
618 ret = nvlist_add_string(nvl, TOPO_PROP_VAL_NAME, pp->prop);
619 ret |= nvlist_add_uint32(nvl, TOPO_PROP_VAL_TYPE, type);
620 if (ret != 0) {
621 (void) fprintf(stderr, "%s: invalid property type %s for %s\n",
622 g_pname, pp->type, pp->prop);
623 goto out;
624 }
625
626 errno = 0;
627 switch (type) {
628 case TOPO_TYPE_INT32:
629 {
630 int32_t val;
631
632 val = strtol(pp->value, &end, 0);
633 if (errno == ERANGE) {
634 ret = -1;
635 break;
636 }
637 ret = nvlist_add_int32(nvl, TOPO_PROP_VAL_VAL, val);
638 break;
639 }
640 case TOPO_TYPE_UINT32:
641 {
642 uint32_t val;
643
664 case TOPO_TYPE_UINT64:
665 {
666 uint64_t val;
667
668 val = strtoull(pp->value, &end, 0);
669 if (errno == ERANGE) {
670 ret = -1;
671 break;
672 }
673 ret = nvlist_add_uint64(nvl, TOPO_PROP_VAL_VAL, val);
674 break;
675 }
676 case TOPO_TYPE_STRING:
677 {
678 ret = nvlist_add_string(nvl, TOPO_PROP_VAL_VAL,
679 pp->value);
680 break;
681 }
682 case TOPO_TYPE_FMRI:
683 {
684 nvlist_t *val = NULL;
685
686 if ((ret = topo_fmri_str2nvl(thp, pp->value, &val,
687 &err)) < 0)
688 break;
689
690 if ((ret = nvlist_add_nvlist(nvl, TOPO_PROP_VAL_VAL,
691 val)) != 0)
692 err = ETOPO_PROP_NVL;
693
694 nvlist_free(val);
695 break;
696 }
697 default:
698 ret = -1;
699 }
700
701 if (ret != 0) {
702 (void) fprintf(stderr, "%s: unable to set property value for "
703 "%s: %s\n", g_pname, pp->prop, topo_strerror(err));
704 goto out;
705 }
706
707 if (node != NULL) {
708 if ((ret = topo_prop_setprop(node, pp->group, nvl,
709 TOPO_PROP_MUTABLE, nvl, &err)) < 0) {
710 (void) fprintf(stderr, "%s: unable to set property "
711 "value for " "%s=%s:%s: %s\n", g_pname, pp->prop,
712 pp->type, pp->value, topo_strerror(err));
713 goto out;
714 }
715 } else {
716 if ((ret = topo_fmri_setprop(thp, fmri, pp->group, nvl,
717 TOPO_PROP_MUTABLE, nvl, &err)) < 0) {
718 (void) fprintf(stderr, "%s: unable to set property "
719 "value for " "%s=%s:%s: %s\n", g_pname, pp->prop,
720 pp->type, pp->value, topo_strerror(err));
721 goto out;
722 }
723 }
724
725 nvlist_free(nvl);
726 nvl = NULL;
727
728 /*
729 * Now, get the property back for printing
730 */
731 if (node != NULL) {
732 if ((ret = topo_prop_getprop(node, pp->group, pp->prop, NULL,
733 &nvl, &err)) < 0) {
734 (void) fprintf(stderr, "%s: failed to get %s.%s: %s\n",
735 g_pname, pp->group, pp->prop, topo_strerror(err));
736 goto out;
737 }
738 } else {
739 if ((ret = topo_fmri_getprop(thp, fmri, pp->group, pp->prop,
740 NULL, &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 goto out;
744 }
745 }
746
747 print_pgroup(thp, node, pp->group, NULL, NULL, 0);
748 print_prop_nameval(thp, node, nvl);
749
750 out:
751 nvlist_free(nvl);
752 }
753
754 static void
755 print_props(topo_hdl_t *thp, tnode_t *node)
756 {
757 int i, err;
758 nvlist_t *nvl;
759 struct prop_args *pp;
760
761 if (pcnt == 0)
762 return;
763
764 for (i = 0; i < pcnt; ++i) {
765 pp = pargs[i];
766
767 if (pp->group == NULL)
768 continue;
769
770 /*
771 * If we have a valid value, this is a request to
|