xref: /titanic_41/usr/src/cmd/fm/fmd/common/fmd_api.c (revision 48258c6b4e17f36ab09fba0bd6307d1fec9dcbce)
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, *serial = 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 	/*
1702 	 * In some cases, serial information for the resource will not be
1703 	 * available at enumeration but may instead be available by invoking
1704 	 * a dynamic property method on the FRU.  In order to ensure the serial
1705 	 * number is persisted properly in the ASRU cache, we'll fetch the
1706 	 * property, if it exists, and add it to the resource and fru fmris.
1707 	 */
1708 	if (fru != NULL) {
1709 		(void) topo_fmri_serial(thp, fru, &serial, &err);
1710 		if (serial != NULL) {
1711 			(void) nvlist_add_string(rsrc, "serial", serial);
1712 			(void) nvlist_add_string(fru, "serial", serial);
1713 			topo_hdl_strfree(thp, serial);
1714 		}
1715 	}
1716 	nvl = fmd_protocol_fault(class, certainty, asru, fru, rsrc, loc);
1717 
1718 	if (loc != NULL)
1719 		topo_hdl_strfree(thp, loc);
1720 
1721 	err = fmd_module_topo_rele(mp, thp);
1722 	ASSERT(err == 0);
1723 
1724 	fmd_module_unlock(mp);
1725 
1726 	return (nvl);
1727 }
1728 
1729 int
1730 fmd_nvl_class_match(fmd_hdl_t *hdl, nvlist_t *nvl, const char *pattern)
1731 {
1732 	fmd_module_t *mp = fmd_api_module_lock(hdl);
1733 	char *class;
1734 	int rv;
1735 
1736 	rv = (nvl != NULL && nvlist_lookup_string(nvl,
1737 	    FM_CLASS, &class) == 0 && fmd_strmatch(class, pattern));
1738 
1739 	fmd_module_unlock(mp);
1740 	return (rv);
1741 }
1742 
1743 int
1744 fmd_nvl_fmri_expand(fmd_hdl_t *hdl, nvlist_t *nvl)
1745 {
1746 	fmd_module_t *mp = fmd_api_module_lock(hdl);
1747 	int rv;
1748 
1749 	if (nvl == NULL) {
1750 		fmd_api_error(mp, EFMD_NVL_INVAL,
1751 		    "invalid nvlist %p\n", (void *)nvl);
1752 	}
1753 
1754 	rv = fmd_fmri_expand(nvl);
1755 	fmd_module_unlock(mp);
1756 	return (rv);
1757 }
1758 
1759 int
1760 fmd_nvl_fmri_present(fmd_hdl_t *hdl, nvlist_t *nvl)
1761 {
1762 	fmd_module_t *mp = fmd_api_module_lock(hdl);
1763 	int rv;
1764 
1765 	if (nvl == NULL) {
1766 		fmd_api_error(mp, EFMD_NVL_INVAL,
1767 		    "invalid nvlist %p\n", (void *)nvl);
1768 	}
1769 
1770 	rv = fmd_fmri_present(nvl);
1771 	fmd_module_unlock(mp);
1772 
1773 	if (rv < 0) {
1774 		fmd_api_error(mp, EFMD_FMRI_OP, "invalid fmri for "
1775 		    "fmd_nvl_fmri_present\n");
1776 	}
1777 
1778 	return (rv);
1779 }
1780 
1781 int
1782 fmd_nvl_fmri_unusable(fmd_hdl_t *hdl, nvlist_t *nvl)
1783 {
1784 	fmd_module_t *mp = fmd_api_module_lock(hdl);
1785 	int rv;
1786 
1787 	if (nvl == NULL) {
1788 		fmd_api_error(mp, EFMD_NVL_INVAL,
1789 		    "invalid nvlist %p\n", (void *)nvl);
1790 	}
1791 
1792 	rv = fmd_fmri_unusable(nvl);
1793 	fmd_module_unlock(mp);
1794 
1795 	if (rv < 0) {
1796 		fmd_api_error(mp, EFMD_FMRI_OP, "invalid fmri for "
1797 		    "fmd_nvl_fmri_unusable\n");
1798 	}
1799 
1800 	return (rv);
1801 }
1802 
1803 int
1804 fmd_nvl_fmri_faulty(fmd_hdl_t *hdl, nvlist_t *nvl)
1805 {
1806 	fmd_module_t *mp = fmd_api_module_lock(hdl);
1807 	fmd_asru_hash_t *ahp = fmd.d_asrus;
1808 	fmd_asru_t *ap;
1809 	int rv = 0;
1810 
1811 	if (nvl == NULL) {
1812 		fmd_api_error(mp, EFMD_NVL_INVAL,
1813 		    "invalid nvlist %p\n", (void *)nvl);
1814 	}
1815 
1816 	if ((ap = fmd_asru_hash_lookup_nvl(ahp, nvl)) != NULL) {
1817 		rv = (ap->asru_flags & FMD_ASRU_FAULTY) != 0;
1818 		fmd_asru_hash_release(ahp, ap);
1819 	}
1820 
1821 	fmd_module_unlock(mp);
1822 	return (rv);
1823 }
1824 
1825 int
1826 fmd_nvl_fmri_contains(fmd_hdl_t *hdl, nvlist_t *n1, nvlist_t *n2)
1827 {
1828 	fmd_module_t *mp = fmd_api_module_lock(hdl);
1829 	int rv;
1830 
1831 	if (n1 == NULL || n2 == NULL) {
1832 		fmd_api_error(mp, EFMD_NVL_INVAL,
1833 		    "invalid nvlist(s): %p, %p\n", (void *)n1, (void *)n2);
1834 	}
1835 
1836 	rv = fmd_fmri_contains(n1, n2);
1837 	fmd_module_unlock(mp);
1838 
1839 	if (rv < 0) {
1840 		fmd_api_error(mp, EFMD_FMRI_OP, "invalid fmri for "
1841 		    "fmd_nvl_fmri_contains\n");
1842 	}
1843 
1844 	return (rv);
1845 }
1846 
1847 nvlist_t *
1848 fmd_nvl_fmri_translate(fmd_hdl_t *hdl, nvlist_t *fmri, nvlist_t *auth)
1849 {
1850 	fmd_module_t *mp = fmd_api_module_lock(hdl);
1851 	nvlist_t *xfmri;
1852 
1853 	if (fmri == NULL || auth == NULL) {
1854 		fmd_api_error(mp, EFMD_NVL_INVAL,
1855 		    "invalid nvlist(s): %p, %p\n", (void *)fmri, (void *)auth);
1856 	}
1857 
1858 	xfmri = fmd_fmri_translate(fmri, auth);
1859 	fmd_module_unlock(mp);
1860 	return (xfmri);
1861 }
1862 
1863 int
1864 fmd_event_local(fmd_hdl_t *hdl, fmd_event_t *ep)
1865 {
1866 	if (hdl == NULL || ep == NULL) {
1867 		fmd_api_error(fmd_api_module_lock(hdl), EFMD_EVENT_INVAL,
1868 		    "NULL parameter specified to fmd_event_local\n");
1869 	}
1870 
1871 	return (((fmd_event_impl_t *)ep)->ev_flags & FMD_EVF_LOCAL);
1872 }
1873 
1874 /*ARGSUSED*/
1875 uint64_t
1876 fmd_event_ena_create(fmd_hdl_t *hdl)
1877 {
1878 	return (fmd_ena());
1879 }
1880 
1881 fmd_xprt_t *
1882 fmd_xprt_open(fmd_hdl_t *hdl, uint_t flags, nvlist_t *auth, void *data)
1883 {
1884 	fmd_module_t *mp = fmd_api_module_lock(hdl);
1885 	fmd_xprt_t *xp;
1886 
1887 	if (flags & ~FMD_XPRT_CMASK) {
1888 		fmd_api_error(mp, EFMD_XPRT_INVAL,
1889 		    "invalid transport flags 0x%x\n", flags);
1890 	}
1891 
1892 	if ((flags & FMD_XPRT_RDWR) != FMD_XPRT_RDWR &&
1893 	    (flags & FMD_XPRT_RDWR) != FMD_XPRT_RDONLY) {
1894 		fmd_api_error(mp, EFMD_XPRT_INVAL,
1895 		    "cannot open write-only transport\n");
1896 	}
1897 
1898 	if (mp->mod_stats->ms_xprtopen.fmds_value.ui32 >=
1899 	    mp->mod_stats->ms_xprtlimit.fmds_value.ui32) {
1900 		fmd_api_error(mp, EFMD_XPRT_LIMIT, "%s request to create a "
1901 		    "transport exceeds module transport limit (%u)\n",
1902 		    mp->mod_name, mp->mod_stats->ms_xprtlimit.fmds_value.ui32);
1903 	}
1904 
1905 	if ((xp = fmd_xprt_create(mp, flags, auth, data)) == NULL)
1906 		fmd_api_error(mp, errno, "cannot create transport");
1907 
1908 	fmd_module_unlock(mp);
1909 	return (xp);
1910 }
1911 
1912 void
1913 fmd_xprt_close(fmd_hdl_t *hdl, fmd_xprt_t *xp)
1914 {
1915 	fmd_module_t *mp = fmd_api_module_lock(hdl);
1916 	fmd_xprt_impl_t *xip = fmd_api_transport_impl(hdl, xp);
1917 
1918 	/*
1919 	 * Although this could be supported, it doesn't seem necessary or worth
1920 	 * the trouble.  For now, just detect this and trigger a module abort.
1921 	 * If it is needed, transports should grow reference counts and a new
1922 	 * event type will need to be enqueued for the main thread to reap it.
1923 	 */
1924 	if (xip->xi_thread != NULL &&
1925 	    xip->xi_thread->thr_tid == pthread_self()) {
1926 		fmd_api_error(mp, EFMD_XPRT_INVAL,
1927 		    "fmd_xprt_close() cannot be called from fmdo_send()\n");
1928 	}
1929 
1930 	fmd_xprt_destroy(xp);
1931 	fmd_module_unlock(mp);
1932 }
1933 
1934 void
1935 fmd_xprt_post(fmd_hdl_t *hdl, fmd_xprt_t *xp, nvlist_t *nvl, hrtime_t hrt)
1936 {
1937 	fmd_xprt_impl_t *xip = fmd_api_transport_impl(hdl, xp);
1938 
1939 	/*
1940 	 * fmd_xprt_recv() must block during startup waiting for fmd to globally
1941 	 * clear FMD_XPRT_DSUSPENDED.  As such, we can't allow it to be called
1942 	 * from a module's _fmd_init() routine, because that would block
1943 	 * fmd from completing initial module loading, resulting in a deadlock.
1944 	 */
1945 	if ((xip->xi_flags & FMD_XPRT_ISUSPENDED) &&
1946 	    (pthread_self() == xip->xi_queue->eq_mod->mod_thread->thr_tid)) {
1947 		fmd_api_error(fmd_api_module_lock(hdl), EFMD_XPRT_INVAL,
1948 		    "fmd_xprt_post() cannot be called from _fmd_init()\n");
1949 	}
1950 
1951 	fmd_xprt_recv(xp, nvl, hrt);
1952 }
1953 
1954 void
1955 fmd_xprt_suspend(fmd_hdl_t *hdl, fmd_xprt_t *xp)
1956 {
1957 	(void) fmd_api_transport_impl(hdl, xp); /* validate 'xp' */
1958 	fmd_xprt_xsuspend(xp, FMD_XPRT_SUSPENDED);
1959 }
1960 
1961 void
1962 fmd_xprt_resume(fmd_hdl_t *hdl, fmd_xprt_t *xp)
1963 {
1964 	(void) fmd_api_transport_impl(hdl, xp); /* validate 'xp' */
1965 	fmd_xprt_xresume(xp, FMD_XPRT_SUSPENDED);
1966 }
1967 
1968 int
1969 fmd_xprt_error(fmd_hdl_t *hdl, fmd_xprt_t *xp)
1970 {
1971 	fmd_xprt_impl_t *xip = fmd_api_transport_impl(hdl, xp);
1972 	return (xip->xi_state == _fmd_xprt_state_err);
1973 }
1974 
1975 /*
1976  * Translate all FMRIs in the specified name-value pair list for the specified
1977  * FMRI authority, and return a new name-value pair list for the translation.
1978  * This function is the recursive engine used by fmd_xprt_translate(), below.
1979  */
1980 static nvlist_t *
1981 fmd_xprt_xtranslate(nvlist_t *nvl, nvlist_t *auth)
1982 {
1983 	uint_t i, j, n;
1984 	nvpair_t *nvp, **nvps;
1985 	uint_t nvpslen = 0;
1986 	char *name;
1987 	size_t namelen = 0;
1988 
1989 	nvlist_t **a, **b;
1990 	nvlist_t *l, *r;
1991 	data_type_t type;
1992 	char *s;
1993 	int err;
1994 
1995 	(void) nvlist_xdup(nvl, &nvl, &fmd.d_nva);
1996 
1997 	/*
1998 	 * Count up the number of name-value pairs in 'nvl' and compute the
1999 	 * maximum length of a name used in this list for use below.
2000 	 */
2001 	for (nvp = nvlist_next_nvpair(nvl, NULL);
2002 	    nvp != NULL; nvp = nvlist_next_nvpair(nvl, nvp), nvpslen++) {
2003 		size_t len = strlen(nvpair_name(nvp));
2004 		namelen = MAX(namelen, len);
2005 	}
2006 
2007 	nvps = alloca(sizeof (nvpair_t *) * nvpslen);
2008 	name = alloca(namelen + 1);
2009 
2010 	/*
2011 	 * Store a snapshot of the name-value pairs in 'nvl' into nvps[] so
2012 	 * that we can iterate over the original pairs in the loop below while
2013 	 * performing arbitrary insert and delete operations on 'nvl' itself.
2014 	 */
2015 	for (i = 0, nvp = nvlist_next_nvpair(nvl, NULL);
2016 	    nvp != NULL; nvp = nvlist_next_nvpair(nvl, nvp))
2017 		nvps[i++] = nvp;
2018 
2019 	/*
2020 	 * Now iterate over the snapshot of the name-value pairs.  If we find a
2021 	 * value that is of type NVLIST or NVLIST_ARRAY, we translate that
2022 	 * object by either calling ourself recursively on it, or calling into
2023 	 * fmd_fmri_translate() if the object is an FMRI.  We then rip out the
2024 	 * original name-value pair and replace it with the translated one.
2025 	 */
2026 	for (i = 0; i < nvpslen; i++) {
2027 		nvp = nvps[i];
2028 		type = nvpair_type(nvp);
2029 
2030 		switch (type) {
2031 		case DATA_TYPE_NVLIST_ARRAY:
2032 			if (nvpair_value_nvlist_array(nvp, &a, &n) != 0 ||
2033 			    a == NULL || n == 0)
2034 				continue; /* array is zero-sized; skip it */
2035 
2036 			b = fmd_alloc(sizeof (nvlist_t *) * n, FMD_SLEEP);
2037 
2038 			/*
2039 			 * If the first array nvlist element looks like an FMRI
2040 			 * then assume the other elements are FMRIs as well.
2041 			 * If any b[j]'s can't be translated, then EINVAL will
2042 			 * be returned from nvlist_add_nvlist_array() below.
2043 			 */
2044 			if (nvlist_lookup_string(*a, FM_FMRI_SCHEME, &s) == 0) {
2045 				for (j = 0; j < n; j++)
2046 					b[j] = fmd_fmri_translate(a[j], auth);
2047 			} else {
2048 				for (j = 0; j < n; j++)
2049 					b[j] = fmd_xprt_xtranslate(a[j], auth);
2050 			}
2051 
2052 			(void) strcpy(name, nvpair_name(nvp));
2053 			(void) nvlist_remove(nvl, name, type);
2054 			err = nvlist_add_nvlist_array(nvl, name, b, n);
2055 
2056 			for (j = 0; j < n; j++)
2057 				nvlist_free(b[j]);
2058 
2059 			fmd_free(b, sizeof (nvlist_t *) * n);
2060 
2061 			if (err != 0) {
2062 				nvlist_free(nvl);
2063 				errno = err;
2064 				return (NULL);
2065 			}
2066 			break;
2067 
2068 		case DATA_TYPE_NVLIST:
2069 			if (nvpair_value_nvlist(nvp, &l) == 0 &&
2070 			    nvlist_lookup_string(l, FM_FMRI_SCHEME, &s) == 0)
2071 				r = fmd_fmri_translate(l, auth);
2072 			else
2073 				r = fmd_xprt_xtranslate(l, auth);
2074 
2075 			if (r == NULL) {
2076 				nvlist_free(nvl);
2077 				return (NULL);
2078 			}
2079 
2080 			(void) strcpy(name, nvpair_name(nvp));
2081 			(void) nvlist_remove(nvl, name, type);
2082 			(void) nvlist_add_nvlist(nvl, name, r);
2083 
2084 			nvlist_free(r);
2085 			break;
2086 		}
2087 	}
2088 
2089 	return (nvl);
2090 }
2091 
2092 nvlist_t *
2093 fmd_xprt_translate(fmd_hdl_t *hdl, fmd_xprt_t *xp, fmd_event_t *ep)
2094 {
2095 	fmd_xprt_impl_t *xip = fmd_api_transport_impl(hdl, xp);
2096 
2097 	if (xip->xi_auth == NULL) {
2098 		fmd_api_error(fmd_api_module_lock(hdl), EFMD_XPRT_INVAL,
2099 		    "no authority defined for transport %p\n", (void *)xp);
2100 	}
2101 
2102 	return (fmd_xprt_xtranslate(FMD_EVENT_NVL(ep), xip->xi_auth));
2103 }
2104 
2105 void
2106 fmd_xprt_setspecific(fmd_hdl_t *hdl, fmd_xprt_t *xp, void *data)
2107 {
2108 	fmd_api_transport_impl(hdl, xp)->xi_data = data;
2109 }
2110 
2111 void *
2112 fmd_xprt_getspecific(fmd_hdl_t *hdl, fmd_xprt_t *xp)
2113 {
2114 	return (fmd_api_transport_impl(hdl, xp)->xi_data);
2115 }
2116