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