xref: /titanic_52/usr/src/cmd/fm/fmd/common/fmd_api.c (revision f498645a3eecf2ddd304b4ea9c7f1b4c155ff79e)
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/types.h>
30 #include <sys/fm/protocol.h>
31 #include <fm/libtopo.h>
32 
33 #include <unistd.h>
34 #include <signal.h>
35 #include <limits.h>
36 #include <syslog.h>
37 #include <alloca.h>
38 
39 #include <fmd_module.h>
40 #include <fmd_api.h>
41 #include <fmd_string.h>
42 #include <fmd_subr.h>
43 #include <fmd_error.h>
44 #include <fmd_event.h>
45 #include <fmd_eventq.h>
46 #include <fmd_dispq.h>
47 #include <fmd_timerq.h>
48 #include <fmd_thread.h>
49 #include <fmd_ustat.h>
50 #include <fmd_case.h>
51 #include <fmd_protocol.h>
52 #include <fmd_buf.h>
53 #include <fmd_asru.h>
54 #include <fmd_fmri.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_3)
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, sizeof (ops) - sizeof (void *));
396 	else
397 		bcopy(mip->fmdi_ops, &ops, sizeof (ops));
398 
399 	if (ops.fmdo_recv == NULL)
400 		ops.fmdo_recv = (void (*)())fmd_hdl_nop;
401 	if (ops.fmdo_timeout == NULL)
402 		ops.fmdo_timeout = (void (*)())fmd_hdl_nop;
403 	if (ops.fmdo_close == NULL)
404 		ops.fmdo_close = (void (*)())fmd_hdl_nop;
405 	if (ops.fmdo_stats == NULL)
406 		ops.fmdo_stats = (void (*)())fmd_hdl_nop;
407 	if (ops.fmdo_gc == NULL)
408 		ops.fmdo_gc = (void (*)())fmd_hdl_nop;
409 	if (ops.fmdo_send == NULL)
410 		ops.fmdo_send = (int (*)())fmd_hdl_nop;
411 
412 	/*
413 	 * Make two passes through the property array to initialize the formals
414 	 * to use for processing the module's .conf file.  In the first pass,
415 	 * we validate the types and count the number of properties.  In the
416 	 * second pass we copy the strings and fill in the appropriate ops.
417 	 */
418 	for (prop = mip->fmdi_props, i = 0; prop != NULL &&
419 	    prop->fmdp_name != NULL; prop++, i++) {
420 		if (prop->fmdp_type >=
421 		    sizeof (_fmd_prop_ops) / sizeof (_fmd_prop_ops[0])) {
422 			fmd_api_xerror(mp, EFMD_HDL_PROP,
423 			    "property %s uses invalid type %u\n",
424 			    prop->fmdp_name, prop->fmdp_type);
425 			return (fmd_hdl_register_error(mp, EFMD_HDL_PROP));
426 		}
427 	}
428 
429 	mp->mod_argc = i;
430 	mp->mod_argv = fmd_zalloc(sizeof (fmd_conf_formal_t) * i, FMD_SLEEP);
431 
432 	prop = mip->fmdi_props;
433 	cfp = mp->mod_argv;
434 
435 	for (i = 0; i < mp->mod_argc; i++, prop++, cfp++) {
436 		cfp->cf_name = fmd_strdup(prop->fmdp_name, FMD_SLEEP);
437 		cfp->cf_ops = _fmd_prop_ops[prop->fmdp_type];
438 		cfp->cf_default = fmd_strdup(prop->fmdp_defv, FMD_SLEEP);
439 	}
440 
441 	/*
442 	 * If this module came from an on-disk file, compute the name of the
443 	 * corresponding .conf file and parse properties from it if it exists.
444 	 */
445 	if (mp->mod_path != NULL) {
446 		(void) strlcpy(buf, mp->mod_path, sizeof (buf));
447 		(void) fmd_strdirname(buf);
448 
449 		(void) strlcat(buf, "/", sizeof (buf));
450 		(void) strlcat(buf, mp->mod_name, sizeof (buf));
451 		(void) strlcat(buf, ".conf", sizeof (buf));
452 
453 		if (access(buf, F_OK) == 0)
454 			conf = buf;
455 	}
456 
457 	if ((mp->mod_conf = fmd_conf_open(conf,
458 	    mp->mod_argc, mp->mod_argv, 0)) == NULL)
459 		return (fmd_hdl_register_error(mp, EFMD_MOD_CONF));
460 
461 	fmd_conf_propagate(fmd.d_conf, mp->mod_conf, mp->mod_name);
462 
463 	/*
464 	 * Look up the list of the libdiagcode dictionaries associated with the
465 	 * module.  If none were specified, use the value from daemon's config.
466 	 * We only fail if the module specified an explicit dictionary.
467 	 */
468 	(void) fmd_conf_getprop(mp->mod_conf, FMD_PROP_DICTIONARIES, &pap);
469 	if (pap->cpa_argc == 0 && mp->mod_ops == &fmd_bltin_ops)
470 		(void) fmd_conf_getprop(fmd.d_conf, "self.dict", &pap);
471 
472 	for (i = 0; i < pap->cpa_argc; i++) {
473 		if (fmd_module_dc_opendict(mp, pap->cpa_argv[i]) != 0) {
474 			fmd_api_xerror(mp, errno,
475 			    "failed to open dictionary %s", pap->cpa_argv[i]);
476 			return (fmd_hdl_register_error(mp, EFMD_MOD_CONF));
477 		}
478 	}
479 
480 	/*
481 	 * Make a copy of the handle information and store it in mod_info.  We
482 	 * do not need to bother copying fmdi_props since they're already read.
483 	 */
484 	mp->mod_info = fmd_alloc(sizeof (fmd_hdl_info_t), FMD_SLEEP);
485 	mp->mod_info->fmdi_desc = fmd_strdup(mip->fmdi_desc, FMD_SLEEP);
486 	mp->mod_info->fmdi_vers = fmd_strdup(mip->fmdi_vers, FMD_SLEEP);
487 	mp->mod_info->fmdi_ops = fmd_alloc(sizeof (fmd_hdl_ops_t), FMD_SLEEP);
488 	bcopy(&ops, (void *)mp->mod_info->fmdi_ops, sizeof (fmd_hdl_ops_t));
489 	mp->mod_info->fmdi_props = NULL;
490 
491 	/*
492 	 * Allocate an FMRI representing this module.  We'll use this later
493 	 * if the module decides to publish any events (e.g. list.suspects).
494 	 */
495 	mp->mod_fmri = fmd_protocol_fmri_module(mp);
496 
497 	/*
498 	 * Any subscriptions specified in the conf file are now stored in the
499 	 * corresponding property.  Add all of these to the dispatch queue.
500 	 */
501 	(void) fmd_conf_getprop(mp->mod_conf, FMD_PROP_SUBSCRIPTIONS, &pap);
502 
503 	for (i = 0; i < pap->cpa_argc; i++) {
504 		fmd_dispq_insert(fmd.d_disp, mp->mod_queue, pap->cpa_argv[i]);
505 		fmd_xprt_subscribe_all(pap->cpa_argv[i]);
506 	}
507 
508 	/*
509 	 * Unlock the module and restore any pre-existing module checkpoint.
510 	 * If the checkpoint is missing or corrupt, we just keep going.
511 	 */
512 	fmd_module_unlock(mp);
513 	fmd_ckpt_restore(mp);
514 	return (0);
515 }
516 
517 /*
518  * If an auxiliary thread exists for the specified module at unregistration
519  * time, send it an asynchronous cancellation to force it to exit and then
520  * join with it (we expect this to either succeed quickly or return ESRCH).
521  * Once this is complete we can destroy the associated fmd_thread_t data.
522  */
523 static void
524 fmd_module_thrcancel(fmd_idspace_t *ids, id_t id, fmd_module_t *mp)
525 {
526 	fmd_thread_t *tp = fmd_idspace_getspecific(ids, id);
527 
528 	fmd_dprintf(FMD_DBG_MOD, "cancelling %s auxiliary thread %u\n",
529 	    mp->mod_name, tp->thr_tid);
530 
531 	ASSERT(tp->thr_tid == id);
532 	(void) pthread_cancel(tp->thr_tid);
533 	(void) pthread_join(tp->thr_tid, NULL);
534 
535 	fmd_thread_destroy(tp, FMD_THREAD_NOJOIN);
536 }
537 
538 void
539 fmd_module_unregister(fmd_module_t *mp)
540 {
541 	fmd_conf_formal_t *cfp = mp->mod_argv;
542 	const fmd_conf_path_t *pap;
543 	fmd_case_t *cp;
544 	fmd_xprt_t *xp;
545 	int i;
546 
547 	TRACE((FMD_DBG_MOD, "unregister %p (%s)", (void *)mp, mp->mod_name));
548 	ASSERT(fmd_module_locked(mp));
549 
550 	/*
551 	 * If any transports are still open, they have send threads that are
552 	 * using the module handle: shut them down and join with these threads.
553 	 */
554 	while ((xp = fmd_list_next(&mp->mod_transports)) != NULL)
555 		fmd_xprt_destroy(xp);
556 
557 	/*
558 	 * If any auxiliary threads exist, they may be using our module handle,
559 	 * and therefore could cause a fault as soon as we start destroying it.
560 	 * Module writers should clean up any threads before unregistering: we
561 	 * forcibly cancel any remaining auxiliary threads before proceeding.
562 	 */
563 	fmd_idspace_apply(mp->mod_threads,
564 	    (void (*)())fmd_module_thrcancel, mp);
565 
566 	if (mp->mod_error == 0)
567 		fmd_ckpt_save(mp); /* take one more checkpoint if needed */
568 
569 	/*
570 	 * Delete any cases associated with the module (UNSOLVED, SOLVED, or
571 	 * CLOSE_WAIT) as if fmdo_close() has finished processing them.
572 	 */
573 	while ((cp = fmd_list_next(&mp->mod_cases)) != NULL)
574 		fmd_case_delete(cp);
575 
576 	fmd_ustat_delete_references(mp->mod_ustat);
577 	(void) fmd_conf_getprop(mp->mod_conf, FMD_PROP_SUBSCRIPTIONS, &pap);
578 
579 	for (i = 0; i < pap->cpa_argc; i++) {
580 		fmd_xprt_unsubscribe_all(pap->cpa_argv[i]);
581 		fmd_dispq_delete(fmd.d_disp, mp->mod_queue, pap->cpa_argv[i]);
582 	}
583 
584 	fmd_conf_close(mp->mod_conf);
585 	mp->mod_conf = NULL;
586 
587 	for (i = 0; i < mp->mod_argc; i++, cfp++) {
588 		fmd_strfree((char *)cfp->cf_name);
589 		fmd_strfree((char *)cfp->cf_default);
590 	}
591 
592 	fmd_free(mp->mod_argv, sizeof (fmd_conf_formal_t) * mp->mod_argc);
593 	mp->mod_argv = NULL;
594 	mp->mod_argc = 0;
595 
596 	nvlist_free(mp->mod_fmri);
597 	mp->mod_fmri = NULL;
598 
599 	fmd_strfree((char *)mp->mod_info->fmdi_desc);
600 	fmd_strfree((char *)mp->mod_info->fmdi_vers);
601 	fmd_free((void *)mp->mod_info->fmdi_ops, sizeof (fmd_hdl_ops_t));
602 	fmd_free(mp->mod_info, sizeof (fmd_hdl_info_t));
603 	mp->mod_info = NULL;
604 
605 	fmd_eventq_abort(mp->mod_queue);
606 }
607 
608 void
609 fmd_hdl_unregister(fmd_hdl_t *hdl)
610 {
611 	fmd_module_t *mp = fmd_api_module_lock(hdl);
612 	fmd_module_unregister(mp);
613 	fmd_module_unlock(mp);
614 }
615 
616 void
617 fmd_hdl_subscribe(fmd_hdl_t *hdl, const char *class)
618 {
619 	fmd_module_t *mp = fmd_api_module_lock(hdl);
620 
621 	if (fmd_conf_setprop(mp->mod_conf,
622 	    FMD_PROP_SUBSCRIPTIONS, class) == 0) {
623 		fmd_dispq_insert(fmd.d_disp, mp->mod_queue, class);
624 		fmd_xprt_subscribe_all(class);
625 	}
626 
627 	fmd_module_unlock(mp);
628 }
629 
630 
631 void
632 fmd_hdl_unsubscribe(fmd_hdl_t *hdl, const char *class)
633 {
634 	fmd_module_t *mp = fmd_api_module_lock(hdl);
635 
636 	if (fmd_conf_delprop(mp->mod_conf,
637 	    FMD_PROP_SUBSCRIPTIONS, class) == 0) {
638 		fmd_xprt_unsubscribe_all(class);
639 		fmd_dispq_delete(fmd.d_disp, mp->mod_queue, class);
640 	}
641 
642 	fmd_module_unlock(mp);
643 	fmd_eventq_cancel(mp->mod_queue, FMD_EVT_PROTOCOL, (void *)class);
644 }
645 
646 void
647 fmd_hdl_setspecific(fmd_hdl_t *hdl, void *spec)
648 {
649 	fmd_module_t *mp = fmd_api_module_lock(hdl);
650 
651 	mp->mod_spec = spec;
652 	fmd_module_unlock(mp);
653 }
654 
655 void *
656 fmd_hdl_getspecific(fmd_hdl_t *hdl)
657 {
658 	fmd_module_t *mp = fmd_api_module_lock(hdl);
659 	void *spec = mp->mod_spec;
660 
661 	fmd_module_unlock(mp);
662 	return (spec);
663 }
664 
665 void
666 fmd_hdl_opendict(fmd_hdl_t *hdl, const char *dict)
667 {
668 	fmd_module_t *mp = fmd_api_module_lock(hdl);
669 	const fmd_conf_path_t *pap;
670 	int i;
671 
672 	/*
673 	 * Update the dictionary property in order to preserve the list of
674 	 * pathnames and expand any % tokens in the path.  Then retrieve the
675 	 * new dictionary names from cpa_argv[] and open them one at a time.
676 	 */
677 	(void) fmd_conf_setprop(mp->mod_conf, FMD_PROP_DICTIONARIES, dict);
678 	(void) fmd_conf_getprop(mp->mod_conf, FMD_PROP_DICTIONARIES, &pap);
679 
680 	ASSERT(pap->cpa_argc > mp->mod_dictc);
681 
682 	for (i = mp->mod_dictc; i < pap->cpa_argc; i++) {
683 		if (fmd_module_dc_opendict(mp, pap->cpa_argv[i]) != 0) {
684 			fmd_api_error(mp, EFMD_MOD_DICT,
685 			    "failed to open dictionary %s for module %s",
686 			    pap->cpa_argv[i], mp->mod_name);
687 		}
688 	}
689 
690 	fmd_module_unlock(mp);
691 }
692 
693 topo_hdl_t *
694 fmd_hdl_topology(fmd_hdl_t *hdl, int v)
695 {
696 	fmd_module_t *mp = fmd_api_module_lock(hdl);
697 	topo_hdl_t *thp;
698 
699 	if (v != TOPO_VERSION) {
700 		fmd_api_error(mp, EFMD_MOD_TOPO, "libtopo version mismatch: "
701 		    "fmd version %d != client version %d\n", TOPO_VERSION, v);
702 	}
703 
704 	thp = fmd.d_topo;
705 	fmd_module_unlock(mp);
706 	return (thp);
707 }
708 
709 void *
710 fmd_hdl_alloc(fmd_hdl_t *hdl, size_t size, int flags)
711 {
712 	fmd_module_t *mp = fmd_api_module_lock(hdl);
713 	void *data;
714 
715 	if (mp->mod_stats->ms_memlimit.fmds_value.ui64 -
716 	    mp->mod_stats->ms_memtotal.fmds_value.ui64 < size) {
717 		fmd_api_error(mp, EFMD_HDL_NOMEM, "%s's allocation of %lu "
718 		    "bytes exceeds module memory limit (%llu)\n",
719 		    mp->mod_name, (ulong_t)size, (u_longlong_t)
720 		    mp->mod_stats->ms_memtotal.fmds_value.ui64);
721 	}
722 
723 	if ((data = fmd_alloc(size, flags)) != NULL)
724 		mp->mod_stats->ms_memtotal.fmds_value.ui64 += size;
725 
726 	fmd_module_unlock(mp);
727 	return (data);
728 }
729 
730 void *
731 fmd_hdl_zalloc(fmd_hdl_t *hdl, size_t size, int flags)
732 {
733 	void *data = fmd_hdl_alloc(hdl, size, flags);
734 
735 	if (data != NULL)
736 		bzero(data, size);
737 
738 	return (data);
739 }
740 
741 void
742 fmd_hdl_free(fmd_hdl_t *hdl, void *data, size_t size)
743 {
744 	fmd_module_t *mp = fmd_api_module_lock(hdl);
745 
746 	fmd_free(data, size);
747 	mp->mod_stats->ms_memtotal.fmds_value.ui64 -= size;
748 
749 	fmd_module_unlock(mp);
750 }
751 
752 char *
753 fmd_hdl_strdup(fmd_hdl_t *hdl, const char *s, int flags)
754 {
755 	char *p;
756 
757 	if (s != NULL)
758 		p = fmd_hdl_alloc(hdl, strlen(s) + 1, flags);
759 	else
760 		p = NULL;
761 
762 	if (p != NULL)
763 		(void) strcpy(p, s);
764 
765 	return (p);
766 }
767 
768 void
769 fmd_hdl_strfree(fmd_hdl_t *hdl, char *s)
770 {
771 	if (s != NULL)
772 		fmd_hdl_free(hdl, s, strlen(s) + 1);
773 }
774 
775 void
776 fmd_hdl_vabort(fmd_hdl_t *hdl, const char *format, va_list ap)
777 {
778 	fmd_api_verror(fmd_api_module_lock(hdl), EFMD_HDL_ABORT, format, ap);
779 }
780 
781 /*PRINTFLIKE2*/
782 void
783 fmd_hdl_abort(fmd_hdl_t *hdl, const char *format, ...)
784 {
785 	fmd_module_t *mp = fmd_api_module_lock(hdl);
786 	va_list ap;
787 
788 	va_start(ap, format);
789 	fmd_api_verror(mp, EFMD_HDL_ABORT, format, ap);
790 	va_end(ap);
791 }
792 
793 void
794 fmd_hdl_verror(fmd_hdl_t *hdl, const char *format, va_list ap)
795 {
796 	fmd_module_t *mp = fmd_api_module_lock(hdl);
797 	fmd_api_vxerror(mp, errno, format, ap);
798 	fmd_module_unlock(mp);
799 }
800 
801 /*PRINTFLIKE2*/
802 void
803 fmd_hdl_error(fmd_hdl_t *hdl, const char *format, ...)
804 {
805 	va_list ap;
806 
807 	va_start(ap, format);
808 	fmd_hdl_verror(hdl, format, ap);
809 	va_end(ap);
810 }
811 
812 void
813 fmd_hdl_vdebug(fmd_hdl_t *hdl, const char *format, va_list ap)
814 {
815 	fmd_module_t *mp = fmd_api_module_lock(hdl);
816 
817 	char *msg;
818 	size_t len;
819 	char c;
820 
821 	if (!(fmd.d_hdl_debug)) {
822 		mp->mod_stats->ms_debugdrop.fmds_value.ui64++;
823 		fmd_module_unlock(mp);
824 		return;
825 	}
826 
827 	len = vsnprintf(&c, 1, format, ap);
828 
829 	if ((msg = fmd_alloc(len + 2, FMD_NOSLEEP)) == NULL) {
830 		mp->mod_stats->ms_debugdrop.fmds_value.ui64++;
831 		fmd_module_unlock(mp);
832 		return;
833 	}
834 
835 	(void) vsnprintf(msg, len + 1, format, ap);
836 
837 	if (msg[len - 1] != '\n')
838 		(void) strcpy(&msg[len], "\n");
839 
840 	if (fmd.d_hdl_dbout & FMD_DBOUT_STDERR) {
841 		(void) pthread_mutex_lock(&fmd.d_err_lock);
842 		(void) fprintf(stderr, "%s DEBUG: %s: %s",
843 		    fmd.d_pname, mp->mod_name, msg);
844 		(void) pthread_mutex_unlock(&fmd.d_err_lock);
845 	}
846 
847 	if (fmd.d_hdl_dbout & FMD_DBOUT_SYSLOG) {
848 		syslog(LOG_DEBUG | LOG_DAEMON, "%s DEBUG: %s: %s",
849 		    fmd.d_pname, mp->mod_name, msg);
850 	}
851 
852 	fmd_free(msg, len + 2);
853 	fmd_module_unlock(mp);
854 }
855 
856 /*PRINTFLIKE2*/
857 void
858 fmd_hdl_debug(fmd_hdl_t *hdl, const char *format, ...)
859 {
860 	va_list ap;
861 
862 	va_start(ap, format);
863 	fmd_hdl_vdebug(hdl, format, ap);
864 	va_end(ap);
865 }
866 
867 int32_t
868 fmd_prop_get_int32(fmd_hdl_t *hdl, const char *name)
869 {
870 	fmd_module_t *mp = fmd_api_module_lock(hdl);
871 	const fmd_conf_ops_t *ops = fmd_conf_gettype(mp->mod_conf, name);
872 	int32_t value = 0;
873 
874 	if (ops == &fmd_conf_bool || ops == &fmd_conf_int32 ||
875 	    ops == &fmd_conf_uint32)
876 		(void) fmd_conf_getprop(mp->mod_conf, name, &value);
877 	else if (ops != NULL) {
878 		fmd_api_error(mp, EFMD_PROP_TYPE,
879 		    "property %s is not of int32 type\n", name);
880 	} else {
881 		fmd_api_error(mp, EFMD_PROP_DEFN,
882 		    "property %s is not defined\n", name);
883 	}
884 
885 	fmd_module_unlock(mp);
886 	return (value);
887 }
888 
889 int64_t
890 fmd_prop_get_int64(fmd_hdl_t *hdl, const char *name)
891 {
892 	fmd_module_t *mp = fmd_api_module_lock(hdl);
893 	const fmd_conf_ops_t *ops = fmd_conf_gettype(mp->mod_conf, name);
894 	int64_t value = 0;
895 
896 	if (ops == &fmd_conf_int64 || ops == &fmd_conf_uint64 ||
897 	    ops == &fmd_conf_time || ops == &fmd_conf_size)
898 		(void) fmd_conf_getprop(mp->mod_conf, name, &value);
899 	else if (ops != NULL) {
900 		fmd_api_error(mp, EFMD_PROP_TYPE,
901 		    "property %s is not of int64 type\n", name);
902 	} else {
903 		fmd_api_error(mp, EFMD_PROP_DEFN,
904 		    "property %s is not defined\n", name);
905 	}
906 
907 	fmd_module_unlock(mp);
908 	return (value);
909 }
910 
911 char *
912 fmd_prop_get_string(fmd_hdl_t *hdl, const char *name)
913 {
914 	fmd_module_t *mp = fmd_api_module_lock(hdl);
915 	const fmd_conf_ops_t *ops = fmd_conf_gettype(mp->mod_conf, name);
916 	char *value = NULL;
917 	const char *s;
918 
919 	if (ops == &fmd_conf_string) {
920 		(void) fmd_conf_getprop(mp->mod_conf, name, &s);
921 		value = fmd_strdup(s, FMD_SLEEP);
922 	} else if (ops != NULL) {
923 		fmd_api_error(mp, EFMD_PROP_TYPE,
924 		    "property %s is not of string type\n", name);
925 	} else {
926 		fmd_api_error(mp, EFMD_PROP_DEFN,
927 		    "property %s is not defined\n", name);
928 	}
929 
930 	fmd_module_unlock(mp);
931 	return (value);
932 }
933 
934 void
935 fmd_prop_free_string(fmd_hdl_t *hdl, char *s)
936 {
937 	fmd_module_t *mp = fmd_api_module_lock(hdl);
938 	fmd_strfree(s);
939 	fmd_module_unlock(mp);
940 }
941 
942 fmd_stat_t *
943 fmd_stat_create(fmd_hdl_t *hdl, uint_t flags, uint_t argc, fmd_stat_t *argv)
944 {
945 	fmd_module_t *mp = fmd_api_module_lock(hdl);
946 	fmd_stat_t *ep, *sp;
947 
948 	if (flags & ~FMD_STAT_ALLOC) {
949 		fmd_api_error(mp, EFMD_STAT_FLAGS,
950 		    "invalid flags 0x%x passed to fmd_stat_create\n", flags);
951 	}
952 
953 	if ((sp = fmd_ustat_insert(mp->mod_ustat,
954 	    flags | FMD_USTAT_VALIDATE, argc, argv, &ep)) == NULL) {
955 		fmd_api_error(mp, errno,
956 		    "failed to publish stat '%s'", ep->fmds_name);
957 	}
958 
959 	fmd_module_unlock(mp);
960 	return (sp);
961 }
962 
963 void
964 fmd_stat_destroy(fmd_hdl_t *hdl, uint_t argc, fmd_stat_t *argv)
965 {
966 	fmd_module_t *mp = fmd_api_module_lock(hdl);
967 	fmd_ustat_delete(mp->mod_ustat, argc, argv);
968 	fmd_module_unlock(mp);
969 }
970 
971 void
972 fmd_stat_setstr(fmd_hdl_t *hdl, fmd_stat_t *sp, const char *s)
973 {
974 	char *str = fmd_strdup(s, FMD_SLEEP);
975 	fmd_module_t *mp = fmd_api_module_lock(hdl);
976 
977 	if (sp->fmds_type != FMD_TYPE_STRING) {
978 		fmd_strfree(str);
979 		fmd_api_error(mp, EFMD_STAT_TYPE,
980 		    "stat '%s' is not a string\n", sp->fmds_name);
981 	}
982 
983 	fmd_strfree(sp->fmds_value.str);
984 	sp->fmds_value.str = str;
985 
986 	fmd_module_unlock(mp);
987 }
988 
989 fmd_case_t *
990 fmd_case_open(fmd_hdl_t *hdl, void *data)
991 {
992 	fmd_module_t *mp = fmd_api_module_lock(hdl);
993 	fmd_case_t *cp = fmd_case_create(mp, data);
994 	fmd_module_unlock(mp);
995 	return (cp);
996 }
997 
998 void
999 fmd_case_reset(fmd_hdl_t *hdl, fmd_case_t *cp)
1000 {
1001 	fmd_module_t *mp = fmd_api_module_lock(hdl);
1002 	fmd_case_impl_t *cip = fmd_api_case_impl(mp, cp);
1003 
1004 	if (cip->ci_state >= FMD_CASE_SOLVED) {
1005 		fmd_api_error(mp, EFMD_CASE_STATE, "cannot solve %s: "
1006 		    "case is already solved or closed\n", cip->ci_uuid);
1007 	}
1008 
1009 	fmd_case_reset_suspects(cp);
1010 	fmd_module_unlock(mp);
1011 }
1012 
1013 void
1014 fmd_case_solve(fmd_hdl_t *hdl, fmd_case_t *cp)
1015 {
1016 	fmd_module_t *mp = fmd_api_module_lock(hdl);
1017 	fmd_case_impl_t *cip = fmd_api_case_impl(mp, cp);
1018 
1019 	if (cip->ci_state >= FMD_CASE_SOLVED) {
1020 		fmd_api_error(mp, EFMD_CASE_STATE, "cannot solve %s: "
1021 		    "case is already solved or closed\n", cip->ci_uuid);
1022 	}
1023 
1024 	fmd_case_transition(cp, FMD_CASE_SOLVED, FMD_CF_SOLVED);
1025 	fmd_module_unlock(mp);
1026 }
1027 
1028 void
1029 fmd_case_close(fmd_hdl_t *hdl, fmd_case_t *cp)
1030 {
1031 	fmd_module_t *mp = fmd_api_module_lock(hdl);
1032 
1033 	(void) fmd_api_case_impl(mp, cp); /* validate 'cp' */
1034 	fmd_case_transition(cp, FMD_CASE_CLOSE_WAIT, FMD_CF_ISOLATED);
1035 
1036 	fmd_module_unlock(mp);
1037 }
1038 
1039 const char *
1040 fmd_case_uuid(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 	const char *uuid = cip->ci_uuid;
1045 
1046 	fmd_module_unlock(mp);
1047 	return (uuid);
1048 }
1049 
1050 fmd_case_t *
1051 fmd_case_uulookup(fmd_hdl_t *hdl, const char *uuid)
1052 {
1053 	fmd_module_t *cmp, *mp = fmd_api_module_lock(hdl);
1054 	fmd_case_t *cp = fmd_case_hash_lookup(fmd.d_cases, uuid);
1055 
1056 	if (cp != NULL) {
1057 		cmp = ((fmd_case_impl_t *)cp)->ci_mod;
1058 		fmd_case_rele(cp);
1059 	} else
1060 		cmp = NULL;
1061 
1062 	fmd_module_unlock(mp);
1063 	return (cmp == mp ? cp : NULL);
1064 }
1065 
1066 void
1067 fmd_case_uuclose(fmd_hdl_t *hdl, const char *uuid)
1068 {
1069 	fmd_module_t *mp = fmd_api_module_lock(hdl);
1070 	fmd_case_t *cp = fmd_case_hash_lookup(fmd.d_cases, uuid);
1071 
1072 	if (cp != NULL) {
1073 		fmd_case_transition(cp, FMD_CASE_CLOSE_WAIT, FMD_CF_ISOLATED);
1074 		fmd_case_rele(cp);
1075 	}
1076 
1077 	fmd_module_unlock(mp);
1078 }
1079 
1080 int
1081 fmd_case_uuclosed(fmd_hdl_t *hdl, const char *uuid)
1082 {
1083 	fmd_module_t *mp = fmd_api_module_lock(hdl);
1084 	fmd_case_t *cp = fmd_case_hash_lookup(fmd.d_cases, uuid);
1085 	fmd_case_impl_t *cip = (fmd_case_impl_t *)cp;
1086 	int rv = FMD_B_TRUE;
1087 
1088 	if (cip != NULL) {
1089 		rv = cip->ci_state >= FMD_CASE_CLOSE_WAIT;
1090 		fmd_case_rele(cp);
1091 	}
1092 
1093 	fmd_module_unlock(mp);
1094 	return (rv);
1095 }
1096 
1097 static int
1098 fmd_case_instate(fmd_hdl_t *hdl, fmd_case_t *cp, uint_t state)
1099 {
1100 	fmd_module_t *mp = fmd_api_module_lock(hdl);
1101 	fmd_case_impl_t *cip = fmd_api_case_impl(mp, cp);
1102 	int rv = cip->ci_state >= state;
1103 
1104 	fmd_module_unlock(mp);
1105 	return (rv);
1106 }
1107 
1108 int
1109 fmd_case_solved(fmd_hdl_t *hdl, fmd_case_t *cp)
1110 {
1111 	return (fmd_case_instate(hdl, cp, FMD_CASE_SOLVED));
1112 }
1113 
1114 int
1115 fmd_case_closed(fmd_hdl_t *hdl, fmd_case_t *cp)
1116 {
1117 	return (fmd_case_instate(hdl, cp, FMD_CASE_CLOSE_WAIT));
1118 }
1119 
1120 void
1121 fmd_case_add_ereport(fmd_hdl_t *hdl, fmd_case_t *cp, fmd_event_t *ep)
1122 {
1123 	fmd_module_t *mp = fmd_api_module_lock(hdl);
1124 
1125 	(void) fmd_api_case_impl(mp, cp); /* validate 'cp' */
1126 
1127 	if (fmd_case_insert_event(cp, ep))
1128 		mp->mod_stats->ms_accepted.fmds_value.ui64++;
1129 
1130 	fmd_module_unlock(mp);
1131 }
1132 
1133 void
1134 fmd_case_add_serd(fmd_hdl_t *hdl, fmd_case_t *cp, const char *name)
1135 {
1136 	fmd_module_t *mp = fmd_api_module_lock(hdl);
1137 	fmd_serd_elem_t *sep;
1138 	fmd_serd_eng_t *sgp;
1139 
1140 	if ((sgp = fmd_serd_eng_lookup(&mp->mod_serds, name)) == NULL) {
1141 		fmd_api_error(mp, EFMD_SERD_NAME,
1142 		    "failed to add events from serd engine '%s'", name);
1143 	}
1144 
1145 	(void) fmd_api_case_impl(mp, cp); /* validate 'cp' */
1146 
1147 	for (sep = fmd_list_next(&sgp->sg_list);
1148 	    sep != NULL; sep = fmd_list_next(sep)) {
1149 		if (fmd_case_insert_event(cp, sep->se_event))
1150 			mp->mod_stats->ms_accepted.fmds_value.ui64++;
1151 	}
1152 
1153 	fmd_module_unlock(mp);
1154 }
1155 
1156 void
1157 fmd_case_add_suspect(fmd_hdl_t *hdl, fmd_case_t *cp, nvlist_t *nvl)
1158 {
1159 	fmd_module_t *mp = fmd_api_module_lock(hdl);
1160 	fmd_case_impl_t *cip = fmd_api_case_impl(mp, cp);
1161 	char *class;
1162 
1163 	if (cip->ci_state >= FMD_CASE_SOLVED) {
1164 		fmd_api_error(mp, EFMD_CASE_STATE, "cannot add suspect to "
1165 		    "%s: case is already solved or closed\n", cip->ci_uuid);
1166 	}
1167 
1168 	if (nvlist_lookup_string(nvl, FM_CLASS, &class) != 0 ||
1169 	    class == NULL || *class == '\0') {
1170 		fmd_api_error(mp, EFMD_CASE_EVENT, "cannot add suspect to "
1171 		    "%s: suspect event is missing a class\n", cip->ci_uuid);
1172 	}
1173 
1174 	fmd_case_insert_suspect(cp, nvl);
1175 	fmd_module_unlock(mp);
1176 }
1177 
1178 void
1179 fmd_case_setspecific(fmd_hdl_t *hdl, fmd_case_t *cp, void *data)
1180 {
1181 	fmd_module_t *mp = fmd_api_module_lock(hdl);
1182 	fmd_case_impl_t *cip = fmd_api_case_impl(mp, cp);
1183 
1184 	(void) pthread_mutex_lock(&cip->ci_lock);
1185 	cip->ci_data = data;
1186 	(void) pthread_mutex_unlock(&cip->ci_lock);
1187 
1188 	fmd_module_unlock(mp);
1189 }
1190 
1191 void *
1192 fmd_case_getspecific(fmd_hdl_t *hdl, fmd_case_t *cp)
1193 {
1194 	fmd_module_t *mp = fmd_api_module_lock(hdl);
1195 	fmd_case_impl_t *cip = fmd_api_case_impl(mp, cp);
1196 	void *data;
1197 
1198 	(void) pthread_mutex_lock(&cip->ci_lock);
1199 	data = cip->ci_data;
1200 	(void) pthread_mutex_unlock(&cip->ci_lock);
1201 
1202 	fmd_module_unlock(mp);
1203 	return (data);
1204 }
1205 
1206 void
1207 fmd_case_setprincipal(fmd_hdl_t *hdl, fmd_case_t *cp, fmd_event_t *ep)
1208 {
1209 	fmd_module_t *mp = fmd_api_module_lock(hdl);
1210 
1211 	(void) fmd_api_case_impl(mp, cp); /* validate 'cp' */
1212 
1213 	if (fmd_case_insert_principal(cp, ep))
1214 		mp->mod_stats->ms_accepted.fmds_value.ui64++;
1215 
1216 	fmd_module_unlock(mp);
1217 }
1218 
1219 fmd_event_t *
1220 fmd_case_getprincipal(fmd_hdl_t *hdl, fmd_case_t *cp)
1221 {
1222 	fmd_module_t *mp = fmd_api_module_lock(hdl);
1223 	fmd_case_impl_t *cip = fmd_api_case_impl(mp, cp);
1224 	fmd_event_t *ep;
1225 
1226 	(void) pthread_mutex_lock(&cip->ci_lock);
1227 	ep = cip->ci_principal;
1228 	(void) pthread_mutex_unlock(&cip->ci_lock);
1229 
1230 	fmd_module_unlock(mp);
1231 	return (ep);
1232 }
1233 
1234 fmd_case_t *
1235 fmd_case_next(fmd_hdl_t *hdl, fmd_case_t *cp)
1236 {
1237 	fmd_module_t *mp = fmd_api_module_lock(hdl);
1238 
1239 	if (cp != NULL)
1240 		cp = fmd_list_next(fmd_api_case_impl(mp, cp));
1241 	else
1242 		cp = fmd_list_next(&mp->mod_cases);
1243 
1244 	fmd_module_unlock(mp);
1245 	return (cp);
1246 }
1247 
1248 fmd_case_t *
1249 fmd_case_prev(fmd_hdl_t *hdl, fmd_case_t *cp)
1250 {
1251 	fmd_module_t *mp = fmd_api_module_lock(hdl);
1252 
1253 	if (cp != NULL)
1254 		cp = fmd_list_prev(fmd_api_case_impl(mp, cp));
1255 	else
1256 		cp = fmd_list_prev(&mp->mod_cases);
1257 
1258 	fmd_module_unlock(mp);
1259 	return (cp);
1260 }
1261 
1262 /*
1263  * Utility function for fmd_buf_* routines.  If a case is specified, use the
1264  * case's ci_bufs hash; otherwise use the module's global mod_bufs hash.
1265  */
1266 static fmd_buf_hash_t *
1267 fmd_buf_gethash(fmd_module_t *mp, fmd_case_t *cp)
1268 {
1269 	return (cp ? &fmd_api_case_impl(mp, cp)->ci_bufs : &mp->mod_bufs);
1270 }
1271 
1272 void
1273 fmd_buf_create(fmd_hdl_t *hdl, fmd_case_t *cp, const char *name, size_t size)
1274 {
1275 	fmd_module_t *mp = fmd_api_module_lock(hdl);
1276 	fmd_buf_hash_t *bhp = fmd_buf_gethash(mp, cp);
1277 	fmd_buf_t *bp = fmd_buf_lookup(bhp, name);
1278 
1279 	if (bp == NULL) {
1280 		if (fmd_strbadid(name, FMD_B_TRUE) != NULL || size == 0) {
1281 			fmd_api_error(mp, EFMD_BUF_INVAL, "cannot create '%s' "
1282 			    "(size %lu): %s\n", name, (ulong_t)size,
1283 			    fmd_strerror(EFMD_BUF_INVAL));
1284 		}
1285 
1286 		if (mp->mod_stats->ms_buflimit.fmds_value.ui64 -
1287 		    mp->mod_stats->ms_buftotal.fmds_value.ui64 < size) {
1288 			fmd_api_error(mp, EFMD_BUF_LIMIT, "cannot create '%s': "
1289 			    "buf limit exceeded (%llu)\n", name, (u_longlong_t)
1290 			    mp->mod_stats->ms_buflimit.fmds_value.ui64);
1291 		}
1292 
1293 		mp->mod_stats->ms_buftotal.fmds_value.ui64 += size;
1294 		bp = fmd_buf_insert(bhp, name, size);
1295 
1296 	} else {
1297 		fmd_api_error(mp, EFMD_BUF_EXISTS,
1298 		    "cannot create '%s': buffer already exists\n", name);
1299 	}
1300 
1301 	if (cp != NULL)
1302 		fmd_case_setdirty(cp);
1303 	else
1304 		fmd_module_setdirty(mp);
1305 
1306 	fmd_module_unlock(mp);
1307 }
1308 
1309 void
1310 fmd_buf_destroy(fmd_hdl_t *hdl, fmd_case_t *cp, const char *name)
1311 {
1312 	fmd_module_t *mp = fmd_api_module_lock(hdl);
1313 	fmd_buf_hash_t *bhp = fmd_buf_gethash(mp, cp);
1314 	fmd_buf_t *bp = fmd_buf_lookup(bhp, name);
1315 
1316 	if (bp != NULL) {
1317 		mp->mod_stats->ms_buftotal.fmds_value.ui64 -= bp->buf_size;
1318 		fmd_buf_delete(bhp, name);
1319 
1320 		if (cp != NULL)
1321 			fmd_case_setdirty(cp);
1322 		else
1323 			fmd_module_setdirty(mp);
1324 	}
1325 
1326 	fmd_module_unlock(mp);
1327 }
1328 
1329 void
1330 fmd_buf_read(fmd_hdl_t *hdl, fmd_case_t *cp,
1331     const char *name, void *buf, size_t size)
1332 {
1333 	fmd_module_t *mp = fmd_api_module_lock(hdl);
1334 	fmd_buf_t *bp = fmd_buf_lookup(fmd_buf_gethash(mp, cp), name);
1335 
1336 	if (bp == NULL) {
1337 		fmd_api_error(mp, EFMD_BUF_NOENT, "no buf named '%s' is "
1338 		    "associated with %s\n", name, cp ? "case" : "module");
1339 	}
1340 
1341 	bcopy(bp->buf_data, buf, MIN(bp->buf_size, size));
1342 	if (size > bp->buf_size)
1343 		bzero((char *)buf + bp->buf_size, size - bp->buf_size);
1344 
1345 	fmd_module_unlock(mp);
1346 }
1347 
1348 void
1349 fmd_buf_write(fmd_hdl_t *hdl, fmd_case_t *cp,
1350     const char *name, const void *buf, size_t size)
1351 {
1352 	fmd_module_t *mp = fmd_api_module_lock(hdl);
1353 	fmd_buf_hash_t *bhp = fmd_buf_gethash(mp, cp);
1354 	fmd_buf_t *bp = fmd_buf_lookup(bhp, name);
1355 
1356 	if (bp == NULL) {
1357 		if (fmd_strbadid(name, FMD_B_TRUE) != NULL || size == 0) {
1358 			fmd_api_error(mp, EFMD_BUF_INVAL, "cannot write '%s' "
1359 			    "(size %lu): %s\n", name, (ulong_t)size,
1360 			    fmd_strerror(EFMD_BUF_INVAL));
1361 		}
1362 
1363 		if (mp->mod_stats->ms_buflimit.fmds_value.ui64 -
1364 		    mp->mod_stats->ms_buftotal.fmds_value.ui64 < size) {
1365 			fmd_api_error(mp, EFMD_BUF_LIMIT, "cannot write '%s': "
1366 			    "buf limit exceeded (%llu)\n", name, (u_longlong_t)
1367 			    mp->mod_stats->ms_buflimit.fmds_value.ui64);
1368 		}
1369 
1370 		mp->mod_stats->ms_buftotal.fmds_value.ui64 += size;
1371 		bp = fmd_buf_insert(bhp, name, size);
1372 
1373 	} else if (size > bp->buf_size) {
1374 		fmd_api_error(mp, EFMD_BUF_OFLOW,
1375 		    "write to buf '%s' overflows buf size (%lu > %lu)\n",
1376 		    name, (ulong_t)size, (ulong_t)bp->buf_size);
1377 	}
1378 
1379 	bcopy(buf, bp->buf_data, MIN(bp->buf_size, size));
1380 	bp->buf_flags |= FMD_BUF_DIRTY;
1381 
1382 	if (cp != NULL)
1383 		fmd_case_setdirty(cp);
1384 	else
1385 		fmd_module_setdirty(mp);
1386 
1387 	fmd_module_unlock(mp);
1388 }
1389 
1390 size_t
1391 fmd_buf_size(fmd_hdl_t *hdl, fmd_case_t *cp, const char *name)
1392 {
1393 	fmd_module_t *mp = fmd_api_module_lock(hdl);
1394 	fmd_buf_hash_t *bhp = fmd_buf_gethash(mp, cp);
1395 
1396 	fmd_buf_t *bp;
1397 	size_t size;
1398 
1399 	if ((bp = fmd_buf_lookup(bhp, name)) != NULL)
1400 		size = bp->buf_size;
1401 	else
1402 		size = 0;
1403 
1404 	fmd_module_unlock(mp);
1405 	return (size);
1406 }
1407 
1408 void
1409 fmd_serd_create(fmd_hdl_t *hdl, const char *name, uint_t n, hrtime_t t)
1410 {
1411 	fmd_module_t *mp = fmd_api_module_lock(hdl);
1412 
1413 	if (fmd_serd_eng_lookup(&mp->mod_serds, name) != NULL) {
1414 		fmd_api_error(mp, EFMD_SERD_EXISTS,
1415 		    "failed to create serd engine '%s': %s\n",
1416 		    name, fmd_strerror(EFMD_SERD_EXISTS));
1417 	}
1418 
1419 	(void) fmd_serd_eng_insert(&mp->mod_serds, name, n, t);
1420 	fmd_module_setdirty(mp);
1421 	fmd_module_unlock(mp);
1422 }
1423 
1424 void
1425 fmd_serd_destroy(fmd_hdl_t *hdl, const char *name)
1426 {
1427 	fmd_module_t *mp = fmd_api_module_lock(hdl);
1428 
1429 	fmd_serd_eng_delete(&mp->mod_serds, name);
1430 	fmd_module_setdirty(mp);
1431 	fmd_module_unlock(mp);
1432 }
1433 
1434 int
1435 fmd_serd_exists(fmd_hdl_t *hdl, const char *name)
1436 {
1437 	fmd_module_t *mp = fmd_api_module_lock(hdl);
1438 	int rv = (fmd_serd_eng_lookup(&mp->mod_serds, name) != NULL);
1439 	fmd_module_unlock(mp);
1440 
1441 	return (rv);
1442 }
1443 
1444 void
1445 fmd_serd_reset(fmd_hdl_t *hdl, const char *name)
1446 {
1447 	fmd_module_t *mp = fmd_api_module_lock(hdl);
1448 	fmd_serd_eng_t *sgp;
1449 
1450 	if ((sgp = fmd_serd_eng_lookup(&mp->mod_serds, name)) == NULL) {
1451 		fmd_api_error(mp, EFMD_SERD_NAME,
1452 		    "serd engine '%s' does not exist\n", name);
1453 	}
1454 
1455 	fmd_serd_eng_reset(sgp);
1456 	fmd_module_setdirty(mp);
1457 	fmd_module_unlock(mp);
1458 }
1459 
1460 int
1461 fmd_serd_record(fmd_hdl_t *hdl, const char *name, fmd_event_t *ep)
1462 {
1463 	fmd_module_t *mp = fmd_api_module_lock(hdl);
1464 	fmd_serd_eng_t *sgp;
1465 	int err;
1466 
1467 	if ((sgp = fmd_serd_eng_lookup(&mp->mod_serds, name)) == NULL) {
1468 		fmd_api_error(mp, EFMD_SERD_NAME,
1469 		    "failed to add record to serd engine '%s'", name);
1470 	}
1471 
1472 	err = fmd_serd_eng_record(sgp, ep);
1473 
1474 	if (sgp->sg_flags & FMD_SERD_DIRTY)
1475 		fmd_module_setdirty(mp);
1476 
1477 	fmd_module_unlock(mp);
1478 	return (err);
1479 }
1480 
1481 int
1482 fmd_serd_fired(fmd_hdl_t *hdl, const char *name)
1483 {
1484 	fmd_module_t *mp = fmd_api_module_lock(hdl);
1485 	fmd_serd_eng_t *sgp;
1486 	int err;
1487 
1488 	if ((sgp = fmd_serd_eng_lookup(&mp->mod_serds, name)) == NULL) {
1489 		fmd_api_error(mp, EFMD_SERD_NAME,
1490 		    "serd engine '%s' does not exist\n", name);
1491 	}
1492 
1493 	err = fmd_serd_eng_fired(sgp);
1494 	fmd_module_unlock(mp);
1495 	return (err);
1496 }
1497 
1498 int
1499 fmd_serd_empty(fmd_hdl_t *hdl, const char *name)
1500 {
1501 	fmd_module_t *mp = fmd_api_module_lock(hdl);
1502 	fmd_serd_eng_t *sgp;
1503 	int empty;
1504 
1505 	if ((sgp = fmd_serd_eng_lookup(&mp->mod_serds, name)) == NULL) {
1506 		fmd_api_error(mp, EFMD_SERD_NAME,
1507 		    "serd engine '%s' does not exist\n", name);
1508 	}
1509 
1510 	empty = fmd_serd_eng_empty(sgp);
1511 	fmd_module_unlock(mp);
1512 	return (empty);
1513 }
1514 
1515 pthread_t
1516 fmd_thr_create(fmd_hdl_t *hdl, void (*func)(void *), void *arg)
1517 {
1518 	fmd_module_t *mp = fmd_api_module_lock(hdl);
1519 	fmd_thread_t *tp;
1520 	pthread_t tid;
1521 
1522 	if (mp->mod_stats->ms_thrtotal.fmds_value.ui32 >=
1523 	    mp->mod_stats->ms_thrlimit.fmds_value.ui32) {
1524 		fmd_api_error(mp, EFMD_THR_LIMIT, "%s request to create an "
1525 		    "auxiliary thread exceeds module thread limit (%u)\n",
1526 		    mp->mod_name, mp->mod_stats->ms_thrlimit.fmds_value.ui32);
1527 	}
1528 
1529 	if ((tp = fmd_thread_create(mp, func, arg)) == NULL) {
1530 		fmd_api_error(mp, EFMD_THR_CREATE,
1531 		    "failed to create auxiliary thread");
1532 	}
1533 
1534 	tid = tp->thr_tid;
1535 	mp->mod_stats->ms_thrtotal.fmds_value.ui32++;
1536 	(void) fmd_idspace_xalloc(mp->mod_threads, tid, tp);
1537 
1538 	fmd_module_unlock(mp);
1539 	return (tid);
1540 }
1541 
1542 void
1543 fmd_thr_destroy(fmd_hdl_t *hdl, pthread_t tid)
1544 {
1545 	fmd_module_t *mp = fmd_api_module_lock(hdl);
1546 	fmd_thread_t *tp;
1547 	int err;
1548 
1549 	if (pthread_self() == tid) {
1550 		fmd_api_error(mp, EFMD_THR_INVAL, "auxiliary thread tried to "
1551 		    "destroy itself (tid %u)\n", tid);
1552 	}
1553 
1554 	if ((tp = fmd_idspace_getspecific(mp->mod_threads, tid)) == NULL) {
1555 		fmd_api_error(mp, EFMD_THR_INVAL, "auxiliary thread tried to "
1556 		    "destroy an invalid thread (tid %u)\n", tid);
1557 	}
1558 
1559 	/*
1560 	 * Wait for the specified thread to exit and then join with it.  Since
1561 	 * the thread may need to make API calls in order to complete its work
1562 	 * we must sleep with the module lock unheld, and then reacquire it.
1563 	 */
1564 	fmd_module_unlock(mp);
1565 	err = pthread_join(tid, NULL);
1566 	mp = fmd_api_module_lock(hdl);
1567 
1568 	/*
1569 	 * Since pthread_join() was called without the module lock held, if
1570 	 * multiple callers attempted to destroy the same auxiliary thread
1571 	 * simultaneously, one will succeed and the others will get ESRCH.
1572 	 * Therefore we silently ignore ESRCH but only allow the caller who
1573 	 * succeessfully joined with the auxiliary thread to destroy it.
1574 	 */
1575 	if (err != 0 && err != ESRCH) {
1576 		fmd_api_error(mp, EFMD_THR_JOIN,
1577 		    "failed to join with auxiliary thread %u\n", tid);
1578 	}
1579 
1580 	if (err == 0) {
1581 		fmd_thread_destroy(tp, FMD_THREAD_NOJOIN);
1582 		mp->mod_stats->ms_thrtotal.fmds_value.ui32--;
1583 		(void) fmd_idspace_free(mp->mod_threads, tid);
1584 	}
1585 
1586 	fmd_module_unlock(mp);
1587 }
1588 
1589 void
1590 fmd_thr_signal(fmd_hdl_t *hdl, pthread_t tid)
1591 {
1592 	fmd_module_t *mp = fmd_api_module_lock(hdl);
1593 
1594 	if (tid != mp->mod_thread->thr_tid &&
1595 	    fmd_idspace_getspecific(mp->mod_threads, tid) == NULL) {
1596 		fmd_api_error(mp, EFMD_THR_INVAL, "tid %u is not a valid "
1597 		    "thread id for module %s\n", tid, mp->mod_name);
1598 	}
1599 
1600 	(void) pthread_kill(tid, fmd.d_thr_sig);
1601 	fmd_module_unlock(mp);
1602 }
1603 
1604 id_t
1605 fmd_timer_install(fmd_hdl_t *hdl, void *arg, fmd_event_t *ep, hrtime_t delta)
1606 {
1607 	fmd_module_t *mp = fmd_api_module_lock(hdl);
1608 	fmd_modtimer_t *t;
1609 	id_t id;
1610 
1611 	if (delta < 0) {
1612 		fmd_api_error(mp, EFMD_TIMER_INVAL,
1613 		    "timer delta %lld is not a valid interval\n", delta);
1614 	}
1615 
1616 	t = fmd_alloc(sizeof (fmd_modtimer_t), FMD_SLEEP);
1617 	t->mt_mod = mp;
1618 	t->mt_arg = arg;
1619 	t->mt_id = -1;
1620 
1621 	if ((id = fmd_timerq_install(fmd.d_timers, mp->mod_timerids,
1622 	    (fmd_timer_f *)fmd_module_timeout, t, ep, delta)) == -1) {
1623 		fmd_free(t, sizeof (fmd_modtimer_t));
1624 		fmd_api_error(mp, EFMD_TIMER_LIMIT,
1625 		    "failed to install timer +%lld", delta);
1626 	}
1627 
1628 	fmd_module_unlock(mp);
1629 	return (id);
1630 }
1631 
1632 void
1633 fmd_timer_remove(fmd_hdl_t *hdl, id_t id)
1634 {
1635 	fmd_module_t *mp = fmd_api_module_lock(hdl);
1636 	fmd_modtimer_t *t;
1637 
1638 	if (!fmd_idspace_valid(mp->mod_timerids, id)) {
1639 		fmd_api_error(mp, EFMD_TIMER_INVAL,
1640 		    "id %ld is not a valid timer id\n", id);
1641 	}
1642 
1643 	t = fmd_timerq_remove(fmd.d_timers, mp->mod_timerids, id);
1644 	fmd_module_unlock(mp);
1645 
1646 	if (t != NULL) {
1647 		fmd_eventq_cancel(mp->mod_queue, FMD_EVT_TIMEOUT, t);
1648 		fmd_free(t, sizeof (fmd_modtimer_t));
1649 	}
1650 }
1651 
1652 nvlist_t *
1653 fmd_nvl_create_fault(fmd_hdl_t *hdl, const char *class,
1654     uint8_t certainty, nvlist_t *asru, nvlist_t *fru, nvlist_t *rsrc)
1655 {
1656 	fmd_module_t *mp = fmd_api_module_lock(hdl);
1657 	nvlist_t *nvl;
1658 
1659 	if (class == NULL || class[0] == '\0')
1660 		fmd_api_error(mp, EFMD_NVL_INVAL, "invalid fault class\n");
1661 
1662 	nvl = fmd_protocol_fault(class, certainty, asru, fru, rsrc);
1663 	fmd_module_unlock(mp);
1664 	return (nvl);
1665 }
1666 
1667 int
1668 fmd_nvl_class_match(fmd_hdl_t *hdl, nvlist_t *nvl, const char *pattern)
1669 {
1670 	fmd_module_t *mp = fmd_api_module_lock(hdl);
1671 	char *class;
1672 	int rv;
1673 
1674 	rv = (nvl != NULL && nvlist_lookup_string(nvl,
1675 	    FM_CLASS, &class) == 0 && fmd_strmatch(class, pattern));
1676 
1677 	fmd_module_unlock(mp);
1678 	return (rv);
1679 }
1680 
1681 int
1682 fmd_nvl_fmri_expand(fmd_hdl_t *hdl, nvlist_t *nvl)
1683 {
1684 	fmd_module_t *mp = fmd_api_module_lock(hdl);
1685 	int rv;
1686 
1687 	if (nvl == NULL) {
1688 		fmd_api_error(mp, EFMD_NVL_INVAL,
1689 		    "invalid nvlist %p\n", (void *)nvl);
1690 	}
1691 
1692 	rv = fmd_fmri_expand(nvl);
1693 	fmd_module_unlock(mp);
1694 	return (rv);
1695 }
1696 
1697 int
1698 fmd_nvl_fmri_present(fmd_hdl_t *hdl, nvlist_t *nvl)
1699 {
1700 	fmd_module_t *mp = fmd_api_module_lock(hdl);
1701 	int rv;
1702 
1703 	if (nvl == NULL) {
1704 		fmd_api_error(mp, EFMD_NVL_INVAL,
1705 		    "invalid nvlist %p\n", (void *)nvl);
1706 	}
1707 
1708 	rv = fmd_fmri_present(nvl);
1709 	fmd_module_unlock(mp);
1710 
1711 	if (rv < 0) {
1712 		fmd_api_error(mp, EFMD_FMRI_OP, "invalid fmri for "
1713 		    "fmd_nvl_fmri_present\n");
1714 	}
1715 
1716 	return (rv);
1717 }
1718 
1719 int
1720 fmd_nvl_fmri_unusable(fmd_hdl_t *hdl, nvlist_t *nvl)
1721 {
1722 	fmd_module_t *mp = fmd_api_module_lock(hdl);
1723 	int rv;
1724 
1725 	if (nvl == NULL) {
1726 		fmd_api_error(mp, EFMD_NVL_INVAL,
1727 		    "invalid nvlist %p\n", (void *)nvl);
1728 	}
1729 
1730 	rv = fmd_fmri_unusable(nvl);
1731 	fmd_module_unlock(mp);
1732 
1733 	if (rv < 0) {
1734 		fmd_api_error(mp, EFMD_FMRI_OP, "invalid fmri for "
1735 		    "fmd_nvl_fmri_unusable\n");
1736 	}
1737 
1738 	return (rv);
1739 }
1740 
1741 int
1742 fmd_nvl_fmri_faulty(fmd_hdl_t *hdl, nvlist_t *nvl)
1743 {
1744 	fmd_module_t *mp = fmd_api_module_lock(hdl);
1745 	fmd_asru_hash_t *ahp = fmd.d_asrus;
1746 	fmd_asru_t *ap;
1747 	int rv = 0;
1748 
1749 	if (nvl == NULL) {
1750 		fmd_api_error(mp, EFMD_NVL_INVAL,
1751 		    "invalid nvlist %p\n", (void *)nvl);
1752 	}
1753 
1754 	if ((ap = fmd_asru_hash_lookup_nvl(ahp, nvl, FMD_B_FALSE)) != NULL) {
1755 		rv = (ap->asru_flags & FMD_ASRU_FAULTY) != 0;
1756 		fmd_asru_hash_release(ahp, ap);
1757 	}
1758 
1759 	fmd_module_unlock(mp);
1760 	return (rv);
1761 }
1762 
1763 int
1764 fmd_nvl_fmri_contains(fmd_hdl_t *hdl, nvlist_t *n1, nvlist_t *n2)
1765 {
1766 	fmd_module_t *mp = fmd_api_module_lock(hdl);
1767 	int rv;
1768 
1769 	if (n1 == NULL || n2 == NULL) {
1770 		fmd_api_error(mp, EFMD_NVL_INVAL,
1771 		    "invalid nvlist(s): %p, %p\n", (void *)n1, (void *)n2);
1772 	}
1773 
1774 	rv = fmd_fmri_contains(n1, n2);
1775 	fmd_module_unlock(mp);
1776 
1777 	if (rv < 0) {
1778 		fmd_api_error(mp, EFMD_FMRI_OP, "invalid fmri for "
1779 		    "fmd_nvl_fmri_contains\n");
1780 	}
1781 
1782 	return (rv);
1783 }
1784 
1785 nvlist_t *
1786 fmd_nvl_fmri_translate(fmd_hdl_t *hdl, nvlist_t *fmri, nvlist_t *auth)
1787 {
1788 	fmd_module_t *mp = fmd_api_module_lock(hdl);
1789 	nvlist_t *xfmri;
1790 
1791 	if (fmri == NULL || auth == NULL) {
1792 		fmd_api_error(mp, EFMD_NVL_INVAL,
1793 		    "invalid nvlist(s): %p, %p\n", (void *)fmri, (void *)auth);
1794 	}
1795 
1796 	xfmri = fmd_fmri_translate(fmri, auth);
1797 	fmd_module_unlock(mp);
1798 	return (xfmri);
1799 }
1800 
1801 int
1802 fmd_event_local(fmd_hdl_t *hdl, fmd_event_t *ep)
1803 {
1804 	if (hdl == NULL || ep == NULL) {
1805 		fmd_api_error(fmd_api_module_lock(hdl), EFMD_EVENT_INVAL,
1806 		    "NULL parameter specified to fmd_event_local\n");
1807 	}
1808 
1809 	return (((fmd_event_impl_t *)ep)->ev_flags & FMD_EVF_LOCAL);
1810 }
1811 
1812 fmd_xprt_t *
1813 fmd_xprt_open(fmd_hdl_t *hdl, uint_t flags, nvlist_t *auth, void *data)
1814 {
1815 	fmd_module_t *mp = fmd_api_module_lock(hdl);
1816 	fmd_xprt_t *xp;
1817 
1818 	if (flags & ~FMD_XPRT_CMASK) {
1819 		fmd_api_error(mp, EFMD_XPRT_INVAL,
1820 		    "invalid transport flags 0x%x\n", flags);
1821 	}
1822 
1823 	if ((flags & FMD_XPRT_RDWR) != FMD_XPRT_RDWR &&
1824 	    (flags & FMD_XPRT_RDWR) != FMD_XPRT_RDONLY) {
1825 		fmd_api_error(mp, EFMD_XPRT_INVAL,
1826 		    "cannot open write-only transport\n");
1827 	}
1828 
1829 	if (mp->mod_stats->ms_xprtopen.fmds_value.ui32 >=
1830 	    mp->mod_stats->ms_xprtlimit.fmds_value.ui32) {
1831 		fmd_api_error(mp, EFMD_XPRT_LIMIT, "%s request to create a "
1832 		    "transport exceeds module transport limit (%u)\n",
1833 		    mp->mod_name, mp->mod_stats->ms_xprtlimit.fmds_value.ui32);
1834 	}
1835 
1836 	if ((xp = fmd_xprt_create(mp, flags, auth, data)) == NULL)
1837 		fmd_api_error(mp, errno, "cannot create transport");
1838 
1839 	fmd_module_unlock(mp);
1840 	return (xp);
1841 }
1842 
1843 void
1844 fmd_xprt_close(fmd_hdl_t *hdl, fmd_xprt_t *xp)
1845 {
1846 	fmd_module_t *mp = fmd_api_module_lock(hdl);
1847 	fmd_xprt_impl_t *xip = fmd_api_transport_impl(hdl, xp);
1848 
1849 	/*
1850 	 * Although this could be supported, it doesn't seem necessary or worth
1851 	 * the trouble.  For now, just detect this and trigger a module abort.
1852 	 * If it is needed, transports should grow reference counts and a new
1853 	 * event type will need to be enqueued for the main thread to reap it.
1854 	 */
1855 	if (xip->xi_thread != NULL &&
1856 	    xip->xi_thread->thr_tid == pthread_self()) {
1857 		fmd_api_error(mp, EFMD_XPRT_INVAL,
1858 		    "fmd_xprt_close() cannot be called from fmdo_send()\n");
1859 	}
1860 
1861 	fmd_xprt_destroy(xp);
1862 	fmd_module_unlock(mp);
1863 }
1864 
1865 void
1866 fmd_xprt_post(fmd_hdl_t *hdl, fmd_xprt_t *xp, nvlist_t *nvl, hrtime_t hrt)
1867 {
1868 	fmd_xprt_impl_t *xip = fmd_api_transport_impl(hdl, xp);
1869 
1870 	/*
1871 	 * fmd_xprt_recv() must block during startup waiting for fmd to globally
1872 	 * clear FMD_XPRT_DSUSPENDED.  As such, we can't allow it to be called
1873 	 * from a module's _fmd_init() routine, because that would block
1874 	 * fmd from completing initial module loading, resulting in a deadlock.
1875 	 */
1876 	if ((xip->xi_flags & FMD_XPRT_ISUSPENDED) &&
1877 	    (pthread_self() == xip->xi_queue->eq_mod->mod_thread->thr_tid)) {
1878 		fmd_api_error(fmd_api_module_lock(hdl), EFMD_XPRT_INVAL,
1879 		    "fmd_xprt_post() cannot be called from _fmd_init()\n");
1880 	}
1881 
1882 	fmd_xprt_recv(xp, nvl, hrt);
1883 }
1884 
1885 void
1886 fmd_xprt_suspend(fmd_hdl_t *hdl, fmd_xprt_t *xp)
1887 {
1888 	(void) fmd_api_transport_impl(hdl, xp); /* validate 'xp' */
1889 	fmd_xprt_xsuspend(xp, FMD_XPRT_SUSPENDED);
1890 }
1891 
1892 void
1893 fmd_xprt_resume(fmd_hdl_t *hdl, fmd_xprt_t *xp)
1894 {
1895 	(void) fmd_api_transport_impl(hdl, xp); /* validate 'xp' */
1896 	fmd_xprt_xresume(xp, FMD_XPRT_SUSPENDED);
1897 }
1898 
1899 int
1900 fmd_xprt_error(fmd_hdl_t *hdl, fmd_xprt_t *xp)
1901 {
1902 	fmd_xprt_impl_t *xip = fmd_api_transport_impl(hdl, xp);
1903 	return (xip->xi_state == _fmd_xprt_state_err);
1904 }
1905 
1906 /*
1907  * Translate all FMRIs in the specified name-value pair list for the specified
1908  * FMRI authority, and return a new name-value pair list for the translation.
1909  * This function is the recursive engine used by fmd_xprt_translate(), below.
1910  */
1911 static nvlist_t *
1912 fmd_xprt_xtranslate(nvlist_t *nvl, nvlist_t *auth)
1913 {
1914 	uint_t i, j, n;
1915 	nvpair_t *nvp, **nvps;
1916 	uint_t nvpslen = 0;
1917 	char *name;
1918 	size_t namelen = 0;
1919 
1920 	nvlist_t **a, **b;
1921 	nvlist_t *l, *r;
1922 	data_type_t type;
1923 	char *s;
1924 	int err;
1925 
1926 	(void) nvlist_xdup(nvl, &nvl, &fmd.d_nva);
1927 
1928 	/*
1929 	 * Count up the number of name-value pairs in 'nvl' and compute the
1930 	 * maximum length of a name used in this list for use below.
1931 	 */
1932 	for (nvp = nvlist_next_nvpair(nvl, NULL);
1933 	    nvp != NULL; nvp = nvlist_next_nvpair(nvl, nvp), nvpslen++) {
1934 		size_t len = strlen(nvpair_name(nvp));
1935 		namelen = MAX(namelen, len);
1936 	}
1937 
1938 	nvps = alloca(sizeof (nvpair_t *) * nvpslen);
1939 	name = alloca(namelen + 1);
1940 
1941 	/*
1942 	 * Store a snapshot of the name-value pairs in 'nvl' into nvps[] so
1943 	 * that we can iterate over the original pairs in the loop below while
1944 	 * performing arbitrary insert and delete operations on 'nvl' itself.
1945 	 */
1946 	for (i = 0, nvp = nvlist_next_nvpair(nvl, NULL);
1947 	    nvp != NULL; nvp = nvlist_next_nvpair(nvl, nvp))
1948 		nvps[i++] = nvp;
1949 
1950 	/*
1951 	 * Now iterate over the snapshot of the name-value pairs.  If we find a
1952 	 * value that is of type NVLIST or NVLIST_ARRAY, we translate that
1953 	 * object by either calling ourself recursively on it, or calling into
1954 	 * fmd_fmri_translate() if the object is an FMRI.  We then rip out the
1955 	 * original name-value pair and replace it with the translated one.
1956 	 */
1957 	for (i = 0; i < nvpslen; i++) {
1958 		nvp = nvps[i];
1959 		type = nvpair_type(nvp);
1960 
1961 		switch (type) {
1962 		case DATA_TYPE_NVLIST_ARRAY:
1963 			if (nvpair_value_nvlist_array(nvp, &a, &n) != 0 ||
1964 			    a == NULL || n == 0)
1965 				continue; /* array is zero-sized; skip it */
1966 
1967 			b = fmd_alloc(sizeof (nvlist_t *) * n, FMD_SLEEP);
1968 
1969 			/*
1970 			 * If the first array nvlist element looks like an FMRI
1971 			 * then assume the other elements are FMRIs as well.
1972 			 * If any b[j]'s can't be translated, then EINVAL will
1973 			 * be returned from nvlist_add_nvlist_array() below.
1974 			 */
1975 			if (nvlist_lookup_string(*a, FM_FMRI_SCHEME, &s) == 0) {
1976 				for (j = 0; j < n; j++)
1977 					b[j] = fmd_fmri_translate(a[j], auth);
1978 			} else {
1979 				for (j = 0; j < n; j++)
1980 					b[j] = fmd_xprt_xtranslate(a[j], auth);
1981 			}
1982 
1983 			(void) strcpy(name, nvpair_name(nvp));
1984 			(void) nvlist_remove(nvl, name, type);
1985 			err = nvlist_add_nvlist_array(nvl, name, b, n);
1986 
1987 			for (j = 0; j < n; j++)
1988 				nvlist_free(b[j]);
1989 
1990 			fmd_free(b, sizeof (nvlist_t *) * n);
1991 
1992 			if (err != 0) {
1993 				nvlist_free(nvl);
1994 				errno = err;
1995 				return (NULL);
1996 			}
1997 			break;
1998 
1999 		case DATA_TYPE_NVLIST:
2000 			if (nvpair_value_nvlist(nvp, &l) == 0 &&
2001 			    nvlist_lookup_string(l, FM_FMRI_SCHEME, &s) == 0)
2002 				r = fmd_fmri_translate(l, auth);
2003 			else
2004 				r = fmd_xprt_xtranslate(l, auth);
2005 
2006 			if (r == NULL) {
2007 				nvlist_free(nvl);
2008 				return (NULL);
2009 			}
2010 
2011 			(void) strcpy(name, nvpair_name(nvp));
2012 			(void) nvlist_remove(nvl, name, type);
2013 			(void) nvlist_add_nvlist(nvl, name, r);
2014 
2015 			nvlist_free(r);
2016 			break;
2017 		}
2018 	}
2019 
2020 	return (nvl);
2021 }
2022 
2023 nvlist_t *
2024 fmd_xprt_translate(fmd_hdl_t *hdl, fmd_xprt_t *xp, fmd_event_t *ep)
2025 {
2026 	fmd_xprt_impl_t *xip = fmd_api_transport_impl(hdl, xp);
2027 
2028 	if (xip->xi_auth == NULL) {
2029 		fmd_api_error(fmd_api_module_lock(hdl), EFMD_XPRT_INVAL,
2030 		    "no authority defined for transport %p\n", (void *)xp);
2031 	}
2032 
2033 	return (fmd_xprt_xtranslate(FMD_EVENT_NVL(ep), xip->xi_auth));
2034 }
2035 
2036 void
2037 fmd_xprt_setspecific(fmd_hdl_t *hdl, fmd_xprt_t *xp, void *data)
2038 {
2039 	fmd_api_transport_impl(hdl, xp)->xi_data = data;
2040 }
2041 
2042 void *
2043 fmd_xprt_getspecific(fmd_hdl_t *hdl, fmd_xprt_t *xp)
2044 {
2045 	return (fmd_api_transport_impl(hdl, xp)->xi_data);
2046 }
2047