xref: /titanic_51/usr/src/cmd/fm/fmd/common/fmd_self.c (revision 87c5f7b3eef6309c168257f261ac6ace4581d234)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <sys/fm/protocol.h>
30 
31 #include <fmd_api.h>
32 #include <fmd_subr.h>
33 #include <fmd_string.h>
34 #include <fmd_protocol.h>
35 #include <fmd_module.h>
36 #include <fmd_error.h>
37 
38 static struct {
39 	fmd_stat_t nosub;
40 	fmd_stat_t module;
41 } self_stats = {
42 	{ "nosub", FMD_TYPE_UINT64, "event classes with no subscribers seen" },
43 	{ "module", FMD_TYPE_UINT64, "error events received from fmd modules" },
44 };
45 
46 typedef struct self_case {
47 	enum { SC_CLASS, SC_MODULE } sc_kind;
48 	char *sc_name;
49 } self_case_t;
50 
51 static self_case_t *
52 self_case_create(fmd_hdl_t *hdl, int kind, const char *name)
53 {
54 	self_case_t *scp = fmd_hdl_alloc(hdl, sizeof (self_case_t), FMD_SLEEP);
55 
56 	scp->sc_kind = kind;
57 	scp->sc_name = fmd_hdl_strdup(hdl, name, FMD_SLEEP);
58 
59 	return (scp);
60 }
61 
62 static void
63 self_case_destroy(fmd_hdl_t *hdl, self_case_t *scp)
64 {
65 	fmd_hdl_strfree(hdl, scp->sc_name);
66 	fmd_hdl_free(hdl, scp, sizeof (self_case_t));
67 }
68 
69 static fmd_case_t *
70 self_case_lookup(fmd_hdl_t *hdl, int kind, const char *name)
71 {
72 	fmd_case_t *cp = NULL;
73 
74 	while ((cp = fmd_case_next(hdl, cp)) != NULL) {
75 		self_case_t *scp = fmd_case_getspecific(hdl, cp);
76 		if (scp->sc_kind == kind && strcmp(scp->sc_name, name) == 0)
77 			break;
78 	}
79 
80 	return (cp);
81 }
82 
83 /*ARGSUSED*/
84 static void
85 self_recv(fmd_hdl_t *hdl, fmd_event_t *ep, nvlist_t *nvl, const char *class)
86 {
87 	fmd_case_t *cp;
88 	nvlist_t *flt, *mod;
89 	char *name;
90 	int err = 0;
91 
92 	/*
93 	 * If we get an error report from another fmd module, then create a
94 	 * case for the module and add the ereport to it.  The error is either
95 	 * from fmd_hdl_error() or from fmd_api_error().  If it is the latter,
96 	 * fmd_module_error() will send another event of class EFMD_MOD_FAIL
97 	 * when the module has failed, at which point we can solve the case.
98 	 * We can also close the case on EFMD_MOD_CONF (bad config file).
99 	 */
100 	if (strcmp(class, fmd_errclass(EFMD_MODULE)) == 0 &&
101 	    nvlist_lookup_nvlist(nvl, FM_EREPORT_DETECTOR, &mod) == 0 &&
102 	    nvlist_lookup_string(mod, FM_FMRI_FMD_NAME, &name) == 0) {
103 
104 		if ((cp = self_case_lookup(hdl, SC_MODULE, name)) == NULL) {
105 			cp = fmd_case_open(hdl,
106 			    self_case_create(hdl, SC_MODULE, name));
107 		}
108 
109 		fmd_case_add_ereport(hdl, cp, ep);
110 		self_stats.module.fmds_value.ui64++;
111 		(void) nvlist_lookup_int32(nvl, FMD_ERR_MOD_ERRNO, &err);
112 
113 		if (err != EFMD_MOD_FAIL && err != EFMD_MOD_CONF)
114 			return; /* module is still active, so keep case open */
115 
116 		if (fmd_case_solved(hdl, cp))
117 			return; /* case is already closed but error in _fini */
118 
119 		class = err == EFMD_MOD_FAIL ? FMD_FLT_MOD : FMD_FLT_CONF;
120 		flt = fmd_protocol_fault(class, 100, mod, NULL, NULL, NULL);
121 
122 		fmd_case_add_suspect(hdl, cp, flt);
123 		fmd_case_solve(hdl, cp);
124 
125 		return;
126 	}
127 
128 	/*
129 	 * If we get an I/O DDI ereport, drop it for now until the I/O DE is
130 	 * implemented and integrated.  Existing drivers in O/N have bugs that
131 	 * will trigger these and we don't want this producing FMD_FLT_NOSUB.
132 	 */
133 	if (strncmp(class, "ereport.io.ddi.", strlen("ereport.io.ddi.")) == 0)
134 		return; /* if we got a DDI ereport, drop it for now */
135 
136 	/*
137 	 * If we get any other type of event then it is of a class for which
138 	 * there are no subscribers.  Some of these correspond to internal fmd
139 	 * errors, which we ignore.  Otherwise we keep one case per class and
140 	 * use it to produce a message indicating that something is awry.
141 	 */
142 	if (strcmp(class, FM_LIST_SUSPECT_CLASS) == 0 ||
143 	    strcmp(class, FM_LIST_ISOLATED_CLASS) == 0 ||
144 	    strcmp(class, FM_LIST_REPAIRED_CLASS) == 0)
145 		return; /* if no agents are present just drop list.suspect */
146 
147 	if (strncmp(class, FMD_ERR_CLASS, FMD_ERR_CLASS_LEN) == 0)
148 		return; /* if fmd itself produced the error just drop it */
149 
150 	if (strncmp(class, FMD_RSRC_CLASS, FMD_RSRC_CLASS_LEN) == 0)
151 		return; /* if fmd itself produced the event just drop it */
152 
153 	if (strncmp(class, SYSEVENT_RSRC_CLASS, SYSEVENT_RSRC_CLASS_LEN) == 0)
154 		return; /* sysvent resources are auto generated by fmd */
155 
156 	if (self_case_lookup(hdl, SC_CLASS, class) != NULL)
157 		return; /* case is already open against this class */
158 
159 	cp = fmd_case_open(hdl, self_case_create(hdl, SC_CLASS, class));
160 	fmd_case_add_ereport(hdl, cp, ep);
161 	self_stats.nosub.fmds_value.ui64++;
162 
163 	flt = fmd_protocol_fault(FMD_FLT_NOSUB, 100, NULL, NULL, NULL, NULL);
164 	fmd_case_add_suspect(hdl, cp, flt);
165 	fmd_case_solve(hdl, cp);
166 }
167 
168 static void
169 self_close(fmd_hdl_t *hdl, fmd_case_t *cp)
170 {
171 	self_case_destroy(hdl, fmd_case_getspecific(hdl, cp));
172 }
173 
174 static const fmd_hdl_ops_t self_ops = {
175 	self_recv,	/* fmdo_recv */
176 	NULL,		/* fmdo_timeout */
177 	self_close,	/* fmdo_close */
178 	NULL,		/* fmdo_stats */
179 	NULL,		/* fmdo_gc */
180 };
181 
182 void
183 self_init(fmd_hdl_t *hdl)
184 {
185 	fmd_module_t *mp = (fmd_module_t *)hdl; /* see below */
186 
187 	fmd_hdl_info_t info = {
188 	    "Fault Manager Self-Diagnosis", "1.0", &self_ops, NULL
189 	};
190 
191 	/*
192 	 * Unlike other modules, fmd-self-diagnosis has some special needs that
193 	 * fall outside of what we want in the module API.  Manually disable
194 	 * checkpointing for this module by tweaking the mod_stats values.
195 	 * The self-diagnosis world relates to fmd's running state and modules
196 	 * which all change when it restarts, so don't bother w/ checkpointing.
197 	 */
198 	(void) pthread_mutex_lock(&mp->mod_stats_lock);
199 	mp->mod_stats->ms_ckpt_save.fmds_value.bool = FMD_B_FALSE;
200 	mp->mod_stats->ms_ckpt_restore.fmds_value.bool = FMD_B_FALSE;
201 	(void) pthread_mutex_unlock(&mp->mod_stats_lock);
202 
203 	if (fmd_hdl_register(hdl, FMD_API_VERSION, &info) != 0)
204 		return; /* failed to register with fmd */
205 
206 	(void) fmd_stat_create(hdl, FMD_STAT_NOALLOC, sizeof (self_stats) /
207 	    sizeof (fmd_stat_t), (fmd_stat_t *)&self_stats);
208 }
209 
210 void
211 self_fini(fmd_hdl_t *hdl)
212 {
213 	fmd_case_t *cp = NULL;
214 
215 	while ((cp = fmd_case_next(hdl, cp)) != NULL)
216 		self_case_destroy(hdl, fmd_case_getspecific(hdl, cp));
217 }
218