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