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
59dd0f810Scindi * Common Development and Distribution License (the "License").
69dd0f810Scindi * 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 /*
23*f6e214c7SGavin Maltby * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
247c478bd9Sstevel@tonic-gate */
257c478bd9Sstevel@tonic-gate
267c478bd9Sstevel@tonic-gate #include <sys/fm/protocol.h>
277c478bd9Sstevel@tonic-gate
287c478bd9Sstevel@tonic-gate #include <fmd_api.h>
297c478bd9Sstevel@tonic-gate #include <fmd_subr.h>
307c478bd9Sstevel@tonic-gate #include <fmd_string.h>
317c478bd9Sstevel@tonic-gate #include <fmd_protocol.h>
327c478bd9Sstevel@tonic-gate #include <fmd_module.h>
337c478bd9Sstevel@tonic-gate #include <fmd_error.h>
347c478bd9Sstevel@tonic-gate
357c478bd9Sstevel@tonic-gate static struct {
367c478bd9Sstevel@tonic-gate fmd_stat_t nosub;
377c478bd9Sstevel@tonic-gate fmd_stat_t module;
387c478bd9Sstevel@tonic-gate } self_stats = {
397c478bd9Sstevel@tonic-gate { "nosub", FMD_TYPE_UINT64, "event classes with no subscribers seen" },
407c478bd9Sstevel@tonic-gate { "module", FMD_TYPE_UINT64, "error events received from fmd modules" },
417c478bd9Sstevel@tonic-gate };
427c478bd9Sstevel@tonic-gate
437c478bd9Sstevel@tonic-gate typedef struct self_case {
447c478bd9Sstevel@tonic-gate enum { SC_CLASS, SC_MODULE } sc_kind;
457c478bd9Sstevel@tonic-gate char *sc_name;
467c478bd9Sstevel@tonic-gate } self_case_t;
477c478bd9Sstevel@tonic-gate
487c478bd9Sstevel@tonic-gate static self_case_t *
self_case_create(fmd_hdl_t * hdl,int kind,const char * name)497c478bd9Sstevel@tonic-gate self_case_create(fmd_hdl_t *hdl, int kind, const char *name)
507c478bd9Sstevel@tonic-gate {
517c478bd9Sstevel@tonic-gate self_case_t *scp = fmd_hdl_alloc(hdl, sizeof (self_case_t), FMD_SLEEP);
527c478bd9Sstevel@tonic-gate
537c478bd9Sstevel@tonic-gate scp->sc_kind = kind;
547c478bd9Sstevel@tonic-gate scp->sc_name = fmd_hdl_strdup(hdl, name, FMD_SLEEP);
557c478bd9Sstevel@tonic-gate
567c478bd9Sstevel@tonic-gate return (scp);
577c478bd9Sstevel@tonic-gate }
587c478bd9Sstevel@tonic-gate
597c478bd9Sstevel@tonic-gate static void
self_case_destroy(fmd_hdl_t * hdl,self_case_t * scp)607c478bd9Sstevel@tonic-gate self_case_destroy(fmd_hdl_t *hdl, self_case_t *scp)
617c478bd9Sstevel@tonic-gate {
627c478bd9Sstevel@tonic-gate fmd_hdl_strfree(hdl, scp->sc_name);
637c478bd9Sstevel@tonic-gate fmd_hdl_free(hdl, scp, sizeof (self_case_t));
647c478bd9Sstevel@tonic-gate }
657c478bd9Sstevel@tonic-gate
667c478bd9Sstevel@tonic-gate static fmd_case_t *
self_case_lookup(fmd_hdl_t * hdl,int kind,const char * name)677c478bd9Sstevel@tonic-gate self_case_lookup(fmd_hdl_t *hdl, int kind, const char *name)
687c478bd9Sstevel@tonic-gate {
697c478bd9Sstevel@tonic-gate fmd_case_t *cp = NULL;
707c478bd9Sstevel@tonic-gate
717c478bd9Sstevel@tonic-gate while ((cp = fmd_case_next(hdl, cp)) != NULL) {
727c478bd9Sstevel@tonic-gate self_case_t *scp = fmd_case_getspecific(hdl, cp);
737c478bd9Sstevel@tonic-gate if (scp->sc_kind == kind && strcmp(scp->sc_name, name) == 0)
747c478bd9Sstevel@tonic-gate break;
757c478bd9Sstevel@tonic-gate }
767c478bd9Sstevel@tonic-gate
777c478bd9Sstevel@tonic-gate return (cp);
787c478bd9Sstevel@tonic-gate }
797c478bd9Sstevel@tonic-gate
807c478bd9Sstevel@tonic-gate /*ARGSUSED*/
817c478bd9Sstevel@tonic-gate static void
self_recv(fmd_hdl_t * hdl,fmd_event_t * ep,nvlist_t * nvl,const char * class)827c478bd9Sstevel@tonic-gate self_recv(fmd_hdl_t *hdl, fmd_event_t *ep, nvlist_t *nvl, const char *class)
837c478bd9Sstevel@tonic-gate {
847c478bd9Sstevel@tonic-gate fmd_case_t *cp;
857c478bd9Sstevel@tonic-gate nvlist_t *flt, *mod;
867c478bd9Sstevel@tonic-gate char *name;
877c478bd9Sstevel@tonic-gate int err = 0;
887c478bd9Sstevel@tonic-gate
897c478bd9Sstevel@tonic-gate /*
907c478bd9Sstevel@tonic-gate * If we get an error report from another fmd module, then create a
917c478bd9Sstevel@tonic-gate * case for the module and add the ereport to it. The error is either
927c478bd9Sstevel@tonic-gate * from fmd_hdl_error() or from fmd_api_error(). If it is the latter,
937c478bd9Sstevel@tonic-gate * fmd_module_error() will send another event of class EFMD_MOD_FAIL
947c478bd9Sstevel@tonic-gate * when the module has failed, at which point we can solve the case.
957c478bd9Sstevel@tonic-gate * We can also close the case on EFMD_MOD_CONF (bad config file).
967c478bd9Sstevel@tonic-gate */
977c478bd9Sstevel@tonic-gate if (strcmp(class, fmd_errclass(EFMD_MODULE)) == 0 &&
987c478bd9Sstevel@tonic-gate nvlist_lookup_nvlist(nvl, FM_EREPORT_DETECTOR, &mod) == 0 &&
997c478bd9Sstevel@tonic-gate nvlist_lookup_string(mod, FM_FMRI_FMD_NAME, &name) == 0) {
1007c478bd9Sstevel@tonic-gate
1017c478bd9Sstevel@tonic-gate if ((cp = self_case_lookup(hdl, SC_MODULE, name)) == NULL) {
1027c478bd9Sstevel@tonic-gate cp = fmd_case_open(hdl,
1037c478bd9Sstevel@tonic-gate self_case_create(hdl, SC_MODULE, name));
1047c478bd9Sstevel@tonic-gate }
1057c478bd9Sstevel@tonic-gate
1067c478bd9Sstevel@tonic-gate fmd_case_add_ereport(hdl, cp, ep);
1077c478bd9Sstevel@tonic-gate self_stats.module.fmds_value.ui64++;
1087c478bd9Sstevel@tonic-gate (void) nvlist_lookup_int32(nvl, FMD_ERR_MOD_ERRNO, &err);
1097c478bd9Sstevel@tonic-gate
1107c478bd9Sstevel@tonic-gate if (err != EFMD_MOD_FAIL && err != EFMD_MOD_CONF)
1117c478bd9Sstevel@tonic-gate return; /* module is still active, so keep case open */
1127c478bd9Sstevel@tonic-gate
1137c478bd9Sstevel@tonic-gate if (fmd_case_solved(hdl, cp))
1147c478bd9Sstevel@tonic-gate return; /* case is already closed but error in _fini */
1157c478bd9Sstevel@tonic-gate
1167c478bd9Sstevel@tonic-gate class = err == EFMD_MOD_FAIL ? FMD_FLT_MOD : FMD_FLT_CONF;
1179dd0f810Scindi flt = fmd_protocol_fault(class, 100, mod, NULL, NULL, NULL);
1187c478bd9Sstevel@tonic-gate
1197c478bd9Sstevel@tonic-gate fmd_case_add_suspect(hdl, cp, flt);
1207c478bd9Sstevel@tonic-gate fmd_case_solve(hdl, cp);
1217c478bd9Sstevel@tonic-gate
1227c478bd9Sstevel@tonic-gate return;
1237c478bd9Sstevel@tonic-gate }
1247c478bd9Sstevel@tonic-gate
1257c478bd9Sstevel@tonic-gate /*
1267c478bd9Sstevel@tonic-gate * If we get an I/O DDI ereport, drop it for now until the I/O DE is
1277c478bd9Sstevel@tonic-gate * implemented and integrated. Existing drivers in O/N have bugs that
1287c478bd9Sstevel@tonic-gate * will trigger these and we don't want this producing FMD_FLT_NOSUB.
1297c478bd9Sstevel@tonic-gate */
1307c478bd9Sstevel@tonic-gate if (strncmp(class, "ereport.io.ddi.", strlen("ereport.io.ddi.")) == 0)
1317c478bd9Sstevel@tonic-gate return; /* if we got a DDI ereport, drop it for now */
1327c478bd9Sstevel@tonic-gate
1337c478bd9Sstevel@tonic-gate /*
1347c478bd9Sstevel@tonic-gate * If we get any other type of event then it is of a class for which
1357c478bd9Sstevel@tonic-gate * there are no subscribers. Some of these correspond to internal fmd
1367c478bd9Sstevel@tonic-gate * errors, which we ignore. Otherwise we keep one case per class and
1377c478bd9Sstevel@tonic-gate * use it to produce a message indicating that something is awry.
1387c478bd9Sstevel@tonic-gate */
139d9638e54Smws if (strcmp(class, FM_LIST_SUSPECT_CLASS) == 0 ||
140d9638e54Smws strcmp(class, FM_LIST_ISOLATED_CLASS) == 0 ||
14125c6ff4bSstephh strcmp(class, FM_LIST_UPDATED_CLASS) == 0 ||
14225c6ff4bSstephh strcmp(class, FM_LIST_RESOLVED_CLASS) == 0 ||
143705e9f42SStephen Hanson strcmp(class, FM_LIST_REPAIRED_CLASS) == 0 ||
144705e9f42SStephen Hanson strncmp(class, FM_FAULT_CLASS, strlen(FM_FAULT_CLASS)) == 0 ||
145705e9f42SStephen Hanson strncmp(class, FM_DEFECT_CLASS, strlen(FM_DEFECT_CLASS)) == 0)
14625c6ff4bSstephh return; /* if no agents are present just drop list.* */
1477c478bd9Sstevel@tonic-gate
148d9638e54Smws if (strncmp(class, FMD_ERR_CLASS, FMD_ERR_CLASS_LEN) == 0)
1497c478bd9Sstevel@tonic-gate return; /* if fmd itself produced the error just drop it */
1507c478bd9Sstevel@tonic-gate
151d9638e54Smws if (strncmp(class, FMD_RSRC_CLASS, FMD_RSRC_CLASS_LEN) == 0)
152d9638e54Smws return; /* if fmd itself produced the event just drop it */
153d9638e54Smws
1549af3851aSeschrock if (strncmp(class, SYSEVENT_RSRC_CLASS, SYSEVENT_RSRC_CLASS_LEN) == 0)
1559af3851aSeschrock return; /* sysvent resources are auto generated by fmd */
1569af3851aSeschrock
1577c478bd9Sstevel@tonic-gate if (self_case_lookup(hdl, SC_CLASS, class) != NULL)
1587c478bd9Sstevel@tonic-gate return; /* case is already open against this class */
1597c478bd9Sstevel@tonic-gate
160*f6e214c7SGavin Maltby if (strncmp(class, FM_IREPORT_CLASS ".",
161*f6e214c7SGavin Maltby sizeof (FM_IREPORT_CLASS)) == 0)
162*f6e214c7SGavin Maltby return; /* no subscriber required for ireport.* */
163*f6e214c7SGavin Maltby
1647c478bd9Sstevel@tonic-gate cp = fmd_case_open(hdl, self_case_create(hdl, SC_CLASS, class));
1657c478bd9Sstevel@tonic-gate fmd_case_add_ereport(hdl, cp, ep);
1667c478bd9Sstevel@tonic-gate self_stats.nosub.fmds_value.ui64++;
1677c478bd9Sstevel@tonic-gate
1689dd0f810Scindi flt = fmd_protocol_fault(FMD_FLT_NOSUB, 100, NULL, NULL, NULL, NULL);
169705e9f42SStephen Hanson (void) nvlist_add_string(flt, "nosub_class", class);
1707c478bd9Sstevel@tonic-gate fmd_case_add_suspect(hdl, cp, flt);
1717c478bd9Sstevel@tonic-gate fmd_case_solve(hdl, cp);
1727c478bd9Sstevel@tonic-gate }
1737c478bd9Sstevel@tonic-gate
1747c478bd9Sstevel@tonic-gate static void
self_close(fmd_hdl_t * hdl,fmd_case_t * cp)1757c478bd9Sstevel@tonic-gate self_close(fmd_hdl_t *hdl, fmd_case_t *cp)
1767c478bd9Sstevel@tonic-gate {
1777c478bd9Sstevel@tonic-gate self_case_destroy(hdl, fmd_case_getspecific(hdl, cp));
1787c478bd9Sstevel@tonic-gate }
1797c478bd9Sstevel@tonic-gate
1807c478bd9Sstevel@tonic-gate static const fmd_hdl_ops_t self_ops = {
1817c478bd9Sstevel@tonic-gate self_recv, /* fmdo_recv */
1827c478bd9Sstevel@tonic-gate NULL, /* fmdo_timeout */
1837c478bd9Sstevel@tonic-gate self_close, /* fmdo_close */
1847c478bd9Sstevel@tonic-gate NULL, /* fmdo_stats */
1857c478bd9Sstevel@tonic-gate NULL, /* fmdo_gc */
1867c478bd9Sstevel@tonic-gate };
1877c478bd9Sstevel@tonic-gate
1887c478bd9Sstevel@tonic-gate void
self_init(fmd_hdl_t * hdl)1897c478bd9Sstevel@tonic-gate self_init(fmd_hdl_t *hdl)
1907c478bd9Sstevel@tonic-gate {
1917c478bd9Sstevel@tonic-gate fmd_module_t *mp = (fmd_module_t *)hdl; /* see below */
1927c478bd9Sstevel@tonic-gate
1937c478bd9Sstevel@tonic-gate fmd_hdl_info_t info = {
1947c478bd9Sstevel@tonic-gate "Fault Manager Self-Diagnosis", "1.0", &self_ops, NULL
1957c478bd9Sstevel@tonic-gate };
1967c478bd9Sstevel@tonic-gate
1977c478bd9Sstevel@tonic-gate /*
1987c478bd9Sstevel@tonic-gate * Unlike other modules, fmd-self-diagnosis has some special needs that
1997c478bd9Sstevel@tonic-gate * fall outside of what we want in the module API. Manually disable
2007c478bd9Sstevel@tonic-gate * checkpointing for this module by tweaking the mod_stats values.
2017c478bd9Sstevel@tonic-gate * The self-diagnosis world relates to fmd's running state and modules
2027c478bd9Sstevel@tonic-gate * which all change when it restarts, so don't bother w/ checkpointing.
2037c478bd9Sstevel@tonic-gate */
2047c478bd9Sstevel@tonic-gate (void) pthread_mutex_lock(&mp->mod_stats_lock);
2057c478bd9Sstevel@tonic-gate mp->mod_stats->ms_ckpt_save.fmds_value.bool = FMD_B_FALSE;
2067c478bd9Sstevel@tonic-gate mp->mod_stats->ms_ckpt_restore.fmds_value.bool = FMD_B_FALSE;
2077c478bd9Sstevel@tonic-gate (void) pthread_mutex_unlock(&mp->mod_stats_lock);
2087c478bd9Sstevel@tonic-gate
2097c478bd9Sstevel@tonic-gate if (fmd_hdl_register(hdl, FMD_API_VERSION, &info) != 0)
2107c478bd9Sstevel@tonic-gate return; /* failed to register with fmd */
2117c478bd9Sstevel@tonic-gate
2127c478bd9Sstevel@tonic-gate (void) fmd_stat_create(hdl, FMD_STAT_NOALLOC, sizeof (self_stats) /
2137c478bd9Sstevel@tonic-gate sizeof (fmd_stat_t), (fmd_stat_t *)&self_stats);
2147c478bd9Sstevel@tonic-gate }
2157c478bd9Sstevel@tonic-gate
2167c478bd9Sstevel@tonic-gate void
self_fini(fmd_hdl_t * hdl)2177c478bd9Sstevel@tonic-gate self_fini(fmd_hdl_t *hdl)
2187c478bd9Sstevel@tonic-gate {
2197c478bd9Sstevel@tonic-gate fmd_case_t *cp = NULL;
2207c478bd9Sstevel@tonic-gate
2217c478bd9Sstevel@tonic-gate while ((cp = fmd_case_next(hdl, cp)) != NULL)
2227c478bd9Sstevel@tonic-gate self_case_destroy(hdl, fmd_case_getspecific(hdl, cp));
2237c478bd9Sstevel@tonic-gate }
224