xref: /titanic_50/usr/src/cmd/fm/fmd/common/fmd_api.c (revision e2ca2865a6870e9c6cbef6becbcc68cafde64537)
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/types.h>
28 #include <sys/fm/protocol.h>
29 #include <fm/topo_hc.h>
30 
31 #include <unistd.h>
32 #include <signal.h>
33 #include <limits.h>
34 #include <syslog.h>
35 #include <alloca.h>
36 #include <stddef.h>
37 
38 #include <fmd_module.h>
39 #include <fmd_api.h>
40 #include <fmd_string.h>
41 #include <fmd_subr.h>
42 #include <fmd_error.h>
43 #include <fmd_event.h>
44 #include <fmd_eventq.h>
45 #include <fmd_dispq.h>
46 #include <fmd_timerq.h>
47 #include <fmd_thread.h>
48 #include <fmd_ustat.h>
49 #include <fmd_case.h>
50 #include <fmd_protocol.h>
51 #include <fmd_buf.h>
52 #include <fmd_asru.h>
53 #include <fmd_fmri.h>
54 #include <fmd_topo.h>
55 #include <fmd_ckpt.h>
56 #include <fmd_xprt.h>
57 
58 #include <fmd.h>
59 
60 /*
61  * Table of configuration file variable types ops-vector pointers.  We use this
62  * to convert from the property description array specified by the module to an
63  * array of fmd_conf_formal_t's.  The order of this array must match the order
64  * of #define values specified in <fmd_api.h> (i.e. FMD_TYPE_BOOL must be 0).
65  * For now, the fmd_conf_list and fmd_conf_path types are not supported as we
66  * do not believe modules need them and they would require more complexity.
67  */
68 static const fmd_conf_ops_t *const _fmd_prop_ops[] = {
69 	&fmd_conf_bool,		/* FMD_TYPE_BOOL */
70 	&fmd_conf_int32,	/* FMD_TYPE_INT32 */
71 	&fmd_conf_uint32,	/* FMD_TYPE_UINT32 */
72 	&fmd_conf_int64,	/* FMD_TYPE_INT64 */
73 	&fmd_conf_uint64,	/* FMD_TYPE_UINT64 */
74 	&fmd_conf_string,	/* FMD_TYPE_STRING */
75 	&fmd_conf_time,		/* FMD_TYPE_TIME */
76 	&fmd_conf_size,		/* FMD_TYPE_SIZE */
77 };
78 
79 static void fmd_api_verror(fmd_module_t *, int, const char *, va_list)
80     __NORETURN;
81 static void fmd_api_error(fmd_module_t *, int, const char *, ...) __NORETURN;
82 
83 /*
84  * fmd_api_vxerror() provides the engine underlying the fmd_hdl_[v]error() API
85  * calls and the fmd_api_[v]error() utility routine defined below.  The routine
86  * formats the error, optionally associated with a particular errno code 'err',
87  * and logs it as an ereport associated with the calling module.  Depending on
88  * other optional properties, we also emit a message to stderr and to syslog.
89  */
90 static void
91 fmd_api_vxerror(fmd_module_t *mp, int err, const char *format, va_list ap)
92 {
93 	int raw_err = err;
94 	nvlist_t *nvl;
95 	fmd_event_t *e;
96 	char *class, *msg;
97 	size_t len1, len2;
98 	char c;
99 
100 	/*
101 	 * fmd_api_vxerror() counts as both an error of class EFMD_MODULE
102 	 * as well as an instance of 'err' w.r.t. our internal bean counters.
103 	 */
104 	(void) pthread_mutex_lock(&fmd.d_err_lock);
105 	fmd.d_errstats[EFMD_MODULE - EFMD_UNKNOWN].fmds_value.ui64++;
106 
107 	if (err > EFMD_UNKNOWN && err < EFMD_END)
108 		fmd.d_errstats[err - EFMD_UNKNOWN].fmds_value.ui64++;
109 
110 	(void) pthread_mutex_unlock(&fmd.d_err_lock);
111 
112 	/*
113 	 * Format the message using vsnprintf().  As usual, if the format has a
114 	 * newline in it, it is printed alone; otherwise strerror() is added.
115 	 */
116 	if (strchr(format, '\n') != NULL)
117 		err = 0; /* err is not relevant in the message */
118 
119 	len1 = vsnprintf(&c, 1, format, ap);
120 	len2 = err != 0 ? snprintf(&c, 1, ": %s\n", fmd_strerror(err)) : 0;
121 
122 	msg = fmd_alloc(len1 + len2 + 1, FMD_SLEEP);
123 	(void) vsnprintf(msg, len1 + 1, format, ap);
124 
125 	if (err != 0) {
126 		(void) snprintf(&msg[len1], len2 + 1,
127 		    ": %s\n", fmd_strerror(err));
128 	}
129 
130 	/*
131 	 * Create an error event corresponding to the error, insert it into the
132 	 * error log, and dispatch it to the fmd-self-diagnosis engine.
133 	 */
134 	if (mp != fmd.d_self && (raw_err != EFMD_HDL_ABORT || fmd.d_running)) {
135 		if ((c = msg[len1 + len2 - 1]) == '\n')
136 			msg[len1 + len2 - 1] = '\0'; /* strip \n for event */
137 
138 		nvl = fmd_protocol_moderror(mp, err, msg);
139 
140 		if (c == '\n')
141 			msg[len1 + len2 - 1] = c;
142 
143 		(void) nvlist_lookup_string(nvl, FM_CLASS, &class);
144 		e = fmd_event_create(FMD_EVT_PROTOCOL, FMD_HRT_NOW, nvl, class);
145 
146 		(void) pthread_rwlock_rdlock(&fmd.d_log_lock);
147 		fmd_log_append(fmd.d_errlog, e, NULL);
148 		(void) pthread_rwlock_unlock(&fmd.d_log_lock);
149 
150 		fmd_event_transition(e, FMD_EVS_ACCEPTED);
151 		fmd_event_commit(e);
152 
153 		fmd_dispq_dispatch(fmd.d_disp, e, class);
154 	}
155 
156 	/*
157 	 * Similar to fmd_vdebug(), if the debugging switches are enabled we
158 	 * echo the module name and message to stderr and/or syslog.  Unlike
159 	 * fmd_vdebug(), we also print to stderr if foreground mode is enabled.
160 	 * We also print the message if a built-in module is aborting before
161 	 * fmd has detached from its parent (e.g. default transport failure).
162 	 */
163 	if (fmd.d_fg || (fmd.d_hdl_dbout & FMD_DBOUT_STDERR) || (
164 	    raw_err == EFMD_HDL_ABORT && !fmd.d_running)) {
165 		(void) pthread_mutex_lock(&fmd.d_err_lock);
166 		(void) fprintf(stderr, "%s: %s: %s",
167 		    fmd.d_pname, mp->mod_name, msg);
168 		(void) pthread_mutex_unlock(&fmd.d_err_lock);
169 	}
170 
171 	if (fmd.d_hdl_dbout & FMD_DBOUT_SYSLOG) {
172 		syslog(LOG_ERR | LOG_DAEMON, "%s ERROR: %s: %s",
173 		    fmd.d_pname, mp->mod_name, msg);
174 	}
175 
176 	fmd_free(msg, len1 + len2 + 1);
177 }
178 
179 /*PRINTFLIKE3*/
180 static void
181 fmd_api_xerror(fmd_module_t *mp, int err, const char *format, ...)
182 {
183 	va_list ap;
184 
185 	va_start(ap, format);
186 	fmd_api_vxerror(mp, err, format, ap);
187 	va_end(ap);
188 }
189 
190 /*
191  * fmd_api_verror() is a wrapper around fmd_api_vxerror() for API subroutines.
192  * It calls fmd_module_unlock() on behalf of its caller, logs the error, and
193  * then aborts the API call and the surrounding module entry point by doing an
194  * fmd_module_abort(), which longjmps to the place where we entered the module.
195  */
196 static void
197 fmd_api_verror(fmd_module_t *mp, int err, const char *format, va_list ap)
198 {
199 	if (fmd_module_locked(mp))
200 		fmd_module_unlock(mp);
201 
202 	fmd_api_vxerror(mp, err, format, ap);
203 	fmd_module_abort(mp, err);
204 }
205 
206 /*PRINTFLIKE3*/
207 static void
208 fmd_api_error(fmd_module_t *mp, int err, const char *format, ...)
209 {
210 	va_list ap;
211 
212 	va_start(ap, format);
213 	fmd_api_verror(mp, err, format, ap);
214 	va_end(ap);
215 }
216 
217 /*
218  * Common code for fmd_api_module_lock() and fmd_api_transport_impl().  This
219  * code verifies that the handle is valid and associated with a proper thread.
220  */
221 static fmd_module_t *
222 fmd_api_module(fmd_hdl_t *hdl)
223 {
224 	fmd_thread_t *tp;
225 	fmd_module_t *mp;
226 
227 	/*
228 	 * If our TSD is not present at all, this is either a serious bug or
229 	 * someone has created a thread behind our back and is using fmd's API.
230 	 * We can't call fmd_api_error() because we can't be sure that we can
231 	 * unwind our state back to an enclosing fmd_module_dispatch(), so we
232 	 * must panic instead.  This is likely a module design or coding error.
233 	 */
234 	if ((tp = pthread_getspecific(fmd.d_key)) == NULL) {
235 		fmd_panic("fmd module api call made using "
236 		    "client handle %p from unknown thread\n", (void *)hdl);
237 	}
238 
239 	/*
240 	 * If our TSD refers to the root module and is a door server thread,
241 	 * then it was created asynchronously at the request of a module but
242 	 * is using now the module API as an auxiliary module thread.  We reset
243 	 * tp->thr_mod to the module handle so it can act as a module thread.
244 	 */
245 	if (tp->thr_mod == fmd.d_rmod && tp->thr_func == &fmd_door_server)
246 		tp->thr_mod = (fmd_module_t *)hdl;
247 
248 	if ((mp = tp->thr_mod) != (fmd_module_t *)hdl) {
249 		fmd_api_error(mp, EFMD_HDL_INVAL,
250 		    "client handle %p is not valid\n", (void *)hdl);
251 	}
252 
253 	if (mp->mod_flags & FMD_MOD_FAIL) {
254 		fmd_api_error(mp, EFMD_MOD_FAIL,
255 		    "module has experienced an unrecoverable error\n");
256 	}
257 
258 	return (mp);
259 }
260 
261 /*
262  * fmd_api_module_lock() is used as a wrapper around fmd_module_lock() and a
263  * common prologue to each fmd_api.c routine.  It verifies that the handle is
264  * valid and owned by the current server thread, locks the handle, and then
265  * verifies that the caller is performing an operation on a registered handle.
266  * If any tests fail, the entire API call is aborted by fmd_api_error().
267  */
268 static fmd_module_t *
269 fmd_api_module_lock(fmd_hdl_t *hdl)
270 {
271 	fmd_module_t *mp = fmd_api_module(hdl);
272 
273 	fmd_module_lock(mp);
274 
275 	if (mp->mod_info == NULL) {
276 		fmd_api_error(mp, EFMD_HDL_NOTREG,
277 		    "client handle %p has not been registered\n", (void *)hdl);
278 	}
279 
280 	return (mp);
281 }
282 
283 /*
284  * Utility function for API entry points that accept fmd_case_t's.  We cast cp
285  * to fmd_case_impl_t and check to make sure the case is owned by the caller.
286  */
287 static fmd_case_impl_t *
288 fmd_api_case_impl(fmd_module_t *mp, fmd_case_t *cp)
289 {
290 	fmd_case_impl_t *cip = (fmd_case_impl_t *)cp;
291 
292 	if (cip == NULL || cip->ci_mod != mp) {
293 		fmd_api_error(mp, EFMD_CASE_OWNER,
294 		    "case %p is invalid or not owned by caller\n", (void *)cip);
295 	}
296 
297 	return (cip);
298 }
299 
300 /*
301  * Utility function for API entry points that accept fmd_xprt_t's.  We cast xp
302  * to fmd_transport_t and check to make sure the case is owned by the caller.
303  * Note that we could make this check safer by actually walking mp's transport
304  * list, but that requires holding the module lock and this routine needs to be
305  * MT-hot w.r.t. auxiliary module threads.  Ultimately any loadable module can
306  * cause us to crash anyway, so we optimize for scalability over safety here.
307  */
308 static fmd_xprt_impl_t *
309 fmd_api_transport_impl(fmd_hdl_t *hdl, fmd_xprt_t *xp)
310 {
311 	fmd_module_t *mp = fmd_api_module(hdl);
312 	fmd_xprt_impl_t *xip = (fmd_xprt_impl_t *)xp;
313 
314 	if (xip == NULL || xip->xi_queue->eq_mod != mp) {
315 		fmd_api_error(mp, EFMD_XPRT_OWNER,
316 		    "xprt %p is invalid or not owned by caller\n", (void *)xp);
317 	}
318 
319 	return (xip);
320 }
321 
322 /*
323  * fmd_hdl_register() is the one function which cannot use fmd_api_error() to
324  * report errors, because that routine causes the module to abort.  Failure to
325  * register is instead handled by having fmd_hdl_register() return an error to
326  * the _fmd_init() function and then detecting no registration when it returns.
327  * So we use this routine for fmd_hdl_register() error paths instead.
328  */
329 static int
330 fmd_hdl_register_error(fmd_module_t *mp, int err)
331 {
332 	if (fmd_module_locked(mp))
333 		fmd_module_unlock(mp);
334 
335 	fmd_api_xerror(mp, err, "failed to register");
336 	return (fmd_set_errno(err));
337 }
338 
339 static void
340 fmd_hdl_nop(void)
341 {
342 	/* empty function for use with unspecified module entry points */
343 }
344 
345 int
346 fmd_hdl_register(fmd_hdl_t *hdl, int version, const fmd_hdl_info_t *mip)
347 {
348 	fmd_thread_t *tp = pthread_getspecific(fmd.d_key);
349 	fmd_module_t *mp = tp->thr_mod;
350 
351 	const fmd_prop_t *prop;
352 	const fmd_conf_path_t *pap;
353 	fmd_conf_formal_t *cfp;
354 	fmd_hdl_ops_t ops;
355 
356 	const char *conf = NULL;
357 	char buf[PATH_MAX];
358 	int i;
359 
360 	if (mp != (fmd_module_t *)hdl)
361 		return (fmd_hdl_register_error(mp, EFMD_HDL_INVAL));
362 
363 	fmd_module_lock(mp);
364 
365 	/*
366 	 * First perform some sanity checks on our input.  The API version must
367 	 * be supported by FMD and the handle can only be registered once by
368 	 * the module thread to which we assigned this client handle.  The info
369 	 * provided for the handle must be valid and have the minimal settings.
370 	 */
371 	if (version > FMD_API_VERSION_4)
372 		return (fmd_hdl_register_error(mp, EFMD_VER_NEW));
373 
374 	if (version < FMD_API_VERSION_1)
375 		return (fmd_hdl_register_error(mp, EFMD_VER_OLD));
376 
377 	if (mp->mod_conf != NULL)
378 		return (fmd_hdl_register_error(mp, EFMD_HDL_REG));
379 
380 	if (pthread_self() != mp->mod_thread->thr_tid)
381 		return (fmd_hdl_register_error(mp, EFMD_HDL_TID));
382 
383 	if (mip == NULL || mip->fmdi_desc == NULL ||
384 	    mip->fmdi_vers == NULL || mip->fmdi_ops == NULL)
385 		return (fmd_hdl_register_error(mp, EFMD_HDL_INFO));
386 
387 	/*
388 	 * Copy the module's ops vector into a local variable to account for
389 	 * changes in the module ABI.  Then if any of the optional entry points
390 	 * are NULL, set them to nop so we don't have to check before calling.
391 	 */
392 	bzero(&ops, sizeof (ops));
393 
394 	if (version < FMD_API_VERSION_3)
395 		bcopy(mip->fmdi_ops, &ops, offsetof(fmd_hdl_ops_t, fmdo_send));
396 	else if (version < FMD_API_VERSION_4)
397 		bcopy(mip->fmdi_ops, &ops,
398 		    offsetof(fmd_hdl_ops_t, fmdo_topo));
399 	else
400 		bcopy(mip->fmdi_ops, &ops, sizeof (ops));
401 
402 	if (ops.fmdo_recv == NULL)
403 		ops.fmdo_recv = (void (*)())fmd_hdl_nop;
404 	if (ops.fmdo_timeout == NULL)
405 		ops.fmdo_timeout = (void (*)())fmd_hdl_nop;
406 	if (ops.fmdo_close == NULL)
407 		ops.fmdo_close = (void (*)())fmd_hdl_nop;
408 	if (ops.fmdo_stats == NULL)
409 		ops.fmdo_stats = (void (*)())fmd_hdl_nop;
410 	if (ops.fmdo_gc == NULL)
411 		ops.fmdo_gc = (void (*)())fmd_hdl_nop;
412 	if (ops.fmdo_send == NULL)
413 		ops.fmdo_send = (int (*)())fmd_hdl_nop;
414 	if (ops.fmdo_topo == NULL)
415 		ops.fmdo_topo = (void (*)())fmd_hdl_nop;
416 
417 	/*
418 	 * Make two passes through the property array to initialize the formals
419 	 * to use for processing the module's .conf file.  In the first pass,
420 	 * we validate the types and count the number of properties.  In the
421 	 * second pass we copy the strings and fill in the appropriate ops.
422 	 */
423 	for (prop = mip->fmdi_props, i = 0; prop != NULL &&
424 	    prop->fmdp_name != NULL; prop++, i++) {
425 		if (prop->fmdp_type >=
426 		    sizeof (_fmd_prop_ops) / sizeof (_fmd_prop_ops[0])) {
427 			fmd_api_xerror(mp, EFMD_HDL_PROP,
428 			    "property %s uses invalid type %u\n",
429 			    prop->fmdp_name, prop->fmdp_type);
430 			return (fmd_hdl_register_error(mp, EFMD_HDL_PROP));
431 		}
432 	}
433 
434 	mp->mod_argc = i;
435 	mp->mod_argv = fmd_zalloc(sizeof (fmd_conf_formal_t) * i, FMD_SLEEP);
436 
437 	prop = mip->fmdi_props;
438 	cfp = mp->mod_argv;
439 
440 	for (i = 0; i < mp->mod_argc; i++, prop++, cfp++) {
441 		cfp->cf_name = fmd_strdup(prop->fmdp_name, FMD_SLEEP);
442 		cfp->cf_ops = _fmd_prop_ops[prop->fmdp_type];
443 		cfp->cf_default = fmd_strdup(prop->fmdp_defv, FMD_SLEEP);
444 	}
445 
446 	/*
447 	 * If this module came from an on-disk file, compute the name of the
448 	 * corresponding .conf file and parse properties from it if it exists.
449 	 */
450 	if (mp->mod_path != NULL) {
451 		(void) strlcpy(buf, mp->mod_path, sizeof (buf));
452 		(void) fmd_strdirname(buf);
453 
454 		(void) strlcat(buf, "/", sizeof (buf));
455 		(void) strlcat(buf, mp->mod_name, sizeof (buf));
456 		(void) strlcat(buf, ".conf", sizeof (buf));
457 
458 		if (access(buf, F_OK) == 0)
459 			conf = buf;
460 	}
461 
462 	if ((mp->mod_conf = fmd_conf_open(conf,
463 	    mp->mod_argc, mp->mod_argv, 0)) == NULL)
464 		return (fmd_hdl_register_error(mp, EFMD_MOD_CONF));
465 
466 	fmd_conf_propagate(fmd.d_conf, mp->mod_conf, mp->mod_name);
467 
468 	/*
469 	 * Look up the list of the libdiagcode dictionaries associated with the
470 	 * module.  If none were specified, use the value from daemon's config.
471 	 * We only fail if the module specified an explicit dictionary.
472 	 */
473 	(void) fmd_conf_getprop(mp->mod_conf, FMD_PROP_DICTIONARIES, &pap);
474 	if (pap->cpa_argc == 0 && mp->mod_ops == &fmd_bltin_ops)
475 		(void) fmd_conf_getprop(fmd.d_conf, "self.dict", &pap);
476 
477 	for (i = 0; i < pap->cpa_argc; i++) {
478 		if (fmd_module_dc_opendict(mp, pap->cpa_argv[i]) != 0) {
479 			fmd_api_xerror(mp, errno,
480 			    "failed to open dictionary %s", pap->cpa_argv[i]);
481 			return (fmd_hdl_register_error(mp, EFMD_MOD_CONF));
482 		}
483 	}
484 
485 	/*
486 	 * Make a copy of the handle information and store it in mod_info.  We
487 	 * do not need to bother copying fmdi_props since they're already read.
488 	 */
489 	mp->mod_info = fmd_alloc(sizeof (fmd_hdl_info_t), FMD_SLEEP);
490 	mp->mod_info->fmdi_desc = fmd_strdup(mip->fmdi_desc, FMD_SLEEP);
491 	mp->mod_info->fmdi_vers = fmd_strdup(mip->fmdi_vers, FMD_SLEEP);
492 	mp->mod_info->fmdi_ops = fmd_alloc(sizeof (fmd_hdl_ops_t), FMD_SLEEP);
493 	bcopy(&ops, (void *)mp->mod_info->fmdi_ops, sizeof (fmd_hdl_ops_t));
494 	mp->mod_info->fmdi_props = NULL;
495 
496 	/*
497 	 * Store a copy of module version in mp for fmd_scheme_fmd_present()
498 	 */
499 	if (mp->mod_vers == NULL)
500 		mp->mod_vers = fmd_strdup(mip->fmdi_vers, FMD_SLEEP);
501 
502 	/*
503 	 * Allocate an FMRI representing this module.  We'll use this later
504 	 * if the module decides to publish any events (e.g. list.suspects).
505 	 */
506 	mp->mod_fmri = fmd_protocol_fmri_module(mp);
507 
508 	/*
509 	 * Any subscriptions specified in the conf file are now stored in the
510 	 * corresponding property.  Add all of these to the dispatch queue.
511 	 */
512 	(void) fmd_conf_getprop(mp->mod_conf, FMD_PROP_SUBSCRIPTIONS, &pap);
513 
514 	for (i = 0; i < pap->cpa_argc; i++) {
515 		fmd_dispq_insert(fmd.d_disp, mp->mod_queue, pap->cpa_argv[i]);
516 		fmd_xprt_subscribe_all(pap->cpa_argv[i]);
517 	}
518 
519 	/*
520 	 * Unlock the module and restore any pre-existing module checkpoint.
521 	 * If the checkpoint is missing or corrupt, we just keep going.
522 	 */
523 	fmd_module_unlock(mp);
524 	fmd_ckpt_restore(mp);
525 	return (0);
526 }
527 
528 /*
529  * If an auxiliary thread exists for the specified module at unregistration
530  * time, send it an asynchronous cancellation to force it to exit and then
531  * join with it (we expect this to either succeed quickly or return ESRCH).
532  * Once this is complete we can destroy the associated fmd_thread_t data.
533  */
534 static void
535 fmd_module_thrcancel(fmd_idspace_t *ids, id_t id, fmd_module_t *mp)
536 {
537 	fmd_thread_t *tp = fmd_idspace_getspecific(ids, id);
538 
539 	fmd_dprintf(FMD_DBG_MOD, "cancelling %s auxiliary thread %u\n",
540 	    mp->mod_name, tp->thr_tid);
541 
542 	ASSERT(tp->thr_tid == id);
543 	(void) pthread_cancel(tp->thr_tid);
544 	(void) pthread_join(tp->thr_tid, NULL);
545 
546 	fmd_thread_destroy(tp, FMD_THREAD_NOJOIN);
547 }
548 
549 void
550 fmd_module_unregister(fmd_module_t *mp)
551 {
552 	fmd_conf_formal_t *cfp = mp->mod_argv;
553 	const fmd_conf_path_t *pap;
554 	fmd_case_t *cp;
555 	fmd_xprt_t *xp;
556 	int i;
557 
558 	TRACE((FMD_DBG_MOD, "unregister %p (%s)", (void *)mp, mp->mod_name));
559 	ASSERT(fmd_module_locked(mp));
560 
561 	/*
562 	 * If any transports are still open, they have send threads that are
563 	 * using the module handle: shut them down and join with these threads.
564 	 */
565 	while ((xp = fmd_list_next(&mp->mod_transports)) != NULL)
566 		fmd_xprt_destroy(xp);
567 
568 	/*
569 	 * If any auxiliary threads exist, they may be using our module handle,
570 	 * and therefore could cause a fault as soon as we start destroying it.
571 	 * Module writers should clean up any threads before unregistering: we
572 	 * forcibly cancel any remaining auxiliary threads before proceeding.
573 	 */
574 	fmd_idspace_apply(mp->mod_threads,
575 	    (void (*)())fmd_module_thrcancel, mp);
576 
577 	if (mp->mod_error == 0)
578 		fmd_ckpt_save(mp); /* take one more checkpoint if needed */
579 
580 	/*
581 	 * Delete any cases associated with the module (UNSOLVED, SOLVED, or
582 	 * CLOSE_WAIT) as if fmdo_close() has finished processing them.
583 	 */
584 	while ((cp = fmd_list_next(&mp->mod_cases)) != NULL)
585 		fmd_case_delete(cp);
586 
587 	fmd_ustat_delete_references(mp->mod_ustat);
588 	(void) fmd_conf_getprop(mp->mod_conf, FMD_PROP_SUBSCRIPTIONS, &pap);
589 
590 	for (i = 0; i < pap->cpa_argc; i++) {
591 		fmd_xprt_unsubscribe_all(pap->cpa_argv[i]);
592 		fmd_dispq_delete(fmd.d_disp, mp->mod_queue, pap->cpa_argv[i]);
593 	}
594 
595 	fmd_conf_close(mp->mod_conf);
596 	mp->mod_conf = NULL;
597 
598 	for (i = 0; i < mp->mod_argc; i++, cfp++) {
599 		fmd_strfree((char *)cfp->cf_name);
600 		fmd_strfree((char *)cfp->cf_default);
601 	}
602 
603 	fmd_free(mp->mod_argv, sizeof (fmd_conf_formal_t) * mp->mod_argc);
604 	mp->mod_argv = NULL;
605 	mp->mod_argc = 0;
606 
607 	nvlist_free(mp->mod_fmri);
608 	mp->mod_fmri = NULL;
609 
610 	fmd_strfree((char *)mp->mod_info->fmdi_desc);
611 	fmd_strfree((char *)mp->mod_info->fmdi_vers);
612 	fmd_free((void *)mp->mod_info->fmdi_ops, sizeof (fmd_hdl_ops_t));
613 	fmd_free(mp->mod_info, sizeof (fmd_hdl_info_t));
614 	mp->mod_info = NULL;
615 
616 	fmd_eventq_abort(mp->mod_queue);
617 }
618 
619 void
620 fmd_hdl_unregister(fmd_hdl_t *hdl)
621 {
622 	fmd_module_t *mp = fmd_api_module_lock(hdl);
623 	fmd_module_unregister(mp);
624 	fmd_module_unlock(mp);
625 }
626 
627 void
628 fmd_hdl_subscribe(fmd_hdl_t *hdl, const char *class)
629 {
630 	fmd_module_t *mp = fmd_api_module_lock(hdl);
631 
632 	if (fmd_conf_setprop(mp->mod_conf,
633 	    FMD_PROP_SUBSCRIPTIONS, class) == 0) {
634 		fmd_dispq_insert(fmd.d_disp, mp->mod_queue, class);
635 		fmd_xprt_subscribe_all(class);
636 	}
637 
638 	fmd_module_unlock(mp);
639 }
640 
641 
642 void
643 fmd_hdl_unsubscribe(fmd_hdl_t *hdl, const char *class)
644 {
645 	fmd_module_t *mp = fmd_api_module_lock(hdl);
646 
647 	if (fmd_conf_delprop(mp->mod_conf,
648 	    FMD_PROP_SUBSCRIPTIONS, class) == 0) {
649 		fmd_xprt_unsubscribe_all(class);
650 		fmd_dispq_delete(fmd.d_disp, mp->mod_queue, class);
651 	}
652 
653 	fmd_module_unlock(mp);
654 	fmd_eventq_cancel(mp->mod_queue, FMD_EVT_PROTOCOL, (void *)class);
655 }
656 
657 void
658 fmd_hdl_setspecific(fmd_hdl_t *hdl, void *spec)
659 {
660 	fmd_module_t *mp = fmd_api_module_lock(hdl);
661 
662 	mp->mod_spec = spec;
663 	fmd_module_unlock(mp);
664 }
665 
666 void *
667 fmd_hdl_getspecific(fmd_hdl_t *hdl)
668 {
669 	fmd_module_t *mp = fmd_api_module_lock(hdl);
670 	void *spec = mp->mod_spec;
671 
672 	fmd_module_unlock(mp);
673 	return (spec);
674 }
675 
676 void
677 fmd_hdl_opendict(fmd_hdl_t *hdl, const char *dict)
678 {
679 	fmd_module_t *mp = fmd_api_module_lock(hdl);
680 	const fmd_conf_path_t *pap;
681 	int i;
682 
683 	/*
684 	 * Update the dictionary property in order to preserve the list of
685 	 * pathnames and expand any % tokens in the path.  Then retrieve the
686 	 * new dictionary names from cpa_argv[] and open them one at a time.
687 	 */
688 	(void) fmd_conf_setprop(mp->mod_conf, FMD_PROP_DICTIONARIES, dict);
689 	(void) fmd_conf_getprop(mp->mod_conf, FMD_PROP_DICTIONARIES, &pap);
690 
691 	ASSERT(pap->cpa_argc > mp->mod_dictc);
692 
693 	for (i = mp->mod_dictc; i < pap->cpa_argc; i++) {
694 		if (fmd_module_dc_opendict(mp, pap->cpa_argv[i]) != 0) {
695 			fmd_api_error(mp, EFMD_MOD_DICT,
696 			    "failed to open dictionary %s for module %s",
697 			    pap->cpa_argv[i], mp->mod_name);
698 		}
699 	}
700 
701 	fmd_module_unlock(mp);
702 }
703 
704 topo_hdl_t *
705 fmd_hdl_topo_hold(fmd_hdl_t *hdl, int v)
706 {
707 	fmd_module_t *mp = fmd_api_module_lock(hdl);
708 	topo_hdl_t *thp;
709 
710 	if (v != TOPO_VERSION) {
711 		fmd_api_error(mp, EFMD_MOD_TOPO, "libtopo version mismatch: "
712 		    "fmd version %d != client version %d\n", TOPO_VERSION, v);
713 	}
714 
715 	thp = fmd_module_topo_hold(mp);
716 	ASSERT(thp != NULL);
717 
718 	fmd_module_unlock(mp);
719 	return (thp);
720 }
721 
722 void
723 fmd_hdl_topo_rele(fmd_hdl_t *hdl, topo_hdl_t *thp)
724 {
725 	fmd_module_t *mp = fmd_api_module_lock(hdl);
726 
727 	if (fmd_module_topo_rele(mp, thp) != 0)
728 		fmd_api_error(mp, EFMD_MOD_TOPO, "failed to release invalid "
729 		    "topo handle: %p\n", (void *)thp);
730 
731 	fmd_module_unlock(mp);
732 }
733 
734 static void *
735 fmd_hdl_alloc_locked(fmd_module_t *mp, size_t size, int flags)
736 {
737 	void *data;
738 
739 	if (mp->mod_stats->ms_memlimit.fmds_value.ui64 -
740 	    mp->mod_stats->ms_memtotal.fmds_value.ui64 < size) {
741 		fmd_api_error(mp, EFMD_HDL_NOMEM, "%s's allocation of %lu "
742 		    "bytes exceeds module memory limit (%llu)\n",
743 		    mp->mod_name, (ulong_t)size, (u_longlong_t)
744 		    mp->mod_stats->ms_memtotal.fmds_value.ui64);
745 	}
746 
747 	if ((data = fmd_alloc(size, flags)) != NULL)
748 		mp->mod_stats->ms_memtotal.fmds_value.ui64 += size;
749 
750 	return (data);
751 }
752 
753 void *
754 fmd_hdl_alloc(fmd_hdl_t *hdl, size_t size, int flags)
755 {
756 	fmd_module_t *mp = fmd_api_module_lock(hdl);
757 	void *data;
758 
759 	data = fmd_hdl_alloc_locked(mp, size, flags);
760 
761 	fmd_module_unlock(mp);
762 	return (data);
763 }
764 
765 void *
766 fmd_hdl_zalloc(fmd_hdl_t *hdl, size_t size, int flags)
767 {
768 	void *data = fmd_hdl_alloc(hdl, size, flags);
769 
770 	if (data != NULL)
771 		bzero(data, size);
772 
773 	return (data);
774 }
775 
776 static void
777 fmd_hdl_free_locked(fmd_module_t *mp, void *data, size_t size)
778 {
779 	fmd_free(data, size);
780 	mp->mod_stats->ms_memtotal.fmds_value.ui64 -= size;
781 }
782 
783 void
784 fmd_hdl_free(fmd_hdl_t *hdl, void *data, size_t size)
785 {
786 	fmd_module_t *mp = fmd_api_module_lock(hdl);
787 
788 	fmd_hdl_free_locked(mp, data, size);
789 
790 	fmd_module_unlock(mp);
791 }
792 
793 char *
794 fmd_hdl_strdup(fmd_hdl_t *hdl, const char *s, int flags)
795 {
796 	char *p;
797 
798 	if (s != NULL)
799 		p = fmd_hdl_alloc(hdl, strlen(s) + 1, flags);
800 	else
801 		p = NULL;
802 
803 	if (p != NULL)
804 		(void) strcpy(p, s);
805 
806 	return (p);
807 }
808 
809 void
810 fmd_hdl_strfree(fmd_hdl_t *hdl, char *s)
811 {
812 	if (s != NULL)
813 		fmd_hdl_free(hdl, s, strlen(s) + 1);
814 }
815 
816 void
817 fmd_hdl_vabort(fmd_hdl_t *hdl, const char *format, va_list ap)
818 {
819 	fmd_api_verror(fmd_api_module_lock(hdl), EFMD_HDL_ABORT, format, ap);
820 }
821 
822 /*PRINTFLIKE2*/
823 void
824 fmd_hdl_abort(fmd_hdl_t *hdl, const char *format, ...)
825 {
826 	fmd_module_t *mp = fmd_api_module_lock(hdl);
827 	va_list ap;
828 
829 	va_start(ap, format);
830 	fmd_api_verror(mp, EFMD_HDL_ABORT, format, ap);
831 	va_end(ap);
832 }
833 
834 void
835 fmd_hdl_verror(fmd_hdl_t *hdl, const char *format, va_list ap)
836 {
837 	fmd_module_t *mp = fmd_api_module_lock(hdl);
838 	fmd_api_vxerror(mp, errno, format, ap);
839 	fmd_module_unlock(mp);
840 }
841 
842 /*PRINTFLIKE2*/
843 void
844 fmd_hdl_error(fmd_hdl_t *hdl, const char *format, ...)
845 {
846 	va_list ap;
847 
848 	va_start(ap, format);
849 	fmd_hdl_verror(hdl, format, ap);
850 	va_end(ap);
851 }
852 
853 void
854 fmd_hdl_vdebug(fmd_hdl_t *hdl, const char *format, va_list ap)
855 {
856 	fmd_module_t *mp = fmd_api_module_lock(hdl);
857 
858 	char *msg;
859 	size_t len;
860 	char c;
861 
862 	if (!(fmd.d_hdl_debug)) {
863 		mp->mod_stats->ms_debugdrop.fmds_value.ui64++;
864 		fmd_module_unlock(mp);
865 		return;
866 	}
867 
868 	len = vsnprintf(&c, 1, format, ap);
869 
870 	if ((msg = fmd_alloc(len + 2, FMD_NOSLEEP)) == NULL) {
871 		mp->mod_stats->ms_debugdrop.fmds_value.ui64++;
872 		fmd_module_unlock(mp);
873 		return;
874 	}
875 
876 	(void) vsnprintf(msg, len + 1, format, ap);
877 
878 	if (msg[len - 1] != '\n')
879 		(void) strcpy(&msg[len], "\n");
880 
881 	if (fmd.d_hdl_dbout & FMD_DBOUT_STDERR) {
882 		(void) pthread_mutex_lock(&fmd.d_err_lock);
883 		(void) fprintf(stderr, "%s DEBUG: %s: %s",
884 		    fmd.d_pname, mp->mod_name, msg);
885 		(void) pthread_mutex_unlock(&fmd.d_err_lock);
886 	}
887 
888 	if (fmd.d_hdl_dbout & FMD_DBOUT_SYSLOG) {
889 		syslog(LOG_DEBUG | LOG_DAEMON, "%s DEBUG: %s: %s",
890 		    fmd.d_pname, mp->mod_name, msg);
891 	}
892 
893 	fmd_free(msg, len + 2);
894 	fmd_module_unlock(mp);
895 }
896 
897 /*PRINTFLIKE2*/
898 void
899 fmd_hdl_debug(fmd_hdl_t *hdl, const char *format, ...)
900 {
901 	va_list ap;
902 
903 	va_start(ap, format);
904 	fmd_hdl_vdebug(hdl, format, ap);
905 	va_end(ap);
906 }
907 
908 int32_t
909 fmd_prop_get_int32(fmd_hdl_t *hdl, const char *name)
910 {
911 	fmd_module_t *mp = fmd_api_module_lock(hdl);
912 	const fmd_conf_ops_t *ops = fmd_conf_gettype(mp->mod_conf, name);
913 	int32_t value = 0;
914 
915 	if (ops == &fmd_conf_bool || ops == &fmd_conf_int32 ||
916 	    ops == &fmd_conf_uint32)
917 		(void) fmd_conf_getprop(mp->mod_conf, name, &value);
918 	else if (ops != NULL) {
919 		fmd_api_error(mp, EFMD_PROP_TYPE,
920 		    "property %s is not of int32 type\n", name);
921 	} else {
922 		fmd_api_error(mp, EFMD_PROP_DEFN,
923 		    "property %s is not defined\n", name);
924 	}
925 
926 	fmd_module_unlock(mp);
927 	return (value);
928 }
929 
930 int64_t
931 fmd_prop_get_int64(fmd_hdl_t *hdl, const char *name)
932 {
933 	fmd_module_t *mp = fmd_api_module_lock(hdl);
934 	const fmd_conf_ops_t *ops = fmd_conf_gettype(mp->mod_conf, name);
935 	int64_t value = 0;
936 
937 	if (ops == &fmd_conf_int64 || ops == &fmd_conf_uint64 ||
938 	    ops == &fmd_conf_time || ops == &fmd_conf_size)
939 		(void) fmd_conf_getprop(mp->mod_conf, name, &value);
940 	else if (ops != NULL) {
941 		fmd_api_error(mp, EFMD_PROP_TYPE,
942 		    "property %s is not of int64 type\n", name);
943 	} else {
944 		fmd_api_error(mp, EFMD_PROP_DEFN,
945 		    "property %s is not defined\n", name);
946 	}
947 
948 	fmd_module_unlock(mp);
949 	return (value);
950 }
951 
952 char *
953 fmd_prop_get_string(fmd_hdl_t *hdl, const char *name)
954 {
955 	fmd_module_t *mp = fmd_api_module_lock(hdl);
956 	const fmd_conf_ops_t *ops = fmd_conf_gettype(mp->mod_conf, name);
957 	char *value = NULL;
958 	const char *s;
959 
960 	if (ops == &fmd_conf_string) {
961 		(void) fmd_conf_getprop(mp->mod_conf, name, &s);
962 		value = fmd_strdup(s, FMD_SLEEP);
963 	} else if (ops != NULL) {
964 		fmd_api_error(mp, EFMD_PROP_TYPE,
965 		    "property %s is not of string type\n", name);
966 	} else {
967 		fmd_api_error(mp, EFMD_PROP_DEFN,
968 		    "property %s is not defined\n", name);
969 	}
970 
971 	fmd_module_unlock(mp);
972 	return (value);
973 }
974 
975 void
976 fmd_prop_free_string(fmd_hdl_t *hdl, char *s)
977 {
978 	fmd_module_t *mp = fmd_api_module_lock(hdl);
979 	fmd_strfree(s);
980 	fmd_module_unlock(mp);
981 }
982 
983 fmd_stat_t *
984 fmd_stat_create(fmd_hdl_t *hdl, uint_t flags, uint_t argc, fmd_stat_t *argv)
985 {
986 	fmd_module_t *mp = fmd_api_module_lock(hdl);
987 	fmd_stat_t *ep, *sp;
988 
989 	if (flags & ~FMD_STAT_ALLOC) {
990 		fmd_api_error(mp, EFMD_STAT_FLAGS,
991 		    "invalid flags 0x%x passed to fmd_stat_create\n", flags);
992 	}
993 
994 	if ((sp = fmd_ustat_insert(mp->mod_ustat,
995 	    flags | FMD_USTAT_VALIDATE, argc, argv, &ep)) == NULL) {
996 		fmd_api_error(mp, errno,
997 		    "failed to publish stat '%s'", ep->fmds_name);
998 	}
999 
1000 	fmd_module_unlock(mp);
1001 	return (sp);
1002 }
1003 
1004 void
1005 fmd_stat_destroy(fmd_hdl_t *hdl, uint_t argc, fmd_stat_t *argv)
1006 {
1007 	fmd_module_t *mp = fmd_api_module_lock(hdl);
1008 	fmd_ustat_delete(mp->mod_ustat, argc, argv);
1009 	fmd_module_unlock(mp);
1010 }
1011 
1012 void
1013 fmd_stat_setstr(fmd_hdl_t *hdl, fmd_stat_t *sp, const char *s)
1014 {
1015 	char *str = fmd_strdup(s, FMD_SLEEP);
1016 	fmd_module_t *mp = fmd_api_module_lock(hdl);
1017 
1018 	if (sp->fmds_type != FMD_TYPE_STRING) {
1019 		fmd_strfree(str);
1020 		fmd_api_error(mp, EFMD_STAT_TYPE,
1021 		    "stat '%s' is not a string\n", sp->fmds_name);
1022 	}
1023 
1024 	fmd_strfree(sp->fmds_value.str);
1025 	sp->fmds_value.str = str;
1026 
1027 	fmd_module_unlock(mp);
1028 }
1029 
1030 fmd_case_t *
1031 fmd_case_open(fmd_hdl_t *hdl, void *data)
1032 {
1033 	fmd_module_t *mp = fmd_api_module_lock(hdl);
1034 	fmd_case_t *cp = fmd_case_create(mp, data);
1035 	fmd_module_unlock(mp);
1036 	return (cp);
1037 }
1038 
1039 void
1040 fmd_case_reset(fmd_hdl_t *hdl, fmd_case_t *cp)
1041 {
1042 	fmd_module_t *mp = fmd_api_module_lock(hdl);
1043 	fmd_case_impl_t *cip = fmd_api_case_impl(mp, cp);
1044 
1045 	if (cip->ci_state >= FMD_CASE_SOLVED) {
1046 		fmd_api_error(mp, EFMD_CASE_STATE, "cannot solve %s: "
1047 		    "case is already solved or closed\n", cip->ci_uuid);
1048 	}
1049 
1050 	fmd_case_reset_suspects(cp);
1051 	fmd_module_unlock(mp);
1052 }
1053 
1054 void
1055 fmd_case_solve(fmd_hdl_t *hdl, fmd_case_t *cp)
1056 {
1057 	fmd_module_t *mp = fmd_api_module_lock(hdl);
1058 	fmd_case_impl_t *cip = fmd_api_case_impl(mp, cp);
1059 
1060 	if (cip->ci_state >= FMD_CASE_SOLVED) {
1061 		fmd_api_error(mp, EFMD_CASE_STATE, "cannot solve %s: "
1062 		    "case is already solved or closed\n", cip->ci_uuid);
1063 	}
1064 
1065 	fmd_case_transition(cp, FMD_CASE_SOLVED, FMD_CF_SOLVED);
1066 	fmd_module_unlock(mp);
1067 }
1068 
1069 void
1070 fmd_case_close(fmd_hdl_t *hdl, fmd_case_t *cp)
1071 {
1072 	fmd_module_t *mp = fmd_api_module_lock(hdl);
1073 
1074 	(void) fmd_api_case_impl(mp, cp); /* validate 'cp' */
1075 	fmd_case_transition(cp, FMD_CASE_CLOSE_WAIT, FMD_CF_ISOLATED);
1076 
1077 	fmd_module_unlock(mp);
1078 }
1079 
1080 const char *
1081 fmd_case_uuid(fmd_hdl_t *hdl, fmd_case_t *cp)
1082 {
1083 	fmd_module_t *mp = fmd_api_module_lock(hdl);
1084 	fmd_case_impl_t *cip = fmd_api_case_impl(mp, cp);
1085 	const char *uuid = cip->ci_uuid;
1086 
1087 	fmd_module_unlock(mp);
1088 	return (uuid);
1089 }
1090 
1091 fmd_case_t *
1092 fmd_case_uulookup(fmd_hdl_t *hdl, const char *uuid)
1093 {
1094 	fmd_module_t *cmp, *mp = fmd_api_module_lock(hdl);
1095 	fmd_case_t *cp = fmd_case_hash_lookup(fmd.d_cases, uuid);
1096 
1097 	if (cp != NULL) {
1098 		cmp = ((fmd_case_impl_t *)cp)->ci_mod;
1099 		fmd_case_rele(cp);
1100 	} else
1101 		cmp = NULL;
1102 
1103 	fmd_module_unlock(mp);
1104 	return (cmp == mp ? cp : NULL);
1105 }
1106 
1107 void
1108 fmd_case_uuclose(fmd_hdl_t *hdl, const char *uuid)
1109 {
1110 	fmd_module_t *mp = fmd_api_module_lock(hdl);
1111 	fmd_case_t *cp = fmd_case_hash_lookup(fmd.d_cases, uuid);
1112 
1113 	if (cp != NULL) {
1114 		fmd_case_transition(cp, FMD_CASE_CLOSE_WAIT, FMD_CF_ISOLATED);
1115 		fmd_case_rele(cp);
1116 	}
1117 
1118 	fmd_module_unlock(mp);
1119 }
1120 
1121 int
1122 fmd_case_uuclosed(fmd_hdl_t *hdl, const char *uuid)
1123 {
1124 	fmd_module_t *mp = fmd_api_module_lock(hdl);
1125 	fmd_case_t *cp = fmd_case_hash_lookup(fmd.d_cases, uuid);
1126 	fmd_case_impl_t *cip = (fmd_case_impl_t *)cp;
1127 	int rv = FMD_B_TRUE;
1128 
1129 	if (cip != NULL) {
1130 		rv = cip->ci_state >= FMD_CASE_CLOSE_WAIT;
1131 		fmd_case_rele(cp);
1132 	}
1133 
1134 	fmd_module_unlock(mp);
1135 	return (rv);
1136 }
1137 
1138 void
1139 fmd_case_uuresolved(fmd_hdl_t *hdl, const char *uuid)
1140 {
1141 	fmd_module_t *mp = fmd_api_module_lock(hdl);
1142 	fmd_case_t *cp = fmd_case_hash_lookup(fmd.d_cases, uuid);
1143 
1144 	if (cp != NULL) {
1145 		fmd_case_impl_t *cip = (fmd_case_impl_t *)cp;
1146 		/*
1147 		 * For a proxy, we notify the diagnosing side, and then
1148 		 * wait for it to send us back a list.resolved.
1149 		 */
1150 		if (cip->ci_xprt != NULL)
1151 			fmd_xprt_uuresolved(cip->ci_xprt, cip->ci_uuid);
1152 		else
1153 			fmd_case_transition(cp, FMD_CASE_RESOLVED, 0);
1154 		fmd_case_rele(cp);
1155 	}
1156 
1157 	fmd_module_unlock(mp);
1158 }
1159 
1160 static int
1161 fmd_case_instate(fmd_hdl_t *hdl, fmd_case_t *cp, uint_t state)
1162 {
1163 	fmd_module_t *mp = fmd_api_module_lock(hdl);
1164 	fmd_case_impl_t *cip = fmd_api_case_impl(mp, cp);
1165 	int rv = cip->ci_state >= state;
1166 
1167 	fmd_module_unlock(mp);
1168 	return (rv);
1169 }
1170 
1171 int
1172 fmd_case_solved(fmd_hdl_t *hdl, fmd_case_t *cp)
1173 {
1174 	return (fmd_case_instate(hdl, cp, FMD_CASE_SOLVED));
1175 }
1176 
1177 int
1178 fmd_case_closed(fmd_hdl_t *hdl, fmd_case_t *cp)
1179 {
1180 	return (fmd_case_instate(hdl, cp, FMD_CASE_CLOSE_WAIT));
1181 }
1182 
1183 void
1184 fmd_case_add_ereport(fmd_hdl_t *hdl, fmd_case_t *cp, fmd_event_t *ep)
1185 {
1186 	fmd_module_t *mp = fmd_api_module_lock(hdl);
1187 
1188 	(void) fmd_api_case_impl(mp, cp); /* validate 'cp' */
1189 
1190 	if (fmd_case_insert_event(cp, ep))
1191 		mp->mod_stats->ms_accepted.fmds_value.ui64++;
1192 
1193 	fmd_module_unlock(mp);
1194 }
1195 
1196 void
1197 fmd_case_add_serd(fmd_hdl_t *hdl, fmd_case_t *cp, const char *name)
1198 {
1199 	fmd_module_t *mp = fmd_api_module_lock(hdl);
1200 	fmd_serd_elem_t *sep;
1201 	fmd_serd_eng_t *sgp;
1202 
1203 	if ((sgp = fmd_serd_eng_lookup(&mp->mod_serds, name)) == NULL) {
1204 		fmd_api_error(mp, EFMD_SERD_NAME,
1205 		    "failed to add events from serd engine '%s'", name);
1206 	}
1207 
1208 	(void) fmd_api_case_impl(mp, cp); /* validate 'cp' */
1209 
1210 	for (sep = fmd_list_next(&sgp->sg_list);
1211 	    sep != NULL; sep = fmd_list_next(sep)) {
1212 		if (fmd_case_insert_event(cp, sep->se_event))
1213 			mp->mod_stats->ms_accepted.fmds_value.ui64++;
1214 	}
1215 
1216 	fmd_module_unlock(mp);
1217 }
1218 
1219 void
1220 fmd_case_add_suspect(fmd_hdl_t *hdl, fmd_case_t *cp, nvlist_t *nvl)
1221 {
1222 	fmd_module_t *mp = fmd_api_module_lock(hdl);
1223 	fmd_case_impl_t *cip = fmd_api_case_impl(mp, cp);
1224 	char *class;
1225 	topo_hdl_t *thp;
1226 	int err;
1227 	nvlist_t *rsrc = NULL, *asru_prop = NULL, *asru = NULL, *fru = NULL;
1228 	char *loc = NULL, *serial = NULL;
1229 
1230 	if (cip->ci_state >= FMD_CASE_SOLVED) {
1231 		fmd_api_error(mp, EFMD_CASE_STATE, "cannot add suspect to "
1232 		    "%s: case is already solved or closed\n", cip->ci_uuid);
1233 	}
1234 
1235 	if (nvlist_lookup_string(nvl, FM_CLASS, &class) != 0 ||
1236 	    class == NULL || *class == '\0') {
1237 		fmd_api_error(mp, EFMD_CASE_EVENT, "cannot add suspect to "
1238 		    "%s: suspect event is missing a class\n", cip->ci_uuid);
1239 	}
1240 
1241 	thp = fmd_module_topo_hold(mp);
1242 	(void) nvlist_lookup_nvlist(nvl, FM_FAULT_RESOURCE, &rsrc);
1243 	(void) nvlist_lookup_nvlist(nvl, FM_FAULT_ASRU, &asru);
1244 	(void) nvlist_lookup_nvlist(nvl, FM_FAULT_FRU, &fru);
1245 	if (rsrc != NULL) {
1246 		if (strncmp(class, "defect", 6) == 0) {
1247 			if (asru == NULL && topo_fmri_getprop(thp, rsrc,
1248 			    TOPO_PGROUP_IO, TOPO_IO_MODULE, rsrc,
1249 			    &asru_prop, &err) == 0 &&
1250 			    nvlist_lookup_nvlist(asru_prop, TOPO_PROP_VAL_VAL,
1251 			    &asru) == 0) {
1252 				(void) nvlist_add_nvlist(nvl, FM_FAULT_ASRU,
1253 				    asru);
1254 				nvlist_free(asru);
1255 				(void) nvlist_lookup_nvlist(nvl, FM_FAULT_ASRU,
1256 				    &asru);
1257 			}
1258 		} else {
1259 			if (topo_fmri_asru(thp, rsrc, &asru, &err) == 0) {
1260 				(void) nvlist_remove(nvl, FM_FAULT_ASRU,
1261 				    DATA_TYPE_NVLIST);
1262 				(void) nvlist_add_nvlist(nvl, FM_FAULT_ASRU,
1263 				    asru);
1264 				nvlist_free(asru);
1265 				(void) nvlist_lookup_nvlist(nvl, FM_FAULT_ASRU,
1266 				    &asru);
1267 			}
1268 			if (topo_fmri_fru(thp, rsrc, &fru, &err) == 0) {
1269 				(void) nvlist_remove(nvl, FM_FAULT_FRU,
1270 				    DATA_TYPE_NVLIST);
1271 				(void) nvlist_add_nvlist(nvl, FM_FAULT_FRU,
1272 				    fru);
1273 				nvlist_free(fru);
1274 				(void) nvlist_lookup_nvlist(nvl, FM_FAULT_FRU,
1275 				    &fru);
1276 			}
1277 		}
1278 	}
1279 
1280 	/*
1281 	 * Try to find the location label for this resource
1282 	 */
1283 	if (fru != NULL)
1284 		(void) topo_fmri_label(thp, fru, &loc, &err);
1285 	else if (rsrc != NULL)
1286 		(void) topo_fmri_label(thp, rsrc, &loc, &err);
1287 	if (strncmp(class, "defect", 6) != 0 && loc != NULL) {
1288 		(void) nvlist_remove(nvl, FM_FAULT_LOCATION, DATA_TYPE_STRING);
1289 		(void) nvlist_add_string(nvl, FM_FAULT_LOCATION, loc);
1290 		topo_hdl_strfree(thp, loc);
1291 	}
1292 
1293 	/*
1294 	 * In some cases, serial information for the resource will not be
1295 	 * available at enumeration but may instead be available by invoking
1296 	 * a dynamic property method on the FRU.  In order to ensure the serial
1297 	 * number is persisted properly in the ASRU cache, we'll fetch the
1298 	 * property, if it exists, and add it to the resource and fru fmris.
1299 	 * If the DE has not listed a fru in the suspect, see if we can
1300 	 * retrieve the serial from the resource instead.
1301 	 */
1302 	if (fru != NULL) {
1303 		(void) topo_fmri_serial(thp, fru, &serial, &err);
1304 		if (serial != NULL) {
1305 			if (rsrc != NULL)
1306 				(void) nvlist_add_string(rsrc, "serial",
1307 				    serial);
1308 			(void) nvlist_add_string(fru, "serial", serial);
1309 			topo_hdl_strfree(thp, serial);
1310 		}
1311 	} else if (rsrc != NULL) {
1312 		(void) topo_fmri_serial(thp, rsrc, &serial, &err);
1313 		if (serial != NULL) {
1314 			(void) nvlist_add_string(rsrc, "serial", serial);
1315 			topo_hdl_strfree(thp, serial);
1316 		}
1317 	}
1318 
1319 	err = fmd_module_topo_rele(mp, thp);
1320 	ASSERT(err == 0);
1321 
1322 	fmd_case_insert_suspect(cp, nvl);
1323 	fmd_module_unlock(mp);
1324 }
1325 
1326 void
1327 fmd_case_setspecific(fmd_hdl_t *hdl, fmd_case_t *cp, void *data)
1328 {
1329 	fmd_module_t *mp = fmd_api_module_lock(hdl);
1330 	fmd_case_impl_t *cip = fmd_api_case_impl(mp, cp);
1331 
1332 	(void) pthread_mutex_lock(&cip->ci_lock);
1333 	cip->ci_data = data;
1334 	(void) pthread_mutex_unlock(&cip->ci_lock);
1335 
1336 	fmd_module_unlock(mp);
1337 }
1338 
1339 void *
1340 fmd_case_getspecific(fmd_hdl_t *hdl, fmd_case_t *cp)
1341 {
1342 	fmd_module_t *mp = fmd_api_module_lock(hdl);
1343 	fmd_case_impl_t *cip = fmd_api_case_impl(mp, cp);
1344 	void *data;
1345 
1346 	(void) pthread_mutex_lock(&cip->ci_lock);
1347 	data = cip->ci_data;
1348 	(void) pthread_mutex_unlock(&cip->ci_lock);
1349 
1350 	fmd_module_unlock(mp);
1351 	return (data);
1352 }
1353 
1354 void
1355 fmd_case_setprincipal(fmd_hdl_t *hdl, fmd_case_t *cp, fmd_event_t *ep)
1356 {
1357 	fmd_module_t *mp = fmd_api_module_lock(hdl);
1358 
1359 	(void) fmd_api_case_impl(mp, cp); /* validate 'cp' */
1360 
1361 	if (fmd_case_insert_principal(cp, ep))
1362 		mp->mod_stats->ms_accepted.fmds_value.ui64++;
1363 
1364 	fmd_module_unlock(mp);
1365 }
1366 
1367 fmd_event_t *
1368 fmd_case_getprincipal(fmd_hdl_t *hdl, fmd_case_t *cp)
1369 {
1370 	fmd_module_t *mp = fmd_api_module_lock(hdl);
1371 	fmd_case_impl_t *cip = fmd_api_case_impl(mp, cp);
1372 	fmd_event_t *ep;
1373 
1374 	(void) pthread_mutex_lock(&cip->ci_lock);
1375 	ep = cip->ci_principal;
1376 	(void) pthread_mutex_unlock(&cip->ci_lock);
1377 
1378 	fmd_module_unlock(mp);
1379 	return (ep);
1380 }
1381 
1382 fmd_case_t *
1383 fmd_case_next(fmd_hdl_t *hdl, fmd_case_t *cp)
1384 {
1385 	fmd_module_t *mp = fmd_api_module_lock(hdl);
1386 
1387 	if (cp != NULL)
1388 		cp = fmd_list_next(fmd_api_case_impl(mp, cp));
1389 	else
1390 		cp = fmd_list_next(&mp->mod_cases);
1391 
1392 	fmd_module_unlock(mp);
1393 	return (cp);
1394 }
1395 
1396 fmd_case_t *
1397 fmd_case_prev(fmd_hdl_t *hdl, fmd_case_t *cp)
1398 {
1399 	fmd_module_t *mp = fmd_api_module_lock(hdl);
1400 
1401 	if (cp != NULL)
1402 		cp = fmd_list_prev(fmd_api_case_impl(mp, cp));
1403 	else
1404 		cp = fmd_list_prev(&mp->mod_cases);
1405 
1406 	fmd_module_unlock(mp);
1407 	return (cp);
1408 }
1409 
1410 /*
1411  * Utility function for fmd_buf_* routines.  If a case is specified, use the
1412  * case's ci_bufs hash; otherwise use the module's global mod_bufs hash.
1413  */
1414 static fmd_buf_hash_t *
1415 fmd_buf_gethash(fmd_module_t *mp, fmd_case_t *cp)
1416 {
1417 	return (cp ? &fmd_api_case_impl(mp, cp)->ci_bufs : &mp->mod_bufs);
1418 }
1419 
1420 void
1421 fmd_buf_create(fmd_hdl_t *hdl, fmd_case_t *cp, const char *name, size_t size)
1422 {
1423 	fmd_module_t *mp = fmd_api_module_lock(hdl);
1424 	fmd_buf_hash_t *bhp = fmd_buf_gethash(mp, cp);
1425 	fmd_buf_t *bp = fmd_buf_lookup(bhp, name);
1426 
1427 	if (bp == NULL) {
1428 		if (fmd_strbadid(name, FMD_B_TRUE) != NULL || size == 0) {
1429 			fmd_api_error(mp, EFMD_BUF_INVAL, "cannot create '%s' "
1430 			    "(size %lu): %s\n", name, (ulong_t)size,
1431 			    fmd_strerror(EFMD_BUF_INVAL));
1432 		}
1433 
1434 		if (mp->mod_stats->ms_buflimit.fmds_value.ui64 -
1435 		    mp->mod_stats->ms_buftotal.fmds_value.ui64 < size) {
1436 			fmd_api_error(mp, EFMD_BUF_LIMIT, "cannot create '%s': "
1437 			    "buf limit exceeded (%llu)\n", name, (u_longlong_t)
1438 			    mp->mod_stats->ms_buflimit.fmds_value.ui64);
1439 		}
1440 
1441 		mp->mod_stats->ms_buftotal.fmds_value.ui64 += size;
1442 		bp = fmd_buf_insert(bhp, name, size);
1443 
1444 	} else {
1445 		fmd_api_error(mp, EFMD_BUF_EXISTS,
1446 		    "cannot create '%s': buffer already exists\n", name);
1447 	}
1448 
1449 	if (cp != NULL)
1450 		fmd_case_setdirty(cp);
1451 	else
1452 		fmd_module_setdirty(mp);
1453 
1454 	fmd_module_unlock(mp);
1455 }
1456 
1457 void
1458 fmd_buf_destroy(fmd_hdl_t *hdl, fmd_case_t *cp, const char *name)
1459 {
1460 	fmd_module_t *mp = fmd_api_module_lock(hdl);
1461 	fmd_buf_hash_t *bhp = fmd_buf_gethash(mp, cp);
1462 	fmd_buf_t *bp = fmd_buf_lookup(bhp, name);
1463 
1464 	if (bp != NULL) {
1465 		mp->mod_stats->ms_buftotal.fmds_value.ui64 -= bp->buf_size;
1466 		fmd_buf_delete(bhp, name);
1467 
1468 		if (cp != NULL)
1469 			fmd_case_setdirty(cp);
1470 		else
1471 			fmd_module_setdirty(mp);
1472 	}
1473 
1474 	fmd_module_unlock(mp);
1475 }
1476 
1477 void
1478 fmd_buf_read(fmd_hdl_t *hdl, fmd_case_t *cp,
1479     const char *name, void *buf, size_t size)
1480 {
1481 	fmd_module_t *mp = fmd_api_module_lock(hdl);
1482 	fmd_buf_t *bp = fmd_buf_lookup(fmd_buf_gethash(mp, cp), name);
1483 
1484 	if (bp == NULL) {
1485 		fmd_api_error(mp, EFMD_BUF_NOENT, "no buf named '%s' is "
1486 		    "associated with %s\n", name, cp ? "case" : "module");
1487 	}
1488 
1489 	bcopy(bp->buf_data, buf, MIN(bp->buf_size, size));
1490 	if (size > bp->buf_size)
1491 		bzero((char *)buf + bp->buf_size, size - bp->buf_size);
1492 
1493 	fmd_module_unlock(mp);
1494 }
1495 
1496 void
1497 fmd_buf_write(fmd_hdl_t *hdl, fmd_case_t *cp,
1498     const char *name, const void *buf, size_t size)
1499 {
1500 	fmd_module_t *mp = fmd_api_module_lock(hdl);
1501 	fmd_buf_hash_t *bhp = fmd_buf_gethash(mp, cp);
1502 	fmd_buf_t *bp = fmd_buf_lookup(bhp, name);
1503 
1504 	if (bp == NULL) {
1505 		if (fmd_strbadid(name, FMD_B_TRUE) != NULL || size == 0) {
1506 			fmd_api_error(mp, EFMD_BUF_INVAL, "cannot write '%s' "
1507 			    "(size %lu): %s\n", name, (ulong_t)size,
1508 			    fmd_strerror(EFMD_BUF_INVAL));
1509 		}
1510 
1511 		if (mp->mod_stats->ms_buflimit.fmds_value.ui64 -
1512 		    mp->mod_stats->ms_buftotal.fmds_value.ui64 < size) {
1513 			fmd_api_error(mp, EFMD_BUF_LIMIT, "cannot write '%s': "
1514 			    "buf limit exceeded (%llu)\n", name, (u_longlong_t)
1515 			    mp->mod_stats->ms_buflimit.fmds_value.ui64);
1516 		}
1517 
1518 		mp->mod_stats->ms_buftotal.fmds_value.ui64 += size;
1519 		bp = fmd_buf_insert(bhp, name, size);
1520 
1521 	} else if (size > bp->buf_size) {
1522 		fmd_api_error(mp, EFMD_BUF_OFLOW,
1523 		    "write to buf '%s' overflows buf size (%lu > %lu)\n",
1524 		    name, (ulong_t)size, (ulong_t)bp->buf_size);
1525 	}
1526 
1527 	bcopy(buf, bp->buf_data, MIN(bp->buf_size, size));
1528 	bp->buf_flags |= FMD_BUF_DIRTY;
1529 
1530 	if (cp != NULL)
1531 		fmd_case_setdirty(cp);
1532 	else
1533 		fmd_module_setdirty(mp);
1534 
1535 	fmd_module_unlock(mp);
1536 }
1537 
1538 size_t
1539 fmd_buf_size(fmd_hdl_t *hdl, fmd_case_t *cp, const char *name)
1540 {
1541 	fmd_module_t *mp = fmd_api_module_lock(hdl);
1542 	fmd_buf_hash_t *bhp = fmd_buf_gethash(mp, cp);
1543 
1544 	fmd_buf_t *bp;
1545 	size_t size;
1546 
1547 	if ((bp = fmd_buf_lookup(bhp, name)) != NULL)
1548 		size = bp->buf_size;
1549 	else
1550 		size = 0;
1551 
1552 	fmd_module_unlock(mp);
1553 	return (size);
1554 }
1555 
1556 void
1557 fmd_serd_create(fmd_hdl_t *hdl, const char *name, uint_t n, hrtime_t t)
1558 {
1559 	fmd_module_t *mp = fmd_api_module_lock(hdl);
1560 
1561 	if (fmd_serd_eng_lookup(&mp->mod_serds, name) != NULL) {
1562 		fmd_api_error(mp, EFMD_SERD_EXISTS,
1563 		    "failed to create serd engine '%s': %s\n",
1564 		    name, fmd_strerror(EFMD_SERD_EXISTS));
1565 	}
1566 
1567 	(void) fmd_serd_eng_insert(&mp->mod_serds, name, n, t);
1568 	fmd_module_setdirty(mp);
1569 	fmd_module_unlock(mp);
1570 }
1571 
1572 void
1573 fmd_serd_destroy(fmd_hdl_t *hdl, const char *name)
1574 {
1575 	fmd_module_t *mp = fmd_api_module_lock(hdl);
1576 
1577 	fmd_serd_eng_delete(&mp->mod_serds, name);
1578 	fmd_module_setdirty(mp);
1579 	fmd_module_unlock(mp);
1580 }
1581 
1582 int
1583 fmd_serd_exists(fmd_hdl_t *hdl, const char *name)
1584 {
1585 	fmd_module_t *mp = fmd_api_module_lock(hdl);
1586 	int rv = (fmd_serd_eng_lookup(&mp->mod_serds, name) != NULL);
1587 	fmd_module_unlock(mp);
1588 
1589 	return (rv);
1590 }
1591 
1592 void
1593 fmd_serd_reset(fmd_hdl_t *hdl, const char *name)
1594 {
1595 	fmd_module_t *mp = fmd_api_module_lock(hdl);
1596 	fmd_serd_eng_t *sgp;
1597 
1598 	if ((sgp = fmd_serd_eng_lookup(&mp->mod_serds, name)) == NULL) {
1599 		fmd_api_error(mp, EFMD_SERD_NAME,
1600 		    "serd engine '%s' does not exist\n", name);
1601 	}
1602 
1603 	fmd_serd_eng_reset(sgp);
1604 	fmd_module_setdirty(mp);
1605 	fmd_module_unlock(mp);
1606 }
1607 
1608 int
1609 fmd_serd_record(fmd_hdl_t *hdl, const char *name, fmd_event_t *ep)
1610 {
1611 	fmd_module_t *mp = fmd_api_module_lock(hdl);
1612 	fmd_serd_eng_t *sgp;
1613 	int err;
1614 
1615 	if ((sgp = fmd_serd_eng_lookup(&mp->mod_serds, name)) == NULL) {
1616 		fmd_api_error(mp, EFMD_SERD_NAME,
1617 		    "failed to add record to serd engine '%s'", name);
1618 	}
1619 
1620 	err = fmd_serd_eng_record(sgp, ep);
1621 
1622 	if (sgp->sg_flags & FMD_SERD_DIRTY)
1623 		fmd_module_setdirty(mp);
1624 
1625 	fmd_module_unlock(mp);
1626 	return (err);
1627 }
1628 
1629 int
1630 fmd_serd_fired(fmd_hdl_t *hdl, const char *name)
1631 {
1632 	fmd_module_t *mp = fmd_api_module_lock(hdl);
1633 	fmd_serd_eng_t *sgp;
1634 	int err;
1635 
1636 	if ((sgp = fmd_serd_eng_lookup(&mp->mod_serds, name)) == NULL) {
1637 		fmd_api_error(mp, EFMD_SERD_NAME,
1638 		    "serd engine '%s' does not exist\n", name);
1639 	}
1640 
1641 	err = fmd_serd_eng_fired(sgp);
1642 	fmd_module_unlock(mp);
1643 	return (err);
1644 }
1645 
1646 int
1647 fmd_serd_empty(fmd_hdl_t *hdl, const char *name)
1648 {
1649 	fmd_module_t *mp = fmd_api_module_lock(hdl);
1650 	fmd_serd_eng_t *sgp;
1651 	int empty;
1652 
1653 	if ((sgp = fmd_serd_eng_lookup(&mp->mod_serds, name)) == NULL) {
1654 		fmd_api_error(mp, EFMD_SERD_NAME,
1655 		    "serd engine '%s' does not exist\n", name);
1656 	}
1657 
1658 	empty = fmd_serd_eng_empty(sgp);
1659 	fmd_module_unlock(mp);
1660 	return (empty);
1661 }
1662 
1663 pthread_t
1664 fmd_thr_create(fmd_hdl_t *hdl, void (*func)(void *), void *arg)
1665 {
1666 	fmd_module_t *mp = fmd_api_module_lock(hdl);
1667 	fmd_thread_t *tp;
1668 	pthread_t tid;
1669 
1670 	if (mp->mod_stats->ms_thrtotal.fmds_value.ui32 >=
1671 	    mp->mod_stats->ms_thrlimit.fmds_value.ui32) {
1672 		fmd_api_error(mp, EFMD_THR_LIMIT, "%s request to create an "
1673 		    "auxiliary thread exceeds module thread limit (%u)\n",
1674 		    mp->mod_name, mp->mod_stats->ms_thrlimit.fmds_value.ui32);
1675 	}
1676 
1677 	if ((tp = fmd_thread_create(mp, func, arg)) == NULL) {
1678 		fmd_api_error(mp, EFMD_THR_CREATE,
1679 		    "failed to create auxiliary thread");
1680 	}
1681 
1682 	tid = tp->thr_tid;
1683 	mp->mod_stats->ms_thrtotal.fmds_value.ui32++;
1684 	(void) fmd_idspace_xalloc(mp->mod_threads, tid, tp);
1685 
1686 	fmd_module_unlock(mp);
1687 	return (tid);
1688 }
1689 
1690 void
1691 fmd_thr_destroy(fmd_hdl_t *hdl, pthread_t tid)
1692 {
1693 	fmd_module_t *mp = fmd_api_module_lock(hdl);
1694 	fmd_thread_t *tp;
1695 	int err;
1696 
1697 	if (pthread_self() == tid) {
1698 		fmd_api_error(mp, EFMD_THR_INVAL, "auxiliary thread tried to "
1699 		    "destroy itself (tid %u)\n", tid);
1700 	}
1701 
1702 	if ((tp = fmd_idspace_getspecific(mp->mod_threads, tid)) == NULL) {
1703 		fmd_api_error(mp, EFMD_THR_INVAL, "auxiliary thread tried to "
1704 		    "destroy an invalid thread (tid %u)\n", tid);
1705 	}
1706 
1707 	/*
1708 	 * Wait for the specified thread to exit and then join with it.  Since
1709 	 * the thread may need to make API calls in order to complete its work
1710 	 * we must sleep with the module lock unheld, and then reacquire it.
1711 	 */
1712 	fmd_module_unlock(mp);
1713 	err = pthread_join(tid, NULL);
1714 	mp = fmd_api_module_lock(hdl);
1715 
1716 	/*
1717 	 * Since pthread_join() was called without the module lock held, if
1718 	 * multiple callers attempted to destroy the same auxiliary thread
1719 	 * simultaneously, one will succeed and the others will get ESRCH.
1720 	 * Therefore we silently ignore ESRCH but only allow the caller who
1721 	 * succeessfully joined with the auxiliary thread to destroy it.
1722 	 */
1723 	if (err != 0 && err != ESRCH) {
1724 		fmd_api_error(mp, EFMD_THR_JOIN,
1725 		    "failed to join with auxiliary thread %u\n", tid);
1726 	}
1727 
1728 	if (err == 0) {
1729 		fmd_thread_destroy(tp, FMD_THREAD_NOJOIN);
1730 		mp->mod_stats->ms_thrtotal.fmds_value.ui32--;
1731 		(void) fmd_idspace_free(mp->mod_threads, tid);
1732 	}
1733 
1734 	fmd_module_unlock(mp);
1735 }
1736 
1737 void
1738 fmd_thr_signal(fmd_hdl_t *hdl, pthread_t tid)
1739 {
1740 	fmd_module_t *mp = fmd_api_module_lock(hdl);
1741 
1742 	if (tid != mp->mod_thread->thr_tid &&
1743 	    fmd_idspace_getspecific(mp->mod_threads, tid) == NULL) {
1744 		fmd_api_error(mp, EFMD_THR_INVAL, "tid %u is not a valid "
1745 		    "thread id for module %s\n", tid, mp->mod_name);
1746 	}
1747 
1748 	(void) pthread_kill(tid, fmd.d_thr_sig);
1749 	fmd_module_unlock(mp);
1750 }
1751 
1752 void
1753 fmd_thr_checkpoint(fmd_hdl_t *hdl)
1754 {
1755 	fmd_module_t *mp = fmd_api_module_lock(hdl);
1756 	pthread_t tid = pthread_self();
1757 
1758 	if (tid == mp->mod_thread->thr_tid ||
1759 	    fmd_idspace_getspecific(mp->mod_threads, tid) == NULL) {
1760 		fmd_api_error(mp, EFMD_THR_INVAL, "tid %u is not a valid "
1761 		    "auxiliary thread id for module %s\n", tid, mp->mod_name);
1762 	}
1763 
1764 	fmd_ckpt_save(mp);
1765 
1766 	fmd_module_unlock(mp);
1767 }
1768 
1769 id_t
1770 fmd_timer_install(fmd_hdl_t *hdl, void *arg, fmd_event_t *ep, hrtime_t delta)
1771 {
1772 	fmd_module_t *mp = fmd_api_module_lock(hdl);
1773 	fmd_modtimer_t *t;
1774 	id_t id;
1775 
1776 	if (delta < 0) {
1777 		fmd_api_error(mp, EFMD_TIMER_INVAL,
1778 		    "timer delta %lld is not a valid interval\n", delta);
1779 	}
1780 
1781 	t = fmd_alloc(sizeof (fmd_modtimer_t), FMD_SLEEP);
1782 	t->mt_mod = mp;
1783 	t->mt_arg = arg;
1784 	t->mt_id = -1;
1785 
1786 	if ((id = fmd_timerq_install(fmd.d_timers, mp->mod_timerids,
1787 	    (fmd_timer_f *)fmd_module_timeout, t, ep, delta)) == -1) {
1788 		fmd_free(t, sizeof (fmd_modtimer_t));
1789 		fmd_api_error(mp, EFMD_TIMER_LIMIT,
1790 		    "failed to install timer +%lld", delta);
1791 	}
1792 
1793 	fmd_module_unlock(mp);
1794 	return (id);
1795 }
1796 
1797 void
1798 fmd_timer_remove(fmd_hdl_t *hdl, id_t id)
1799 {
1800 	fmd_module_t *mp = fmd_api_module_lock(hdl);
1801 	fmd_modtimer_t *t;
1802 
1803 	if (!fmd_idspace_valid(mp->mod_timerids, id)) {
1804 		fmd_api_error(mp, EFMD_TIMER_INVAL,
1805 		    "id %ld is not a valid timer id\n", id);
1806 	}
1807 
1808 	/*
1809 	 * If the timer has not fired (t != NULL), remove it from the timer
1810 	 * queue.  If the timer has fired (t == NULL), we could be in one of
1811 	 * two situations: a) we are processing the timer callback or b)
1812 	 * the timer event is on the module queue awaiting dispatch.  For a),
1813 	 * fmd_timerq_remove() will wait for the timer callback function
1814 	 * to complete and queue an event for dispatch.  For a) and b),
1815 	 * we cancel the outstanding timer event from the module's dispatch
1816 	 * queue.
1817 	 */
1818 	if ((t = fmd_timerq_remove(fmd.d_timers, mp->mod_timerids, id)) != NULL)
1819 		fmd_free(t, sizeof (fmd_modtimer_t));
1820 	fmd_module_unlock(mp);
1821 
1822 	fmd_eventq_cancel(mp->mod_queue, FMD_EVT_TIMEOUT, (void *)id);
1823 }
1824 
1825 nvlist_t *
1826 fmd_nvl_create_fault(fmd_hdl_t *hdl, const char *class,
1827     uint8_t certainty, nvlist_t *asru, nvlist_t *fru, nvlist_t *rsrc)
1828 {
1829 	fmd_module_t *mp;
1830 	nvlist_t *nvl;
1831 
1832 	mp = fmd_api_module_lock(hdl);
1833 	if (class == NULL || class[0] == '\0')
1834 		fmd_api_error(mp, EFMD_NVL_INVAL, "invalid fault class\n");
1835 
1836 	nvl = fmd_protocol_fault(class, certainty, asru, fru, rsrc, NULL);
1837 
1838 	fmd_module_unlock(mp);
1839 
1840 	return (nvl);
1841 }
1842 
1843 int
1844 fmd_nvl_class_match(fmd_hdl_t *hdl, nvlist_t *nvl, const char *pattern)
1845 {
1846 	fmd_module_t *mp = fmd_api_module_lock(hdl);
1847 	char *class;
1848 	int rv;
1849 
1850 	rv = (nvl != NULL && nvlist_lookup_string(nvl,
1851 	    FM_CLASS, &class) == 0 && fmd_strmatch(class, pattern));
1852 
1853 	fmd_module_unlock(mp);
1854 	return (rv);
1855 }
1856 
1857 int
1858 fmd_nvl_fmri_expand(fmd_hdl_t *hdl, nvlist_t *nvl)
1859 {
1860 	fmd_module_t *mp = fmd_api_module_lock(hdl);
1861 	int rv;
1862 
1863 	if (nvl == NULL) {
1864 		fmd_api_error(mp, EFMD_NVL_INVAL,
1865 		    "invalid nvlist %p\n", (void *)nvl);
1866 	}
1867 
1868 	rv = fmd_fmri_expand(nvl);
1869 	fmd_module_unlock(mp);
1870 	return (rv);
1871 }
1872 
1873 int
1874 fmd_nvl_fmri_present(fmd_hdl_t *hdl, nvlist_t *nvl)
1875 {
1876 	fmd_module_t *mp = fmd_api_module_lock(hdl);
1877 	int rv;
1878 
1879 	if (nvl == NULL) {
1880 		fmd_api_error(mp, EFMD_NVL_INVAL,
1881 		    "invalid nvlist %p\n", (void *)nvl);
1882 	}
1883 
1884 	rv = fmd_fmri_present(nvl);
1885 	fmd_module_unlock(mp);
1886 
1887 	if (rv < 0) {
1888 		fmd_api_error(mp, EFMD_FMRI_OP, "invalid fmri for "
1889 		    "fmd_nvl_fmri_present\n");
1890 	}
1891 
1892 	return (rv);
1893 }
1894 
1895 int
1896 fmd_nvl_fmri_replaced(fmd_hdl_t *hdl, nvlist_t *nvl)
1897 {
1898 	fmd_module_t *mp = fmd_api_module_lock(hdl);
1899 	int rv;
1900 
1901 	if (nvl == NULL) {
1902 		fmd_api_error(mp, EFMD_NVL_INVAL,
1903 		    "invalid nvlist %p\n", (void *)nvl);
1904 	}
1905 
1906 	rv = fmd_fmri_replaced(nvl);
1907 	fmd_module_unlock(mp);
1908 
1909 	return (rv);
1910 }
1911 
1912 int
1913 fmd_nvl_fmri_unusable(fmd_hdl_t *hdl, nvlist_t *nvl)
1914 {
1915 	fmd_module_t *mp = fmd_api_module_lock(hdl);
1916 	int rv;
1917 
1918 	if (nvl == NULL) {
1919 		fmd_api_error(mp, EFMD_NVL_INVAL,
1920 		    "invalid nvlist %p\n", (void *)nvl);
1921 	}
1922 
1923 	rv = fmd_fmri_unusable(nvl);
1924 	fmd_module_unlock(mp);
1925 
1926 	if (rv < 0) {
1927 		fmd_api_error(mp, EFMD_FMRI_OP, "invalid fmri for "
1928 		    "fmd_nvl_fmri_unusable\n");
1929 	}
1930 
1931 	return (rv);
1932 }
1933 
1934 int
1935 fmd_nvl_fmri_retire(fmd_hdl_t *hdl, nvlist_t *nvl)
1936 {
1937 	fmd_module_t *mp = fmd_api_module_lock(hdl);
1938 	int rv;
1939 
1940 	if (nvl == NULL) {
1941 		fmd_api_error(mp, EFMD_NVL_INVAL,
1942 		    "invalid nvlist %p\n", (void *)nvl);
1943 	}
1944 
1945 	rv = fmd_fmri_retire(nvl);
1946 	fmd_module_unlock(mp);
1947 
1948 	return (rv);
1949 }
1950 
1951 int
1952 fmd_nvl_fmri_unretire(fmd_hdl_t *hdl, nvlist_t *nvl)
1953 {
1954 	fmd_module_t *mp = fmd_api_module_lock(hdl);
1955 	int rv;
1956 
1957 	if (nvl == NULL) {
1958 		fmd_api_error(mp, EFMD_NVL_INVAL,
1959 		    "invalid nvlist %p\n", (void *)nvl);
1960 	}
1961 
1962 	rv = fmd_fmri_unretire(nvl);
1963 	fmd_module_unlock(mp);
1964 
1965 	return (rv);
1966 }
1967 
1968 int
1969 fmd_nvl_fmri_service_state(fmd_hdl_t *hdl, nvlist_t *nvl)
1970 {
1971 	fmd_module_t *mp = fmd_api_module_lock(hdl);
1972 	int rv;
1973 
1974 	if (nvl == NULL) {
1975 		fmd_api_error(mp, EFMD_NVL_INVAL,
1976 		    "invalid nvlist %p\n", (void *)nvl);
1977 	}
1978 
1979 	rv = fmd_fmri_service_state(nvl);
1980 	if (rv < 0)
1981 		rv = fmd_fmri_unusable(nvl) ? FMD_SERVICE_STATE_UNUSABLE :
1982 		    FMD_SERVICE_STATE_OK;
1983 	fmd_module_unlock(mp);
1984 
1985 	if (rv < 0) {
1986 		fmd_api_error(mp, EFMD_FMRI_OP, "invalid fmri for "
1987 		    "fmd_nvl_fmri_service_state\n");
1988 	}
1989 
1990 	return (rv);
1991 }
1992 
1993 typedef struct {
1994 	const char	*class;
1995 	int	*rvp;
1996 } fmd_has_fault_arg_t;
1997 
1998 static void
1999 fmd_rsrc_has_fault(fmd_asru_link_t *alp, void *arg)
2000 {
2001 	fmd_has_fault_arg_t *fhfp = (fmd_has_fault_arg_t *)arg;
2002 	char *class;
2003 
2004 	if (fhfp->class == NULL) {
2005 		if (alp->al_flags & FMD_ASRU_FAULTY)
2006 			*fhfp->rvp = 1;
2007 	} else {
2008 		if ((alp->al_flags & FMD_ASRU_FAULTY) &&
2009 		    alp->al_event != NULL && nvlist_lookup_string(alp->al_event,
2010 		    FM_CLASS, &class) == 0 && fmd_strmatch(class, fhfp->class))
2011 			*fhfp->rvp = 1;
2012 	}
2013 }
2014 
2015 int
2016 fmd_nvl_fmri_has_fault(fmd_hdl_t *hdl, nvlist_t *nvl, int type, char *class)
2017 {
2018 	fmd_module_t *mp = fmd_api_module_lock(hdl);
2019 	fmd_asru_hash_t *ahp = fmd.d_asrus;
2020 	int rv = 0;
2021 	char *name;
2022 	int namelen;
2023 	fmd_has_fault_arg_t fhf;
2024 
2025 	if (nvl == NULL) {
2026 		fmd_api_error(mp, EFMD_NVL_INVAL,
2027 		    "invalid nvlist %p\n", (void *)nvl);
2028 	}
2029 	if ((namelen = fmd_fmri_nvl2str(nvl, NULL, 0)) == -1)
2030 		fmd_api_error(mp, EFMD_NVL_INVAL,
2031 		    "invalid nvlist: %p\n", (void *)nvl);
2032 	name = fmd_alloc(namelen + 1, FMD_SLEEP);
2033 	if (fmd_fmri_nvl2str(nvl, name, namelen + 1) == -1) {
2034 		if (name != NULL)
2035 			fmd_free(name, namelen + 1);
2036 		fmd_api_error(mp, EFMD_NVL_INVAL,
2037 		    "invalid nvlist: %p\n", (void *)nvl);
2038 	}
2039 
2040 	fhf.class = class;
2041 	fhf.rvp = &rv;
2042 	if (type == FMD_HAS_FAULT_RESOURCE)
2043 		fmd_asru_hash_apply_by_rsrc(ahp, name, fmd_rsrc_has_fault,
2044 		    &fhf);
2045 	else if (type == FMD_HAS_FAULT_ASRU)
2046 		fmd_asru_hash_apply_by_asru(ahp, name, fmd_rsrc_has_fault,
2047 		    &fhf);
2048 	else if (type == FMD_HAS_FAULT_FRU)
2049 		fmd_asru_hash_apply_by_fru(ahp, name, fmd_rsrc_has_fault,
2050 		    &fhf);
2051 
2052 	if (name != NULL)
2053 		fmd_free(name, namelen + 1);
2054 	fmd_module_unlock(mp);
2055 	return (rv);
2056 }
2057 
2058 int
2059 fmd_nvl_fmri_contains(fmd_hdl_t *hdl, nvlist_t *n1, nvlist_t *n2)
2060 {
2061 	fmd_module_t *mp = fmd_api_module_lock(hdl);
2062 	int rv;
2063 
2064 	if (n1 == NULL || n2 == NULL) {
2065 		fmd_api_error(mp, EFMD_NVL_INVAL,
2066 		    "invalid nvlist(s): %p, %p\n", (void *)n1, (void *)n2);
2067 	}
2068 
2069 	rv = fmd_fmri_contains(n1, n2);
2070 	fmd_module_unlock(mp);
2071 
2072 	if (rv < 0) {
2073 		fmd_api_error(mp, EFMD_FMRI_OP, "invalid fmri for "
2074 		    "fmd_nvl_fmri_contains\n");
2075 	}
2076 
2077 	return (rv);
2078 }
2079 
2080 nvlist_t *
2081 fmd_nvl_fmri_translate(fmd_hdl_t *hdl, nvlist_t *fmri, nvlist_t *auth)
2082 {
2083 	fmd_module_t *mp = fmd_api_module_lock(hdl);
2084 	nvlist_t *xfmri;
2085 
2086 	if (fmri == NULL || auth == NULL) {
2087 		fmd_api_error(mp, EFMD_NVL_INVAL,
2088 		    "invalid nvlist(s): %p, %p\n", (void *)fmri, (void *)auth);
2089 	}
2090 
2091 	xfmri = fmd_fmri_translate(fmri, auth);
2092 	fmd_module_unlock(mp);
2093 	return (xfmri);
2094 }
2095 
2096 static int
2097 fmd_nvl_op_init(nv_alloc_t *ops, va_list ap)
2098 {
2099 	fmd_module_t *mp = va_arg(ap, fmd_module_t *);
2100 
2101 	ops->nva_arg = mp;
2102 
2103 	return (0);
2104 }
2105 
2106 static void *
2107 fmd_nvl_op_alloc_sleep(nv_alloc_t *ops, size_t size)
2108 {
2109 	fmd_module_t *mp = ops->nva_arg;
2110 
2111 	return (fmd_hdl_alloc_locked(mp, size, FMD_SLEEP));
2112 }
2113 
2114 static void *
2115 fmd_nvl_op_alloc_nosleep(nv_alloc_t *ops, size_t size)
2116 {
2117 	fmd_module_t *mp = ops->nva_arg;
2118 
2119 	return (fmd_hdl_alloc_locked(mp, size, FMD_NOSLEEP));
2120 }
2121 
2122 static void
2123 fmd_nvl_op_free(nv_alloc_t *ops, void *data, size_t size)
2124 {
2125 	fmd_module_t *mp = ops->nva_arg;
2126 
2127 	fmd_hdl_free_locked(mp, data, size);
2128 }
2129 
2130 nv_alloc_ops_t fmd_module_nva_ops_sleep = {
2131 	fmd_nvl_op_init,
2132 	NULL,
2133 	fmd_nvl_op_alloc_sleep,
2134 	fmd_nvl_op_free,
2135 	NULL
2136 };
2137 
2138 nv_alloc_ops_t fmd_module_nva_ops_nosleep = {
2139 	fmd_nvl_op_init,
2140 	NULL,
2141 	fmd_nvl_op_alloc_nosleep,
2142 	fmd_nvl_op_free,
2143 	NULL
2144 };
2145 
2146 nvlist_t *
2147 fmd_nvl_alloc(fmd_hdl_t *hdl, int flags)
2148 {
2149 	fmd_module_t *mp = fmd_api_module_lock(hdl);
2150 	nv_alloc_t *nva;
2151 	nvlist_t *nvl;
2152 	int ret;
2153 
2154 	if (flags == FMD_SLEEP)
2155 		nva = &mp->mod_nva_sleep;
2156 	else
2157 		nva = &mp->mod_nva_nosleep;
2158 
2159 	ret = nvlist_xalloc(&nvl, NV_UNIQUE_NAME, nva);
2160 
2161 	fmd_module_unlock(mp);
2162 
2163 	if (ret != 0)
2164 		return (NULL);
2165 	else
2166 		return (nvl);
2167 }
2168 
2169 nvlist_t *
2170 fmd_nvl_dup(fmd_hdl_t *hdl, nvlist_t *src, int flags)
2171 {
2172 	fmd_module_t *mp = fmd_api_module_lock(hdl);
2173 	nv_alloc_t *nva;
2174 	nvlist_t *nvl;
2175 	int ret;
2176 
2177 	if (flags == FMD_SLEEP)
2178 		nva = &mp->mod_nva_sleep;
2179 	else
2180 		nva = &mp->mod_nva_nosleep;
2181 
2182 	ret = nvlist_xdup(src, &nvl, nva);
2183 
2184 	fmd_module_unlock(mp);
2185 
2186 	if (ret != 0)
2187 		return (NULL);
2188 	else
2189 		return (nvl);
2190 }
2191 
2192 /*ARGSUSED*/
2193 void
2194 fmd_repair_fru(fmd_hdl_t *hdl, const char *fmri)
2195 {
2196 	int err;
2197 	fmd_asru_rep_arg_t fara;
2198 
2199 	fara.fara_reason = FMD_ASRU_REPAIRED;
2200 	fara.fara_bywhat = FARA_BY_FRU;
2201 	fara.fara_rval = &err;
2202 	fmd_asru_hash_apply_by_fru(fmd.d_asrus, (char *)fmri,
2203 	    fmd_asru_repaired, &fara);
2204 }
2205 
2206 /*ARGSUSED*/
2207 int
2208 fmd_repair_asru(fmd_hdl_t *hdl, const char *fmri)
2209 {
2210 	int err = FARA_ERR_RSRCNOTF;
2211 	fmd_asru_rep_arg_t fara;
2212 
2213 	fara.fara_reason = FMD_ASRU_REPAIRED;
2214 	fara.fara_rval = &err;
2215 	fara.fara_uuid = NULL;
2216 	fara.fara_bywhat = FARA_BY_ASRU;
2217 	fmd_asru_hash_apply_by_asru(fmd.d_asrus, fmri,
2218 	    fmd_asru_repaired, &fara);
2219 	return (err);
2220 }
2221 
2222 int
2223 fmd_event_local(fmd_hdl_t *hdl, fmd_event_t *ep)
2224 {
2225 	if (hdl == NULL || ep == NULL) {
2226 		fmd_api_error(fmd_api_module_lock(hdl), EFMD_EVENT_INVAL,
2227 		    "NULL parameter specified to fmd_event_local\n");
2228 	}
2229 
2230 	return (((fmd_event_impl_t *)ep)->ev_flags & FMD_EVF_LOCAL);
2231 }
2232 
2233 /*ARGSUSED*/
2234 uint64_t
2235 fmd_event_ena_create(fmd_hdl_t *hdl)
2236 {
2237 	return (fmd_ena());
2238 }
2239 
2240 fmd_xprt_t *
2241 fmd_xprt_open(fmd_hdl_t *hdl, uint_t flags, nvlist_t *auth, void *data)
2242 {
2243 	fmd_module_t *mp = fmd_api_module_lock(hdl);
2244 	fmd_xprt_t *xp;
2245 
2246 	if (flags & ~FMD_XPRT_CMASK) {
2247 		fmd_api_error(mp, EFMD_XPRT_INVAL,
2248 		    "invalid transport flags 0x%x\n", flags);
2249 	}
2250 
2251 	if ((flags & FMD_XPRT_RDWR) != FMD_XPRT_RDWR &&
2252 	    (flags & FMD_XPRT_RDWR) != FMD_XPRT_RDONLY) {
2253 		fmd_api_error(mp, EFMD_XPRT_INVAL,
2254 		    "cannot open write-only transport\n");
2255 	}
2256 
2257 	if (mp->mod_stats->ms_xprtopen.fmds_value.ui32 >=
2258 	    mp->mod_stats->ms_xprtlimit.fmds_value.ui32) {
2259 		fmd_api_error(mp, EFMD_XPRT_LIMIT, "%s request to create a "
2260 		    "transport exceeds module transport limit (%u)\n",
2261 		    mp->mod_name, mp->mod_stats->ms_xprtlimit.fmds_value.ui32);
2262 	}
2263 
2264 	if ((xp = fmd_xprt_create(mp, flags, auth, data)) == NULL)
2265 		fmd_api_error(mp, errno, "cannot create transport");
2266 
2267 	fmd_module_unlock(mp);
2268 	return (xp);
2269 }
2270 
2271 void
2272 fmd_xprt_close(fmd_hdl_t *hdl, fmd_xprt_t *xp)
2273 {
2274 	fmd_module_t *mp = fmd_api_module_lock(hdl);
2275 	fmd_xprt_impl_t *xip = fmd_api_transport_impl(hdl, xp);
2276 
2277 	/*
2278 	 * Although this could be supported, it doesn't seem necessary or worth
2279 	 * the trouble.  For now, just detect this and trigger a module abort.
2280 	 * If it is needed, transports should grow reference counts and a new
2281 	 * event type will need to be enqueued for the main thread to reap it.
2282 	 */
2283 	if (xip->xi_thread != NULL &&
2284 	    xip->xi_thread->thr_tid == pthread_self()) {
2285 		fmd_api_error(mp, EFMD_XPRT_INVAL,
2286 		    "fmd_xprt_close() cannot be called from fmdo_send()\n");
2287 	}
2288 
2289 	fmd_xprt_destroy(xp);
2290 	fmd_module_unlock(mp);
2291 }
2292 
2293 void
2294 fmd_xprt_post(fmd_hdl_t *hdl, fmd_xprt_t *xp, nvlist_t *nvl, hrtime_t hrt)
2295 {
2296 	nv_alloc_t *nva = nvlist_lookup_nv_alloc(nvl);
2297 	fmd_module_t *mp = fmd_api_module(hdl);
2298 	fmd_xprt_impl_t *xip = fmd_api_transport_impl(hdl, xp);
2299 	nvlist_t *tmp;
2300 
2301 	/*
2302 	 * If this event was allocated using the module-specific nvlist ops, we
2303 	 * need to create a copy using the standard fmd nvlist ops.  Otherwise,
2304 	 * the event may persist after the module has been unloaded and we'll
2305 	 * die when attempting to free the nvlist.
2306 	 */
2307 	if (nva == &mp->mod_nva_sleep || nva == &mp->mod_nva_nosleep) {
2308 		(void) nvlist_xdup(nvl, &tmp, &fmd.d_nva);
2309 		nvlist_free(nvl);
2310 		nvl = tmp;
2311 	}
2312 
2313 	/*
2314 	 * fmd_xprt_recv() must block during startup waiting for fmd to globally
2315 	 * clear FMD_XPRT_DSUSPENDED.  As such, we can't allow it to be called
2316 	 * from a module's _fmd_init() routine, because that would block
2317 	 * fmd from completing initial module loading, resulting in a deadlock.
2318 	 */
2319 	if ((xip->xi_flags & FMD_XPRT_ISUSPENDED) &&
2320 	    (pthread_self() == xip->xi_queue->eq_mod->mod_thread->thr_tid)) {
2321 		fmd_api_error(fmd_api_module_lock(hdl), EFMD_XPRT_INVAL,
2322 		    "fmd_xprt_post() cannot be called from _fmd_init()\n");
2323 	}
2324 
2325 	fmd_xprt_recv(xp, nvl, hrt, FMD_B_FALSE);
2326 }
2327 
2328 void
2329 fmd_xprt_log(fmd_hdl_t *hdl, fmd_xprt_t *xp, nvlist_t *nvl, hrtime_t hrt)
2330 {
2331 	fmd_xprt_impl_t *xip = fmd_api_transport_impl(hdl, xp);
2332 
2333 	/*
2334 	 * fmd_xprt_recv() must block during startup waiting for fmd to globally
2335 	 * clear FMD_XPRT_DSUSPENDED.  As such, we can't allow it to be called
2336 	 * from a module's _fmd_init() routine, because that would block
2337 	 * fmd from completing initial module loading, resulting in a deadlock.
2338 	 */
2339 	if ((xip->xi_flags & FMD_XPRT_ISUSPENDED) &&
2340 	    (pthread_self() == xip->xi_queue->eq_mod->mod_thread->thr_tid)) {
2341 		fmd_api_error(fmd_api_module_lock(hdl), EFMD_XPRT_INVAL,
2342 		    "fmd_xprt_log() cannot be called from _fmd_init()\n");
2343 	}
2344 
2345 	fmd_xprt_recv(xp, nvl, hrt, FMD_B_TRUE);
2346 }
2347 
2348 void
2349 fmd_xprt_suspend(fmd_hdl_t *hdl, fmd_xprt_t *xp)
2350 {
2351 	(void) fmd_api_transport_impl(hdl, xp); /* validate 'xp' */
2352 	fmd_xprt_xsuspend(xp, FMD_XPRT_SUSPENDED);
2353 }
2354 
2355 void
2356 fmd_xprt_resume(fmd_hdl_t *hdl, fmd_xprt_t *xp)
2357 {
2358 	(void) fmd_api_transport_impl(hdl, xp); /* validate 'xp' */
2359 	fmd_xprt_xresume(xp, FMD_XPRT_SUSPENDED);
2360 }
2361 
2362 int
2363 fmd_xprt_error(fmd_hdl_t *hdl, fmd_xprt_t *xp)
2364 {
2365 	fmd_xprt_impl_t *xip = fmd_api_transport_impl(hdl, xp);
2366 	return (xip->xi_state == _fmd_xprt_state_err);
2367 }
2368 
2369 /*
2370  * Translate all FMRIs in the specified name-value pair list for the specified
2371  * FMRI authority, and return a new name-value pair list for the translation.
2372  * This function is the recursive engine used by fmd_xprt_translate(), below.
2373  */
2374 static nvlist_t *
2375 fmd_xprt_xtranslate(nvlist_t *nvl, nvlist_t *auth)
2376 {
2377 	uint_t i, j, n;
2378 	nvpair_t *nvp, **nvps;
2379 	uint_t nvpslen = 0;
2380 	char *name;
2381 	size_t namelen = 0;
2382 
2383 	nvlist_t **a, **b;
2384 	nvlist_t *l, *r;
2385 	data_type_t type;
2386 	char *s;
2387 	int err;
2388 
2389 	(void) nvlist_xdup(nvl, &nvl, &fmd.d_nva);
2390 
2391 	/*
2392 	 * Count up the number of name-value pairs in 'nvl' and compute the
2393 	 * maximum length of a name used in this list for use below.
2394 	 */
2395 	for (nvp = nvlist_next_nvpair(nvl, NULL);
2396 	    nvp != NULL; nvp = nvlist_next_nvpair(nvl, nvp), nvpslen++) {
2397 		size_t len = strlen(nvpair_name(nvp));
2398 		namelen = MAX(namelen, len);
2399 	}
2400 
2401 	nvps = alloca(sizeof (nvpair_t *) * nvpslen);
2402 	name = alloca(namelen + 1);
2403 
2404 	/*
2405 	 * Store a snapshot of the name-value pairs in 'nvl' into nvps[] so
2406 	 * that we can iterate over the original pairs in the loop below while
2407 	 * performing arbitrary insert and delete operations on 'nvl' itself.
2408 	 */
2409 	for (i = 0, nvp = nvlist_next_nvpair(nvl, NULL);
2410 	    nvp != NULL; nvp = nvlist_next_nvpair(nvl, nvp))
2411 		nvps[i++] = nvp;
2412 
2413 	/*
2414 	 * Now iterate over the snapshot of the name-value pairs.  If we find a
2415 	 * value that is of type NVLIST or NVLIST_ARRAY, we translate that
2416 	 * object by either calling ourself recursively on it, or calling into
2417 	 * fmd_fmri_translate() if the object is an FMRI.  We then rip out the
2418 	 * original name-value pair and replace it with the translated one.
2419 	 */
2420 	for (i = 0; i < nvpslen; i++) {
2421 		nvp = nvps[i];
2422 		type = nvpair_type(nvp);
2423 
2424 		switch (type) {
2425 		case DATA_TYPE_NVLIST_ARRAY:
2426 			if (nvpair_value_nvlist_array(nvp, &a, &n) != 0 ||
2427 			    a == NULL || n == 0)
2428 				continue; /* array is zero-sized; skip it */
2429 
2430 			b = fmd_alloc(sizeof (nvlist_t *) * n, FMD_SLEEP);
2431 
2432 			/*
2433 			 * If the first array nvlist element looks like an FMRI
2434 			 * then assume the other elements are FMRIs as well.
2435 			 * If any b[j]'s can't be translated, then EINVAL will
2436 			 * be returned from nvlist_add_nvlist_array() below.
2437 			 */
2438 			if (nvlist_lookup_string(*a, FM_FMRI_SCHEME, &s) == 0) {
2439 				for (j = 0; j < n; j++)
2440 					b[j] = fmd_fmri_translate(a[j], auth);
2441 			} else {
2442 				for (j = 0; j < n; j++)
2443 					b[j] = fmd_xprt_xtranslate(a[j], auth);
2444 			}
2445 
2446 			(void) strcpy(name, nvpair_name(nvp));
2447 			(void) nvlist_remove(nvl, name, type);
2448 			err = nvlist_add_nvlist_array(nvl, name, b, n);
2449 
2450 			for (j = 0; j < n; j++)
2451 				nvlist_free(b[j]);
2452 
2453 			fmd_free(b, sizeof (nvlist_t *) * n);
2454 
2455 			if (err != 0) {
2456 				nvlist_free(nvl);
2457 				errno = err;
2458 				return (NULL);
2459 			}
2460 			break;
2461 
2462 		case DATA_TYPE_NVLIST:
2463 			if (nvpair_value_nvlist(nvp, &l) == 0 &&
2464 			    nvlist_lookup_string(l, FM_FMRI_SCHEME, &s) == 0)
2465 				r = fmd_fmri_translate(l, auth);
2466 			else
2467 				r = fmd_xprt_xtranslate(l, auth);
2468 
2469 			if (r == NULL) {
2470 				nvlist_free(nvl);
2471 				return (NULL);
2472 			}
2473 
2474 			(void) strcpy(name, nvpair_name(nvp));
2475 			(void) nvlist_remove(nvl, name, type);
2476 			(void) nvlist_add_nvlist(nvl, name, r);
2477 
2478 			nvlist_free(r);
2479 			break;
2480 		}
2481 	}
2482 
2483 	return (nvl);
2484 }
2485 
2486 nvlist_t *
2487 fmd_xprt_translate(fmd_hdl_t *hdl, fmd_xprt_t *xp, fmd_event_t *ep)
2488 {
2489 	fmd_xprt_impl_t *xip = fmd_api_transport_impl(hdl, xp);
2490 
2491 	if (xip->xi_auth == NULL) {
2492 		fmd_api_error(fmd_api_module_lock(hdl), EFMD_XPRT_INVAL,
2493 		    "no authority defined for transport %p\n", (void *)xp);
2494 	}
2495 
2496 	return (fmd_xprt_xtranslate(FMD_EVENT_NVL(ep), xip->xi_auth));
2497 }
2498 
2499 /*ARGSUSED*/
2500 void
2501 fmd_xprt_add_domain(fmd_hdl_t *hdl, nvlist_t *nvl, char *domain)
2502 {
2503 	nvpair_t *nvp, *nvp2;
2504 	nvlist_t *nvl2, *nvl3;
2505 	char *class;
2506 
2507 	if (nvl == NULL || domain == NULL)
2508 		return;
2509 	for (nvp = nvlist_next_nvpair(nvl, NULL); nvp != NULL;
2510 	    nvp = nvlist_next_nvpair(nvl, nvp)) {
2511 		if (strcmp(nvpair_name(nvp), FM_CLASS) == 0) {
2512 			(void) nvpair_value_string(nvp, &class);
2513 			if (strcmp(class, FM_LIST_SUSPECT_CLASS) != 0)
2514 				return;
2515 		}
2516 	}
2517 	for (nvp = nvlist_next_nvpair(nvl, NULL); nvp != NULL;
2518 	    nvp = nvlist_next_nvpair(nvl, nvp)) {
2519 		if (strcmp(nvpair_name(nvp), FM_SUSPECT_DE) == 0) {
2520 			(void) nvpair_value_nvlist(nvp, &nvl2);
2521 			for (nvp2 = nvlist_next_nvpair(nvl2, NULL);
2522 			    nvp2 != NULL;
2523 			    nvp2 = nvlist_next_nvpair(nvl2, nvp2)) {
2524 				if (strcmp(nvpair_name(nvp2),
2525 				    FM_FMRI_AUTHORITY) == 0) {
2526 					(void) nvpair_value_nvlist(nvp2, &nvl3);
2527 					(void) nvlist_add_string(nvl3,
2528 					    FM_FMRI_AUTH_DOMAIN, domain);
2529 					break;
2530 				}
2531 			}
2532 			break;
2533 		}
2534 	}
2535 }
2536 
2537 void
2538 fmd_xprt_setspecific(fmd_hdl_t *hdl, fmd_xprt_t *xp, void *data)
2539 {
2540 	fmd_api_transport_impl(hdl, xp)->xi_data = data;
2541 }
2542 
2543 void *
2544 fmd_xprt_getspecific(fmd_hdl_t *hdl, fmd_xprt_t *xp)
2545 {
2546 	return (fmd_api_transport_impl(hdl, xp)->xi_data);
2547 }
2548