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