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