xref: /titanic_52/usr/src/cmd/fm/schemes/hc/scheme.c (revision 84ab085a13f931bc78e7415e7ce921dbaa14fcb3)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <fm/fmd_fmri.h>
30 
31 /*
32  * buf_append -- Append str to buf (if it's non-NULL).  Place prepend
33  * in buf in front of str and append behind it (if they're non-NULL).
34  * Continue to update size even if we run out of space to actually
35  * stuff characters in the buffer.
36  */
37 static void
38 buf_append(ssize_t *sz, char *buf, size_t buflen, char *str,
39     char *prepend, char *append)
40 {
41 	ssize_t left;
42 
43 	if (str == NULL)
44 		return;
45 
46 	if (buflen == 0 || (left = buflen - *sz) < 0)
47 		left = 0;
48 
49 	if (buf != NULL && left != 0)
50 		buf += *sz;
51 
52 	if (prepend == NULL && append == NULL)
53 		*sz += snprintf(buf, left, "%s", str);
54 	else if (append == NULL)
55 		*sz += snprintf(buf, left, "%s%s", prepend, str);
56 	else if (prepend == NULL)
57 		*sz += snprintf(buf, left, "%s%s", str, append);
58 	else
59 		*sz += snprintf(buf, left, "%s%s%s", prepend, str, append);
60 }
61 
62 ssize_t
63 fmd_fmri_nvl2str(nvlist_t *nvl, char *buf, size_t buflen)
64 {
65 	nvlist_t **hcprs = NULL;
66 	nvlist_t *anvl = NULL;
67 	uint8_t version;
68 	ssize_t size = 0;
69 	uint_t hcnprs;
70 	char *achas = NULL;
71 	char *adom = NULL;
72 	char *aprod = NULL;
73 	char *asrvr = NULL;
74 	char *ahost = NULL;
75 	char *serial = NULL;
76 	char *part = NULL;
77 	char *root = NULL;
78 	char *rev = NULL;
79 	int more_auth = 0;
80 	int err, i;
81 
82 	if (nvlist_lookup_uint8(nvl, FM_VERSION, &version) != 0 ||
83 	    version > FM_HC_SCHEME_VERSION)
84 		return (fmd_fmri_set_errno(EINVAL));
85 
86 	/* Get authority, if present */
87 	err = nvlist_lookup_nvlist(nvl, FM_FMRI_AUTHORITY, &anvl);
88 	if (err != 0 && err != ENOENT)
89 		return (fmd_fmri_set_errno(err));
90 
91 	if ((err = nvlist_lookup_string(nvl, FM_FMRI_HC_ROOT, &root)) != 0)
92 		return (fmd_fmri_set_errno(EINVAL));
93 
94 	err = nvlist_lookup_nvlist_array(nvl, FM_FMRI_HC_LIST, &hcprs, &hcnprs);
95 	if (err != 0 || hcprs == NULL)
96 		return (fmd_fmri_set_errno(EINVAL));
97 
98 	if (anvl != NULL) {
99 		(void) nvlist_lookup_string(anvl,
100 		    FM_FMRI_AUTH_PRODUCT, &aprod);
101 		(void) nvlist_lookup_string(anvl,
102 		    FM_FMRI_AUTH_CHASSIS, &achas);
103 		(void) nvlist_lookup_string(anvl,
104 		    FM_FMRI_AUTH_DOMAIN, &adom);
105 		(void) nvlist_lookup_string(anvl,
106 		    FM_FMRI_AUTH_SERVER, &asrvr);
107 		(void) nvlist_lookup_string(anvl,
108 		    FM_FMRI_AUTH_HOST, &ahost);
109 		if (aprod != NULL)
110 			more_auth++;
111 		if (achas != NULL)
112 			more_auth++;
113 		if (adom != NULL)
114 			more_auth++;
115 		if (asrvr != NULL)
116 			more_auth++;
117 		if (ahost != NULL)
118 			more_auth++;
119 	}
120 
121 	(void) nvlist_lookup_string(nvl, FM_FMRI_HC_SERIAL_ID, &serial);
122 	(void) nvlist_lookup_string(nvl, FM_FMRI_HC_PART, &part);
123 	(void) nvlist_lookup_string(nvl, FM_FMRI_HC_REVISION, &rev);
124 
125 	/* hc:// */
126 	buf_append(&size, buf, buflen, FM_FMRI_SCHEME_HC, NULL, "://");
127 
128 	/* authority, if any */
129 	if (aprod != NULL)
130 		buf_append(&size, buf, buflen, aprod, FM_FMRI_AUTH_PRODUCT "=",
131 		    --more_auth > 0 ? "," : NULL);
132 	if (achas != NULL)
133 		buf_append(&size, buf, buflen, achas, FM_FMRI_AUTH_CHASSIS "=",
134 		    --more_auth > 0 ? "," : NULL);
135 	if (adom != NULL)
136 		buf_append(&size, buf, buflen, adom, FM_FMRI_AUTH_DOMAIN "=",
137 		    --more_auth > 0 ? "," : NULL);
138 	if (asrvr != NULL)
139 		buf_append(&size, buf, buflen, asrvr, FM_FMRI_AUTH_SERVER "=",
140 		    --more_auth > 0 ? "," : NULL);
141 	if (ahost != NULL)
142 		buf_append(&size, buf, buflen, ahost, FM_FMRI_AUTH_HOST "=",
143 		    NULL);
144 
145 	/* separating slash */
146 	if (serial != NULL || part != NULL || rev != NULL)
147 		buf_append(&size, buf, buflen, "/", NULL, NULL);
148 
149 	/* hardware-id part */
150 	buf_append(&size, buf, buflen, serial, ":" FM_FMRI_HC_SERIAL_ID "=",
151 	    NULL);
152 	buf_append(&size, buf, buflen, part, ":" FM_FMRI_HC_PART "=", NULL);
153 	buf_append(&size, buf, buflen, rev, ":" FM_FMRI_HC_REVISION "=", NULL);
154 
155 	/* separating slash */
156 	buf_append(&size, buf, buflen, "/", NULL, NULL);
157 
158 	/* hc-root */
159 	buf_append(&size, buf, buflen, root, NULL, NULL);
160 
161 	/* all the pairs */
162 	for (i = 0; i < hcnprs; i++) {
163 		char *nm = NULL;
164 		char *id = NULL;
165 
166 		if (i > 0)
167 			buf_append(&size, buf, buflen, "/", NULL, NULL);
168 		(void) nvlist_lookup_string(hcprs[i], FM_FMRI_HC_NAME, &nm);
169 		(void) nvlist_lookup_string(hcprs[i], FM_FMRI_HC_ID, &id);
170 		if (nm == NULL || id == NULL)
171 			return (fmd_fmri_set_errno(EINVAL));
172 		buf_append(&size, buf, buflen, nm, NULL, "=");
173 		buf_append(&size, buf, buflen, id, NULL, NULL);
174 	}
175 
176 	return (size);
177 }
178 
179 /*
180  * fmd_fmri_present() is called by fmadm to determine if a faulty ASRU
181  * is still present in the system.  In general we don't expect to get
182  * ASRUs in this scheme, so it's unlikely this routine will get called.
183  * In case it does, though, we just return true by default, as we have no
184  * real way to look up the component in the system configuration.
185  */
186 /*ARGSUSED*/
187 int
188 fmd_fmri_present(nvlist_t *nvl)
189 {
190 	return (1);
191 }
192 
193 /*
194  * fmd_fmri_unusable() is called by fmadm to determine if a faulty ASRU
195  * is usable.  In general we don't expect to get ASRUs in this scheme,
196  * so it's unlikely this routine will get called.  In case it does,
197  * though, we just return false by default, as we have no real way to
198  * find the component or determine the component's usability.
199  */
200 /*ARGSUSED*/
201 int
202 fmd_fmri_unusable(nvlist_t *nvl)
203 {
204 	return (0);
205 }
206