xref: /onnv-gate/usr/src/cmd/fm/fmd/common/fmd.c (revision 3062:46d280f5351d)
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 /*
231222Smws  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include <sys/types.h>
300Sstevel@tonic-gate #include <sys/utsname.h>
310Sstevel@tonic-gate #include <sys/param.h>
320Sstevel@tonic-gate #include <sys/systeminfo.h>
330Sstevel@tonic-gate #include <sys/fm/util.h>
340Sstevel@tonic-gate 
351414Scindi #include <smbios.h>
360Sstevel@tonic-gate #include <limits.h>
370Sstevel@tonic-gate #include <unistd.h>
380Sstevel@tonic-gate #include <signal.h>
390Sstevel@tonic-gate #include <stdlib.h>
400Sstevel@tonic-gate #include <stdio.h>
411193Smws #include <door.h>
420Sstevel@tonic-gate 
430Sstevel@tonic-gate #include <fmd_conf.h>
440Sstevel@tonic-gate #include <fmd_dispq.h>
450Sstevel@tonic-gate #include <fmd_timerq.h>
460Sstevel@tonic-gate #include <fmd_subr.h>
470Sstevel@tonic-gate #include <fmd_error.h>
480Sstevel@tonic-gate #include <fmd_module.h>
490Sstevel@tonic-gate #include <fmd_thread.h>
500Sstevel@tonic-gate #include <fmd_alloc.h>
510Sstevel@tonic-gate #include <fmd_string.h>
520Sstevel@tonic-gate #include <fmd_builtin.h>
530Sstevel@tonic-gate #include <fmd_ustat.h>
540Sstevel@tonic-gate #include <fmd_protocol.h>
550Sstevel@tonic-gate #include <fmd_scheme.h>
560Sstevel@tonic-gate #include <fmd_asru.h>
570Sstevel@tonic-gate #include <fmd_case.h>
580Sstevel@tonic-gate #include <fmd_log.h>
591193Smws #include <fmd_idspace.h>
600Sstevel@tonic-gate #include <fmd_rpc.h>
610Sstevel@tonic-gate #include <fmd_dr.h>
62*3062Scindi #include <fmd_topo.h>
631193Smws #include <fmd_xprt.h>
641193Smws #include <fmd_ctl.h>
652112Sav145390 #include <sys/openpromio.h>
662112Sav145390 #include <libdevinfo.h>
670Sstevel@tonic-gate 
680Sstevel@tonic-gate #include <fmd.h>
690Sstevel@tonic-gate 
700Sstevel@tonic-gate extern const nv_alloc_ops_t fmd_nv_alloc_ops;	/* see fmd_nv.c */
710Sstevel@tonic-gate 
721052Sdilpreet const char _fmd_version[] = "1.1";		/* daemon version string */
730Sstevel@tonic-gate static char _fmd_plat[MAXNAMELEN];		/* native platform string */
740Sstevel@tonic-gate static char _fmd_isa[MAXNAMELEN];		/* native instruction set */
750Sstevel@tonic-gate static struct utsname _fmd_uts;			/* native uname(2) info */
761414Scindi static char _fmd_csn[MAXNAMELEN];		/* chassis serial number */
771414Scindi static char _fmd_prod[MAXNAMELEN];		/* product name string */
780Sstevel@tonic-gate 
790Sstevel@tonic-gate /*
800Sstevel@tonic-gate  * Note: the configuration file path is ordered from most common to most host-
810Sstevel@tonic-gate  * specific because new conf files are merged/override previous ones.  The
820Sstevel@tonic-gate  * module paths are in the opposite order, from most specific to most common,
830Sstevel@tonic-gate  * because once a module is loaded fmd will not try to load over the same name.
840Sstevel@tonic-gate  */
850Sstevel@tonic-gate 
860Sstevel@tonic-gate static const char _fmd_conf_path[] =
870Sstevel@tonic-gate 	"%r/usr/lib/fm/fmd:"
880Sstevel@tonic-gate 	"%r/usr/platform/%m/lib/fm/fmd:"
890Sstevel@tonic-gate 	"%r/usr/platform/%i/lib/fm/fmd:"
900Sstevel@tonic-gate 	"%r/etc/fm/fmd";
910Sstevel@tonic-gate 
920Sstevel@tonic-gate static const char _fmd_agent_path[] =
930Sstevel@tonic-gate 	"%r/usr/platform/%i/lib/fm/fmd/agents:"
940Sstevel@tonic-gate 	"%r/usr/platform/%m/lib/fm/fmd/agents:"
950Sstevel@tonic-gate 	"%r/usr/lib/fm/fmd/agents";
960Sstevel@tonic-gate 
970Sstevel@tonic-gate static const char _fmd_plugin_path[] =
980Sstevel@tonic-gate 	"%r/usr/platform/%i/lib/fm/fmd/plugins:"
990Sstevel@tonic-gate 	"%r/usr/platform/%m/lib/fm/fmd/plugins:"
1000Sstevel@tonic-gate 	"%r/usr/lib/fm/fmd/plugins";
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate static const char _fmd_scheme_path[] =
1030Sstevel@tonic-gate 	"usr/lib/fm/fmd/schemes";
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate static const fmd_conf_mode_t _fmd_cerror_modes[] = {
1060Sstevel@tonic-gate 	{ "unload", "unload offending client module", FMD_CERROR_UNLOAD },
1070Sstevel@tonic-gate 	{ "stop", "stop daemon for debugger attach", FMD_CERROR_STOP },
1080Sstevel@tonic-gate 	{ "abort", "abort daemon and force core dump", FMD_CERROR_ABORT },
1090Sstevel@tonic-gate 	{ NULL, NULL, 0 }
1100Sstevel@tonic-gate };
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate static const fmd_conf_mode_t _fmd_dbout_modes[] = {
1130Sstevel@tonic-gate 	{ "stderr", "send debug messages to stderr", FMD_DBOUT_STDERR },
1140Sstevel@tonic-gate 	{ "syslog", "send debug messages to syslog", FMD_DBOUT_SYSLOG },
1150Sstevel@tonic-gate 	{ NULL, NULL, 0 }
1160Sstevel@tonic-gate };
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate static const fmd_conf_mode_t _fmd_debug_modes[] = {
1190Sstevel@tonic-gate 	{ "help", "display debugging modes and exit", FMD_DBG_HELP },
1200Sstevel@tonic-gate 	{ "mod", "debug module load/unload/locking", FMD_DBG_MOD },
1210Sstevel@tonic-gate 	{ "disp", "debug dispatch queue processing", FMD_DBG_DISP },
1220Sstevel@tonic-gate 	{ "xprt", "debug transport-specific routines", FMD_DBG_XPRT },
1230Sstevel@tonic-gate 	{ "evt", "debug event subsystem routines", FMD_DBG_EVT },
1240Sstevel@tonic-gate 	{ "log", "debug log subsystem routines", FMD_DBG_LOG },
1250Sstevel@tonic-gate 	{ "tmr", "debug timer subsystem routines", FMD_DBG_TMR },
1260Sstevel@tonic-gate 	{ "fmri", "debug fmri subsystem routines", FMD_DBG_FMRI },
1270Sstevel@tonic-gate 	{ "asru", "debug asru subsystem routines", FMD_DBG_ASRU },
1280Sstevel@tonic-gate 	{ "case", "debug case subsystem routines", FMD_DBG_CASE },
1290Sstevel@tonic-gate 	{ "ckpt", "debug checkpoint routines", FMD_DBG_CKPT },
1300Sstevel@tonic-gate 	{ "rpc", "debug rpc service routines", FMD_DBG_RPC },
1311193Smws 	{ "trace", "display matching trace calls", FMD_DBG_TRACE },
1320Sstevel@tonic-gate 	{ "all", "enable all available debug modes", FMD_DBG_ALL },
1330Sstevel@tonic-gate 	{ NULL, NULL, 0 }
1340Sstevel@tonic-gate };
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate static int
1370Sstevel@tonic-gate fmd_cerror_set(fmd_conf_param_t *pp, const char *value)
1380Sstevel@tonic-gate {
1390Sstevel@tonic-gate 	return (fmd_conf_mode_set(_fmd_cerror_modes, pp, value));
1400Sstevel@tonic-gate }
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate static int
1430Sstevel@tonic-gate fmd_dbout_set(fmd_conf_param_t *pp, const char *value)
1440Sstevel@tonic-gate {
1450Sstevel@tonic-gate 	return (fmd_conf_mode_set(_fmd_dbout_modes, pp, value));
1460Sstevel@tonic-gate }
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate static int
1490Sstevel@tonic-gate fmd_debug_set(fmd_conf_param_t *pp, const char *value)
1500Sstevel@tonic-gate {
1510Sstevel@tonic-gate 	int err = fmd_conf_mode_set(_fmd_debug_modes, pp, value);
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate 	if (err == 0)
1540Sstevel@tonic-gate 		fmd.d_fmd_debug = pp->cp_value.cpv_num;
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate 	return (err);
1570Sstevel@tonic-gate }
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate static int
1600Sstevel@tonic-gate fmd_trmode_set(fmd_conf_param_t *pp, const char *value)
1610Sstevel@tonic-gate {
1620Sstevel@tonic-gate 	fmd_tracebuf_f *func;
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate 	if (strcasecmp(value, "none") == 0)
1650Sstevel@tonic-gate 		func = fmd_trace_none;
1660Sstevel@tonic-gate 	else if (strcasecmp(value, "lite") == 0)
1670Sstevel@tonic-gate 		func = fmd_trace_lite;
1680Sstevel@tonic-gate 	else if (strcasecmp(value, "full") == 0)
1690Sstevel@tonic-gate 		func = fmd_trace_full;
1700Sstevel@tonic-gate 	else
1710Sstevel@tonic-gate 		return (fmd_set_errno(EFMD_CONF_INVAL));
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate 	fmd.d_thr_trace = (void (*)())func;
1740Sstevel@tonic-gate 	pp->cp_value.cpv_ptr = (void *)func;
1750Sstevel@tonic-gate 	return (0);
1760Sstevel@tonic-gate }
1770Sstevel@tonic-gate 
1780Sstevel@tonic-gate static void
1790Sstevel@tonic-gate fmd_trmode_get(const fmd_conf_param_t *pp, void *ptr)
1800Sstevel@tonic-gate {
1810Sstevel@tonic-gate 	*((void **)ptr) = pp->cp_value.cpv_ptr;
1820Sstevel@tonic-gate }
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate static int
1850Sstevel@tonic-gate fmd_clkmode_set(fmd_conf_param_t *pp, const char *value)
1860Sstevel@tonic-gate {
1870Sstevel@tonic-gate 	const fmd_timeops_t *ops;
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate 	if (strcasecmp(value, "native") == 0)
1900Sstevel@tonic-gate 		ops = &fmd_timeops_native;
1910Sstevel@tonic-gate 	else if (strcasecmp(value, "simulated") == 0)
1920Sstevel@tonic-gate 		ops = &fmd_timeops_simulated;
1930Sstevel@tonic-gate 	else
1940Sstevel@tonic-gate 		return (fmd_set_errno(EFMD_CONF_INVAL));
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate 	fmd.d_clockops = ops;
1970Sstevel@tonic-gate 	pp->cp_value.cpv_ptr = (void *)ops;
1980Sstevel@tonic-gate 	return (0);
1990Sstevel@tonic-gate }
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate static void
2020Sstevel@tonic-gate fmd_clkmode_get(const fmd_conf_param_t *pp, void *ptr)
2030Sstevel@tonic-gate {
2040Sstevel@tonic-gate 	*((void **)ptr) = pp->cp_value.cpv_ptr;
2050Sstevel@tonic-gate }
2060Sstevel@tonic-gate 
2070Sstevel@tonic-gate static const fmd_conf_ops_t fmd_cerror_ops = {
2080Sstevel@tonic-gate 	fmd_cerror_set, fmd_conf_mode_get, fmd_conf_notsup, fmd_conf_nop
2090Sstevel@tonic-gate };
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate static const fmd_conf_ops_t fmd_dbout_ops = {
2120Sstevel@tonic-gate 	fmd_dbout_set, fmd_conf_mode_get, fmd_conf_notsup, fmd_conf_nop
2130Sstevel@tonic-gate };
2140Sstevel@tonic-gate 
2150Sstevel@tonic-gate static const fmd_conf_ops_t fmd_debug_ops = {
2160Sstevel@tonic-gate 	fmd_debug_set, fmd_conf_mode_get, fmd_conf_notsup, fmd_conf_nop
2170Sstevel@tonic-gate };
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate static const fmd_conf_ops_t fmd_trmode_ops = {
2200Sstevel@tonic-gate 	fmd_trmode_set, fmd_trmode_get, fmd_conf_notsup, fmd_conf_nop
2210Sstevel@tonic-gate };
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate static const fmd_conf_ops_t fmd_clkmode_ops = {
2240Sstevel@tonic-gate 	fmd_clkmode_set, fmd_clkmode_get, fmd_conf_notsup, fmd_conf_nop
2250Sstevel@tonic-gate };
2260Sstevel@tonic-gate 
2270Sstevel@tonic-gate static const fmd_conf_formal_t _fmd_conf[] = {
2280Sstevel@tonic-gate { "agent.path", &fmd_conf_path, _fmd_agent_path }, /* path for agents */
2290Sstevel@tonic-gate { "alloc_msecs", &fmd_conf_uint32, "10" },	/* msecs before alloc retry */
2300Sstevel@tonic-gate { "alloc_tries", &fmd_conf_uint32, "3" },	/* max # of alloc retries */
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 */
2330Sstevel@tonic-gate { "ckpt.dirmode", &fmd_conf_int32, "0700" },	/* ckpt directory perm mode */
2340Sstevel@tonic-gate { "ckpt.mode", &fmd_conf_int32, "0400" },	/* 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 */
2410Sstevel@tonic-gate { "client.error", &fmd_cerror_ops, "unload" },	/* client error policy */
2420Sstevel@tonic-gate { "client.memlim", &fmd_conf_size, "10m" },	/* client allocation limit */
2430Sstevel@tonic-gate { "client.evqlim", &fmd_conf_uint32, "256" },	/* client event queue limit */
2440Sstevel@tonic-gate { "client.thrlim", &fmd_conf_uint32, "8" },	/* client aux thread limit */
2450Sstevel@tonic-gate { "client.thrsig", &fmd_conf_signal, "SIGUSR1" }, /* fmd_thr_signal() value */
2460Sstevel@tonic-gate { "client.tmrlim", &fmd_conf_uint32, "1024" },	/* client pending timer limit */
2471193Smws { "client.xprtlim", &fmd_conf_uint32, "256" },	/* client transport limit */
2481193Smws { "client.xprtlog", &fmd_conf_bool, NULL },	/* client transport logging? */
2492808Sav145390 { "client.xprtqlim", &fmd_conf_uint32, "1024" }, /* client transport queue li */
2500Sstevel@tonic-gate { "clock", &fmd_clkmode_ops, "native" },	/* clock operation mode */
2510Sstevel@tonic-gate { "conf_path", &fmd_conf_path, _fmd_conf_path }, /* root config file path */
2520Sstevel@tonic-gate { "conf_file", &fmd_conf_string, "fmd.conf" },	/* root config file name */
2530Sstevel@tonic-gate { "core", &fmd_conf_bool, "false" },		/* force core dump on quit */
2540Sstevel@tonic-gate { "dbout", &fmd_dbout_ops, NULL },		/* daemon debug output sinks */
2550Sstevel@tonic-gate { "debug", &fmd_debug_ops, NULL },		/* daemon debugging flags */
2560Sstevel@tonic-gate { "dictdir", &fmd_conf_string, "usr/lib/fm/dict" }, /* default diagcode dir */
2570Sstevel@tonic-gate { "domain", &fmd_conf_string, NULL },		/* domain id for de auth */
2580Sstevel@tonic-gate { "fg", &fmd_conf_bool, "false" },		/* run daemon in foreground */
2590Sstevel@tonic-gate { "gc_interval", &fmd_conf_time, "1d" },	/* garbage collection intvl */
2600Sstevel@tonic-gate { "ids.avg", &fmd_conf_uint32, "4" },		/* desired idspace chain len */
2610Sstevel@tonic-gate { "ids.max", &fmd_conf_uint32, "1024" },	/* maximum idspace buckets */
2620Sstevel@tonic-gate { "isaname", &fmd_conf_string, _fmd_isa },	/* instruction set (uname -p) */
2630Sstevel@tonic-gate { "log.creator", &fmd_conf_string, "fmd" },	/* exacct log creator string */
2640Sstevel@tonic-gate { "log.error", &fmd_conf_string, "var/fm/fmd/errlog" }, /* error log path */
2650Sstevel@tonic-gate { "log.fault", &fmd_conf_string, "var/fm/fmd/fltlog" }, /* fault log path */
2660Sstevel@tonic-gate { "log.minfree", &fmd_conf_size, "2m" },	/* min log fsys free space */
2671193Smws { "log.rsrc", &fmd_conf_string, "var/fm/fmd/rsrc" }, /* asru log dir path */
2680Sstevel@tonic-gate { "log.tryrotate", &fmd_conf_uint32, "10" },	/* max log rotation attempts */
2690Sstevel@tonic-gate { "log.waitrotate", &fmd_conf_time, "200ms" },	/* log rotation retry delay */
2701193Smws { "log.xprt", &fmd_conf_string, "var/fm/fmd/xprt" }, /* transport log dir */
2710Sstevel@tonic-gate { "machine", &fmd_conf_string, _fmd_uts.machine }, /* machine name (uname -m) */
2720Sstevel@tonic-gate { "nodiagcode", &fmd_conf_string, "-" },	/* diagcode to use if error */
2730Sstevel@tonic-gate { "osrelease", &fmd_conf_string, _fmd_uts.release }, /* release (uname -r) */
2740Sstevel@tonic-gate { "osversion", &fmd_conf_string, _fmd_uts.version }, /* version (uname -v) */
2750Sstevel@tonic-gate { "platform", &fmd_conf_string, _fmd_plat },	/* platform string (uname -i) */
2760Sstevel@tonic-gate { "plugin.close", &fmd_conf_bool, "true" },	/* dlclose plugins on fini */
2770Sstevel@tonic-gate { "plugin.path", &fmd_conf_path, _fmd_plugin_path }, /* path for plugin mods */
2781414Scindi { "product", &fmd_conf_string, _fmd_prod },	/* product name string */
2790Sstevel@tonic-gate { "rootdir", &fmd_conf_string, "" },		/* root directory for paths */
2800Sstevel@tonic-gate { "rpc.adm.path", &fmd_conf_string, NULL },	/* FMD_ADM rendezvous file */
2810Sstevel@tonic-gate { "rpc.adm.prog", &fmd_conf_uint32, "100169" },	/* FMD_ADM rpc program num */
2820Sstevel@tonic-gate { "rpc.api.path", &fmd_conf_string, NULL },	/* FMD_API rendezvous file */
2830Sstevel@tonic-gate { "rpc.api.prog", &fmd_conf_uint32, "100170" },	/* FMD_API rpc program num */
2840Sstevel@tonic-gate { "rpc.rcvsize", &fmd_conf_size, "128k" },	/* rpc receive buffer size */
2850Sstevel@tonic-gate { "rpc.sndsize", &fmd_conf_size, "128k" },	/* rpc send buffer size */
2860Sstevel@tonic-gate { "rsrc.age", &fmd_conf_time, "30d" },		/* max age of old rsrc log */
2870Sstevel@tonic-gate { "rsrc.zero", &fmd_conf_bool, "false" },	/* zero rsrc cache on start? */
2880Sstevel@tonic-gate { "schemedir", &fmd_conf_string, _fmd_scheme_path }, /* path for scheme mods */
2890Sstevel@tonic-gate { "self.name", &fmd_conf_string, "fmd-self-diagnosis" }, /* self-diag module */
2900Sstevel@tonic-gate { "self.dict", &fmd_conf_list, "FMD.dict" },	/* self-diag dictionary list */
2910Sstevel@tonic-gate { "server", &fmd_conf_string, _fmd_uts.nodename }, /* server id for de auth */
2920Sstevel@tonic-gate { "strbuckets", &fmd_conf_uint32, "211" },	/* size of string hashes */
2930Sstevel@tonic-gate #ifdef DEBUG
2940Sstevel@tonic-gate { "trace.mode", &fmd_trmode_ops, "full" },	/* trace mode: none/lite/full */
2950Sstevel@tonic-gate #else
2960Sstevel@tonic-gate { "trace.mode", &fmd_trmode_ops, "lite" },	/* trace mode: none/lite/full */
2970Sstevel@tonic-gate #endif
2980Sstevel@tonic-gate { "trace.recs", &fmd_conf_uint32, "128" },	/* trace records per thread */
2990Sstevel@tonic-gate { "trace.frames", &fmd_conf_uint32, "16" },	/* max trace rec stack frames */
3000Sstevel@tonic-gate { "uuidlen", &fmd_conf_uint32, "36" },		/* UUID ASCII string length */
3011193Smws { "xprt.ttl", &fmd_conf_uint8, "1" },		/* default event time-to-live */
3020Sstevel@tonic-gate };
3030Sstevel@tonic-gate 
3040Sstevel@tonic-gate /*
3050Sstevel@tonic-gate  * Statistics maintained by fmd itself on behalf of various global subsystems.
3060Sstevel@tonic-gate  * NOTE: FMD_TYPE_STRING statistics should not be used here.  If they are
3070Sstevel@tonic-gate  * required in the future, the FMD_ADM_MODGSTAT service routine must change.
3080Sstevel@tonic-gate  */
3090Sstevel@tonic-gate static fmd_statistics_t _fmd_stats = {
3100Sstevel@tonic-gate { "errlog.replayed", FMD_TYPE_UINT64, "total events replayed from errlog" },
3110Sstevel@tonic-gate { "errlog.partials", FMD_TYPE_UINT64, "events partially committed in errlog" },
3120Sstevel@tonic-gate { "errlog.enospc", FMD_TYPE_UINT64, "events not appended to errlog (ENOSPC)" },
3130Sstevel@tonic-gate { "fltlog.enospc", FMD_TYPE_UINT64, "events not appended to fltlog (ENOSPC)" },
3140Sstevel@tonic-gate { "log.enospc", FMD_TYPE_UINT64, "events not appended to other logs (ENOSPC)" },
3150Sstevel@tonic-gate { "dr.gen", FMD_TYPE_UINT64, "dynamic reconfiguration generation" },
316*3062Scindi { "topo.gen", FMD_TYPE_UINT64, "topology snapshot generation" },
317*3062Scindi { "topo.drgen", FMD_TYPE_UINT64, "current topology DR generation number" },
3180Sstevel@tonic-gate };
3190Sstevel@tonic-gate 
3200Sstevel@tonic-gate void
3210Sstevel@tonic-gate fmd_create(fmd_t *dp, const char *arg0, const char *root, const char *conf)
3220Sstevel@tonic-gate {
3230Sstevel@tonic-gate 	fmd_conf_path_t *pap;
3240Sstevel@tonic-gate 	char file[PATH_MAX];
3250Sstevel@tonic-gate 	const char *name;
3260Sstevel@tonic-gate 	fmd_stat_t *sp;
3270Sstevel@tonic-gate 	int i;
3280Sstevel@tonic-gate 
3291414Scindi 	smbios_hdl_t *shp;
3301414Scindi 	smbios_system_t s1;
3311414Scindi 	smbios_info_t s2;
3321414Scindi 	id_t id;
3331414Scindi 
3342112Sav145390 	di_prom_handle_t promh = DI_PROM_HANDLE_NIL;
3352112Sav145390 	di_node_t rooth = DI_NODE_NIL;
3362112Sav145390 	char *bufp;
3372112Sav145390 
3380Sstevel@tonic-gate 	(void) sysinfo(SI_PLATFORM, _fmd_plat, sizeof (_fmd_plat));
3390Sstevel@tonic-gate 	(void) sysinfo(SI_ARCHITECTURE, _fmd_isa, sizeof (_fmd_isa));
3400Sstevel@tonic-gate 	(void) uname(&_fmd_uts);
3410Sstevel@tonic-gate 
3421414Scindi 	if ((shp = smbios_open(NULL, SMB_VERSION, 0, NULL)) != NULL) {
3431414Scindi 		if ((id = smbios_info_system(shp, &s1)) != SMB_ERR &&
3441414Scindi 		    smbios_info_common(shp, id, &s2) != SMB_ERR) {
3451414Scindi 			(void) strlcpy(_fmd_prod, s2.smbi_product, MAXNAMELEN);
3461414Scindi 			(void) strlcpy(_fmd_csn, s2.smbi_serial, MAXNAMELEN);
3471414Scindi 		}
3481414Scindi 		smbios_close(shp);
3492338Shueston 	} else if ((rooth = di_init("/", DINFOPROP)) != DI_NODE_NIL &&
3502112Sav145390 	    (promh = di_prom_init()) != DI_PROM_HANDLE_NIL) {
3512112Sav145390 		if (di_prom_prop_lookup_bytes(promh, rooth, "chassis-sn",
3522112Sav145390 		    (unsigned char **)&bufp) != -1) {
3532112Sav145390 			(void) strlcpy(_fmd_csn, bufp, MAXNAMELEN);
3542112Sav145390 		}
3551414Scindi 	}
3561414Scindi 
3572112Sav145390 	if (promh != DI_PROM_HANDLE_NIL)
3582112Sav145390 		di_prom_fini(promh);
3592112Sav145390 	if (rooth != DI_NODE_NIL)
3602112Sav145390 		di_fini(rooth);
3612112Sav145390 
3620Sstevel@tonic-gate 	bzero(dp, sizeof (fmd_t));
3630Sstevel@tonic-gate 
3640Sstevel@tonic-gate 	dp->d_version = _fmd_version;
3650Sstevel@tonic-gate 	dp->d_pname = fmd_strbasename(arg0);
3660Sstevel@tonic-gate 	dp->d_pid = getpid();
3670Sstevel@tonic-gate 
3680Sstevel@tonic-gate 	if (pthread_key_create(&dp->d_key, NULL) != 0)
3690Sstevel@tonic-gate 		fmd_error(EFMD_EXIT, "failed to create pthread key");
3700Sstevel@tonic-gate 
3710Sstevel@tonic-gate 	(void) pthread_mutex_init(&dp->d_xprt_lock, NULL);
3720Sstevel@tonic-gate 	(void) pthread_mutex_init(&dp->d_err_lock, NULL);
3730Sstevel@tonic-gate 	(void) pthread_mutex_init(&dp->d_thr_lock, NULL);
3740Sstevel@tonic-gate 	(void) pthread_mutex_init(&dp->d_mod_lock, NULL);
3750Sstevel@tonic-gate 	(void) pthread_mutex_init(&dp->d_stats_lock, NULL);
376*3062Scindi 	(void) pthread_mutex_init(&dp->d_topo_lock, NULL);
3770Sstevel@tonic-gate 	(void) pthread_rwlock_init(&dp->d_log_lock, NULL);
3780Sstevel@tonic-gate 
3790Sstevel@tonic-gate 	/*
3800Sstevel@tonic-gate 	 * A small number of properties must be set manually before we open
3810Sstevel@tonic-gate 	 * the root configuration file.  These include any settings for our
3820Sstevel@tonic-gate 	 * memory allocator and path expansion token values, because these
3830Sstevel@tonic-gate 	 * values are needed by the routines in fmd_conf.c itself.  After
3840Sstevel@tonic-gate 	 * the root configuration file is processed, we reset these properties
3850Sstevel@tonic-gate 	 * based upon the latest values from the configuration file.
3860Sstevel@tonic-gate 	 */
3870Sstevel@tonic-gate 	dp->d_alloc_msecs = 10;
3880Sstevel@tonic-gate 	dp->d_alloc_tries = 3;
3890Sstevel@tonic-gate 	dp->d_str_buckets = 211;
3900Sstevel@tonic-gate 
3910Sstevel@tonic-gate 	dp->d_rootdir = root ? root : "";
3920Sstevel@tonic-gate 	dp->d_platform = _fmd_plat;
3930Sstevel@tonic-gate 	dp->d_machine = _fmd_uts.machine;
3940Sstevel@tonic-gate 	dp->d_isaname = _fmd_isa;
3950Sstevel@tonic-gate 
3961193Smws 	dp->d_conf = fmd_conf_open(conf, sizeof (_fmd_conf) /
3971193Smws 	    sizeof (_fmd_conf[0]), _fmd_conf, FMD_CONF_DEFER);
3980Sstevel@tonic-gate 
3990Sstevel@tonic-gate 	if (dp->d_conf == NULL) {
4000Sstevel@tonic-gate 		fmd_error(EFMD_EXIT,
4010Sstevel@tonic-gate 		    "failed to load required configuration properties\n");
4020Sstevel@tonic-gate 	}
4030Sstevel@tonic-gate 
4040Sstevel@tonic-gate 	(void) fmd_conf_getprop(dp->d_conf, "alloc.msecs", &dp->d_alloc_msecs);
4050Sstevel@tonic-gate 	(void) fmd_conf_getprop(dp->d_conf, "alloc.tries", &dp->d_alloc_tries);
4060Sstevel@tonic-gate 	(void) fmd_conf_getprop(dp->d_conf, "strbuckets", &dp->d_str_buckets);
4070Sstevel@tonic-gate 
4080Sstevel@tonic-gate 	(void) fmd_conf_getprop(dp->d_conf, "platform", &dp->d_platform);
4090Sstevel@tonic-gate 	(void) fmd_conf_getprop(dp->d_conf, "machine", &dp->d_machine);
4100Sstevel@tonic-gate 	(void) fmd_conf_getprop(dp->d_conf, "isaname", &dp->d_isaname);
4110Sstevel@tonic-gate 
4120Sstevel@tonic-gate 	/*
4130Sstevel@tonic-gate 	 * Manually specified rootdirs override config files, so only update
4140Sstevel@tonic-gate 	 * d_rootdir based on the config files we parsed if no 'root' was set.
4150Sstevel@tonic-gate 	 */
4160Sstevel@tonic-gate 	if (root == NULL)
4170Sstevel@tonic-gate 		(void) fmd_conf_getprop(dp->d_conf, "rootdir", &dp->d_rootdir);
4180Sstevel@tonic-gate 	else
4190Sstevel@tonic-gate 		(void) fmd_conf_setprop(dp->d_conf, "rootdir", dp->d_rootdir);
4200Sstevel@tonic-gate 
4210Sstevel@tonic-gate 	/*
4220Sstevel@tonic-gate 	 * Once the base conf file properties are loaded, lookup the values
4230Sstevel@tonic-gate 	 * of $conf_path and $conf_file and merge in any other conf files.
4240Sstevel@tonic-gate 	 */
4250Sstevel@tonic-gate 	(void) fmd_conf_getprop(dp->d_conf, "conf_path", &pap);
4260Sstevel@tonic-gate 	(void) fmd_conf_getprop(dp->d_conf, "conf_file", &name);
4270Sstevel@tonic-gate 
4280Sstevel@tonic-gate 	for (i = 0; i < pap->cpa_argc; i++) {
4290Sstevel@tonic-gate 		(void) snprintf(file, sizeof (file),
4300Sstevel@tonic-gate 		    "%s/%s", pap->cpa_argv[i], name);
4310Sstevel@tonic-gate 		if (access(file, F_OK) == 0)
4320Sstevel@tonic-gate 			fmd_conf_merge(dp->d_conf, file);
4330Sstevel@tonic-gate 	}
4340Sstevel@tonic-gate 
4350Sstevel@tonic-gate 	/*
4360Sstevel@tonic-gate 	 * Update the value of fmd.d_fg based on "fg".  We cache this property
4370Sstevel@tonic-gate 	 * because it must be accessed deep within fmd at fmd_verror() time.
4381193Smws 	 * Update any other properties that must be cached for performance.
4390Sstevel@tonic-gate 	 */
4400Sstevel@tonic-gate 	(void) fmd_conf_getprop(fmd.d_conf, "fg", &fmd.d_fg);
4411193Smws 	(void) fmd_conf_getprop(fmd.d_conf, "xprt.ttl", &fmd.d_xprt_ttl);
4420Sstevel@tonic-gate 
4430Sstevel@tonic-gate 	/*
4440Sstevel@tonic-gate 	 * Initialize our custom libnvpair allocator and create an nvlist for
4450Sstevel@tonic-gate 	 * authority elements corresponding to this instance of the daemon.
4460Sstevel@tonic-gate 	 */
4470Sstevel@tonic-gate 	(void) nv_alloc_init(&dp->d_nva, &fmd_nv_alloc_ops);
4480Sstevel@tonic-gate 	dp->d_auth = fmd_protocol_authority();
4490Sstevel@tonic-gate 
4500Sstevel@tonic-gate 	/*
4510Sstevel@tonic-gate 	 * The fmd_module_t for the root module must be created manually.  Most
4520Sstevel@tonic-gate 	 * of it remains unused and zero, except for the few things we fill in.
4530Sstevel@tonic-gate 	 */
4540Sstevel@tonic-gate 	dp->d_rmod = fmd_zalloc(sizeof (fmd_module_t), FMD_SLEEP);
4550Sstevel@tonic-gate 	dp->d_rmod->mod_name = fmd_strdup(dp->d_pname, FMD_SLEEP);
4561193Smws 	dp->d_rmod->mod_fmri = fmd_protocol_fmri_module(dp->d_rmod);
4571193Smws 
4580Sstevel@tonic-gate 	fmd_list_append(&dp->d_mod_list, dp->d_rmod);
4591193Smws 	fmd_module_hold(dp->d_rmod);
4600Sstevel@tonic-gate 
4610Sstevel@tonic-gate 	(void) pthread_mutex_init(&dp->d_rmod->mod_lock, NULL);
4620Sstevel@tonic-gate 	(void) pthread_cond_init(&dp->d_rmod->mod_cv, NULL);
4631193Smws 	(void) pthread_mutex_init(&dp->d_rmod->mod_stats_lock, NULL);
4640Sstevel@tonic-gate 
4650Sstevel@tonic-gate 	dp->d_rmod->mod_thread = fmd_thread_xcreate(dp->d_rmod, pthread_self());
4661193Smws 	dp->d_rmod->mod_stats = fmd_zalloc(sizeof (fmd_modstat_t), FMD_SLEEP);
4670Sstevel@tonic-gate 	dp->d_rmod->mod_ustat = fmd_ustat_create();
4680Sstevel@tonic-gate 
4690Sstevel@tonic-gate 	if (pthread_setspecific(dp->d_key, dp->d_rmod->mod_thread) != 0)
4700Sstevel@tonic-gate 		fmd_error(EFMD_EXIT, "failed to attach main thread key");
4710Sstevel@tonic-gate 
4720Sstevel@tonic-gate 	if ((dp->d_stats = (fmd_statistics_t *)fmd_ustat_insert(
4730Sstevel@tonic-gate 	    dp->d_rmod->mod_ustat, FMD_USTAT_NOALLOC, sizeof (_fmd_stats) /
4740Sstevel@tonic-gate 	    sizeof (fmd_stat_t), (fmd_stat_t *)&_fmd_stats, NULL)) == NULL)
4750Sstevel@tonic-gate 		fmd_error(EFMD_EXIT, "failed to initialize statistics");
4760Sstevel@tonic-gate 
4771193Smws 	(void) pthread_mutex_lock(&dp->d_rmod->mod_lock);
4781193Smws 	dp->d_rmod->mod_flags |= FMD_MOD_INIT;
4791193Smws 	(void) pthread_mutex_unlock(&dp->d_rmod->mod_lock);
4801193Smws 
4810Sstevel@tonic-gate 	/*
4820Sstevel@tonic-gate 	 * In addition to inserting the _fmd_stats collection of program-wide
4830Sstevel@tonic-gate 	 * statistics, we also insert a statistic named after each of our
4840Sstevel@tonic-gate 	 * errors and update these counts in fmd_verror() (see fmd_subr.c).
4850Sstevel@tonic-gate 	 */
4860Sstevel@tonic-gate 	dp->d_errstats = sp = fmd_zalloc(sizeof (fmd_stat_t) *
4870Sstevel@tonic-gate 	    (EFMD_END - EFMD_UNKNOWN), FMD_SLEEP);
4880Sstevel@tonic-gate 
4890Sstevel@tonic-gate 	for (i = 0; i < EFMD_END - EFMD_UNKNOWN; i++, sp++) {
4900Sstevel@tonic-gate 		(void) snprintf(sp->fmds_name, sizeof (sp->fmds_name), "err.%s",
4910Sstevel@tonic-gate 		    strrchr(fmd_errclass(EFMD_UNKNOWN + i), '.') + 1);
4920Sstevel@tonic-gate 		sp->fmds_type = FMD_TYPE_UINT64;
4930Sstevel@tonic-gate 	}
4940Sstevel@tonic-gate 
4950Sstevel@tonic-gate 	(void) fmd_ustat_insert(dp->d_rmod->mod_ustat, FMD_USTAT_NOALLOC,
4960Sstevel@tonic-gate 	    EFMD_END - EFMD_UNKNOWN, dp->d_errstats, NULL);
4970Sstevel@tonic-gate }
4980Sstevel@tonic-gate 
4990Sstevel@tonic-gate void
5000Sstevel@tonic-gate fmd_destroy(fmd_t *dp)
5010Sstevel@tonic-gate {
5020Sstevel@tonic-gate 	fmd_module_t *mp;
5031193Smws 	fmd_case_t *cp;
5040Sstevel@tonic-gate 	int core;
5050Sstevel@tonic-gate 
5060Sstevel@tonic-gate 	(void) fmd_conf_getprop(fmd.d_conf, "core", &core);
5070Sstevel@tonic-gate 
5080Sstevel@tonic-gate 	fmd_rpc_fini();
5090Sstevel@tonic-gate 	fmd_dr_fini();
5101222Smws 
5111222Smws 	if (dp->d_xprt_ids != NULL)
5121222Smws 		fmd_xprt_suspend_all();
5130Sstevel@tonic-gate 
5140Sstevel@tonic-gate 	/*
5150Sstevel@tonic-gate 	 * Unload the self-diagnosis module first.  This ensures that it does
5160Sstevel@tonic-gate 	 * not get confused as we start unloading other modules, etc.  We must
5170Sstevel@tonic-gate 	 * hold the dispq lock as a writer while doing so since it uses d_self.
5180Sstevel@tonic-gate 	 */
5190Sstevel@tonic-gate 	if (dp->d_self != NULL) {
5202438Scindi 		fmd_module_t *self;
5212438Scindi 
5220Sstevel@tonic-gate 		(void) pthread_rwlock_wrlock(&dp->d_disp->dq_lock);
5232438Scindi 		self = dp->d_self;
5240Sstevel@tonic-gate 		dp->d_self = NULL;
5250Sstevel@tonic-gate 		(void) pthread_rwlock_unlock(&dp->d_disp->dq_lock);
5262438Scindi 
5272438Scindi 		fmd_module_unload(self);
5282438Scindi 		fmd_module_rele(self);
5290Sstevel@tonic-gate 	}
5300Sstevel@tonic-gate 
5310Sstevel@tonic-gate 	/*
5320Sstevel@tonic-gate 	 * Unload modules in reverse order *except* for the root module, which
5330Sstevel@tonic-gate 	 * is first in the list.  This allows it to keep its thread and trace.
5340Sstevel@tonic-gate 	 */
5350Sstevel@tonic-gate 	for (mp = fmd_list_prev(&dp->d_mod_list); mp != dp->d_rmod; ) {
5360Sstevel@tonic-gate 		fmd_module_unload(mp);
5370Sstevel@tonic-gate 		mp = fmd_list_prev(mp);
5380Sstevel@tonic-gate 	}
5390Sstevel@tonic-gate 
5400Sstevel@tonic-gate 	if (dp->d_mod_hash != NULL) {
5410Sstevel@tonic-gate 		fmd_modhash_destroy(dp->d_mod_hash);
5420Sstevel@tonic-gate 		dp->d_mod_hash = NULL;
5430Sstevel@tonic-gate 	}
5440Sstevel@tonic-gate 
5450Sstevel@tonic-gate 	/*
5460Sstevel@tonic-gate 	 * Close both log files now that modules are no longer active.  We must
5470Sstevel@tonic-gate 	 * set these pointers to NULL in case any subsequent errors occur.
5480Sstevel@tonic-gate 	 */
5490Sstevel@tonic-gate 	if (dp->d_errlog != NULL) {
5500Sstevel@tonic-gate 		fmd_log_rele(dp->d_errlog);
5510Sstevel@tonic-gate 		dp->d_errlog = NULL;
5520Sstevel@tonic-gate 	}
5530Sstevel@tonic-gate 
5540Sstevel@tonic-gate 	if (dp->d_fltlog != NULL) {
5550Sstevel@tonic-gate 		fmd_log_rele(dp->d_fltlog);
5560Sstevel@tonic-gate 		dp->d_fltlog = NULL;
5570Sstevel@tonic-gate 	}
5580Sstevel@tonic-gate 
5590Sstevel@tonic-gate 	/*
5601193Smws 	 * Now destroy the resource cache: each ASRU contains a case reference,
5611193Smws 	 * which may in turn contain a pointer to a referenced owning module.
5621193Smws 	 */
5631193Smws 	if (dp->d_asrus != NULL) {
5641193Smws 		fmd_asru_hash_destroy(dp->d_asrus);
5651193Smws 		dp->d_asrus = NULL;
5661193Smws 	}
5671193Smws 
5681193Smws 	/*
5690Sstevel@tonic-gate 	 * Now that all data structures that refer to modules are torn down,
5700Sstevel@tonic-gate 	 * no modules should be remaining on the module list except for d_rmod.
5710Sstevel@tonic-gate 	 * If we trip one of these assertions, we're missing a rele somewhere.
5720Sstevel@tonic-gate 	 */
5730Sstevel@tonic-gate 	ASSERT(fmd_list_prev(&dp->d_mod_list) == dp->d_rmod);
5740Sstevel@tonic-gate 	ASSERT(fmd_list_next(&dp->d_mod_list) == dp->d_rmod);
5750Sstevel@tonic-gate 
5760Sstevel@tonic-gate 	/*
5770Sstevel@tonic-gate 	 * Now destroy the root module.  We clear its thread key first so any
5780Sstevel@tonic-gate 	 * calls to fmd_trace() inside of the module code will be ignored.
5790Sstevel@tonic-gate 	 */
5800Sstevel@tonic-gate 	(void) pthread_setspecific(dp->d_key, NULL);
5811193Smws 	fmd_module_lock(dp->d_rmod);
5821193Smws 
5831193Smws 	while ((cp = fmd_list_next(&dp->d_rmod->mod_cases)) != NULL)
5841193Smws 		fmd_case_discard(cp);
5851193Smws 
5861193Smws 	fmd_module_unlock(dp->d_rmod);
5871193Smws 	fmd_free(dp->d_rmod->mod_stats, sizeof (fmd_modstat_t));
5881193Smws 	dp->d_rmod->mod_stats = NULL;
5890Sstevel@tonic-gate 
5901193Smws 	(void) pthread_mutex_lock(&dp->d_rmod->mod_lock);
5911193Smws 	dp->d_rmod->mod_flags |= FMD_MOD_FINI;
5921193Smws 	(void) pthread_mutex_unlock(&dp->d_rmod->mod_lock);
5931193Smws 
5941193Smws 	fmd_module_rele(dp->d_rmod);
5951193Smws 	ASSERT(fmd_list_next(&dp->d_mod_list) == NULL);
5961193Smws 
5971193Smws 	/*
5981193Smws 	 * Now destroy the remaining global data structures.  If 'core' was
5991193Smws 	 * set to true, force a core dump so we can check for memory leaks.
6001193Smws 	 */
6011193Smws 	if (dp->d_cases != NULL)
6021193Smws 		fmd_case_hash_destroy(dp->d_cases);
6030Sstevel@tonic-gate 	if (dp->d_disp != NULL)
6040Sstevel@tonic-gate 		fmd_dispq_destroy(dp->d_disp);
6051193Smws 	if (dp->d_timers != NULL)
6061193Smws 		fmd_timerq_destroy(dp->d_timers);
6070Sstevel@tonic-gate 	if (dp->d_schemes != NULL)
6080Sstevel@tonic-gate 		fmd_scheme_hash_destroy(dp->d_schemes);
6091193Smws 	if (dp->d_xprt_ids != NULL)
6101193Smws 		fmd_idspace_destroy(dp->d_xprt_ids);
6110Sstevel@tonic-gate 
6120Sstevel@tonic-gate 	if (dp->d_errstats != NULL) {
6130Sstevel@tonic-gate 		fmd_free(dp->d_errstats,
6140Sstevel@tonic-gate 		    sizeof (fmd_stat_t) * (EFMD_END - EFMD_UNKNOWN));
6150Sstevel@tonic-gate 	}
6160Sstevel@tonic-gate 
6170Sstevel@tonic-gate 	if (dp->d_conf != NULL)
6180Sstevel@tonic-gate 		fmd_conf_close(dp->d_conf);
6190Sstevel@tonic-gate 
620*3062Scindi 	fmd_topo_fini();
6211414Scindi 
6220Sstevel@tonic-gate 	nvlist_free(dp->d_auth);
6230Sstevel@tonic-gate 	(void) nv_alloc_fini(&dp->d_nva);
6240Sstevel@tonic-gate 	dp->d_clockops->fto_fini(dp->d_clockptr);
6250Sstevel@tonic-gate 
6260Sstevel@tonic-gate 	(void) pthread_key_delete(dp->d_key);
6270Sstevel@tonic-gate 	bzero(dp, sizeof (fmd_t));
6280Sstevel@tonic-gate 
6290Sstevel@tonic-gate 	if (core)
6300Sstevel@tonic-gate 		fmd_panic("forcing core dump at user request\n");
6310Sstevel@tonic-gate }
6320Sstevel@tonic-gate 
6330Sstevel@tonic-gate /*ARGSUSED*/
6340Sstevel@tonic-gate static void
6350Sstevel@tonic-gate fmd_gc(fmd_t *dp, id_t id, hrtime_t hrt)
6360Sstevel@tonic-gate {
6370Sstevel@tonic-gate 	hrtime_t delta;
6380Sstevel@tonic-gate 
6390Sstevel@tonic-gate 	if (id != 0) {
6400Sstevel@tonic-gate 		TRACE((FMD_DBG_MOD, "garbage collect start"));
6410Sstevel@tonic-gate 		fmd_modhash_apply(dp->d_mod_hash, fmd_module_gc);
6420Sstevel@tonic-gate 		TRACE((FMD_DBG_MOD, "garbage collect end"));
6430Sstevel@tonic-gate 
6440Sstevel@tonic-gate 		(void) pthread_rwlock_rdlock(&dp->d_log_lock);
6450Sstevel@tonic-gate 		fmd_log_update(dp->d_errlog);
6460Sstevel@tonic-gate 		(void) pthread_rwlock_unlock(&dp->d_log_lock);
6470Sstevel@tonic-gate 	}
6480Sstevel@tonic-gate 
6490Sstevel@tonic-gate 	(void) fmd_conf_getprop(dp->d_conf, "gc_interval", &delta);
6500Sstevel@tonic-gate 	(void) fmd_timerq_install(dp->d_timers, dp->d_rmod->mod_timerids,
6510Sstevel@tonic-gate 	    (fmd_timer_f *)fmd_gc, dp, NULL, delta);
6520Sstevel@tonic-gate }
6530Sstevel@tonic-gate 
6540Sstevel@tonic-gate /*
6550Sstevel@tonic-gate  * Events are committed to the errlog after cases are checkpointed.  If fmd
6560Sstevel@tonic-gate  * crashes before an event is ever associated with a module, this function will
6570Sstevel@tonic-gate  * be called to replay it to all subscribers.  If fmd crashes in between the
6580Sstevel@tonic-gate  * subscriber checkpointing and committing the event in the error log, the
6590Sstevel@tonic-gate  * module will have seen the event and we don't want to replay it.  So we look
6600Sstevel@tonic-gate  * for the event in all modules and transition it to the proper state.  If
6610Sstevel@tonic-gate  * it is found, we commit it to the error log and do not replay it.  The in-
6620Sstevel@tonic-gate  * memory case search used by fmd_module_contains() et al isn't particularly
6630Sstevel@tonic-gate  * efficient, but it is faster than doing read i/o's on every case event to
6640Sstevel@tonic-gate  * check their status or write i/o's on every event to replay to update states.
6650Sstevel@tonic-gate  * We can improve the efficiency of this lookup algorithm later if necessary.
6660Sstevel@tonic-gate  */
6670Sstevel@tonic-gate /*ARGSUSED*/
6680Sstevel@tonic-gate static void
6690Sstevel@tonic-gate fmd_err_replay(fmd_log_t *lp, fmd_event_t *ep, fmd_t *dp)
6700Sstevel@tonic-gate {
6710Sstevel@tonic-gate 	fmd_module_t *mp;
6720Sstevel@tonic-gate 	fmd_stat_t *sp;
6730Sstevel@tonic-gate 
6740Sstevel@tonic-gate 	(void) pthread_mutex_lock(&dp->d_mod_lock);
6750Sstevel@tonic-gate 
6760Sstevel@tonic-gate 	for (mp = fmd_list_next(&dp->d_mod_list);
6770Sstevel@tonic-gate 	    mp != NULL; mp = fmd_list_next(mp)) {
6780Sstevel@tonic-gate 		if (fmd_module_contains(mp, ep)) {
6790Sstevel@tonic-gate 			fmd_module_hold(mp);
6800Sstevel@tonic-gate 			break;
6810Sstevel@tonic-gate 		}
6820Sstevel@tonic-gate 	}
6830Sstevel@tonic-gate 
6840Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&dp->d_mod_lock);
6850Sstevel@tonic-gate 
6860Sstevel@tonic-gate 	if (mp != NULL) {
6870Sstevel@tonic-gate 		fmd_event_commit(ep);
6880Sstevel@tonic-gate 		fmd_module_rele(mp);
6890Sstevel@tonic-gate 		sp = &dp->d_stats->ds_log_partials;
6900Sstevel@tonic-gate 	} else {
6911193Smws 		fmd_dispq_dispatch(dp->d_disp, ep, FMD_EVENT_DATA(ep));
6920Sstevel@tonic-gate 		sp = &dp->d_stats->ds_log_replayed;
6930Sstevel@tonic-gate 	}
6940Sstevel@tonic-gate 
6950Sstevel@tonic-gate 	(void) pthread_mutex_lock(&dp->d_stats_lock);
6960Sstevel@tonic-gate 	sp->fmds_value.ui64++;
6970Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&dp->d_stats_lock);
6980Sstevel@tonic-gate }
6990Sstevel@tonic-gate 
7001193Smws void
7011193Smws fmd_door_server(void *dip)
7021193Smws {
7031193Smws 	fmd_dprintf(FMD_DBG_XPRT, "door server starting for %p\n", dip);
7041193Smws 	(void) pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
7051193Smws 	(void) door_return(NULL, 0, NULL, 0);
7061193Smws }
7071193Smws 
7081193Smws /*
7091193Smws  * Custom door server create callback.  Any fmd services that use doors will
7101193Smws  * require those threads to have their fmd-specific TSD initialized, etc.
7111193Smws  */
7121193Smws static void
7131193Smws fmd_door(door_info_t *dip)
7141193Smws {
7151193Smws 	if (fmd_thread_create(fmd.d_rmod, fmd_door_server, dip) == NULL)
7161193Smws 		fmd_panic("failed to create server for door %p", (void *)dip);
7171193Smws }
7181193Smws 
7190Sstevel@tonic-gate /*
7200Sstevel@tonic-gate  * This signal handler is installed for the client.thrsig signal to be used to
7210Sstevel@tonic-gate  * force an auxiliary thread to wake up from a system call and return EINTR in
7220Sstevel@tonic-gate  * response to a module's use of fmd_thr_signal().  We also trace the event.
7230Sstevel@tonic-gate  */
7240Sstevel@tonic-gate static void
7250Sstevel@tonic-gate fmd_signal(int sig)
7260Sstevel@tonic-gate {
7270Sstevel@tonic-gate 	TRACE((FMD_DBG_MOD, "module thread received sig #%d", sig));
7280Sstevel@tonic-gate }
7290Sstevel@tonic-gate 
7300Sstevel@tonic-gate void
7310Sstevel@tonic-gate fmd_run(fmd_t *dp, int pfd)
7320Sstevel@tonic-gate {
7330Sstevel@tonic-gate 	char *nodc_key[] = { FMD_FLT_NODC, NULL };
7340Sstevel@tonic-gate 	char nodc_str[128];
7350Sstevel@tonic-gate 	struct sigaction act;
7360Sstevel@tonic-gate 
7370Sstevel@tonic-gate 	int status = FMD_EXIT_SUCCESS;
7380Sstevel@tonic-gate 	const char *name;
7390Sstevel@tonic-gate 	fmd_conf_path_t *pap;
7401193Smws 	fmd_event_t *e;
741*3062Scindi 	int dbout;
7420Sstevel@tonic-gate 
7430Sstevel@tonic-gate 	/*
7440Sstevel@tonic-gate 	 * Cache all the current debug property settings in d_fmd_debug,
7450Sstevel@tonic-gate 	 * d_fmd_dbout, d_hdl_debug, and d_hdl_dbout.  If a given debug mask
7460Sstevel@tonic-gate 	 * is non-zero and the corresponding dbout mask is zero, set dbout
7470Sstevel@tonic-gate 	 * to a sensible default value based on whether we have daemonized.
7480Sstevel@tonic-gate 	 */
7490Sstevel@tonic-gate 	(void) fmd_conf_getprop(dp->d_conf, "dbout", &dbout);
7500Sstevel@tonic-gate 
7510Sstevel@tonic-gate 	if (dp->d_fmd_debug != 0 && dbout == 0)
7520Sstevel@tonic-gate 		dp->d_fmd_dbout = dp->d_fg? FMD_DBOUT_STDERR : FMD_DBOUT_SYSLOG;
7530Sstevel@tonic-gate 	else
7540Sstevel@tonic-gate 		dp->d_fmd_dbout = dbout;
7550Sstevel@tonic-gate 
7560Sstevel@tonic-gate 	(void) fmd_conf_getprop(dp->d_conf, "client.debug", &dp->d_hdl_debug);
7570Sstevel@tonic-gate 	(void) fmd_conf_getprop(dp->d_conf, "client.dbout", &dbout);
7580Sstevel@tonic-gate 
7590Sstevel@tonic-gate 	if (dp->d_hdl_debug != 0 && dbout == 0)
7600Sstevel@tonic-gate 		dp->d_hdl_dbout = dp->d_fg? FMD_DBOUT_STDERR : FMD_DBOUT_SYSLOG;
7610Sstevel@tonic-gate 	else
7620Sstevel@tonic-gate 		dp->d_hdl_dbout = dbout;
7630Sstevel@tonic-gate 
7640Sstevel@tonic-gate 	/*
7651193Smws 	 * Initialize remaining major program data structures such as the
7661193Smws 	 * clock, dispatch queues, log files, module hash collections, etc.
7670Sstevel@tonic-gate 	 * This work is done here rather than in fmd_create() to permit the -o
7680Sstevel@tonic-gate 	 * command-line option to modify properties after fmd_create() is done.
7690Sstevel@tonic-gate 	 */
7701414Scindi 	name = dp->d_rootdir != NULL &&
7711414Scindi 	    *dp->d_rootdir != '\0' ? dp->d_rootdir : NULL;
7721414Scindi 
773*3062Scindi 	fmd_topo_init();
7741414Scindi 
7750Sstevel@tonic-gate 	dp->d_clockptr = dp->d_clockops->fto_init();
7761193Smws 	dp->d_xprt_ids = fmd_idspace_create("xprt_ids", 1, INT_MAX);
7771193Smws 	fmd_xprt_suspend_all();
7781193Smws 
7791193Smws 	(void) door_server_create(fmd_door);
7800Sstevel@tonic-gate 	fmd_dr_init();
7810Sstevel@tonic-gate 
7820Sstevel@tonic-gate 	dp->d_rmod->mod_timerids = fmd_idspace_create(dp->d_pname, 1, 16);
7830Sstevel@tonic-gate 	dp->d_timers = fmd_timerq_create();
7840Sstevel@tonic-gate 	dp->d_disp = fmd_dispq_create();
7850Sstevel@tonic-gate 	dp->d_cases = fmd_case_hash_create();
7860Sstevel@tonic-gate 
7870Sstevel@tonic-gate 	/*
7881193Smws 	 * The root module's mod_queue is created with limit zero, making it
7891193Smws 	 * act like /dev/null; anything inserted here is simply ignored.
7901193Smws 	 */
7911193Smws 	dp->d_rmod->mod_queue = fmd_eventq_create(dp->d_rmod,
7921193Smws 	    &dp->d_rmod->mod_stats->ms_evqstat, &dp->d_rmod->mod_stats_lock, 0);
7931193Smws 
7941193Smws 	/*
7950Sstevel@tonic-gate 	 * Once our subsystems that use signals have been set up, install the
7960Sstevel@tonic-gate 	 * signal handler for the fmd_thr_signal() API.  Verify that the signal
7970Sstevel@tonic-gate 	 * being used for this purpose doesn't conflict with something else.
7980Sstevel@tonic-gate 	 */
7990Sstevel@tonic-gate 	(void) fmd_conf_getprop(dp->d_conf, "client.thrsig", &dp->d_thr_sig);
8000Sstevel@tonic-gate 
8010Sstevel@tonic-gate 	if (sigaction(dp->d_thr_sig, NULL, &act) != 0) {
8020Sstevel@tonic-gate 		fmd_error(EFMD_EXIT, "invalid signal selected for "
8030Sstevel@tonic-gate 		    "client.thrsig property: %d\n", dp->d_thr_sig);
8040Sstevel@tonic-gate 	}
8050Sstevel@tonic-gate 
8060Sstevel@tonic-gate 	if (act.sa_handler != SIG_IGN && act.sa_handler != SIG_DFL) {
8070Sstevel@tonic-gate 		fmd_error(EFMD_EXIT, "signal selected for client.thrsig "
8080Sstevel@tonic-gate 		    "property is already in use: %d\n", dp->d_thr_sig);
8090Sstevel@tonic-gate 	}
8100Sstevel@tonic-gate 
8110Sstevel@tonic-gate 	act.sa_handler = fmd_signal;
8120Sstevel@tonic-gate 	act.sa_flags = 0;
8130Sstevel@tonic-gate 
8140Sstevel@tonic-gate 	(void) sigemptyset(&act.sa_mask);
8150Sstevel@tonic-gate 	(void) sigaction(dp->d_thr_sig, &act, NULL);
8160Sstevel@tonic-gate 
8170Sstevel@tonic-gate 	(void) fmd_conf_getprop(dp->d_conf, "schemedir", &name);
8180Sstevel@tonic-gate 	dp->d_schemes = fmd_scheme_hash_create(dp->d_rootdir, name);
8190Sstevel@tonic-gate 
8200Sstevel@tonic-gate 	(void) fmd_conf_getprop(dp->d_conf, "log.rsrc", &name);
8210Sstevel@tonic-gate 	dp->d_asrus = fmd_asru_hash_create(dp->d_rootdir, name);
8220Sstevel@tonic-gate 
8230Sstevel@tonic-gate 	(void) fmd_conf_getprop(dp->d_conf, "log.error", &name);
8240Sstevel@tonic-gate 	dp->d_errlog = fmd_log_open(dp->d_rootdir, name, FMD_LOG_ERROR);
8250Sstevel@tonic-gate 
8260Sstevel@tonic-gate 	(void) fmd_conf_getprop(dp->d_conf, "log.fault", &name);
8270Sstevel@tonic-gate 	dp->d_fltlog = fmd_log_open(dp->d_rootdir, name, FMD_LOG_FAULT);
8280Sstevel@tonic-gate 
8290Sstevel@tonic-gate 	if (dp->d_asrus == NULL || dp->d_errlog == NULL || dp->d_fltlog == NULL)
8300Sstevel@tonic-gate 		fmd_error(EFMD_EXIT, "failed to initialize log files\n");
8310Sstevel@tonic-gate 
8321193Smws 	/*
8331193Smws 	 * Before loading modules, create an empty control event which will act
8341193Smws 	 * as a global barrier for module event processing.  Each module we
8351193Smws 	 * load successfully will insert it at their head of their event queue,
8361193Smws 	 * and then pause inside of fmd_ctl_rele() after dequeuing the event.
8371193Smws 	 * This module barrier is required for two reasons:
8381193Smws 	 *
8391193Smws 	 * (a) During module loading, the restoration of case checkpoints may
8401193Smws 	 *    result in a list.* event being recreated for which the intended
8411193Smws 	 *    subscriber has not yet loaded depending on the load order. Such
8421193Smws 	 *    events could then result in spurious "no subscriber" errors.
8431193Smws 	 *
8441193Smws 	 * (b) During errlog replay, a sequence of errors from a long time ago
8451193Smws 	 *    may be replayed, and the module may attempt to install relative
8461193Smws 	 *    timers associated with one or more of these events.  If errlog
8471193Smws 	 *    replay were "racing" with active module threads, an event E1
8481193Smws 	 *    that resulted in a relative timer T at time E1 + N nsec could
8491193Smws 	 *    fire prior to an event E2 being enqueued, even if the relative
8501193Smws 	 *    time ordering was E1 < E2 < E1 + N, causing mis-diagnosis.
8511193Smws 	 */
8521193Smws 	dp->d_mod_event = e = fmd_event_create(FMD_EVT_CTL,
8531193Smws 	    FMD_HRT_NOW, NULL, fmd_ctl_init(NULL));
8541193Smws 
8551193Smws 	fmd_event_hold(e);
8561193Smws 
8571193Smws 	/*
8581193Smws 	 * Once all data structures are initialized, we load all of our modules
8591193Smws 	 * in order according to class in order to load up any subscriptions.
8601193Smws 	 * Once built-in modules are loaded, we detach from our waiting parent.
8611193Smws 	 */
8620Sstevel@tonic-gate 	dp->d_mod_hash = fmd_modhash_create();
8631193Smws 
8641193Smws 	if (fmd_builtin_loadall(dp->d_mod_hash) != 0 && !dp->d_fg)
8651193Smws 		fmd_error(EFMD_EXIT, "failed to initialize fault manager\n");
8661193Smws 
8671193Smws 	(void) fmd_conf_getprop(dp->d_conf, "self.name", &name);
8681193Smws 	dp->d_self = fmd_modhash_lookup(dp->d_mod_hash, name);
8691193Smws 
8701193Smws 	if (dp->d_self != NULL && fmd_module_dc_key2code(dp->d_self,
8711193Smws 	    nodc_key, nodc_str, sizeof (nodc_str)) == 0)
8721193Smws 		(void) fmd_conf_setprop(dp->d_conf, "nodiagcode", nodc_str);
8731193Smws 
8741193Smws 	fmd_rpc_init();
8750Sstevel@tonic-gate 	dp->d_running = 1; /* we are now officially an active fmd */
8760Sstevel@tonic-gate 
8770Sstevel@tonic-gate 	/*
8780Sstevel@tonic-gate 	 * Now that we're running, if a pipe fd was specified, write an exit
8790Sstevel@tonic-gate 	 * status to it to indicate that our parent process can safely detach.
8801193Smws 	 * Then proceed to loading the remaining non-built-in modules.
8810Sstevel@tonic-gate 	 */
8820Sstevel@tonic-gate 	if (pfd >= 0)
8830Sstevel@tonic-gate 		(void) write(pfd, &status, sizeof (status));
8840Sstevel@tonic-gate 
8851552Smws 	/*
8861552Smws 	 * Before loading all modules, repopulate the ASRU cache from its
8871552Smws 	 * persistent repository on disk.  Then during module loading, the
8881552Smws 	 * restoration of checkpoint files will reparent any active cases.
8891552Smws 	 */
8901552Smws 	fmd_asru_hash_refresh(dp->d_asrus);
8911552Smws 
8920Sstevel@tonic-gate 	(void) fmd_conf_getprop(dp->d_conf, "plugin.path", &pap);
8931429Smws 	fmd_modhash_loadall(dp->d_mod_hash, pap, &fmd_rtld_ops, ".so");
8940Sstevel@tonic-gate 
8950Sstevel@tonic-gate 	(void) fmd_conf_getprop(dp->d_conf, "agent.path", &pap);
8961429Smws 	fmd_modhash_loadall(dp->d_mod_hash, pap, &fmd_proc_ops, NULL);
8970Sstevel@tonic-gate 
8980Sstevel@tonic-gate 	/*
8991193Smws 	 * With all modules loaded, replay fault events from the ASRU cache for
9001193Smws 	 * any ASRUs that must be retired, replay error events from the errlog
9011193Smws 	 * that did not finish processing the last time ran, and then release
9021193Smws 	 * the global module barrier by executing a final rele on d_mod_event.
9030Sstevel@tonic-gate 	 */
9041552Smws 	fmd_asru_hash_replay(dp->d_asrus);
9050Sstevel@tonic-gate 
9060Sstevel@tonic-gate 	(void) pthread_rwlock_rdlock(&dp->d_log_lock);
9070Sstevel@tonic-gate 	fmd_log_replay(dp->d_errlog, (fmd_log_f *)fmd_err_replay, dp);
9080Sstevel@tonic-gate 	fmd_log_update(dp->d_errlog);
9090Sstevel@tonic-gate 	(void) pthread_rwlock_unlock(&dp->d_log_lock);
9100Sstevel@tonic-gate 
9111193Smws 	dp->d_mod_event = NULL;
9121193Smws 	fmd_event_rele(e);
9130Sstevel@tonic-gate 
9140Sstevel@tonic-gate 	/*
9150Sstevel@tonic-gate 	 * Finally, awaken any threads associated with receiving events from
9161193Smws 	 * open transports and tell them to proceed with fmd_xprt_recv().
9170Sstevel@tonic-gate 	 */
9181193Smws 	fmd_xprt_resume_all();
9191193Smws 	fmd_gc(dp, 0, 0);
9200Sstevel@tonic-gate 
9211193Smws 	dp->d_booted = 1;
9220Sstevel@tonic-gate }
9230Sstevel@tonic-gate 
9240Sstevel@tonic-gate void
9250Sstevel@tonic-gate fmd_help(fmd_t *dp)
9260Sstevel@tonic-gate {
9270Sstevel@tonic-gate 	const fmd_conf_mode_t *cmp;
9280Sstevel@tonic-gate 
9290Sstevel@tonic-gate 	(void) printf("Usage: %s -o debug=mode[,mode]\n", dp->d_pname);
9300Sstevel@tonic-gate 
9310Sstevel@tonic-gate 	for (cmp = _fmd_debug_modes; cmp->cm_name != NULL; cmp++)
9320Sstevel@tonic-gate 		(void) printf("\t%s\t%s\n", cmp->cm_name, cmp->cm_desc);
9330Sstevel@tonic-gate }
934