xref: /titanic_50/usr/src/cmd/fm/schemes/svc/scheme.c (revision d09832051bb4b41ce2b3202c09fceedc089678af)
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 2009 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 <string.h>
31 #include <sys/fm/protocol.h>
32 
33 int
34 fmd_fmri_init(void)
35 {
36 	return (0);
37 }
38 
39 void
40 fmd_fmri_fini(void)
41 {
42 }
43 
44 ssize_t
45 fmd_fmri_nvl2str(nvlist_t *nvl, char *buf, size_t buflen)
46 {
47 	int err;
48 	ssize_t len;
49 	topo_hdl_t *thp;
50 	char *str;
51 
52 	if ((thp = fmd_fmri_topo_hold(TOPO_VERSION)) == NULL)
53 		return (fmd_fmri_set_errno(EINVAL));
54 
55 	if (topo_fmri_nvl2str(thp, nvl, &str, &err) != 0) {
56 		fmd_fmri_topo_rele(thp);
57 		return (fmd_fmri_set_errno(EINVAL));
58 	}
59 
60 	if (buf != NULL)
61 		len = snprintf(buf, buflen, "%s", str);
62 	else
63 		len = strlen(str);
64 
65 	topo_hdl_strfree(thp, str);
66 	fmd_fmri_topo_rele(thp);
67 
68 	return (len);
69 }
70 
71 /*ARGSUSED*/
72 int
73 fmd_fmri_present(nvlist_t *nvl)
74 {
75 	uint8_t version;
76 	int err, present;
77 	topo_hdl_t *thp;
78 
79 	if (nvlist_lookup_uint8(nvl, FM_VERSION, &version) != 0 ||
80 	    version > FM_SVC_SCHEME_VERSION)
81 		return (fmd_fmri_set_errno(EINVAL));
82 
83 	if ((thp = fmd_fmri_topo_hold(TOPO_VERSION)) == NULL)
84 		return (fmd_fmri_set_errno(EINVAL));
85 	present = topo_fmri_present(thp, nvl, &err);
86 	fmd_fmri_topo_rele(thp);
87 
88 	return (present);
89 }
90 
91 int
92 fmd_fmri_replaced(nvlist_t *nvl)
93 {
94 	uint8_t version;
95 	int err, replaced;
96 	topo_hdl_t *thp;
97 
98 	if (nvlist_lookup_uint8(nvl, FM_VERSION, &version) != 0 ||
99 	    version > FM_SVC_SCHEME_VERSION)
100 		return (fmd_fmri_set_errno(EINVAL));
101 
102 	if ((thp = fmd_fmri_topo_hold(TOPO_VERSION)) == NULL)
103 		return (fmd_fmri_set_errno(EINVAL));
104 	replaced = topo_fmri_replaced(thp, nvl, &err);
105 	fmd_fmri_topo_rele(thp);
106 
107 	return (replaced);
108 }
109 
110 int
111 fmd_fmri_service_state(nvlist_t *nvl)
112 {
113 	uint8_t version;
114 	int err, service_state;
115 	topo_hdl_t *thp;
116 
117 	if (nvlist_lookup_uint8(nvl, FM_VERSION, &version) != 0 ||
118 	    version > FM_DEV_SCHEME_VERSION)
119 		return (fmd_fmri_set_errno(EINVAL));
120 
121 	if ((thp = fmd_fmri_topo_hold(TOPO_VERSION)) == NULL)
122 		return (fmd_fmri_set_errno(EINVAL));
123 	err = 0;
124 	service_state = topo_fmri_service_state(thp, nvl, &err);
125 	fmd_fmri_topo_rele(thp);
126 
127 	if (err != 0)
128 		return (FMD_SERVICE_STATE_UNKNOWN);
129 	else
130 		return (service_state);
131 }
132 
133 /*ARGSUSED*/
134 int
135 fmd_fmri_unusable(nvlist_t *nvl)
136 {
137 	uint8_t version;
138 	int err, unusable;
139 	topo_hdl_t *thp;
140 
141 	if (nvlist_lookup_uint8(nvl, FM_VERSION, &version) != 0 ||
142 	    version > FM_SVC_SCHEME_VERSION)
143 		return (fmd_fmri_set_errno(EINVAL));
144 
145 	if ((thp = fmd_fmri_topo_hold(TOPO_VERSION)) == NULL)
146 		return (fmd_fmri_set_errno(EINVAL));
147 	unusable = topo_fmri_unusable(thp, nvl, &err);
148 	fmd_fmri_topo_rele(thp);
149 
150 	return (unusable == 1 ? 1 : 0);
151 }
152