xref: /illumos-gate/usr/src/cmd/fm/fmd/common/fmd_self.c (revision 8b80e8cb6855118d46f605e91b5ed4ce83417395)
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_UPDATED_CLASS) == 0 ||
145 	    strcmp(class, FM_LIST_RESOLVED_CLASS) == 0 ||
146 	    strcmp(class, FM_LIST_REPAIRED_CLASS) == 0)
147 		return; /* if no agents are present just drop list.* */
148 
149 	if (strncmp(class, FMD_ERR_CLASS, FMD_ERR_CLASS_LEN) == 0)
150 		return; /* if fmd itself produced the error just drop it */
151 
152 	if (strncmp(class, FMD_RSRC_CLASS, FMD_RSRC_CLASS_LEN) == 0)
153 		return; /* if fmd itself produced the event just drop it */
154 
155 	if (strncmp(class, SYSEVENT_RSRC_CLASS, SYSEVENT_RSRC_CLASS_LEN) == 0)
156 		return; /* sysvent resources are auto generated by fmd */
157 
158 	if (self_case_lookup(hdl, SC_CLASS, class) != NULL)
159 		return; /* case is already open against this class */
160 
161 	cp = fmd_case_open(hdl, self_case_create(hdl, SC_CLASS, class));
162 	fmd_case_add_ereport(hdl, cp, ep);
163 	self_stats.nosub.fmds_value.ui64++;
164 
165 	flt = fmd_protocol_fault(FMD_FLT_NOSUB, 100, NULL, NULL, NULL, NULL);
166 	fmd_case_add_suspect(hdl, cp, flt);
167 	fmd_case_solve(hdl, cp);
168 }
169 
170 static void
171 self_close(fmd_hdl_t *hdl, fmd_case_t *cp)
172 {
173 	self_case_destroy(hdl, fmd_case_getspecific(hdl, cp));
174 }
175 
176 static const fmd_hdl_ops_t self_ops = {
177 	self_recv,	/* fmdo_recv */
178 	NULL,		/* fmdo_timeout */
179 	self_close,	/* fmdo_close */
180 	NULL,		/* fmdo_stats */
181 	NULL,		/* fmdo_gc */
182 };
183 
184 void
185 self_init(fmd_hdl_t *hdl)
186 {
187 	fmd_module_t *mp = (fmd_module_t *)hdl; /* see below */
188 
189 	fmd_hdl_info_t info = {
190 	    "Fault Manager Self-Diagnosis", "1.0", &self_ops, NULL
191 	};
192 
193 	/*
194 	 * Unlike other modules, fmd-self-diagnosis has some special needs that
195 	 * fall outside of what we want in the module API.  Manually disable
196 	 * checkpointing for this module by tweaking the mod_stats values.
197 	 * The self-diagnosis world relates to fmd's running state and modules
198 	 * which all change when it restarts, so don't bother w/ checkpointing.
199 	 */
200 	(void) pthread_mutex_lock(&mp->mod_stats_lock);
201 	mp->mod_stats->ms_ckpt_save.fmds_value.bool = FMD_B_FALSE;
202 	mp->mod_stats->ms_ckpt_restore.fmds_value.bool = FMD_B_FALSE;
203 	(void) pthread_mutex_unlock(&mp->mod_stats_lock);
204 
205 	if (fmd_hdl_register(hdl, FMD_API_VERSION, &info) != 0)
206 		return; /* failed to register with fmd */
207 
208 	(void) fmd_stat_create(hdl, FMD_STAT_NOALLOC, sizeof (self_stats) /
209 	    sizeof (fmd_stat_t), (fmd_stat_t *)&self_stats);
210 }
211 
212 void
213 self_fini(fmd_hdl_t *hdl)
214 {
215 	fmd_case_t *cp = NULL;
216 
217 	while ((cp = fmd_case_next(hdl, cp)) != NULL)
218 		self_case_destroy(hdl, fmd_case_getspecific(hdl, cp));
219 }
220