xref: /onnv-gate/usr/src/cmd/fm/fmd/common/fmd_self.c (revision 12967:ab9ae749152f)
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
53323Scindi  * Common Development and Distribution License (the "License").
63323Scindi  * 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 /*
23*12967Sgavin.maltby@oracle.com  * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #include <sys/fm/protocol.h>
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <fmd_api.h>
290Sstevel@tonic-gate #include <fmd_subr.h>
300Sstevel@tonic-gate #include <fmd_string.h>
310Sstevel@tonic-gate #include <fmd_protocol.h>
320Sstevel@tonic-gate #include <fmd_module.h>
330Sstevel@tonic-gate #include <fmd_error.h>
340Sstevel@tonic-gate 
350Sstevel@tonic-gate static struct {
360Sstevel@tonic-gate 	fmd_stat_t nosub;
370Sstevel@tonic-gate 	fmd_stat_t module;
380Sstevel@tonic-gate } self_stats = {
390Sstevel@tonic-gate 	{ "nosub", FMD_TYPE_UINT64, "event classes with no subscribers seen" },
400Sstevel@tonic-gate 	{ "module", FMD_TYPE_UINT64, "error events received from fmd modules" },
410Sstevel@tonic-gate };
420Sstevel@tonic-gate 
430Sstevel@tonic-gate typedef struct self_case {
440Sstevel@tonic-gate 	enum { SC_CLASS, SC_MODULE } sc_kind;
450Sstevel@tonic-gate 	char *sc_name;
460Sstevel@tonic-gate } self_case_t;
470Sstevel@tonic-gate 
480Sstevel@tonic-gate static self_case_t *
self_case_create(fmd_hdl_t * hdl,int kind,const char * name)490Sstevel@tonic-gate self_case_create(fmd_hdl_t *hdl, int kind, const char *name)
500Sstevel@tonic-gate {
510Sstevel@tonic-gate 	self_case_t *scp = fmd_hdl_alloc(hdl, sizeof (self_case_t), FMD_SLEEP);
520Sstevel@tonic-gate 
530Sstevel@tonic-gate 	scp->sc_kind = kind;
540Sstevel@tonic-gate 	scp->sc_name = fmd_hdl_strdup(hdl, name, FMD_SLEEP);
550Sstevel@tonic-gate 
560Sstevel@tonic-gate 	return (scp);
570Sstevel@tonic-gate }
580Sstevel@tonic-gate 
590Sstevel@tonic-gate static void
self_case_destroy(fmd_hdl_t * hdl,self_case_t * scp)600Sstevel@tonic-gate self_case_destroy(fmd_hdl_t *hdl, self_case_t *scp)
610Sstevel@tonic-gate {
620Sstevel@tonic-gate 	fmd_hdl_strfree(hdl, scp->sc_name);
630Sstevel@tonic-gate 	fmd_hdl_free(hdl, scp, sizeof (self_case_t));
640Sstevel@tonic-gate }
650Sstevel@tonic-gate 
660Sstevel@tonic-gate static fmd_case_t *
self_case_lookup(fmd_hdl_t * hdl,int kind,const char * name)670Sstevel@tonic-gate self_case_lookup(fmd_hdl_t *hdl, int kind, const char *name)
680Sstevel@tonic-gate {
690Sstevel@tonic-gate 	fmd_case_t *cp = NULL;
700Sstevel@tonic-gate 
710Sstevel@tonic-gate 	while ((cp = fmd_case_next(hdl, cp)) != NULL) {
720Sstevel@tonic-gate 		self_case_t *scp = fmd_case_getspecific(hdl, cp);
730Sstevel@tonic-gate 		if (scp->sc_kind == kind && strcmp(scp->sc_name, name) == 0)
740Sstevel@tonic-gate 			break;
750Sstevel@tonic-gate 	}
760Sstevel@tonic-gate 
770Sstevel@tonic-gate 	return (cp);
780Sstevel@tonic-gate }
790Sstevel@tonic-gate 
800Sstevel@tonic-gate /*ARGSUSED*/
810Sstevel@tonic-gate static void
self_recv(fmd_hdl_t * hdl,fmd_event_t * ep,nvlist_t * nvl,const char * class)820Sstevel@tonic-gate self_recv(fmd_hdl_t *hdl, fmd_event_t *ep, nvlist_t *nvl, const char *class)
830Sstevel@tonic-gate {
840Sstevel@tonic-gate 	fmd_case_t *cp;
850Sstevel@tonic-gate 	nvlist_t *flt, *mod;
860Sstevel@tonic-gate 	char *name;
870Sstevel@tonic-gate 	int err = 0;
880Sstevel@tonic-gate 
890Sstevel@tonic-gate 	/*
900Sstevel@tonic-gate 	 * If we get an error report from another fmd module, then create a
910Sstevel@tonic-gate 	 * case for the module and add the ereport to it.  The error is either
920Sstevel@tonic-gate 	 * from fmd_hdl_error() or from fmd_api_error().  If it is the latter,
930Sstevel@tonic-gate 	 * fmd_module_error() will send another event of class EFMD_MOD_FAIL
940Sstevel@tonic-gate 	 * when the module has failed, at which point we can solve the case.
950Sstevel@tonic-gate 	 * We can also close the case on EFMD_MOD_CONF (bad config file).
960Sstevel@tonic-gate 	 */
970Sstevel@tonic-gate 	if (strcmp(class, fmd_errclass(EFMD_MODULE)) == 0 &&
980Sstevel@tonic-gate 	    nvlist_lookup_nvlist(nvl, FM_EREPORT_DETECTOR, &mod) == 0 &&
990Sstevel@tonic-gate 	    nvlist_lookup_string(mod, FM_FMRI_FMD_NAME, &name) == 0) {
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate 		if ((cp = self_case_lookup(hdl, SC_MODULE, name)) == NULL) {
1020Sstevel@tonic-gate 			cp = fmd_case_open(hdl,
1030Sstevel@tonic-gate 			    self_case_create(hdl, SC_MODULE, name));
1040Sstevel@tonic-gate 		}
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate 		fmd_case_add_ereport(hdl, cp, ep);
1070Sstevel@tonic-gate 		self_stats.module.fmds_value.ui64++;
1080Sstevel@tonic-gate 		(void) nvlist_lookup_int32(nvl, FMD_ERR_MOD_ERRNO, &err);
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate 		if (err != EFMD_MOD_FAIL && err != EFMD_MOD_CONF)
1110Sstevel@tonic-gate 			return; /* module is still active, so keep case open */
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate 		if (fmd_case_solved(hdl, cp))
1140Sstevel@tonic-gate 			return; /* case is already closed but error in _fini */
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate 		class = err == EFMD_MOD_FAIL ? FMD_FLT_MOD : FMD_FLT_CONF;
1173323Scindi 		flt = fmd_protocol_fault(class, 100, mod, NULL, NULL, NULL);
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate 		fmd_case_add_suspect(hdl, cp, flt);
1200Sstevel@tonic-gate 		fmd_case_solve(hdl, cp);
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate 		return;
1230Sstevel@tonic-gate 	}
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate 	/*
1260Sstevel@tonic-gate 	 * If we get an I/O DDI ereport, drop it for now until the I/O DE is
1270Sstevel@tonic-gate 	 * implemented and integrated.  Existing drivers in O/N have bugs that
1280Sstevel@tonic-gate 	 * will trigger these and we don't want this producing FMD_FLT_NOSUB.
1290Sstevel@tonic-gate 	 */
1300Sstevel@tonic-gate 	if (strncmp(class, "ereport.io.ddi.", strlen("ereport.io.ddi.")) == 0)
1310Sstevel@tonic-gate 		return; /* if we got a DDI ereport, drop it for now */
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate 	/*
1340Sstevel@tonic-gate 	 * If we get any other type of event then it is of a class for which
1350Sstevel@tonic-gate 	 * there are no subscribers.  Some of these correspond to internal fmd
1360Sstevel@tonic-gate 	 * errors, which we ignore.  Otherwise we keep one case per class and
1370Sstevel@tonic-gate 	 * use it to produce a message indicating that something is awry.
1380Sstevel@tonic-gate 	 */
1391193Smws 	if (strcmp(class, FM_LIST_SUSPECT_CLASS) == 0 ||
1401193Smws 	    strcmp(class, FM_LIST_ISOLATED_CLASS) == 0 ||
1417275Sstephh 	    strcmp(class, FM_LIST_UPDATED_CLASS) == 0 ||
1427275Sstephh 	    strcmp(class, FM_LIST_RESOLVED_CLASS) == 0 ||
14311416SStephen.Hanson@Sun.COM 	    strcmp(class, FM_LIST_REPAIRED_CLASS) == 0 ||
14411416SStephen.Hanson@Sun.COM 	    strncmp(class, FM_FAULT_CLASS, strlen(FM_FAULT_CLASS)) == 0 ||
14511416SStephen.Hanson@Sun.COM 	    strncmp(class, FM_DEFECT_CLASS, strlen(FM_DEFECT_CLASS)) == 0)
1467275Sstephh 		return; /* if no agents are present just drop list.* */
1470Sstevel@tonic-gate 
1481193Smws 	if (strncmp(class, FMD_ERR_CLASS, FMD_ERR_CLASS_LEN) == 0)
1490Sstevel@tonic-gate 		return; /* if fmd itself produced the error just drop it */
1500Sstevel@tonic-gate 
1511193Smws 	if (strncmp(class, FMD_RSRC_CLASS, FMD_RSRC_CLASS_LEN) == 0)
1521193Smws 		return; /* if fmd itself produced the event just drop it */
1531193Smws 
1547171Seschrock 	if (strncmp(class, SYSEVENT_RSRC_CLASS, SYSEVENT_RSRC_CLASS_LEN) == 0)
1557171Seschrock 		return; /* sysvent resources are auto generated by fmd */
1567171Seschrock 
1570Sstevel@tonic-gate 	if (self_case_lookup(hdl, SC_CLASS, class) != NULL)
1580Sstevel@tonic-gate 		return; /* case is already open against this class */
1590Sstevel@tonic-gate 
160*12967Sgavin.maltby@oracle.com 	if (strncmp(class, FM_IREPORT_CLASS ".",
161*12967Sgavin.maltby@oracle.com 	    sizeof (FM_IREPORT_CLASS)) == 0)
162*12967Sgavin.maltby@oracle.com 		return; /* no subscriber required for ireport.* */
163*12967Sgavin.maltby@oracle.com 
1640Sstevel@tonic-gate 	cp = fmd_case_open(hdl, self_case_create(hdl, SC_CLASS, class));
1650Sstevel@tonic-gate 	fmd_case_add_ereport(hdl, cp, ep);
1660Sstevel@tonic-gate 	self_stats.nosub.fmds_value.ui64++;
1670Sstevel@tonic-gate 
1683323Scindi 	flt = fmd_protocol_fault(FMD_FLT_NOSUB, 100, NULL, NULL, NULL, NULL);
16911416SStephen.Hanson@Sun.COM 	(void) nvlist_add_string(flt, "nosub_class", class);
1700Sstevel@tonic-gate 	fmd_case_add_suspect(hdl, cp, flt);
1710Sstevel@tonic-gate 	fmd_case_solve(hdl, cp);
1720Sstevel@tonic-gate }
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate static void
self_close(fmd_hdl_t * hdl,fmd_case_t * cp)1750Sstevel@tonic-gate self_close(fmd_hdl_t *hdl, fmd_case_t *cp)
1760Sstevel@tonic-gate {
1770Sstevel@tonic-gate 	self_case_destroy(hdl, fmd_case_getspecific(hdl, cp));
1780Sstevel@tonic-gate }
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate static const fmd_hdl_ops_t self_ops = {
1810Sstevel@tonic-gate 	self_recv,	/* fmdo_recv */
1820Sstevel@tonic-gate 	NULL,		/* fmdo_timeout */
1830Sstevel@tonic-gate 	self_close,	/* fmdo_close */
1840Sstevel@tonic-gate 	NULL,		/* fmdo_stats */
1850Sstevel@tonic-gate 	NULL,		/* fmdo_gc */
1860Sstevel@tonic-gate };
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate void
self_init(fmd_hdl_t * hdl)1890Sstevel@tonic-gate self_init(fmd_hdl_t *hdl)
1900Sstevel@tonic-gate {
1910Sstevel@tonic-gate 	fmd_module_t *mp = (fmd_module_t *)hdl; /* see below */
1920Sstevel@tonic-gate 
1930Sstevel@tonic-gate 	fmd_hdl_info_t info = {
1940Sstevel@tonic-gate 	    "Fault Manager Self-Diagnosis", "1.0", &self_ops, NULL
1950Sstevel@tonic-gate 	};
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate 	/*
1980Sstevel@tonic-gate 	 * Unlike other modules, fmd-self-diagnosis has some special needs that
1990Sstevel@tonic-gate 	 * fall outside of what we want in the module API.  Manually disable
2000Sstevel@tonic-gate 	 * checkpointing for this module by tweaking the mod_stats values.
2010Sstevel@tonic-gate 	 * The self-diagnosis world relates to fmd's running state and modules
2020Sstevel@tonic-gate 	 * which all change when it restarts, so don't bother w/ checkpointing.
2030Sstevel@tonic-gate 	 */
2040Sstevel@tonic-gate 	(void) pthread_mutex_lock(&mp->mod_stats_lock);
2050Sstevel@tonic-gate 	mp->mod_stats->ms_ckpt_save.fmds_value.bool = FMD_B_FALSE;
2060Sstevel@tonic-gate 	mp->mod_stats->ms_ckpt_restore.fmds_value.bool = FMD_B_FALSE;
2070Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&mp->mod_stats_lock);
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate 	if (fmd_hdl_register(hdl, FMD_API_VERSION, &info) != 0)
2100Sstevel@tonic-gate 		return; /* failed to register with fmd */
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate 	(void) fmd_stat_create(hdl, FMD_STAT_NOALLOC, sizeof (self_stats) /
2130Sstevel@tonic-gate 	    sizeof (fmd_stat_t), (fmd_stat_t *)&self_stats);
2140Sstevel@tonic-gate }
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate void
self_fini(fmd_hdl_t * hdl)2170Sstevel@tonic-gate self_fini(fmd_hdl_t *hdl)
2180Sstevel@tonic-gate {
2190Sstevel@tonic-gate 	fmd_case_t *cp = NULL;
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate 	while ((cp = fmd_case_next(hdl, cp)) != NULL)
2220Sstevel@tonic-gate 		self_case_destroy(hdl, fmd_case_getspecific(hdl, cp));
2230Sstevel@tonic-gate }
224