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