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