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 2009 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 #include <sys/types.h> 28 #include <sys/utsname.h> 29 #include <sys/param.h> 30 #include <sys/systeminfo.h> 31 #include <sys/fm/util.h> 32 33 #include <smbios.h> 34 #include <limits.h> 35 #include <unistd.h> 36 #include <signal.h> 37 #include <stdlib.h> 38 #include <stdio.h> 39 #include <ctype.h> 40 #include <door.h> 41 42 #include <fmd_conf.h> 43 #include <fmd_dispq.h> 44 #include <fmd_timerq.h> 45 #include <fmd_subr.h> 46 #include <fmd_error.h> 47 #include <fmd_module.h> 48 #include <fmd_thread.h> 49 #include <fmd_alloc.h> 50 #include <fmd_string.h> 51 #include <fmd_builtin.h> 52 #include <fmd_ustat.h> 53 #include <fmd_protocol.h> 54 #include <fmd_scheme.h> 55 #include <fmd_asru.h> 56 #include <fmd_case.h> 57 #include <fmd_log.h> 58 #include <fmd_idspace.h> 59 #include <fmd_rpc.h> 60 #include <fmd_dr.h> 61 #include <fmd_topo.h> 62 #include <fmd_xprt.h> 63 #include <fmd_ctl.h> 64 #include <sys/openpromio.h> 65 #include <libdevinfo.h> 66 67 #include <fmd.h> 68 69 extern const nv_alloc_ops_t fmd_nv_alloc_ops; /* see fmd_nv.c */ 70 71 const char _fmd_version[] = "1.2"; /* daemon version string */ 72 static char _fmd_plat[MAXNAMELEN]; /* native platform string */ 73 static char _fmd_isa[MAXNAMELEN]; /* native instruction set */ 74 static struct utsname _fmd_uts; /* native uname(2) info */ 75 static char _fmd_csn[MAXNAMELEN]; /* chassis serial number */ 76 static char _fmd_prod[MAXNAMELEN]; /* product name string */ 77 78 /* 79 * Note: the configuration file path is ordered from most common to most host- 80 * specific because new conf files are merged/override previous ones. The 81 * module paths are in the opposite order, from most specific to most common, 82 * because once a module is loaded fmd will not try to load over the same name. 83 */ 84 85 static const char _fmd_conf_path[] = 86 "%r/usr/lib/fm/fmd:" 87 "%r/usr/platform/%m/lib/fm/fmd:" 88 "%r/usr/platform/%i/lib/fm/fmd:" 89 "%r/etc/fm/fmd"; 90 91 static const char _fmd_agent_path[] = 92 "%r/usr/platform/%i/lib/fm/fmd/agents:" 93 "%r/usr/platform/%m/lib/fm/fmd/agents:" 94 "%r/usr/lib/fm/fmd/agents"; 95 96 static const char _fmd_plugin_path[] = 97 "%r/usr/platform/%i/lib/fm/fmd/plugins:" 98 "%r/usr/platform/%m/lib/fm/fmd/plugins:" 99 "%r/usr/lib/fm/fmd/plugins"; 100 101 static const char _fmd_scheme_path[] = 102 "usr/lib/fm/fmd/schemes"; 103 104 static const fmd_conf_mode_t _fmd_cerror_modes[] = { 105 { "unload", "unload offending client module", FMD_CERROR_UNLOAD }, 106 { "stop", "stop daemon for debugger attach", FMD_CERROR_STOP }, 107 { "abort", "abort daemon and force core dump", FMD_CERROR_ABORT }, 108 { NULL, NULL, 0 } 109 }; 110 111 static const fmd_conf_mode_t _fmd_dbout_modes[] = { 112 { "stderr", "send debug messages to stderr", FMD_DBOUT_STDERR }, 113 { "syslog", "send debug messages to syslog", FMD_DBOUT_SYSLOG }, 114 { NULL, NULL, 0 } 115 }; 116 117 static const fmd_conf_mode_t _fmd_debug_modes[] = { 118 { "help", "display debugging modes and exit", FMD_DBG_HELP }, 119 { "mod", "debug module load/unload/locking", FMD_DBG_MOD }, 120 { "disp", "debug dispatch queue processing", FMD_DBG_DISP }, 121 { "xprt", "debug transport-specific routines", FMD_DBG_XPRT }, 122 { "evt", "debug event subsystem routines", FMD_DBG_EVT }, 123 { "log", "debug log subsystem routines", FMD_DBG_LOG }, 124 { "tmr", "debug timer subsystem routines", FMD_DBG_TMR }, 125 { "fmri", "debug fmri subsystem routines", FMD_DBG_FMRI }, 126 { "asru", "debug asru subsystem routines", FMD_DBG_ASRU }, 127 { "case", "debug case subsystem routines", FMD_DBG_CASE }, 128 { "ckpt", "debug checkpoint routines", FMD_DBG_CKPT }, 129 { "rpc", "debug rpc service routines", FMD_DBG_RPC }, 130 { "trace", "display matching trace calls", FMD_DBG_TRACE }, 131 { "all", "enable all available debug modes", FMD_DBG_ALL }, 132 { NULL, NULL, 0 } 133 }; 134 135 static int 136 fmd_cerror_set(fmd_conf_param_t *pp, const char *value) 137 { 138 return (fmd_conf_mode_set(_fmd_cerror_modes, pp, value)); 139 } 140 141 static int 142 fmd_dbout_set(fmd_conf_param_t *pp, const char *value) 143 { 144 return (fmd_conf_mode_set(_fmd_dbout_modes, pp, value)); 145 } 146 147 static int 148 fmd_debug_set(fmd_conf_param_t *pp, const char *value) 149 { 150 int err = fmd_conf_mode_set(_fmd_debug_modes, pp, value); 151 152 if (err == 0) 153 fmd.d_fmd_debug = pp->cp_value.cpv_num; 154 155 return (err); 156 } 157 158 static int 159 fmd_trmode_set(fmd_conf_param_t *pp, const char *value) 160 { 161 fmd_tracebuf_f *func; 162 163 if (strcasecmp(value, "none") == 0) 164 func = fmd_trace_none; 165 else if (strcasecmp(value, "lite") == 0) 166 func = fmd_trace_lite; 167 else if (strcasecmp(value, "full") == 0) 168 func = fmd_trace_full; 169 else 170 return (fmd_set_errno(EFMD_CONF_INVAL)); 171 172 fmd.d_thr_trace = (void (*)())func; 173 pp->cp_value.cpv_ptr = (void *)func; 174 return (0); 175 } 176 177 static void 178 fmd_trmode_get(const fmd_conf_param_t *pp, void *ptr) 179 { 180 *((void **)ptr) = pp->cp_value.cpv_ptr; 181 } 182 183 static int 184 fmd_clkmode_set(fmd_conf_param_t *pp, const char *value) 185 { 186 const fmd_timeops_t *ops; 187 188 if (strcasecmp(value, "native") == 0) 189 ops = &fmd_timeops_native; 190 else if (strcasecmp(value, "simulated") == 0) 191 ops = &fmd_timeops_simulated; 192 else 193 return (fmd_set_errno(EFMD_CONF_INVAL)); 194 195 fmd.d_clockops = ops; 196 pp->cp_value.cpv_ptr = (void *)ops; 197 return (0); 198 } 199 200 static void 201 fmd_clkmode_get(const fmd_conf_param_t *pp, void *ptr) 202 { 203 *((void **)ptr) = pp->cp_value.cpv_ptr; 204 } 205 206 static const fmd_conf_ops_t fmd_cerror_ops = { 207 fmd_cerror_set, fmd_conf_mode_get, fmd_conf_notsup, fmd_conf_nop 208 }; 209 210 static const fmd_conf_ops_t fmd_dbout_ops = { 211 fmd_dbout_set, fmd_conf_mode_get, fmd_conf_notsup, fmd_conf_nop 212 }; 213 214 static const fmd_conf_ops_t fmd_debug_ops = { 215 fmd_debug_set, fmd_conf_mode_get, fmd_conf_notsup, fmd_conf_nop 216 }; 217 218 static const fmd_conf_ops_t fmd_trmode_ops = { 219 fmd_trmode_set, fmd_trmode_get, fmd_conf_notsup, fmd_conf_nop 220 }; 221 222 static const fmd_conf_ops_t fmd_clkmode_ops = { 223 fmd_clkmode_set, fmd_clkmode_get, fmd_conf_notsup, fmd_conf_nop 224 }; 225 226 static const fmd_conf_formal_t _fmd_conf[] = { 227 { "agent.path", &fmd_conf_path, _fmd_agent_path }, /* path for agents */ 228 { "alloc_msecs", &fmd_conf_uint32, "10" }, /* msecs before alloc retry */ 229 { "alloc_tries", &fmd_conf_uint32, "3" }, /* max # of alloc retries */ 230 { "chassis", &fmd_conf_string, _fmd_csn }, /* chassis serial number */ 231 { "ckpt.dir", &fmd_conf_string, "var/fm/fmd/ckpt" }, /* ckpt directory path */ 232 { "ckpt.dirmode", &fmd_conf_int32, "0700" }, /* ckpt directory perm mode */ 233 { "ckpt.mode", &fmd_conf_int32, "0400" }, /* ckpt file perm mode */ 234 { "ckpt.restore", &fmd_conf_bool, "true" }, /* restore checkpoints? */ 235 { "ckpt.save", &fmd_conf_bool, "true" }, /* save checkpoints? */ 236 { "ckpt.zero", &fmd_conf_bool, "false" }, /* zero checkpoints on start? */ 237 { "client.buflim", &fmd_conf_size, "10m" }, /* client buffer space limit */ 238 { "client.dbout", &fmd_dbout_ops, NULL }, /* client debug output sinks */ 239 { "client.debug", &fmd_conf_bool, NULL }, /* client debug enable */ 240 { "client.error", &fmd_cerror_ops, "unload" }, /* client error policy */ 241 { "client.memlim", &fmd_conf_size, "10m" }, /* client allocation limit */ 242 { "client.evqlim", &fmd_conf_uint32, "256" }, /* client event queue limit */ 243 { "client.thrlim", &fmd_conf_uint32, "20" }, /* client aux thread limit */ 244 { "client.thrsig", &fmd_conf_signal, "SIGUSR1" }, /* fmd_thr_signal() value */ 245 { "client.tmrlim", &fmd_conf_uint32, "1024" }, /* client pending timer limit */ 246 { "client.xprtlim", &fmd_conf_uint32, "256" }, /* client transport limit */ 247 { "client.xprtlog", &fmd_conf_bool, NULL }, /* client transport logging? */ 248 { "client.xprtqlim", &fmd_conf_uint32, "1024" }, /* client transport queue li */ 249 { "clock", &fmd_clkmode_ops, "native" }, /* clock operation mode */ 250 { "conf_path", &fmd_conf_path, _fmd_conf_path }, /* root config file path */ 251 { "conf_file", &fmd_conf_string, "fmd.conf" }, /* root config file name */ 252 { "core", &fmd_conf_bool, "false" }, /* force core dump on quit */ 253 { "dbout", &fmd_dbout_ops, NULL }, /* daemon debug output sinks */ 254 { "debug", &fmd_debug_ops, NULL }, /* daemon debugging flags */ 255 { "dictdir", &fmd_conf_string, "usr/lib/fm/dict" }, /* default diagcode dir */ 256 { "domain", &fmd_conf_string, NULL }, /* domain id for de auth */ 257 { "fakenotpresent", &fmd_conf_uint32, "0" }, /* simulate rsrc not present */ 258 { "fg", &fmd_conf_bool, "false" }, /* run daemon in foreground */ 259 { "gc_interval", &fmd_conf_time, "1d" }, /* garbage collection intvl */ 260 { "ids.avg", &fmd_conf_uint32, "4" }, /* desired idspace chain len */ 261 { "ids.max", &fmd_conf_uint32, "1024" }, /* maximum idspace buckets */ 262 { "isaname", &fmd_conf_string, _fmd_isa }, /* instruction set (uname -p) */ 263 { "log.creator", &fmd_conf_string, "fmd" }, /* exacct log creator string */ 264 { "log.error", &fmd_conf_string, "var/fm/fmd/errlog" }, /* error log path */ 265 { "log.fault", &fmd_conf_string, "var/fm/fmd/fltlog" }, /* fault log path */ 266 { "log.minfree", &fmd_conf_size, "2m" }, /* min log fsys free space */ 267 { "log.rsrc", &fmd_conf_string, "var/fm/fmd/rsrc" }, /* asru log dir path */ 268 { "log.tryrotate", &fmd_conf_uint32, "10" }, /* max log rotation attempts */ 269 { "log.waitrotate", &fmd_conf_time, "200ms" }, /* log rotation retry delay */ 270 { "log.xprt", &fmd_conf_string, "var/fm/fmd/xprt" }, /* transport log dir */ 271 { "machine", &fmd_conf_string, _fmd_uts.machine }, /* machine name (uname -m) */ 272 { "nodiagcode", &fmd_conf_string, "-" }, /* diagcode to use if error */ 273 { "repaircode", &fmd_conf_string, "-" }, /* diagcode for list.repaired */ 274 { "resolvecode", &fmd_conf_string, "-" }, /* diagcode for list.resolved */ 275 { "updatecode", &fmd_conf_string, "-" }, /* diagcode for list.updated */ 276 { "osrelease", &fmd_conf_string, _fmd_uts.release }, /* release (uname -r) */ 277 { "osversion", &fmd_conf_string, _fmd_uts.version }, /* version (uname -v) */ 278 { "platform", &fmd_conf_string, _fmd_plat }, /* platform string (uname -i) */ 279 { "plugin.close", &fmd_conf_bool, "true" }, /* dlclose plugins on fini */ 280 { "plugin.path", &fmd_conf_path, _fmd_plugin_path }, /* path for plugin mods */ 281 { "product", &fmd_conf_string, _fmd_prod }, /* product name string */ 282 { "rootdir", &fmd_conf_string, "" }, /* root directory for paths */ 283 { "rpc.adm.path", &fmd_conf_string, NULL }, /* FMD_ADM rendezvous file */ 284 { "rpc.adm.prog", &fmd_conf_uint32, "100169" }, /* FMD_ADM rpc program num */ 285 { "rpc.api.path", &fmd_conf_string, NULL }, /* FMD_API rendezvous file */ 286 { "rpc.api.prog", &fmd_conf_uint32, "100170" }, /* FMD_API rpc program num */ 287 { "rpc.rcvsize", &fmd_conf_size, "128k" }, /* rpc receive buffer size */ 288 { "rpc.sndsize", &fmd_conf_size, "128k" }, /* rpc send buffer size */ 289 { "rsrc.age", &fmd_conf_time, "30d" }, /* max age of old rsrc log */ 290 { "rsrc.zero", &fmd_conf_bool, "false" }, /* zero rsrc cache on start? */ 291 { "schemedir", &fmd_conf_string, _fmd_scheme_path }, /* path for scheme mods */ 292 { "self.name", &fmd_conf_string, "fmd-self-diagnosis" }, /* self-diag module */ 293 { "self.dict", &fmd_conf_list, "FMD.dict" }, /* self-diag dictionary list */ 294 { "server", &fmd_conf_string, _fmd_uts.nodename }, /* server id for de auth */ 295 { "strbuckets", &fmd_conf_uint32, "211" }, /* size of string hashes */ 296 #ifdef DEBUG 297 { "trace.mode", &fmd_trmode_ops, "full" }, /* trace mode: none/lite/full */ 298 #else 299 { "trace.mode", &fmd_trmode_ops, "lite" }, /* trace mode: none/lite/full */ 300 #endif 301 { "trace.recs", &fmd_conf_uint32, "128" }, /* trace records per thread */ 302 { "trace.frames", &fmd_conf_uint32, "16" }, /* max trace rec stack frames */ 303 { "uuidlen", &fmd_conf_uint32, "36" }, /* UUID ASCII string length */ 304 { "xprt.ttl", &fmd_conf_uint8, "1" }, /* default event time-to-live */ 305 }; 306 307 /* 308 * Statistics maintained by fmd itself on behalf of various global subsystems. 309 * NOTE: FMD_TYPE_STRING statistics should not be used here. If they are 310 * required in the future, the FMD_ADM_MODGSTAT service routine must change. 311 */ 312 static fmd_statistics_t _fmd_stats = { 313 { "errlog.replayed", FMD_TYPE_UINT64, "total events replayed from errlog" }, 314 { "errlog.partials", FMD_TYPE_UINT64, "events partially committed in errlog" }, 315 { "errlog.enospc", FMD_TYPE_UINT64, "events not appended to errlog (ENOSPC)" }, 316 { "fltlog.enospc", FMD_TYPE_UINT64, "events not appended to fltlog (ENOSPC)" }, 317 { "log.enospc", FMD_TYPE_UINT64, "events not appended to other logs (ENOSPC)" }, 318 { "dr.gen", FMD_TYPE_UINT64, "dynamic reconfiguration generation" }, 319 { "topo.gen", FMD_TYPE_UINT64, "topology snapshot generation" }, 320 { "topo.drgen", FMD_TYPE_UINT64, "current topology DR generation number" }, 321 }; 322 323 /* 324 * SMBIOS serial numbers can contain characters (particularly ':' and ' ') 325 * that are invalid for the authority and can break FMRI parsing. We translate 326 * any invalid characters to a safe '-', as well as trimming any leading or 327 * trailing whitespace. Similarly, '/' can be found in some product names 328 * so we translate that to '-'. 329 */ 330 void 331 fmd_cleanup_auth_str(char *buf, const char *begin) 332 { 333 const char *end, *cp; 334 char c; 335 int i; 336 337 end = begin + strlen(begin); 338 339 while (begin < end && isspace(*begin)) 340 begin++; 341 while (begin < end && isspace(*(end - 1))) 342 end--; 343 344 if (begin >= end) 345 return; 346 347 cp = begin; 348 for (i = 0; i < MAXNAMELEN - 1; i++) { 349 if (cp >= end) 350 break; 351 c = *cp; 352 if (c == ':' || c == '=' || c == '/' || isspace(c) || 353 !isprint(c)) 354 buf[i] = '-'; 355 else 356 buf[i] = c; 357 cp++; 358 } 359 buf[i] = 0; 360 } 361 362 void 363 fmd_create(fmd_t *dp, const char *arg0, const char *root, const char *conf) 364 { 365 fmd_conf_path_t *pap; 366 char file[PATH_MAX]; 367 const char *name; 368 fmd_stat_t *sp; 369 int i; 370 371 smbios_hdl_t *shp; 372 smbios_system_t s1; 373 smbios_info_t s2; 374 id_t id; 375 376 di_prom_handle_t promh = DI_PROM_HANDLE_NIL; 377 di_node_t rooth = DI_NODE_NIL; 378 char *bufp; 379 380 (void) sysinfo(SI_PLATFORM, _fmd_plat, sizeof (_fmd_plat)); 381 (void) sysinfo(SI_ARCHITECTURE, _fmd_isa, sizeof (_fmd_isa)); 382 (void) uname(&_fmd_uts); 383 384 if ((shp = smbios_open(NULL, SMB_VERSION, 0, NULL)) != NULL) { 385 if ((id = smbios_info_system(shp, &s1)) != SMB_ERR && 386 smbios_info_common(shp, id, &s2) != SMB_ERR) { 387 fmd_cleanup_auth_str(_fmd_prod, s2.smbi_product); 388 fmd_cleanup_auth_str(_fmd_csn, s2.smbi_serial); 389 } 390 smbios_close(shp); 391 } else if ((rooth = di_init("/", DINFOPROP)) != DI_NODE_NIL && 392 (promh = di_prom_init()) != DI_PROM_HANDLE_NIL) { 393 if (di_prom_prop_lookup_bytes(promh, rooth, "chassis-sn", 394 (unsigned char **)&bufp) != -1) { 395 fmd_cleanup_auth_str(_fmd_csn, bufp); 396 } 397 } 398 399 if (promh != DI_PROM_HANDLE_NIL) 400 di_prom_fini(promh); 401 if (rooth != DI_NODE_NIL) 402 di_fini(rooth); 403 404 bzero(dp, sizeof (fmd_t)); 405 406 dp->d_version = _fmd_version; 407 dp->d_pname = fmd_strbasename(arg0); 408 dp->d_pid = getpid(); 409 410 if (pthread_key_create(&dp->d_key, NULL) != 0) 411 fmd_error(EFMD_EXIT, "failed to create pthread key"); 412 413 (void) pthread_mutex_init(&dp->d_xprt_lock, NULL); 414 (void) pthread_mutex_init(&dp->d_err_lock, NULL); 415 (void) pthread_mutex_init(&dp->d_thr_lock, NULL); 416 (void) pthread_mutex_init(&dp->d_mod_lock, NULL); 417 (void) pthread_mutex_init(&dp->d_stats_lock, NULL); 418 (void) pthread_mutex_init(&dp->d_topo_lock, NULL); 419 (void) pthread_rwlock_init(&dp->d_log_lock, NULL); 420 (void) pthread_mutex_init(&dp->d_fmd_lock, NULL); 421 (void) pthread_cond_init(&dp->d_fmd_cv, NULL); 422 423 /* 424 * A small number of properties must be set manually before we open 425 * the root configuration file. These include any settings for our 426 * memory allocator and path expansion token values, because these 427 * values are needed by the routines in fmd_conf.c itself. After 428 * the root configuration file is processed, we reset these properties 429 * based upon the latest values from the configuration file. 430 */ 431 dp->d_alloc_msecs = 10; 432 dp->d_alloc_tries = 3; 433 dp->d_str_buckets = 211; 434 435 dp->d_rootdir = root ? root : ""; 436 dp->d_platform = _fmd_plat; 437 dp->d_machine = _fmd_uts.machine; 438 dp->d_isaname = _fmd_isa; 439 440 dp->d_conf = fmd_conf_open(conf, sizeof (_fmd_conf) / 441 sizeof (_fmd_conf[0]), _fmd_conf, FMD_CONF_DEFER); 442 443 if (dp->d_conf == NULL) { 444 fmd_error(EFMD_EXIT, 445 "failed to load required configuration properties\n"); 446 } 447 448 (void) fmd_conf_getprop(dp->d_conf, "alloc.msecs", &dp->d_alloc_msecs); 449 (void) fmd_conf_getprop(dp->d_conf, "alloc.tries", &dp->d_alloc_tries); 450 (void) fmd_conf_getprop(dp->d_conf, "strbuckets", &dp->d_str_buckets); 451 452 (void) fmd_conf_getprop(dp->d_conf, "platform", &dp->d_platform); 453 (void) fmd_conf_getprop(dp->d_conf, "machine", &dp->d_machine); 454 (void) fmd_conf_getprop(dp->d_conf, "isaname", &dp->d_isaname); 455 456 /* 457 * Manually specified rootdirs override config files, so only update 458 * d_rootdir based on the config files we parsed if no 'root' was set. 459 */ 460 if (root == NULL) 461 (void) fmd_conf_getprop(dp->d_conf, "rootdir", &dp->d_rootdir); 462 else 463 (void) fmd_conf_setprop(dp->d_conf, "rootdir", dp->d_rootdir); 464 465 /* 466 * Once the base conf file properties are loaded, lookup the values 467 * of $conf_path and $conf_file and merge in any other conf files. 468 */ 469 (void) fmd_conf_getprop(dp->d_conf, "conf_path", &pap); 470 (void) fmd_conf_getprop(dp->d_conf, "conf_file", &name); 471 472 for (i = 0; i < pap->cpa_argc; i++) { 473 (void) snprintf(file, sizeof (file), 474 "%s/%s", pap->cpa_argv[i], name); 475 if (access(file, F_OK) == 0) 476 fmd_conf_merge(dp->d_conf, file); 477 } 478 479 /* 480 * Update the value of fmd.d_fg based on "fg". We cache this property 481 * because it must be accessed deep within fmd at fmd_verror() time. 482 * Update any other properties that must be cached for performance. 483 */ 484 (void) fmd_conf_getprop(fmd.d_conf, "fg", &fmd.d_fg); 485 (void) fmd_conf_getprop(fmd.d_conf, "xprt.ttl", &fmd.d_xprt_ttl); 486 487 /* 488 * Initialize our custom libnvpair allocator and create an nvlist for 489 * authority elements corresponding to this instance of the daemon. 490 */ 491 (void) nv_alloc_init(&dp->d_nva, &fmd_nv_alloc_ops); 492 dp->d_auth = fmd_protocol_authority(); 493 494 /* 495 * The fmd_module_t for the root module must be created manually. Most 496 * of it remains unused and zero, except for the few things we fill in. 497 */ 498 dp->d_rmod = fmd_zalloc(sizeof (fmd_module_t), FMD_SLEEP); 499 dp->d_rmod->mod_name = fmd_strdup(dp->d_pname, FMD_SLEEP); 500 dp->d_rmod->mod_fmri = fmd_protocol_fmri_module(dp->d_rmod); 501 502 fmd_list_append(&dp->d_mod_list, dp->d_rmod); 503 fmd_module_hold(dp->d_rmod); 504 505 (void) pthread_mutex_init(&dp->d_rmod->mod_lock, NULL); 506 (void) pthread_cond_init(&dp->d_rmod->mod_cv, NULL); 507 (void) pthread_mutex_init(&dp->d_rmod->mod_stats_lock, NULL); 508 509 dp->d_rmod->mod_thread = fmd_thread_xcreate(dp->d_rmod, pthread_self()); 510 dp->d_rmod->mod_stats = fmd_zalloc(sizeof (fmd_modstat_t), FMD_SLEEP); 511 dp->d_rmod->mod_ustat = fmd_ustat_create(); 512 513 if (pthread_setspecific(dp->d_key, dp->d_rmod->mod_thread) != 0) 514 fmd_error(EFMD_EXIT, "failed to attach main thread key"); 515 516 if ((dp->d_stats = (fmd_statistics_t *)fmd_ustat_insert( 517 dp->d_rmod->mod_ustat, FMD_USTAT_NOALLOC, sizeof (_fmd_stats) / 518 sizeof (fmd_stat_t), (fmd_stat_t *)&_fmd_stats, NULL)) == NULL) 519 fmd_error(EFMD_EXIT, "failed to initialize statistics"); 520 521 (void) pthread_mutex_lock(&dp->d_rmod->mod_lock); 522 dp->d_rmod->mod_flags |= FMD_MOD_INIT; 523 (void) pthread_mutex_unlock(&dp->d_rmod->mod_lock); 524 525 /* 526 * In addition to inserting the _fmd_stats collection of program-wide 527 * statistics, we also insert a statistic named after each of our 528 * errors and update these counts in fmd_verror() (see fmd_subr.c). 529 */ 530 dp->d_errstats = sp = fmd_zalloc(sizeof (fmd_stat_t) * 531 (EFMD_END - EFMD_UNKNOWN), FMD_SLEEP); 532 533 for (i = 0; i < EFMD_END - EFMD_UNKNOWN; i++, sp++) { 534 (void) snprintf(sp->fmds_name, sizeof (sp->fmds_name), "err.%s", 535 strrchr(fmd_errclass(EFMD_UNKNOWN + i), '.') + 1); 536 sp->fmds_type = FMD_TYPE_UINT64; 537 } 538 539 (void) fmd_ustat_insert(dp->d_rmod->mod_ustat, FMD_USTAT_NOALLOC, 540 EFMD_END - EFMD_UNKNOWN, dp->d_errstats, NULL); 541 } 542 543 void 544 fmd_destroy(fmd_t *dp) 545 { 546 fmd_module_t *mp; 547 fmd_case_t *cp; 548 int core; 549 550 (void) fmd_conf_getprop(fmd.d_conf, "core", &core); 551 552 fmd_rpc_fini(); 553 554 if (dp->d_xprt_ids != NULL) 555 fmd_xprt_suspend_all(); 556 557 /* 558 * Unload the self-diagnosis module first. This ensures that it does 559 * not get confused as we start unloading other modules, etc. We must 560 * hold the dispq lock as a writer while doing so since it uses d_self. 561 */ 562 if (dp->d_self != NULL) { 563 fmd_module_t *self; 564 565 (void) pthread_rwlock_wrlock(&dp->d_disp->dq_lock); 566 self = dp->d_self; 567 dp->d_self = NULL; 568 (void) pthread_rwlock_unlock(&dp->d_disp->dq_lock); 569 570 fmd_module_unload(self); 571 fmd_module_rele(self); 572 } 573 574 /* 575 * Unload modules in reverse order *except* for the root module, which 576 * is first in the list. This allows it to keep its thread and trace. 577 */ 578 for (mp = fmd_list_prev(&dp->d_mod_list); mp != dp->d_rmod; ) { 579 fmd_module_unload(mp); 580 mp = fmd_list_prev(mp); 581 } 582 583 if (dp->d_mod_hash != NULL) { 584 fmd_modhash_destroy(dp->d_mod_hash); 585 dp->d_mod_hash = NULL; 586 } 587 588 /* 589 * Close both log files now that modules are no longer active. We must 590 * set these pointers to NULL in case any subsequent errors occur. 591 */ 592 if (dp->d_errlog != NULL) { 593 fmd_log_rele(dp->d_errlog); 594 dp->d_errlog = NULL; 595 } 596 597 if (dp->d_fltlog != NULL) { 598 fmd_log_rele(dp->d_fltlog); 599 dp->d_fltlog = NULL; 600 } 601 602 /* 603 * Now destroy the resource cache: each ASRU contains a case reference, 604 * which may in turn contain a pointer to a referenced owning module. 605 */ 606 if (dp->d_asrus != NULL) { 607 fmd_asru_hash_destroy(dp->d_asrus); 608 dp->d_asrus = NULL; 609 } 610 611 /* 612 * Now that all data structures that refer to modules are torn down, 613 * no modules should be remaining on the module list except for d_rmod. 614 * If we trip one of these assertions, we're missing a rele somewhere. 615 */ 616 ASSERT(fmd_list_prev(&dp->d_mod_list) == dp->d_rmod); 617 ASSERT(fmd_list_next(&dp->d_mod_list) == dp->d_rmod); 618 619 /* 620 * Now destroy the root module. We clear its thread key first so any 621 * calls to fmd_trace() inside of the module code will be ignored. 622 */ 623 (void) pthread_setspecific(dp->d_key, NULL); 624 fmd_module_lock(dp->d_rmod); 625 626 while ((cp = fmd_list_next(&dp->d_rmod->mod_cases)) != NULL) 627 fmd_case_discard(cp, B_FALSE); 628 629 fmd_module_unlock(dp->d_rmod); 630 fmd_free(dp->d_rmod->mod_stats, sizeof (fmd_modstat_t)); 631 dp->d_rmod->mod_stats = NULL; 632 633 (void) pthread_mutex_lock(&dp->d_rmod->mod_lock); 634 dp->d_rmod->mod_flags |= FMD_MOD_FINI; 635 (void) pthread_mutex_unlock(&dp->d_rmod->mod_lock); 636 637 fmd_module_rele(dp->d_rmod); 638 ASSERT(fmd_list_next(&dp->d_mod_list) == NULL); 639 640 /* 641 * Now destroy the remaining global data structures. If 'core' was 642 * set to true, force a core dump so we can check for memory leaks. 643 */ 644 if (dp->d_cases != NULL) 645 fmd_case_hash_destroy(dp->d_cases); 646 if (dp->d_disp != NULL) 647 fmd_dispq_destroy(dp->d_disp); 648 if (dp->d_timers != NULL) 649 fmd_timerq_destroy(dp->d_timers); 650 if (dp->d_schemes != NULL) 651 fmd_scheme_hash_destroy(dp->d_schemes); 652 if (dp->d_xprt_ids != NULL) 653 fmd_idspace_destroy(dp->d_xprt_ids); 654 655 if (dp->d_errstats != NULL) { 656 fmd_free(dp->d_errstats, 657 sizeof (fmd_stat_t) * (EFMD_END - EFMD_UNKNOWN)); 658 } 659 660 if (dp->d_conf != NULL) 661 fmd_conf_close(dp->d_conf); 662 663 fmd_topo_fini(); 664 665 nvlist_free(dp->d_auth); 666 (void) nv_alloc_fini(&dp->d_nva); 667 dp->d_clockops->fto_fini(dp->d_clockptr); 668 669 (void) pthread_key_delete(dp->d_key); 670 bzero(dp, sizeof (fmd_t)); 671 672 if (core) 673 fmd_panic("forcing core dump at user request\n"); 674 } 675 676 /*ARGSUSED*/ 677 static void 678 fmd_gc(fmd_t *dp, id_t id, hrtime_t hrt) 679 { 680 hrtime_t delta; 681 682 if (id != 0) { 683 TRACE((FMD_DBG_MOD, "garbage collect start")); 684 fmd_modhash_apply(dp->d_mod_hash, fmd_module_gc); 685 TRACE((FMD_DBG_MOD, "garbage collect end")); 686 687 (void) pthread_rwlock_rdlock(&dp->d_log_lock); 688 fmd_log_update(dp->d_errlog); 689 (void) pthread_rwlock_unlock(&dp->d_log_lock); 690 } 691 692 (void) fmd_conf_getprop(dp->d_conf, "gc_interval", &delta); 693 (void) fmd_timerq_install(dp->d_timers, dp->d_rmod->mod_timerids, 694 (fmd_timer_f *)fmd_gc, dp, NULL, delta); 695 } 696 697 /*ARGSUSED*/ 698 static void 699 fmd_clear_aged_rsrcs(fmd_t *dp, id_t id, hrtime_t hrt) 700 { 701 hrtime_t delta; 702 703 fmd_asru_clear_aged_rsrcs(); 704 (void) fmd_conf_getprop(dp->d_conf, "rsrc.age", &delta); 705 (void) fmd_timerq_install(dp->d_timers, dp->d_rmod->mod_timerids, 706 (fmd_timer_f *)fmd_clear_aged_rsrcs, dp, NULL, delta/10); 707 } 708 709 /* 710 * Events are committed to the errlog after cases are checkpointed. If fmd 711 * crashes before an event is ever associated with a module, this function will 712 * be called to replay it to all subscribers. If fmd crashes in between the 713 * subscriber checkpointing and committing the event in the error log, the 714 * module will have seen the event and we don't want to replay it. So we look 715 * for the event in all modules and transition it to the proper state. If 716 * it is found, we commit it to the error log and do not replay it. The in- 717 * memory case search used by fmd_module_contains() et al isn't particularly 718 * efficient, but it is faster than doing read i/o's on every case event to 719 * check their status or write i/o's on every event to replay to update states. 720 * We can improve the efficiency of this lookup algorithm later if necessary. 721 */ 722 /*ARGSUSED*/ 723 static void 724 fmd_err_replay(fmd_log_t *lp, fmd_event_t *ep, fmd_t *dp) 725 { 726 fmd_module_t *mp; 727 fmd_stat_t *sp; 728 729 (void) pthread_mutex_lock(&dp->d_mod_lock); 730 731 for (mp = fmd_list_next(&dp->d_mod_list); 732 mp != NULL; mp = fmd_list_next(mp)) { 733 if (fmd_module_contains(mp, ep)) { 734 fmd_module_hold(mp); 735 break; 736 } 737 } 738 739 (void) pthread_mutex_unlock(&dp->d_mod_lock); 740 741 if (mp != NULL) { 742 fmd_event_commit(ep); 743 fmd_module_rele(mp); 744 sp = &dp->d_stats->ds_log_partials; 745 } else { 746 fmd_dispq_dispatch(dp->d_disp, ep, FMD_EVENT_DATA(ep)); 747 sp = &dp->d_stats->ds_log_replayed; 748 } 749 750 (void) pthread_mutex_lock(&dp->d_stats_lock); 751 sp->fmds_value.ui64++; 752 (void) pthread_mutex_unlock(&dp->d_stats_lock); 753 } 754 755 void 756 fmd_door_server(void *dip) 757 { 758 fmd_dprintf(FMD_DBG_XPRT, "door server starting for %p\n", dip); 759 (void) pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL); 760 (void) door_return(NULL, 0, NULL, 0); 761 } 762 763 /* 764 * Custom door server create callback. Any fmd services that use doors will 765 * require those threads to have their fmd-specific TSD initialized, etc. 766 */ 767 static void 768 fmd_door(door_info_t *dip) 769 { 770 if (fmd_thread_create(fmd.d_rmod, fmd_door_server, dip) == NULL) 771 fmd_panic("failed to create server for door %p", (void *)dip); 772 } 773 774 /* 775 * This signal handler is installed for the client.thrsig signal to be used to 776 * force an auxiliary thread to wake up from a system call and return EINTR in 777 * response to a module's use of fmd_thr_signal(). We also trace the event. 778 */ 779 static void 780 fmd_signal(int sig) 781 { 782 TRACE((FMD_DBG_MOD, "module thread received sig #%d", sig)); 783 } 784 785 void 786 fmd_run(fmd_t *dp, int pfd) 787 { 788 char *nodc_key[] = { FMD_FLT_NODC, NULL }; 789 char *repair_key[] = { FM_LIST_REPAIRED_CLASS, NULL }; 790 char *resolve_key[] = { FM_LIST_RESOLVED_CLASS, NULL }; 791 char *update_key[] = { FM_LIST_UPDATED_CLASS, NULL }; 792 char code_str[128]; 793 struct sigaction act; 794 795 int status = FMD_EXIT_SUCCESS; 796 const char *name; 797 fmd_conf_path_t *pap; 798 fmd_event_t *e; 799 int dbout; 800 801 /* 802 * Cache all the current debug property settings in d_fmd_debug, 803 * d_fmd_dbout, d_hdl_debug, and d_hdl_dbout. If a given debug mask 804 * is non-zero and the corresponding dbout mask is zero, set dbout 805 * to a sensible default value based on whether we have daemonized. 806 */ 807 (void) fmd_conf_getprop(dp->d_conf, "dbout", &dbout); 808 809 if (dp->d_fmd_debug != 0 && dbout == 0) 810 dp->d_fmd_dbout = dp->d_fg? FMD_DBOUT_STDERR : FMD_DBOUT_SYSLOG; 811 else 812 dp->d_fmd_dbout = dbout; 813 814 (void) fmd_conf_getprop(dp->d_conf, "client.debug", &dp->d_hdl_debug); 815 (void) fmd_conf_getprop(dp->d_conf, "client.dbout", &dbout); 816 817 if (dp->d_hdl_debug != 0 && dbout == 0) 818 dp->d_hdl_dbout = dp->d_fg? FMD_DBOUT_STDERR : FMD_DBOUT_SYSLOG; 819 else 820 dp->d_hdl_dbout = dbout; 821 822 /* 823 * Initialize remaining major program data structures such as the 824 * clock, dispatch queues, log files, module hash collections, etc. 825 * This work is done here rather than in fmd_create() to permit the -o 826 * command-line option to modify properties after fmd_create() is done. 827 */ 828 name = dp->d_rootdir != NULL && 829 *dp->d_rootdir != '\0' ? dp->d_rootdir : NULL; 830 831 /* 832 * The clock must be initialized before fmd_topo_init() because 833 * fmd_topo_update() calls fmd_time_gethrtime(). 834 */ 835 dp->d_clockptr = dp->d_clockops->fto_init(); 836 837 fmd_topo_init(); 838 839 dp->d_xprt_ids = fmd_idspace_create("xprt_ids", 1, INT_MAX); 840 fmd_xprt_suspend_all(); 841 842 (void) door_server_create(fmd_door); 843 844 dp->d_rmod->mod_timerids = fmd_idspace_create(dp->d_pname, 1, 16); 845 dp->d_timers = fmd_timerq_create(); 846 dp->d_disp = fmd_dispq_create(); 847 dp->d_cases = fmd_case_hash_create(); 848 849 /* 850 * The root module's mod_queue is created with limit zero, making it 851 * act like /dev/null; anything inserted here is simply ignored. 852 */ 853 dp->d_rmod->mod_queue = fmd_eventq_create(dp->d_rmod, 854 &dp->d_rmod->mod_stats->ms_evqstat, &dp->d_rmod->mod_stats_lock, 0); 855 856 /* 857 * Once our subsystems that use signals have been set up, install the 858 * signal handler for the fmd_thr_signal() API. Verify that the signal 859 * being used for this purpose doesn't conflict with something else. 860 */ 861 (void) fmd_conf_getprop(dp->d_conf, "client.thrsig", &dp->d_thr_sig); 862 863 if (sigaction(dp->d_thr_sig, NULL, &act) != 0) { 864 fmd_error(EFMD_EXIT, "invalid signal selected for " 865 "client.thrsig property: %d\n", dp->d_thr_sig); 866 } 867 868 if (act.sa_handler != SIG_IGN && act.sa_handler != SIG_DFL) { 869 fmd_error(EFMD_EXIT, "signal selected for client.thrsig " 870 "property is already in use: %d\n", dp->d_thr_sig); 871 } 872 873 act.sa_handler = fmd_signal; 874 act.sa_flags = 0; 875 876 (void) sigemptyset(&act.sa_mask); 877 (void) sigaction(dp->d_thr_sig, &act, NULL); 878 879 (void) fmd_conf_getprop(dp->d_conf, "schemedir", &name); 880 dp->d_schemes = fmd_scheme_hash_create(dp->d_rootdir, name); 881 882 (void) fmd_conf_getprop(dp->d_conf, "log.rsrc", &name); 883 dp->d_asrus = fmd_asru_hash_create(dp->d_rootdir, name); 884 885 (void) fmd_conf_getprop(dp->d_conf, "log.error", &name); 886 dp->d_errlog = fmd_log_open(dp->d_rootdir, name, FMD_LOG_ERROR); 887 888 (void) fmd_conf_getprop(dp->d_conf, "log.fault", &name); 889 dp->d_fltlog = fmd_log_open(dp->d_rootdir, name, FMD_LOG_FAULT); 890 891 if (dp->d_asrus == NULL || dp->d_errlog == NULL || dp->d_fltlog == NULL) 892 fmd_error(EFMD_EXIT, "failed to initialize log files\n"); 893 894 /* 895 * Before loading modules, create an empty control event which will act 896 * as a global barrier for module event processing. Each module we 897 * load successfully will insert it at their head of their event queue, 898 * and then pause inside of fmd_ctl_rele() after dequeuing the event. 899 * This module barrier is required for two reasons: 900 * 901 * (a) During module loading, the restoration of case checkpoints may 902 * result in a list.* event being recreated for which the intended 903 * subscriber has not yet loaded depending on the load order. Such 904 * events could then result in spurious "no subscriber" errors. 905 * 906 * (b) During errlog replay, a sequence of errors from a long time ago 907 * may be replayed, and the module may attempt to install relative 908 * timers associated with one or more of these events. If errlog 909 * replay were "racing" with active module threads, an event E1 910 * that resulted in a relative timer T at time E1 + N nsec could 911 * fire prior to an event E2 being enqueued, even if the relative 912 * time ordering was E1 < E2 < E1 + N, causing mis-diagnosis. 913 */ 914 dp->d_mod_event = e = fmd_event_create(FMD_EVT_CTL, 915 FMD_HRT_NOW, NULL, fmd_ctl_init(NULL)); 916 917 fmd_event_hold(e); 918 919 /* 920 * Once all data structures are initialized, we load all of our modules 921 * in order according to class in order to load up any subscriptions. 922 * Once built-in modules are loaded, we detach from our waiting parent. 923 */ 924 dp->d_mod_hash = fmd_modhash_create(); 925 926 if (fmd_builtin_loadall(dp->d_mod_hash) != 0 && !dp->d_fg) 927 fmd_error(EFMD_EXIT, "failed to initialize fault manager\n"); 928 929 (void) fmd_conf_getprop(dp->d_conf, "self.name", &name); 930 dp->d_self = fmd_modhash_lookup(dp->d_mod_hash, name); 931 932 if (dp->d_self != NULL) { 933 if (fmd_module_dc_key2code(dp->d_self, nodc_key, code_str, 934 sizeof (code_str)) == 0) 935 (void) fmd_conf_setprop(dp->d_conf, "nodiagcode", 936 code_str); 937 if (fmd_module_dc_key2code(dp->d_self, repair_key, code_str, 938 sizeof (code_str)) == 0) 939 (void) fmd_conf_setprop(dp->d_conf, "repaircode", 940 code_str); 941 if (fmd_module_dc_key2code(dp->d_self, resolve_key, code_str, 942 sizeof (code_str)) == 0) 943 (void) fmd_conf_setprop(dp->d_conf, "resolvecode", 944 code_str); 945 if (fmd_module_dc_key2code(dp->d_self, update_key, code_str, 946 sizeof (code_str)) == 0) 947 (void) fmd_conf_setprop(dp->d_conf, "updatecode", 948 code_str); 949 } 950 951 fmd_rpc_init(); 952 dp->d_running = 1; /* we are now officially an active fmd */ 953 954 /* 955 * Now that we're running, if a pipe fd was specified, write an exit 956 * status to it to indicate that our parent process can safely detach. 957 * Then proceed to loading the remaining non-built-in modules. 958 */ 959 if (pfd >= 0) 960 (void) write(pfd, &status, sizeof (status)); 961 962 /* 963 * Before loading all modules, repopulate the ASRU cache from its 964 * persistent repository on disk. Then during module loading, the 965 * restoration of checkpoint files will reparent any active cases. 966 */ 967 fmd_asru_hash_refresh(dp->d_asrus); 968 969 (void) fmd_conf_getprop(dp->d_conf, "plugin.path", &pap); 970 fmd_modhash_loadall(dp->d_mod_hash, pap, &fmd_rtld_ops, ".so"); 971 972 (void) fmd_conf_getprop(dp->d_conf, "agent.path", &pap); 973 fmd_modhash_loadall(dp->d_mod_hash, pap, &fmd_proc_ops, NULL); 974 975 /* 976 * With all modules loaded, replay fault events from the ASRU cache for 977 * any ASRUs that must be retired, replay error events from the errlog 978 * that did not finish processing the last time ran, and then release 979 * the global module barrier by executing a final rele on d_mod_event. 980 */ 981 fmd_asru_hash_replay(dp->d_asrus); 982 983 (void) pthread_rwlock_rdlock(&dp->d_log_lock); 984 fmd_log_replay(dp->d_errlog, (fmd_log_f *)fmd_err_replay, dp); 985 fmd_log_update(dp->d_errlog); 986 (void) pthread_rwlock_unlock(&dp->d_log_lock); 987 988 dp->d_mod_event = NULL; 989 fmd_event_rele(e); 990 991 /* 992 * Now replay list.updated and list.repaired events 993 */ 994 fmd_case_repair_replay(); 995 996 /* 997 * Finally, awaken any threads associated with receiving events from 998 * open transports and tell them to proceed with fmd_xprt_recv(). 999 */ 1000 fmd_xprt_resume_all(); 1001 fmd_gc(dp, 0, 0); 1002 fmd_clear_aged_rsrcs(dp, 0, 0); 1003 1004 (void) pthread_mutex_lock(&dp->d_fmd_lock); 1005 dp->d_booted = 1; 1006 (void) pthread_cond_broadcast(&dp->d_fmd_cv); 1007 (void) pthread_mutex_unlock(&dp->d_fmd_lock); 1008 } 1009 1010 void 1011 fmd_help(fmd_t *dp) 1012 { 1013 const fmd_conf_mode_t *cmp; 1014 1015 (void) printf("Usage: %s -o debug=mode[,mode]\n", dp->d_pname); 1016 1017 for (cmp = _fmd_debug_modes; cmp->cm_name != NULL; cmp++) 1018 (void) printf("\t%s\t%s\n", cmp->cm_name, cmp->cm_desc); 1019 } 1020