xref: /titanic_44/usr/src/cmd/fm/modules/common/eversholt/eft.c (revision 1cb6af97c6f66f456d4f726ef056e1ebc0f73305)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 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 <stdio.h>
30 #include <string.h>
31 #include <fm/fmd_api.h>
32 #include <libnvpair.h>
33 #include "out.h"
34 #include "stats.h"
35 #include "alloc.h"
36 #include "stable.h"
37 #include "literals.h"
38 #include "lut.h"
39 #include "esclex.h"
40 #include "tree.h"
41 #include "ipath.h"
42 #include "itree.h"
43 #include "ptree.h"
44 #include "check.h"
45 #include "version.h"
46 #include "fme.h"
47 #include "eval.h"
48 #include "config.h"
49 #include "platform.h"
50 
51 /*
52  * eversholt diagnosis engine (eft.so) main entry points
53  */
54 
55 fmd_hdl_t *Hdl;		/* handle in global for platform.c */
56 
57 int Debug = 1;	/* turn on here and let fmd_hdl_debug() decide if really on */
58 char *Autoclose;	/* close cases automatically after solving */
59 int Autoconvict;	/* true if autoconvict set in conf file */
60 hrtime_t Hesitate;	/* hesitation time in ns */
61 int Verbose;
62 int Estats;
63 int Warn;	/* zero -- eft.so should not issue language warnings */
64 char **Efts;
65 
66 /* stuff exported by yacc-generated parsers */
67 extern void yyparse(void);
68 extern int yydebug;
69 
70 extern struct lut *Dicts;
71 
72 extern void literals_init(void);
73 extern void literals_fini(void);
74 
75 /*ARGSUSED*/
76 static void
77 eft_recv(fmd_hdl_t *hdl, fmd_event_t *ep, nvlist_t *nvl, const char *class)
78 {
79 	static char prefix[] = "ereport.";
80 
81 	if (strncmp(class, prefix, sizeof (prefix) - 1))
82 		out(O_DIE,
83 		    "eft_recv: event class \"%s\" does not begin with %s",
84 		    class, prefix);
85 
86 	fme_receive_external_report(hdl, ep, nvl, class);
87 }
88 
89 /*ARGSUSED*/
90 static void
91 eft_timeout(fmd_hdl_t *hdl, id_t tid, void *arg)
92 {
93 	out(O_ALTFP|O_STAMP,
94 	    "\neft.so timer %ld fired with arg %p", tid, arg);
95 
96 	if (arg == NULL)
97 		return;
98 
99 	fme_timer_fired(arg, tid);
100 }
101 
102 /*ARGSUSED*/
103 static void
104 eft_close(fmd_hdl_t *hdl, fmd_case_t *fmcase)
105 {
106 	out(O_ALTFP, "eft_close called for case %s",
107 	    fmd_case_uuid(hdl, fmcase));
108 	fme_close_case(hdl, fmcase);
109 }
110 
111 static const fmd_prop_t eft_props[] = {
112 	{ "autoconvict", FMD_TYPE_BOOL, NULL },
113 	{ "autoclose", FMD_TYPE_STRING, NULL },
114 	{ "estats", FMD_TYPE_BOOL, "false" },
115 	{ "hesitate", FMD_TYPE_INT64, "10000000000" },
116 	{ "verbose", FMD_TYPE_INT32, "0" },
117 	{ "warn", FMD_TYPE_BOOL, "false" },
118 	{ "status", FMD_TYPE_STRING, NULL },
119 	{ NULL, 0, NULL }
120 };
121 
122 static const fmd_hdl_ops_t eft_ops = {
123 	eft_recv,	/* fmdo_recv */
124 	eft_timeout,	/* fmdo_timeout */
125 	eft_close,	/* fmdo_close */
126 	NULL,		/* fmdo_stats */
127 	NULL,		/* fmdo_gc */
128 };
129 
130 #define	xstr(s) str(s)
131 #define	str(s) #s
132 
133 static const fmd_hdl_info_t fmd_info = {
134 	"eft diagnosis engine",
135 	xstr(VERSION_MAJOR) "." xstr(VERSION_MINOR),
136 	&eft_ops, eft_props
137 };
138 
139 /*
140  * ename_strdup -- like strdup but ename comes in and class string goes out
141  */
142 static char *
143 ename_strdup(struct node *np)
144 {
145 	struct node *mynp;
146 	int len;
147 	char *buf;
148 
149 	/* calculate length of buffer required */
150 	len = 0;
151 	for (mynp = np; mynp; mynp = mynp->u.name.next)
152 		len += strlen(mynp->u.name.s) + 1;	/* +1 for dot or NULL */
153 
154 	buf = MALLOC(len);
155 	buf[0] = '\0';
156 
157 	/* now build the string */
158 	while (np) {
159 		(void) strcat(buf, np->u.name.s);
160 		np = np->u.name.next;
161 		if (np)
162 			(void) strcat(buf, ".");
163 	}
164 
165 	return (buf);
166 }
167 
168 /*ARGSUSED*/
169 static void
170 dosubscribe(struct node *lhs, struct node *rhs, void *arg)
171 {
172 	char *ename = ename_strdup(lhs);
173 
174 	out(O_DEBUG, "subscribe: \"%s\"", ename);
175 	fmd_hdl_subscribe(Hdl, ename);
176 	FREE(ename);
177 }
178 
179 extern struct stats *Filecount;
180 
181 /*
182  * Call all of the _fini() routines to clean up the exiting DE
183  */
184 void
185 call_finis(void)
186 {
187 	platform_free_eft_files(Efts);
188 	Efts = NULL;
189 	platform_fini();
190 	fme_fini();
191 	itree_fini();
192 	ipath_fini();
193 	lex_free();
194 	check_fini();
195 	tree_fini();
196 	lut_fini();
197 	literals_fini();
198 	stable_fini();
199 	stats_fini();
200 	out_fini();
201 	alloc_fini();
202 }
203 
204 /*ARGSUSED*/
205 static void
206 doopendict(const char *lhs, void *rhs, void *arg)
207 {
208 	out(O_DEBUG, "opendict: \"%s\"", lhs);
209 	fmd_hdl_opendict(Hdl, lhs);
210 }
211 
212 void
213 _fmd_init(fmd_hdl_t *hdl)
214 {
215 	fmd_case_t *casep = NULL;
216 	int count;
217 	char *fname;
218 
219 	(void) fmd_hdl_register(hdl, FMD_API_VERSION, &fmd_info);
220 
221 	/* keep handle for routines like out() which need it */
222 	Hdl = hdl;
223 
224 	Estats = fmd_prop_get_int32(hdl, "estats");
225 
226 	alloc_init();
227 	out_init("eft");
228 	stats_init(Estats);
229 	stable_init(0);
230 	literals_init();
231 	platform_init();
232 	lut_init();
233 	tree_init();
234 	ipath_init();
235 	Efts = platform_get_eft_files();
236 	lex_init(Efts, NULL, 0);
237 	check_init();
238 
239 	/*
240 	 *  If we read no .eft files, we can't do any
241 	 *  diagnosing, so we just unload ourselves
242 	 */
243 	if (stats_counter_value(Filecount) == 0) {
244 		(void) lex_fini();
245 		call_finis();
246 		fmd_hdl_debug(hdl, "no fault trees provided.");
247 		fmd_hdl_unregister(hdl);
248 		return;
249 	}
250 
251 	yyparse();
252 	(void) lex_fini();
253 	tree_report();
254 	if (count = out_errcount())
255 		out(O_DIE, "%d language error%s encountered, exiting.",
256 		    OUTS(count));
257 
258 	/* subscribe to events we expect to consume */
259 	lut_walk(Ereportenames, (lut_cb)dosubscribe, NULL);
260 
261 	/* open dictionaries referenced by all .eft files */
262 	lut_walk(Dicts, (lut_cb)doopendict, (void *)0);
263 
264 	Verbose = fmd_prop_get_int32(hdl, "verbose");
265 	Warn = fmd_prop_get_int32(hdl, "warn");
266 	Autoclose = fmd_prop_get_string(hdl, "autoclose");
267 	Hesitate = fmd_prop_get_int64(hdl, "hesitate");
268 	Autoconvict = fmd_prop_get_int32(hdl, "autoconvict");
269 
270 	if ((fname = fmd_prop_get_string(hdl, "status")) != NULL) {
271 		FILE *fp;
272 
273 		if ((fp = fopen(fname, "a")) == NULL) {
274 			fmd_prop_free_string(hdl, fname);
275 			out(O_DIE|O_SYS, "status property file: %s", fname);
276 		}
277 
278 		(void) setlinebuf(fp);
279 		out_altfp(fp);
280 
281 		out(O_DEBUG, "appending status changes to \"%s\"", fname);
282 		fmd_prop_free_string(hdl, fname);
283 
284 		out(O_ALTFP|O_STAMP, "\neft.so startup");
285 	}
286 
287 	out(O_DEBUG,
288 	    "initialized, verbose %d warn %d autoclose %s autoconvict %d",
289 	    Verbose, Warn, Autoclose == NULL ? "(NULL)" : Autoclose,
290 	    Autoconvict);
291 
292 	out(O_DEBUG, "reconstituting any existing fmes");
293 	while ((casep = fmd_case_next(hdl, casep)) != NULL) {
294 		fme_restart(hdl, casep);
295 	}
296 }
297 
298 /*ARGSUSED*/
299 void
300 _fmd_fini(fmd_hdl_t *hdl)
301 {
302 	fmd_prop_free_string(hdl, Autoclose);
303 	Autoclose = NULL;
304 	call_finis();
305 }
306