xref: /onnv-gate/usr/src/cmd/fm/modules/common/eversholt/eft.c (revision 4436:35b72f77cdd9)
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
52120Sgavinm  * Common Development and Distribution License (the "License").
62120Sgavinm  * 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*4436Sstephh  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include <stdio.h>
300Sstevel@tonic-gate #include <string.h>
310Sstevel@tonic-gate #include <fm/fmd_api.h>
320Sstevel@tonic-gate #include <libnvpair.h>
330Sstevel@tonic-gate #include "out.h"
340Sstevel@tonic-gate #include "stats.h"
350Sstevel@tonic-gate #include "alloc.h"
360Sstevel@tonic-gate #include "stable.h"
370Sstevel@tonic-gate #include "literals.h"
380Sstevel@tonic-gate #include "lut.h"
390Sstevel@tonic-gate #include "esclex.h"
400Sstevel@tonic-gate #include "tree.h"
410Sstevel@tonic-gate #include "ipath.h"
420Sstevel@tonic-gate #include "itree.h"
431414Scindi #include "iexpr.h"
440Sstevel@tonic-gate #include "ptree.h"
450Sstevel@tonic-gate #include "check.h"
460Sstevel@tonic-gate #include "version.h"
470Sstevel@tonic-gate #include "fme.h"
480Sstevel@tonic-gate #include "eval.h"
490Sstevel@tonic-gate #include "config.h"
500Sstevel@tonic-gate #include "platform.h"
510Sstevel@tonic-gate 
520Sstevel@tonic-gate /*
530Sstevel@tonic-gate  * eversholt diagnosis engine (eft.so) main entry points
540Sstevel@tonic-gate  */
550Sstevel@tonic-gate 
560Sstevel@tonic-gate fmd_hdl_t *Hdl;		/* handle in global for platform.c */
570Sstevel@tonic-gate 
580Sstevel@tonic-gate int Debug = 1;	/* turn on here and let fmd_hdl_debug() decide if really on */
590Sstevel@tonic-gate char *Autoclose;	/* close cases automatically after solving */
601414Scindi int Dupclose;		/* close cases on duplicate diagosis */
610Sstevel@tonic-gate hrtime_t Hesitate;	/* hesitation time in ns */
620Sstevel@tonic-gate int Verbose;
630Sstevel@tonic-gate int Estats;
640Sstevel@tonic-gate int Warn;	/* zero -- eft.so should not issue language warnings */
650Sstevel@tonic-gate char **Efts;
66814Sjrutt int Max_fme;		/* Maximum number of open FMEs */
670Sstevel@tonic-gate 
680Sstevel@tonic-gate /* stuff exported by yacc-generated parsers */
690Sstevel@tonic-gate extern void yyparse(void);
700Sstevel@tonic-gate extern int yydebug;
710Sstevel@tonic-gate 
720Sstevel@tonic-gate extern struct lut *Dicts;
730Sstevel@tonic-gate 
740Sstevel@tonic-gate extern void literals_init(void);
750Sstevel@tonic-gate extern void literals_fini(void);
760Sstevel@tonic-gate 
772120Sgavinm struct eftsubr {
782120Sgavinm 	const char *prefix;
792120Sgavinm 	void (*hdlr)(fmd_hdl_t *, fmd_event_t *, nvlist_t *, const char *);
802120Sgavinm } eftsubrs[] = {
812120Sgavinm 	{ "ereport.",		fme_receive_external_report },
822120Sgavinm 	{ "list.repaired",	fme_receive_repair_list },
832120Sgavinm 	{ NULL,			NULL }
842120Sgavinm };
852120Sgavinm 
860Sstevel@tonic-gate /*ARGSUSED*/
870Sstevel@tonic-gate static void
880Sstevel@tonic-gate eft_recv(fmd_hdl_t *hdl, fmd_event_t *ep, nvlist_t *nvl, const char *class)
890Sstevel@tonic-gate {
902120Sgavinm 	struct eftsubr *sp = eftsubrs;
910Sstevel@tonic-gate 
922120Sgavinm 	while (sp->prefix != NULL) {
932120Sgavinm 		if (strncmp(class, sp->prefix, strlen(sp->prefix)) == 0)
942120Sgavinm 			break;
952120Sgavinm 		sp++;
962120Sgavinm 	}
972120Sgavinm 
982120Sgavinm 	if (sp->prefix != NULL) {
992120Sgavinm 		(sp->hdlr)(hdl, ep, nvl, class);
1002120Sgavinm 	} else {
1010Sstevel@tonic-gate 		out(O_DIE,
1022120Sgavinm 		    "eft_recv: event class \"%s\" does not match our "
1032120Sgavinm 		    "subscriptions", class);
1042120Sgavinm 	}
1050Sstevel@tonic-gate }
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate /*ARGSUSED*/
1080Sstevel@tonic-gate static void
1090Sstevel@tonic-gate eft_timeout(fmd_hdl_t *hdl, id_t tid, void *arg)
1100Sstevel@tonic-gate {
1110Sstevel@tonic-gate 	out(O_ALTFP|O_STAMP,
1120Sstevel@tonic-gate 	    "\neft.so timer %ld fired with arg %p", tid, arg);
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate 	if (arg == NULL)
1150Sstevel@tonic-gate 		return;
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate 	fme_timer_fired(arg, tid);
1180Sstevel@tonic-gate }
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate static void
1210Sstevel@tonic-gate eft_close(fmd_hdl_t *hdl, fmd_case_t *fmcase)
1220Sstevel@tonic-gate {
1230Sstevel@tonic-gate 	out(O_ALTFP, "eft_close called for case %s",
1240Sstevel@tonic-gate 	    fmd_case_uuid(hdl, fmcase));
1250Sstevel@tonic-gate 	fme_close_case(hdl, fmcase);
1260Sstevel@tonic-gate }
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate static const fmd_prop_t eft_props[] = {
1290Sstevel@tonic-gate 	{ "autoclose", FMD_TYPE_STRING, NULL },
1301414Scindi 	{ "dupclose", FMD_TYPE_BOOL, "false" },
1310Sstevel@tonic-gate 	{ "estats", FMD_TYPE_BOOL, "false" },
1320Sstevel@tonic-gate 	{ "hesitate", FMD_TYPE_INT64, "10000000000" },
1330Sstevel@tonic-gate 	{ "verbose", FMD_TYPE_INT32, "0" },
1340Sstevel@tonic-gate 	{ "warn", FMD_TYPE_BOOL, "false" },
1350Sstevel@tonic-gate 	{ "status", FMD_TYPE_STRING, NULL },
136814Sjrutt 	{ "maxfme", FMD_TYPE_INT32, "0" },
1370Sstevel@tonic-gate 	{ NULL, 0, NULL }
1380Sstevel@tonic-gate };
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate static const fmd_hdl_ops_t eft_ops = {
1410Sstevel@tonic-gate 	eft_recv,	/* fmdo_recv */
1420Sstevel@tonic-gate 	eft_timeout,	/* fmdo_timeout */
1430Sstevel@tonic-gate 	eft_close,	/* fmdo_close */
1440Sstevel@tonic-gate 	NULL,		/* fmdo_stats */
1450Sstevel@tonic-gate 	NULL,		/* fmdo_gc */
1460Sstevel@tonic-gate };
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate #define	xstr(s) str(s)
1490Sstevel@tonic-gate #define	str(s) #s
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate static const fmd_hdl_info_t fmd_info = {
1520Sstevel@tonic-gate 	"eft diagnosis engine",
1530Sstevel@tonic-gate 	xstr(VERSION_MAJOR) "." xstr(VERSION_MINOR),
1540Sstevel@tonic-gate 	&eft_ops, eft_props
1550Sstevel@tonic-gate };
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate /*
1580Sstevel@tonic-gate  * ename_strdup -- like strdup but ename comes in and class string goes out
1590Sstevel@tonic-gate  */
1600Sstevel@tonic-gate static char *
1610Sstevel@tonic-gate ename_strdup(struct node *np)
1620Sstevel@tonic-gate {
1630Sstevel@tonic-gate 	struct node *mynp;
1640Sstevel@tonic-gate 	int len;
1650Sstevel@tonic-gate 	char *buf;
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate 	/* calculate length of buffer required */
1680Sstevel@tonic-gate 	len = 0;
1690Sstevel@tonic-gate 	for (mynp = np; mynp; mynp = mynp->u.name.next)
1700Sstevel@tonic-gate 		len += strlen(mynp->u.name.s) + 1;	/* +1 for dot or NULL */
1710Sstevel@tonic-gate 
1720Sstevel@tonic-gate 	buf = MALLOC(len);
1730Sstevel@tonic-gate 	buf[0] = '\0';
1740Sstevel@tonic-gate 
1750Sstevel@tonic-gate 	/* now build the string */
1760Sstevel@tonic-gate 	while (np) {
1770Sstevel@tonic-gate 		(void) strcat(buf, np->u.name.s);
1780Sstevel@tonic-gate 		np = np->u.name.next;
1790Sstevel@tonic-gate 		if (np)
1800Sstevel@tonic-gate 			(void) strcat(buf, ".");
1810Sstevel@tonic-gate 	}
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate 	return (buf);
1840Sstevel@tonic-gate }
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate /*ARGSUSED*/
1870Sstevel@tonic-gate static void
1880Sstevel@tonic-gate dosubscribe(struct node *lhs, struct node *rhs, void *arg)
1890Sstevel@tonic-gate {
1900Sstevel@tonic-gate 	char *ename = ename_strdup(lhs);
1910Sstevel@tonic-gate 
1920Sstevel@tonic-gate 	out(O_DEBUG, "subscribe: \"%s\"", ename);
1930Sstevel@tonic-gate 	fmd_hdl_subscribe(Hdl, ename);
1940Sstevel@tonic-gate 	FREE(ename);
1950Sstevel@tonic-gate }
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate extern struct stats *Filecount;
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate /*
2000Sstevel@tonic-gate  * Call all of the _fini() routines to clean up the exiting DE
2010Sstevel@tonic-gate  */
2020Sstevel@tonic-gate void
2030Sstevel@tonic-gate call_finis(void)
2040Sstevel@tonic-gate {
2050Sstevel@tonic-gate 	platform_free_eft_files(Efts);
2060Sstevel@tonic-gate 	Efts = NULL;
2070Sstevel@tonic-gate 	platform_fini();
2080Sstevel@tonic-gate 	fme_fini();
2090Sstevel@tonic-gate 	itree_fini();
2100Sstevel@tonic-gate 	ipath_fini();
2111414Scindi 	iexpr_fini();
2121414Scindi 	istat_fini();
213*4436Sstephh 	serd_fini();
2140Sstevel@tonic-gate 	lex_free();
2150Sstevel@tonic-gate 	check_fini();
2160Sstevel@tonic-gate 	tree_fini();
2170Sstevel@tonic-gate 	lut_fini();
2180Sstevel@tonic-gate 	literals_fini();
2190Sstevel@tonic-gate 	stable_fini();
2200Sstevel@tonic-gate 	stats_fini();
2210Sstevel@tonic-gate 	out_fini();
2220Sstevel@tonic-gate 	alloc_fini();
2230Sstevel@tonic-gate }
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate /*ARGSUSED*/
2260Sstevel@tonic-gate static void
2270Sstevel@tonic-gate doopendict(const char *lhs, void *rhs, void *arg)
2280Sstevel@tonic-gate {
2290Sstevel@tonic-gate 	out(O_DEBUG, "opendict: \"%s\"", lhs);
2300Sstevel@tonic-gate 	fmd_hdl_opendict(Hdl, lhs);
2310Sstevel@tonic-gate }
2320Sstevel@tonic-gate 
2330Sstevel@tonic-gate void
2340Sstevel@tonic-gate _fmd_init(fmd_hdl_t *hdl)
2350Sstevel@tonic-gate {
2360Sstevel@tonic-gate 	fmd_case_t *casep = NULL;
2370Sstevel@tonic-gate 	int count;
2380Sstevel@tonic-gate 	char *fname;
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate 	(void) fmd_hdl_register(hdl, FMD_API_VERSION, &fmd_info);
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate 	/* keep handle for routines like out() which need it */
2430Sstevel@tonic-gate 	Hdl = hdl;
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate 	Estats = fmd_prop_get_int32(hdl, "estats");
2460Sstevel@tonic-gate 
2470Sstevel@tonic-gate 	alloc_init();
2480Sstevel@tonic-gate 	out_init("eft");
2490Sstevel@tonic-gate 	stats_init(Estats);
2500Sstevel@tonic-gate 	stable_init(0);
2510Sstevel@tonic-gate 	literals_init();
2520Sstevel@tonic-gate 	platform_init();
2530Sstevel@tonic-gate 	lut_init();
2540Sstevel@tonic-gate 	tree_init();
2550Sstevel@tonic-gate 	ipath_init();
2561414Scindi 	iexpr_init();
2570Sstevel@tonic-gate 	Efts = platform_get_eft_files();
2580Sstevel@tonic-gate 	lex_init(Efts, NULL, 0);
2590Sstevel@tonic-gate 	check_init();
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate 	/*
2620Sstevel@tonic-gate 	 *  If we read no .eft files, we can't do any
2630Sstevel@tonic-gate 	 *  diagnosing, so we just unload ourselves
2640Sstevel@tonic-gate 	 */
2650Sstevel@tonic-gate 	if (stats_counter_value(Filecount) == 0) {
2660Sstevel@tonic-gate 		(void) lex_fini();
2670Sstevel@tonic-gate 		call_finis();
2680Sstevel@tonic-gate 		fmd_hdl_debug(hdl, "no fault trees provided.");
2690Sstevel@tonic-gate 		fmd_hdl_unregister(hdl);
2700Sstevel@tonic-gate 		return;
2710Sstevel@tonic-gate 	}
2720Sstevel@tonic-gate 
2730Sstevel@tonic-gate 	yyparse();
2740Sstevel@tonic-gate 	(void) lex_fini();
2750Sstevel@tonic-gate 	tree_report();
2760Sstevel@tonic-gate 	if (count = out_errcount())
2770Sstevel@tonic-gate 		out(O_DIE, "%d language error%s encountered, exiting.",
2780Sstevel@tonic-gate 		    OUTS(count));
2790Sstevel@tonic-gate 
2800Sstevel@tonic-gate 	/* subscribe to events we expect to consume */
2810Sstevel@tonic-gate 	lut_walk(Ereportenames, (lut_cb)dosubscribe, NULL);
2820Sstevel@tonic-gate 
2832120Sgavinm 	/* subscribe to repair events so we can clear state on repair */
2842120Sgavinm 	fmd_hdl_subscribe(hdl, "list.repaired");
2852120Sgavinm 
2860Sstevel@tonic-gate 	/* open dictionaries referenced by all .eft files */
2870Sstevel@tonic-gate 	lut_walk(Dicts, (lut_cb)doopendict, (void *)0);
2880Sstevel@tonic-gate 
2890Sstevel@tonic-gate 	Verbose = fmd_prop_get_int32(hdl, "verbose");
2900Sstevel@tonic-gate 	Warn = fmd_prop_get_int32(hdl, "warn");
2910Sstevel@tonic-gate 	Autoclose = fmd_prop_get_string(hdl, "autoclose");
2921414Scindi 	Dupclose = fmd_prop_get_int32(hdl, "dupclose");
2930Sstevel@tonic-gate 	Hesitate = fmd_prop_get_int64(hdl, "hesitate");
294814Sjrutt 	Max_fme = fmd_prop_get_int32(hdl, "maxfme");
2950Sstevel@tonic-gate 
2960Sstevel@tonic-gate 	if ((fname = fmd_prop_get_string(hdl, "status")) != NULL) {
2970Sstevel@tonic-gate 		FILE *fp;
2980Sstevel@tonic-gate 
2990Sstevel@tonic-gate 		if ((fp = fopen(fname, "a")) == NULL) {
3000Sstevel@tonic-gate 			fmd_prop_free_string(hdl, fname);
3010Sstevel@tonic-gate 			out(O_DIE|O_SYS, "status property file: %s", fname);
3020Sstevel@tonic-gate 		}
3030Sstevel@tonic-gate 
3040Sstevel@tonic-gate 		(void) setlinebuf(fp);
3050Sstevel@tonic-gate 		out_altfp(fp);
3060Sstevel@tonic-gate 
3070Sstevel@tonic-gate 		out(O_DEBUG, "appending status changes to \"%s\"", fname);
3080Sstevel@tonic-gate 		fmd_prop_free_string(hdl, fname);
3090Sstevel@tonic-gate 
3100Sstevel@tonic-gate 		out(O_ALTFP|O_STAMP, "\neft.so startup");
3110Sstevel@tonic-gate 	}
3120Sstevel@tonic-gate 
3131414Scindi 	out(O_DEBUG, "initialized, verbose %d warn %d autoclose %s "
3141414Scindi 	    "dupclose %d maxfme %d",
3151414Scindi 	    Verbose, Warn, Autoclose == NULL ? "(NULL)" : Autoclose,
3161414Scindi 	    Dupclose, Max_fme);
3170Sstevel@tonic-gate 
318*4436Sstephh 	fme_istat_load(hdl);
319*4436Sstephh 	fme_serd_load(hdl);
320*4436Sstephh 
3210Sstevel@tonic-gate 	out(O_DEBUG, "reconstituting any existing fmes");
3220Sstevel@tonic-gate 	while ((casep = fmd_case_next(hdl, casep)) != NULL) {
3230Sstevel@tonic-gate 		fme_restart(hdl, casep);
3240Sstevel@tonic-gate 	}
3250Sstevel@tonic-gate }
3260Sstevel@tonic-gate 
3270Sstevel@tonic-gate /*ARGSUSED*/
3280Sstevel@tonic-gate void
3290Sstevel@tonic-gate _fmd_fini(fmd_hdl_t *hdl)
3300Sstevel@tonic-gate {
3310Sstevel@tonic-gate 	fmd_prop_free_string(hdl, Autoclose);
3320Sstevel@tonic-gate 	Autoclose = NULL;
3330Sstevel@tonic-gate 	call_finis();
3340Sstevel@tonic-gate }
335