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 2008 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 #include <fm/fmd_fmri.h> 28 #include <fm/libtopo.h> 29 #include <fm/topo_mod.h> 30 #include <libdevinfo.h> 31 #include <alloca.h> 32 #include <string.h> 33 34 int 35 fmd_fmri_init(void) 36 { 37 return (0); 38 } 39 40 void 41 fmd_fmri_fini(void) 42 { 43 } 44 45 ssize_t 46 fmd_fmri_nvl2str(nvlist_t *nvl, char *buf, size_t buflen) 47 { 48 int err; 49 ssize_t len; 50 topo_hdl_t *thp; 51 char *str; 52 53 if ((thp = fmd_fmri_topo_hold(TOPO_VERSION)) == NULL) 54 return (fmd_fmri_set_errno(EINVAL)); 55 56 if (topo_fmri_nvl2str(thp, nvl, &str, &err) != 0) { 57 fmd_fmri_topo_rele(thp); 58 return (fmd_fmri_set_errno(EINVAL)); 59 } 60 61 if (buf != NULL) 62 len = snprintf(buf, buflen, "%s", str); 63 else 64 len = strlen(str); 65 66 topo_hdl_strfree(thp, str); 67 fmd_fmri_topo_rele(thp); 68 69 return (len); 70 } 71 72 int 73 fmd_fmri_present(nvlist_t *nvl) 74 { 75 int err, present; 76 topo_hdl_t *thp; 77 78 if ((thp = fmd_fmri_topo_hold(TOPO_VERSION)) == NULL) 79 return (fmd_fmri_set_errno(EINVAL)); 80 err = 0; 81 present = topo_fmri_present(thp, nvl, &err); 82 fmd_fmri_topo_rele(thp); 83 84 return (present); 85 } 86 87 int 88 fmd_fmri_replaced(nvlist_t *nvl) 89 { 90 int err, rval; 91 topo_hdl_t *thp; 92 93 if ((thp = fmd_fmri_topo_hold(TOPO_VERSION)) == NULL) 94 return (fmd_fmri_set_errno(EINVAL)); 95 err = 0; 96 rval = topo_fmri_replaced(thp, nvl, &err); 97 fmd_fmri_topo_rele(thp); 98 99 return (rval); 100 } 101 102 int 103 fmd_fmri_unusable(nvlist_t *nvl) 104 { 105 uint8_t version; 106 int err, unusable; 107 topo_hdl_t *thp; 108 109 if (nvlist_lookup_uint8(nvl, FM_VERSION, &version) != 0 || 110 version > FM_DEV_SCHEME_VERSION) 111 return (fmd_fmri_set_errno(EINVAL)); 112 113 if ((thp = fmd_fmri_topo_hold(TOPO_VERSION)) == NULL) 114 return (fmd_fmri_set_errno(EINVAL)); 115 err = 0; 116 unusable = topo_fmri_unusable(thp, nvl, &err); 117 fmd_fmri_topo_rele(thp); 118 119 if (err != 0) 120 return (0); 121 else 122 return (unusable); 123 } 124 125 int 126 fmd_fmri_service_state(nvlist_t *nvl) 127 { 128 uint8_t version; 129 int err, service_state; 130 topo_hdl_t *thp; 131 132 if (nvlist_lookup_uint8(nvl, FM_VERSION, &version) != 0 || 133 version > FM_DEV_SCHEME_VERSION) 134 return (fmd_fmri_set_errno(EINVAL)); 135 136 if ((thp = fmd_fmri_topo_hold(TOPO_VERSION)) == NULL) 137 return (fmd_fmri_set_errno(EINVAL)); 138 err = 0; 139 service_state = topo_fmri_service_state(thp, nvl, &err); 140 fmd_fmri_topo_rele(thp); 141 142 if (err != 0) 143 return (FMD_SERVICE_STATE_UNKNOWN); 144 else 145 return (service_state); 146 } 147