dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / cmd / fm / fmd / common / fmd_api.c
blob008a55c90fe72f9cd21d8a1fcdecf0a6f0f690fa
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
23 * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
26 #include <sys/types.h>
27 #include <sys/fm/protocol.h>
28 #include <fm/topo_hc.h>
29 #include <uuid/uuid.h>
31 #include <unistd.h>
32 #include <signal.h>
33 #include <limits.h>
34 #include <syslog.h>
35 #include <alloca.h>
36 #include <stddef.h>
37 #include <door.h>
39 #include <fmd_module.h>
40 #include <fmd_api.h>
41 #include <fmd_string.h>
42 #include <fmd_subr.h>
43 #include <fmd_error.h>
44 #include <fmd_event.h>
45 #include <fmd_eventq.h>
46 #include <fmd_dispq.h>
47 #include <fmd_timerq.h>
48 #include <fmd_thread.h>
49 #include <fmd_ustat.h>
50 #include <fmd_case.h>
51 #include <fmd_protocol.h>
52 #include <fmd_buf.h>
53 #include <fmd_asru.h>
54 #include <fmd_fmri.h>
55 #include <fmd_topo.h>
56 #include <fmd_ckpt.h>
57 #include <fmd_xprt.h>
59 #include <fmd.h>
62 * Table of configuration file variable types ops-vector pointers. We use this
63 * to convert from the property description array specified by the module to an
64 * array of fmd_conf_formal_t's. The order of this array must match the order
65 * of #define values specified in <fmd_api.h> (i.e. FMD_TYPE_BOOL must be 0).
66 * For now, the fmd_conf_list and fmd_conf_path types are not supported as we
67 * do not believe modules need them and they would require more complexity.
69 static const fmd_conf_ops_t *const _fmd_prop_ops[] = {
70 &fmd_conf_bool, /* FMD_TYPE_BOOL */
71 &fmd_conf_int32, /* FMD_TYPE_INT32 */
72 &fmd_conf_uint32, /* FMD_TYPE_UINT32 */
73 &fmd_conf_int64, /* FMD_TYPE_INT64 */
74 &fmd_conf_uint64, /* FMD_TYPE_UINT64 */
75 &fmd_conf_string, /* FMD_TYPE_STRING */
76 &fmd_conf_time, /* FMD_TYPE_TIME */
77 &fmd_conf_size, /* FMD_TYPE_SIZE */
80 static void fmd_api_verror(fmd_module_t *, int, const char *, va_list)
81 __NORETURN;
82 static void fmd_api_error(fmd_module_t *, int, const char *, ...) __NORETURN;
85 * fmd_api_vxerror() provides the engine underlying the fmd_hdl_[v]error() API
86 * calls and the fmd_api_[v]error() utility routine defined below. The routine
87 * formats the error, optionally associated with a particular errno code 'err',
88 * and logs it as an ereport associated with the calling module. Depending on
89 * other optional properties, we also emit a message to stderr and to syslog.
91 static void
92 fmd_api_vxerror(fmd_module_t *mp, int err, const char *format, va_list ap)
94 int raw_err = err;
95 nvlist_t *nvl;
96 fmd_event_t *e;
97 char *class, *msg;
98 size_t len1, len2;
99 char c;
102 * fmd_api_vxerror() counts as both an error of class EFMD_MODULE
103 * as well as an instance of 'err' w.r.t. our internal bean counters.
105 (void) pthread_mutex_lock(&fmd.d_err_lock);
106 fmd.d_errstats[EFMD_MODULE - EFMD_UNKNOWN].fmds_value.ui64++;
108 if (err > EFMD_UNKNOWN && err < EFMD_END)
109 fmd.d_errstats[err - EFMD_UNKNOWN].fmds_value.ui64++;
111 (void) pthread_mutex_unlock(&fmd.d_err_lock);
114 * Format the message using vsnprintf(). As usual, if the format has a
115 * newline in it, it is printed alone; otherwise strerror() is added.
117 if (strchr(format, '\n') != NULL)
118 err = 0; /* err is not relevant in the message */
120 len1 = vsnprintf(&c, 1, format, ap);
121 len2 = err != 0 ? snprintf(&c, 1, ": %s\n", fmd_strerror(err)) : 0;
123 msg = fmd_alloc(len1 + len2 + 1, FMD_SLEEP);
124 (void) vsnprintf(msg, len1 + 1, format, ap);
126 if (err != 0) {
127 (void) snprintf(&msg[len1], len2 + 1,
128 ": %s\n", fmd_strerror(err));
132 * Create an error event corresponding to the error, insert it into the
133 * error log, and dispatch it to the fmd-self-diagnosis engine.
135 if (mp != fmd.d_self && (raw_err != EFMD_HDL_ABORT || fmd.d_running)) {
136 if ((c = msg[len1 + len2 - 1]) == '\n')
137 msg[len1 + len2 - 1] = '\0'; /* strip \n for event */
139 nvl = fmd_protocol_moderror(mp, err, msg);
141 if (c == '\n')
142 msg[len1 + len2 - 1] = c;
144 (void) nvlist_lookup_string(nvl, FM_CLASS, &class);
145 e = fmd_event_create(FMD_EVT_PROTOCOL, FMD_HRT_NOW, nvl, class);
147 (void) pthread_rwlock_rdlock(&fmd.d_log_lock);
148 fmd_log_append(fmd.d_errlog, e, NULL);
149 (void) pthread_rwlock_unlock(&fmd.d_log_lock);
151 fmd_event_transition(e, FMD_EVS_ACCEPTED);
152 fmd_event_commit(e);
154 fmd_dispq_dispatch(fmd.d_disp, e, class);
158 * Similar to fmd_vdebug(), if the debugging switches are enabled we
159 * echo the module name and message to stderr and/or syslog. Unlike
160 * fmd_vdebug(), we also print to stderr if foreground mode is enabled.
161 * We also print the message if a built-in module is aborting before
162 * fmd has detached from its parent (e.g. default transport failure).
164 if (fmd.d_fg || (fmd.d_hdl_dbout & FMD_DBOUT_STDERR) || (
165 raw_err == EFMD_HDL_ABORT && !fmd.d_running)) {
166 (void) pthread_mutex_lock(&fmd.d_err_lock);
167 (void) fprintf(stderr, "%s: %s: %s",
168 fmd.d_pname, mp->mod_name, msg);
169 (void) pthread_mutex_unlock(&fmd.d_err_lock);
172 if (fmd.d_hdl_dbout & FMD_DBOUT_SYSLOG) {
173 syslog(LOG_ERR | LOG_DAEMON, "%s ERROR: %s: %s",
174 fmd.d_pname, mp->mod_name, msg);
177 fmd_free(msg, len1 + len2 + 1);
180 /*PRINTFLIKE3*/
181 static void
182 fmd_api_xerror(fmd_module_t *mp, int err, const char *format, ...)
184 va_list ap;
186 va_start(ap, format);
187 fmd_api_vxerror(mp, err, format, ap);
188 va_end(ap);
192 * fmd_api_verror() is a wrapper around fmd_api_vxerror() for API subroutines.
193 * It calls fmd_module_unlock() on behalf of its caller, logs the error, and
194 * then aborts the API call and the surrounding module entry point by doing an
195 * fmd_module_abort(), which longjmps to the place where we entered the module.
197 static void
198 fmd_api_verror(fmd_module_t *mp, int err, const char *format, va_list ap)
200 if (fmd_module_locked(mp))
201 fmd_module_unlock(mp);
203 fmd_api_vxerror(mp, err, format, ap);
204 fmd_module_abort(mp, err);
207 /*PRINTFLIKE3*/
208 static void
209 fmd_api_error(fmd_module_t *mp, int err, const char *format, ...)
211 va_list ap;
213 va_start(ap, format);
214 fmd_api_verror(mp, err, format, ap);
215 va_end(ap);
219 * Common code for fmd_api_module_lock() and fmd_api_transport_impl(). This
220 * code verifies that the handle is valid and associated with a proper thread.
222 static fmd_module_t *
223 fmd_api_module(fmd_hdl_t *hdl)
225 fmd_thread_t *tp;
226 fmd_module_t *mp;
229 * If our TSD is not present at all, this is either a serious bug or
230 * someone has created a thread behind our back and is using fmd's API.
231 * We can't call fmd_api_error() because we can't be sure that we can
232 * unwind our state back to an enclosing fmd_module_dispatch(), so we
233 * must panic instead. This is likely a module design or coding error.
235 if ((tp = pthread_getspecific(fmd.d_key)) == NULL) {
236 fmd_panic("fmd module api call made using "
237 "client handle %p from unknown thread\n", (void *)hdl);
241 * If our TSD refers to the root module and is a non-private
242 * door server thread, then it was created asynchronously at the
243 * request of a module but is using now the module API as an
244 * auxiliary module thread. We reset tp->thr_mod to the module
245 * handle so it can act as a module thread.
247 * If more than one module uses non-private doors then the
248 * "client handle is not valid" check below can fail since
249 * door server threads for such doors can service *any*
250 * non-private door. We use non-private door for legacy sysevent
251 * alone.
253 if (tp->thr_mod == fmd.d_rmod && tp->thr_func == &fmd_door_server)
254 tp->thr_mod = (fmd_module_t *)hdl;
256 if ((mp = tp->thr_mod) != (fmd_module_t *)hdl) {
257 fmd_api_error(mp, EFMD_HDL_INVAL,
258 "client handle %p is not valid\n", (void *)hdl);
261 if (mp->mod_flags & FMD_MOD_FAIL) {
262 fmd_api_error(mp, EFMD_MOD_FAIL,
263 "module has experienced an unrecoverable error\n");
266 return (mp);
270 * fmd_api_module_lock() is used as a wrapper around fmd_module_lock() and a
271 * common prologue to each fmd_api.c routine. It verifies that the handle is
272 * valid and owned by the current server thread, locks the handle, and then
273 * verifies that the caller is performing an operation on a registered handle.
274 * If any tests fail, the entire API call is aborted by fmd_api_error().
276 static fmd_module_t *
277 fmd_api_module_lock(fmd_hdl_t *hdl)
279 fmd_module_t *mp = fmd_api_module(hdl);
281 fmd_module_lock(mp);
283 if (mp->mod_info == NULL) {
284 fmd_api_error(mp, EFMD_HDL_NOTREG,
285 "client handle %p has not been registered\n", (void *)hdl);
288 return (mp);
292 * Utility function for API entry points that accept fmd_case_t's. We cast cp
293 * to fmd_case_impl_t and check to make sure the case is owned by the caller.
295 static fmd_case_impl_t *
296 fmd_api_case_impl(fmd_module_t *mp, fmd_case_t *cp)
298 fmd_case_impl_t *cip = (fmd_case_impl_t *)cp;
300 if (cip == NULL || cip->ci_mod != mp) {
301 fmd_api_error(mp, EFMD_CASE_OWNER,
302 "case %p is invalid or not owned by caller\n", (void *)cip);
305 return (cip);
309 * Utility function for API entry points that accept fmd_xprt_t's. We cast xp
310 * to fmd_transport_t and check to make sure the case is owned by the caller.
311 * Note that we could make this check safer by actually walking mp's transport
312 * list, but that requires holding the module lock and this routine needs to be
313 * MT-hot w.r.t. auxiliary module threads. Ultimately any loadable module can
314 * cause us to crash anyway, so we optimize for scalability over safety here.
316 static fmd_xprt_impl_t *
317 fmd_api_transport_impl(fmd_hdl_t *hdl, fmd_xprt_t *xp)
319 fmd_module_t *mp = fmd_api_module(hdl);
320 fmd_xprt_impl_t *xip = (fmd_xprt_impl_t *)xp;
322 if (xip == NULL || xip->xi_queue->eq_mod != mp) {
323 fmd_api_error(mp, EFMD_XPRT_OWNER,
324 "xprt %p is invalid or not owned by caller\n", (void *)xp);
327 return (xip);
331 * fmd_hdl_register() is the one function which cannot use fmd_api_error() to
332 * report errors, because that routine causes the module to abort. Failure to
333 * register is instead handled by having fmd_hdl_register() return an error to
334 * the _fmd_init() function and then detecting no registration when it returns.
335 * So we use this routine for fmd_hdl_register() error paths instead.
337 static int
338 fmd_hdl_register_error(fmd_module_t *mp, int err)
340 if (fmd_module_locked(mp))
341 fmd_module_unlock(mp);
343 fmd_api_xerror(mp, err, "failed to register");
344 return (fmd_set_errno(err));
347 static void
348 fmd_hdl_nop(void)
350 /* empty function for use with unspecified module entry points */
354 fmd_hdl_register(fmd_hdl_t *hdl, int version, const fmd_hdl_info_t *mip)
356 fmd_thread_t *tp = pthread_getspecific(fmd.d_key);
357 fmd_module_t *mp = tp->thr_mod;
359 const fmd_prop_t *prop;
360 const fmd_conf_path_t *pap;
361 fmd_conf_formal_t *cfp;
362 fmd_hdl_ops_t ops;
364 const char *conf = NULL;
365 char buf[PATH_MAX];
366 int i;
368 if (mp != (fmd_module_t *)hdl)
369 return (fmd_hdl_register_error(mp, EFMD_HDL_INVAL));
371 fmd_module_lock(mp);
374 * First perform some sanity checks on our input. The API version must
375 * be supported by FMD and the handle can only be registered once by
376 * the module thread to which we assigned this client handle. The info
377 * provided for the handle must be valid and have the minimal settings.
379 if (version > FMD_API_VERSION_5)
380 return (fmd_hdl_register_error(mp, EFMD_VER_NEW));
382 if (version < FMD_API_VERSION_1)
383 return (fmd_hdl_register_error(mp, EFMD_VER_OLD));
385 if (mp->mod_conf != NULL)
386 return (fmd_hdl_register_error(mp, EFMD_HDL_REG));
388 if (pthread_self() != mp->mod_thread->thr_tid)
389 return (fmd_hdl_register_error(mp, EFMD_HDL_TID));
391 if (mip == NULL || mip->fmdi_desc == NULL ||
392 mip->fmdi_vers == NULL || mip->fmdi_ops == NULL)
393 return (fmd_hdl_register_error(mp, EFMD_HDL_INFO));
396 * Copy the module's ops vector into a local variable to account for
397 * changes in the module ABI. Then if any of the optional entry points
398 * are NULL, set them to nop so we don't have to check before calling.
400 bzero(&ops, sizeof (ops));
402 if (version < FMD_API_VERSION_3)
403 bcopy(mip->fmdi_ops, &ops, offsetof(fmd_hdl_ops_t, fmdo_send));
404 else if (version < FMD_API_VERSION_4)
405 bcopy(mip->fmdi_ops, &ops,
406 offsetof(fmd_hdl_ops_t, fmdo_topo));
407 else
408 bcopy(mip->fmdi_ops, &ops, sizeof (ops));
410 if (ops.fmdo_recv == NULL)
411 ops.fmdo_recv = (void (*)())fmd_hdl_nop;
412 if (ops.fmdo_timeout == NULL)
413 ops.fmdo_timeout = (void (*)())fmd_hdl_nop;
414 if (ops.fmdo_close == NULL)
415 ops.fmdo_close = (void (*)())fmd_hdl_nop;
416 if (ops.fmdo_stats == NULL)
417 ops.fmdo_stats = (void (*)())fmd_hdl_nop;
418 if (ops.fmdo_gc == NULL)
419 ops.fmdo_gc = (void (*)())fmd_hdl_nop;
420 if (ops.fmdo_send == NULL)
421 ops.fmdo_send = (int (*)())fmd_hdl_nop;
422 if (ops.fmdo_topo == NULL)
423 ops.fmdo_topo = (void (*)())fmd_hdl_nop;
426 * Make two passes through the property array to initialize the formals
427 * to use for processing the module's .conf file. In the first pass,
428 * we validate the types and count the number of properties. In the
429 * second pass we copy the strings and fill in the appropriate ops.
431 for (prop = mip->fmdi_props, i = 0; prop != NULL &&
432 prop->fmdp_name != NULL; prop++, i++) {
433 if (prop->fmdp_type >=
434 sizeof (_fmd_prop_ops) / sizeof (_fmd_prop_ops[0])) {
435 fmd_api_xerror(mp, EFMD_HDL_PROP,
436 "property %s uses invalid type %u\n",
437 prop->fmdp_name, prop->fmdp_type);
438 return (fmd_hdl_register_error(mp, EFMD_HDL_PROP));
442 mp->mod_argc = i;
443 mp->mod_argv = fmd_zalloc(sizeof (fmd_conf_formal_t) * i, FMD_SLEEP);
445 prop = mip->fmdi_props;
446 cfp = mp->mod_argv;
448 for (i = 0; i < mp->mod_argc; i++, prop++, cfp++) {
449 cfp->cf_name = fmd_strdup(prop->fmdp_name, FMD_SLEEP);
450 cfp->cf_ops = _fmd_prop_ops[prop->fmdp_type];
451 cfp->cf_default = fmd_strdup(prop->fmdp_defv, FMD_SLEEP);
455 * If this module came from an on-disk file, compute the name of the
456 * corresponding .conf file and parse properties from it if it exists.
458 if (mp->mod_path != NULL) {
459 (void) strlcpy(buf, mp->mod_path, sizeof (buf));
460 (void) fmd_strdirname(buf);
462 (void) strlcat(buf, "/", sizeof (buf));
463 (void) strlcat(buf, mp->mod_name, sizeof (buf));
464 (void) strlcat(buf, ".conf", sizeof (buf));
466 if (access(buf, F_OK) == 0)
467 conf = buf;
470 if ((mp->mod_conf = fmd_conf_open(conf,
471 mp->mod_argc, mp->mod_argv, 0)) == NULL)
472 return (fmd_hdl_register_error(mp, EFMD_MOD_CONF));
474 fmd_conf_propagate(fmd.d_conf, mp->mod_conf, mp->mod_name);
477 * Look up the list of the libdiagcode dictionaries associated with the
478 * module. If none were specified, use the value from daemon's config.
479 * We only fail if the module specified an explicit dictionary.
481 (void) fmd_conf_getprop(mp->mod_conf, FMD_PROP_DICTIONARIES, &pap);
482 if (pap->cpa_argc == 0 && mp->mod_ops == &fmd_bltin_ops)
483 (void) fmd_conf_getprop(fmd.d_conf, "self.dict", &pap);
485 for (i = 0; i < pap->cpa_argc; i++) {
486 if (fmd_module_dc_opendict(mp, pap->cpa_argv[i]) != 0) {
487 fmd_api_xerror(mp, errno,
488 "failed to open dictionary %s", pap->cpa_argv[i]);
489 return (fmd_hdl_register_error(mp, EFMD_MOD_CONF));
494 * Make a copy of the handle information and store it in mod_info. We
495 * do not need to bother copying fmdi_props since they're already read.
497 mp->mod_info = fmd_alloc(sizeof (fmd_hdl_info_t), FMD_SLEEP);
498 mp->mod_info->fmdi_desc = fmd_strdup(mip->fmdi_desc, FMD_SLEEP);
499 mp->mod_info->fmdi_vers = fmd_strdup(mip->fmdi_vers, FMD_SLEEP);
500 mp->mod_info->fmdi_ops = fmd_alloc(sizeof (fmd_hdl_ops_t), FMD_SLEEP);
501 bcopy(&ops, (void *)mp->mod_info->fmdi_ops, sizeof (fmd_hdl_ops_t));
502 mp->mod_info->fmdi_props = NULL;
505 * Store a copy of module version in mp for fmd_scheme_fmd_present()
507 if (mp->mod_vers == NULL)
508 mp->mod_vers = fmd_strdup(mip->fmdi_vers, FMD_SLEEP);
511 * Allocate an FMRI representing this module. We'll use this later
512 * if the module decides to publish any events (e.g. list.suspects).
514 mp->mod_fmri = fmd_protocol_fmri_module(mp);
517 * Any subscriptions specified in the conf file are now stored in the
518 * corresponding property. Add all of these to the dispatch queue.
520 (void) fmd_conf_getprop(mp->mod_conf, FMD_PROP_SUBSCRIPTIONS, &pap);
522 for (i = 0; i < pap->cpa_argc; i++) {
523 fmd_dispq_insert(fmd.d_disp, mp->mod_queue, pap->cpa_argv[i]);
524 fmd_xprt_subscribe_all(pap->cpa_argv[i]);
528 * Unlock the module and restore any pre-existing module checkpoint.
529 * If the checkpoint is missing or corrupt, we just keep going.
531 fmd_module_unlock(mp);
532 fmd_ckpt_restore(mp);
533 return (0);
537 * If an auxiliary thread exists for the specified module at unregistration
538 * time, send it an asynchronous cancellation to force it to exit and then
539 * join with it (we expect this to either succeed quickly or return ESRCH).
540 * Once this is complete we can destroy the associated fmd_thread_t data.
542 static void
543 fmd_module_thrcancel(fmd_idspace_t *ids, id_t id, fmd_module_t *mp)
545 fmd_thread_t *tp = fmd_idspace_getspecific(ids, id);
548 * Door service threads are not cancellable (worse - if they're
549 * waiting in door_return then that is interrupted, but they then spin
550 * endlessly!). Non-private door service threads are not tracked
551 * in the module thread idspace so it's only private server threads
552 * created via fmd_doorthr_create that we'll encounter. In most
553 * cases the module _fini should have tidied up (e.g., calling
554 * sysevent_evc_unbind which will cleanup door threads if
555 * sysevent_evc_xsubscribe was used). One case that does not
556 * clean up is sysev_fini which explicitly does not unbind the
557 * channel, so we must skip any remaining door threads here.
559 if (tp->thr_isdoor) {
560 fmd_dprintf(FMD_DBG_MOD, "not cancelling %s private door "
561 "thread %u\n", mp->mod_name, tp->thr_tid);
562 fmd_thread_destroy(tp, FMD_THREAD_NOJOIN);
563 return;
566 fmd_dprintf(FMD_DBG_MOD, "cancelling %s auxiliary thread %u\n",
567 mp->mod_name, tp->thr_tid);
569 ASSERT(tp->thr_tid == id);
570 (void) pthread_cancel(tp->thr_tid);
571 (void) pthread_join(tp->thr_tid, NULL);
573 fmd_thread_destroy(tp, FMD_THREAD_NOJOIN);
576 void
577 fmd_module_unregister(fmd_module_t *mp)
579 fmd_conf_formal_t *cfp = mp->mod_argv;
580 const fmd_conf_path_t *pap;
581 fmd_case_t *cp;
582 fmd_xprt_t *xp;
583 int i;
585 TRACE((FMD_DBG_MOD, "unregister %p (%s)", (void *)mp, mp->mod_name));
586 ASSERT(fmd_module_locked(mp));
589 * If any transports are still open, they have send threads that are
590 * using the module handle: shut them down and join with these threads.
592 while ((xp = fmd_list_next(&mp->mod_transports)) != NULL)
593 fmd_xprt_destroy(xp);
596 * If any auxiliary threads exist, they may be using our module handle,
597 * and therefore could cause a fault as soon as we start destroying it.
598 * Module writers should clean up any threads before unregistering: we
599 * forcibly cancel any remaining auxiliary threads before proceeding.
601 fmd_idspace_apply(mp->mod_threads,
602 (void (*)())fmd_module_thrcancel, mp);
604 if (mp->mod_error == 0)
605 fmd_ckpt_save(mp); /* take one more checkpoint if needed */
608 * Delete any cases associated with the module (UNSOLVED, SOLVED, or
609 * CLOSE_WAIT) as if fmdo_close() has finished processing them.
611 while ((cp = fmd_list_next(&mp->mod_cases)) != NULL)
612 fmd_case_delete(cp);
614 fmd_ustat_delete_references(mp->mod_ustat);
615 (void) fmd_conf_getprop(mp->mod_conf, FMD_PROP_SUBSCRIPTIONS, &pap);
617 for (i = 0; i < pap->cpa_argc; i++) {
618 fmd_xprt_unsubscribe_all(pap->cpa_argv[i]);
619 fmd_dispq_delete(fmd.d_disp, mp->mod_queue, pap->cpa_argv[i]);
622 fmd_conf_close(mp->mod_conf);
623 mp->mod_conf = NULL;
625 for (i = 0; i < mp->mod_argc; i++, cfp++) {
626 fmd_strfree((char *)cfp->cf_name);
627 fmd_strfree((char *)cfp->cf_default);
630 fmd_free(mp->mod_argv, sizeof (fmd_conf_formal_t) * mp->mod_argc);
631 mp->mod_argv = NULL;
632 mp->mod_argc = 0;
634 nvlist_free(mp->mod_fmri);
635 mp->mod_fmri = NULL;
637 fmd_strfree((char *)mp->mod_info->fmdi_desc);
638 fmd_strfree((char *)mp->mod_info->fmdi_vers);
639 fmd_free((void *)mp->mod_info->fmdi_ops, sizeof (fmd_hdl_ops_t));
640 fmd_free(mp->mod_info, sizeof (fmd_hdl_info_t));
641 mp->mod_info = NULL;
643 fmd_eventq_abort(mp->mod_queue);
646 void
647 fmd_hdl_unregister(fmd_hdl_t *hdl)
649 fmd_module_t *mp = fmd_api_module_lock(hdl);
650 fmd_module_unregister(mp);
651 fmd_module_unlock(mp);
654 void
655 fmd_hdl_subscribe(fmd_hdl_t *hdl, const char *class)
657 fmd_module_t *mp = fmd_api_module_lock(hdl);
659 if (fmd_conf_setprop(mp->mod_conf,
660 FMD_PROP_SUBSCRIPTIONS, class) == 0) {
661 fmd_dispq_insert(fmd.d_disp, mp->mod_queue, class);
662 fmd_xprt_subscribe_all(class);
665 fmd_module_unlock(mp);
669 void
670 fmd_hdl_unsubscribe(fmd_hdl_t *hdl, const char *class)
672 fmd_module_t *mp = fmd_api_module_lock(hdl);
674 if (fmd_conf_delprop(mp->mod_conf,
675 FMD_PROP_SUBSCRIPTIONS, class) == 0) {
676 fmd_xprt_unsubscribe_all(class);
677 fmd_dispq_delete(fmd.d_disp, mp->mod_queue, class);
680 fmd_module_unlock(mp);
681 fmd_eventq_cancel(mp->mod_queue, FMD_EVT_PROTOCOL, (void *)class);
684 void
685 fmd_hdl_setspecific(fmd_hdl_t *hdl, void *spec)
687 fmd_module_t *mp = fmd_api_module_lock(hdl);
689 mp->mod_spec = spec;
690 fmd_module_unlock(mp);
693 void *
694 fmd_hdl_getspecific(fmd_hdl_t *hdl)
696 fmd_module_t *mp = fmd_api_module_lock(hdl);
697 void *spec = mp->mod_spec;
699 fmd_module_unlock(mp);
700 return (spec);
703 void
704 fmd_hdl_opendict(fmd_hdl_t *hdl, const char *dict)
706 fmd_module_t *mp = fmd_api_module_lock(hdl);
707 const fmd_conf_path_t *pap;
708 int i;
711 * Update the dictionary property in order to preserve the list of
712 * pathnames and expand any % tokens in the path. Then retrieve the
713 * new dictionary names from cpa_argv[] and open them one at a time.
715 (void) fmd_conf_setprop(mp->mod_conf, FMD_PROP_DICTIONARIES, dict);
716 (void) fmd_conf_getprop(mp->mod_conf, FMD_PROP_DICTIONARIES, &pap);
718 ASSERT(pap->cpa_argc > mp->mod_dictc);
720 for (i = mp->mod_dictc; i < pap->cpa_argc; i++) {
721 if (fmd_module_dc_opendict(mp, pap->cpa_argv[i]) != 0) {
722 fmd_api_error(mp, EFMD_MOD_DICT,
723 "failed to open dictionary %s for module %s",
724 pap->cpa_argv[i], mp->mod_name);
728 fmd_module_unlock(mp);
731 topo_hdl_t *
732 fmd_hdl_topo_hold(fmd_hdl_t *hdl, int v)
734 fmd_module_t *mp = fmd_api_module_lock(hdl);
735 topo_hdl_t *thp;
737 if (v != TOPO_VERSION) {
738 fmd_api_error(mp, EFMD_MOD_TOPO, "libtopo version mismatch: "
739 "fmd version %d != client version %d\n", TOPO_VERSION, v);
742 thp = fmd_module_topo_hold(mp);
743 ASSERT(thp != NULL);
745 fmd_module_unlock(mp);
746 return (thp);
749 void
750 fmd_hdl_topo_rele(fmd_hdl_t *hdl, topo_hdl_t *thp)
752 fmd_module_t *mp = fmd_api_module_lock(hdl);
754 if (fmd_module_topo_rele(mp, thp) != 0)
755 fmd_api_error(mp, EFMD_MOD_TOPO, "failed to release invalid "
756 "topo handle: %p\n", (void *)thp);
758 fmd_module_unlock(mp);
761 static void *
762 fmd_hdl_alloc_locked(fmd_module_t *mp, size_t size, int flags)
764 void *data;
766 if (mp->mod_stats->ms_memlimit.fmds_value.ui64 -
767 mp->mod_stats->ms_memtotal.fmds_value.ui64 < size) {
768 fmd_api_error(mp, EFMD_HDL_NOMEM, "%s's allocation of %lu "
769 "bytes exceeds module memory limit (%llu)\n",
770 mp->mod_name, (ulong_t)size, (u_longlong_t)
771 mp->mod_stats->ms_memtotal.fmds_value.ui64);
774 if ((data = fmd_alloc(size, flags)) != NULL)
775 mp->mod_stats->ms_memtotal.fmds_value.ui64 += size;
777 return (data);
780 void *
781 fmd_hdl_alloc(fmd_hdl_t *hdl, size_t size, int flags)
783 fmd_module_t *mp = fmd_api_module_lock(hdl);
784 void *data;
786 data = fmd_hdl_alloc_locked(mp, size, flags);
788 fmd_module_unlock(mp);
789 return (data);
792 void *
793 fmd_hdl_zalloc(fmd_hdl_t *hdl, size_t size, int flags)
795 void *data = fmd_hdl_alloc(hdl, size, flags);
797 if (data != NULL)
798 bzero(data, size);
800 return (data);
803 static void
804 fmd_hdl_free_locked(fmd_module_t *mp, void *data, size_t size)
806 fmd_free(data, size);
807 mp->mod_stats->ms_memtotal.fmds_value.ui64 -= size;
810 void
811 fmd_hdl_free(fmd_hdl_t *hdl, void *data, size_t size)
813 fmd_module_t *mp = fmd_api_module_lock(hdl);
815 fmd_hdl_free_locked(mp, data, size);
817 fmd_module_unlock(mp);
820 char *
821 fmd_hdl_strdup(fmd_hdl_t *hdl, const char *s, int flags)
823 char *p;
825 if (s != NULL)
826 p = fmd_hdl_alloc(hdl, strlen(s) + 1, flags);
827 else
828 p = NULL;
830 if (p != NULL)
831 (void) strcpy(p, s);
833 return (p);
836 void
837 fmd_hdl_strfree(fmd_hdl_t *hdl, char *s)
839 if (s != NULL)
840 fmd_hdl_free(hdl, s, strlen(s) + 1);
843 void
844 fmd_hdl_vabort(fmd_hdl_t *hdl, const char *format, va_list ap)
846 fmd_api_verror(fmd_api_module_lock(hdl), EFMD_HDL_ABORT, format, ap);
849 /*PRINTFLIKE2*/
850 void
851 fmd_hdl_abort(fmd_hdl_t *hdl, const char *format, ...)
853 fmd_module_t *mp = fmd_api_module_lock(hdl);
854 va_list ap;
856 va_start(ap, format);
857 fmd_api_verror(mp, EFMD_HDL_ABORT, format, ap);
858 va_end(ap);
861 void
862 fmd_hdl_verror(fmd_hdl_t *hdl, const char *format, va_list ap)
864 fmd_module_t *mp = fmd_api_module_lock(hdl);
865 fmd_api_vxerror(mp, errno, format, ap);
866 fmd_module_unlock(mp);
869 /*PRINTFLIKE2*/
870 void
871 fmd_hdl_error(fmd_hdl_t *hdl, const char *format, ...)
873 va_list ap;
875 va_start(ap, format);
876 fmd_hdl_verror(hdl, format, ap);
877 va_end(ap);
880 void
881 fmd_hdl_vdebug(fmd_hdl_t *hdl, const char *format, va_list ap)
883 fmd_module_t *mp = fmd_api_module_lock(hdl);
885 char *msg;
886 size_t len;
887 char c;
889 if (!(fmd.d_hdl_debug)) {
890 mp->mod_stats->ms_debugdrop.fmds_value.ui64++;
891 fmd_module_unlock(mp);
892 return;
895 len = vsnprintf(&c, 1, format, ap);
897 if ((msg = fmd_alloc(len + 2, FMD_NOSLEEP)) == NULL) {
898 mp->mod_stats->ms_debugdrop.fmds_value.ui64++;
899 fmd_module_unlock(mp);
900 return;
903 (void) vsnprintf(msg, len + 1, format, ap);
905 if (msg[len - 1] != '\n')
906 (void) strcpy(&msg[len], "\n");
908 if (fmd.d_hdl_dbout & FMD_DBOUT_STDERR) {
909 (void) pthread_mutex_lock(&fmd.d_err_lock);
910 (void) fprintf(stderr, "%s DEBUG: %s: %s",
911 fmd.d_pname, mp->mod_name, msg);
912 (void) pthread_mutex_unlock(&fmd.d_err_lock);
915 if (fmd.d_hdl_dbout & FMD_DBOUT_SYSLOG) {
916 syslog(LOG_DEBUG | LOG_DAEMON, "%s DEBUG: %s: %s",
917 fmd.d_pname, mp->mod_name, msg);
920 fmd_free(msg, len + 2);
921 fmd_module_unlock(mp);
924 /*PRINTFLIKE2*/
925 void
926 fmd_hdl_debug(fmd_hdl_t *hdl, const char *format, ...)
928 va_list ap;
930 va_start(ap, format);
931 fmd_hdl_vdebug(hdl, format, ap);
932 va_end(ap);
935 int32_t
936 fmd_prop_get_int32(fmd_hdl_t *hdl, const char *name)
938 fmd_module_t *mp = fmd_api_module_lock(hdl);
939 const fmd_conf_ops_t *ops = fmd_conf_gettype(mp->mod_conf, name);
940 int32_t value = 0;
942 if (ops == &fmd_conf_bool || ops == &fmd_conf_int32 ||
943 ops == &fmd_conf_uint32)
944 (void) fmd_conf_getprop(mp->mod_conf, name, &value);
945 else if (ops != NULL) {
946 fmd_api_error(mp, EFMD_PROP_TYPE,
947 "property %s is not of int32 type\n", name);
948 } else {
949 fmd_api_error(mp, EFMD_PROP_DEFN,
950 "property %s is not defined\n", name);
953 fmd_module_unlock(mp);
954 return (value);
957 int64_t
958 fmd_prop_get_int64(fmd_hdl_t *hdl, const char *name)
960 fmd_module_t *mp = fmd_api_module_lock(hdl);
961 const fmd_conf_ops_t *ops = fmd_conf_gettype(mp->mod_conf, name);
962 int64_t value = 0;
964 if (ops == &fmd_conf_int64 || ops == &fmd_conf_uint64 ||
965 ops == &fmd_conf_time || ops == &fmd_conf_size)
966 (void) fmd_conf_getprop(mp->mod_conf, name, &value);
967 else if (ops != NULL) {
968 fmd_api_error(mp, EFMD_PROP_TYPE,
969 "property %s is not of int64 type\n", name);
970 } else {
971 fmd_api_error(mp, EFMD_PROP_DEFN,
972 "property %s is not defined\n", name);
975 fmd_module_unlock(mp);
976 return (value);
979 char *
980 fmd_prop_get_string(fmd_hdl_t *hdl, const char *name)
982 fmd_module_t *mp = fmd_api_module_lock(hdl);
983 const fmd_conf_ops_t *ops = fmd_conf_gettype(mp->mod_conf, name);
984 char *value = NULL;
985 const char *s;
987 if (ops == &fmd_conf_string) {
988 (void) fmd_conf_getprop(mp->mod_conf, name, &s);
989 value = fmd_strdup(s, FMD_SLEEP);
990 } else if (ops != NULL) {
991 fmd_api_error(mp, EFMD_PROP_TYPE,
992 "property %s is not of string type\n", name);
993 } else {
994 fmd_api_error(mp, EFMD_PROP_DEFN,
995 "property %s is not defined\n", name);
998 fmd_module_unlock(mp);
999 return (value);
1002 void
1003 fmd_prop_free_string(fmd_hdl_t *hdl, char *s)
1005 fmd_module_t *mp = fmd_api_module_lock(hdl);
1006 fmd_strfree(s);
1007 fmd_module_unlock(mp);
1010 fmd_stat_t *
1011 fmd_stat_create(fmd_hdl_t *hdl, uint_t flags, uint_t argc, fmd_stat_t *argv)
1013 fmd_module_t *mp = fmd_api_module_lock(hdl);
1014 fmd_stat_t *ep, *sp;
1016 if (flags & ~FMD_STAT_ALLOC) {
1017 fmd_api_error(mp, EFMD_STAT_FLAGS,
1018 "invalid flags 0x%x passed to fmd_stat_create\n", flags);
1021 if ((sp = fmd_ustat_insert(mp->mod_ustat,
1022 flags | FMD_USTAT_VALIDATE, argc, argv, &ep)) == NULL) {
1023 fmd_api_error(mp, errno,
1024 "failed to publish stat '%s'", ep->fmds_name);
1027 fmd_module_unlock(mp);
1028 return (sp);
1031 void
1032 fmd_stat_destroy(fmd_hdl_t *hdl, uint_t argc, fmd_stat_t *argv)
1034 fmd_module_t *mp = fmd_api_module_lock(hdl);
1035 fmd_ustat_delete(mp->mod_ustat, argc, argv);
1036 fmd_module_unlock(mp);
1039 void
1040 fmd_stat_setstr(fmd_hdl_t *hdl, fmd_stat_t *sp, const char *s)
1042 char *str = fmd_strdup(s, FMD_SLEEP);
1043 fmd_module_t *mp = fmd_api_module_lock(hdl);
1045 if (sp->fmds_type != FMD_TYPE_STRING) {
1046 fmd_strfree(str);
1047 fmd_api_error(mp, EFMD_STAT_TYPE,
1048 "stat '%s' is not a string\n", sp->fmds_name);
1051 fmd_strfree(sp->fmds_value.str);
1052 sp->fmds_value.str = str;
1054 fmd_module_unlock(mp);
1057 fmd_case_t *
1058 fmd_case_open(fmd_hdl_t *hdl, void *data)
1060 fmd_module_t *mp = fmd_api_module_lock(hdl);
1061 fmd_case_t *cp = fmd_case_create(mp, NULL, data);
1062 fmd_module_unlock(mp);
1063 return (cp);
1066 fmd_case_t *
1067 fmd_case_open_uuid(fmd_hdl_t *hdl, const char *uuidstr, void *data)
1069 fmd_module_t *mp;
1070 fmd_case_t *cp;
1071 uint_t uuidlen;
1072 uuid_t uuid;
1074 mp = fmd_api_module_lock(hdl);
1076 (void) fmd_conf_getprop(fmd.d_conf, "uuidlen", &uuidlen);
1078 if (uuidstr == NULL) {
1079 fmd_api_error(mp, EFMD_CASE_INVAL, "NULL uuid string\n");
1080 } else if (strnlen(uuidstr, uuidlen + 1) != uuidlen) {
1081 fmd_api_error(mp, EFMD_CASE_INVAL, "invalid uuid string: '%s' "
1082 "(expected length %d)\n", uuidstr, uuidlen);
1083 } else if (uuid_parse((char *)uuidstr, uuid) == -1) {
1084 fmd_api_error(mp, EFMD_CASE_INVAL, "cannot parse uuid string: "
1085 "'%s'\n", uuidstr);
1088 if ((cp = fmd_case_hash_lookup(fmd.d_cases, uuidstr)) == NULL) {
1089 cp = fmd_case_create(mp, uuidstr, data);
1090 } else {
1091 fmd_case_rele(cp);
1092 cp = NULL;
1095 fmd_module_unlock(mp);
1096 return (cp); /* May be NULL iff case already exists */
1099 void
1100 fmd_case_reset(fmd_hdl_t *hdl, fmd_case_t *cp)
1102 fmd_module_t *mp = fmd_api_module_lock(hdl);
1103 fmd_case_impl_t *cip = fmd_api_case_impl(mp, cp);
1105 if (cip->ci_state >= FMD_CASE_SOLVED) {
1106 fmd_api_error(mp, EFMD_CASE_STATE, "cannot solve %s: "
1107 "case is already solved or closed\n", cip->ci_uuid);
1110 fmd_case_reset_suspects(cp);
1111 fmd_module_unlock(mp);
1114 void
1115 fmd_case_solve(fmd_hdl_t *hdl, fmd_case_t *cp)
1117 fmd_module_t *mp = fmd_api_module_lock(hdl);
1118 fmd_case_impl_t *cip = fmd_api_case_impl(mp, cp);
1120 if (cip->ci_state >= FMD_CASE_SOLVED) {
1121 fmd_api_error(mp, EFMD_CASE_STATE, "cannot solve %s: "
1122 "case is already solved or closed\n", cip->ci_uuid);
1125 fmd_case_transition(cp, FMD_CASE_SOLVED, FMD_CF_SOLVED);
1126 fmd_module_unlock(mp);
1129 void
1130 fmd_case_close(fmd_hdl_t *hdl, fmd_case_t *cp)
1132 fmd_module_t *mp = fmd_api_module_lock(hdl);
1134 (void) fmd_api_case_impl(mp, cp); /* validate 'cp' */
1135 fmd_case_transition(cp, FMD_CASE_CLOSE_WAIT, FMD_CF_ISOLATED);
1137 fmd_module_unlock(mp);
1140 const char *
1141 fmd_case_uuid(fmd_hdl_t *hdl, fmd_case_t *cp)
1143 fmd_module_t *mp = fmd_api_module_lock(hdl);
1144 fmd_case_impl_t *cip = fmd_api_case_impl(mp, cp);
1145 const char *uuid = cip->ci_uuid;
1147 fmd_module_unlock(mp);
1148 return (uuid);
1151 fmd_case_t *
1152 fmd_case_uulookup(fmd_hdl_t *hdl, const char *uuid)
1154 fmd_module_t *cmp, *mp = fmd_api_module_lock(hdl);
1155 fmd_case_t *cp = fmd_case_hash_lookup(fmd.d_cases, uuid);
1157 if (cp != NULL) {
1158 cmp = ((fmd_case_impl_t *)cp)->ci_mod;
1159 fmd_case_rele(cp);
1160 } else
1161 cmp = NULL;
1163 fmd_module_unlock(mp);
1164 return (cmp == mp ? cp : NULL);
1167 void
1168 fmd_case_uuclose(fmd_hdl_t *hdl, const char *uuid)
1170 fmd_module_t *mp = fmd_api_module_lock(hdl);
1171 fmd_case_t *cp = fmd_case_hash_lookup(fmd.d_cases, uuid);
1173 if (cp != NULL) {
1174 fmd_case_transition(cp, FMD_CASE_CLOSE_WAIT, FMD_CF_ISOLATED);
1175 fmd_case_rele(cp);
1178 fmd_module_unlock(mp);
1182 fmd_case_uuclosed(fmd_hdl_t *hdl, const char *uuid)
1184 fmd_module_t *mp = fmd_api_module_lock(hdl);
1185 fmd_case_t *cp = fmd_case_hash_lookup(fmd.d_cases, uuid);
1186 fmd_case_impl_t *cip = (fmd_case_impl_t *)cp;
1187 int rv = FMD_B_TRUE;
1189 if (cip != NULL) {
1190 rv = cip->ci_state >= FMD_CASE_CLOSE_WAIT;
1191 fmd_case_rele(cp);
1194 fmd_module_unlock(mp);
1195 return (rv);
1198 void
1199 fmd_case_uuresolved(fmd_hdl_t *hdl, const char *uuid)
1201 fmd_module_t *mp = fmd_api_module_lock(hdl);
1202 fmd_case_t *cp = fmd_case_hash_lookup(fmd.d_cases, uuid);
1204 if (cp != NULL) {
1205 fmd_case_impl_t *cip = (fmd_case_impl_t *)cp;
1207 * For a proxy, we notify the diagnosing side, and then
1208 * wait for it to send us back a list.resolved.
1210 if (cip->ci_xprt != NULL)
1211 fmd_xprt_uuresolved(cip->ci_xprt, cip->ci_uuid);
1212 else
1213 fmd_case_transition(cp, FMD_CASE_RESOLVED, 0);
1214 fmd_case_rele(cp);
1217 fmd_module_unlock(mp);
1221 fmd_case_uuisresolved(fmd_hdl_t *hdl, const char *uuid)
1223 fmd_module_t *mp = fmd_api_module_lock(hdl);
1224 fmd_case_t *cp = fmd_case_hash_lookup(fmd.d_cases, uuid);
1225 fmd_case_impl_t *cip = (fmd_case_impl_t *)cp;
1226 int rv = FMD_B_FALSE;
1228 if (cip != NULL) {
1229 rv = (cip->ci_state >= FMD_CASE_RESOLVED);
1230 fmd_case_rele(cp);
1233 fmd_module_unlock(mp);
1234 return (rv);
1237 static int
1238 fmd_case_instate(fmd_hdl_t *hdl, fmd_case_t *cp, uint_t state)
1240 fmd_module_t *mp = fmd_api_module_lock(hdl);
1241 fmd_case_impl_t *cip = fmd_api_case_impl(mp, cp);
1242 int rv = cip->ci_state >= state;
1244 fmd_module_unlock(mp);
1245 return (rv);
1249 fmd_case_solved(fmd_hdl_t *hdl, fmd_case_t *cp)
1251 return (fmd_case_instate(hdl, cp, FMD_CASE_SOLVED));
1255 fmd_case_closed(fmd_hdl_t *hdl, fmd_case_t *cp)
1257 return (fmd_case_instate(hdl, cp, FMD_CASE_CLOSE_WAIT));
1260 void
1261 fmd_case_add_ereport(fmd_hdl_t *hdl, fmd_case_t *cp, fmd_event_t *ep)
1263 fmd_module_t *mp = fmd_api_module_lock(hdl);
1265 (void) fmd_api_case_impl(mp, cp); /* validate 'cp' */
1267 if (fmd_case_insert_event(cp, ep))
1268 mp->mod_stats->ms_accepted.fmds_value.ui64++;
1270 fmd_module_unlock(mp);
1273 void
1274 fmd_case_add_serd(fmd_hdl_t *hdl, fmd_case_t *cp, const char *name)
1276 fmd_module_t *mp = fmd_api_module_lock(hdl);
1277 fmd_serd_elem_t *sep;
1278 fmd_serd_eng_t *sgp;
1280 if ((sgp = fmd_serd_eng_lookup(&mp->mod_serds, name)) == NULL) {
1281 fmd_api_error(mp, EFMD_SERD_NAME,
1282 "failed to add events from serd engine '%s'", name);
1285 (void) fmd_api_case_impl(mp, cp); /* validate 'cp' */
1287 for (sep = fmd_list_next(&sgp->sg_list);
1288 sep != NULL; sep = fmd_list_next(sep)) {
1289 if (fmd_case_insert_event(cp, sep->se_event))
1290 mp->mod_stats->ms_accepted.fmds_value.ui64++;
1293 fmd_module_unlock(mp);
1296 void
1297 fmd_case_add_suspect(fmd_hdl_t *hdl, fmd_case_t *cp, nvlist_t *nvl)
1299 fmd_module_t *mp = fmd_api_module_lock(hdl);
1300 fmd_case_impl_t *cip = fmd_api_case_impl(mp, cp);
1301 char *class;
1302 topo_hdl_t *thp;
1303 int err;
1304 nvlist_t *rsrc = NULL, *asru_prop = NULL, *asru = NULL, *fru = NULL;
1305 char *loc = NULL, *serial = NULL;
1307 if (cip->ci_state >= FMD_CASE_SOLVED) {
1308 fmd_api_error(mp, EFMD_CASE_STATE, "cannot add suspect to "
1309 "%s: case is already solved or closed\n", cip->ci_uuid);
1312 if (nvlist_lookup_string(nvl, FM_CLASS, &class) != 0 ||
1313 class == NULL || *class == '\0') {
1314 fmd_api_error(mp, EFMD_CASE_EVENT, "cannot add suspect to "
1315 "%s: suspect event is missing a class\n", cip->ci_uuid);
1318 thp = fmd_module_topo_hold(mp);
1319 (void) nvlist_lookup_nvlist(nvl, FM_FAULT_RESOURCE, &rsrc);
1320 (void) nvlist_lookup_nvlist(nvl, FM_FAULT_ASRU, &asru);
1321 (void) nvlist_lookup_nvlist(nvl, FM_FAULT_FRU, &fru);
1322 if (rsrc != NULL) {
1323 if (strncmp(class, "defect", 6) == 0) {
1324 if (asru == NULL && topo_fmri_getprop(thp, rsrc,
1325 TOPO_PGROUP_IO, TOPO_IO_MODULE, rsrc,
1326 &asru_prop, &err) == 0 &&
1327 nvlist_lookup_nvlist(asru_prop, TOPO_PROP_VAL_VAL,
1328 &asru) == 0) {
1329 (void) nvlist_add_nvlist(nvl, FM_FAULT_ASRU,
1330 asru);
1331 nvlist_free(asru_prop);
1332 (void) nvlist_lookup_nvlist(nvl, FM_FAULT_ASRU,
1333 &asru);
1335 } else {
1336 if (topo_fmri_asru(thp, rsrc, &asru, &err) == 0) {
1337 (void) nvlist_remove(nvl, FM_FAULT_ASRU,
1338 DATA_TYPE_NVLIST);
1339 (void) nvlist_add_nvlist(nvl, FM_FAULT_ASRU,
1340 asru);
1341 nvlist_free(asru);
1342 (void) nvlist_lookup_nvlist(nvl, FM_FAULT_ASRU,
1343 &asru);
1345 if (topo_fmri_fru(thp, rsrc, &fru, &err) == 0) {
1346 (void) nvlist_remove(nvl, FM_FAULT_FRU,
1347 DATA_TYPE_NVLIST);
1348 (void) nvlist_add_nvlist(nvl, FM_FAULT_FRU,
1349 fru);
1350 nvlist_free(fru);
1351 (void) nvlist_lookup_nvlist(nvl, FM_FAULT_FRU,
1352 &fru);
1358 * Try to find the location label for this resource
1360 if (strncmp(class, "defect", 6) != 0) {
1361 if (fru != NULL)
1362 (void) topo_fmri_label(thp, fru, &loc, &err);
1363 else if (rsrc != NULL)
1364 (void) topo_fmri_label(thp, rsrc, &loc, &err);
1365 if (loc != NULL) {
1366 (void) nvlist_remove(nvl, FM_FAULT_LOCATION,
1367 DATA_TYPE_STRING);
1368 (void) nvlist_add_string(nvl, FM_FAULT_LOCATION, loc);
1369 topo_hdl_strfree(thp, loc);
1374 * In some cases, serial information for the resource will not be
1375 * available at enumeration but may instead be available by invoking
1376 * a dynamic property method on the FRU. In order to ensure the serial
1377 * number is persisted properly in the ASRU cache, we'll fetch the
1378 * property, if it exists, and add it to the resource and fru fmris.
1379 * If the DE has not listed a fru in the suspect, see if we can
1380 * retrieve the serial from the resource instead.
1382 if (fru != NULL) {
1383 (void) topo_fmri_serial(thp, fru, &serial, &err);
1384 if (serial != NULL) {
1385 (void) nvlist_add_string(fru, "serial", serial);
1386 topo_hdl_strfree(thp, serial);
1390 err = fmd_module_topo_rele(mp, thp);
1391 ASSERT(err == 0);
1393 fmd_case_insert_suspect(cp, nvl);
1394 fmd_module_unlock(mp);
1397 void
1398 fmd_case_setspecific(fmd_hdl_t *hdl, fmd_case_t *cp, void *data)
1400 fmd_module_t *mp = fmd_api_module_lock(hdl);
1401 fmd_case_impl_t *cip = fmd_api_case_impl(mp, cp);
1403 (void) pthread_mutex_lock(&cip->ci_lock);
1404 cip->ci_data = data;
1405 (void) pthread_mutex_unlock(&cip->ci_lock);
1407 fmd_module_unlock(mp);
1410 void *
1411 fmd_case_getspecific(fmd_hdl_t *hdl, fmd_case_t *cp)
1413 fmd_module_t *mp = fmd_api_module_lock(hdl);
1414 fmd_case_impl_t *cip = fmd_api_case_impl(mp, cp);
1415 void *data;
1417 (void) pthread_mutex_lock(&cip->ci_lock);
1418 data = cip->ci_data;
1419 (void) pthread_mutex_unlock(&cip->ci_lock);
1421 fmd_module_unlock(mp);
1422 return (data);
1425 void
1426 fmd_case_setprincipal(fmd_hdl_t *hdl, fmd_case_t *cp, fmd_event_t *ep)
1428 fmd_module_t *mp = fmd_api_module_lock(hdl);
1430 (void) fmd_api_case_impl(mp, cp); /* validate 'cp' */
1432 if (fmd_case_insert_principal(cp, ep))
1433 mp->mod_stats->ms_accepted.fmds_value.ui64++;
1435 fmd_module_unlock(mp);
1438 fmd_event_t *
1439 fmd_case_getprincipal(fmd_hdl_t *hdl, fmd_case_t *cp)
1441 fmd_module_t *mp = fmd_api_module_lock(hdl);
1442 fmd_case_impl_t *cip = fmd_api_case_impl(mp, cp);
1443 fmd_event_t *ep;
1445 (void) pthread_mutex_lock(&cip->ci_lock);
1446 ep = cip->ci_principal;
1447 (void) pthread_mutex_unlock(&cip->ci_lock);
1449 fmd_module_unlock(mp);
1450 return (ep);
1453 fmd_case_t *
1454 fmd_case_next(fmd_hdl_t *hdl, fmd_case_t *cp)
1456 fmd_module_t *mp = fmd_api_module_lock(hdl);
1458 if (cp != NULL)
1459 cp = fmd_list_next(fmd_api_case_impl(mp, cp));
1460 else
1461 cp = fmd_list_next(&mp->mod_cases);
1463 fmd_module_unlock(mp);
1464 return (cp);
1467 fmd_case_t *
1468 fmd_case_prev(fmd_hdl_t *hdl, fmd_case_t *cp)
1470 fmd_module_t *mp = fmd_api_module_lock(hdl);
1472 if (cp != NULL)
1473 cp = fmd_list_prev(fmd_api_case_impl(mp, cp));
1474 else
1475 cp = fmd_list_prev(&mp->mod_cases);
1477 fmd_module_unlock(mp);
1478 return (cp);
1482 * Utility function for fmd_buf_* routines. If a case is specified, use the
1483 * case's ci_bufs hash; otherwise use the module's global mod_bufs hash.
1485 static fmd_buf_hash_t *
1486 fmd_buf_gethash(fmd_module_t *mp, fmd_case_t *cp)
1488 return (cp ? &fmd_api_case_impl(mp, cp)->ci_bufs : &mp->mod_bufs);
1491 void
1492 fmd_buf_create(fmd_hdl_t *hdl, fmd_case_t *cp, const char *name, size_t size)
1494 fmd_module_t *mp = fmd_api_module_lock(hdl);
1495 fmd_buf_hash_t *bhp = fmd_buf_gethash(mp, cp);
1496 fmd_buf_t *bp = fmd_buf_lookup(bhp, name);
1498 if (bp == NULL) {
1499 if (fmd_strbadid(name, FMD_B_TRUE) != NULL || size == 0) {
1500 fmd_api_error(mp, EFMD_BUF_INVAL, "cannot create '%s' "
1501 "(size %lu): %s\n", name, (ulong_t)size,
1502 fmd_strerror(EFMD_BUF_INVAL));
1505 if (mp->mod_stats->ms_buflimit.fmds_value.ui64 -
1506 mp->mod_stats->ms_buftotal.fmds_value.ui64 < size) {
1507 fmd_api_error(mp, EFMD_BUF_LIMIT, "cannot create '%s': "
1508 "buf limit exceeded (%llu)\n", name, (u_longlong_t)
1509 mp->mod_stats->ms_buflimit.fmds_value.ui64);
1512 mp->mod_stats->ms_buftotal.fmds_value.ui64 += size;
1513 bp = fmd_buf_insert(bhp, name, size);
1515 } else {
1516 fmd_api_error(mp, EFMD_BUF_EXISTS,
1517 "cannot create '%s': buffer already exists\n", name);
1520 if (cp != NULL)
1521 fmd_case_setdirty(cp);
1522 else
1523 fmd_module_setdirty(mp);
1525 fmd_module_unlock(mp);
1528 void
1529 fmd_buf_destroy(fmd_hdl_t *hdl, fmd_case_t *cp, const char *name)
1531 fmd_module_t *mp = fmd_api_module_lock(hdl);
1532 fmd_buf_hash_t *bhp = fmd_buf_gethash(mp, cp);
1533 fmd_buf_t *bp = fmd_buf_lookup(bhp, name);
1535 if (bp != NULL) {
1536 mp->mod_stats->ms_buftotal.fmds_value.ui64 -= bp->buf_size;
1537 fmd_buf_delete(bhp, name);
1539 if (cp != NULL)
1540 fmd_case_setdirty(cp);
1541 else
1542 fmd_module_setdirty(mp);
1545 fmd_module_unlock(mp);
1548 void
1549 fmd_buf_read(fmd_hdl_t *hdl, fmd_case_t *cp,
1550 const char *name, void *buf, size_t size)
1552 fmd_module_t *mp = fmd_api_module_lock(hdl);
1553 fmd_buf_t *bp = fmd_buf_lookup(fmd_buf_gethash(mp, cp), name);
1555 if (bp == NULL) {
1556 fmd_api_error(mp, EFMD_BUF_NOENT, "no buf named '%s' is "
1557 "associated with %s\n", name, cp ? "case" : "module");
1560 bcopy(bp->buf_data, buf, MIN(bp->buf_size, size));
1561 if (size > bp->buf_size)
1562 bzero((char *)buf + bp->buf_size, size - bp->buf_size);
1564 fmd_module_unlock(mp);
1567 void
1568 fmd_buf_write(fmd_hdl_t *hdl, fmd_case_t *cp,
1569 const char *name, const void *buf, size_t size)
1571 fmd_module_t *mp = fmd_api_module_lock(hdl);
1572 fmd_buf_hash_t *bhp = fmd_buf_gethash(mp, cp);
1573 fmd_buf_t *bp = fmd_buf_lookup(bhp, name);
1575 if (bp == NULL) {
1576 if (fmd_strbadid(name, FMD_B_TRUE) != NULL || size == 0) {
1577 fmd_api_error(mp, EFMD_BUF_INVAL, "cannot write '%s' "
1578 "(size %lu): %s\n", name, (ulong_t)size,
1579 fmd_strerror(EFMD_BUF_INVAL));
1582 if (mp->mod_stats->ms_buflimit.fmds_value.ui64 -
1583 mp->mod_stats->ms_buftotal.fmds_value.ui64 < size) {
1584 fmd_api_error(mp, EFMD_BUF_LIMIT, "cannot write '%s': "
1585 "buf limit exceeded (%llu)\n", name, (u_longlong_t)
1586 mp->mod_stats->ms_buflimit.fmds_value.ui64);
1589 mp->mod_stats->ms_buftotal.fmds_value.ui64 += size;
1590 bp = fmd_buf_insert(bhp, name, size);
1592 } else if (size > bp->buf_size) {
1593 fmd_api_error(mp, EFMD_BUF_OFLOW,
1594 "write to buf '%s' overflows buf size (%lu > %lu)\n",
1595 name, (ulong_t)size, (ulong_t)bp->buf_size);
1598 bcopy(buf, bp->buf_data, MIN(bp->buf_size, size));
1599 bp->buf_flags |= FMD_BUF_DIRTY;
1601 if (cp != NULL)
1602 fmd_case_setdirty(cp);
1603 else
1604 fmd_module_setdirty(mp);
1606 fmd_module_unlock(mp);
1609 size_t
1610 fmd_buf_size(fmd_hdl_t *hdl, fmd_case_t *cp, const char *name)
1612 fmd_module_t *mp = fmd_api_module_lock(hdl);
1613 fmd_buf_hash_t *bhp = fmd_buf_gethash(mp, cp);
1615 fmd_buf_t *bp;
1616 size_t size;
1618 if ((bp = fmd_buf_lookup(bhp, name)) != NULL)
1619 size = bp->buf_size;
1620 else
1621 size = 0;
1623 fmd_module_unlock(mp);
1624 return (size);
1627 void
1628 fmd_serd_create(fmd_hdl_t *hdl, const char *name, uint_t n, hrtime_t t)
1630 fmd_module_t *mp = fmd_api_module_lock(hdl);
1632 if (fmd_serd_eng_lookup(&mp->mod_serds, name) != NULL) {
1633 fmd_api_error(mp, EFMD_SERD_EXISTS,
1634 "failed to create serd engine '%s': %s\n",
1635 name, fmd_strerror(EFMD_SERD_EXISTS));
1638 (void) fmd_serd_eng_insert(&mp->mod_serds, name, n, t);
1639 fmd_module_setdirty(mp);
1640 fmd_module_unlock(mp);
1643 void
1644 fmd_serd_destroy(fmd_hdl_t *hdl, const char *name)
1646 fmd_module_t *mp = fmd_api_module_lock(hdl);
1648 fmd_serd_eng_delete(&mp->mod_serds, name);
1649 fmd_module_setdirty(mp);
1650 fmd_module_unlock(mp);
1654 fmd_serd_exists(fmd_hdl_t *hdl, const char *name)
1656 fmd_module_t *mp = fmd_api_module_lock(hdl);
1657 int rv = (fmd_serd_eng_lookup(&mp->mod_serds, name) != NULL);
1658 fmd_module_unlock(mp);
1660 return (rv);
1663 void
1664 fmd_serd_reset(fmd_hdl_t *hdl, const char *name)
1666 fmd_module_t *mp = fmd_api_module_lock(hdl);
1667 fmd_serd_eng_t *sgp;
1669 if ((sgp = fmd_serd_eng_lookup(&mp->mod_serds, name)) == NULL) {
1670 fmd_api_error(mp, EFMD_SERD_NAME,
1671 "serd engine '%s' does not exist\n", name);
1674 fmd_serd_eng_reset(sgp);
1675 fmd_module_setdirty(mp);
1676 fmd_module_unlock(mp);
1680 fmd_serd_record(fmd_hdl_t *hdl, const char *name, fmd_event_t *ep)
1682 fmd_module_t *mp = fmd_api_module_lock(hdl);
1683 fmd_serd_eng_t *sgp;
1684 int err;
1686 if ((sgp = fmd_serd_eng_lookup(&mp->mod_serds, name)) == NULL) {
1687 fmd_api_error(mp, EFMD_SERD_NAME,
1688 "failed to add record to serd engine '%s'", name);
1691 err = fmd_serd_eng_record(sgp, ep);
1693 if (sgp->sg_flags & FMD_SERD_DIRTY)
1694 fmd_module_setdirty(mp);
1696 fmd_module_unlock(mp);
1697 return (err);
1701 fmd_serd_fired(fmd_hdl_t *hdl, const char *name)
1703 fmd_module_t *mp = fmd_api_module_lock(hdl);
1704 fmd_serd_eng_t *sgp;
1705 int err;
1707 if ((sgp = fmd_serd_eng_lookup(&mp->mod_serds, name)) == NULL) {
1708 fmd_api_error(mp, EFMD_SERD_NAME,
1709 "serd engine '%s' does not exist\n", name);
1712 err = fmd_serd_eng_fired(sgp);
1713 fmd_module_unlock(mp);
1714 return (err);
1718 fmd_serd_empty(fmd_hdl_t *hdl, const char *name)
1720 fmd_module_t *mp = fmd_api_module_lock(hdl);
1721 fmd_serd_eng_t *sgp;
1722 int empty;
1724 if ((sgp = fmd_serd_eng_lookup(&mp->mod_serds, name)) == NULL) {
1725 fmd_api_error(mp, EFMD_SERD_NAME,
1726 "serd engine '%s' does not exist\n", name);
1729 empty = fmd_serd_eng_empty(sgp);
1730 fmd_module_unlock(mp);
1731 return (empty);
1734 pthread_t
1735 fmd_thr_create(fmd_hdl_t *hdl, void (*func)(void *), void *arg)
1737 fmd_module_t *mp = fmd_api_module_lock(hdl);
1738 fmd_thread_t *tp;
1739 pthread_t tid;
1741 if (mp->mod_stats->ms_thrtotal.fmds_value.ui32 >=
1742 mp->mod_stats->ms_thrlimit.fmds_value.ui32) {
1743 fmd_api_error(mp, EFMD_THR_LIMIT, "%s request to create an "
1744 "auxiliary thread exceeds module thread limit (%u)\n",
1745 mp->mod_name, mp->mod_stats->ms_thrlimit.fmds_value.ui32);
1748 if ((tp = fmd_thread_create(mp, func, arg)) == NULL) {
1749 fmd_api_error(mp, EFMD_THR_CREATE,
1750 "failed to create auxiliary thread");
1753 tid = tp->thr_tid;
1754 mp->mod_stats->ms_thrtotal.fmds_value.ui32++;
1755 (void) fmd_idspace_xalloc(mp->mod_threads, tid, tp);
1757 fmd_module_unlock(mp);
1758 return (tid);
1761 void
1762 fmd_thr_destroy(fmd_hdl_t *hdl, pthread_t tid)
1764 fmd_module_t *mp = fmd_api_module_lock(hdl);
1765 fmd_thread_t *tp;
1766 int err;
1768 if (pthread_self() == tid) {
1769 fmd_api_error(mp, EFMD_THR_INVAL, "auxiliary thread tried to "
1770 "destroy itself (tid %u)\n", tid);
1773 if ((tp = fmd_idspace_getspecific(mp->mod_threads, tid)) == NULL) {
1774 fmd_api_error(mp, EFMD_THR_INVAL, "auxiliary thread tried to "
1775 "destroy an invalid thread (tid %u)\n", tid);
1779 * Wait for the specified thread to exit and then join with it. Since
1780 * the thread may need to make API calls in order to complete its work
1781 * we must sleep with the module lock unheld, and then reacquire it.
1783 fmd_module_unlock(mp);
1784 err = pthread_join(tid, NULL);
1785 mp = fmd_api_module_lock(hdl);
1788 * Since pthread_join() was called without the module lock held, if
1789 * multiple callers attempted to destroy the same auxiliary thread
1790 * simultaneously, one will succeed and the others will get ESRCH.
1791 * Therefore we silently ignore ESRCH but only allow the caller who
1792 * succeessfully joined with the auxiliary thread to destroy it.
1794 if (err != 0 && err != ESRCH) {
1795 fmd_api_error(mp, EFMD_THR_JOIN,
1796 "failed to join with auxiliary thread %u\n", tid);
1799 if (err == 0) {
1800 fmd_thread_destroy(tp, FMD_THREAD_NOJOIN);
1801 mp->mod_stats->ms_thrtotal.fmds_value.ui32--;
1802 (void) fmd_idspace_free(mp->mod_threads, tid);
1805 fmd_module_unlock(mp);
1808 void
1809 fmd_thr_signal(fmd_hdl_t *hdl, pthread_t tid)
1811 fmd_module_t *mp = fmd_api_module_lock(hdl);
1813 if (tid != mp->mod_thread->thr_tid &&
1814 fmd_idspace_getspecific(mp->mod_threads, tid) == NULL) {
1815 fmd_api_error(mp, EFMD_THR_INVAL, "tid %u is not a valid "
1816 "thread id for module %s\n", tid, mp->mod_name);
1819 (void) pthread_kill(tid, fmd.d_thr_sig);
1820 fmd_module_unlock(mp);
1823 void
1824 fmd_thr_checkpoint(fmd_hdl_t *hdl)
1826 fmd_module_t *mp = fmd_api_module_lock(hdl);
1827 pthread_t tid = pthread_self();
1829 if (tid == mp->mod_thread->thr_tid ||
1830 fmd_idspace_getspecific(mp->mod_threads, tid) == NULL) {
1831 fmd_api_error(mp, EFMD_THR_INVAL, "tid %u is not a valid "
1832 "auxiliary thread id for module %s\n", tid, mp->mod_name);
1835 fmd_ckpt_save(mp);
1837 fmd_module_unlock(mp);
1840 /*ARGSUSED3*/
1842 fmd_doorthr_create(door_info_t *dip, void *(*crf)(void *), void *crarg,
1843 void *cookie)
1845 fmd_thread_t *old_tp, *new_tp;
1846 fmd_module_t *mp;
1847 pthread_t tid;
1850 * We're called either during initial door_xcreate or during
1851 * a depletion callback. In both cases the current thread
1852 * is already setup so we can retrieve the fmd_thread_t.
1853 * If not then we panic. The new thread will be associated with
1854 * the same module as the old.
1856 * If dip == NULL we're being called as part of the
1857 * sysevent_bind_subscriber hack - see comments there.
1859 if ((old_tp = pthread_getspecific(fmd.d_key)) == NULL)
1860 fmd_panic("fmd_doorthr_create from unrecognized thread\n");
1862 mp = old_tp->thr_mod;
1863 (void) fmd_api_module_lock((fmd_hdl_t *)mp);
1865 if (dip && mp->mod_stats->ms_doorthrtotal.fmds_value.ui32 >=
1866 mp->mod_stats->ms_doorthrlimit.fmds_value.ui32) {
1867 fmd_module_unlock(mp);
1868 (void) fmd_dprintf(FMD_DBG_XPRT, "door server %s for %p "
1869 "not attemped - at max\n",
1870 dip->di_attributes & DOOR_DEPLETION_CB ?
1871 "depletion callback" : "startup", (void *)dip);
1872 return (0);
1875 if ((new_tp = fmd_doorthread_create(mp, (fmd_thread_f *)crf,
1876 crarg)) != NULL) {
1877 tid = new_tp->thr_tid;
1878 mp->mod_stats->ms_doorthrtotal.fmds_value.ui32++;
1879 (void) fmd_idspace_xalloc(mp->mod_threads, tid, new_tp);
1882 fmd_module_unlock(mp);
1884 if (dip) {
1885 fmd_dprintf(FMD_DBG_XPRT, "door server startup for %p %s\n",
1886 (void *)dip, new_tp ? "successful" : "failed");
1889 return (new_tp ? 1 : -1);
1892 /*ARGSUSED*/
1893 void
1894 fmd_doorthr_setup(void *cookie)
1896 (void) pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
1899 id_t
1900 fmd_timer_install(fmd_hdl_t *hdl, void *arg, fmd_event_t *ep, hrtime_t delta)
1902 fmd_module_t *mp = fmd_api_module_lock(hdl);
1903 fmd_modtimer_t *t;
1904 id_t id;
1906 if (delta < 0) {
1907 fmd_api_error(mp, EFMD_TIMER_INVAL,
1908 "timer delta %lld is not a valid interval\n", delta);
1911 t = fmd_alloc(sizeof (fmd_modtimer_t), FMD_SLEEP);
1912 t->mt_mod = mp;
1913 t->mt_arg = arg;
1914 t->mt_id = -1;
1916 if ((id = fmd_timerq_install(fmd.d_timers, mp->mod_timerids,
1917 (fmd_timer_f *)fmd_module_timeout, t, ep, delta)) == -1) {
1918 fmd_free(t, sizeof (fmd_modtimer_t));
1919 fmd_api_error(mp, EFMD_TIMER_LIMIT,
1920 "failed to install timer +%lld", delta);
1923 fmd_module_unlock(mp);
1924 return (id);
1927 void
1928 fmd_timer_remove(fmd_hdl_t *hdl, id_t id)
1930 fmd_module_t *mp = fmd_api_module_lock(hdl);
1931 fmd_modtimer_t *t;
1933 if (!fmd_idspace_valid(mp->mod_timerids, id)) {
1934 fmd_api_error(mp, EFMD_TIMER_INVAL,
1935 "id %ld is not a valid timer id\n", id);
1939 * If the timer has not fired (t != NULL), remove it from the timer
1940 * queue. If the timer has fired (t == NULL), we could be in one of
1941 * two situations: a) we are processing the timer callback or b)
1942 * the timer event is on the module queue awaiting dispatch. For a),
1943 * fmd_timerq_remove() will wait for the timer callback function
1944 * to complete and queue an event for dispatch. For a) and b),
1945 * we cancel the outstanding timer event from the module's dispatch
1946 * queue.
1948 if ((t = fmd_timerq_remove(fmd.d_timers, mp->mod_timerids, id)) != NULL)
1949 fmd_free(t, sizeof (fmd_modtimer_t));
1950 fmd_module_unlock(mp);
1952 fmd_eventq_cancel(mp->mod_queue, FMD_EVT_TIMEOUT, (void *)id);
1955 static nvlist_t *
1956 fmd_nvl_create_suspect(fmd_hdl_t *hdl, const char *class,
1957 uint8_t certainty, nvlist_t *asru, nvlist_t *fru, nvlist_t *rsrc,
1958 const char *pfx, boolean_t chkpfx)
1960 fmd_module_t *mp;
1961 nvlist_t *nvl;
1963 mp = fmd_api_module_lock(hdl);
1964 if (class == NULL || class[0] == '\0' ||
1965 chkpfx == B_TRUE && strncmp(class, pfx, strlen(pfx)) != 0)
1966 fmd_api_error(mp, EFMD_NVL_INVAL, "invalid %s class: '%s'\n",
1967 pfx, class ? class : "(empty)");
1969 nvl = fmd_protocol_fault(class, certainty, asru, fru, rsrc, NULL);
1971 fmd_module_unlock(mp);
1973 return (nvl);
1976 nvlist_t *
1977 fmd_nvl_create_fault(fmd_hdl_t *hdl, const char *class,
1978 uint8_t certainty, nvlist_t *asru, nvlist_t *fru, nvlist_t *rsrc)
1981 * We can't enforce that callers only specifiy classes matching
1982 * fault.* since there are already a number of modules that
1983 * use fmd_nvl_create_fault to create a defect event. Since
1984 * fmd_nvl_create_{fault,defect} are equivalent, for now anyway,
1985 * no harm is done. So call fmd_nvl_create_suspect with last
1986 * argument B_FALSE.
1988 return (fmd_nvl_create_suspect(hdl, class, certainty, asru,
1989 fru, rsrc, FM_FAULT_CLASS ".", B_FALSE));
1992 nvlist_t *
1993 fmd_nvl_create_defect(fmd_hdl_t *hdl, const char *class,
1994 uint8_t certainty, nvlist_t *asru, nvlist_t *fru, nvlist_t *rsrc)
1996 return (fmd_nvl_create_suspect(hdl, class, certainty, asru,
1997 fru, rsrc, FM_DEFECT_CLASS ".", B_TRUE));
2000 const nvlist_t *
2001 fmd_hdl_fmauth(fmd_hdl_t *hdl)
2003 fmd_module_t *mp = fmd_api_module_lock(hdl);
2004 const nvlist_t *auth;
2006 auth = (const nvlist_t *)fmd.d_rmod->mod_fmri;
2008 fmd_module_unlock(mp);
2010 return (auth);
2013 const nvlist_t *
2014 fmd_hdl_modauth(fmd_hdl_t *hdl)
2016 fmd_module_t *mp = fmd_api_module_lock(hdl);
2017 const nvlist_t *auth;
2019 auth = (const nvlist_t *)mp->mod_fmri;
2021 fmd_module_unlock(mp);
2023 return (auth);
2028 fmd_nvl_class_match(fmd_hdl_t *hdl, nvlist_t *nvl, const char *pattern)
2030 fmd_module_t *mp = fmd_api_module_lock(hdl);
2031 char *class;
2032 int rv;
2034 rv = (nvl != NULL && nvlist_lookup_string(nvl,
2035 FM_CLASS, &class) == 0 && fmd_strmatch(class, pattern));
2037 fmd_module_unlock(mp);
2038 return (rv);
2042 fmd_nvl_fmri_expand(fmd_hdl_t *hdl, nvlist_t *nvl)
2044 fmd_module_t *mp = fmd_api_module_lock(hdl);
2045 int rv;
2047 if (nvl == NULL) {
2048 fmd_api_error(mp, EFMD_NVL_INVAL,
2049 "invalid nvlist %p\n", (void *)nvl);
2052 rv = fmd_fmri_expand(nvl);
2053 fmd_module_unlock(mp);
2054 return (rv);
2058 fmd_nvl_fmri_present(fmd_hdl_t *hdl, nvlist_t *nvl)
2060 fmd_module_t *mp = fmd_api_module_lock(hdl);
2061 int rv;
2063 if (nvl == NULL) {
2064 fmd_api_error(mp, EFMD_NVL_INVAL,
2065 "invalid nvlist %p\n", (void *)nvl);
2068 rv = fmd_fmri_present(nvl);
2069 fmd_module_unlock(mp);
2071 if (rv < 0) {
2072 fmd_api_error(mp, EFMD_FMRI_OP, "invalid fmri for "
2073 "fmd_nvl_fmri_present\n");
2076 return (rv);
2080 fmd_nvl_fmri_replaced(fmd_hdl_t *hdl, nvlist_t *nvl)
2082 fmd_module_t *mp = fmd_api_module_lock(hdl);
2083 int rv;
2085 if (nvl == NULL) {
2086 fmd_api_error(mp, EFMD_NVL_INVAL,
2087 "invalid nvlist %p\n", (void *)nvl);
2090 rv = fmd_fmri_replaced(nvl);
2091 fmd_module_unlock(mp);
2093 return (rv);
2097 fmd_nvl_fmri_unusable(fmd_hdl_t *hdl, nvlist_t *nvl)
2099 fmd_module_t *mp = fmd_api_module_lock(hdl);
2100 int rv;
2102 if (nvl == NULL) {
2103 fmd_api_error(mp, EFMD_NVL_INVAL,
2104 "invalid nvlist %p\n", (void *)nvl);
2107 rv = fmd_fmri_unusable(nvl);
2108 fmd_module_unlock(mp);
2110 if (rv < 0) {
2111 fmd_api_error(mp, EFMD_FMRI_OP, "invalid fmri for "
2112 "fmd_nvl_fmri_unusable\n");
2115 return (rv);
2119 fmd_nvl_fmri_retire(fmd_hdl_t *hdl, nvlist_t *nvl)
2121 fmd_module_t *mp = fmd_api_module_lock(hdl);
2122 int rv;
2124 if (nvl == NULL) {
2125 fmd_api_error(mp, EFMD_NVL_INVAL,
2126 "invalid nvlist %p\n", (void *)nvl);
2129 rv = fmd_fmri_retire(nvl);
2130 fmd_module_unlock(mp);
2132 return (rv);
2136 fmd_nvl_fmri_unretire(fmd_hdl_t *hdl, nvlist_t *nvl)
2138 fmd_module_t *mp = fmd_api_module_lock(hdl);
2139 int rv;
2141 if (nvl == NULL) {
2142 fmd_api_error(mp, EFMD_NVL_INVAL,
2143 "invalid nvlist %p\n", (void *)nvl);
2146 rv = fmd_fmri_unretire(nvl);
2147 fmd_module_unlock(mp);
2149 return (rv);
2153 fmd_nvl_fmri_service_state(fmd_hdl_t *hdl, nvlist_t *nvl)
2155 fmd_module_t *mp = fmd_api_module_lock(hdl);
2156 int rv;
2158 if (nvl == NULL) {
2159 fmd_api_error(mp, EFMD_NVL_INVAL,
2160 "invalid nvlist %p\n", (void *)nvl);
2163 rv = fmd_fmri_service_state(nvl);
2164 if (rv < 0)
2165 rv = fmd_fmri_unusable(nvl) ? FMD_SERVICE_STATE_UNUSABLE :
2166 FMD_SERVICE_STATE_OK;
2167 fmd_module_unlock(mp);
2169 if (rv < 0) {
2170 fmd_api_error(mp, EFMD_FMRI_OP, "invalid fmri for "
2171 "fmd_nvl_fmri_service_state\n");
2174 return (rv);
2177 typedef struct {
2178 const char *class;
2179 int *rvp;
2180 } fmd_has_fault_arg_t;
2182 static void
2183 fmd_rsrc_has_fault(fmd_asru_link_t *alp, void *arg)
2185 fmd_has_fault_arg_t *fhfp = (fmd_has_fault_arg_t *)arg;
2186 char *class;
2188 if (fhfp->class == NULL) {
2189 if (alp->al_flags & FMD_ASRU_FAULTY)
2190 *fhfp->rvp = 1;
2191 } else {
2192 if ((alp->al_flags & FMD_ASRU_FAULTY) &&
2193 alp->al_event != NULL && nvlist_lookup_string(alp->al_event,
2194 FM_CLASS, &class) == 0 && fmd_strmatch(class, fhfp->class))
2195 *fhfp->rvp = 1;
2200 fmd_nvl_fmri_has_fault(fmd_hdl_t *hdl, nvlist_t *nvl, int type, char *class)
2202 fmd_module_t *mp = fmd_api_module_lock(hdl);
2203 fmd_asru_hash_t *ahp = fmd.d_asrus;
2204 int rv = 0;
2205 char *name;
2206 int namelen;
2207 fmd_has_fault_arg_t fhf;
2209 if (nvl == NULL) {
2210 fmd_api_error(mp, EFMD_NVL_INVAL,
2211 "invalid nvlist %p\n", (void *)nvl);
2213 if ((namelen = fmd_fmri_nvl2str(nvl, NULL, 0)) == -1)
2214 fmd_api_error(mp, EFMD_NVL_INVAL,
2215 "invalid nvlist: %p\n", (void *)nvl);
2216 name = fmd_alloc(namelen + 1, FMD_SLEEP);
2217 if (fmd_fmri_nvl2str(nvl, name, namelen + 1) == -1) {
2218 if (name != NULL)
2219 fmd_free(name, namelen + 1);
2220 fmd_api_error(mp, EFMD_NVL_INVAL,
2221 "invalid nvlist: %p\n", (void *)nvl);
2224 fhf.class = class;
2225 fhf.rvp = &rv;
2226 if (type == FMD_HAS_FAULT_RESOURCE)
2227 fmd_asru_hash_apply_by_rsrc(ahp, name, fmd_rsrc_has_fault,
2228 &fhf);
2229 else if (type == FMD_HAS_FAULT_ASRU)
2230 fmd_asru_hash_apply_by_asru(ahp, name, fmd_rsrc_has_fault,
2231 &fhf);
2232 else if (type == FMD_HAS_FAULT_FRU)
2233 fmd_asru_hash_apply_by_fru(ahp, name, fmd_rsrc_has_fault,
2234 &fhf);
2236 if (name != NULL)
2237 fmd_free(name, namelen + 1);
2238 fmd_module_unlock(mp);
2239 return (rv);
2243 fmd_nvl_fmri_contains(fmd_hdl_t *hdl, nvlist_t *n1, nvlist_t *n2)
2245 fmd_module_t *mp = fmd_api_module_lock(hdl);
2246 int rv;
2248 if (n1 == NULL || n2 == NULL) {
2249 fmd_api_error(mp, EFMD_NVL_INVAL,
2250 "invalid nvlist(s): %p, %p\n", (void *)n1, (void *)n2);
2253 rv = fmd_fmri_contains(n1, n2);
2254 fmd_module_unlock(mp);
2256 if (rv < 0) {
2257 fmd_api_error(mp, EFMD_FMRI_OP, "invalid fmri for "
2258 "fmd_nvl_fmri_contains\n");
2261 return (rv);
2264 nvlist_t *
2265 fmd_nvl_fmri_translate(fmd_hdl_t *hdl, nvlist_t *fmri, nvlist_t *auth)
2267 fmd_module_t *mp = fmd_api_module_lock(hdl);
2268 nvlist_t *xfmri;
2270 if (fmri == NULL || auth == NULL) {
2271 fmd_api_error(mp, EFMD_NVL_INVAL,
2272 "invalid nvlist(s): %p, %p\n", (void *)fmri, (void *)auth);
2275 xfmri = fmd_fmri_translate(fmri, auth);
2276 fmd_module_unlock(mp);
2277 return (xfmri);
2280 static int
2281 fmd_nvl_op_init(nv_alloc_t *ops, va_list ap)
2283 fmd_module_t *mp = va_arg(ap, fmd_module_t *);
2285 ops->nva_arg = mp;
2287 return (0);
2290 static void *
2291 fmd_nvl_op_alloc_sleep(nv_alloc_t *ops, size_t size)
2293 fmd_module_t *mp = ops->nva_arg;
2295 return (fmd_hdl_alloc_locked(mp, size, FMD_SLEEP));
2298 static void *
2299 fmd_nvl_op_alloc_nosleep(nv_alloc_t *ops, size_t size)
2301 fmd_module_t *mp = ops->nva_arg;
2303 return (fmd_hdl_alloc_locked(mp, size, FMD_NOSLEEP));
2306 static void
2307 fmd_nvl_op_free(nv_alloc_t *ops, void *data, size_t size)
2309 fmd_module_t *mp = ops->nva_arg;
2311 fmd_hdl_free_locked(mp, data, size);
2314 nv_alloc_ops_t fmd_module_nva_ops_sleep = {
2315 fmd_nvl_op_init,
2316 NULL,
2317 fmd_nvl_op_alloc_sleep,
2318 fmd_nvl_op_free,
2319 NULL
2322 nv_alloc_ops_t fmd_module_nva_ops_nosleep = {
2323 fmd_nvl_op_init,
2324 NULL,
2325 fmd_nvl_op_alloc_nosleep,
2326 fmd_nvl_op_free,
2327 NULL
2330 nvlist_t *
2331 fmd_nvl_alloc(fmd_hdl_t *hdl, int flags)
2333 fmd_module_t *mp = fmd_api_module_lock(hdl);
2334 nv_alloc_t *nva;
2335 nvlist_t *nvl;
2336 int ret;
2338 if (flags == FMD_SLEEP)
2339 nva = &mp->mod_nva_sleep;
2340 else
2341 nva = &mp->mod_nva_nosleep;
2343 ret = nvlist_xalloc(&nvl, NV_UNIQUE_NAME, nva);
2345 fmd_module_unlock(mp);
2347 if (ret != 0)
2348 return (NULL);
2349 else
2350 return (nvl);
2353 nvlist_t *
2354 fmd_nvl_dup(fmd_hdl_t *hdl, nvlist_t *src, int flags)
2356 fmd_module_t *mp = fmd_api_module_lock(hdl);
2357 nv_alloc_t *nva;
2358 nvlist_t *nvl;
2359 int ret;
2361 if (flags == FMD_SLEEP)
2362 nva = &mp->mod_nva_sleep;
2363 else
2364 nva = &mp->mod_nva_nosleep;
2366 ret = nvlist_xdup(src, &nvl, nva);
2368 fmd_module_unlock(mp);
2370 if (ret != 0)
2371 return (NULL);
2372 else
2373 return (nvl);
2376 /*ARGSUSED*/
2377 void
2378 fmd_repair_fru(fmd_hdl_t *hdl, const char *fmri)
2380 int err;
2381 fmd_asru_rep_arg_t fara;
2383 fara.fara_reason = FMD_ASRU_REPAIRED;
2384 fara.fara_bywhat = FARA_BY_FRU;
2385 fara.fara_rval = &err;
2386 fmd_asru_hash_apply_by_fru(fmd.d_asrus, (char *)fmri,
2387 fmd_asru_repaired, &fara);
2390 /*ARGSUSED*/
2392 fmd_repair_asru(fmd_hdl_t *hdl, const char *fmri)
2394 int err = FARA_ERR_RSRCNOTF;
2395 fmd_asru_rep_arg_t fara;
2397 fara.fara_reason = FMD_ASRU_REPAIRED;
2398 fara.fara_rval = &err;
2399 fara.fara_uuid = NULL;
2400 fara.fara_bywhat = FARA_BY_ASRU;
2401 fmd_asru_hash_apply_by_asru(fmd.d_asrus, fmri,
2402 fmd_asru_repaired, &fara);
2403 return (err);
2407 fmd_event_local(fmd_hdl_t *hdl, fmd_event_t *ep)
2409 if (hdl == NULL || ep == NULL) {
2410 fmd_api_error(fmd_api_module_lock(hdl), EFMD_EVENT_INVAL,
2411 "NULL parameter specified to fmd_event_local\n");
2414 return (((fmd_event_impl_t *)ep)->ev_flags & FMD_EVF_LOCAL);
2417 /*ARGSUSED*/
2418 uint64_t
2419 fmd_event_ena_create(fmd_hdl_t *hdl)
2421 return (fmd_ena());
2424 fmd_xprt_t *
2425 fmd_xprt_open(fmd_hdl_t *hdl, uint_t flags, nvlist_t *auth, void *data)
2427 fmd_module_t *mp = fmd_api_module_lock(hdl);
2428 fmd_xprt_t *xp;
2430 if (flags & ~FMD_XPRT_CMASK) {
2431 fmd_api_error(mp, EFMD_XPRT_INVAL,
2432 "invalid transport flags 0x%x\n", flags);
2435 if ((flags & FMD_XPRT_RDWR) != FMD_XPRT_RDWR &&
2436 (flags & FMD_XPRT_RDWR) != FMD_XPRT_RDONLY) {
2437 fmd_api_error(mp, EFMD_XPRT_INVAL,
2438 "cannot open write-only transport\n");
2441 if (mp->mod_stats->ms_xprtopen.fmds_value.ui32 >=
2442 mp->mod_stats->ms_xprtlimit.fmds_value.ui32) {
2443 fmd_api_error(mp, EFMD_XPRT_LIMIT, "%s request to create a "
2444 "transport exceeds module transport limit (%u)\n",
2445 mp->mod_name, mp->mod_stats->ms_xprtlimit.fmds_value.ui32);
2448 if ((xp = fmd_xprt_create(mp, flags, auth, data)) == NULL)
2449 fmd_api_error(mp, errno, "cannot create transport");
2451 fmd_module_unlock(mp);
2452 return (xp);
2455 void
2456 fmd_xprt_close(fmd_hdl_t *hdl, fmd_xprt_t *xp)
2458 fmd_module_t *mp = fmd_api_module_lock(hdl);
2459 fmd_xprt_impl_t *xip = fmd_api_transport_impl(hdl, xp);
2462 * Although this could be supported, it doesn't seem necessary or worth
2463 * the trouble. For now, just detect this and trigger a module abort.
2464 * If it is needed, transports should grow reference counts and a new
2465 * event type will need to be enqueued for the main thread to reap it.
2467 if (xip->xi_thread != NULL &&
2468 xip->xi_thread->thr_tid == pthread_self()) {
2469 fmd_api_error(mp, EFMD_XPRT_INVAL,
2470 "fmd_xprt_close() cannot be called from fmdo_send()\n");
2473 fmd_xprt_destroy(xp);
2474 fmd_module_unlock(mp);
2477 void
2478 fmd_xprt_post(fmd_hdl_t *hdl, fmd_xprt_t *xp, nvlist_t *nvl, hrtime_t hrt)
2480 nv_alloc_t *nva = nvlist_lookup_nv_alloc(nvl);
2481 fmd_module_t *mp = fmd_api_module(hdl);
2482 fmd_xprt_impl_t *xip = fmd_api_transport_impl(hdl, xp);
2483 nvlist_t *tmp;
2486 * If this event was allocated using the module-specific nvlist ops, we
2487 * need to create a copy using the standard fmd nvlist ops. Otherwise,
2488 * the event may persist after the module has been unloaded and we'll
2489 * die when attempting to free the nvlist.
2491 if (nva == &mp->mod_nva_sleep || nva == &mp->mod_nva_nosleep) {
2492 (void) nvlist_xdup(nvl, &tmp, &fmd.d_nva);
2493 nvlist_free(nvl);
2494 nvl = tmp;
2498 * fmd_xprt_recv() must block during startup waiting for fmd to globally
2499 * clear FMD_XPRT_DSUSPENDED. As such, we can't allow it to be called
2500 * from a module's _fmd_init() routine, because that would block
2501 * fmd from completing initial module loading, resulting in a deadlock.
2503 if ((xip->xi_flags & FMD_XPRT_ISUSPENDED) &&
2504 (pthread_self() == xip->xi_queue->eq_mod->mod_thread->thr_tid)) {
2505 fmd_api_error(fmd_api_module_lock(hdl), EFMD_XPRT_INVAL,
2506 "fmd_xprt_post() cannot be called from _fmd_init()\n");
2509 fmd_xprt_recv(xp, nvl, hrt, FMD_B_FALSE);
2512 void
2513 fmd_xprt_log(fmd_hdl_t *hdl, fmd_xprt_t *xp, nvlist_t *nvl, hrtime_t hrt)
2515 fmd_xprt_impl_t *xip = fmd_api_transport_impl(hdl, xp);
2518 * fmd_xprt_recv() must block during startup waiting for fmd to globally
2519 * clear FMD_XPRT_DSUSPENDED. As such, we can't allow it to be called
2520 * from a module's _fmd_init() routine, because that would block
2521 * fmd from completing initial module loading, resulting in a deadlock.
2523 if ((xip->xi_flags & FMD_XPRT_ISUSPENDED) &&
2524 (pthread_self() == xip->xi_queue->eq_mod->mod_thread->thr_tid)) {
2525 fmd_api_error(fmd_api_module_lock(hdl), EFMD_XPRT_INVAL,
2526 "fmd_xprt_log() cannot be called from _fmd_init()\n");
2529 fmd_xprt_recv(xp, nvl, hrt, FMD_B_TRUE);
2532 void
2533 fmd_xprt_suspend(fmd_hdl_t *hdl, fmd_xprt_t *xp)
2535 (void) fmd_api_transport_impl(hdl, xp); /* validate 'xp' */
2536 fmd_xprt_xsuspend(xp, FMD_XPRT_SUSPENDED);
2539 void
2540 fmd_xprt_resume(fmd_hdl_t *hdl, fmd_xprt_t *xp)
2542 (void) fmd_api_transport_impl(hdl, xp); /* validate 'xp' */
2543 fmd_xprt_xresume(xp, FMD_XPRT_SUSPENDED);
2547 fmd_xprt_error(fmd_hdl_t *hdl, fmd_xprt_t *xp)
2549 fmd_xprt_impl_t *xip = fmd_api_transport_impl(hdl, xp);
2550 return (xip->xi_state == _fmd_xprt_state_err);
2554 * Translate all FMRIs in the specified name-value pair list for the specified
2555 * FMRI authority, and return a new name-value pair list for the translation.
2556 * This function is the recursive engine used by fmd_xprt_translate(), below.
2558 static nvlist_t *
2559 fmd_xprt_xtranslate(nvlist_t *nvl, nvlist_t *auth)
2561 uint_t i, j, n;
2562 nvpair_t *nvp, **nvps;
2563 uint_t nvpslen = 0;
2564 char *name;
2565 size_t namelen = 0;
2567 nvlist_t **a, **b;
2568 nvlist_t *l, *r;
2569 data_type_t type;
2570 char *s;
2571 int err;
2573 (void) nvlist_xdup(nvl, &nvl, &fmd.d_nva);
2576 * Count up the number of name-value pairs in 'nvl' and compute the
2577 * maximum length of a name used in this list for use below.
2579 for (nvp = nvlist_next_nvpair(nvl, NULL);
2580 nvp != NULL; nvp = nvlist_next_nvpair(nvl, nvp), nvpslen++) {
2581 size_t len = strlen(nvpair_name(nvp));
2582 namelen = MAX(namelen, len);
2585 nvps = alloca(sizeof (nvpair_t *) * nvpslen);
2586 name = alloca(namelen + 1);
2589 * Store a snapshot of the name-value pairs in 'nvl' into nvps[] so
2590 * that we can iterate over the original pairs in the loop below while
2591 * performing arbitrary insert and delete operations on 'nvl' itself.
2593 for (i = 0, nvp = nvlist_next_nvpair(nvl, NULL);
2594 nvp != NULL; nvp = nvlist_next_nvpair(nvl, nvp))
2595 nvps[i++] = nvp;
2598 * Now iterate over the snapshot of the name-value pairs. If we find a
2599 * value that is of type NVLIST or NVLIST_ARRAY, we translate that
2600 * object by either calling ourself recursively on it, or calling into
2601 * fmd_fmri_translate() if the object is an FMRI. We then rip out the
2602 * original name-value pair and replace it with the translated one.
2604 for (i = 0; i < nvpslen; i++) {
2605 nvp = nvps[i];
2606 type = nvpair_type(nvp);
2608 switch (type) {
2609 case DATA_TYPE_NVLIST_ARRAY:
2610 if (nvpair_value_nvlist_array(nvp, &a, &n) != 0 ||
2611 a == NULL || n == 0)
2612 continue; /* array is zero-sized; skip it */
2614 b = fmd_alloc(sizeof (nvlist_t *) * n, FMD_SLEEP);
2617 * If the first array nvlist element looks like an FMRI
2618 * then assume the other elements are FMRIs as well.
2619 * If any b[j]'s can't be translated, then EINVAL will
2620 * be returned from nvlist_add_nvlist_array() below.
2622 if (nvlist_lookup_string(*a, FM_FMRI_SCHEME, &s) == 0) {
2623 for (j = 0; j < n; j++)
2624 b[j] = fmd_fmri_translate(a[j], auth);
2625 } else {
2626 for (j = 0; j < n; j++)
2627 b[j] = fmd_xprt_xtranslate(a[j], auth);
2630 (void) strcpy(name, nvpair_name(nvp));
2631 (void) nvlist_remove(nvl, name, type);
2632 err = nvlist_add_nvlist_array(nvl, name, b, n);
2634 for (j = 0; j < n; j++)
2635 nvlist_free(b[j]);
2637 fmd_free(b, sizeof (nvlist_t *) * n);
2639 if (err != 0) {
2640 nvlist_free(nvl);
2641 errno = err;
2642 return (NULL);
2644 break;
2646 case DATA_TYPE_NVLIST:
2647 if (nvpair_value_nvlist(nvp, &l) == 0 &&
2648 nvlist_lookup_string(l, FM_FMRI_SCHEME, &s) == 0)
2649 r = fmd_fmri_translate(l, auth);
2650 else
2651 r = fmd_xprt_xtranslate(l, auth);
2653 if (r == NULL) {
2654 nvlist_free(nvl);
2655 return (NULL);
2658 (void) strcpy(name, nvpair_name(nvp));
2659 (void) nvlist_remove(nvl, name, type);
2660 (void) nvlist_add_nvlist(nvl, name, r);
2662 nvlist_free(r);
2663 break;
2667 return (nvl);
2670 nvlist_t *
2671 fmd_xprt_translate(fmd_hdl_t *hdl, fmd_xprt_t *xp, fmd_event_t *ep)
2673 fmd_xprt_impl_t *xip = fmd_api_transport_impl(hdl, xp);
2675 if (xip->xi_auth == NULL) {
2676 fmd_api_error(fmd_api_module_lock(hdl), EFMD_XPRT_INVAL,
2677 "no authority defined for transport %p\n", (void *)xp);
2680 return (fmd_xprt_xtranslate(FMD_EVENT_NVL(ep), xip->xi_auth));
2683 /*ARGSUSED*/
2684 void
2685 fmd_xprt_add_domain(fmd_hdl_t *hdl, nvlist_t *nvl, char *domain)
2687 nvpair_t *nvp, *nvp2;
2688 nvlist_t *nvl2, *nvl3;
2689 char *class;
2691 if (nvl == NULL || domain == NULL)
2692 return;
2693 for (nvp = nvlist_next_nvpair(nvl, NULL); nvp != NULL;
2694 nvp = nvlist_next_nvpair(nvl, nvp)) {
2695 if (strcmp(nvpair_name(nvp), FM_CLASS) == 0) {
2696 (void) nvpair_value_string(nvp, &class);
2697 if (strcmp(class, FM_LIST_SUSPECT_CLASS) != 0)
2698 return;
2701 for (nvp = nvlist_next_nvpair(nvl, NULL); nvp != NULL;
2702 nvp = nvlist_next_nvpair(nvl, nvp)) {
2703 if (strcmp(nvpair_name(nvp), FM_SUSPECT_DE) == 0) {
2704 (void) nvpair_value_nvlist(nvp, &nvl2);
2705 for (nvp2 = nvlist_next_nvpair(nvl2, NULL);
2706 nvp2 != NULL;
2707 nvp2 = nvlist_next_nvpair(nvl2, nvp2)) {
2708 if (strcmp(nvpair_name(nvp2),
2709 FM_FMRI_AUTHORITY) == 0) {
2710 (void) nvpair_value_nvlist(nvp2, &nvl3);
2711 (void) nvlist_add_string(nvl3,
2712 FM_FMRI_AUTH_DOMAIN, domain);
2713 break;
2716 break;
2721 void
2722 fmd_xprt_setspecific(fmd_hdl_t *hdl, fmd_xprt_t *xp, void *data)
2724 fmd_api_transport_impl(hdl, xp)->xi_data = data;
2727 void *
2728 fmd_xprt_getspecific(fmd_hdl_t *hdl, fmd_xprt_t *xp)
2730 return (fmd_api_transport_impl(hdl, xp)->xi_data);