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