xref: /onnv-gate/usr/src/cmd/fm/fmd/common/fmd.c (revision 13036:debbc43aae95)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
52112Sav145390  * Common Development and Distribution License (the "License").
62112Sav145390  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
211193Smws 
220Sstevel@tonic-gate /*
2312967Sgavin.maltby@oracle.com  * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #include <sys/types.h>
270Sstevel@tonic-gate #include <sys/utsname.h>
280Sstevel@tonic-gate #include <sys/param.h>
290Sstevel@tonic-gate #include <sys/systeminfo.h>
300Sstevel@tonic-gate #include <sys/fm/util.h>
310Sstevel@tonic-gate 
321414Scindi #include <smbios.h>
330Sstevel@tonic-gate #include <limits.h>
340Sstevel@tonic-gate #include <unistd.h>
350Sstevel@tonic-gate #include <signal.h>
360Sstevel@tonic-gate #include <stdlib.h>
370Sstevel@tonic-gate #include <stdio.h>
389874SStephen.Hanson@Sun.COM #include <ctype.h>
391193Smws #include <door.h>
400Sstevel@tonic-gate 
410Sstevel@tonic-gate #include <fmd_conf.h>
420Sstevel@tonic-gate #include <fmd_dispq.h>
430Sstevel@tonic-gate #include <fmd_timerq.h>
440Sstevel@tonic-gate #include <fmd_subr.h>
450Sstevel@tonic-gate #include <fmd_error.h>
460Sstevel@tonic-gate #include <fmd_module.h>
470Sstevel@tonic-gate #include <fmd_thread.h>
480Sstevel@tonic-gate #include <fmd_alloc.h>
490Sstevel@tonic-gate #include <fmd_string.h>
500Sstevel@tonic-gate #include <fmd_builtin.h>
510Sstevel@tonic-gate #include <fmd_ustat.h>
520Sstevel@tonic-gate #include <fmd_protocol.h>
530Sstevel@tonic-gate #include <fmd_scheme.h>
540Sstevel@tonic-gate #include <fmd_asru.h>
550Sstevel@tonic-gate #include <fmd_case.h>
560Sstevel@tonic-gate #include <fmd_log.h>
571193Smws #include <fmd_idspace.h>
580Sstevel@tonic-gate #include <fmd_rpc.h>
590Sstevel@tonic-gate #include <fmd_dr.h>
603062Scindi #include <fmd_topo.h>
611193Smws #include <fmd_xprt.h>
621193Smws #include <fmd_ctl.h>
632112Sav145390 #include <sys/openpromio.h>
642112Sav145390 #include <libdevinfo.h>
650Sstevel@tonic-gate 
660Sstevel@tonic-gate #include <fmd.h>
670Sstevel@tonic-gate 
680Sstevel@tonic-gate extern const nv_alloc_ops_t fmd_nv_alloc_ops;	/* see fmd_nv.c */
690Sstevel@tonic-gate 
704198Seschrock const char _fmd_version[] = "1.2";		/* daemon version string */
710Sstevel@tonic-gate static char _fmd_plat[MAXNAMELEN];		/* native platform string */
720Sstevel@tonic-gate static char _fmd_isa[MAXNAMELEN];		/* native instruction set */
730Sstevel@tonic-gate static struct utsname _fmd_uts;			/* native uname(2) info */
7410462SSean.Ye@Sun.COM static char _fmd_psn[MAXNAMELEN];		/* product serial number */
751414Scindi static char _fmd_csn[MAXNAMELEN];		/* chassis serial number */
761414Scindi static char _fmd_prod[MAXNAMELEN];		/* product name string */
770Sstevel@tonic-gate 
780Sstevel@tonic-gate /*
790Sstevel@tonic-gate  * Note: the configuration file path is ordered from most common to most host-
800Sstevel@tonic-gate  * specific because new conf files are merged/override previous ones.  The
810Sstevel@tonic-gate  * module paths are in the opposite order, from most specific to most common,
820Sstevel@tonic-gate  * because once a module is loaded fmd will not try to load over the same name.
830Sstevel@tonic-gate  */
840Sstevel@tonic-gate 
850Sstevel@tonic-gate static const char _fmd_conf_path[] =
860Sstevel@tonic-gate 	"%r/usr/lib/fm/fmd:"
870Sstevel@tonic-gate 	"%r/usr/platform/%m/lib/fm/fmd:"
880Sstevel@tonic-gate 	"%r/usr/platform/%i/lib/fm/fmd:"
890Sstevel@tonic-gate 	"%r/etc/fm/fmd";
900Sstevel@tonic-gate 
910Sstevel@tonic-gate static const char _fmd_agent_path[] =
920Sstevel@tonic-gate 	"%r/usr/platform/%i/lib/fm/fmd/agents:"
930Sstevel@tonic-gate 	"%r/usr/platform/%m/lib/fm/fmd/agents:"
940Sstevel@tonic-gate 	"%r/usr/lib/fm/fmd/agents";
950Sstevel@tonic-gate 
960Sstevel@tonic-gate static const char _fmd_plugin_path[] =
970Sstevel@tonic-gate 	"%r/usr/platform/%i/lib/fm/fmd/plugins:"
980Sstevel@tonic-gate 	"%r/usr/platform/%m/lib/fm/fmd/plugins:"
990Sstevel@tonic-gate 	"%r/usr/lib/fm/fmd/plugins";
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate static const char _fmd_scheme_path[] =
1020Sstevel@tonic-gate 	"usr/lib/fm/fmd/schemes";
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate static const fmd_conf_mode_t _fmd_cerror_modes[] = {
1050Sstevel@tonic-gate 	{ "unload", "unload offending client module", FMD_CERROR_UNLOAD },
1060Sstevel@tonic-gate 	{ "stop", "stop daemon for debugger attach", FMD_CERROR_STOP },
1070Sstevel@tonic-gate 	{ "abort", "abort daemon and force core dump", FMD_CERROR_ABORT },
1080Sstevel@tonic-gate 	{ NULL, NULL, 0 }
1090Sstevel@tonic-gate };
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate static const fmd_conf_mode_t _fmd_dbout_modes[] = {
1120Sstevel@tonic-gate 	{ "stderr", "send debug messages to stderr", FMD_DBOUT_STDERR },
1130Sstevel@tonic-gate 	{ "syslog", "send debug messages to syslog", FMD_DBOUT_SYSLOG },
1140Sstevel@tonic-gate 	{ NULL, NULL, 0 }
1150Sstevel@tonic-gate };
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate static const fmd_conf_mode_t _fmd_debug_modes[] = {
1180Sstevel@tonic-gate 	{ "help", "display debugging modes and exit", FMD_DBG_HELP },
1190Sstevel@tonic-gate 	{ "mod", "debug module load/unload/locking", FMD_DBG_MOD },
1200Sstevel@tonic-gate 	{ "disp", "debug dispatch queue processing", FMD_DBG_DISP },
1210Sstevel@tonic-gate 	{ "xprt", "debug transport-specific routines", FMD_DBG_XPRT },
1220Sstevel@tonic-gate 	{ "evt", "debug event subsystem routines", FMD_DBG_EVT },
1230Sstevel@tonic-gate 	{ "log", "debug log subsystem routines", FMD_DBG_LOG },
1240Sstevel@tonic-gate 	{ "tmr", "debug timer subsystem routines", FMD_DBG_TMR },
1250Sstevel@tonic-gate 	{ "fmri", "debug fmri subsystem routines", FMD_DBG_FMRI },
1260Sstevel@tonic-gate 	{ "asru", "debug asru subsystem routines", FMD_DBG_ASRU },
1270Sstevel@tonic-gate 	{ "case", "debug case subsystem routines", FMD_DBG_CASE },
1280Sstevel@tonic-gate 	{ "ckpt", "debug checkpoint routines", FMD_DBG_CKPT },
1290Sstevel@tonic-gate 	{ "rpc", "debug rpc service routines", FMD_DBG_RPC },
1301193Smws 	{ "trace", "display matching trace calls", FMD_DBG_TRACE },
1310Sstevel@tonic-gate 	{ "all", "enable all available debug modes", FMD_DBG_ALL },
1320Sstevel@tonic-gate 	{ NULL, NULL, 0 }
1330Sstevel@tonic-gate };
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate static int
fmd_cerror_set(fmd_conf_param_t * pp,const char * value)1360Sstevel@tonic-gate fmd_cerror_set(fmd_conf_param_t *pp, const char *value)
1370Sstevel@tonic-gate {
1380Sstevel@tonic-gate 	return (fmd_conf_mode_set(_fmd_cerror_modes, pp, value));
1390Sstevel@tonic-gate }
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate static int
fmd_dbout_set(fmd_conf_param_t * pp,const char * value)1420Sstevel@tonic-gate fmd_dbout_set(fmd_conf_param_t *pp, const char *value)
1430Sstevel@tonic-gate {
1440Sstevel@tonic-gate 	return (fmd_conf_mode_set(_fmd_dbout_modes, pp, value));
1450Sstevel@tonic-gate }
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate static int
fmd_debug_set(fmd_conf_param_t * pp,const char * value)1480Sstevel@tonic-gate fmd_debug_set(fmd_conf_param_t *pp, const char *value)
1490Sstevel@tonic-gate {
1500Sstevel@tonic-gate 	int err = fmd_conf_mode_set(_fmd_debug_modes, pp, value);
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate 	if (err == 0)
1530Sstevel@tonic-gate 		fmd.d_fmd_debug = pp->cp_value.cpv_num;
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate 	return (err);
1560Sstevel@tonic-gate }
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate static int
fmd_trmode_set(fmd_conf_param_t * pp,const char * value)1590Sstevel@tonic-gate fmd_trmode_set(fmd_conf_param_t *pp, const char *value)
1600Sstevel@tonic-gate {
1610Sstevel@tonic-gate 	fmd_tracebuf_f *func;
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate 	if (strcasecmp(value, "none") == 0)
1640Sstevel@tonic-gate 		func = fmd_trace_none;
1650Sstevel@tonic-gate 	else if (strcasecmp(value, "lite") == 0)
1660Sstevel@tonic-gate 		func = fmd_trace_lite;
1670Sstevel@tonic-gate 	else if (strcasecmp(value, "full") == 0)
1680Sstevel@tonic-gate 		func = fmd_trace_full;
1690Sstevel@tonic-gate 	else
1700Sstevel@tonic-gate 		return (fmd_set_errno(EFMD_CONF_INVAL));
1710Sstevel@tonic-gate 
1720Sstevel@tonic-gate 	fmd.d_thr_trace = (void (*)())func;
1730Sstevel@tonic-gate 	pp->cp_value.cpv_ptr = (void *)func;
1740Sstevel@tonic-gate 	return (0);
1750Sstevel@tonic-gate }
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate static void
fmd_trmode_get(const fmd_conf_param_t * pp,void * ptr)1780Sstevel@tonic-gate fmd_trmode_get(const fmd_conf_param_t *pp, void *ptr)
1790Sstevel@tonic-gate {
1800Sstevel@tonic-gate 	*((void **)ptr) = pp->cp_value.cpv_ptr;
1810Sstevel@tonic-gate }
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate static int
fmd_clkmode_set(fmd_conf_param_t * pp,const char * value)1840Sstevel@tonic-gate fmd_clkmode_set(fmd_conf_param_t *pp, const char *value)
1850Sstevel@tonic-gate {
1860Sstevel@tonic-gate 	const fmd_timeops_t *ops;
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate 	if (strcasecmp(value, "native") == 0)
1890Sstevel@tonic-gate 		ops = &fmd_timeops_native;
1900Sstevel@tonic-gate 	else if (strcasecmp(value, "simulated") == 0)
1910Sstevel@tonic-gate 		ops = &fmd_timeops_simulated;
1920Sstevel@tonic-gate 	else
1930Sstevel@tonic-gate 		return (fmd_set_errno(EFMD_CONF_INVAL));
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate 	fmd.d_clockops = ops;
1960Sstevel@tonic-gate 	pp->cp_value.cpv_ptr = (void *)ops;
1970Sstevel@tonic-gate 	return (0);
1980Sstevel@tonic-gate }
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate static void
fmd_clkmode_get(const fmd_conf_param_t * pp,void * ptr)2010Sstevel@tonic-gate fmd_clkmode_get(const fmd_conf_param_t *pp, void *ptr)
2020Sstevel@tonic-gate {
2030Sstevel@tonic-gate 	*((void **)ptr) = pp->cp_value.cpv_ptr;
2040Sstevel@tonic-gate }
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate static const fmd_conf_ops_t fmd_cerror_ops = {
2070Sstevel@tonic-gate 	fmd_cerror_set, fmd_conf_mode_get, fmd_conf_notsup, fmd_conf_nop
2080Sstevel@tonic-gate };
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate static const fmd_conf_ops_t fmd_dbout_ops = {
2110Sstevel@tonic-gate 	fmd_dbout_set, fmd_conf_mode_get, fmd_conf_notsup, fmd_conf_nop
2120Sstevel@tonic-gate };
2130Sstevel@tonic-gate 
2140Sstevel@tonic-gate static const fmd_conf_ops_t fmd_debug_ops = {
2150Sstevel@tonic-gate 	fmd_debug_set, fmd_conf_mode_get, fmd_conf_notsup, fmd_conf_nop
2160Sstevel@tonic-gate };
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate static const fmd_conf_ops_t fmd_trmode_ops = {
2190Sstevel@tonic-gate 	fmd_trmode_set, fmd_trmode_get, fmd_conf_notsup, fmd_conf_nop
2200Sstevel@tonic-gate };
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate static const fmd_conf_ops_t fmd_clkmode_ops = {
2230Sstevel@tonic-gate 	fmd_clkmode_set, fmd_clkmode_get, fmd_conf_notsup, fmd_conf_nop
2240Sstevel@tonic-gate };
2250Sstevel@tonic-gate 
2260Sstevel@tonic-gate static const fmd_conf_formal_t _fmd_conf[] = {
2270Sstevel@tonic-gate { "agent.path", &fmd_conf_path, _fmd_agent_path }, /* path for agents */
2280Sstevel@tonic-gate { "alloc_msecs", &fmd_conf_uint32, "10" },	/* msecs before alloc retry */
2290Sstevel@tonic-gate { "alloc_tries", &fmd_conf_uint32, "3" },	/* max # of alloc retries */
23010462SSean.Ye@Sun.COM { "product_sn", &fmd_conf_string, _fmd_psn },	/* product serial number */
2311414Scindi { "chassis", &fmd_conf_string, _fmd_csn },	/* chassis serial number */
2320Sstevel@tonic-gate { "ckpt.dir", &fmd_conf_string, "var/fm/fmd/ckpt" }, /* ckpt directory path */
23310928SStephen.Hanson@Sun.COM { "ckpt.dirmode", &fmd_conf_int32, "0755" },	/* ckpt directory perm mode */
23410928SStephen.Hanson@Sun.COM { "ckpt.mode", &fmd_conf_int32, "0644" },	/* ckpt file perm mode */
2350Sstevel@tonic-gate { "ckpt.restore", &fmd_conf_bool, "true" },	/* restore checkpoints? */
2360Sstevel@tonic-gate { "ckpt.save", &fmd_conf_bool, "true" },	/* save checkpoints? */
2370Sstevel@tonic-gate { "ckpt.zero", &fmd_conf_bool, "false" },	/* zero checkpoints on start? */
2380Sstevel@tonic-gate { "client.buflim", &fmd_conf_size, "10m" },	/* client buffer space limit */
2390Sstevel@tonic-gate { "client.dbout", &fmd_dbout_ops, NULL },	/* client debug output sinks */
2400Sstevel@tonic-gate { "client.debug", &fmd_conf_bool, NULL },	/* client debug enable */
24112967Sgavin.maltby@oracle.com { "client.doorthrlim", &fmd_conf_uint32, "20" }, /* client door thread limit */
2420Sstevel@tonic-gate { "client.error", &fmd_cerror_ops, "unload" },	/* client error policy */
2430Sstevel@tonic-gate { "client.memlim", &fmd_conf_size, "10m" },	/* client allocation limit */
2440Sstevel@tonic-gate { "client.evqlim", &fmd_conf_uint32, "256" },	/* client event queue limit */
2457850SVuong.Nguyen@Sun.COM { "client.thrlim", &fmd_conf_uint32, "20" },	/* client aux thread limit */
2460Sstevel@tonic-gate { "client.thrsig", &fmd_conf_signal, "SIGUSR1" }, /* fmd_thr_signal() value */
2470Sstevel@tonic-gate { "client.tmrlim", &fmd_conf_uint32, "1024" },	/* client pending timer limit */
2481193Smws { "client.xprtlim", &fmd_conf_uint32, "256" },	/* client transport limit */
2491193Smws { "client.xprtlog", &fmd_conf_bool, NULL },	/* client transport logging? */
2502808Sav145390 { "client.xprtqlim", &fmd_conf_uint32, "1024" }, /* client transport queue li */
2510Sstevel@tonic-gate { "clock", &fmd_clkmode_ops, "native" },	/* clock operation mode */
2520Sstevel@tonic-gate { "conf_path", &fmd_conf_path, _fmd_conf_path }, /* root config file path */
2530Sstevel@tonic-gate { "conf_file", &fmd_conf_string, "fmd.conf" },	/* root config file name */
2540Sstevel@tonic-gate { "core", &fmd_conf_bool, "false" },		/* force core dump on quit */
2550Sstevel@tonic-gate { "dbout", &fmd_dbout_ops, NULL },		/* daemon debug output sinks */
2560Sstevel@tonic-gate { "debug", &fmd_debug_ops, NULL },		/* daemon debugging flags */
2570Sstevel@tonic-gate { "dictdir", &fmd_conf_string, "usr/lib/fm/dict" }, /* default diagcode dir */
2580Sstevel@tonic-gate { "domain", &fmd_conf_string, NULL },		/* domain id for de auth */
2597275Sstephh { "fakenotpresent", &fmd_conf_uint32, "0" },	/* simulate rsrc not present */
2600Sstevel@tonic-gate { "fg", &fmd_conf_bool, "false" },		/* run daemon in foreground */
2610Sstevel@tonic-gate { "gc_interval", &fmd_conf_time, "1d" },	/* garbage collection intvl */
2620Sstevel@tonic-gate { "ids.avg", &fmd_conf_uint32, "4" },		/* desired idspace chain len */
2630Sstevel@tonic-gate { "ids.max", &fmd_conf_uint32, "1024" },	/* maximum idspace buckets */
2640Sstevel@tonic-gate { "isaname", &fmd_conf_string, _fmd_isa },	/* instruction set (uname -p) */
2650Sstevel@tonic-gate { "log.creator", &fmd_conf_string, "fmd" },	/* exacct log creator string */
2660Sstevel@tonic-gate { "log.error", &fmd_conf_string, "var/fm/fmd/errlog" }, /* error log path */
2670Sstevel@tonic-gate { "log.fault", &fmd_conf_string, "var/fm/fmd/fltlog" }, /* fault log path */
26812967Sgavin.maltby@oracle.com { "log.info", &fmd_conf_string, "var/fm/fmd/infolog" }, /* info log path */
26912967Sgavin.maltby@oracle.com { "log.info_hival", &fmd_conf_string, "var/fm/fmd/infolog_hival" }, /* hi pri */
2700Sstevel@tonic-gate { "log.minfree", &fmd_conf_size, "2m" },	/* min log fsys free space */
2711193Smws { "log.rsrc", &fmd_conf_string, "var/fm/fmd/rsrc" }, /* asru log dir path */
2720Sstevel@tonic-gate { "log.tryrotate", &fmd_conf_uint32, "10" },	/* max log rotation attempts */
2730Sstevel@tonic-gate { "log.waitrotate", &fmd_conf_time, "200ms" },	/* log rotation retry delay */
2741193Smws { "log.xprt", &fmd_conf_string, "var/fm/fmd/xprt" }, /* transport log dir */
2750Sstevel@tonic-gate { "machine", &fmd_conf_string, _fmd_uts.machine }, /* machine name (uname -m) */
2760Sstevel@tonic-gate { "nodiagcode", &fmd_conf_string, "-" },	/* diagcode to use if error */
2776276Scy152378 { "repaircode", &fmd_conf_string, "-" },	/* diagcode for list.repaired */
2787275Sstephh { "resolvecode", &fmd_conf_string, "-" },	/* diagcode for list.resolved */
2797275Sstephh { "updatecode", &fmd_conf_string, "-" },	/* diagcode for list.updated */
2800Sstevel@tonic-gate { "osrelease", &fmd_conf_string, _fmd_uts.release }, /* release (uname -r) */
2810Sstevel@tonic-gate { "osversion", &fmd_conf_string, _fmd_uts.version }, /* version (uname -v) */
2820Sstevel@tonic-gate { "platform", &fmd_conf_string, _fmd_plat },	/* platform string (uname -i) */
2830Sstevel@tonic-gate { "plugin.close", &fmd_conf_bool, "true" },	/* dlclose plugins on fini */
2840Sstevel@tonic-gate { "plugin.path", &fmd_conf_path, _fmd_plugin_path }, /* path for plugin mods */
2851414Scindi { "product", &fmd_conf_string, _fmd_prod },	/* product name string */
2860Sstevel@tonic-gate { "rootdir", &fmd_conf_string, "" },		/* root directory for paths */
2870Sstevel@tonic-gate { "rpc.adm.path", &fmd_conf_string, NULL },	/* FMD_ADM rendezvous file */
2880Sstevel@tonic-gate { "rpc.adm.prog", &fmd_conf_uint32, "100169" },	/* FMD_ADM rpc program num */
2890Sstevel@tonic-gate { "rpc.api.path", &fmd_conf_string, NULL },	/* FMD_API rendezvous file */
2900Sstevel@tonic-gate { "rpc.api.prog", &fmd_conf_uint32, "100170" },	/* FMD_API rpc program num */
2910Sstevel@tonic-gate { "rpc.rcvsize", &fmd_conf_size, "128k" },	/* rpc receive buffer size */
2920Sstevel@tonic-gate { "rpc.sndsize", &fmd_conf_size, "128k" },	/* rpc send buffer size */
29310656SStephen.Hanson@Sun.COM { "rsrc.pollperiod", &fmd_conf_time, "1h" },	/* aged rsrcs poller period */
2940Sstevel@tonic-gate { "rsrc.age", &fmd_conf_time, "30d" },		/* max age of old rsrc log */
2950Sstevel@tonic-gate { "rsrc.zero", &fmd_conf_bool, "false" },	/* zero rsrc cache on start? */
2960Sstevel@tonic-gate { "schemedir", &fmd_conf_string, _fmd_scheme_path }, /* path for scheme mods */
2970Sstevel@tonic-gate { "self.name", &fmd_conf_string, "fmd-self-diagnosis" }, /* self-diag module */
2980Sstevel@tonic-gate { "self.dict", &fmd_conf_list, "FMD.dict" },	/* self-diag dictionary list */
2990Sstevel@tonic-gate { "server", &fmd_conf_string, _fmd_uts.nodename }, /* server id for de auth */
3000Sstevel@tonic-gate { "strbuckets", &fmd_conf_uint32, "211" },	/* size of string hashes */
3010Sstevel@tonic-gate #ifdef DEBUG
3020Sstevel@tonic-gate { "trace.mode", &fmd_trmode_ops, "full" },	/* trace mode: none/lite/full */
3030Sstevel@tonic-gate #else
3040Sstevel@tonic-gate { "trace.mode", &fmd_trmode_ops, "lite" },	/* trace mode: none/lite/full */
3050Sstevel@tonic-gate #endif
3060Sstevel@tonic-gate { "trace.recs", &fmd_conf_uint32, "128" },	/* trace records per thread */
3070Sstevel@tonic-gate { "trace.frames", &fmd_conf_uint32, "16" },	/* max trace rec stack frames */
3080Sstevel@tonic-gate { "uuidlen", &fmd_conf_uint32, "36" },		/* UUID ASCII string length */
3091193Smws { "xprt.ttl", &fmd_conf_uint8, "1" },		/* default event time-to-live */
3100Sstevel@tonic-gate };
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate /*
3130Sstevel@tonic-gate  * Statistics maintained by fmd itself on behalf of various global subsystems.
3140Sstevel@tonic-gate  * NOTE: FMD_TYPE_STRING statistics should not be used here.  If they are
3150Sstevel@tonic-gate  * required in the future, the FMD_ADM_MODGSTAT service routine must change.
3160Sstevel@tonic-gate  */
3170Sstevel@tonic-gate static fmd_statistics_t _fmd_stats = {
3180Sstevel@tonic-gate { "errlog.replayed", FMD_TYPE_UINT64, "total events replayed from errlog" },
3190Sstevel@tonic-gate { "errlog.partials", FMD_TYPE_UINT64, "events partially committed in errlog" },
3200Sstevel@tonic-gate { "errlog.enospc", FMD_TYPE_UINT64, "events not appended to errlog (ENOSPC)" },
3210Sstevel@tonic-gate { "fltlog.enospc", FMD_TYPE_UINT64, "events not appended to fltlog (ENOSPC)" },
3220Sstevel@tonic-gate { "log.enospc", FMD_TYPE_UINT64, "events not appended to other logs (ENOSPC)" },
3230Sstevel@tonic-gate { "dr.gen", FMD_TYPE_UINT64, "dynamic reconfiguration generation" },
3243062Scindi { "topo.gen", FMD_TYPE_UINT64, "topology snapshot generation" },
3253062Scindi { "topo.drgen", FMD_TYPE_UINT64, "current topology DR generation number" },
3260Sstevel@tonic-gate };
3270Sstevel@tonic-gate 
3289874SStephen.Hanson@Sun.COM /*
3299874SStephen.Hanson@Sun.COM  * SMBIOS serial numbers can contain characters (particularly ':' and ' ')
3309874SStephen.Hanson@Sun.COM  * that are invalid for the authority and can break FMRI parsing.  We translate
3319874SStephen.Hanson@Sun.COM  * any invalid characters to a safe '-', as well as trimming any leading or
3329874SStephen.Hanson@Sun.COM  * trailing whitespace.  Similarly, '/' can be found in some product names
3339874SStephen.Hanson@Sun.COM  * so we translate that to '-'.
3349874SStephen.Hanson@Sun.COM  */
3359874SStephen.Hanson@Sun.COM void
fmd_cleanup_auth_str(char * buf,const char * begin)3369874SStephen.Hanson@Sun.COM fmd_cleanup_auth_str(char *buf, const char *begin)
3379874SStephen.Hanson@Sun.COM {
3389874SStephen.Hanson@Sun.COM 	const char *end, *cp;
3399874SStephen.Hanson@Sun.COM 	char c;
3409874SStephen.Hanson@Sun.COM 	int i;
3419874SStephen.Hanson@Sun.COM 
3429874SStephen.Hanson@Sun.COM 	end = begin + strlen(begin);
3439874SStephen.Hanson@Sun.COM 
3449874SStephen.Hanson@Sun.COM 	while (begin < end && isspace(*begin))
3459874SStephen.Hanson@Sun.COM 		begin++;
3469874SStephen.Hanson@Sun.COM 	while (begin < end && isspace(*(end - 1)))
3479874SStephen.Hanson@Sun.COM 		end--;
3489874SStephen.Hanson@Sun.COM 
3499874SStephen.Hanson@Sun.COM 	if (begin >= end)
3509874SStephen.Hanson@Sun.COM 		return;
3519874SStephen.Hanson@Sun.COM 
3529874SStephen.Hanson@Sun.COM 	cp = begin;
3539874SStephen.Hanson@Sun.COM 	for (i = 0; i < MAXNAMELEN - 1; i++) {
3549874SStephen.Hanson@Sun.COM 		if (cp >= end)
3559874SStephen.Hanson@Sun.COM 			break;
3569874SStephen.Hanson@Sun.COM 		c = *cp;
3579874SStephen.Hanson@Sun.COM 		if (c == ':' || c == '=' || c == '/' || isspace(c) ||
3589874SStephen.Hanson@Sun.COM 		    !isprint(c))
3599874SStephen.Hanson@Sun.COM 			buf[i] = '-';
3609874SStephen.Hanson@Sun.COM 		else
3619874SStephen.Hanson@Sun.COM 			buf[i] = c;
3629874SStephen.Hanson@Sun.COM 		cp++;
3639874SStephen.Hanson@Sun.COM 	}
3649874SStephen.Hanson@Sun.COM 	buf[i] = 0;
3659874SStephen.Hanson@Sun.COM }
3669874SStephen.Hanson@Sun.COM 
3670Sstevel@tonic-gate void
fmd_create(fmd_t * dp,const char * arg0,const char * root,const char * conf)3680Sstevel@tonic-gate fmd_create(fmd_t *dp, const char *arg0, const char *root, const char *conf)
3690Sstevel@tonic-gate {
3700Sstevel@tonic-gate 	fmd_conf_path_t *pap;
3710Sstevel@tonic-gate 	char file[PATH_MAX];
37210462SSean.Ye@Sun.COM 	const char *name, *psn, *csn;
3730Sstevel@tonic-gate 	fmd_stat_t *sp;
3740Sstevel@tonic-gate 	int i;
3750Sstevel@tonic-gate 
3761414Scindi 	smbios_hdl_t *shp;
3771414Scindi 	smbios_system_t s1;
3781414Scindi 	smbios_info_t s2;
3791414Scindi 	id_t id;
3801414Scindi 
3812112Sav145390 	di_prom_handle_t promh = DI_PROM_HANDLE_NIL;
3822112Sav145390 	di_node_t rooth = DI_NODE_NIL;
3832112Sav145390 	char *bufp;
3842112Sav145390 
3850Sstevel@tonic-gate 	(void) sysinfo(SI_PLATFORM, _fmd_plat, sizeof (_fmd_plat));
3860Sstevel@tonic-gate 	(void) sysinfo(SI_ARCHITECTURE, _fmd_isa, sizeof (_fmd_isa));
3870Sstevel@tonic-gate 	(void) uname(&_fmd_uts);
3880Sstevel@tonic-gate 
3891414Scindi 	if ((shp = smbios_open(NULL, SMB_VERSION, 0, NULL)) != NULL) {
3901414Scindi 		if ((id = smbios_info_system(shp, &s1)) != SMB_ERR &&
39110462SSean.Ye@Sun.COM 		    smbios_info_common(shp, id, &s2) != SMB_ERR)
3929874SStephen.Hanson@Sun.COM 			fmd_cleanup_auth_str(_fmd_prod, s2.smbi_product);
39310462SSean.Ye@Sun.COM 
39410462SSean.Ye@Sun.COM 		if ((psn = smbios_psn(shp)) != NULL)
39510462SSean.Ye@Sun.COM 			fmd_cleanup_auth_str(_fmd_psn, psn);
39610462SSean.Ye@Sun.COM 
39710462SSean.Ye@Sun.COM 		if ((csn = smbios_csn(shp)) != NULL)
39810462SSean.Ye@Sun.COM 			fmd_cleanup_auth_str(_fmd_csn, csn);
39910462SSean.Ye@Sun.COM 
4001414Scindi 		smbios_close(shp);
4012338Shueston 	} else if ((rooth = di_init("/", DINFOPROP)) != DI_NODE_NIL &&
4022112Sav145390 	    (promh = di_prom_init()) != DI_PROM_HANDLE_NIL) {
4032112Sav145390 		if (di_prom_prop_lookup_bytes(promh, rooth, "chassis-sn",
4042112Sav145390 		    (unsigned char **)&bufp) != -1) {
4059874SStephen.Hanson@Sun.COM 			fmd_cleanup_auth_str(_fmd_csn, bufp);
4062112Sav145390 		}
4071414Scindi 	}
4081414Scindi 
4092112Sav145390 	if (promh != DI_PROM_HANDLE_NIL)
4102112Sav145390 		di_prom_fini(promh);
4112112Sav145390 	if (rooth != DI_NODE_NIL)
4122112Sav145390 		di_fini(rooth);
4132112Sav145390 
4140Sstevel@tonic-gate 	bzero(dp, sizeof (fmd_t));
4150Sstevel@tonic-gate 
4160Sstevel@tonic-gate 	dp->d_version = _fmd_version;
4170Sstevel@tonic-gate 	dp->d_pname = fmd_strbasename(arg0);
4180Sstevel@tonic-gate 	dp->d_pid = getpid();
4190Sstevel@tonic-gate 
4200Sstevel@tonic-gate 	if (pthread_key_create(&dp->d_key, NULL) != 0)
4210Sstevel@tonic-gate 		fmd_error(EFMD_EXIT, "failed to create pthread key");
4220Sstevel@tonic-gate 
4230Sstevel@tonic-gate 	(void) pthread_mutex_init(&dp->d_xprt_lock, NULL);
4240Sstevel@tonic-gate 	(void) pthread_mutex_init(&dp->d_err_lock, NULL);
4250Sstevel@tonic-gate 	(void) pthread_mutex_init(&dp->d_thr_lock, NULL);
4260Sstevel@tonic-gate 	(void) pthread_mutex_init(&dp->d_mod_lock, NULL);
4270Sstevel@tonic-gate 	(void) pthread_mutex_init(&dp->d_stats_lock, NULL);
4283062Scindi 	(void) pthread_mutex_init(&dp->d_topo_lock, NULL);
4290Sstevel@tonic-gate 	(void) pthread_rwlock_init(&dp->d_log_lock, NULL);
43012967Sgavin.maltby@oracle.com 	(void) pthread_rwlock_init(&dp->d_hvilog_lock, NULL);
43112967Sgavin.maltby@oracle.com 	(void) pthread_rwlock_init(&dp->d_ilog_lock, NULL);
4326559Sstephh 	(void) pthread_mutex_init(&dp->d_fmd_lock, NULL);
4336559Sstephh 	(void) pthread_cond_init(&dp->d_fmd_cv, NULL);
4340Sstevel@tonic-gate 
4350Sstevel@tonic-gate 	/*
4360Sstevel@tonic-gate 	 * A small number of properties must be set manually before we open
4370Sstevel@tonic-gate 	 * the root configuration file.  These include any settings for our
4380Sstevel@tonic-gate 	 * memory allocator and path expansion token values, because these
4390Sstevel@tonic-gate 	 * values are needed by the routines in fmd_conf.c itself.  After
4400Sstevel@tonic-gate 	 * the root configuration file is processed, we reset these properties
4410Sstevel@tonic-gate 	 * based upon the latest values from the configuration file.
4420Sstevel@tonic-gate 	 */
4430Sstevel@tonic-gate 	dp->d_alloc_msecs = 10;
4440Sstevel@tonic-gate 	dp->d_alloc_tries = 3;
4450Sstevel@tonic-gate 	dp->d_str_buckets = 211;
4460Sstevel@tonic-gate 
4470Sstevel@tonic-gate 	dp->d_rootdir = root ? root : "";
4480Sstevel@tonic-gate 	dp->d_platform = _fmd_plat;
4490Sstevel@tonic-gate 	dp->d_machine = _fmd_uts.machine;
4500Sstevel@tonic-gate 	dp->d_isaname = _fmd_isa;
4510Sstevel@tonic-gate 
4521193Smws 	dp->d_conf = fmd_conf_open(conf, sizeof (_fmd_conf) /
4531193Smws 	    sizeof (_fmd_conf[0]), _fmd_conf, FMD_CONF_DEFER);
4540Sstevel@tonic-gate 
4550Sstevel@tonic-gate 	if (dp->d_conf == NULL) {
4560Sstevel@tonic-gate 		fmd_error(EFMD_EXIT,
4570Sstevel@tonic-gate 		    "failed to load required configuration properties\n");
4580Sstevel@tonic-gate 	}
4590Sstevel@tonic-gate 
4600Sstevel@tonic-gate 	(void) fmd_conf_getprop(dp->d_conf, "alloc.msecs", &dp->d_alloc_msecs);
4610Sstevel@tonic-gate 	(void) fmd_conf_getprop(dp->d_conf, "alloc.tries", &dp->d_alloc_tries);
4620Sstevel@tonic-gate 	(void) fmd_conf_getprop(dp->d_conf, "strbuckets", &dp->d_str_buckets);
4630Sstevel@tonic-gate 
4640Sstevel@tonic-gate 	(void) fmd_conf_getprop(dp->d_conf, "platform", &dp->d_platform);
4650Sstevel@tonic-gate 	(void) fmd_conf_getprop(dp->d_conf, "machine", &dp->d_machine);
4660Sstevel@tonic-gate 	(void) fmd_conf_getprop(dp->d_conf, "isaname", &dp->d_isaname);
4670Sstevel@tonic-gate 
4680Sstevel@tonic-gate 	/*
4690Sstevel@tonic-gate 	 * Manually specified rootdirs override config files, so only update
4700Sstevel@tonic-gate 	 * d_rootdir based on the config files we parsed if no 'root' was set.
4710Sstevel@tonic-gate 	 */
4720Sstevel@tonic-gate 	if (root == NULL)
4730Sstevel@tonic-gate 		(void) fmd_conf_getprop(dp->d_conf, "rootdir", &dp->d_rootdir);
4740Sstevel@tonic-gate 	else
4750Sstevel@tonic-gate 		(void) fmd_conf_setprop(dp->d_conf, "rootdir", dp->d_rootdir);
4760Sstevel@tonic-gate 
4770Sstevel@tonic-gate 	/*
4780Sstevel@tonic-gate 	 * Once the base conf file properties are loaded, lookup the values
4790Sstevel@tonic-gate 	 * of $conf_path and $conf_file and merge in any other conf files.
4800Sstevel@tonic-gate 	 */
4810Sstevel@tonic-gate 	(void) fmd_conf_getprop(dp->d_conf, "conf_path", &pap);
4820Sstevel@tonic-gate 	(void) fmd_conf_getprop(dp->d_conf, "conf_file", &name);
4830Sstevel@tonic-gate 
4840Sstevel@tonic-gate 	for (i = 0; i < pap->cpa_argc; i++) {
4850Sstevel@tonic-gate 		(void) snprintf(file, sizeof (file),
4860Sstevel@tonic-gate 		    "%s/%s", pap->cpa_argv[i], name);
4870Sstevel@tonic-gate 		if (access(file, F_OK) == 0)
4880Sstevel@tonic-gate 			fmd_conf_merge(dp->d_conf, file);
4890Sstevel@tonic-gate 	}
4900Sstevel@tonic-gate 
4910Sstevel@tonic-gate 	/*
4920Sstevel@tonic-gate 	 * Update the value of fmd.d_fg based on "fg".  We cache this property
4930Sstevel@tonic-gate 	 * because it must be accessed deep within fmd at fmd_verror() time.
4941193Smws 	 * Update any other properties that must be cached for performance.
4950Sstevel@tonic-gate 	 */
4960Sstevel@tonic-gate 	(void) fmd_conf_getprop(fmd.d_conf, "fg", &fmd.d_fg);
4971193Smws 	(void) fmd_conf_getprop(fmd.d_conf, "xprt.ttl", &fmd.d_xprt_ttl);
4980Sstevel@tonic-gate 
4990Sstevel@tonic-gate 	/*
5000Sstevel@tonic-gate 	 * Initialize our custom libnvpair allocator and create an nvlist for
5010Sstevel@tonic-gate 	 * authority elements corresponding to this instance of the daemon.
5020Sstevel@tonic-gate 	 */
5030Sstevel@tonic-gate 	(void) nv_alloc_init(&dp->d_nva, &fmd_nv_alloc_ops);
5040Sstevel@tonic-gate 	dp->d_auth = fmd_protocol_authority();
5050Sstevel@tonic-gate 
5060Sstevel@tonic-gate 	/*
5070Sstevel@tonic-gate 	 * The fmd_module_t for the root module must be created manually.  Most
5080Sstevel@tonic-gate 	 * of it remains unused and zero, except for the few things we fill in.
5090Sstevel@tonic-gate 	 */
5100Sstevel@tonic-gate 	dp->d_rmod = fmd_zalloc(sizeof (fmd_module_t), FMD_SLEEP);
5110Sstevel@tonic-gate 	dp->d_rmod->mod_name = fmd_strdup(dp->d_pname, FMD_SLEEP);
5121193Smws 	dp->d_rmod->mod_fmri = fmd_protocol_fmri_module(dp->d_rmod);
5131193Smws 
5140Sstevel@tonic-gate 	fmd_list_append(&dp->d_mod_list, dp->d_rmod);
5151193Smws 	fmd_module_hold(dp->d_rmod);
5160Sstevel@tonic-gate 
5170Sstevel@tonic-gate 	(void) pthread_mutex_init(&dp->d_rmod->mod_lock, NULL);
5180Sstevel@tonic-gate 	(void) pthread_cond_init(&dp->d_rmod->mod_cv, NULL);
5191193Smws 	(void) pthread_mutex_init(&dp->d_rmod->mod_stats_lock, NULL);
5200Sstevel@tonic-gate 
5210Sstevel@tonic-gate 	dp->d_rmod->mod_thread = fmd_thread_xcreate(dp->d_rmod, pthread_self());
5221193Smws 	dp->d_rmod->mod_stats = fmd_zalloc(sizeof (fmd_modstat_t), FMD_SLEEP);
5230Sstevel@tonic-gate 	dp->d_rmod->mod_ustat = fmd_ustat_create();
5240Sstevel@tonic-gate 
5250Sstevel@tonic-gate 	if (pthread_setspecific(dp->d_key, dp->d_rmod->mod_thread) != 0)
5260Sstevel@tonic-gate 		fmd_error(EFMD_EXIT, "failed to attach main thread key");
5270Sstevel@tonic-gate 
5280Sstevel@tonic-gate 	if ((dp->d_stats = (fmd_statistics_t *)fmd_ustat_insert(
5290Sstevel@tonic-gate 	    dp->d_rmod->mod_ustat, FMD_USTAT_NOALLOC, sizeof (_fmd_stats) /
5300Sstevel@tonic-gate 	    sizeof (fmd_stat_t), (fmd_stat_t *)&_fmd_stats, NULL)) == NULL)
5310Sstevel@tonic-gate 		fmd_error(EFMD_EXIT, "failed to initialize statistics");
5320Sstevel@tonic-gate 
5331193Smws 	(void) pthread_mutex_lock(&dp->d_rmod->mod_lock);
5341193Smws 	dp->d_rmod->mod_flags |= FMD_MOD_INIT;
5351193Smws 	(void) pthread_mutex_unlock(&dp->d_rmod->mod_lock);
5361193Smws 
5370Sstevel@tonic-gate 	/*
5380Sstevel@tonic-gate 	 * In addition to inserting the _fmd_stats collection of program-wide
5390Sstevel@tonic-gate 	 * statistics, we also insert a statistic named after each of our
5400Sstevel@tonic-gate 	 * errors and update these counts in fmd_verror() (see fmd_subr.c).
5410Sstevel@tonic-gate 	 */
5420Sstevel@tonic-gate 	dp->d_errstats = sp = fmd_zalloc(sizeof (fmd_stat_t) *
5430Sstevel@tonic-gate 	    (EFMD_END - EFMD_UNKNOWN), FMD_SLEEP);
5440Sstevel@tonic-gate 
5450Sstevel@tonic-gate 	for (i = 0; i < EFMD_END - EFMD_UNKNOWN; i++, sp++) {
5460Sstevel@tonic-gate 		(void) snprintf(sp->fmds_name, sizeof (sp->fmds_name), "err.%s",
5470Sstevel@tonic-gate 		    strrchr(fmd_errclass(EFMD_UNKNOWN + i), '.') + 1);
5480Sstevel@tonic-gate 		sp->fmds_type = FMD_TYPE_UINT64;
5490Sstevel@tonic-gate 	}
5500Sstevel@tonic-gate 
5510Sstevel@tonic-gate 	(void) fmd_ustat_insert(dp->d_rmod->mod_ustat, FMD_USTAT_NOALLOC,
5520Sstevel@tonic-gate 	    EFMD_END - EFMD_UNKNOWN, dp->d_errstats, NULL);
5530Sstevel@tonic-gate }
5540Sstevel@tonic-gate 
5550Sstevel@tonic-gate void
fmd_destroy(fmd_t * dp)5560Sstevel@tonic-gate fmd_destroy(fmd_t *dp)
5570Sstevel@tonic-gate {
5580Sstevel@tonic-gate 	fmd_module_t *mp;
5591193Smws 	fmd_case_t *cp;
5600Sstevel@tonic-gate 	int core;
5610Sstevel@tonic-gate 
5620Sstevel@tonic-gate 	(void) fmd_conf_getprop(fmd.d_conf, "core", &core);
5630Sstevel@tonic-gate 
5640Sstevel@tonic-gate 	fmd_rpc_fini();
5651222Smws 
5661222Smws 	if (dp->d_xprt_ids != NULL)
5671222Smws 		fmd_xprt_suspend_all();
5680Sstevel@tonic-gate 
5690Sstevel@tonic-gate 	/*
5700Sstevel@tonic-gate 	 * Unload the self-diagnosis module first.  This ensures that it does
5710Sstevel@tonic-gate 	 * not get confused as we start unloading other modules, etc.  We must
5720Sstevel@tonic-gate 	 * hold the dispq lock as a writer while doing so since it uses d_self.
5730Sstevel@tonic-gate 	 */
5740Sstevel@tonic-gate 	if (dp->d_self != NULL) {
5752438Scindi 		fmd_module_t *self;
5762438Scindi 
5770Sstevel@tonic-gate 		(void) pthread_rwlock_wrlock(&dp->d_disp->dq_lock);
5782438Scindi 		self = dp->d_self;
5790Sstevel@tonic-gate 		dp->d_self = NULL;
5800Sstevel@tonic-gate 		(void) pthread_rwlock_unlock(&dp->d_disp->dq_lock);
5812438Scindi 
5822438Scindi 		fmd_module_unload(self);
5832438Scindi 		fmd_module_rele(self);
5840Sstevel@tonic-gate 	}
5850Sstevel@tonic-gate 
5860Sstevel@tonic-gate 	/*
5870Sstevel@tonic-gate 	 * Unload modules in reverse order *except* for the root module, which
5880Sstevel@tonic-gate 	 * is first in the list.  This allows it to keep its thread and trace.
5890Sstevel@tonic-gate 	 */
5900Sstevel@tonic-gate 	for (mp = fmd_list_prev(&dp->d_mod_list); mp != dp->d_rmod; ) {
5910Sstevel@tonic-gate 		fmd_module_unload(mp);
5920Sstevel@tonic-gate 		mp = fmd_list_prev(mp);
5930Sstevel@tonic-gate 	}
5940Sstevel@tonic-gate 
5950Sstevel@tonic-gate 	if (dp->d_mod_hash != NULL) {
5960Sstevel@tonic-gate 		fmd_modhash_destroy(dp->d_mod_hash);
5970Sstevel@tonic-gate 		dp->d_mod_hash = NULL;
5980Sstevel@tonic-gate 	}
5990Sstevel@tonic-gate 
6000Sstevel@tonic-gate 	/*
6010Sstevel@tonic-gate 	 * Close both log files now that modules are no longer active.  We must
6020Sstevel@tonic-gate 	 * set these pointers to NULL in case any subsequent errors occur.
6030Sstevel@tonic-gate 	 */
6040Sstevel@tonic-gate 	if (dp->d_errlog != NULL) {
6050Sstevel@tonic-gate 		fmd_log_rele(dp->d_errlog);
6060Sstevel@tonic-gate 		dp->d_errlog = NULL;
6070Sstevel@tonic-gate 	}
6080Sstevel@tonic-gate 
6090Sstevel@tonic-gate 	if (dp->d_fltlog != NULL) {
6100Sstevel@tonic-gate 		fmd_log_rele(dp->d_fltlog);
6110Sstevel@tonic-gate 		dp->d_fltlog = NULL;
6120Sstevel@tonic-gate 	}
6130Sstevel@tonic-gate 
6140Sstevel@tonic-gate 	/*
6151193Smws 	 * Now destroy the resource cache: each ASRU contains a case reference,
6161193Smws 	 * which may in turn contain a pointer to a referenced owning module.
6171193Smws 	 */
6181193Smws 	if (dp->d_asrus != NULL) {
6191193Smws 		fmd_asru_hash_destroy(dp->d_asrus);
6201193Smws 		dp->d_asrus = NULL;
6211193Smws 	}
6221193Smws 
6231193Smws 	/*
6240Sstevel@tonic-gate 	 * Now that all data structures that refer to modules are torn down,
6250Sstevel@tonic-gate 	 * no modules should be remaining on the module list except for d_rmod.
6260Sstevel@tonic-gate 	 * If we trip one of these assertions, we're missing a rele somewhere.
6270Sstevel@tonic-gate 	 */
6280Sstevel@tonic-gate 	ASSERT(fmd_list_prev(&dp->d_mod_list) == dp->d_rmod);
6290Sstevel@tonic-gate 	ASSERT(fmd_list_next(&dp->d_mod_list) == dp->d_rmod);
6300Sstevel@tonic-gate 
6310Sstevel@tonic-gate 	/*
6320Sstevel@tonic-gate 	 * Now destroy the root module.  We clear its thread key first so any
6330Sstevel@tonic-gate 	 * calls to fmd_trace() inside of the module code will be ignored.
6340Sstevel@tonic-gate 	 */
6350Sstevel@tonic-gate 	(void) pthread_setspecific(dp->d_key, NULL);
6361193Smws 	fmd_module_lock(dp->d_rmod);
6371193Smws 
6381193Smws 	while ((cp = fmd_list_next(&dp->d_rmod->mod_cases)) != NULL)
6399120SStephen.Hanson@Sun.COM 		fmd_case_discard(cp, B_FALSE);
6401193Smws 
6411193Smws 	fmd_module_unlock(dp->d_rmod);
6421193Smws 	fmd_free(dp->d_rmod->mod_stats, sizeof (fmd_modstat_t));
6431193Smws 	dp->d_rmod->mod_stats = NULL;
6440Sstevel@tonic-gate 
6451193Smws 	(void) pthread_mutex_lock(&dp->d_rmod->mod_lock);
6461193Smws 	dp->d_rmod->mod_flags |= FMD_MOD_FINI;
6471193Smws 	(void) pthread_mutex_unlock(&dp->d_rmod->mod_lock);
6481193Smws 
6491193Smws 	fmd_module_rele(dp->d_rmod);
6501193Smws 	ASSERT(fmd_list_next(&dp->d_mod_list) == NULL);
6511193Smws 
6521193Smws 	/*
6531193Smws 	 * Now destroy the remaining global data structures.  If 'core' was
6541193Smws 	 * set to true, force a core dump so we can check for memory leaks.
6551193Smws 	 */
6561193Smws 	if (dp->d_cases != NULL)
6571193Smws 		fmd_case_hash_destroy(dp->d_cases);
6580Sstevel@tonic-gate 	if (dp->d_disp != NULL)
6590Sstevel@tonic-gate 		fmd_dispq_destroy(dp->d_disp);
6601193Smws 	if (dp->d_timers != NULL)
6611193Smws 		fmd_timerq_destroy(dp->d_timers);
6620Sstevel@tonic-gate 	if (dp->d_schemes != NULL)
6630Sstevel@tonic-gate 		fmd_scheme_hash_destroy(dp->d_schemes);
6641193Smws 	if (dp->d_xprt_ids != NULL)
6651193Smws 		fmd_idspace_destroy(dp->d_xprt_ids);
6660Sstevel@tonic-gate 
6670Sstevel@tonic-gate 	if (dp->d_errstats != NULL) {
6680Sstevel@tonic-gate 		fmd_free(dp->d_errstats,
6690Sstevel@tonic-gate 		    sizeof (fmd_stat_t) * (EFMD_END - EFMD_UNKNOWN));
6700Sstevel@tonic-gate 	}
6710Sstevel@tonic-gate 
6720Sstevel@tonic-gate 	if (dp->d_conf != NULL)
6730Sstevel@tonic-gate 		fmd_conf_close(dp->d_conf);
6740Sstevel@tonic-gate 
6753062Scindi 	fmd_topo_fini();
6761414Scindi 
6770Sstevel@tonic-gate 	nvlist_free(dp->d_auth);
6780Sstevel@tonic-gate 	(void) nv_alloc_fini(&dp->d_nva);
6790Sstevel@tonic-gate 	dp->d_clockops->fto_fini(dp->d_clockptr);
6800Sstevel@tonic-gate 
6810Sstevel@tonic-gate 	(void) pthread_key_delete(dp->d_key);
6820Sstevel@tonic-gate 	bzero(dp, sizeof (fmd_t));
6830Sstevel@tonic-gate 
6840Sstevel@tonic-gate 	if (core)
6850Sstevel@tonic-gate 		fmd_panic("forcing core dump at user request\n");
6860Sstevel@tonic-gate }
6870Sstevel@tonic-gate 
6880Sstevel@tonic-gate /*ARGSUSED*/
6890Sstevel@tonic-gate static void
fmd_gc(fmd_t * dp,id_t id,hrtime_t hrt)6900Sstevel@tonic-gate fmd_gc(fmd_t *dp, id_t id, hrtime_t hrt)
6910Sstevel@tonic-gate {
6920Sstevel@tonic-gate 	hrtime_t delta;
6930Sstevel@tonic-gate 
6940Sstevel@tonic-gate 	if (id != 0) {
6950Sstevel@tonic-gate 		TRACE((FMD_DBG_MOD, "garbage collect start"));
6960Sstevel@tonic-gate 		fmd_modhash_apply(dp->d_mod_hash, fmd_module_gc);
6970Sstevel@tonic-gate 		TRACE((FMD_DBG_MOD, "garbage collect end"));
6980Sstevel@tonic-gate 
6990Sstevel@tonic-gate 		(void) pthread_rwlock_rdlock(&dp->d_log_lock);
7000Sstevel@tonic-gate 		fmd_log_update(dp->d_errlog);
7010Sstevel@tonic-gate 		(void) pthread_rwlock_unlock(&dp->d_log_lock);
70212967Sgavin.maltby@oracle.com 
70312967Sgavin.maltby@oracle.com 		(void) pthread_rwlock_rdlock(&dp->d_hvilog_lock);
70412967Sgavin.maltby@oracle.com 		fmd_log_update(dp->d_hvilog);
70512967Sgavin.maltby@oracle.com 		(void) pthread_rwlock_unlock(&dp->d_hvilog_lock);
70612967Sgavin.maltby@oracle.com 
70712967Sgavin.maltby@oracle.com 		(void) pthread_rwlock_rdlock(&dp->d_ilog_lock);
70812967Sgavin.maltby@oracle.com 		fmd_log_update(dp->d_ilog);
70912967Sgavin.maltby@oracle.com 		(void) pthread_rwlock_unlock(&dp->d_ilog_lock);
7100Sstevel@tonic-gate 	}
7110Sstevel@tonic-gate 
7120Sstevel@tonic-gate 	(void) fmd_conf_getprop(dp->d_conf, "gc_interval", &delta);
7130Sstevel@tonic-gate 	(void) fmd_timerq_install(dp->d_timers, dp->d_rmod->mod_timerids,
7140Sstevel@tonic-gate 	    (fmd_timer_f *)fmd_gc, dp, NULL, delta);
7150Sstevel@tonic-gate }
7160Sstevel@tonic-gate 
7176228Sstephh /*ARGSUSED*/
7186228Sstephh static void
fmd_clear_aged_rsrcs(fmd_t * dp,id_t id,hrtime_t hrt)7196228Sstephh fmd_clear_aged_rsrcs(fmd_t *dp, id_t id, hrtime_t hrt)
7206228Sstephh {
72110656SStephen.Hanson@Sun.COM 	hrtime_t period;
7226228Sstephh 
7236228Sstephh 	fmd_asru_clear_aged_rsrcs();
72410656SStephen.Hanson@Sun.COM 	(void) fmd_conf_getprop(dp->d_conf, "rsrc.pollperiod", &period);
7256228Sstephh 	(void) fmd_timerq_install(dp->d_timers, dp->d_rmod->mod_timerids,
72610656SStephen.Hanson@Sun.COM 	    (fmd_timer_f *)fmd_clear_aged_rsrcs, dp, NULL, period);
7276228Sstephh }
7286228Sstephh 
7290Sstevel@tonic-gate /*
7300Sstevel@tonic-gate  * Events are committed to the errlog after cases are checkpointed.  If fmd
7310Sstevel@tonic-gate  * crashes before an event is ever associated with a module, this function will
7320Sstevel@tonic-gate  * be called to replay it to all subscribers.  If fmd crashes in between the
7330Sstevel@tonic-gate  * subscriber checkpointing and committing the event in the error log, the
7340Sstevel@tonic-gate  * module will have seen the event and we don't want to replay it.  So we look
7350Sstevel@tonic-gate  * for the event in all modules and transition it to the proper state.  If
7360Sstevel@tonic-gate  * it is found, we commit it to the error log and do not replay it.  The in-
7370Sstevel@tonic-gate  * memory case search used by fmd_module_contains() et al isn't particularly
7380Sstevel@tonic-gate  * efficient, but it is faster than doing read i/o's on every case event to
7390Sstevel@tonic-gate  * check their status or write i/o's on every event to replay to update states.
7400Sstevel@tonic-gate  * We can improve the efficiency of this lookup algorithm later if necessary.
7410Sstevel@tonic-gate  */
7420Sstevel@tonic-gate /*ARGSUSED*/
7430Sstevel@tonic-gate static void
fmd_err_replay(fmd_log_t * lp,fmd_event_t * ep,fmd_t * dp)7440Sstevel@tonic-gate fmd_err_replay(fmd_log_t *lp, fmd_event_t *ep, fmd_t *dp)
7450Sstevel@tonic-gate {
7460Sstevel@tonic-gate 	fmd_module_t *mp;
7470Sstevel@tonic-gate 	fmd_stat_t *sp;
7480Sstevel@tonic-gate 
7490Sstevel@tonic-gate 	(void) pthread_mutex_lock(&dp->d_mod_lock);
7500Sstevel@tonic-gate 
7510Sstevel@tonic-gate 	for (mp = fmd_list_next(&dp->d_mod_list);
7520Sstevel@tonic-gate 	    mp != NULL; mp = fmd_list_next(mp)) {
7530Sstevel@tonic-gate 		if (fmd_module_contains(mp, ep)) {
7540Sstevel@tonic-gate 			fmd_module_hold(mp);
7550Sstevel@tonic-gate 			break;
7560Sstevel@tonic-gate 		}
7570Sstevel@tonic-gate 	}
7580Sstevel@tonic-gate 
7590Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&dp->d_mod_lock);
7600Sstevel@tonic-gate 
7610Sstevel@tonic-gate 	if (mp != NULL) {
7620Sstevel@tonic-gate 		fmd_event_commit(ep);
7630Sstevel@tonic-gate 		fmd_module_rele(mp);
7640Sstevel@tonic-gate 		sp = &dp->d_stats->ds_log_partials;
7650Sstevel@tonic-gate 	} else {
7661193Smws 		fmd_dispq_dispatch(dp->d_disp, ep, FMD_EVENT_DATA(ep));
7670Sstevel@tonic-gate 		sp = &dp->d_stats->ds_log_replayed;
7680Sstevel@tonic-gate 	}
7690Sstevel@tonic-gate 
7700Sstevel@tonic-gate 	(void) pthread_mutex_lock(&dp->d_stats_lock);
7710Sstevel@tonic-gate 	sp->fmds_value.ui64++;
7720Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&dp->d_stats_lock);
7730Sstevel@tonic-gate }
7740Sstevel@tonic-gate 
7751193Smws void
fmd_door_server(void * dip)7761193Smws fmd_door_server(void *dip)
7771193Smws {
7781193Smws 	fmd_dprintf(FMD_DBG_XPRT, "door server starting for %p\n", dip);
7791193Smws 	(void) pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
7801193Smws 	(void) door_return(NULL, 0, NULL, 0);
7811193Smws }
7821193Smws 
7831193Smws /*
7841193Smws  * Custom door server create callback.  Any fmd services that use doors will
7851193Smws  * require those threads to have their fmd-specific TSD initialized, etc.
78612967Sgavin.maltby@oracle.com  * Modules should use door_xcreate and derivatives such as
78712967Sgavin.maltby@oracle.com  * sysevent_evc_xsubscribe in order to use private doors that
78812967Sgavin.maltby@oracle.com  * avoid this global door server function (see fmd_api_module comments).
7891193Smws  */
7901193Smws static void
fmd_door(door_info_t * dip)7911193Smws fmd_door(door_info_t *dip)
7921193Smws {
7931193Smws 	if (fmd_thread_create(fmd.d_rmod, fmd_door_server, dip) == NULL)
7941193Smws 		fmd_panic("failed to create server for door %p", (void *)dip);
7951193Smws }
7961193Smws 
7970Sstevel@tonic-gate /*
7980Sstevel@tonic-gate  * This signal handler is installed for the client.thrsig signal to be used to
7990Sstevel@tonic-gate  * force an auxiliary thread to wake up from a system call and return EINTR in
8000Sstevel@tonic-gate  * response to a module's use of fmd_thr_signal().  We also trace the event.
8010Sstevel@tonic-gate  */
8020Sstevel@tonic-gate static void
fmd_signal(int sig)8030Sstevel@tonic-gate fmd_signal(int sig)
8040Sstevel@tonic-gate {
8050Sstevel@tonic-gate 	TRACE((FMD_DBG_MOD, "module thread received sig #%d", sig));
8060Sstevel@tonic-gate }
8070Sstevel@tonic-gate 
8080Sstevel@tonic-gate void
fmd_run(fmd_t * dp,int pfd)8090Sstevel@tonic-gate fmd_run(fmd_t *dp, int pfd)
8100Sstevel@tonic-gate {
8110Sstevel@tonic-gate 	char *nodc_key[] = { FMD_FLT_NODC, NULL };
8126276Scy152378 	char *repair_key[] = { FM_LIST_REPAIRED_CLASS, NULL };
8137275Sstephh 	char *resolve_key[] = { FM_LIST_RESOLVED_CLASS, NULL };
8147275Sstephh 	char *update_key[] = { FM_LIST_UPDATED_CLASS, NULL };
8156276Scy152378 	char code_str[128];
8160Sstevel@tonic-gate 	struct sigaction act;
8170Sstevel@tonic-gate 
8180Sstevel@tonic-gate 	int status = FMD_EXIT_SUCCESS;
8190Sstevel@tonic-gate 	const char *name;
8200Sstevel@tonic-gate 	fmd_conf_path_t *pap;
8211193Smws 	fmd_event_t *e;
8223062Scindi 	int dbout;
8230Sstevel@tonic-gate 
8240Sstevel@tonic-gate 	/*
8250Sstevel@tonic-gate 	 * Cache all the current debug property settings in d_fmd_debug,
8260Sstevel@tonic-gate 	 * d_fmd_dbout, d_hdl_debug, and d_hdl_dbout.  If a given debug mask
8270Sstevel@tonic-gate 	 * is non-zero and the corresponding dbout mask is zero, set dbout
8280Sstevel@tonic-gate 	 * to a sensible default value based on whether we have daemonized.
8290Sstevel@tonic-gate 	 */
8300Sstevel@tonic-gate 	(void) fmd_conf_getprop(dp->d_conf, "dbout", &dbout);
8310Sstevel@tonic-gate 
8320Sstevel@tonic-gate 	if (dp->d_fmd_debug != 0 && dbout == 0)
8330Sstevel@tonic-gate 		dp->d_fmd_dbout = dp->d_fg? FMD_DBOUT_STDERR : FMD_DBOUT_SYSLOG;
8340Sstevel@tonic-gate 	else
8350Sstevel@tonic-gate 		dp->d_fmd_dbout = dbout;
8360Sstevel@tonic-gate 
8370Sstevel@tonic-gate 	(void) fmd_conf_getprop(dp->d_conf, "client.debug", &dp->d_hdl_debug);
8380Sstevel@tonic-gate 	(void) fmd_conf_getprop(dp->d_conf, "client.dbout", &dbout);
8390Sstevel@tonic-gate 
8400Sstevel@tonic-gate 	if (dp->d_hdl_debug != 0 && dbout == 0)
8410Sstevel@tonic-gate 		dp->d_hdl_dbout = dp->d_fg? FMD_DBOUT_STDERR : FMD_DBOUT_SYSLOG;
8420Sstevel@tonic-gate 	else
8430Sstevel@tonic-gate 		dp->d_hdl_dbout = dbout;
8440Sstevel@tonic-gate 
8450Sstevel@tonic-gate 	/*
8461193Smws 	 * Initialize remaining major program data structures such as the
8471193Smws 	 * clock, dispatch queues, log files, module hash collections, etc.
8480Sstevel@tonic-gate 	 * This work is done here rather than in fmd_create() to permit the -o
8490Sstevel@tonic-gate 	 * command-line option to modify properties after fmd_create() is done.
8500Sstevel@tonic-gate 	 */
8511414Scindi 	name = dp->d_rootdir != NULL &&
8521414Scindi 	    *dp->d_rootdir != '\0' ? dp->d_rootdir : NULL;
8531414Scindi 
8544282Seschrock 	/*
8554282Seschrock 	 * The clock must be initialized before fmd_topo_init() because
8564282Seschrock 	 * fmd_topo_update() calls fmd_time_gethrtime().
8574282Seschrock 	 */
8584282Seschrock 	dp->d_clockptr = dp->d_clockops->fto_init();
8594282Seschrock 
8603062Scindi 	fmd_topo_init();
8611414Scindi 
8621193Smws 	dp->d_xprt_ids = fmd_idspace_create("xprt_ids", 1, INT_MAX);
8631193Smws 	fmd_xprt_suspend_all();
8641193Smws 
8651193Smws 	(void) door_server_create(fmd_door);
8660Sstevel@tonic-gate 
8670Sstevel@tonic-gate 	dp->d_rmod->mod_timerids = fmd_idspace_create(dp->d_pname, 1, 16);
8680Sstevel@tonic-gate 	dp->d_timers = fmd_timerq_create();
8690Sstevel@tonic-gate 	dp->d_disp = fmd_dispq_create();
8700Sstevel@tonic-gate 	dp->d_cases = fmd_case_hash_create();
8710Sstevel@tonic-gate 
8720Sstevel@tonic-gate 	/*
8731193Smws 	 * The root module's mod_queue is created with limit zero, making it
8741193Smws 	 * act like /dev/null; anything inserted here is simply ignored.
8751193Smws 	 */
8761193Smws 	dp->d_rmod->mod_queue = fmd_eventq_create(dp->d_rmod,
8771193Smws 	    &dp->d_rmod->mod_stats->ms_evqstat, &dp->d_rmod->mod_stats_lock, 0);
8781193Smws 
8791193Smws 	/*
8800Sstevel@tonic-gate 	 * Once our subsystems that use signals have been set up, install the
8810Sstevel@tonic-gate 	 * signal handler for the fmd_thr_signal() API.  Verify that the signal
8820Sstevel@tonic-gate 	 * being used for this purpose doesn't conflict with something else.
8830Sstevel@tonic-gate 	 */
8840Sstevel@tonic-gate 	(void) fmd_conf_getprop(dp->d_conf, "client.thrsig", &dp->d_thr_sig);
8850Sstevel@tonic-gate 
8860Sstevel@tonic-gate 	if (sigaction(dp->d_thr_sig, NULL, &act) != 0) {
8870Sstevel@tonic-gate 		fmd_error(EFMD_EXIT, "invalid signal selected for "
8880Sstevel@tonic-gate 		    "client.thrsig property: %d\n", dp->d_thr_sig);
8890Sstevel@tonic-gate 	}
8900Sstevel@tonic-gate 
8910Sstevel@tonic-gate 	if (act.sa_handler != SIG_IGN && act.sa_handler != SIG_DFL) {
8920Sstevel@tonic-gate 		fmd_error(EFMD_EXIT, "signal selected for client.thrsig "
8930Sstevel@tonic-gate 		    "property is already in use: %d\n", dp->d_thr_sig);
8940Sstevel@tonic-gate 	}
8950Sstevel@tonic-gate 
8960Sstevel@tonic-gate 	act.sa_handler = fmd_signal;
8970Sstevel@tonic-gate 	act.sa_flags = 0;
8980Sstevel@tonic-gate 
8990Sstevel@tonic-gate 	(void) sigemptyset(&act.sa_mask);
9000Sstevel@tonic-gate 	(void) sigaction(dp->d_thr_sig, &act, NULL);
9010Sstevel@tonic-gate 
9020Sstevel@tonic-gate 	(void) fmd_conf_getprop(dp->d_conf, "schemedir", &name);
9030Sstevel@tonic-gate 	dp->d_schemes = fmd_scheme_hash_create(dp->d_rootdir, name);
9040Sstevel@tonic-gate 
9050Sstevel@tonic-gate 	(void) fmd_conf_getprop(dp->d_conf, "log.rsrc", &name);
9060Sstevel@tonic-gate 	dp->d_asrus = fmd_asru_hash_create(dp->d_rootdir, name);
9070Sstevel@tonic-gate 
9080Sstevel@tonic-gate 	(void) fmd_conf_getprop(dp->d_conf, "log.error", &name);
9090Sstevel@tonic-gate 	dp->d_errlog = fmd_log_open(dp->d_rootdir, name, FMD_LOG_ERROR);
9100Sstevel@tonic-gate 
9110Sstevel@tonic-gate 	(void) fmd_conf_getprop(dp->d_conf, "log.fault", &name);
9120Sstevel@tonic-gate 	dp->d_fltlog = fmd_log_open(dp->d_rootdir, name, FMD_LOG_FAULT);
9130Sstevel@tonic-gate 
91412967Sgavin.maltby@oracle.com 	(void) fmd_conf_getprop(dp->d_conf, "log.info_hival", &name);
91512967Sgavin.maltby@oracle.com 	dp->d_hvilog = fmd_log_open(dp->d_rootdir, name, FMD_LOG_INFO);
91612967Sgavin.maltby@oracle.com 
91712967Sgavin.maltby@oracle.com 	(void) fmd_conf_getprop(dp->d_conf, "log.info", &name);
91812967Sgavin.maltby@oracle.com 	dp->d_ilog = fmd_log_open(dp->d_rootdir, name, FMD_LOG_INFO);
91912967Sgavin.maltby@oracle.com 
9200Sstevel@tonic-gate 	if (dp->d_asrus == NULL || dp->d_errlog == NULL || dp->d_fltlog == NULL)
9210Sstevel@tonic-gate 		fmd_error(EFMD_EXIT, "failed to initialize log files\n");
9220Sstevel@tonic-gate 
9231193Smws 	/*
9241193Smws 	 * Before loading modules, create an empty control event which will act
9251193Smws 	 * as a global barrier for module event processing.  Each module we
9261193Smws 	 * load successfully will insert it at their head of their event queue,
9271193Smws 	 * and then pause inside of fmd_ctl_rele() after dequeuing the event.
9281193Smws 	 * This module barrier is required for two reasons:
9291193Smws 	 *
9301193Smws 	 * (a) During module loading, the restoration of case checkpoints may
9311193Smws 	 *    result in a list.* event being recreated for which the intended
9321193Smws 	 *    subscriber has not yet loaded depending on the load order. Such
9331193Smws 	 *    events could then result in spurious "no subscriber" errors.
9341193Smws 	 *
9351193Smws 	 * (b) During errlog replay, a sequence of errors from a long time ago
9361193Smws 	 *    may be replayed, and the module may attempt to install relative
9371193Smws 	 *    timers associated with one or more of these events.  If errlog
9381193Smws 	 *    replay were "racing" with active module threads, an event E1
9391193Smws 	 *    that resulted in a relative timer T at time E1 + N nsec could
9401193Smws 	 *    fire prior to an event E2 being enqueued, even if the relative
9411193Smws 	 *    time ordering was E1 < E2 < E1 + N, causing mis-diagnosis.
9421193Smws 	 */
9431193Smws 	dp->d_mod_event = e = fmd_event_create(FMD_EVT_CTL,
9441193Smws 	    FMD_HRT_NOW, NULL, fmd_ctl_init(NULL));
9451193Smws 
9461193Smws 	fmd_event_hold(e);
9471193Smws 
9481193Smws 	/*
9491193Smws 	 * Once all data structures are initialized, we load all of our modules
9501193Smws 	 * in order according to class in order to load up any subscriptions.
9511193Smws 	 * Once built-in modules are loaded, we detach from our waiting parent.
9521193Smws 	 */
9530Sstevel@tonic-gate 	dp->d_mod_hash = fmd_modhash_create();
9541193Smws 
9551193Smws 	if (fmd_builtin_loadall(dp->d_mod_hash) != 0 && !dp->d_fg)
9561193Smws 		fmd_error(EFMD_EXIT, "failed to initialize fault manager\n");
9571193Smws 
9581193Smws 	(void) fmd_conf_getprop(dp->d_conf, "self.name", &name);
9591193Smws 	dp->d_self = fmd_modhash_lookup(dp->d_mod_hash, name);
9601193Smws 
9616276Scy152378 	if (dp->d_self != NULL) {
9626276Scy152378 		if (fmd_module_dc_key2code(dp->d_self, nodc_key, code_str,
9636276Scy152378 		    sizeof (code_str)) == 0)
9646276Scy152378 			(void) fmd_conf_setprop(dp->d_conf, "nodiagcode",
9656276Scy152378 			    code_str);
9666276Scy152378 		if (fmd_module_dc_key2code(dp->d_self, repair_key, code_str,
9676276Scy152378 		    sizeof (code_str)) == 0)
9686276Scy152378 			(void) fmd_conf_setprop(dp->d_conf, "repaircode",
9696276Scy152378 			    code_str);
9707275Sstephh 		if (fmd_module_dc_key2code(dp->d_self, resolve_key, code_str,
9717275Sstephh 		    sizeof (code_str)) == 0)
9727275Sstephh 			(void) fmd_conf_setprop(dp->d_conf, "resolvecode",
9737275Sstephh 			    code_str);
9747275Sstephh 		if (fmd_module_dc_key2code(dp->d_self, update_key, code_str,
9757275Sstephh 		    sizeof (code_str)) == 0)
9767275Sstephh 			(void) fmd_conf_setprop(dp->d_conf, "updatecode",
9777275Sstephh 			    code_str);
9786276Scy152378 	}
9791193Smws 
9801193Smws 	fmd_rpc_init();
9810Sstevel@tonic-gate 	dp->d_running = 1; /* we are now officially an active fmd */
9820Sstevel@tonic-gate 
9830Sstevel@tonic-gate 	/*
9840Sstevel@tonic-gate 	 * Now that we're running, if a pipe fd was specified, write an exit
9850Sstevel@tonic-gate 	 * status to it to indicate that our parent process can safely detach.
9861193Smws 	 * Then proceed to loading the remaining non-built-in modules.
9870Sstevel@tonic-gate 	 */
9880Sstevel@tonic-gate 	if (pfd >= 0)
9890Sstevel@tonic-gate 		(void) write(pfd, &status, sizeof (status));
9900Sstevel@tonic-gate 
9911552Smws 	/*
9921552Smws 	 * Before loading all modules, repopulate the ASRU cache from its
9931552Smws 	 * persistent repository on disk.  Then during module loading, the
9941552Smws 	 * restoration of checkpoint files will reparent any active cases.
9951552Smws 	 */
9961552Smws 	fmd_asru_hash_refresh(dp->d_asrus);
9971552Smws 
9980Sstevel@tonic-gate 	(void) fmd_conf_getprop(dp->d_conf, "plugin.path", &pap);
9991429Smws 	fmd_modhash_loadall(dp->d_mod_hash, pap, &fmd_rtld_ops, ".so");
10000Sstevel@tonic-gate 
10010Sstevel@tonic-gate 	(void) fmd_conf_getprop(dp->d_conf, "agent.path", &pap);
10021429Smws 	fmd_modhash_loadall(dp->d_mod_hash, pap, &fmd_proc_ops, NULL);
10030Sstevel@tonic-gate 
1004*13036SStephen.Hanson@Sun.COM 	dp->d_loaded = 1; /* modules are now loaded */
1005*13036SStephen.Hanson@Sun.COM 
10060Sstevel@tonic-gate 	/*
10071193Smws 	 * With all modules loaded, replay fault events from the ASRU cache for
10081193Smws 	 * any ASRUs that must be retired, replay error events from the errlog
10091193Smws 	 * that did not finish processing the last time ran, and then release
10101193Smws 	 * the global module barrier by executing a final rele on d_mod_event.
10110Sstevel@tonic-gate 	 */
10121552Smws 	fmd_asru_hash_replay(dp->d_asrus);
10130Sstevel@tonic-gate 
10140Sstevel@tonic-gate 	(void) pthread_rwlock_rdlock(&dp->d_log_lock);
10150Sstevel@tonic-gate 	fmd_log_replay(dp->d_errlog, (fmd_log_f *)fmd_err_replay, dp);
10160Sstevel@tonic-gate 	fmd_log_update(dp->d_errlog);
10170Sstevel@tonic-gate 	(void) pthread_rwlock_unlock(&dp->d_log_lock);
10180Sstevel@tonic-gate 
10191193Smws 	dp->d_mod_event = NULL;
10201193Smws 	fmd_event_rele(e);
10210Sstevel@tonic-gate 
10220Sstevel@tonic-gate 	/*
10237275Sstephh 	 * Now replay list.updated and list.repaired events
10247275Sstephh 	 */
10257275Sstephh 	fmd_case_repair_replay();
10267275Sstephh 
10277275Sstephh 	/*
10280Sstevel@tonic-gate 	 * Finally, awaken any threads associated with receiving events from
10291193Smws 	 * open transports and tell them to proceed with fmd_xprt_recv().
10300Sstevel@tonic-gate 	 */
10311193Smws 	fmd_xprt_resume_all();
10321193Smws 	fmd_gc(dp, 0, 0);
10336228Sstephh 	fmd_clear_aged_rsrcs(dp, 0, 0);
10340Sstevel@tonic-gate 
10356559Sstephh 	(void) pthread_mutex_lock(&dp->d_fmd_lock);
10361193Smws 	dp->d_booted = 1;
10376559Sstephh 	(void) pthread_cond_broadcast(&dp->d_fmd_cv);
10386559Sstephh 	(void) pthread_mutex_unlock(&dp->d_fmd_lock);
10390Sstevel@tonic-gate }
10400Sstevel@tonic-gate 
10410Sstevel@tonic-gate void
fmd_help(fmd_t * dp)10420Sstevel@tonic-gate fmd_help(fmd_t *dp)
10430Sstevel@tonic-gate {
10440Sstevel@tonic-gate 	const fmd_conf_mode_t *cmp;
10450Sstevel@tonic-gate 
10460Sstevel@tonic-gate 	(void) printf("Usage: %s -o debug=mode[,mode]\n", dp->d_pname);
10470Sstevel@tonic-gate 
10480Sstevel@tonic-gate 	for (cmp = _fmd_debug_modes; cmp->cm_name != NULL; cmp++)
10490Sstevel@tonic-gate 		(void) printf("\t%s\t%s\n", cmp->cm_name, cmp->cm_desc);
10500Sstevel@tonic-gate }
1051