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 static void * 736 fmd_hdl_alloc_locked(fmd_module_t *mp, size_t size, int flags) 737 { 738 void *data; 739 740 if (mp->mod_stats->ms_memlimit.fmds_value.ui64 - 741 mp->mod_stats->ms_memtotal.fmds_value.ui64 < size) { 742 fmd_api_error(mp, EFMD_HDL_NOMEM, "%s's allocation of %lu " 743 "bytes exceeds module memory limit (%llu)\n", 744 mp->mod_name, (ulong_t)size, (u_longlong_t) 745 mp->mod_stats->ms_memtotal.fmds_value.ui64); 746 } 747 748 if ((data = fmd_alloc(size, flags)) != NULL) 749 mp->mod_stats->ms_memtotal.fmds_value.ui64 += size; 750 751 return (data); 752 } 753 754 void * 755 fmd_hdl_alloc(fmd_hdl_t *hdl, size_t size, int flags) 756 { 757 fmd_module_t *mp = fmd_api_module_lock(hdl); 758 void *data; 759 760 data = fmd_hdl_alloc_locked(mp, size, flags); 761 762 fmd_module_unlock(mp); 763 return (data); 764 } 765 766 void * 767 fmd_hdl_zalloc(fmd_hdl_t *hdl, size_t size, int flags) 768 { 769 void *data = fmd_hdl_alloc(hdl, size, flags); 770 771 if (data != NULL) 772 bzero(data, size); 773 774 return (data); 775 } 776 777 static void 778 fmd_hdl_free_locked(fmd_module_t *mp, void *data, size_t size) 779 { 780 fmd_free(data, size); 781 mp->mod_stats->ms_memtotal.fmds_value.ui64 -= size; 782 } 783 784 void 785 fmd_hdl_free(fmd_hdl_t *hdl, void *data, size_t size) 786 { 787 fmd_module_t *mp = fmd_api_module_lock(hdl); 788 789 fmd_hdl_free_locked(mp, data, size); 790 791 fmd_module_unlock(mp); 792 } 793 794 char * 795 fmd_hdl_strdup(fmd_hdl_t *hdl, const char *s, int flags) 796 { 797 char *p; 798 799 if (s != NULL) 800 p = fmd_hdl_alloc(hdl, strlen(s) + 1, flags); 801 else 802 p = NULL; 803 804 if (p != NULL) 805 (void) strcpy(p, s); 806 807 return (p); 808 } 809 810 void 811 fmd_hdl_strfree(fmd_hdl_t *hdl, char *s) 812 { 813 if (s != NULL) 814 fmd_hdl_free(hdl, s, strlen(s) + 1); 815 } 816 817 void 818 fmd_hdl_vabort(fmd_hdl_t *hdl, const char *format, va_list ap) 819 { 820 fmd_api_verror(fmd_api_module_lock(hdl), EFMD_HDL_ABORT, format, ap); 821 } 822 823 /*PRINTFLIKE2*/ 824 void 825 fmd_hdl_abort(fmd_hdl_t *hdl, const char *format, ...) 826 { 827 fmd_module_t *mp = fmd_api_module_lock(hdl); 828 va_list ap; 829 830 va_start(ap, format); 831 fmd_api_verror(mp, EFMD_HDL_ABORT, format, ap); 832 va_end(ap); 833 } 834 835 void 836 fmd_hdl_verror(fmd_hdl_t *hdl, const char *format, va_list ap) 837 { 838 fmd_module_t *mp = fmd_api_module_lock(hdl); 839 fmd_api_vxerror(mp, errno, format, ap); 840 fmd_module_unlock(mp); 841 } 842 843 /*PRINTFLIKE2*/ 844 void 845 fmd_hdl_error(fmd_hdl_t *hdl, const char *format, ...) 846 { 847 va_list ap; 848 849 va_start(ap, format); 850 fmd_hdl_verror(hdl, format, ap); 851 va_end(ap); 852 } 853 854 void 855 fmd_hdl_vdebug(fmd_hdl_t *hdl, const char *format, va_list ap) 856 { 857 fmd_module_t *mp = fmd_api_module_lock(hdl); 858 859 char *msg; 860 size_t len; 861 char c; 862 863 if (!(fmd.d_hdl_debug)) { 864 mp->mod_stats->ms_debugdrop.fmds_value.ui64++; 865 fmd_module_unlock(mp); 866 return; 867 } 868 869 len = vsnprintf(&c, 1, format, ap); 870 871 if ((msg = fmd_alloc(len + 2, FMD_NOSLEEP)) == NULL) { 872 mp->mod_stats->ms_debugdrop.fmds_value.ui64++; 873 fmd_module_unlock(mp); 874 return; 875 } 876 877 (void) vsnprintf(msg, len + 1, format, ap); 878 879 if (msg[len - 1] != '\n') 880 (void) strcpy(&msg[len], "\n"); 881 882 if (fmd.d_hdl_dbout & FMD_DBOUT_STDERR) { 883 (void) pthread_mutex_lock(&fmd.d_err_lock); 884 (void) fprintf(stderr, "%s DEBUG: %s: %s", 885 fmd.d_pname, mp->mod_name, msg); 886 (void) pthread_mutex_unlock(&fmd.d_err_lock); 887 } 888 889 if (fmd.d_hdl_dbout & FMD_DBOUT_SYSLOG) { 890 syslog(LOG_DEBUG | LOG_DAEMON, "%s DEBUG: %s: %s", 891 fmd.d_pname, mp->mod_name, msg); 892 } 893 894 fmd_free(msg, len + 2); 895 fmd_module_unlock(mp); 896 } 897 898 /*PRINTFLIKE2*/ 899 void 900 fmd_hdl_debug(fmd_hdl_t *hdl, const char *format, ...) 901 { 902 va_list ap; 903 904 va_start(ap, format); 905 fmd_hdl_vdebug(hdl, format, ap); 906 va_end(ap); 907 } 908 909 int32_t 910 fmd_prop_get_int32(fmd_hdl_t *hdl, const char *name) 911 { 912 fmd_module_t *mp = fmd_api_module_lock(hdl); 913 const fmd_conf_ops_t *ops = fmd_conf_gettype(mp->mod_conf, name); 914 int32_t value = 0; 915 916 if (ops == &fmd_conf_bool || ops == &fmd_conf_int32 || 917 ops == &fmd_conf_uint32) 918 (void) fmd_conf_getprop(mp->mod_conf, name, &value); 919 else if (ops != NULL) { 920 fmd_api_error(mp, EFMD_PROP_TYPE, 921 "property %s is not of int32 type\n", name); 922 } else { 923 fmd_api_error(mp, EFMD_PROP_DEFN, 924 "property %s is not defined\n", name); 925 } 926 927 fmd_module_unlock(mp); 928 return (value); 929 } 930 931 int64_t 932 fmd_prop_get_int64(fmd_hdl_t *hdl, const char *name) 933 { 934 fmd_module_t *mp = fmd_api_module_lock(hdl); 935 const fmd_conf_ops_t *ops = fmd_conf_gettype(mp->mod_conf, name); 936 int64_t value = 0; 937 938 if (ops == &fmd_conf_int64 || ops == &fmd_conf_uint64 || 939 ops == &fmd_conf_time || ops == &fmd_conf_size) 940 (void) fmd_conf_getprop(mp->mod_conf, name, &value); 941 else if (ops != NULL) { 942 fmd_api_error(mp, EFMD_PROP_TYPE, 943 "property %s is not of int64 type\n", name); 944 } else { 945 fmd_api_error(mp, EFMD_PROP_DEFN, 946 "property %s is not defined\n", name); 947 } 948 949 fmd_module_unlock(mp); 950 return (value); 951 } 952 953 char * 954 fmd_prop_get_string(fmd_hdl_t *hdl, const char *name) 955 { 956 fmd_module_t *mp = fmd_api_module_lock(hdl); 957 const fmd_conf_ops_t *ops = fmd_conf_gettype(mp->mod_conf, name); 958 char *value = NULL; 959 const char *s; 960 961 if (ops == &fmd_conf_string) { 962 (void) fmd_conf_getprop(mp->mod_conf, name, &s); 963 value = fmd_strdup(s, FMD_SLEEP); 964 } else if (ops != NULL) { 965 fmd_api_error(mp, EFMD_PROP_TYPE, 966 "property %s is not of string type\n", name); 967 } else { 968 fmd_api_error(mp, EFMD_PROP_DEFN, 969 "property %s is not defined\n", name); 970 } 971 972 fmd_module_unlock(mp); 973 return (value); 974 } 975 976 void 977 fmd_prop_free_string(fmd_hdl_t *hdl, char *s) 978 { 979 fmd_module_t *mp = fmd_api_module_lock(hdl); 980 fmd_strfree(s); 981 fmd_module_unlock(mp); 982 } 983 984 fmd_stat_t * 985 fmd_stat_create(fmd_hdl_t *hdl, uint_t flags, uint_t argc, fmd_stat_t *argv) 986 { 987 fmd_module_t *mp = fmd_api_module_lock(hdl); 988 fmd_stat_t *ep, *sp; 989 990 if (flags & ~FMD_STAT_ALLOC) { 991 fmd_api_error(mp, EFMD_STAT_FLAGS, 992 "invalid flags 0x%x passed to fmd_stat_create\n", flags); 993 } 994 995 if ((sp = fmd_ustat_insert(mp->mod_ustat, 996 flags | FMD_USTAT_VALIDATE, argc, argv, &ep)) == NULL) { 997 fmd_api_error(mp, errno, 998 "failed to publish stat '%s'", ep->fmds_name); 999 } 1000 1001 fmd_module_unlock(mp); 1002 return (sp); 1003 } 1004 1005 void 1006 fmd_stat_destroy(fmd_hdl_t *hdl, uint_t argc, fmd_stat_t *argv) 1007 { 1008 fmd_module_t *mp = fmd_api_module_lock(hdl); 1009 fmd_ustat_delete(mp->mod_ustat, argc, argv); 1010 fmd_module_unlock(mp); 1011 } 1012 1013 void 1014 fmd_stat_setstr(fmd_hdl_t *hdl, fmd_stat_t *sp, const char *s) 1015 { 1016 char *str = fmd_strdup(s, FMD_SLEEP); 1017 fmd_module_t *mp = fmd_api_module_lock(hdl); 1018 1019 if (sp->fmds_type != FMD_TYPE_STRING) { 1020 fmd_strfree(str); 1021 fmd_api_error(mp, EFMD_STAT_TYPE, 1022 "stat '%s' is not a string\n", sp->fmds_name); 1023 } 1024 1025 fmd_strfree(sp->fmds_value.str); 1026 sp->fmds_value.str = str; 1027 1028 fmd_module_unlock(mp); 1029 } 1030 1031 fmd_case_t * 1032 fmd_case_open(fmd_hdl_t *hdl, void *data) 1033 { 1034 fmd_module_t *mp = fmd_api_module_lock(hdl); 1035 fmd_case_t *cp = fmd_case_create(mp, data); 1036 fmd_module_unlock(mp); 1037 return (cp); 1038 } 1039 1040 void 1041 fmd_case_reset(fmd_hdl_t *hdl, fmd_case_t *cp) 1042 { 1043 fmd_module_t *mp = fmd_api_module_lock(hdl); 1044 fmd_case_impl_t *cip = fmd_api_case_impl(mp, cp); 1045 1046 if (cip->ci_state >= FMD_CASE_SOLVED) { 1047 fmd_api_error(mp, EFMD_CASE_STATE, "cannot solve %s: " 1048 "case is already solved or closed\n", cip->ci_uuid); 1049 } 1050 1051 fmd_case_reset_suspects(cp); 1052 fmd_module_unlock(mp); 1053 } 1054 1055 void 1056 fmd_case_solve(fmd_hdl_t *hdl, fmd_case_t *cp) 1057 { 1058 fmd_module_t *mp = fmd_api_module_lock(hdl); 1059 fmd_case_impl_t *cip = fmd_api_case_impl(mp, cp); 1060 1061 if (cip->ci_state >= FMD_CASE_SOLVED) { 1062 fmd_api_error(mp, EFMD_CASE_STATE, "cannot solve %s: " 1063 "case is already solved or closed\n", cip->ci_uuid); 1064 } 1065 1066 fmd_case_transition(cp, FMD_CASE_SOLVED, FMD_CF_SOLVED); 1067 fmd_module_unlock(mp); 1068 } 1069 1070 void 1071 fmd_case_close(fmd_hdl_t *hdl, fmd_case_t *cp) 1072 { 1073 fmd_module_t *mp = fmd_api_module_lock(hdl); 1074 1075 (void) fmd_api_case_impl(mp, cp); /* validate 'cp' */ 1076 fmd_case_transition(cp, FMD_CASE_CLOSE_WAIT, FMD_CF_ISOLATED); 1077 1078 fmd_module_unlock(mp); 1079 } 1080 1081 const char * 1082 fmd_case_uuid(fmd_hdl_t *hdl, fmd_case_t *cp) 1083 { 1084 fmd_module_t *mp = fmd_api_module_lock(hdl); 1085 fmd_case_impl_t *cip = fmd_api_case_impl(mp, cp); 1086 const char *uuid = cip->ci_uuid; 1087 1088 fmd_module_unlock(mp); 1089 return (uuid); 1090 } 1091 1092 fmd_case_t * 1093 fmd_case_uulookup(fmd_hdl_t *hdl, const char *uuid) 1094 { 1095 fmd_module_t *cmp, *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 cmp = ((fmd_case_impl_t *)cp)->ci_mod; 1100 fmd_case_rele(cp); 1101 } else 1102 cmp = NULL; 1103 1104 fmd_module_unlock(mp); 1105 return (cmp == mp ? cp : NULL); 1106 } 1107 1108 void 1109 fmd_case_uuclose(fmd_hdl_t *hdl, const char *uuid) 1110 { 1111 fmd_module_t *mp = fmd_api_module_lock(hdl); 1112 fmd_case_t *cp = fmd_case_hash_lookup(fmd.d_cases, uuid); 1113 1114 if (cp != NULL) { 1115 fmd_case_transition(cp, FMD_CASE_CLOSE_WAIT, FMD_CF_ISOLATED); 1116 fmd_case_rele(cp); 1117 } 1118 1119 fmd_module_unlock(mp); 1120 } 1121 1122 int 1123 fmd_case_uuclosed(fmd_hdl_t *hdl, const char *uuid) 1124 { 1125 fmd_module_t *mp = fmd_api_module_lock(hdl); 1126 fmd_case_t *cp = fmd_case_hash_lookup(fmd.d_cases, uuid); 1127 fmd_case_impl_t *cip = (fmd_case_impl_t *)cp; 1128 int rv = FMD_B_TRUE; 1129 1130 if (cip != NULL) { 1131 rv = cip->ci_state >= FMD_CASE_CLOSE_WAIT; 1132 fmd_case_rele(cp); 1133 } 1134 1135 fmd_module_unlock(mp); 1136 return (rv); 1137 } 1138 1139 static int 1140 fmd_case_instate(fmd_hdl_t *hdl, fmd_case_t *cp, uint_t state) 1141 { 1142 fmd_module_t *mp = fmd_api_module_lock(hdl); 1143 fmd_case_impl_t *cip = fmd_api_case_impl(mp, cp); 1144 int rv = cip->ci_state >= state; 1145 1146 fmd_module_unlock(mp); 1147 return (rv); 1148 } 1149 1150 int 1151 fmd_case_solved(fmd_hdl_t *hdl, fmd_case_t *cp) 1152 { 1153 return (fmd_case_instate(hdl, cp, FMD_CASE_SOLVED)); 1154 } 1155 1156 int 1157 fmd_case_closed(fmd_hdl_t *hdl, fmd_case_t *cp) 1158 { 1159 return (fmd_case_instate(hdl, cp, FMD_CASE_CLOSE_WAIT)); 1160 } 1161 1162 void 1163 fmd_case_add_ereport(fmd_hdl_t *hdl, fmd_case_t *cp, fmd_event_t *ep) 1164 { 1165 fmd_module_t *mp = fmd_api_module_lock(hdl); 1166 1167 (void) fmd_api_case_impl(mp, cp); /* validate 'cp' */ 1168 1169 if (fmd_case_insert_event(cp, ep)) 1170 mp->mod_stats->ms_accepted.fmds_value.ui64++; 1171 1172 fmd_module_unlock(mp); 1173 } 1174 1175 void 1176 fmd_case_add_serd(fmd_hdl_t *hdl, fmd_case_t *cp, const char *name) 1177 { 1178 fmd_module_t *mp = fmd_api_module_lock(hdl); 1179 fmd_serd_elem_t *sep; 1180 fmd_serd_eng_t *sgp; 1181 1182 if ((sgp = fmd_serd_eng_lookup(&mp->mod_serds, name)) == NULL) { 1183 fmd_api_error(mp, EFMD_SERD_NAME, 1184 "failed to add events from serd engine '%s'", name); 1185 } 1186 1187 (void) fmd_api_case_impl(mp, cp); /* validate 'cp' */ 1188 1189 for (sep = fmd_list_next(&sgp->sg_list); 1190 sep != NULL; sep = fmd_list_next(sep)) { 1191 if (fmd_case_insert_event(cp, sep->se_event)) 1192 mp->mod_stats->ms_accepted.fmds_value.ui64++; 1193 } 1194 1195 fmd_module_unlock(mp); 1196 } 1197 1198 void 1199 fmd_case_add_suspect(fmd_hdl_t *hdl, fmd_case_t *cp, nvlist_t *nvl) 1200 { 1201 fmd_module_t *mp = fmd_api_module_lock(hdl); 1202 fmd_case_impl_t *cip = fmd_api_case_impl(mp, cp); 1203 char *class; 1204 1205 if (cip->ci_state >= FMD_CASE_SOLVED) { 1206 fmd_api_error(mp, EFMD_CASE_STATE, "cannot add suspect to " 1207 "%s: case is already solved or closed\n", cip->ci_uuid); 1208 } 1209 1210 if (nvlist_lookup_string(nvl, FM_CLASS, &class) != 0 || 1211 class == NULL || *class == '\0') { 1212 fmd_api_error(mp, EFMD_CASE_EVENT, "cannot add suspect to " 1213 "%s: suspect event is missing a class\n", cip->ci_uuid); 1214 } 1215 1216 fmd_case_insert_suspect(cp, nvl); 1217 fmd_module_unlock(mp); 1218 } 1219 1220 void 1221 fmd_case_setspecific(fmd_hdl_t *hdl, fmd_case_t *cp, void *data) 1222 { 1223 fmd_module_t *mp = fmd_api_module_lock(hdl); 1224 fmd_case_impl_t *cip = fmd_api_case_impl(mp, cp); 1225 1226 (void) pthread_mutex_lock(&cip->ci_lock); 1227 cip->ci_data = data; 1228 (void) pthread_mutex_unlock(&cip->ci_lock); 1229 1230 fmd_module_unlock(mp); 1231 } 1232 1233 void * 1234 fmd_case_getspecific(fmd_hdl_t *hdl, fmd_case_t *cp) 1235 { 1236 fmd_module_t *mp = fmd_api_module_lock(hdl); 1237 fmd_case_impl_t *cip = fmd_api_case_impl(mp, cp); 1238 void *data; 1239 1240 (void) pthread_mutex_lock(&cip->ci_lock); 1241 data = cip->ci_data; 1242 (void) pthread_mutex_unlock(&cip->ci_lock); 1243 1244 fmd_module_unlock(mp); 1245 return (data); 1246 } 1247 1248 void 1249 fmd_case_setprincipal(fmd_hdl_t *hdl, fmd_case_t *cp, fmd_event_t *ep) 1250 { 1251 fmd_module_t *mp = fmd_api_module_lock(hdl); 1252 1253 (void) fmd_api_case_impl(mp, cp); /* validate 'cp' */ 1254 1255 if (fmd_case_insert_principal(cp, ep)) 1256 mp->mod_stats->ms_accepted.fmds_value.ui64++; 1257 1258 fmd_module_unlock(mp); 1259 } 1260 1261 fmd_event_t * 1262 fmd_case_getprincipal(fmd_hdl_t *hdl, fmd_case_t *cp) 1263 { 1264 fmd_module_t *mp = fmd_api_module_lock(hdl); 1265 fmd_case_impl_t *cip = fmd_api_case_impl(mp, cp); 1266 fmd_event_t *ep; 1267 1268 (void) pthread_mutex_lock(&cip->ci_lock); 1269 ep = cip->ci_principal; 1270 (void) pthread_mutex_unlock(&cip->ci_lock); 1271 1272 fmd_module_unlock(mp); 1273 return (ep); 1274 } 1275 1276 fmd_case_t * 1277 fmd_case_next(fmd_hdl_t *hdl, fmd_case_t *cp) 1278 { 1279 fmd_module_t *mp = fmd_api_module_lock(hdl); 1280 1281 if (cp != NULL) 1282 cp = fmd_list_next(fmd_api_case_impl(mp, cp)); 1283 else 1284 cp = fmd_list_next(&mp->mod_cases); 1285 1286 fmd_module_unlock(mp); 1287 return (cp); 1288 } 1289 1290 fmd_case_t * 1291 fmd_case_prev(fmd_hdl_t *hdl, fmd_case_t *cp) 1292 { 1293 fmd_module_t *mp = fmd_api_module_lock(hdl); 1294 1295 if (cp != NULL) 1296 cp = fmd_list_prev(fmd_api_case_impl(mp, cp)); 1297 else 1298 cp = fmd_list_prev(&mp->mod_cases); 1299 1300 fmd_module_unlock(mp); 1301 return (cp); 1302 } 1303 1304 /* 1305 * Utility function for fmd_buf_* routines. If a case is specified, use the 1306 * case's ci_bufs hash; otherwise use the module's global mod_bufs hash. 1307 */ 1308 static fmd_buf_hash_t * 1309 fmd_buf_gethash(fmd_module_t *mp, fmd_case_t *cp) 1310 { 1311 return (cp ? &fmd_api_case_impl(mp, cp)->ci_bufs : &mp->mod_bufs); 1312 } 1313 1314 void 1315 fmd_buf_create(fmd_hdl_t *hdl, fmd_case_t *cp, const char *name, size_t size) 1316 { 1317 fmd_module_t *mp = fmd_api_module_lock(hdl); 1318 fmd_buf_hash_t *bhp = fmd_buf_gethash(mp, cp); 1319 fmd_buf_t *bp = fmd_buf_lookup(bhp, name); 1320 1321 if (bp == NULL) { 1322 if (fmd_strbadid(name, FMD_B_TRUE) != NULL || size == 0) { 1323 fmd_api_error(mp, EFMD_BUF_INVAL, "cannot create '%s' " 1324 "(size %lu): %s\n", name, (ulong_t)size, 1325 fmd_strerror(EFMD_BUF_INVAL)); 1326 } 1327 1328 if (mp->mod_stats->ms_buflimit.fmds_value.ui64 - 1329 mp->mod_stats->ms_buftotal.fmds_value.ui64 < size) { 1330 fmd_api_error(mp, EFMD_BUF_LIMIT, "cannot create '%s': " 1331 "buf limit exceeded (%llu)\n", name, (u_longlong_t) 1332 mp->mod_stats->ms_buflimit.fmds_value.ui64); 1333 } 1334 1335 mp->mod_stats->ms_buftotal.fmds_value.ui64 += size; 1336 bp = fmd_buf_insert(bhp, name, size); 1337 1338 } else { 1339 fmd_api_error(mp, EFMD_BUF_EXISTS, 1340 "cannot create '%s': buffer already exists\n", name); 1341 } 1342 1343 if (cp != NULL) 1344 fmd_case_setdirty(cp); 1345 else 1346 fmd_module_setdirty(mp); 1347 1348 fmd_module_unlock(mp); 1349 } 1350 1351 void 1352 fmd_buf_destroy(fmd_hdl_t *hdl, fmd_case_t *cp, const char *name) 1353 { 1354 fmd_module_t *mp = fmd_api_module_lock(hdl); 1355 fmd_buf_hash_t *bhp = fmd_buf_gethash(mp, cp); 1356 fmd_buf_t *bp = fmd_buf_lookup(bhp, name); 1357 1358 if (bp != NULL) { 1359 mp->mod_stats->ms_buftotal.fmds_value.ui64 -= bp->buf_size; 1360 fmd_buf_delete(bhp, name); 1361 1362 if (cp != NULL) 1363 fmd_case_setdirty(cp); 1364 else 1365 fmd_module_setdirty(mp); 1366 } 1367 1368 fmd_module_unlock(mp); 1369 } 1370 1371 void 1372 fmd_buf_read(fmd_hdl_t *hdl, fmd_case_t *cp, 1373 const char *name, void *buf, size_t size) 1374 { 1375 fmd_module_t *mp = fmd_api_module_lock(hdl); 1376 fmd_buf_t *bp = fmd_buf_lookup(fmd_buf_gethash(mp, cp), name); 1377 1378 if (bp == NULL) { 1379 fmd_api_error(mp, EFMD_BUF_NOENT, "no buf named '%s' is " 1380 "associated with %s\n", name, cp ? "case" : "module"); 1381 } 1382 1383 bcopy(bp->buf_data, buf, MIN(bp->buf_size, size)); 1384 if (size > bp->buf_size) 1385 bzero((char *)buf + bp->buf_size, size - bp->buf_size); 1386 1387 fmd_module_unlock(mp); 1388 } 1389 1390 void 1391 fmd_buf_write(fmd_hdl_t *hdl, fmd_case_t *cp, 1392 const char *name, const void *buf, size_t size) 1393 { 1394 fmd_module_t *mp = fmd_api_module_lock(hdl); 1395 fmd_buf_hash_t *bhp = fmd_buf_gethash(mp, cp); 1396 fmd_buf_t *bp = fmd_buf_lookup(bhp, name); 1397 1398 if (bp == NULL) { 1399 if (fmd_strbadid(name, FMD_B_TRUE) != NULL || size == 0) { 1400 fmd_api_error(mp, EFMD_BUF_INVAL, "cannot write '%s' " 1401 "(size %lu): %s\n", name, (ulong_t)size, 1402 fmd_strerror(EFMD_BUF_INVAL)); 1403 } 1404 1405 if (mp->mod_stats->ms_buflimit.fmds_value.ui64 - 1406 mp->mod_stats->ms_buftotal.fmds_value.ui64 < size) { 1407 fmd_api_error(mp, EFMD_BUF_LIMIT, "cannot write '%s': " 1408 "buf limit exceeded (%llu)\n", name, (u_longlong_t) 1409 mp->mod_stats->ms_buflimit.fmds_value.ui64); 1410 } 1411 1412 mp->mod_stats->ms_buftotal.fmds_value.ui64 += size; 1413 bp = fmd_buf_insert(bhp, name, size); 1414 1415 } else if (size > bp->buf_size) { 1416 fmd_api_error(mp, EFMD_BUF_OFLOW, 1417 "write to buf '%s' overflows buf size (%lu > %lu)\n", 1418 name, (ulong_t)size, (ulong_t)bp->buf_size); 1419 } 1420 1421 bcopy(buf, bp->buf_data, MIN(bp->buf_size, size)); 1422 bp->buf_flags |= FMD_BUF_DIRTY; 1423 1424 if (cp != NULL) 1425 fmd_case_setdirty(cp); 1426 else 1427 fmd_module_setdirty(mp); 1428 1429 fmd_module_unlock(mp); 1430 } 1431 1432 size_t 1433 fmd_buf_size(fmd_hdl_t *hdl, fmd_case_t *cp, const char *name) 1434 { 1435 fmd_module_t *mp = fmd_api_module_lock(hdl); 1436 fmd_buf_hash_t *bhp = fmd_buf_gethash(mp, cp); 1437 1438 fmd_buf_t *bp; 1439 size_t size; 1440 1441 if ((bp = fmd_buf_lookup(bhp, name)) != NULL) 1442 size = bp->buf_size; 1443 else 1444 size = 0; 1445 1446 fmd_module_unlock(mp); 1447 return (size); 1448 } 1449 1450 void 1451 fmd_serd_create(fmd_hdl_t *hdl, const char *name, uint_t n, hrtime_t t) 1452 { 1453 fmd_module_t *mp = fmd_api_module_lock(hdl); 1454 1455 if (fmd_serd_eng_lookup(&mp->mod_serds, name) != NULL) { 1456 fmd_api_error(mp, EFMD_SERD_EXISTS, 1457 "failed to create serd engine '%s': %s\n", 1458 name, fmd_strerror(EFMD_SERD_EXISTS)); 1459 } 1460 1461 (void) fmd_serd_eng_insert(&mp->mod_serds, name, n, t); 1462 fmd_module_setdirty(mp); 1463 fmd_module_unlock(mp); 1464 } 1465 1466 void 1467 fmd_serd_destroy(fmd_hdl_t *hdl, const char *name) 1468 { 1469 fmd_module_t *mp = fmd_api_module_lock(hdl); 1470 1471 fmd_serd_eng_delete(&mp->mod_serds, name); 1472 fmd_module_setdirty(mp); 1473 fmd_module_unlock(mp); 1474 } 1475 1476 int 1477 fmd_serd_exists(fmd_hdl_t *hdl, const char *name) 1478 { 1479 fmd_module_t *mp = fmd_api_module_lock(hdl); 1480 int rv = (fmd_serd_eng_lookup(&mp->mod_serds, name) != NULL); 1481 fmd_module_unlock(mp); 1482 1483 return (rv); 1484 } 1485 1486 void 1487 fmd_serd_reset(fmd_hdl_t *hdl, const char *name) 1488 { 1489 fmd_module_t *mp = fmd_api_module_lock(hdl); 1490 fmd_serd_eng_t *sgp; 1491 1492 if ((sgp = fmd_serd_eng_lookup(&mp->mod_serds, name)) == NULL) { 1493 fmd_api_error(mp, EFMD_SERD_NAME, 1494 "serd engine '%s' does not exist\n", name); 1495 } 1496 1497 fmd_serd_eng_reset(sgp); 1498 fmd_module_setdirty(mp); 1499 fmd_module_unlock(mp); 1500 } 1501 1502 int 1503 fmd_serd_record(fmd_hdl_t *hdl, const char *name, fmd_event_t *ep) 1504 { 1505 fmd_module_t *mp = fmd_api_module_lock(hdl); 1506 fmd_serd_eng_t *sgp; 1507 int err; 1508 1509 if ((sgp = fmd_serd_eng_lookup(&mp->mod_serds, name)) == NULL) { 1510 fmd_api_error(mp, EFMD_SERD_NAME, 1511 "failed to add record to serd engine '%s'", name); 1512 } 1513 1514 err = fmd_serd_eng_record(sgp, ep); 1515 1516 if (sgp->sg_flags & FMD_SERD_DIRTY) 1517 fmd_module_setdirty(mp); 1518 1519 fmd_module_unlock(mp); 1520 return (err); 1521 } 1522 1523 int 1524 fmd_serd_fired(fmd_hdl_t *hdl, const char *name) 1525 { 1526 fmd_module_t *mp = fmd_api_module_lock(hdl); 1527 fmd_serd_eng_t *sgp; 1528 int err; 1529 1530 if ((sgp = fmd_serd_eng_lookup(&mp->mod_serds, name)) == NULL) { 1531 fmd_api_error(mp, EFMD_SERD_NAME, 1532 "serd engine '%s' does not exist\n", name); 1533 } 1534 1535 err = fmd_serd_eng_fired(sgp); 1536 fmd_module_unlock(mp); 1537 return (err); 1538 } 1539 1540 int 1541 fmd_serd_empty(fmd_hdl_t *hdl, const char *name) 1542 { 1543 fmd_module_t *mp = fmd_api_module_lock(hdl); 1544 fmd_serd_eng_t *sgp; 1545 int empty; 1546 1547 if ((sgp = fmd_serd_eng_lookup(&mp->mod_serds, name)) == NULL) { 1548 fmd_api_error(mp, EFMD_SERD_NAME, 1549 "serd engine '%s' does not exist\n", name); 1550 } 1551 1552 empty = fmd_serd_eng_empty(sgp); 1553 fmd_module_unlock(mp); 1554 return (empty); 1555 } 1556 1557 pthread_t 1558 fmd_thr_create(fmd_hdl_t *hdl, void (*func)(void *), void *arg) 1559 { 1560 fmd_module_t *mp = fmd_api_module_lock(hdl); 1561 fmd_thread_t *tp; 1562 pthread_t tid; 1563 1564 if (mp->mod_stats->ms_thrtotal.fmds_value.ui32 >= 1565 mp->mod_stats->ms_thrlimit.fmds_value.ui32) { 1566 fmd_api_error(mp, EFMD_THR_LIMIT, "%s request to create an " 1567 "auxiliary thread exceeds module thread limit (%u)\n", 1568 mp->mod_name, mp->mod_stats->ms_thrlimit.fmds_value.ui32); 1569 } 1570 1571 if ((tp = fmd_thread_create(mp, func, arg)) == NULL) { 1572 fmd_api_error(mp, EFMD_THR_CREATE, 1573 "failed to create auxiliary thread"); 1574 } 1575 1576 tid = tp->thr_tid; 1577 mp->mod_stats->ms_thrtotal.fmds_value.ui32++; 1578 (void) fmd_idspace_xalloc(mp->mod_threads, tid, tp); 1579 1580 fmd_module_unlock(mp); 1581 return (tid); 1582 } 1583 1584 void 1585 fmd_thr_destroy(fmd_hdl_t *hdl, pthread_t tid) 1586 { 1587 fmd_module_t *mp = fmd_api_module_lock(hdl); 1588 fmd_thread_t *tp; 1589 int err; 1590 1591 if (pthread_self() == tid) { 1592 fmd_api_error(mp, EFMD_THR_INVAL, "auxiliary thread tried to " 1593 "destroy itself (tid %u)\n", tid); 1594 } 1595 1596 if ((tp = fmd_idspace_getspecific(mp->mod_threads, tid)) == NULL) { 1597 fmd_api_error(mp, EFMD_THR_INVAL, "auxiliary thread tried to " 1598 "destroy an invalid thread (tid %u)\n", tid); 1599 } 1600 1601 /* 1602 * Wait for the specified thread to exit and then join with it. Since 1603 * the thread may need to make API calls in order to complete its work 1604 * we must sleep with the module lock unheld, and then reacquire it. 1605 */ 1606 fmd_module_unlock(mp); 1607 err = pthread_join(tid, NULL); 1608 mp = fmd_api_module_lock(hdl); 1609 1610 /* 1611 * Since pthread_join() was called without the module lock held, if 1612 * multiple callers attempted to destroy the same auxiliary thread 1613 * simultaneously, one will succeed and the others will get ESRCH. 1614 * Therefore we silently ignore ESRCH but only allow the caller who 1615 * succeessfully joined with the auxiliary thread to destroy it. 1616 */ 1617 if (err != 0 && err != ESRCH) { 1618 fmd_api_error(mp, EFMD_THR_JOIN, 1619 "failed to join with auxiliary thread %u\n", tid); 1620 } 1621 1622 if (err == 0) { 1623 fmd_thread_destroy(tp, FMD_THREAD_NOJOIN); 1624 mp->mod_stats->ms_thrtotal.fmds_value.ui32--; 1625 (void) fmd_idspace_free(mp->mod_threads, tid); 1626 } 1627 1628 fmd_module_unlock(mp); 1629 } 1630 1631 void 1632 fmd_thr_signal(fmd_hdl_t *hdl, pthread_t tid) 1633 { 1634 fmd_module_t *mp = fmd_api_module_lock(hdl); 1635 1636 if (tid != mp->mod_thread->thr_tid && 1637 fmd_idspace_getspecific(mp->mod_threads, tid) == NULL) { 1638 fmd_api_error(mp, EFMD_THR_INVAL, "tid %u is not a valid " 1639 "thread id for module %s\n", tid, mp->mod_name); 1640 } 1641 1642 (void) pthread_kill(tid, fmd.d_thr_sig); 1643 fmd_module_unlock(mp); 1644 } 1645 1646 id_t 1647 fmd_timer_install(fmd_hdl_t *hdl, void *arg, fmd_event_t *ep, hrtime_t delta) 1648 { 1649 fmd_module_t *mp = fmd_api_module_lock(hdl); 1650 fmd_modtimer_t *t; 1651 id_t id; 1652 1653 if (delta < 0) { 1654 fmd_api_error(mp, EFMD_TIMER_INVAL, 1655 "timer delta %lld is not a valid interval\n", delta); 1656 } 1657 1658 t = fmd_alloc(sizeof (fmd_modtimer_t), FMD_SLEEP); 1659 t->mt_mod = mp; 1660 t->mt_arg = arg; 1661 t->mt_id = -1; 1662 1663 if ((id = fmd_timerq_install(fmd.d_timers, mp->mod_timerids, 1664 (fmd_timer_f *)fmd_module_timeout, t, ep, delta)) == -1) { 1665 fmd_free(t, sizeof (fmd_modtimer_t)); 1666 fmd_api_error(mp, EFMD_TIMER_LIMIT, 1667 "failed to install timer +%lld", delta); 1668 } 1669 1670 fmd_module_unlock(mp); 1671 return (id); 1672 } 1673 1674 void 1675 fmd_timer_remove(fmd_hdl_t *hdl, id_t id) 1676 { 1677 fmd_module_t *mp = fmd_api_module_lock(hdl); 1678 fmd_modtimer_t *t; 1679 1680 if (!fmd_idspace_valid(mp->mod_timerids, id)) { 1681 fmd_api_error(mp, EFMD_TIMER_INVAL, 1682 "id %ld is not a valid timer id\n", id); 1683 } 1684 1685 /* 1686 * If the timer has not fired (t != NULL), remove it from the timer 1687 * queue. If the timer has fired (t == NULL), we could be in one of 1688 * two situations: a) we are processing the timer callback or b) 1689 * the timer event is on the module queue awaiting dispatch. For a), 1690 * fmd_timerq_remove() will wait for the timer callback function 1691 * to complete and queue an event for dispatch. For a) and b), 1692 * we cancel the outstanding timer event from the module's dispatch 1693 * queue. 1694 */ 1695 if ((t = fmd_timerq_remove(fmd.d_timers, mp->mod_timerids, id)) != NULL) 1696 fmd_free(t, sizeof (fmd_modtimer_t)); 1697 fmd_module_unlock(mp); 1698 1699 fmd_eventq_cancel(mp->mod_queue, FMD_EVT_TIMEOUT, (void *)id); 1700 } 1701 1702 nvlist_t * 1703 fmd_nvl_create_fault(fmd_hdl_t *hdl, const char *class, 1704 uint8_t certainty, nvlist_t *asru, nvlist_t *fru, nvlist_t *rsrc) 1705 { 1706 fmd_module_t *mp; 1707 topo_hdl_t *thp; 1708 nvlist_t *nvl; 1709 char *loc = NULL, *serial = NULL; 1710 int err; 1711 1712 mp = fmd_api_module_lock(hdl); 1713 if (class == NULL || class[0] == '\0') 1714 fmd_api_error(mp, EFMD_NVL_INVAL, "invalid fault class\n"); 1715 1716 thp = fmd_module_topo_hold(mp); 1717 1718 /* 1719 * Try to find the location label for this resource 1720 */ 1721 (void) topo_fmri_label(thp, fru, &loc, &err); 1722 1723 /* 1724 * In some cases, serial information for the resource will not be 1725 * available at enumeration but may instead be available by invoking 1726 * a dynamic property method on the FRU. In order to ensure the serial 1727 * number is persisted properly in the ASRU cache, we'll fetch the 1728 * property, if it exists, and add it to the resource and fru fmris. 1729 */ 1730 if (fru != NULL) { 1731 (void) topo_fmri_serial(thp, fru, &serial, &err); 1732 if (serial != NULL) { 1733 (void) nvlist_add_string(rsrc, "serial", serial); 1734 (void) nvlist_add_string(fru, "serial", serial); 1735 topo_hdl_strfree(thp, serial); 1736 } 1737 } 1738 nvl = fmd_protocol_fault(class, certainty, asru, fru, rsrc, loc); 1739 1740 if (loc != NULL) 1741 topo_hdl_strfree(thp, loc); 1742 1743 err = fmd_module_topo_rele(mp, thp); 1744 ASSERT(err == 0); 1745 1746 fmd_module_unlock(mp); 1747 1748 return (nvl); 1749 } 1750 1751 int 1752 fmd_nvl_class_match(fmd_hdl_t *hdl, nvlist_t *nvl, const char *pattern) 1753 { 1754 fmd_module_t *mp = fmd_api_module_lock(hdl); 1755 char *class; 1756 int rv; 1757 1758 rv = (nvl != NULL && nvlist_lookup_string(nvl, 1759 FM_CLASS, &class) == 0 && fmd_strmatch(class, pattern)); 1760 1761 fmd_module_unlock(mp); 1762 return (rv); 1763 } 1764 1765 int 1766 fmd_nvl_fmri_expand(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_expand(nvl); 1777 fmd_module_unlock(mp); 1778 return (rv); 1779 } 1780 1781 int 1782 fmd_nvl_fmri_present(fmd_hdl_t *hdl, nvlist_t *nvl) 1783 { 1784 fmd_module_t *mp = fmd_api_module_lock(hdl); 1785 int rv; 1786 1787 if (nvl == NULL) { 1788 fmd_api_error(mp, EFMD_NVL_INVAL, 1789 "invalid nvlist %p\n", (void *)nvl); 1790 } 1791 1792 rv = fmd_fmri_present(nvl); 1793 fmd_module_unlock(mp); 1794 1795 if (rv < 0) { 1796 fmd_api_error(mp, EFMD_FMRI_OP, "invalid fmri for " 1797 "fmd_nvl_fmri_present\n"); 1798 } 1799 1800 return (rv); 1801 } 1802 1803 int 1804 fmd_nvl_fmri_unusable(fmd_hdl_t *hdl, nvlist_t *nvl) 1805 { 1806 fmd_module_t *mp = fmd_api_module_lock(hdl); 1807 int rv; 1808 1809 if (nvl == NULL) { 1810 fmd_api_error(mp, EFMD_NVL_INVAL, 1811 "invalid nvlist %p\n", (void *)nvl); 1812 } 1813 1814 rv = fmd_fmri_unusable(nvl); 1815 fmd_module_unlock(mp); 1816 1817 if (rv < 0) { 1818 fmd_api_error(mp, EFMD_FMRI_OP, "invalid fmri for " 1819 "fmd_nvl_fmri_unusable\n"); 1820 } 1821 1822 return (rv); 1823 } 1824 1825 int 1826 fmd_nvl_fmri_faulty(fmd_hdl_t *hdl, nvlist_t *nvl) 1827 { 1828 fmd_module_t *mp = fmd_api_module_lock(hdl); 1829 fmd_asru_hash_t *ahp = fmd.d_asrus; 1830 fmd_asru_t *ap; 1831 int rv = 0; 1832 1833 if (nvl == NULL) { 1834 fmd_api_error(mp, EFMD_NVL_INVAL, 1835 "invalid nvlist %p\n", (void *)nvl); 1836 } 1837 1838 if ((ap = fmd_asru_hash_lookup_nvl(ahp, nvl)) != NULL) { 1839 rv = (ap->asru_flags & FMD_ASRU_FAULTY) != 0; 1840 fmd_asru_hash_release(ahp, ap); 1841 } 1842 1843 fmd_module_unlock(mp); 1844 return (rv); 1845 } 1846 1847 int 1848 fmd_nvl_fmri_contains(fmd_hdl_t *hdl, nvlist_t *n1, nvlist_t *n2) 1849 { 1850 fmd_module_t *mp = fmd_api_module_lock(hdl); 1851 int rv; 1852 1853 if (n1 == NULL || n2 == NULL) { 1854 fmd_api_error(mp, EFMD_NVL_INVAL, 1855 "invalid nvlist(s): %p, %p\n", (void *)n1, (void *)n2); 1856 } 1857 1858 rv = fmd_fmri_contains(n1, n2); 1859 fmd_module_unlock(mp); 1860 1861 if (rv < 0) { 1862 fmd_api_error(mp, EFMD_FMRI_OP, "invalid fmri for " 1863 "fmd_nvl_fmri_contains\n"); 1864 } 1865 1866 return (rv); 1867 } 1868 1869 nvlist_t * 1870 fmd_nvl_fmri_translate(fmd_hdl_t *hdl, nvlist_t *fmri, nvlist_t *auth) 1871 { 1872 fmd_module_t *mp = fmd_api_module_lock(hdl); 1873 nvlist_t *xfmri; 1874 1875 if (fmri == NULL || auth == NULL) { 1876 fmd_api_error(mp, EFMD_NVL_INVAL, 1877 "invalid nvlist(s): %p, %p\n", (void *)fmri, (void *)auth); 1878 } 1879 1880 xfmri = fmd_fmri_translate(fmri, auth); 1881 fmd_module_unlock(mp); 1882 return (xfmri); 1883 } 1884 1885 static int 1886 fmd_nvl_op_init(nv_alloc_t *ops, va_list ap) 1887 { 1888 fmd_module_t *mp = va_arg(ap, fmd_module_t *); 1889 1890 ops->nva_arg = mp; 1891 1892 return (0); 1893 } 1894 1895 static void * 1896 fmd_nvl_op_alloc_sleep(nv_alloc_t *ops, size_t size) 1897 { 1898 fmd_module_t *mp = ops->nva_arg; 1899 1900 return (fmd_hdl_alloc_locked(mp, size, FMD_SLEEP)); 1901 } 1902 1903 static void * 1904 fmd_nvl_op_alloc_nosleep(nv_alloc_t *ops, size_t size) 1905 { 1906 fmd_module_t *mp = ops->nva_arg; 1907 1908 return (fmd_hdl_alloc_locked(mp, size, FMD_NOSLEEP)); 1909 } 1910 1911 static void 1912 fmd_nvl_op_free(nv_alloc_t *ops, void *data, size_t size) 1913 { 1914 fmd_module_t *mp = ops->nva_arg; 1915 1916 fmd_hdl_free_locked(mp, data, size); 1917 } 1918 1919 nv_alloc_ops_t fmd_module_nva_ops_sleep = { 1920 fmd_nvl_op_init, 1921 NULL, 1922 fmd_nvl_op_alloc_sleep, 1923 fmd_nvl_op_free, 1924 NULL 1925 }; 1926 1927 nv_alloc_ops_t fmd_module_nva_ops_nosleep = { 1928 fmd_nvl_op_init, 1929 NULL, 1930 fmd_nvl_op_alloc_nosleep, 1931 fmd_nvl_op_free, 1932 NULL 1933 }; 1934 1935 nvlist_t * 1936 fmd_nvl_alloc(fmd_hdl_t *hdl, int flags) 1937 { 1938 fmd_module_t *mp = fmd_api_module_lock(hdl); 1939 nv_alloc_t *nva; 1940 nvlist_t *nvl; 1941 int ret; 1942 1943 if (flags == FMD_SLEEP) 1944 nva = &mp->mod_nva_sleep; 1945 else 1946 nva = &mp->mod_nva_nosleep; 1947 1948 ret = nvlist_xalloc(&nvl, NV_UNIQUE_NAME, nva); 1949 1950 fmd_module_unlock(mp); 1951 1952 if (ret != 0) 1953 return (NULL); 1954 else 1955 return (nvl); 1956 } 1957 1958 nvlist_t * 1959 fmd_nvl_dup(fmd_hdl_t *hdl, nvlist_t *src, int flags) 1960 { 1961 fmd_module_t *mp = fmd_api_module_lock(hdl); 1962 nv_alloc_t *nva; 1963 nvlist_t *nvl; 1964 int ret; 1965 1966 if (flags == FMD_SLEEP) 1967 nva = &mp->mod_nva_sleep; 1968 else 1969 nva = &mp->mod_nva_nosleep; 1970 1971 ret = nvlist_xdup(src, &nvl, nva); 1972 1973 fmd_module_unlock(mp); 1974 1975 if (ret != 0) 1976 return (NULL); 1977 else 1978 return (nvl); 1979 } 1980 1981 int 1982 fmd_event_local(fmd_hdl_t *hdl, fmd_event_t *ep) 1983 { 1984 if (hdl == NULL || ep == NULL) { 1985 fmd_api_error(fmd_api_module_lock(hdl), EFMD_EVENT_INVAL, 1986 "NULL parameter specified to fmd_event_local\n"); 1987 } 1988 1989 return (((fmd_event_impl_t *)ep)->ev_flags & FMD_EVF_LOCAL); 1990 } 1991 1992 /*ARGSUSED*/ 1993 uint64_t 1994 fmd_event_ena_create(fmd_hdl_t *hdl) 1995 { 1996 return (fmd_ena()); 1997 } 1998 1999 fmd_xprt_t * 2000 fmd_xprt_open(fmd_hdl_t *hdl, uint_t flags, nvlist_t *auth, void *data) 2001 { 2002 fmd_module_t *mp = fmd_api_module_lock(hdl); 2003 fmd_xprt_t *xp; 2004 2005 if (flags & ~FMD_XPRT_CMASK) { 2006 fmd_api_error(mp, EFMD_XPRT_INVAL, 2007 "invalid transport flags 0x%x\n", flags); 2008 } 2009 2010 if ((flags & FMD_XPRT_RDWR) != FMD_XPRT_RDWR && 2011 (flags & FMD_XPRT_RDWR) != FMD_XPRT_RDONLY) { 2012 fmd_api_error(mp, EFMD_XPRT_INVAL, 2013 "cannot open write-only transport\n"); 2014 } 2015 2016 if (mp->mod_stats->ms_xprtopen.fmds_value.ui32 >= 2017 mp->mod_stats->ms_xprtlimit.fmds_value.ui32) { 2018 fmd_api_error(mp, EFMD_XPRT_LIMIT, "%s request to create a " 2019 "transport exceeds module transport limit (%u)\n", 2020 mp->mod_name, mp->mod_stats->ms_xprtlimit.fmds_value.ui32); 2021 } 2022 2023 if ((xp = fmd_xprt_create(mp, flags, auth, data)) == NULL) 2024 fmd_api_error(mp, errno, "cannot create transport"); 2025 2026 fmd_module_unlock(mp); 2027 return (xp); 2028 } 2029 2030 void 2031 fmd_xprt_close(fmd_hdl_t *hdl, fmd_xprt_t *xp) 2032 { 2033 fmd_module_t *mp = fmd_api_module_lock(hdl); 2034 fmd_xprt_impl_t *xip = fmd_api_transport_impl(hdl, xp); 2035 2036 /* 2037 * Although this could be supported, it doesn't seem necessary or worth 2038 * the trouble. For now, just detect this and trigger a module abort. 2039 * If it is needed, transports should grow reference counts and a new 2040 * event type will need to be enqueued for the main thread to reap it. 2041 */ 2042 if (xip->xi_thread != NULL && 2043 xip->xi_thread->thr_tid == pthread_self()) { 2044 fmd_api_error(mp, EFMD_XPRT_INVAL, 2045 "fmd_xprt_close() cannot be called from fmdo_send()\n"); 2046 } 2047 2048 fmd_xprt_destroy(xp); 2049 fmd_module_unlock(mp); 2050 } 2051 2052 void 2053 fmd_xprt_post(fmd_hdl_t *hdl, fmd_xprt_t *xp, nvlist_t *nvl, hrtime_t hrt) 2054 { 2055 fmd_xprt_impl_t *xip = fmd_api_transport_impl(hdl, xp); 2056 2057 /* 2058 * fmd_xprt_recv() must block during startup waiting for fmd to globally 2059 * clear FMD_XPRT_DSUSPENDED. As such, we can't allow it to be called 2060 * from a module's _fmd_init() routine, because that would block 2061 * fmd from completing initial module loading, resulting in a deadlock. 2062 */ 2063 if ((xip->xi_flags & FMD_XPRT_ISUSPENDED) && 2064 (pthread_self() == xip->xi_queue->eq_mod->mod_thread->thr_tid)) { 2065 fmd_api_error(fmd_api_module_lock(hdl), EFMD_XPRT_INVAL, 2066 "fmd_xprt_post() cannot be called from _fmd_init()\n"); 2067 } 2068 2069 fmd_xprt_recv(xp, nvl, hrt); 2070 } 2071 2072 void 2073 fmd_xprt_suspend(fmd_hdl_t *hdl, fmd_xprt_t *xp) 2074 { 2075 (void) fmd_api_transport_impl(hdl, xp); /* validate 'xp' */ 2076 fmd_xprt_xsuspend(xp, FMD_XPRT_SUSPENDED); 2077 } 2078 2079 void 2080 fmd_xprt_resume(fmd_hdl_t *hdl, fmd_xprt_t *xp) 2081 { 2082 (void) fmd_api_transport_impl(hdl, xp); /* validate 'xp' */ 2083 fmd_xprt_xresume(xp, FMD_XPRT_SUSPENDED); 2084 } 2085 2086 int 2087 fmd_xprt_error(fmd_hdl_t *hdl, fmd_xprt_t *xp) 2088 { 2089 fmd_xprt_impl_t *xip = fmd_api_transport_impl(hdl, xp); 2090 return (xip->xi_state == _fmd_xprt_state_err); 2091 } 2092 2093 /* 2094 * Translate all FMRIs in the specified name-value pair list for the specified 2095 * FMRI authority, and return a new name-value pair list for the translation. 2096 * This function is the recursive engine used by fmd_xprt_translate(), below. 2097 */ 2098 static nvlist_t * 2099 fmd_xprt_xtranslate(nvlist_t *nvl, nvlist_t *auth) 2100 { 2101 uint_t i, j, n; 2102 nvpair_t *nvp, **nvps; 2103 uint_t nvpslen = 0; 2104 char *name; 2105 size_t namelen = 0; 2106 2107 nvlist_t **a, **b; 2108 nvlist_t *l, *r; 2109 data_type_t type; 2110 char *s; 2111 int err; 2112 2113 (void) nvlist_xdup(nvl, &nvl, &fmd.d_nva); 2114 2115 /* 2116 * Count up the number of name-value pairs in 'nvl' and compute the 2117 * maximum length of a name used in this list for use below. 2118 */ 2119 for (nvp = nvlist_next_nvpair(nvl, NULL); 2120 nvp != NULL; nvp = nvlist_next_nvpair(nvl, nvp), nvpslen++) { 2121 size_t len = strlen(nvpair_name(nvp)); 2122 namelen = MAX(namelen, len); 2123 } 2124 2125 nvps = alloca(sizeof (nvpair_t *) * nvpslen); 2126 name = alloca(namelen + 1); 2127 2128 /* 2129 * Store a snapshot of the name-value pairs in 'nvl' into nvps[] so 2130 * that we can iterate over the original pairs in the loop below while 2131 * performing arbitrary insert and delete operations on 'nvl' itself. 2132 */ 2133 for (i = 0, nvp = nvlist_next_nvpair(nvl, NULL); 2134 nvp != NULL; nvp = nvlist_next_nvpair(nvl, nvp)) 2135 nvps[i++] = nvp; 2136 2137 /* 2138 * Now iterate over the snapshot of the name-value pairs. If we find a 2139 * value that is of type NVLIST or NVLIST_ARRAY, we translate that 2140 * object by either calling ourself recursively on it, or calling into 2141 * fmd_fmri_translate() if the object is an FMRI. We then rip out the 2142 * original name-value pair and replace it with the translated one. 2143 */ 2144 for (i = 0; i < nvpslen; i++) { 2145 nvp = nvps[i]; 2146 type = nvpair_type(nvp); 2147 2148 switch (type) { 2149 case DATA_TYPE_NVLIST_ARRAY: 2150 if (nvpair_value_nvlist_array(nvp, &a, &n) != 0 || 2151 a == NULL || n == 0) 2152 continue; /* array is zero-sized; skip it */ 2153 2154 b = fmd_alloc(sizeof (nvlist_t *) * n, FMD_SLEEP); 2155 2156 /* 2157 * If the first array nvlist element looks like an FMRI 2158 * then assume the other elements are FMRIs as well. 2159 * If any b[j]'s can't be translated, then EINVAL will 2160 * be returned from nvlist_add_nvlist_array() below. 2161 */ 2162 if (nvlist_lookup_string(*a, FM_FMRI_SCHEME, &s) == 0) { 2163 for (j = 0; j < n; j++) 2164 b[j] = fmd_fmri_translate(a[j], auth); 2165 } else { 2166 for (j = 0; j < n; j++) 2167 b[j] = fmd_xprt_xtranslate(a[j], auth); 2168 } 2169 2170 (void) strcpy(name, nvpair_name(nvp)); 2171 (void) nvlist_remove(nvl, name, type); 2172 err = nvlist_add_nvlist_array(nvl, name, b, n); 2173 2174 for (j = 0; j < n; j++) 2175 nvlist_free(b[j]); 2176 2177 fmd_free(b, sizeof (nvlist_t *) * n); 2178 2179 if (err != 0) { 2180 nvlist_free(nvl); 2181 errno = err; 2182 return (NULL); 2183 } 2184 break; 2185 2186 case DATA_TYPE_NVLIST: 2187 if (nvpair_value_nvlist(nvp, &l) == 0 && 2188 nvlist_lookup_string(l, FM_FMRI_SCHEME, &s) == 0) 2189 r = fmd_fmri_translate(l, auth); 2190 else 2191 r = fmd_xprt_xtranslate(l, auth); 2192 2193 if (r == NULL) { 2194 nvlist_free(nvl); 2195 return (NULL); 2196 } 2197 2198 (void) strcpy(name, nvpair_name(nvp)); 2199 (void) nvlist_remove(nvl, name, type); 2200 (void) nvlist_add_nvlist(nvl, name, r); 2201 2202 nvlist_free(r); 2203 break; 2204 } 2205 } 2206 2207 return (nvl); 2208 } 2209 2210 nvlist_t * 2211 fmd_xprt_translate(fmd_hdl_t *hdl, fmd_xprt_t *xp, fmd_event_t *ep) 2212 { 2213 fmd_xprt_impl_t *xip = fmd_api_transport_impl(hdl, xp); 2214 2215 if (xip->xi_auth == NULL) { 2216 fmd_api_error(fmd_api_module_lock(hdl), EFMD_XPRT_INVAL, 2217 "no authority defined for transport %p\n", (void *)xp); 2218 } 2219 2220 return (fmd_xprt_xtranslate(FMD_EVENT_NVL(ep), xip->xi_auth)); 2221 } 2222 2223 void 2224 fmd_xprt_setspecific(fmd_hdl_t *hdl, fmd_xprt_t *xp, void *data) 2225 { 2226 fmd_api_transport_impl(hdl, xp)->xi_data = data; 2227 } 2228 2229 void * 2230 fmd_xprt_getspecific(fmd_hdl_t *hdl, fmd_xprt_t *xp) 2231 { 2232 return (fmd_api_transport_impl(hdl, xp)->xi_data); 2233 } 2234