xref: /titanic_52/usr/src/cmd/fm/fmd/common/fmd_protocol.c (revision d92a527c473246e5c750ae6c33ecd2f242a5bf1f)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <sys/fm/protocol.h>
30 #include <strings.h>
31 #include <alloca.h>
32 #include <stdio.h>
33 
34 #include <fmd_protocol.h>
35 #include <fmd_module.h>
36 #include <fmd_conf.h>
37 #include <fmd_subr.h>
38 #include <fmd_error.h>
39 #include <fmd_time.h>
40 #include <fmd.h>
41 
42 /*
43  * Create an FMRI authority element for the environment in which this instance
44  * of fmd is deployed.  This function is called once and the result is cached.
45  */
46 nvlist_t *
47 fmd_protocol_authority(void)
48 {
49 	const char *str;
50 	nvlist_t *nvl;
51 	int err = 0;
52 
53 	if (nvlist_xalloc(&nvl, NV_UNIQUE_NAME, &fmd.d_nva) != 0)
54 		fmd_panic("failed to xalloc authority nvlist");
55 
56 	err |= nvlist_add_uint8(nvl, FM_VERSION, FM_FMRI_AUTH_VERSION);
57 
58 	if ((str = fmd_conf_getnzstr(fmd.d_conf, "product")) == NULL)
59 		str = fmd_conf_getnzstr(fmd.d_conf, "platform");
60 
61 	if (str != NULL)
62 		err |= nvlist_add_string(nvl, FM_FMRI_AUTH_PRODUCT, str);
63 
64 	if ((str = fmd_conf_getnzstr(fmd.d_conf, "chassis")) != NULL)
65 		err |= nvlist_add_string(nvl, FM_FMRI_AUTH_CHASSIS, str);
66 
67 	if ((str = fmd_conf_getnzstr(fmd.d_conf, "domain")) != NULL)
68 		err |= nvlist_add_string(nvl, FM_FMRI_AUTH_DOMAIN, str);
69 
70 	if ((str = fmd_conf_getnzstr(fmd.d_conf, "server")) != NULL)
71 		err |= nvlist_add_string(nvl, FM_FMRI_AUTH_SERVER, str);
72 
73 	if (err != 0)
74 		fmd_panic("failed to populate nvlist: %s\n", fmd_strerror(err));
75 
76 	return (nvl);
77 }
78 
79 /*
80  * Create an FMRI for the specified module.  We use the cached authority
81  * nvlist saved in fmd.d_auth to fill in the authority member.
82  */
83 nvlist_t *
84 fmd_protocol_fmri_module(fmd_module_t *mp)
85 {
86 	nvlist_t *nvl;
87 	int err = 0;
88 
89 	if (nvlist_xalloc(&nvl, NV_UNIQUE_NAME, &fmd.d_nva) != 0)
90 		fmd_panic("failed to xalloc diag-engine fmri nvlist");
91 
92 	err |= nvlist_add_uint8(nvl, FM_VERSION, FM_FMD_SCHEME_VERSION);
93 	err |= nvlist_add_string(nvl, FM_FMRI_SCHEME, FM_FMRI_SCHEME_FMD);
94 	err |= nvlist_add_nvlist(nvl, FM_FMRI_AUTHORITY, fmd.d_auth);
95 	err |= nvlist_add_string(nvl, FM_FMRI_FMD_NAME, mp->mod_name);
96 
97 	if (mp->mod_info != NULL) {
98 		err |= nvlist_add_string(nvl,
99 		    FM_FMRI_FMD_VERSION, mp->mod_info->fmdi_vers);
100 	} else if (mp == fmd.d_rmod) {
101 		err |= nvlist_add_string(nvl,
102 		    FM_FMRI_FMD_VERSION, fmd.d_version);
103 	}
104 
105 	if (err != 0)
106 		fmd_panic("failed to populate nvlist: %s\n", fmd_strerror(err));
107 
108 	return (nvl);
109 }
110 
111 nvlist_t *
112 fmd_protocol_fault(const char *class, uint8_t certainty,
113     nvlist_t *asru, nvlist_t *fru, nvlist_t *resource, const char *location)
114 {
115 	nvlist_t *nvl;
116 	int err = 0;
117 
118 	if (nvlist_xalloc(&nvl, NV_UNIQUE_NAME, &fmd.d_nva) != 0)
119 		fmd_panic("failed to xalloc fault nvlist");
120 
121 	err |= nvlist_add_uint8(nvl, FM_VERSION, FM_FAULT_VERSION);
122 	err |= nvlist_add_string(nvl, FM_CLASS, class);
123 	err |= nvlist_add_uint8(nvl, FM_FAULT_CERTAINTY, certainty);
124 
125 	if (asru != NULL)
126 		err |= nvlist_add_nvlist(nvl, FM_FAULT_ASRU, asru);
127 	if (fru != NULL)
128 		err |= nvlist_add_nvlist(nvl, FM_FAULT_FRU, fru);
129 	if (resource != NULL)
130 		err |= nvlist_add_nvlist(nvl, FM_FAULT_RESOURCE, resource);
131 	if (location != NULL)
132 		err |= nvlist_add_string(nvl, FM_FAULT_LOCATION, location);
133 
134 	if (err != 0)
135 		fmd_panic("failed to populate nvlist: %s\n", fmd_strerror(err));
136 
137 	return (nvl);
138 }
139 
140 nvlist_t *
141 fmd_protocol_list(const char *class, nvlist_t *de_fmri, const char *uuid,
142     const char *code, uint_t argc, nvlist_t **argv, uint8_t *flagv, int domsg,
143     struct timeval *tvp)
144 {
145 	int64_t tod[2];
146 	nvlist_t *nvl;
147 	int err = 0;
148 
149 	tod[0] = tvp->tv_sec;
150 	tod[1] = tvp->tv_usec;
151 
152 	if (nvlist_xalloc(&nvl, NV_UNIQUE_NAME, &fmd.d_nva) != 0)
153 		fmd_panic("failed to xalloc suspect list nvlist");
154 
155 	err |= nvlist_add_uint8(nvl, FM_VERSION, FM_SUSPECT_VERSION);
156 	err |= nvlist_add_string(nvl, FM_CLASS, class);
157 	err |= nvlist_add_string(nvl, FM_SUSPECT_UUID, uuid);
158 	err |= nvlist_add_string(nvl, FM_SUSPECT_DIAG_CODE, code);
159 	err |= nvlist_add_int64_array(nvl, FM_SUSPECT_DIAG_TIME, tod, 2);
160 	err |= nvlist_add_nvlist(nvl, FM_SUSPECT_DE, de_fmri);
161 	err |= nvlist_add_uint32(nvl, FM_SUSPECT_FAULT_SZ, argc);
162 
163 	if (!domsg) {
164 		err |= nvlist_add_boolean_value(nvl,
165 		    FM_SUSPECT_MESSAGE, B_FALSE);
166 	}
167 
168 	if (argc != 0) {
169 		err |= nvlist_add_nvlist_array(nvl,
170 		    FM_SUSPECT_FAULT_LIST, argv, argc);
171 		err |= nvlist_add_uint8_array(nvl,
172 		    FM_SUSPECT_FAULT_STATUS, flagv, argc);
173 	}
174 
175 	if (err != 0)
176 		fmd_panic("failed to populate nvlist: %s\n", fmd_strerror(err));
177 
178 	return (nvl);
179 }
180 
181 nvlist_t *
182 fmd_protocol_rsrc_asru(const char *class,
183     nvlist_t *fmri, const char *uuid, const char *code,
184     boolean_t faulty, boolean_t unusable, boolean_t message, nvlist_t *event,
185     struct timeval *tvp, boolean_t repaired, boolean_t replaced,
186     boolean_t acquitted)
187 {
188 	nvlist_t *nvl;
189 	int64_t tod[2];
190 	int err = 0;
191 
192 	tod[0] = tvp->tv_sec;
193 	tod[1] = tvp->tv_usec;
194 
195 	if (nvlist_xalloc(&nvl, NV_UNIQUE_NAME, &fmd.d_nva) != 0)
196 		fmd_panic("failed to xalloc resource nvlist");
197 
198 	err |= nvlist_add_uint8(nvl, FM_VERSION, FM_RSRC_VERSION);
199 	err |= nvlist_add_string(nvl, FM_CLASS, class);
200 	if (fmri != NULL)
201 		err |= nvlist_add_nvlist(nvl, FM_RSRC_RESOURCE, fmri);
202 
203 	if (uuid != NULL)
204 		err |= nvlist_add_string(nvl, FM_RSRC_ASRU_UUID, uuid);
205 
206 	if (code != NULL)
207 		err |= nvlist_add_string(nvl, FM_RSRC_ASRU_CODE, code);
208 
209 	err |= nvlist_add_boolean_value(nvl, FM_RSRC_ASRU_FAULTY, faulty);
210 	err |= nvlist_add_boolean_value(nvl, FM_RSRC_ASRU_REPAIRED, repaired);
211 	err |= nvlist_add_boolean_value(nvl, FM_RSRC_ASRU_REPLACED, replaced);
212 	err |= nvlist_add_boolean_value(nvl, FM_RSRC_ASRU_ACQUITTED, acquitted);
213 	err |= nvlist_add_boolean_value(nvl, FM_RSRC_ASRU_UNUSABLE, unusable);
214 	err |= nvlist_add_boolean_value(nvl, FM_SUSPECT_MESSAGE, message);
215 	err |= nvlist_add_int64_array(nvl, FM_SUSPECT_DIAG_TIME, tod, 2);
216 
217 	if (event != NULL)
218 		err |= nvlist_add_nvlist(nvl, FM_RSRC_ASRU_EVENT, event);
219 
220 	if (err != 0)
221 		fmd_panic("failed to populate nvlist: %s\n", fmd_strerror(err));
222 
223 	return (nvl);
224 }
225 
226 nvlist_t *
227 fmd_protocol_fmderror(int errnum, const char *format, va_list ap)
228 {
229 	uint64_t ena = fmd_ena();
230 	nvlist_t *nvl;
231 	int err = 0;
232 	char c, *msg;
233 	size_t len;
234 
235 	if (nvlist_xalloc(&nvl, NV_UNIQUE_NAME, &fmd.d_nva) != 0)
236 		return (NULL);
237 
238 	len = vsnprintf(&c, 1, format, ap);
239 	msg = alloca(len + 1);
240 	(void) vsnprintf(msg, len + 1, format, ap);
241 
242 	if (msg[len] == '\n')
243 		msg[len] = '\0';
244 
245 	err |= nvlist_add_uint8(nvl, FM_VERSION, FM_EREPORT_VERSION);
246 	err |= nvlist_add_string(nvl, FM_CLASS, fmd_errclass(errnum));
247 	err |= nvlist_add_uint64(nvl, FM_EREPORT_ENA, ena);
248 	err |= nvlist_add_string(nvl, FMD_ERR_MOD_MSG, msg);
249 
250 	if (err != 0) {
251 		nvlist_free(nvl);
252 		return (NULL);
253 	}
254 
255 	return (nvl);
256 }
257 
258 nvlist_t *
259 fmd_protocol_moderror(fmd_module_t *mp, int oserr, const char *msg)
260 {
261 	uint64_t ena = fmd_ena();
262 	nvlist_t *nvl, *fmri;
263 	int err = 0;
264 
265 	if (nvlist_xalloc(&nvl, NV_UNIQUE_NAME, &fmd.d_nva) != 0)
266 		fmd_panic("failed to xalloc module error nvlist");
267 
268 	if (mp->mod_fmri == NULL)
269 		fmri = fmd_protocol_fmri_module(mp);
270 	else
271 		fmri = mp->mod_fmri;
272 
273 	err |= nvlist_add_uint8(nvl, FM_VERSION, FM_EREPORT_VERSION);
274 	err |= nvlist_add_string(nvl, FM_CLASS, fmd_errclass(EFMD_MODULE));
275 	err |= nvlist_add_nvlist(nvl, FM_EREPORT_DETECTOR, fmri);
276 	err |= nvlist_add_uint64(nvl, FM_EREPORT_ENA, ena);
277 	err |= nvlist_add_string(nvl, FMD_ERR_MOD_MSG, msg);
278 
279 	if (mp->mod_fmri == NULL)
280 		nvlist_free(fmri);
281 
282 	if (oserr != 0) {
283 		err |= nvlist_add_int32(nvl, FMD_ERR_MOD_ERRNO, oserr);
284 		err |= nvlist_add_string(nvl, FMD_ERR_MOD_ERRCLASS,
285 		    fmd_errclass(oserr));
286 	}
287 
288 	if (err != 0)
289 		fmd_panic("failed to populate nvlist: %s\n", fmd_strerror(err));
290 
291 	return (nvl);
292 }
293 
294 nvlist_t *
295 fmd_protocol_xprt_ctl(fmd_module_t *mp, const char *class, uint8_t version)
296 {
297 	nvlist_t *nvl;
298 	int err = 0;
299 
300 	if (nvlist_xalloc(&nvl, NV_UNIQUE_NAME, &fmd.d_nva) != 0)
301 		fmd_panic("failed to xalloc rsrc xprt nvlist");
302 
303 	err |= nvlist_add_uint8(nvl, FM_VERSION, version);
304 	err |= nvlist_add_string(nvl, FM_CLASS, class);
305 	err |= nvlist_add_nvlist(nvl, FM_RSRC_RESOURCE, mp->mod_fmri);
306 
307 	if (err != 0)
308 		fmd_panic("failed to populate nvlist: %s\n", fmd_strerror(err));
309 
310 	return (nvl);
311 }
312 
313 nvlist_t *
314 fmd_protocol_xprt_sub(fmd_module_t *mp,
315     const char *class, uint8_t version, const char *subclass)
316 {
317 	nvlist_t *nvl = fmd_protocol_xprt_ctl(mp, class, version);
318 	int err = nvlist_add_string(nvl, FM_RSRC_XPRT_SUBCLASS, subclass);
319 
320 	if (err != 0)
321 		fmd_panic("failed to populate nvlist: %s\n", fmd_strerror(err));
322 
323 	return (nvl);
324 }
325 
326 nvlist_t *
327 fmd_protocol_xprt_uuclose(fmd_module_t *mp, const char *class, uint8_t version,
328     const char *uuid)
329 {
330 	nvlist_t *nvl = fmd_protocol_xprt_ctl(mp, class, version);
331 	int err = nvlist_add_string(nvl, FM_RSRC_XPRT_UUID, uuid);
332 
333 	if (err != 0)
334 		fmd_panic("failed to populate nvlist: %s\n", fmd_strerror(err));
335 
336 	return (nvl);
337 }
338