xref: /illumos-gate/usr/src/cmd/fm/fmd/common/fmd.c (revision 1743a90d)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
582d33c01Sav145390  * Common Development and Distribution License (the "License").
682d33c01Sav145390  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21d9638e54Smws 
227c478bd9Sstevel@tonic-gate /*
23f6e214c7SGavin Maltby  * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include <sys/types.h>
277c478bd9Sstevel@tonic-gate #include <sys/utsname.h>
287c478bd9Sstevel@tonic-gate #include <sys/param.h>
297c478bd9Sstevel@tonic-gate #include <sys/systeminfo.h>
307c478bd9Sstevel@tonic-gate #include <sys/fm/util.h>
317c478bd9Sstevel@tonic-gate 
327aec1d6eScindi #include <smbios.h>
337c478bd9Sstevel@tonic-gate #include <limits.h>
347c478bd9Sstevel@tonic-gate #include <unistd.h>
357c478bd9Sstevel@tonic-gate #include <signal.h>
367c478bd9Sstevel@tonic-gate #include <stdlib.h>
377c478bd9Sstevel@tonic-gate #include <stdio.h>
386e1fa242SStephen Hanson #include <ctype.h>
39d9638e54Smws #include <door.h>
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate #include <fmd_conf.h>
427c478bd9Sstevel@tonic-gate #include <fmd_dispq.h>
437c478bd9Sstevel@tonic-gate #include <fmd_timerq.h>
447c478bd9Sstevel@tonic-gate #include <fmd_subr.h>
457c478bd9Sstevel@tonic-gate #include <fmd_error.h>
467c478bd9Sstevel@tonic-gate #include <fmd_module.h>
477c478bd9Sstevel@tonic-gate #include <fmd_thread.h>
487c478bd9Sstevel@tonic-gate #include <fmd_alloc.h>
497c478bd9Sstevel@tonic-gate #include <fmd_string.h>
507c478bd9Sstevel@tonic-gate #include <fmd_builtin.h>
517c478bd9Sstevel@tonic-gate #include <fmd_ustat.h>
527c478bd9Sstevel@tonic-gate #include <fmd_protocol.h>
537c478bd9Sstevel@tonic-gate #include <fmd_scheme.h>
547c478bd9Sstevel@tonic-gate #include <fmd_asru.h>
557c478bd9Sstevel@tonic-gate #include <fmd_case.h>
567c478bd9Sstevel@tonic-gate #include <fmd_log.h>
57d9638e54Smws #include <fmd_idspace.h>
587c478bd9Sstevel@tonic-gate #include <fmd_rpc.h>
597c478bd9Sstevel@tonic-gate #include <fmd_dr.h>
600eb822a1Scindi #include <fmd_topo.h>
61d9638e54Smws #include <fmd_xprt.h>
62d9638e54Smws #include <fmd_ctl.h>
6382d33c01Sav145390 #include <sys/openpromio.h>
6482d33c01Sav145390 #include <libdevinfo.h>
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate #include <fmd.h>
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate extern const nv_alloc_ops_t fmd_nv_alloc_ops;	/* see fmd_nv.c */
697c478bd9Sstevel@tonic-gate 
7024db4641Seschrock const char _fmd_version[] = "1.2";		/* daemon version string */
717c478bd9Sstevel@tonic-gate static char _fmd_plat[MAXNAMELEN];		/* native platform string */
727c478bd9Sstevel@tonic-gate static char _fmd_isa[MAXNAMELEN];		/* native instruction set */
737c478bd9Sstevel@tonic-gate static struct utsname _fmd_uts;			/* native uname(2) info */
749c94f155SCheng Sean Ye static char _fmd_psn[MAXNAMELEN];		/* product serial number */
757aec1d6eScindi static char _fmd_csn[MAXNAMELEN];		/* chassis serial number */
767aec1d6eScindi static char _fmd_prod[MAXNAMELEN];		/* product name string */
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate /*
797c478bd9Sstevel@tonic-gate  * Note: the configuration file path is ordered from most common to most host-
807c478bd9Sstevel@tonic-gate  * specific because new conf files are merged/override previous ones.  The
817c478bd9Sstevel@tonic-gate  * module paths are in the opposite order, from most specific to most common,
827c478bd9Sstevel@tonic-gate  * because once a module is loaded fmd will not try to load over the same name.
837c478bd9Sstevel@tonic-gate  */
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate static const char _fmd_conf_path[] =
867c478bd9Sstevel@tonic-gate 	"%r/usr/lib/fm/fmd:"
877c478bd9Sstevel@tonic-gate 	"%r/usr/platform/%m/lib/fm/fmd:"
887c478bd9Sstevel@tonic-gate 	"%r/usr/platform/%i/lib/fm/fmd:"
897c478bd9Sstevel@tonic-gate 	"%r/etc/fm/fmd";
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate static const char _fmd_agent_path[] =
927c478bd9Sstevel@tonic-gate 	"%r/usr/platform/%i/lib/fm/fmd/agents:"
937c478bd9Sstevel@tonic-gate 	"%r/usr/platform/%m/lib/fm/fmd/agents:"
947c478bd9Sstevel@tonic-gate 	"%r/usr/lib/fm/fmd/agents";
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate static const char _fmd_plugin_path[] =
977c478bd9Sstevel@tonic-gate 	"%r/usr/platform/%i/lib/fm/fmd/plugins:"
987c478bd9Sstevel@tonic-gate 	"%r/usr/platform/%m/lib/fm/fmd/plugins:"
997c478bd9Sstevel@tonic-gate 	"%r/usr/lib/fm/fmd/plugins";
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate static const char _fmd_scheme_path[] =
1027c478bd9Sstevel@tonic-gate 	"usr/lib/fm/fmd/schemes";
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate static const fmd_conf_mode_t _fmd_cerror_modes[] = {
1057c478bd9Sstevel@tonic-gate 	{ "unload", "unload offending client module", FMD_CERROR_UNLOAD },
1067c478bd9Sstevel@tonic-gate 	{ "stop", "stop daemon for debugger attach", FMD_CERROR_STOP },
1077c478bd9Sstevel@tonic-gate 	{ "abort", "abort daemon and force core dump", FMD_CERROR_ABORT },
1087c478bd9Sstevel@tonic-gate 	{ NULL, NULL, 0 }
1097c478bd9Sstevel@tonic-gate };
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate static const fmd_conf_mode_t _fmd_dbout_modes[] = {
1127c478bd9Sstevel@tonic-gate 	{ "stderr", "send debug messages to stderr", FMD_DBOUT_STDERR },
1137c478bd9Sstevel@tonic-gate 	{ "syslog", "send debug messages to syslog", FMD_DBOUT_SYSLOG },
1147c478bd9Sstevel@tonic-gate 	{ NULL, NULL, 0 }
1157c478bd9Sstevel@tonic-gate };
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate static const fmd_conf_mode_t _fmd_debug_modes[] = {
1187c478bd9Sstevel@tonic-gate 	{ "help", "display debugging modes and exit", FMD_DBG_HELP },
1197c478bd9Sstevel@tonic-gate 	{ "mod", "debug module load/unload/locking", FMD_DBG_MOD },
1207c478bd9Sstevel@tonic-gate 	{ "disp", "debug dispatch queue processing", FMD_DBG_DISP },
1217c478bd9Sstevel@tonic-gate 	{ "xprt", "debug transport-specific routines", FMD_DBG_XPRT },
1227c478bd9Sstevel@tonic-gate 	{ "evt", "debug event subsystem routines", FMD_DBG_EVT },
1237c478bd9Sstevel@tonic-gate 	{ "log", "debug log subsystem routines", FMD_DBG_LOG },
1247c478bd9Sstevel@tonic-gate 	{ "tmr", "debug timer subsystem routines", FMD_DBG_TMR },
1257c478bd9Sstevel@tonic-gate 	{ "fmri", "debug fmri subsystem routines", FMD_DBG_FMRI },
1267c478bd9Sstevel@tonic-gate 	{ "asru", "debug asru subsystem routines", FMD_DBG_ASRU },
1277c478bd9Sstevel@tonic-gate 	{ "case", "debug case subsystem routines", FMD_DBG_CASE },
1287c478bd9Sstevel@tonic-gate 	{ "ckpt", "debug checkpoint routines", FMD_DBG_CKPT },
1297c478bd9Sstevel@tonic-gate 	{ "rpc", "debug rpc service routines", FMD_DBG_RPC },
130d9638e54Smws 	{ "trace", "display matching trace calls", FMD_DBG_TRACE },
1317c478bd9Sstevel@tonic-gate 	{ "all", "enable all available debug modes", FMD_DBG_ALL },
1327c478bd9Sstevel@tonic-gate 	{ NULL, NULL, 0 }
1337c478bd9Sstevel@tonic-gate };
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate static int
fmd_cerror_set(fmd_conf_param_t * pp,const char * value)1367c478bd9Sstevel@tonic-gate fmd_cerror_set(fmd_conf_param_t *pp, const char *value)
1377c478bd9Sstevel@tonic-gate {
1387c478bd9Sstevel@tonic-gate 	return (fmd_conf_mode_set(_fmd_cerror_modes, pp, value));
1397c478bd9Sstevel@tonic-gate }
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate static int
fmd_dbout_set(fmd_conf_param_t * pp,const char * value)1427c478bd9Sstevel@tonic-gate fmd_dbout_set(fmd_conf_param_t *pp, const char *value)
1437c478bd9Sstevel@tonic-gate {
1447c478bd9Sstevel@tonic-gate 	return (fmd_conf_mode_set(_fmd_dbout_modes, pp, value));
1457c478bd9Sstevel@tonic-gate }
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate static int
fmd_debug_set(fmd_conf_param_t * pp,const char * value)1487c478bd9Sstevel@tonic-gate fmd_debug_set(fmd_conf_param_t *pp, const char *value)
1497c478bd9Sstevel@tonic-gate {
1507c478bd9Sstevel@tonic-gate 	int err = fmd_conf_mode_set(_fmd_debug_modes, pp, value);
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 	if (err == 0)
1537c478bd9Sstevel@tonic-gate 		fmd.d_fmd_debug = pp->cp_value.cpv_num;
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 	return (err);
1567c478bd9Sstevel@tonic-gate }
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate static int
fmd_trmode_set(fmd_conf_param_t * pp,const char * value)1597c478bd9Sstevel@tonic-gate fmd_trmode_set(fmd_conf_param_t *pp, const char *value)
1607c478bd9Sstevel@tonic-gate {
1617c478bd9Sstevel@tonic-gate 	fmd_tracebuf_f *func;
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate 	if (strcasecmp(value, "none") == 0)
1647c478bd9Sstevel@tonic-gate 		func = fmd_trace_none;
1657c478bd9Sstevel@tonic-gate 	else if (strcasecmp(value, "lite") == 0)
1667c478bd9Sstevel@tonic-gate 		func = fmd_trace_lite;
1677c478bd9Sstevel@tonic-gate 	else if (strcasecmp(value, "full") == 0)
1687c478bd9Sstevel@tonic-gate 		func = fmd_trace_full;
1697c478bd9Sstevel@tonic-gate 	else
1707c478bd9Sstevel@tonic-gate 		return (fmd_set_errno(EFMD_CONF_INVAL));
1717c478bd9Sstevel@tonic-gate 
172*1743a90dSToomas Soome 	fmd.d_thr_trace = func;
173*1743a90dSToomas Soome 	pp->cp_value.cpv_ptr = func;
1747c478bd9Sstevel@tonic-gate 	return (0);
1757c478bd9Sstevel@tonic-gate }
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate static void
fmd_trmode_get(const fmd_conf_param_t * pp,void * ptr)1787c478bd9Sstevel@tonic-gate fmd_trmode_get(const fmd_conf_param_t *pp, void *ptr)
1797c478bd9Sstevel@tonic-gate {
1807c478bd9Sstevel@tonic-gate 	*((void **)ptr) = pp->cp_value.cpv_ptr;
1817c478bd9Sstevel@tonic-gate }
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate static int
fmd_clkmode_set(fmd_conf_param_t * pp,const char * value)1847c478bd9Sstevel@tonic-gate fmd_clkmode_set(fmd_conf_param_t *pp, const char *value)
1857c478bd9Sstevel@tonic-gate {
1867c478bd9Sstevel@tonic-gate 	const fmd_timeops_t *ops;
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 	if (strcasecmp(value, "native") == 0)
1897c478bd9Sstevel@tonic-gate 		ops = &fmd_timeops_native;
1907c478bd9Sstevel@tonic-gate 	else if (strcasecmp(value, "simulated") == 0)
1917c478bd9Sstevel@tonic-gate 		ops = &fmd_timeops_simulated;
1927c478bd9Sstevel@tonic-gate 	else
1937c478bd9Sstevel@tonic-gate 		return (fmd_set_errno(EFMD_CONF_INVAL));
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate 	fmd.d_clockops = ops;
1967c478bd9Sstevel@tonic-gate 	pp->cp_value.cpv_ptr = (void *)ops;
1977c478bd9Sstevel@tonic-gate 	return (0);
1987c478bd9Sstevel@tonic-gate }
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate static void
fmd_clkmode_get(const fmd_conf_param_t * pp,void * ptr)2017c478bd9Sstevel@tonic-gate fmd_clkmode_get(const fmd_conf_param_t *pp, void *ptr)
2027c478bd9Sstevel@tonic-gate {
2037c478bd9Sstevel@tonic-gate 	*((void **)ptr) = pp->cp_value.cpv_ptr;
2047c478bd9Sstevel@tonic-gate }
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate static const fmd_conf_ops_t fmd_cerror_ops = {
2077c478bd9Sstevel@tonic-gate 	fmd_cerror_set, fmd_conf_mode_get, fmd_conf_notsup, fmd_conf_nop
2087c478bd9Sstevel@tonic-gate };
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate static const fmd_conf_ops_t fmd_dbout_ops = {
2117c478bd9Sstevel@tonic-gate 	fmd_dbout_set, fmd_conf_mode_get, fmd_conf_notsup, fmd_conf_nop
2127c478bd9Sstevel@tonic-gate };
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate static const fmd_conf_ops_t fmd_debug_ops = {
2157c478bd9Sstevel@tonic-gate 	fmd_debug_set, fmd_conf_mode_get, fmd_conf_notsup, fmd_conf_nop
2167c478bd9Sstevel@tonic-gate };
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate static const fmd_conf_ops_t fmd_trmode_ops = {
2197c478bd9Sstevel@tonic-gate 	fmd_trmode_set, fmd_trmode_get, fmd_conf_notsup, fmd_conf_nop
2207c478bd9Sstevel@tonic-gate };
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate static const fmd_conf_ops_t fmd_clkmode_ops = {
2237c478bd9Sstevel@tonic-gate 	fmd_clkmode_set, fmd_clkmode_get, fmd_conf_notsup, fmd_conf_nop
2247c478bd9Sstevel@tonic-gate };
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate static const fmd_conf_formal_t _fmd_conf[] = {
2277c478bd9Sstevel@tonic-gate { "agent.path", &fmd_conf_path, _fmd_agent_path }, /* path for agents */
2287c478bd9Sstevel@tonic-gate { "alloc_msecs", &fmd_conf_uint32, "10" },	/* msecs before alloc retry */
2297c478bd9Sstevel@tonic-gate { "alloc_tries", &fmd_conf_uint32, "3" },	/* max # of alloc retries */
2309c94f155SCheng Sean Ye { "product_sn", &fmd_conf_string, _fmd_psn },	/* product serial number */
2317aec1d6eScindi { "chassis", &fmd_conf_string, _fmd_csn },	/* chassis serial number */
2327c478bd9Sstevel@tonic-gate { "ckpt.dir", &fmd_conf_string, "var/fm/fmd/ckpt" }, /* ckpt directory path */
233540db9a9SStephen Hanson { "ckpt.dirmode", &fmd_conf_int32, "0755" },	/* ckpt directory perm mode */
234540db9a9SStephen Hanson { "ckpt.mode", &fmd_conf_int32, "0644" },	/* ckpt file perm mode */
2357c478bd9Sstevel@tonic-gate { "ckpt.restore", &fmd_conf_bool, "true" },	/* restore checkpoints? */
2367c478bd9Sstevel@tonic-gate { "ckpt.save", &fmd_conf_bool, "true" },	/* save checkpoints? */
2377c478bd9Sstevel@tonic-gate { "ckpt.zero", &fmd_conf_bool, "false" },	/* zero checkpoints on start? */
2387c478bd9Sstevel@tonic-gate { "client.buflim", &fmd_conf_size, "10m" },	/* client buffer space limit */
2397c478bd9Sstevel@tonic-gate { "client.dbout", &fmd_dbout_ops, NULL },	/* client debug output sinks */
2407c478bd9Sstevel@tonic-gate { "client.debug", &fmd_conf_bool, NULL },	/* client debug enable */
241f6e214c7SGavin Maltby { "client.doorthrlim", &fmd_conf_uint32, "20" }, /* client door thread limit */
2427c478bd9Sstevel@tonic-gate { "client.error", &fmd_cerror_ops, "unload" },	/* client error policy */
2437c478bd9Sstevel@tonic-gate { "client.memlim", &fmd_conf_size, "10m" },	/* client allocation limit */
2447c478bd9Sstevel@tonic-gate { "client.evqlim", &fmd_conf_uint32, "256" },	/* client event queue limit */
24525351652SVuong Nguyen { "client.thrlim", &fmd_conf_uint32, "20" },	/* client aux thread limit */
2467c478bd9Sstevel@tonic-gate { "client.thrsig", &fmd_conf_signal, "SIGUSR1" }, /* fmd_thr_signal() value */
2477c478bd9Sstevel@tonic-gate { "client.tmrlim", &fmd_conf_uint32, "1024" },	/* client pending timer limit */
248d9638e54Smws { "client.xprtlim", &fmd_conf_uint32, "256" },	/* client transport limit */
249d9638e54Smws { "client.xprtlog", &fmd_conf_bool, NULL },	/* client transport logging? */
25011114147Sav145390 { "client.xprtqlim", &fmd_conf_uint32, "1024" }, /* client transport queue li */
2517c478bd9Sstevel@tonic-gate { "clock", &fmd_clkmode_ops, "native" },	/* clock operation mode */
2527c478bd9Sstevel@tonic-gate { "conf_path", &fmd_conf_path, _fmd_conf_path }, /* root config file path */
2537c478bd9Sstevel@tonic-gate { "conf_file", &fmd_conf_string, "fmd.conf" },	/* root config file name */
2547c478bd9Sstevel@tonic-gate { "core", &fmd_conf_bool, "false" },		/* force core dump on quit */
2557c478bd9Sstevel@tonic-gate { "dbout", &fmd_dbout_ops, NULL },		/* daemon debug output sinks */
2567c478bd9Sstevel@tonic-gate { "debug", &fmd_debug_ops, NULL },		/* daemon debugging flags */
2577c478bd9Sstevel@tonic-gate { "dictdir", &fmd_conf_string, "usr/lib/fm/dict" }, /* default diagcode dir */
2587c478bd9Sstevel@tonic-gate { "domain", &fmd_conf_string, NULL },		/* domain id for de auth */
25925c6ff4bSstephh { "fakenotpresent", &fmd_conf_uint32, "0" },	/* simulate rsrc not present */
2607c478bd9Sstevel@tonic-gate { "fg", &fmd_conf_bool, "false" },		/* run daemon in foreground */
2617c478bd9Sstevel@tonic-gate { "gc_interval", &fmd_conf_time, "1d" },	/* garbage collection intvl */
2627c478bd9Sstevel@tonic-gate { "ids.avg", &fmd_conf_uint32, "4" },		/* desired idspace chain len */
2637c478bd9Sstevel@tonic-gate { "ids.max", &fmd_conf_uint32, "1024" },	/* maximum idspace buckets */
2647c478bd9Sstevel@tonic-gate { "isaname", &fmd_conf_string, _fmd_isa },	/* instruction set (uname -p) */
2657c478bd9Sstevel@tonic-gate { "log.creator", &fmd_conf_string, "fmd" },	/* exacct log creator string */
2667c478bd9Sstevel@tonic-gate { "log.error", &fmd_conf_string, "var/fm/fmd/errlog" }, /* error log path */
2677c478bd9Sstevel@tonic-gate { "log.fault", &fmd_conf_string, "var/fm/fmd/fltlog" }, /* fault log path */
268f6e214c7SGavin Maltby { "log.info", &fmd_conf_string, "var/fm/fmd/infolog" }, /* info log path */
269f6e214c7SGavin Maltby { "log.info_hival", &fmd_conf_string, "var/fm/fmd/infolog_hival" }, /* hi pri */
2707c478bd9Sstevel@tonic-gate { "log.minfree", &fmd_conf_size, "2m" },	/* min log fsys free space */
271d9638e54Smws { "log.rsrc", &fmd_conf_string, "var/fm/fmd/rsrc" }, /* asru log dir path */
2727c478bd9Sstevel@tonic-gate { "log.tryrotate", &fmd_conf_uint32, "10" },	/* max log rotation attempts */
2737c478bd9Sstevel@tonic-gate { "log.waitrotate", &fmd_conf_time, "200ms" },	/* log rotation retry delay */
274d9638e54Smws { "log.xprt", &fmd_conf_string, "var/fm/fmd/xprt" }, /* transport log dir */
2757c478bd9Sstevel@tonic-gate { "machine", &fmd_conf_string, _fmd_uts.machine }, /* machine name (uname -m) */
2767c478bd9Sstevel@tonic-gate { "nodiagcode", &fmd_conf_string, "-" },	/* diagcode to use if error */
277627351e3Scy152378 { "repaircode", &fmd_conf_string, "-" },	/* diagcode for list.repaired */
27825c6ff4bSstephh { "resolvecode", &fmd_conf_string, "-" },	/* diagcode for list.resolved */
27925c6ff4bSstephh { "updatecode", &fmd_conf_string, "-" },	/* diagcode for list.updated */
2807c478bd9Sstevel@tonic-gate { "osrelease", &fmd_conf_string, _fmd_uts.release }, /* release (uname -r) */
2817c478bd9Sstevel@tonic-gate { "osversion", &fmd_conf_string, _fmd_uts.version }, /* version (uname -v) */
2827c478bd9Sstevel@tonic-gate { "platform", &fmd_conf_string, _fmd_plat },	/* platform string (uname -i) */
2837c478bd9Sstevel@tonic-gate { "plugin.close", &fmd_conf_bool, "true" },	/* dlclose plugins on fini */
2847c478bd9Sstevel@tonic-gate { "plugin.path", &fmd_conf_path, _fmd_plugin_path }, /* path for plugin mods */
2857aec1d6eScindi { "product", &fmd_conf_string, _fmd_prod },	/* product name string */
2867c478bd9Sstevel@tonic-gate { "rootdir", &fmd_conf_string, "" },		/* root directory for paths */
2877c478bd9Sstevel@tonic-gate { "rpc.adm.path", &fmd_conf_string, NULL },	/* FMD_ADM rendezvous file */
2887c478bd9Sstevel@tonic-gate { "rpc.adm.prog", &fmd_conf_uint32, "100169" },	/* FMD_ADM rpc program num */
2897c478bd9Sstevel@tonic-gate { "rpc.api.path", &fmd_conf_string, NULL },	/* FMD_API rendezvous file */
2907c478bd9Sstevel@tonic-gate { "rpc.api.prog", &fmd_conf_uint32, "100170" },	/* FMD_API rpc program num */
2917c478bd9Sstevel@tonic-gate { "rpc.rcvsize", &fmd_conf_size, "128k" },	/* rpc receive buffer size */
2927c478bd9Sstevel@tonic-gate { "rpc.sndsize", &fmd_conf_size, "128k" },	/* rpc send buffer size */
2935750ef5cSStephen Hanson { "rsrc.pollperiod", &fmd_conf_time, "1h" },	/* aged rsrcs poller period */
2947c478bd9Sstevel@tonic-gate { "rsrc.age", &fmd_conf_time, "30d" },		/* max age of old rsrc log */
2957c478bd9Sstevel@tonic-gate { "rsrc.zero", &fmd_conf_bool, "false" },	/* zero rsrc cache on start? */
2967c478bd9Sstevel@tonic-gate { "schemedir", &fmd_conf_string, _fmd_scheme_path }, /* path for scheme mods */
2977c478bd9Sstevel@tonic-gate { "self.name", &fmd_conf_string, "fmd-self-diagnosis" }, /* self-diag module */
2987c478bd9Sstevel@tonic-gate { "self.dict", &fmd_conf_list, "FMD.dict" },	/* self-diag dictionary list */
2997c478bd9Sstevel@tonic-gate { "server", &fmd_conf_string, _fmd_uts.nodename }, /* server id for de auth */
3007c478bd9Sstevel@tonic-gate { "strbuckets", &fmd_conf_uint32, "211" },	/* size of string hashes */
3017c478bd9Sstevel@tonic-gate #ifdef DEBUG
3027c478bd9Sstevel@tonic-gate { "trace.mode", &fmd_trmode_ops, "full" },	/* trace mode: none/lite/full */
3037c478bd9Sstevel@tonic-gate #else
3047c478bd9Sstevel@tonic-gate { "trace.mode", &fmd_trmode_ops, "lite" },	/* trace mode: none/lite/full */
3057c478bd9Sstevel@tonic-gate #endif
3067c478bd9Sstevel@tonic-gate { "trace.recs", &fmd_conf_uint32, "128" },	/* trace records per thread */
3077c478bd9Sstevel@tonic-gate { "trace.frames", &fmd_conf_uint32, "16" },	/* max trace rec stack frames */
3087c478bd9Sstevel@tonic-gate { "uuidlen", &fmd_conf_uint32, "36" },		/* UUID ASCII string length */
309d9638e54Smws { "xprt.ttl", &fmd_conf_uint8, "1" },		/* default event time-to-live */
3107c478bd9Sstevel@tonic-gate };
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate /*
3137c478bd9Sstevel@tonic-gate  * Statistics maintained by fmd itself on behalf of various global subsystems.
3147c478bd9Sstevel@tonic-gate  * NOTE: FMD_TYPE_STRING statistics should not be used here.  If they are
3157c478bd9Sstevel@tonic-gate  * required in the future, the FMD_ADM_MODGSTAT service routine must change.
3167c478bd9Sstevel@tonic-gate  */
3177c478bd9Sstevel@tonic-gate static fmd_statistics_t _fmd_stats = {
3187c478bd9Sstevel@tonic-gate { "errlog.replayed", FMD_TYPE_UINT64, "total events replayed from errlog" },
3197c478bd9Sstevel@tonic-gate { "errlog.partials", FMD_TYPE_UINT64, "events partially committed in errlog" },
3207c478bd9Sstevel@tonic-gate { "errlog.enospc", FMD_TYPE_UINT64, "events not appended to errlog (ENOSPC)" },
3217c478bd9Sstevel@tonic-gate { "fltlog.enospc", FMD_TYPE_UINT64, "events not appended to fltlog (ENOSPC)" },
3227c478bd9Sstevel@tonic-gate { "log.enospc", FMD_TYPE_UINT64, "events not appended to other logs (ENOSPC)" },
3237c478bd9Sstevel@tonic-gate { "dr.gen", FMD_TYPE_UINT64, "dynamic reconfiguration generation" },
3240eb822a1Scindi { "topo.gen", FMD_TYPE_UINT64, "topology snapshot generation" },
3250eb822a1Scindi { "topo.drgen", FMD_TYPE_UINT64, "current topology DR generation number" },
3267c478bd9Sstevel@tonic-gate };
3277c478bd9Sstevel@tonic-gate 
3286e1fa242SStephen Hanson /*
3296e1fa242SStephen Hanson  * SMBIOS serial numbers can contain characters (particularly ':' and ' ')
3306e1fa242SStephen Hanson  * that are invalid for the authority and can break FMRI parsing.  We translate
3316e1fa242SStephen Hanson  * any invalid characters to a safe '-', as well as trimming any leading or
3326e1fa242SStephen Hanson  * trailing whitespace.  Similarly, '/' can be found in some product names
3336e1fa242SStephen Hanson  * so we translate that to '-'.
3346e1fa242SStephen Hanson  */
3356e1fa242SStephen Hanson void
fmd_cleanup_auth_str(char * buf,const char * begin)3366e1fa242SStephen Hanson fmd_cleanup_auth_str(char *buf, const char *begin)
3376e1fa242SStephen Hanson {
3386e1fa242SStephen Hanson 	const char *end, *cp;
3396e1fa242SStephen Hanson 	char c;
3406e1fa242SStephen Hanson 	int i;
3416e1fa242SStephen Hanson 
3426e1fa242SStephen Hanson 	end = begin + strlen(begin);
3436e1fa242SStephen Hanson 
3446e1fa242SStephen Hanson 	while (begin < end && isspace(*begin))
3456e1fa242SStephen Hanson 		begin++;
3466e1fa242SStephen Hanson 	while (begin < end && isspace(*(end - 1)))
3476e1fa242SStephen Hanson 		end--;
3486e1fa242SStephen Hanson 
3496e1fa242SStephen Hanson 	if (begin >= end)
3506e1fa242SStephen Hanson 		return;
3516e1fa242SStephen Hanson 
3526e1fa242SStephen Hanson 	cp = begin;
3536e1fa242SStephen Hanson 	for (i = 0; i < MAXNAMELEN - 1; i++) {
3546e1fa242SStephen Hanson 		if (cp >= end)
3556e1fa242SStephen Hanson 			break;
3566e1fa242SStephen Hanson 		c = *cp;
3576e1fa242SStephen Hanson 		if (c == ':' || c == '=' || c == '/' || isspace(c) ||
3586e1fa242SStephen Hanson 		    !isprint(c))
3596e1fa242SStephen Hanson 			buf[i] = '-';
3606e1fa242SStephen Hanson 		else
3616e1fa242SStephen Hanson 			buf[i] = c;
3626e1fa242SStephen Hanson 		cp++;
3636e1fa242SStephen Hanson 	}
3646e1fa242SStephen Hanson 	buf[i] = 0;
3656e1fa242SStephen Hanson }
3666e1fa242SStephen Hanson 
3677c478bd9Sstevel@tonic-gate void
fmd_create(fmd_t * dp,const char * arg0,const char * root,const char * conf)3687c478bd9Sstevel@tonic-gate fmd_create(fmd_t *dp, const char *arg0, const char *root, const char *conf)
3697c478bd9Sstevel@tonic-gate {
3707c478bd9Sstevel@tonic-gate 	fmd_conf_path_t *pap;
3717c478bd9Sstevel@tonic-gate 	char file[PATH_MAX];
3729c94f155SCheng Sean Ye 	const char *name, *psn, *csn;
3737c478bd9Sstevel@tonic-gate 	fmd_stat_t *sp;
3747c478bd9Sstevel@tonic-gate 	int i;
3757c478bd9Sstevel@tonic-gate 
3767aec1d6eScindi 	smbios_hdl_t *shp;
3777aec1d6eScindi 	smbios_system_t s1;
3787aec1d6eScindi 	smbios_info_t s2;
3797aec1d6eScindi 	id_t id;
3807aec1d6eScindi 
38182d33c01Sav145390 	di_prom_handle_t promh = DI_PROM_HANDLE_NIL;
38282d33c01Sav145390 	di_node_t rooth = DI_NODE_NIL;
38382d33c01Sav145390 	char *bufp;
38482d33c01Sav145390 
3857c478bd9Sstevel@tonic-gate 	(void) sysinfo(SI_PLATFORM, _fmd_plat, sizeof (_fmd_plat));
3867c478bd9Sstevel@tonic-gate 	(void) sysinfo(SI_ARCHITECTURE, _fmd_isa, sizeof (_fmd_isa));
3877c478bd9Sstevel@tonic-gate 	(void) uname(&_fmd_uts);
3887c478bd9Sstevel@tonic-gate 
3897aec1d6eScindi 	if ((shp = smbios_open(NULL, SMB_VERSION, 0, NULL)) != NULL) {
3907aec1d6eScindi 		if ((id = smbios_info_system(shp, &s1)) != SMB_ERR &&
3919c94f155SCheng Sean Ye 		    smbios_info_common(shp, id, &s2) != SMB_ERR)
3926e1fa242SStephen Hanson 			fmd_cleanup_auth_str(_fmd_prod, s2.smbi_product);
3939c94f155SCheng Sean Ye 
3949c94f155SCheng Sean Ye 		if ((psn = smbios_psn(shp)) != NULL)
3959c94f155SCheng Sean Ye 			fmd_cleanup_auth_str(_fmd_psn, psn);
3969c94f155SCheng Sean Ye 
3979c94f155SCheng Sean Ye 		if ((csn = smbios_csn(shp)) != NULL)
3989c94f155SCheng Sean Ye 			fmd_cleanup_auth_str(_fmd_csn, csn);
3999c94f155SCheng Sean Ye 
4007aec1d6eScindi 		smbios_close(shp);
401384fce58Shueston 	} else if ((rooth = di_init("/", DINFOPROP)) != DI_NODE_NIL &&
40282d33c01Sav145390 	    (promh = di_prom_init()) != DI_PROM_HANDLE_NIL) {
40382d33c01Sav145390 		if (di_prom_prop_lookup_bytes(promh, rooth, "chassis-sn",
40482d33c01Sav145390 		    (unsigned char **)&bufp) != -1) {
4056e1fa242SStephen Hanson 			fmd_cleanup_auth_str(_fmd_csn, bufp);
4067aec1d6eScindi 		}
40782d33c01Sav145390 	}
40882d33c01Sav145390 
40982d33c01Sav145390 	if (promh != DI_PROM_HANDLE_NIL)
41082d33c01Sav145390 		di_prom_fini(promh);
41182d33c01Sav145390 	if (rooth != DI_NODE_NIL)
41282d33c01Sav145390 		di_fini(rooth);
4137aec1d6eScindi 
4147c478bd9Sstevel@tonic-gate 	bzero(dp, sizeof (fmd_t));
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate 	dp->d_version = _fmd_version;
4177c478bd9Sstevel@tonic-gate 	dp->d_pname = fmd_strbasename(arg0);
4187c478bd9Sstevel@tonic-gate 	dp->d_pid = getpid();
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate 	if (pthread_key_create(&dp->d_key, NULL) != 0)
4217c478bd9Sstevel@tonic-gate 		fmd_error(EFMD_EXIT, "failed to create pthread key");
4227c478bd9Sstevel@tonic-gate 
4237c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_init(&dp->d_xprt_lock, NULL);
4247c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_init(&dp->d_err_lock, NULL);
4257c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_init(&dp->d_thr_lock, NULL);
4267c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_init(&dp->d_mod_lock, NULL);
4277c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_init(&dp->d_stats_lock, NULL);
4280eb822a1Scindi 	(void) pthread_mutex_init(&dp->d_topo_lock, NULL);
4297c478bd9Sstevel@tonic-gate 	(void) pthread_rwlock_init(&dp->d_log_lock, NULL);
430f6e214c7SGavin Maltby 	(void) pthread_rwlock_init(&dp->d_hvilog_lock, NULL);
431f6e214c7SGavin Maltby 	(void) pthread_rwlock_init(&dp->d_ilog_lock, NULL);
43293136fe2Sstephh 	(void) pthread_mutex_init(&dp->d_fmd_lock, NULL);
43393136fe2Sstephh 	(void) pthread_cond_init(&dp->d_fmd_cv, NULL);
4347c478bd9Sstevel@tonic-gate 
4357c478bd9Sstevel@tonic-gate 	/*
4367c478bd9Sstevel@tonic-gate 	 * A small number of properties must be set manually before we open
4377c478bd9Sstevel@tonic-gate 	 * the root configuration file.  These include any settings for our
4387c478bd9Sstevel@tonic-gate 	 * memory allocator and path expansion token values, because these
4397c478bd9Sstevel@tonic-gate 	 * values are needed by the routines in fmd_conf.c itself.  After
4407c478bd9Sstevel@tonic-gate 	 * the root configuration file is processed, we reset these properties
4417c478bd9Sstevel@tonic-gate 	 * based upon the latest values from the configuration file.
4427c478bd9Sstevel@tonic-gate 	 */
4437c478bd9Sstevel@tonic-gate 	dp->d_alloc_msecs = 10;
4447c478bd9Sstevel@tonic-gate 	dp->d_alloc_tries = 3;
4457c478bd9Sstevel@tonic-gate 	dp->d_str_buckets = 211;
4467c478bd9Sstevel@tonic-gate 
4477c478bd9Sstevel@tonic-gate 	dp->d_rootdir = root ? root : "";
4487c478bd9Sstevel@tonic-gate 	dp->d_platform = _fmd_plat;
4497c478bd9Sstevel@tonic-gate 	dp->d_machine = _fmd_uts.machine;
4507c478bd9Sstevel@tonic-gate 	dp->d_isaname = _fmd_isa;
4517c478bd9Sstevel@tonic-gate 
452d9638e54Smws 	dp->d_conf = fmd_conf_open(conf, sizeof (_fmd_conf) /
453d9638e54Smws 	    sizeof (_fmd_conf[0]), _fmd_conf, FMD_CONF_DEFER);
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate 	if (dp->d_conf == NULL) {
4567c478bd9Sstevel@tonic-gate 		fmd_error(EFMD_EXIT,
4577c478bd9Sstevel@tonic-gate 		    "failed to load required configuration properties\n");
4587c478bd9Sstevel@tonic-gate 	}
4597c478bd9Sstevel@tonic-gate 
4607c478bd9Sstevel@tonic-gate 	(void) fmd_conf_getprop(dp->d_conf, "alloc.msecs", &dp->d_alloc_msecs);
4617c478bd9Sstevel@tonic-gate 	(void) fmd_conf_getprop(dp->d_conf, "alloc.tries", &dp->d_alloc_tries);
4627c478bd9Sstevel@tonic-gate 	(void) fmd_conf_getprop(dp->d_conf, "strbuckets", &dp->d_str_buckets);
4637c478bd9Sstevel@tonic-gate 
4647c478bd9Sstevel@tonic-gate 	(void) fmd_conf_getprop(dp->d_conf, "platform", &dp->d_platform);
4657c478bd9Sstevel@tonic-gate 	(void) fmd_conf_getprop(dp->d_conf, "machine", &dp->d_machine);
4667c478bd9Sstevel@tonic-gate 	(void) fmd_conf_getprop(dp->d_conf, "isaname", &dp->d_isaname);
4677c478bd9Sstevel@tonic-gate 
4687c478bd9Sstevel@tonic-gate 	/*
4697c478bd9Sstevel@tonic-gate 	 * Manually specified rootdirs override config files, so only update
4707c478bd9Sstevel@tonic-gate 	 * d_rootdir based on the config files we parsed if no 'root' was set.
4717c478bd9Sstevel@tonic-gate 	 */
4727c478bd9Sstevel@tonic-gate 	if (root == NULL)
4737c478bd9Sstevel@tonic-gate 		(void) fmd_conf_getprop(dp->d_conf, "rootdir", &dp->d_rootdir);
4747c478bd9Sstevel@tonic-gate 	else
4757c478bd9Sstevel@tonic-gate 		(void) fmd_conf_setprop(dp->d_conf, "rootdir", dp->d_rootdir);
4767c478bd9Sstevel@tonic-gate 
4777c478bd9Sstevel@tonic-gate 	/*
4787c478bd9Sstevel@tonic-gate 	 * Once the base conf file properties are loaded, lookup the values
4797c478bd9Sstevel@tonic-gate 	 * of $conf_path and $conf_file and merge in any other conf files.
4807c478bd9Sstevel@tonic-gate 	 */
4817c478bd9Sstevel@tonic-gate 	(void) fmd_conf_getprop(dp->d_conf, "conf_path", &pap);
4827c478bd9Sstevel@tonic-gate 	(void) fmd_conf_getprop(dp->d_conf, "conf_file", &name);
4837c478bd9Sstevel@tonic-gate 
4847c478bd9Sstevel@tonic-gate 	for (i = 0; i < pap->cpa_argc; i++) {
4857c478bd9Sstevel@tonic-gate 		(void) snprintf(file, sizeof (file),
4867c478bd9Sstevel@tonic-gate 		    "%s/%s", pap->cpa_argv[i], name);
4877c478bd9Sstevel@tonic-gate 		if (access(file, F_OK) == 0)
4887c478bd9Sstevel@tonic-gate 			fmd_conf_merge(dp->d_conf, file);
4897c478bd9Sstevel@tonic-gate 	}
4907c478bd9Sstevel@tonic-gate 
4917c478bd9Sstevel@tonic-gate 	/*
4927c478bd9Sstevel@tonic-gate 	 * Update the value of fmd.d_fg based on "fg".  We cache this property
4937c478bd9Sstevel@tonic-gate 	 * because it must be accessed deep within fmd at fmd_verror() time.
494d9638e54Smws 	 * Update any other properties that must be cached for performance.
4957c478bd9Sstevel@tonic-gate 	 */
4967c478bd9Sstevel@tonic-gate 	(void) fmd_conf_getprop(fmd.d_conf, "fg", &fmd.d_fg);
497d9638e54Smws 	(void) fmd_conf_getprop(fmd.d_conf, "xprt.ttl", &fmd.d_xprt_ttl);
4987c478bd9Sstevel@tonic-gate 
4997c478bd9Sstevel@tonic-gate 	/*
5007c478bd9Sstevel@tonic-gate 	 * Initialize our custom libnvpair allocator and create an nvlist for
5017c478bd9Sstevel@tonic-gate 	 * authority elements corresponding to this instance of the daemon.
5027c478bd9Sstevel@tonic-gate 	 */
5037c478bd9Sstevel@tonic-gate 	(void) nv_alloc_init(&dp->d_nva, &fmd_nv_alloc_ops);
5047c478bd9Sstevel@tonic-gate 	dp->d_auth = fmd_protocol_authority();
5057c478bd9Sstevel@tonic-gate 
5067c478bd9Sstevel@tonic-gate 	/*
5077c478bd9Sstevel@tonic-gate 	 * The fmd_module_t for the root module must be created manually.  Most
5087c478bd9Sstevel@tonic-gate 	 * of it remains unused and zero, except for the few things we fill in.
5097c478bd9Sstevel@tonic-gate 	 */
5107c478bd9Sstevel@tonic-gate 	dp->d_rmod = fmd_zalloc(sizeof (fmd_module_t), FMD_SLEEP);
5117c478bd9Sstevel@tonic-gate 	dp->d_rmod->mod_name = fmd_strdup(dp->d_pname, FMD_SLEEP);
512d9638e54Smws 	dp->d_rmod->mod_fmri = fmd_protocol_fmri_module(dp->d_rmod);
513d9638e54Smws 
5147c478bd9Sstevel@tonic-gate 	fmd_list_append(&dp->d_mod_list, dp->d_rmod);
515d9638e54Smws 	fmd_module_hold(dp->d_rmod);
5167c478bd9Sstevel@tonic-gate 
5177c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_init(&dp->d_rmod->mod_lock, NULL);
5187c478bd9Sstevel@tonic-gate 	(void) pthread_cond_init(&dp->d_rmod->mod_cv, NULL);
519d9638e54Smws 	(void) pthread_mutex_init(&dp->d_rmod->mod_stats_lock, NULL);
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate 	dp->d_rmod->mod_thread = fmd_thread_xcreate(dp->d_rmod, pthread_self());
522d9638e54Smws 	dp->d_rmod->mod_stats = fmd_zalloc(sizeof (fmd_modstat_t), FMD_SLEEP);
5237c478bd9Sstevel@tonic-gate 	dp->d_rmod->mod_ustat = fmd_ustat_create();
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate 	if (pthread_setspecific(dp->d_key, dp->d_rmod->mod_thread) != 0)
5267c478bd9Sstevel@tonic-gate 		fmd_error(EFMD_EXIT, "failed to attach main thread key");
5277c478bd9Sstevel@tonic-gate 
5287c478bd9Sstevel@tonic-gate 	if ((dp->d_stats = (fmd_statistics_t *)fmd_ustat_insert(
5297c478bd9Sstevel@tonic-gate 	    dp->d_rmod->mod_ustat, FMD_USTAT_NOALLOC, sizeof (_fmd_stats) /
5307c478bd9Sstevel@tonic-gate 	    sizeof (fmd_stat_t), (fmd_stat_t *)&_fmd_stats, NULL)) == NULL)
5317c478bd9Sstevel@tonic-gate 		fmd_error(EFMD_EXIT, "failed to initialize statistics");
5327c478bd9Sstevel@tonic-gate 
533d9638e54Smws 	(void) pthread_mutex_lock(&dp->d_rmod->mod_lock);
534d9638e54Smws 	dp->d_rmod->mod_flags |= FMD_MOD_INIT;
535d9638e54Smws 	(void) pthread_mutex_unlock(&dp->d_rmod->mod_lock);
536d9638e54Smws 
5377c478bd9Sstevel@tonic-gate 	/*
5387c478bd9Sstevel@tonic-gate 	 * In addition to inserting the _fmd_stats collection of program-wide
5397c478bd9Sstevel@tonic-gate 	 * statistics, we also insert a statistic named after each of our
5407c478bd9Sstevel@tonic-gate 	 * errors and update these counts in fmd_verror() (see fmd_subr.c).
5417c478bd9Sstevel@tonic-gate 	 */
5427c478bd9Sstevel@tonic-gate 	dp->d_errstats = sp = fmd_zalloc(sizeof (fmd_stat_t) *
5437c478bd9Sstevel@tonic-gate 	    (EFMD_END - EFMD_UNKNOWN), FMD_SLEEP);
5447c478bd9Sstevel@tonic-gate 
5457c478bd9Sstevel@tonic-gate 	for (i = 0; i < EFMD_END - EFMD_UNKNOWN; i++, sp++) {
5467c478bd9Sstevel@tonic-gate 		(void) snprintf(sp->fmds_name, sizeof (sp->fmds_name), "err.%s",
5477c478bd9Sstevel@tonic-gate 		    strrchr(fmd_errclass(EFMD_UNKNOWN + i), '.') + 1);
5487c478bd9Sstevel@tonic-gate 		sp->fmds_type = FMD_TYPE_UINT64;
5497c478bd9Sstevel@tonic-gate 	}
5507c478bd9Sstevel@tonic-gate 
5517c478bd9Sstevel@tonic-gate 	(void) fmd_ustat_insert(dp->d_rmod->mod_ustat, FMD_USTAT_NOALLOC,
5527c478bd9Sstevel@tonic-gate 	    EFMD_END - EFMD_UNKNOWN, dp->d_errstats, NULL);
5537c478bd9Sstevel@tonic-gate }
5547c478bd9Sstevel@tonic-gate 
5557c478bd9Sstevel@tonic-gate void
fmd_destroy(fmd_t * dp)5567c478bd9Sstevel@tonic-gate fmd_destroy(fmd_t *dp)
5577c478bd9Sstevel@tonic-gate {
5587c478bd9Sstevel@tonic-gate 	fmd_module_t *mp;
559d9638e54Smws 	fmd_case_t *cp;
5607c478bd9Sstevel@tonic-gate 	int core;
5617c478bd9Sstevel@tonic-gate 
5627c478bd9Sstevel@tonic-gate 	(void) fmd_conf_getprop(fmd.d_conf, "core", &core);
5637c478bd9Sstevel@tonic-gate 
5647c478bd9Sstevel@tonic-gate 	fmd_rpc_fini();
565e4586ebfSmws 
566e4586ebfSmws 	if (dp->d_xprt_ids != NULL)
567d9638e54Smws 		fmd_xprt_suspend_all();
5687c478bd9Sstevel@tonic-gate 
5697c478bd9Sstevel@tonic-gate 	/*
5707c478bd9Sstevel@tonic-gate 	 * Unload the self-diagnosis module first.  This ensures that it does
5717c478bd9Sstevel@tonic-gate 	 * not get confused as we start unloading other modules, etc.  We must
5727c478bd9Sstevel@tonic-gate 	 * hold the dispq lock as a writer while doing so since it uses d_self.
5737c478bd9Sstevel@tonic-gate 	 */
5747c478bd9Sstevel@tonic-gate 	if (dp->d_self != NULL) {
57500958acdScindi 		fmd_module_t *self;
57600958acdScindi 
5777c478bd9Sstevel@tonic-gate 		(void) pthread_rwlock_wrlock(&dp->d_disp->dq_lock);
57800958acdScindi 		self = dp->d_self;
5797c478bd9Sstevel@tonic-gate 		dp->d_self = NULL;
5807c478bd9Sstevel@tonic-gate 		(void) pthread_rwlock_unlock(&dp->d_disp->dq_lock);
58100958acdScindi 
58200958acdScindi 		fmd_module_unload(self);
58300958acdScindi 		fmd_module_rele(self);
5847c478bd9Sstevel@tonic-gate 	}
5857c478bd9Sstevel@tonic-gate 
5867c478bd9Sstevel@tonic-gate 	/*
5877c478bd9Sstevel@tonic-gate 	 * Unload modules in reverse order *except* for the root module, which
5887c478bd9Sstevel@tonic-gate 	 * is first in the list.  This allows it to keep its thread and trace.
5897c478bd9Sstevel@tonic-gate 	 */
5907c478bd9Sstevel@tonic-gate 	for (mp = fmd_list_prev(&dp->d_mod_list); mp != dp->d_rmod; ) {
5917c478bd9Sstevel@tonic-gate 		fmd_module_unload(mp);
5927c478bd9Sstevel@tonic-gate 		mp = fmd_list_prev(mp);
5937c478bd9Sstevel@tonic-gate 	}
5947c478bd9Sstevel@tonic-gate 
5957c478bd9Sstevel@tonic-gate 	if (dp->d_mod_hash != NULL) {
5967c478bd9Sstevel@tonic-gate 		fmd_modhash_destroy(dp->d_mod_hash);
5977c478bd9Sstevel@tonic-gate 		dp->d_mod_hash = NULL;
5987c478bd9Sstevel@tonic-gate 	}
5997c478bd9Sstevel@tonic-gate 
6007c478bd9Sstevel@tonic-gate 	/*
6017c478bd9Sstevel@tonic-gate 	 * Close both log files now that modules are no longer active.  We must
6027c478bd9Sstevel@tonic-gate 	 * set these pointers to NULL in case any subsequent errors occur.
6037c478bd9Sstevel@tonic-gate 	 */
6047c478bd9Sstevel@tonic-gate 	if (dp->d_errlog != NULL) {
6057c478bd9Sstevel@tonic-gate 		fmd_log_rele(dp->d_errlog);
6067c478bd9Sstevel@tonic-gate 		dp->d_errlog = NULL;
6077c478bd9Sstevel@tonic-gate 	}
6087c478bd9Sstevel@tonic-gate 
6097c478bd9Sstevel@tonic-gate 	if (dp->d_fltlog != NULL) {
6107c478bd9Sstevel@tonic-gate 		fmd_log_rele(dp->d_fltlog);
6117c478bd9Sstevel@tonic-gate 		dp->d_fltlog = NULL;
6127c478bd9Sstevel@tonic-gate 	}
6137c478bd9Sstevel@tonic-gate 
6147c478bd9Sstevel@tonic-gate 	/*
615d9638e54Smws 	 * Now destroy the resource cache: each ASRU contains a case reference,
616d9638e54Smws 	 * which may in turn contain a pointer to a referenced owning module.
617d9638e54Smws 	 */
618d9638e54Smws 	if (dp->d_asrus != NULL) {
619d9638e54Smws 		fmd_asru_hash_destroy(dp->d_asrus);
620d9638e54Smws 		dp->d_asrus = NULL;
621d9638e54Smws 	}
622d9638e54Smws 
623d9638e54Smws 	/*
6247c478bd9Sstevel@tonic-gate 	 * Now that all data structures that refer to modules are torn down,
6257c478bd9Sstevel@tonic-gate 	 * no modules should be remaining on the module list except for d_rmod.
6267c478bd9Sstevel@tonic-gate 	 * If we trip one of these assertions, we're missing a rele somewhere.
6277c478bd9Sstevel@tonic-gate 	 */
6287c478bd9Sstevel@tonic-gate 	ASSERT(fmd_list_prev(&dp->d_mod_list) == dp->d_rmod);
6297c478bd9Sstevel@tonic-gate 	ASSERT(fmd_list_next(&dp->d_mod_list) == dp->d_rmod);
6307c478bd9Sstevel@tonic-gate 
6317c478bd9Sstevel@tonic-gate 	/*
6327c478bd9Sstevel@tonic-gate 	 * Now destroy the root module.  We clear its thread key first so any
6337c478bd9Sstevel@tonic-gate 	 * calls to fmd_trace() inside of the module code will be ignored.
6347c478bd9Sstevel@tonic-gate 	 */
6357c478bd9Sstevel@tonic-gate 	(void) pthread_setspecific(dp->d_key, NULL);
636d9638e54Smws 	fmd_module_lock(dp->d_rmod);
6377c478bd9Sstevel@tonic-gate 
638d9638e54Smws 	while ((cp = fmd_list_next(&dp->d_rmod->mod_cases)) != NULL)
639cbf75e67SStephen Hanson 		fmd_case_discard(cp, B_FALSE);
640d9638e54Smws 
641d9638e54Smws 	fmd_module_unlock(dp->d_rmod);
642d9638e54Smws 	fmd_free(dp->d_rmod->mod_stats, sizeof (fmd_modstat_t));
643d9638e54Smws 	dp->d_rmod->mod_stats = NULL;
644d9638e54Smws 
645d9638e54Smws 	(void) pthread_mutex_lock(&dp->d_rmod->mod_lock);
646d9638e54Smws 	dp->d_rmod->mod_flags |= FMD_MOD_FINI;
647d9638e54Smws 	(void) pthread_mutex_unlock(&dp->d_rmod->mod_lock);
648d9638e54Smws 
649d9638e54Smws 	fmd_module_rele(dp->d_rmod);
650d9638e54Smws 	ASSERT(fmd_list_next(&dp->d_mod_list) == NULL);
651d9638e54Smws 
652d9638e54Smws 	/*
653d9638e54Smws 	 * Now destroy the remaining global data structures.  If 'core' was
654d9638e54Smws 	 * set to true, force a core dump so we can check for memory leaks.
655d9638e54Smws 	 */
6567c478bd9Sstevel@tonic-gate 	if (dp->d_cases != NULL)
6577c478bd9Sstevel@tonic-gate 		fmd_case_hash_destroy(dp->d_cases);
658d9638e54Smws 	if (dp->d_disp != NULL)
659d9638e54Smws 		fmd_dispq_destroy(dp->d_disp);
660d9638e54Smws 	if (dp->d_timers != NULL)
661d9638e54Smws 		fmd_timerq_destroy(dp->d_timers);
662d9638e54Smws 	if (dp->d_schemes != NULL)
663d9638e54Smws 		fmd_scheme_hash_destroy(dp->d_schemes);
664d9638e54Smws 	if (dp->d_xprt_ids != NULL)
665d9638e54Smws 		fmd_idspace_destroy(dp->d_xprt_ids);
6667c478bd9Sstevel@tonic-gate 
6677c478bd9Sstevel@tonic-gate 	if (dp->d_errstats != NULL) {
6687c478bd9Sstevel@tonic-gate 		fmd_free(dp->d_errstats,
6697c478bd9Sstevel@tonic-gate 		    sizeof (fmd_stat_t) * (EFMD_END - EFMD_UNKNOWN));
6707c478bd9Sstevel@tonic-gate 	}
6717c478bd9Sstevel@tonic-gate 
6727c478bd9Sstevel@tonic-gate 	if (dp->d_conf != NULL)
6737c478bd9Sstevel@tonic-gate 		fmd_conf_close(dp->d_conf);
6747c478bd9Sstevel@tonic-gate 
6750eb822a1Scindi 	fmd_topo_fini();
6767aec1d6eScindi 
6777c478bd9Sstevel@tonic-gate 	nvlist_free(dp->d_auth);
6787c478bd9Sstevel@tonic-gate 	(void) nv_alloc_fini(&dp->d_nva);
6797c478bd9Sstevel@tonic-gate 	dp->d_clockops->fto_fini(dp->d_clockptr);
6807c478bd9Sstevel@tonic-gate 
6817c478bd9Sstevel@tonic-gate 	(void) pthread_key_delete(dp->d_key);
6827c478bd9Sstevel@tonic-gate 	bzero(dp, sizeof (fmd_t));
6837c478bd9Sstevel@tonic-gate 
6847c478bd9Sstevel@tonic-gate 	if (core)
6857c478bd9Sstevel@tonic-gate 		fmd_panic("forcing core dump at user request\n");
6867c478bd9Sstevel@tonic-gate }
6877c478bd9Sstevel@tonic-gate 
6887c478bd9Sstevel@tonic-gate /*ARGSUSED*/
6897c478bd9Sstevel@tonic-gate static void
fmd_gc(fmd_t * dp,id_t id,hrtime_t hrt)6907c478bd9Sstevel@tonic-gate fmd_gc(fmd_t *dp, id_t id, hrtime_t hrt)
6917c478bd9Sstevel@tonic-gate {
6927c478bd9Sstevel@tonic-gate 	hrtime_t delta;
6937c478bd9Sstevel@tonic-gate 
6947c478bd9Sstevel@tonic-gate 	if (id != 0) {
6957c478bd9Sstevel@tonic-gate 		TRACE((FMD_DBG_MOD, "garbage collect start"));
6967c478bd9Sstevel@tonic-gate 		fmd_modhash_apply(dp->d_mod_hash, fmd_module_gc);
6977c478bd9Sstevel@tonic-gate 		TRACE((FMD_DBG_MOD, "garbage collect end"));
6987c478bd9Sstevel@tonic-gate 
6997c478bd9Sstevel@tonic-gate 		(void) pthread_rwlock_rdlock(&dp->d_log_lock);
7007c478bd9Sstevel@tonic-gate 		fmd_log_update(dp->d_errlog);
7017c478bd9Sstevel@tonic-gate 		(void) pthread_rwlock_unlock(&dp->d_log_lock);
702f6e214c7SGavin Maltby 
703f6e214c7SGavin Maltby 		(void) pthread_rwlock_rdlock(&dp->d_hvilog_lock);
704f6e214c7SGavin Maltby 		fmd_log_update(dp->d_hvilog);
705f6e214c7SGavin Maltby 		(void) pthread_rwlock_unlock(&dp->d_hvilog_lock);
706f6e214c7SGavin Maltby 
707f6e214c7SGavin Maltby 		(void) pthread_rwlock_rdlock(&dp->d_ilog_lock);
708f6e214c7SGavin Maltby 		fmd_log_update(dp->d_ilog);
709f6e214c7SGavin Maltby 		(void) pthread_rwlock_unlock(&dp->d_ilog_lock);
7107c478bd9Sstevel@tonic-gate 	}
7117c478bd9Sstevel@tonic-gate 
7127c478bd9Sstevel@tonic-gate 	(void) fmd_conf_getprop(dp->d_conf, "gc_interval", &delta);
7137c478bd9Sstevel@tonic-gate 	(void) fmd_timerq_install(dp->d_timers, dp->d_rmod->mod_timerids,
7147c478bd9Sstevel@tonic-gate 	    (fmd_timer_f *)fmd_gc, dp, NULL, delta);
7157c478bd9Sstevel@tonic-gate }
7167c478bd9Sstevel@tonic-gate 
717567cc2e6Sstephh /*ARGSUSED*/
718567cc2e6Sstephh static void
fmd_clear_aged_rsrcs(fmd_t * dp,id_t id,hrtime_t hrt)719567cc2e6Sstephh fmd_clear_aged_rsrcs(fmd_t *dp, id_t id, hrtime_t hrt)
720567cc2e6Sstephh {
7215750ef5cSStephen Hanson 	hrtime_t period;
722567cc2e6Sstephh 
723567cc2e6Sstephh 	fmd_asru_clear_aged_rsrcs();
7245750ef5cSStephen Hanson 	(void) fmd_conf_getprop(dp->d_conf, "rsrc.pollperiod", &period);
725567cc2e6Sstephh 	(void) fmd_timerq_install(dp->d_timers, dp->d_rmod->mod_timerids,
7265750ef5cSStephen Hanson 	    (fmd_timer_f *)fmd_clear_aged_rsrcs, dp, NULL, period);
727567cc2e6Sstephh }
728567cc2e6Sstephh 
7297c478bd9Sstevel@tonic-gate /*
7307c478bd9Sstevel@tonic-gate  * Events are committed to the errlog after cases are checkpointed.  If fmd
7317c478bd9Sstevel@tonic-gate  * crashes before an event is ever associated with a module, this function will
7327c478bd9Sstevel@tonic-gate  * be called to replay it to all subscribers.  If fmd crashes in between the
7337c478bd9Sstevel@tonic-gate  * subscriber checkpointing and committing the event in the error log, the
7347c478bd9Sstevel@tonic-gate  * module will have seen the event and we don't want to replay it.  So we look
7357c478bd9Sstevel@tonic-gate  * for the event in all modules and transition it to the proper state.  If
7367c478bd9Sstevel@tonic-gate  * it is found, we commit it to the error log and do not replay it.  The in-
7377c478bd9Sstevel@tonic-gate  * memory case search used by fmd_module_contains() et al isn't particularly
7387c478bd9Sstevel@tonic-gate  * efficient, but it is faster than doing read i/o's on every case event to
7397c478bd9Sstevel@tonic-gate  * check their status or write i/o's on every event to replay to update states.
7407c478bd9Sstevel@tonic-gate  * We can improve the efficiency of this lookup algorithm later if necessary.
7417c478bd9Sstevel@tonic-gate  */
7427c478bd9Sstevel@tonic-gate /*ARGSUSED*/
7437c478bd9Sstevel@tonic-gate static void
fmd_err_replay(fmd_log_t * lp,fmd_event_t * ep,fmd_t * dp)7447c478bd9Sstevel@tonic-gate fmd_err_replay(fmd_log_t *lp, fmd_event_t *ep, fmd_t *dp)
7457c478bd9Sstevel@tonic-gate {
7467c478bd9Sstevel@tonic-gate 	fmd_module_t *mp;
7477c478bd9Sstevel@tonic-gate 	fmd_stat_t *sp;
7487c478bd9Sstevel@tonic-gate 
7497c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&dp->d_mod_lock);
7507c478bd9Sstevel@tonic-gate 
7517c478bd9Sstevel@tonic-gate 	for (mp = fmd_list_next(&dp->d_mod_list);
7527c478bd9Sstevel@tonic-gate 	    mp != NULL; mp = fmd_list_next(mp)) {
7537c478bd9Sstevel@tonic-gate 		if (fmd_module_contains(mp, ep)) {
7547c478bd9Sstevel@tonic-gate 			fmd_module_hold(mp);
7557c478bd9Sstevel@tonic-gate 			break;
7567c478bd9Sstevel@tonic-gate 		}
7577c478bd9Sstevel@tonic-gate 	}
7587c478bd9Sstevel@tonic-gate 
7597c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&dp->d_mod_lock);
7607c478bd9Sstevel@tonic-gate 
7617c478bd9Sstevel@tonic-gate 	if (mp != NULL) {
7627c478bd9Sstevel@tonic-gate 		fmd_event_commit(ep);
7637c478bd9Sstevel@tonic-gate 		fmd_module_rele(mp);
7647c478bd9Sstevel@tonic-gate 		sp = &dp->d_stats->ds_log_partials;
7657c478bd9Sstevel@tonic-gate 	} else {
766d9638e54Smws 		fmd_dispq_dispatch(dp->d_disp, ep, FMD_EVENT_DATA(ep));
7677c478bd9Sstevel@tonic-gate 		sp = &dp->d_stats->ds_log_replayed;
7687c478bd9Sstevel@tonic-gate 	}
7697c478bd9Sstevel@tonic-gate 
7707c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&dp->d_stats_lock);
7717c478bd9Sstevel@tonic-gate 	sp->fmds_value.ui64++;
7727c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&dp->d_stats_lock);
7737c478bd9Sstevel@tonic-gate }
7747c478bd9Sstevel@tonic-gate 
775d9638e54Smws void
fmd_door_server(void * dip)776d9638e54Smws fmd_door_server(void *dip)
777d9638e54Smws {
778d9638e54Smws 	fmd_dprintf(FMD_DBG_XPRT, "door server starting for %p\n", dip);
779d9638e54Smws 	(void) pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
780d9638e54Smws 	(void) door_return(NULL, 0, NULL, 0);
781d9638e54Smws }
782d9638e54Smws 
783d9638e54Smws /*
784d9638e54Smws  * Custom door server create callback.  Any fmd services that use doors will
785d9638e54Smws  * require those threads to have their fmd-specific TSD initialized, etc.
786f6e214c7SGavin Maltby  * Modules should use door_xcreate and derivatives such as
787f6e214c7SGavin Maltby  * sysevent_evc_xsubscribe in order to use private doors that
788f6e214c7SGavin Maltby  * avoid this global door server function (see fmd_api_module comments).
789d9638e54Smws  */
790d9638e54Smws static void
fmd_door(door_info_t * dip)791d9638e54Smws fmd_door(door_info_t *dip)
792d9638e54Smws {
793d9638e54Smws 	if (fmd_thread_create(fmd.d_rmod, fmd_door_server, dip) == NULL)
794d9638e54Smws 		fmd_panic("failed to create server for door %p", (void *)dip);
795d9638e54Smws }
796d9638e54Smws 
7977c478bd9Sstevel@tonic-gate /*
7987c478bd9Sstevel@tonic-gate  * This signal handler is installed for the client.thrsig signal to be used to
7997c478bd9Sstevel@tonic-gate  * force an auxiliary thread to wake up from a system call and return EINTR in
8007c478bd9Sstevel@tonic-gate  * response to a module's use of fmd_thr_signal().  We also trace the event.
8017c478bd9Sstevel@tonic-gate  */
8027c478bd9Sstevel@tonic-gate static void
fmd_signal(int sig)8037c478bd9Sstevel@tonic-gate fmd_signal(int sig)
8047c478bd9Sstevel@tonic-gate {
8057c478bd9Sstevel@tonic-gate 	TRACE((FMD_DBG_MOD, "module thread received sig #%d", sig));
8067c478bd9Sstevel@tonic-gate }
8077c478bd9Sstevel@tonic-gate 
8087c478bd9Sstevel@tonic-gate void
fmd_run(fmd_t * dp,int pfd)8097c478bd9Sstevel@tonic-gate fmd_run(fmd_t *dp, int pfd)
8107c478bd9Sstevel@tonic-gate {
8117c478bd9Sstevel@tonic-gate 	char *nodc_key[] = { FMD_FLT_NODC, NULL };
812627351e3Scy152378 	char *repair_key[] = { FM_LIST_REPAIRED_CLASS, NULL };
81325c6ff4bSstephh 	char *resolve_key[] = { FM_LIST_RESOLVED_CLASS, NULL };
81425c6ff4bSstephh 	char *update_key[] = { FM_LIST_UPDATED_CLASS, NULL };
815627351e3Scy152378 	char code_str[128];
8167c478bd9Sstevel@tonic-gate 	struct sigaction act;
8177c478bd9Sstevel@tonic-gate 
8187c478bd9Sstevel@tonic-gate 	int status = FMD_EXIT_SUCCESS;
8197c478bd9Sstevel@tonic-gate 	const char *name;
8207c478bd9Sstevel@tonic-gate 	fmd_conf_path_t *pap;
821d9638e54Smws 	fmd_event_t *e;
8220eb822a1Scindi 	int dbout;
8237c478bd9Sstevel@tonic-gate 
8247c478bd9Sstevel@tonic-gate 	/*
8257c478bd9Sstevel@tonic-gate 	 * Cache all the current debug property settings in d_fmd_debug,
8267c478bd9Sstevel@tonic-gate 	 * d_fmd_dbout, d_hdl_debug, and d_hdl_dbout.  If a given debug mask
8277c478bd9Sstevel@tonic-gate 	 * is non-zero and the corresponding dbout mask is zero, set dbout
8287c478bd9Sstevel@tonic-gate 	 * to a sensible default value based on whether we have daemonized.
8297c478bd9Sstevel@tonic-gate 	 */
8307c478bd9Sstevel@tonic-gate 	(void) fmd_conf_getprop(dp->d_conf, "dbout", &dbout);
8317c478bd9Sstevel@tonic-gate 
8327c478bd9Sstevel@tonic-gate 	if (dp->d_fmd_debug != 0 && dbout == 0)
8337c478bd9Sstevel@tonic-gate 		dp->d_fmd_dbout = dp->d_fg? FMD_DBOUT_STDERR : FMD_DBOUT_SYSLOG;
8347c478bd9Sstevel@tonic-gate 	else
8357c478bd9Sstevel@tonic-gate 		dp->d_fmd_dbout = dbout;
8367c478bd9Sstevel@tonic-gate 
8377c478bd9Sstevel@tonic-gate 	(void) fmd_conf_getprop(dp->d_conf, "client.debug", &dp->d_hdl_debug);
8387c478bd9Sstevel@tonic-gate 	(void) fmd_conf_getprop(dp->d_conf, "client.dbout", &dbout);
8397c478bd9Sstevel@tonic-gate 
8407c478bd9Sstevel@tonic-gate 	if (dp->d_hdl_debug != 0 && dbout == 0)
8417c478bd9Sstevel@tonic-gate 		dp->d_hdl_dbout = dp->d_fg? FMD_DBOUT_STDERR : FMD_DBOUT_SYSLOG;
8427c478bd9Sstevel@tonic-gate 	else
8437c478bd9Sstevel@tonic-gate 		dp->d_hdl_dbout = dbout;
8447c478bd9Sstevel@tonic-gate 
8457c478bd9Sstevel@tonic-gate 	/*
846d9638e54Smws 	 * Initialize remaining major program data structures such as the
847d9638e54Smws 	 * clock, dispatch queues, log files, module hash collections, etc.
8487c478bd9Sstevel@tonic-gate 	 * This work is done here rather than in fmd_create() to permit the -o
8497c478bd9Sstevel@tonic-gate 	 * command-line option to modify properties after fmd_create() is done.
8507c478bd9Sstevel@tonic-gate 	 */
8517aec1d6eScindi 	name = dp->d_rootdir != NULL &&
8527aec1d6eScindi 	    *dp->d_rootdir != '\0' ? dp->d_rootdir : NULL;
8537aec1d6eScindi 
8540940c086Seschrock 	/*
8550940c086Seschrock 	 * The clock must be initialized before fmd_topo_init() because
8560940c086Seschrock 	 * fmd_topo_update() calls fmd_time_gethrtime().
8570940c086Seschrock 	 */
8580940c086Seschrock 	dp->d_clockptr = dp->d_clockops->fto_init();
8590940c086Seschrock 
8600eb822a1Scindi 	fmd_topo_init();
8617aec1d6eScindi 
862d9638e54Smws 	dp->d_xprt_ids = fmd_idspace_create("xprt_ids", 1, INT_MAX);
863d9638e54Smws 	fmd_xprt_suspend_all();
864d9638e54Smws 
865d9638e54Smws 	(void) door_server_create(fmd_door);
8667c478bd9Sstevel@tonic-gate 
8677c478bd9Sstevel@tonic-gate 	dp->d_rmod->mod_timerids = fmd_idspace_create(dp->d_pname, 1, 16);
8687c478bd9Sstevel@tonic-gate 	dp->d_timers = fmd_timerq_create();
8697c478bd9Sstevel@tonic-gate 	dp->d_disp = fmd_dispq_create();
8707c478bd9Sstevel@tonic-gate 	dp->d_cases = fmd_case_hash_create();
8717c478bd9Sstevel@tonic-gate 
8727c478bd9Sstevel@tonic-gate 	/*
873d9638e54Smws 	 * The root module's mod_queue is created with limit zero, making it
874d9638e54Smws 	 * act like /dev/null; anything inserted here is simply ignored.
875d9638e54Smws 	 */
876d9638e54Smws 	dp->d_rmod->mod_queue = fmd_eventq_create(dp->d_rmod,
877d9638e54Smws 	    &dp->d_rmod->mod_stats->ms_evqstat, &dp->d_rmod->mod_stats_lock, 0);
878d9638e54Smws 
879d9638e54Smws 	/*
8807c478bd9Sstevel@tonic-gate 	 * Once our subsystems that use signals have been set up, install the
8817c478bd9Sstevel@tonic-gate 	 * signal handler for the fmd_thr_signal() API.  Verify that the signal
8827c478bd9Sstevel@tonic-gate 	 * being used for this purpose doesn't conflict with something else.
8837c478bd9Sstevel@tonic-gate 	 */
8847c478bd9Sstevel@tonic-gate 	(void) fmd_conf_getprop(dp->d_conf, "client.thrsig", &dp->d_thr_sig);
8857c478bd9Sstevel@tonic-gate 
8867c478bd9Sstevel@tonic-gate 	if (sigaction(dp->d_thr_sig, NULL, &act) != 0) {
8877c478bd9Sstevel@tonic-gate 		fmd_error(EFMD_EXIT, "invalid signal selected for "
8887c478bd9Sstevel@tonic-gate 		    "client.thrsig property: %d\n", dp->d_thr_sig);
8897c478bd9Sstevel@tonic-gate 	}
8907c478bd9Sstevel@tonic-gate 
8917c478bd9Sstevel@tonic-gate 	if (act.sa_handler != SIG_IGN && act.sa_handler != SIG_DFL) {
8927c478bd9Sstevel@tonic-gate 		fmd_error(EFMD_EXIT, "signal selected for client.thrsig "
8937c478bd9Sstevel@tonic-gate 		    "property is already in use: %d\n", dp->d_thr_sig);
8947c478bd9Sstevel@tonic-gate 	}
8957c478bd9Sstevel@tonic-gate 
8967c478bd9Sstevel@tonic-gate 	act.sa_handler = fmd_signal;
8977c478bd9Sstevel@tonic-gate 	act.sa_flags = 0;
8987c478bd9Sstevel@tonic-gate 
8997c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&act.sa_mask);
9007c478bd9Sstevel@tonic-gate 	(void) sigaction(dp->d_thr_sig, &act, NULL);
9017c478bd9Sstevel@tonic-gate 
9027c478bd9Sstevel@tonic-gate 	(void) fmd_conf_getprop(dp->d_conf, "schemedir", &name);
9037c478bd9Sstevel@tonic-gate 	dp->d_schemes = fmd_scheme_hash_create(dp->d_rootdir, name);
9047c478bd9Sstevel@tonic-gate 
9057c478bd9Sstevel@tonic-gate 	(void) fmd_conf_getprop(dp->d_conf, "log.rsrc", &name);
9067c478bd9Sstevel@tonic-gate 	dp->d_asrus = fmd_asru_hash_create(dp->d_rootdir, name);
9077c478bd9Sstevel@tonic-gate 
9087c478bd9Sstevel@tonic-gate 	(void) fmd_conf_getprop(dp->d_conf, "log.error", &name);
9097c478bd9Sstevel@tonic-gate 	dp->d_errlog = fmd_log_open(dp->d_rootdir, name, FMD_LOG_ERROR);
9107c478bd9Sstevel@tonic-gate 
9117c478bd9Sstevel@tonic-gate 	(void) fmd_conf_getprop(dp->d_conf, "log.fault", &name);
9127c478bd9Sstevel@tonic-gate 	dp->d_fltlog = fmd_log_open(dp->d_rootdir, name, FMD_LOG_FAULT);
9137c478bd9Sstevel@tonic-gate 
914f6e214c7SGavin Maltby 	(void) fmd_conf_getprop(dp->d_conf, "log.info_hival", &name);
915f6e214c7SGavin Maltby 	dp->d_hvilog = fmd_log_open(dp->d_rootdir, name, FMD_LOG_INFO);
916f6e214c7SGavin Maltby 
917f6e214c7SGavin Maltby 	(void) fmd_conf_getprop(dp->d_conf, "log.info", &name);
918f6e214c7SGavin Maltby 	dp->d_ilog = fmd_log_open(dp->d_rootdir, name, FMD_LOG_INFO);
919f6e214c7SGavin Maltby 
9207c478bd9Sstevel@tonic-gate 	if (dp->d_asrus == NULL || dp->d_errlog == NULL || dp->d_fltlog == NULL)
9217c478bd9Sstevel@tonic-gate 		fmd_error(EFMD_EXIT, "failed to initialize log files\n");
9227c478bd9Sstevel@tonic-gate 
923d9638e54Smws 	/*
924d9638e54Smws 	 * Before loading modules, create an empty control event which will act
925d9638e54Smws 	 * as a global barrier for module event processing.  Each module we
926d9638e54Smws 	 * load successfully will insert it at their head of their event queue,
927d9638e54Smws 	 * and then pause inside of fmd_ctl_rele() after dequeuing the event.
928d9638e54Smws 	 * This module barrier is required for two reasons:
929d9638e54Smws 	 *
930d9638e54Smws 	 * (a) During module loading, the restoration of case checkpoints may
931d9638e54Smws 	 *    result in a list.* event being recreated for which the intended
932d9638e54Smws 	 *    subscriber has not yet loaded depending on the load order. Such
933d9638e54Smws 	 *    events could then result in spurious "no subscriber" errors.
934d9638e54Smws 	 *
935d9638e54Smws 	 * (b) During errlog replay, a sequence of errors from a long time ago
936d9638e54Smws 	 *    may be replayed, and the module may attempt to install relative
937d9638e54Smws 	 *    timers associated with one or more of these events.  If errlog
938d9638e54Smws 	 *    replay were "racing" with active module threads, an event E1
939d9638e54Smws 	 *    that resulted in a relative timer T at time E1 + N nsec could
940d9638e54Smws 	 *    fire prior to an event E2 being enqueued, even if the relative
941d9638e54Smws 	 *    time ordering was E1 < E2 < E1 + N, causing mis-diagnosis.
942d9638e54Smws 	 */
943d9638e54Smws 	dp->d_mod_event = e = fmd_event_create(FMD_EVT_CTL,
944d9638e54Smws 	    FMD_HRT_NOW, NULL, fmd_ctl_init(NULL));
945d9638e54Smws 
946d9638e54Smws 	fmd_event_hold(e);
947d9638e54Smws 
948d9638e54Smws 	/*
949d9638e54Smws 	 * Once all data structures are initialized, we load all of our modules
950d9638e54Smws 	 * in order according to class in order to load up any subscriptions.
951d9638e54Smws 	 * Once built-in modules are loaded, we detach from our waiting parent.
952d9638e54Smws 	 */
9537c478bd9Sstevel@tonic-gate 	dp->d_mod_hash = fmd_modhash_create();
954d9638e54Smws 
955d9638e54Smws 	if (fmd_builtin_loadall(dp->d_mod_hash) != 0 && !dp->d_fg)
956d9638e54Smws 		fmd_error(EFMD_EXIT, "failed to initialize fault manager\n");
957d9638e54Smws 
958d9638e54Smws 	(void) fmd_conf_getprop(dp->d_conf, "self.name", &name);
959d9638e54Smws 	dp->d_self = fmd_modhash_lookup(dp->d_mod_hash, name);
960d9638e54Smws 
961627351e3Scy152378 	if (dp->d_self != NULL) {
962627351e3Scy152378 		if (fmd_module_dc_key2code(dp->d_self, nodc_key, code_str,
963627351e3Scy152378 		    sizeof (code_str)) == 0)
964627351e3Scy152378 			(void) fmd_conf_setprop(dp->d_conf, "nodiagcode",
965627351e3Scy152378 			    code_str);
966627351e3Scy152378 		if (fmd_module_dc_key2code(dp->d_self, repair_key, code_str,
967627351e3Scy152378 		    sizeof (code_str)) == 0)
968627351e3Scy152378 			(void) fmd_conf_setprop(dp->d_conf, "repaircode",
969627351e3Scy152378 			    code_str);
97025c6ff4bSstephh 		if (fmd_module_dc_key2code(dp->d_self, resolve_key, code_str,
97125c6ff4bSstephh 		    sizeof (code_str)) == 0)
97225c6ff4bSstephh 			(void) fmd_conf_setprop(dp->d_conf, "resolvecode",
97325c6ff4bSstephh 			    code_str);
97425c6ff4bSstephh 		if (fmd_module_dc_key2code(dp->d_self, update_key, code_str,
97525c6ff4bSstephh 		    sizeof (code_str)) == 0)
97625c6ff4bSstephh 			(void) fmd_conf_setprop(dp->d_conf, "updatecode",
97725c6ff4bSstephh 			    code_str);
978627351e3Scy152378 	}
979d9638e54Smws 
980d9638e54Smws 	fmd_rpc_init();
9817c478bd9Sstevel@tonic-gate 	dp->d_running = 1; /* we are now officially an active fmd */
9827c478bd9Sstevel@tonic-gate 
9837c478bd9Sstevel@tonic-gate 	/*
9847c478bd9Sstevel@tonic-gate 	 * Now that we're running, if a pipe fd was specified, write an exit
9857c478bd9Sstevel@tonic-gate 	 * status to it to indicate that our parent process can safely detach.
986d9638e54Smws 	 * Then proceed to loading the remaining non-built-in modules.
9877c478bd9Sstevel@tonic-gate 	 */
9887c478bd9Sstevel@tonic-gate 	if (pfd >= 0)
9897c478bd9Sstevel@tonic-gate 		(void) write(pfd, &status, sizeof (status));
9907c478bd9Sstevel@tonic-gate 
991162ba6eaSmws 	/*
992162ba6eaSmws 	 * Before loading all modules, repopulate the ASRU cache from its
993162ba6eaSmws 	 * persistent repository on disk.  Then during module loading, the
994162ba6eaSmws 	 * restoration of checkpoint files will reparent any active cases.
995162ba6eaSmws 	 */
996162ba6eaSmws 	fmd_asru_hash_refresh(dp->d_asrus);
997162ba6eaSmws 
9987c478bd9Sstevel@tonic-gate 	(void) fmd_conf_getprop(dp->d_conf, "plugin.path", &pap);
9990b9e3e76Smws 	fmd_modhash_loadall(dp->d_mod_hash, pap, &fmd_rtld_ops, ".so");
10007c478bd9Sstevel@tonic-gate 
10017c478bd9Sstevel@tonic-gate 	(void) fmd_conf_getprop(dp->d_conf, "agent.path", &pap);
10020b9e3e76Smws 	fmd_modhash_loadall(dp->d_mod_hash, pap, &fmd_proc_ops, NULL);
10037c478bd9Sstevel@tonic-gate 
100435f59e50SStephen Hanson 	dp->d_loaded = 1; /* modules are now loaded */
100535f59e50SStephen Hanson 
10067c478bd9Sstevel@tonic-gate 	/*
1007d9638e54Smws 	 * With all modules loaded, replay fault events from the ASRU cache for
1008d9638e54Smws 	 * any ASRUs that must be retired, replay error events from the errlog
1009d9638e54Smws 	 * that did not finish processing the last time ran, and then release
1010d9638e54Smws 	 * the global module barrier by executing a final rele on d_mod_event.
10117c478bd9Sstevel@tonic-gate 	 */
1012162ba6eaSmws 	fmd_asru_hash_replay(dp->d_asrus);
10137c478bd9Sstevel@tonic-gate 
10147c478bd9Sstevel@tonic-gate 	(void) pthread_rwlock_rdlock(&dp->d_log_lock);
10157c478bd9Sstevel@tonic-gate 	fmd_log_replay(dp->d_errlog, (fmd_log_f *)fmd_err_replay, dp);
10167c478bd9Sstevel@tonic-gate 	fmd_log_update(dp->d_errlog);
10177c478bd9Sstevel@tonic-gate 	(void) pthread_rwlock_unlock(&dp->d_log_lock);
10187c478bd9Sstevel@tonic-gate 
1019d9638e54Smws 	dp->d_mod_event = NULL;
1020d9638e54Smws 	fmd_event_rele(e);
10217c478bd9Sstevel@tonic-gate 
10227c478bd9Sstevel@tonic-gate 	/*
102325c6ff4bSstephh 	 * Now replay list.updated and list.repaired events
102425c6ff4bSstephh 	 */
102525c6ff4bSstephh 	fmd_case_repair_replay();
102625c6ff4bSstephh 
102725c6ff4bSstephh 	/*
10287c478bd9Sstevel@tonic-gate 	 * Finally, awaken any threads associated with receiving events from
1029d9638e54Smws 	 * open transports and tell them to proceed with fmd_xprt_recv().
10307c478bd9Sstevel@tonic-gate 	 */
1031d9638e54Smws 	fmd_xprt_resume_all();
10327c478bd9Sstevel@tonic-gate 	fmd_gc(dp, 0, 0);
1033567cc2e6Sstephh 	fmd_clear_aged_rsrcs(dp, 0, 0);
1034d9638e54Smws 
103593136fe2Sstephh 	(void) pthread_mutex_lock(&dp->d_fmd_lock);
1036d9638e54Smws 	dp->d_booted = 1;
103793136fe2Sstephh 	(void) pthread_cond_broadcast(&dp->d_fmd_cv);
103893136fe2Sstephh 	(void) pthread_mutex_unlock(&dp->d_fmd_lock);
10397c478bd9Sstevel@tonic-gate }
10407c478bd9Sstevel@tonic-gate 
10417c478bd9Sstevel@tonic-gate void
fmd_help(fmd_t * dp)10427c478bd9Sstevel@tonic-gate fmd_help(fmd_t *dp)
10437c478bd9Sstevel@tonic-gate {
10447c478bd9Sstevel@tonic-gate 	const fmd_conf_mode_t *cmp;
10457c478bd9Sstevel@tonic-gate 
10467c478bd9Sstevel@tonic-gate 	(void) printf("Usage: %s -o debug=mode[,mode]\n", dp->d_pname);
10477c478bd9Sstevel@tonic-gate 
10487c478bd9Sstevel@tonic-gate 	for (cmp = _fmd_debug_modes; cmp->cm_name != NULL; cmp++)
10497c478bd9Sstevel@tonic-gate 		(void) printf("\t%s\t%s\n", cmp->cm_name, cmp->cm_desc);
10507c478bd9Sstevel@tonic-gate }
1051