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