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