xref: /titanic_41/usr/src/cmd/fm/fmd/common/fmd_protocol.c (revision b7d3956b92a285d8dac2c7f5f7e28d2ef5347ef8)
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)
186 {
187 	nvlist_t *nvl;
188 	int64_t tod[2];
189 	int err = 0;
190 
191 	tod[0] = tvp->tv_sec;
192 	tod[1] = tvp->tv_usec;
193 
194 	if (nvlist_xalloc(&nvl, NV_UNIQUE_NAME, &fmd.d_nva) != 0)
195 		fmd_panic("failed to xalloc resource nvlist");
196 
197 	err |= nvlist_add_uint8(nvl, FM_VERSION, FM_RSRC_VERSION);
198 	err |= nvlist_add_string(nvl, FM_CLASS, class);
199 	if (fmri != NULL)
200 		err |= nvlist_add_nvlist(nvl, FM_RSRC_RESOURCE, fmri);
201 
202 	if (uuid != NULL)
203 		err |= nvlist_add_string(nvl, FM_RSRC_ASRU_UUID, uuid);
204 
205 	if (code != NULL)
206 		err |= nvlist_add_string(nvl, FM_RSRC_ASRU_CODE, code);
207 
208 	err |= nvlist_add_boolean_value(nvl, FM_RSRC_ASRU_FAULTY, faulty);
209 	err |= nvlist_add_boolean_value(nvl, FM_RSRC_ASRU_UNUSABLE, unusable);
210 	err |= nvlist_add_boolean_value(nvl, FM_SUSPECT_MESSAGE, message);
211 	err |= nvlist_add_int64_array(nvl, FM_SUSPECT_DIAG_TIME, tod, 2);
212 
213 	if (event != NULL)
214 		err |= nvlist_add_nvlist(nvl, FM_RSRC_ASRU_EVENT, event);
215 
216 	if (err != 0)
217 		fmd_panic("failed to populate nvlist: %s\n", fmd_strerror(err));
218 
219 	return (nvl);
220 }
221 
222 nvlist_t *
223 fmd_protocol_fmderror(int errnum, const char *format, va_list ap)
224 {
225 	uint64_t ena = fmd_ena();
226 	nvlist_t *nvl;
227 	int err = 0;
228 	char c, *msg;
229 	size_t len;
230 
231 	if (nvlist_xalloc(&nvl, NV_UNIQUE_NAME, &fmd.d_nva) != 0)
232 		return (NULL);
233 
234 	len = vsnprintf(&c, 1, format, ap);
235 	msg = alloca(len + 1);
236 	(void) vsnprintf(msg, len + 1, format, ap);
237 
238 	if (msg[len] == '\n')
239 		msg[len] = '\0';
240 
241 	err |= nvlist_add_uint8(nvl, FM_VERSION, FM_EREPORT_VERSION);
242 	err |= nvlist_add_string(nvl, FM_CLASS, fmd_errclass(errnum));
243 	err |= nvlist_add_uint64(nvl, FM_EREPORT_ENA, ena);
244 	err |= nvlist_add_string(nvl, FMD_ERR_MOD_MSG, msg);
245 
246 	if (err != 0) {
247 		nvlist_free(nvl);
248 		return (NULL);
249 	}
250 
251 	return (nvl);
252 }
253 
254 nvlist_t *
255 fmd_protocol_moderror(fmd_module_t *mp, int oserr, const char *msg)
256 {
257 	uint64_t ena = fmd_ena();
258 	nvlist_t *nvl, *fmri;
259 	int err = 0;
260 
261 	if (nvlist_xalloc(&nvl, NV_UNIQUE_NAME, &fmd.d_nva) != 0)
262 		fmd_panic("failed to xalloc module error nvlist");
263 
264 	if (mp->mod_fmri == NULL)
265 		fmri = fmd_protocol_fmri_module(mp);
266 	else
267 		fmri = mp->mod_fmri;
268 
269 	err |= nvlist_add_uint8(nvl, FM_VERSION, FM_EREPORT_VERSION);
270 	err |= nvlist_add_string(nvl, FM_CLASS, fmd_errclass(EFMD_MODULE));
271 	err |= nvlist_add_nvlist(nvl, FM_EREPORT_DETECTOR, fmri);
272 	err |= nvlist_add_uint64(nvl, FM_EREPORT_ENA, ena);
273 	err |= nvlist_add_string(nvl, FMD_ERR_MOD_MSG, msg);
274 
275 	if (mp->mod_fmri == NULL)
276 		nvlist_free(fmri);
277 
278 	if (oserr != 0) {
279 		err |= nvlist_add_int32(nvl, FMD_ERR_MOD_ERRNO, oserr);
280 		err |= nvlist_add_string(nvl, FMD_ERR_MOD_ERRCLASS,
281 		    fmd_errclass(oserr));
282 	}
283 
284 	if (err != 0)
285 		fmd_panic("failed to populate nvlist: %s\n", fmd_strerror(err));
286 
287 	return (nvl);
288 }
289 
290 nvlist_t *
291 fmd_protocol_xprt_ctl(fmd_module_t *mp, const char *class, uint8_t version)
292 {
293 	nvlist_t *nvl;
294 	int err = 0;
295 
296 	if (nvlist_xalloc(&nvl, NV_UNIQUE_NAME, &fmd.d_nva) != 0)
297 		fmd_panic("failed to xalloc rsrc xprt nvlist");
298 
299 	err |= nvlist_add_uint8(nvl, FM_VERSION, version);
300 	err |= nvlist_add_string(nvl, FM_CLASS, class);
301 	err |= nvlist_add_nvlist(nvl, FM_RSRC_RESOURCE, mp->mod_fmri);
302 
303 	if (err != 0)
304 		fmd_panic("failed to populate nvlist: %s\n", fmd_strerror(err));
305 
306 	return (nvl);
307 }
308 
309 nvlist_t *
310 fmd_protocol_xprt_sub(fmd_module_t *mp,
311     const char *class, uint8_t version, const char *subclass)
312 {
313 	nvlist_t *nvl = fmd_protocol_xprt_ctl(mp, class, version);
314 	int err = nvlist_add_string(nvl, FM_RSRC_XPRT_SUBCLASS, subclass);
315 
316 	if (err != 0)
317 		fmd_panic("failed to populate nvlist: %s\n", fmd_strerror(err));
318 
319 	return (nvl);
320 }
321 
322 nvlist_t *
323 fmd_protocol_xprt_uuclose(fmd_module_t *mp, const char *class, uint8_t version,
324     const char *uuid)
325 {
326 	nvlist_t *nvl = fmd_protocol_xprt_ctl(mp, class, version);
327 	int err = nvlist_add_string(nvl, FM_RSRC_XPRT_UUID, uuid);
328 
329 	if (err != 0)
330 		fmd_panic("failed to populate nvlist: %s\n", fmd_strerror(err));
331 
332 	return (nvl);
333 }
334