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