xref: /onnv-gate/usr/src/cmd/fm/modules/common/eversholt/eft.c (revision 12967:ab9ae749152f)
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*12967Sgavin.maltby@oracle.com  * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
26*12967Sgavin.maltby@oracle.com #include <unistd.h>
270Sstevel@tonic-gate #include <stdio.h>
280Sstevel@tonic-gate #include <string.h>
290Sstevel@tonic-gate #include <fm/fmd_api.h>
300Sstevel@tonic-gate #include <libnvpair.h>
315204Sstephh #include <fm/libtopo.h>
320Sstevel@tonic-gate #include "out.h"
330Sstevel@tonic-gate #include "stats.h"
340Sstevel@tonic-gate #include "alloc.h"
350Sstevel@tonic-gate #include "stable.h"
360Sstevel@tonic-gate #include "literals.h"
370Sstevel@tonic-gate #include "lut.h"
380Sstevel@tonic-gate #include "esclex.h"
390Sstevel@tonic-gate #include "tree.h"
400Sstevel@tonic-gate #include "ipath.h"
410Sstevel@tonic-gate #include "itree.h"
421414Scindi #include "iexpr.h"
430Sstevel@tonic-gate #include "ptree.h"
440Sstevel@tonic-gate #include "check.h"
450Sstevel@tonic-gate #include "version.h"
460Sstevel@tonic-gate #include "fme.h"
470Sstevel@tonic-gate #include "eval.h"
480Sstevel@tonic-gate #include "config.h"
490Sstevel@tonic-gate #include "platform.h"
500Sstevel@tonic-gate 
510Sstevel@tonic-gate /*
520Sstevel@tonic-gate  * eversholt diagnosis engine (eft.so) main entry points
530Sstevel@tonic-gate  */
540Sstevel@tonic-gate 
550Sstevel@tonic-gate fmd_hdl_t *Hdl;		/* handle in global for platform.c */
560Sstevel@tonic-gate 
570Sstevel@tonic-gate int Debug = 1;	/* turn on here and let fmd_hdl_debug() decide if really on */
580Sstevel@tonic-gate hrtime_t Hesitate;	/* hesitation time in ns */
595204Sstephh char *Serd_Override;	/* override for Serd engines */
600Sstevel@tonic-gate int Verbose;
610Sstevel@tonic-gate int Estats;
620Sstevel@tonic-gate int Warn;	/* zero -- eft.so should not issue language warnings */
630Sstevel@tonic-gate char **Efts;
64814Sjrutt int Max_fme;		/* Maximum number of open FMEs */
650Sstevel@tonic-gate 
660Sstevel@tonic-gate /* stuff exported by yacc-generated parsers */
670Sstevel@tonic-gate extern void yyparse(void);
680Sstevel@tonic-gate extern int yydebug;
690Sstevel@tonic-gate 
700Sstevel@tonic-gate extern struct lut *Dicts;
710Sstevel@tonic-gate 
720Sstevel@tonic-gate extern void literals_init(void);
730Sstevel@tonic-gate extern void literals_fini(void);
740Sstevel@tonic-gate 
752120Sgavinm struct eftsubr {
762120Sgavinm 	const char *prefix;
772120Sgavinm 	void (*hdlr)(fmd_hdl_t *, fmd_event_t *, nvlist_t *, const char *);
782120Sgavinm } eftsubrs[] = {
792120Sgavinm 	{ "ereport.",		fme_receive_external_report },
802120Sgavinm 	{ "list.repaired",	fme_receive_repair_list },
812120Sgavinm 	{ NULL,			NULL }
822120Sgavinm };
832120Sgavinm 
840Sstevel@tonic-gate /*ARGSUSED*/
850Sstevel@tonic-gate static void
eft_recv(fmd_hdl_t * hdl,fmd_event_t * ep,nvlist_t * nvl,const char * class)860Sstevel@tonic-gate eft_recv(fmd_hdl_t *hdl, fmd_event_t *ep, nvlist_t *nvl, const char *class)
870Sstevel@tonic-gate {
882120Sgavinm 	struct eftsubr *sp = eftsubrs;
890Sstevel@tonic-gate 
902120Sgavinm 	while (sp->prefix != NULL) {
912120Sgavinm 		if (strncmp(class, sp->prefix, strlen(sp->prefix)) == 0)
922120Sgavinm 			break;
932120Sgavinm 		sp++;
942120Sgavinm 	}
952120Sgavinm 
962120Sgavinm 	if (sp->prefix != NULL) {
972120Sgavinm 		(sp->hdlr)(hdl, ep, nvl, class);
982120Sgavinm 	} else {
990Sstevel@tonic-gate 		out(O_DIE,
1002120Sgavinm 		    "eft_recv: event class \"%s\" does not match our "
1012120Sgavinm 		    "subscriptions", class);
1022120Sgavinm 	}
1030Sstevel@tonic-gate }
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate /*ARGSUSED*/
1060Sstevel@tonic-gate static void
eft_timeout(fmd_hdl_t * hdl,id_t tid,void * arg)1070Sstevel@tonic-gate eft_timeout(fmd_hdl_t *hdl, id_t tid, void *arg)
1080Sstevel@tonic-gate {
1090Sstevel@tonic-gate 	out(O_ALTFP|O_STAMP,
1100Sstevel@tonic-gate 	    "\neft.so timer %ld fired with arg %p", tid, arg);
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate 	if (arg == NULL)
1130Sstevel@tonic-gate 		return;
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate 	fme_timer_fired(arg, tid);
1160Sstevel@tonic-gate }
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate static void
eft_close(fmd_hdl_t * hdl,fmd_case_t * fmcase)1190Sstevel@tonic-gate eft_close(fmd_hdl_t *hdl, fmd_case_t *fmcase)
1200Sstevel@tonic-gate {
1210Sstevel@tonic-gate 	out(O_ALTFP, "eft_close called for case %s",
1220Sstevel@tonic-gate 	    fmd_case_uuid(hdl, fmcase));
1230Sstevel@tonic-gate 	fme_close_case(hdl, fmcase);
1240Sstevel@tonic-gate }
1250Sstevel@tonic-gate 
1265204Sstephh /*
1275204Sstephh  * The "serd_override" property allows the N and T parameters of specified serd
1285204Sstephh  * engines to be overridden. The property is a string consisting of one or more
1295204Sstephh  * space separated triplets. Each triplet is of the form "name,N,T" where "name"
1305204Sstephh  * is the name of the serd engine and N and T are the new paremeters to use.
1315204Sstephh  * For example "serd.io.device.nonfatal,5,3h" would set the parameters for the
1325204Sstephh  * serd.io.device.nonfatal engine to 5 in 3 hours.
1335204Sstephh  */
1340Sstevel@tonic-gate static const fmd_prop_t eft_props[] = {
1350Sstevel@tonic-gate 	{ "estats", FMD_TYPE_BOOL, "false" },
1360Sstevel@tonic-gate 	{ "hesitate", FMD_TYPE_INT64, "10000000000" },
1375204Sstephh 	{ "serd_override", FMD_TYPE_STRING, NULL },
1380Sstevel@tonic-gate 	{ "verbose", FMD_TYPE_INT32, "0" },
1390Sstevel@tonic-gate 	{ "warn", FMD_TYPE_BOOL, "false" },
1400Sstevel@tonic-gate 	{ "status", FMD_TYPE_STRING, NULL },
141814Sjrutt 	{ "maxfme", FMD_TYPE_INT32, "0" },
1420Sstevel@tonic-gate 	{ NULL, 0, NULL }
1430Sstevel@tonic-gate };
1440Sstevel@tonic-gate 
1455204Sstephh /*ARGSUSED*/
1465204Sstephh static void
eft_topo_change(fmd_hdl_t * hdl,topo_hdl_t * thp)1475204Sstephh eft_topo_change(fmd_hdl_t *hdl, topo_hdl_t *thp)
1485204Sstephh {
1495204Sstephh 	fme_receive_topology_change();
1505204Sstephh }
1515204Sstephh 
1520Sstevel@tonic-gate static const fmd_hdl_ops_t eft_ops = {
1530Sstevel@tonic-gate 	eft_recv,	/* fmdo_recv */
1540Sstevel@tonic-gate 	eft_timeout,	/* fmdo_timeout */
1550Sstevel@tonic-gate 	eft_close,	/* fmdo_close */
1560Sstevel@tonic-gate 	NULL,		/* fmdo_stats */
1570Sstevel@tonic-gate 	NULL,		/* fmdo_gc */
1585204Sstephh 	NULL,		/* fmdo_send */
1595204Sstephh 	eft_topo_change	/* fmdo_topo_change */
1600Sstevel@tonic-gate };
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate #define	xstr(s) str(s)
1630Sstevel@tonic-gate #define	str(s) #s
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate static const fmd_hdl_info_t fmd_info = {
1660Sstevel@tonic-gate 	"eft diagnosis engine",
1670Sstevel@tonic-gate 	xstr(VERSION_MAJOR) "." xstr(VERSION_MINOR),
1680Sstevel@tonic-gate 	&eft_ops, eft_props
1690Sstevel@tonic-gate };
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate /*
1720Sstevel@tonic-gate  * ename_strdup -- like strdup but ename comes in and class string goes out
1730Sstevel@tonic-gate  */
1740Sstevel@tonic-gate static char *
ename_strdup(struct node * np)1750Sstevel@tonic-gate ename_strdup(struct node *np)
1760Sstevel@tonic-gate {
1770Sstevel@tonic-gate 	struct node *mynp;
1780Sstevel@tonic-gate 	int len;
1790Sstevel@tonic-gate 	char *buf;
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate 	/* calculate length of buffer required */
1820Sstevel@tonic-gate 	len = 0;
1830Sstevel@tonic-gate 	for (mynp = np; mynp; mynp = mynp->u.name.next)
1840Sstevel@tonic-gate 		len += strlen(mynp->u.name.s) + 1;	/* +1 for dot or NULL */
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate 	buf = MALLOC(len);
1870Sstevel@tonic-gate 	buf[0] = '\0';
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate 	/* now build the string */
1900Sstevel@tonic-gate 	while (np) {
1910Sstevel@tonic-gate 		(void) strcat(buf, np->u.name.s);
1920Sstevel@tonic-gate 		np = np->u.name.next;
1930Sstevel@tonic-gate 		if (np)
1940Sstevel@tonic-gate 			(void) strcat(buf, ".");
1950Sstevel@tonic-gate 	}
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate 	return (buf);
1980Sstevel@tonic-gate }
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate /*ARGSUSED*/
2010Sstevel@tonic-gate static void
dosubscribe(struct node * lhs,struct node * rhs,void * arg)2020Sstevel@tonic-gate dosubscribe(struct node *lhs, struct node *rhs, void *arg)
2030Sstevel@tonic-gate {
2040Sstevel@tonic-gate 	char *ename = ename_strdup(lhs);
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate 	fmd_hdl_subscribe(Hdl, ename);
2070Sstevel@tonic-gate 	FREE(ename);
2080Sstevel@tonic-gate }
2090Sstevel@tonic-gate 
2106640Scth /*ARGSUSED*/
2116640Scth static void
dodiscardprint(struct node * lhs,struct node * rhs,void * arg)2126640Scth dodiscardprint(struct node *lhs, struct node *rhs, void *arg)
2136640Scth {
2146640Scth 	char *ename = (char *)lhs;
2156640Scth 
216*12967Sgavin.maltby@oracle.com 	out(O_VERB, "allow silent discard_if_config_unknown: \"%s\"", ename);
2176640Scth }
2186640Scth 
2190Sstevel@tonic-gate extern struct stats *Filecount;
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate /*
2220Sstevel@tonic-gate  * Call all of the _fini() routines to clean up the exiting DE
2230Sstevel@tonic-gate  */
2240Sstevel@tonic-gate void
call_finis(void)2250Sstevel@tonic-gate call_finis(void)
2260Sstevel@tonic-gate {
2270Sstevel@tonic-gate 	platform_free_eft_files(Efts);
2280Sstevel@tonic-gate 	Efts = NULL;
2290Sstevel@tonic-gate 	platform_fini();
2300Sstevel@tonic-gate 	fme_fini();
2310Sstevel@tonic-gate 	itree_fini();
2320Sstevel@tonic-gate 	ipath_fini();
2331414Scindi 	iexpr_fini();
2341414Scindi 	istat_fini();
2354436Sstephh 	serd_fini();
2360Sstevel@tonic-gate 	lex_free();
2370Sstevel@tonic-gate 	check_fini();
2380Sstevel@tonic-gate 	tree_fini();
2390Sstevel@tonic-gate 	lut_fini();
2400Sstevel@tonic-gate 	literals_fini();
2410Sstevel@tonic-gate 	stable_fini();
2420Sstevel@tonic-gate 	stats_fini();
2430Sstevel@tonic-gate 	out_fini();
2440Sstevel@tonic-gate 	alloc_fini();
2450Sstevel@tonic-gate }
2460Sstevel@tonic-gate 
2470Sstevel@tonic-gate /*ARGSUSED*/
2480Sstevel@tonic-gate static void
doopendict(const char * lhs,void * rhs,void * arg)2490Sstevel@tonic-gate doopendict(const char *lhs, void *rhs, void *arg)
2500Sstevel@tonic-gate {
251*12967Sgavin.maltby@oracle.com 	out(O_VERB, "opendict: \"%s\"", lhs);
2520Sstevel@tonic-gate 	fmd_hdl_opendict(Hdl, lhs);
2530Sstevel@tonic-gate }
2540Sstevel@tonic-gate 
2550Sstevel@tonic-gate void
_fmd_init(fmd_hdl_t * hdl)2560Sstevel@tonic-gate _fmd_init(fmd_hdl_t *hdl)
2570Sstevel@tonic-gate {
2580Sstevel@tonic-gate 	fmd_case_t *casep = NULL;
2590Sstevel@tonic-gate 	int count;
2600Sstevel@tonic-gate 	char *fname;
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate 	(void) fmd_hdl_register(hdl, FMD_API_VERSION, &fmd_info);
2630Sstevel@tonic-gate 
2640Sstevel@tonic-gate 	/* keep handle for routines like out() which need it */
2650Sstevel@tonic-gate 	Hdl = hdl;
2660Sstevel@tonic-gate 
2676640Scth 	/* set up out(O_ALTFP) first things so it is available for debug */
2680Sstevel@tonic-gate 	alloc_init();
2690Sstevel@tonic-gate 	out_init("eft");
2706640Scth 	if ((fname = fmd_prop_get_string(hdl, "status")) != NULL) {
2716640Scth 		FILE *fp;
2726640Scth 
2736640Scth 		if ((fp = fopen(fname, "a")) == NULL) {
2746640Scth 			fmd_prop_free_string(hdl, fname);
2756640Scth 			out(O_DIE|O_SYS, "status property file: %s", fname);
2766640Scth 		}
2776640Scth 
2786640Scth 		(void) setlinebuf(fp);
2796640Scth 		out_altfp(fp);
2806640Scth 
2816640Scth 		out(O_DEBUG, "appending status changes to \"%s\"", fname);
2826640Scth 		fmd_prop_free_string(hdl, fname);
2836640Scth 
2846640Scth 		out(O_ALTFP|O_STAMP, "\neft.so startup");
2856640Scth 	}
2866640Scth 
2876640Scth 
2886640Scth 	Estats = fmd_prop_get_int32(hdl, "estats");
2890Sstevel@tonic-gate 	stats_init(Estats);
2906640Scth 
2910Sstevel@tonic-gate 	stable_init(0);
2920Sstevel@tonic-gate 	literals_init();
2930Sstevel@tonic-gate 	platform_init();
2940Sstevel@tonic-gate 	lut_init();
2950Sstevel@tonic-gate 	tree_init();
2960Sstevel@tonic-gate 	ipath_init();
2971414Scindi 	iexpr_init();
2980Sstevel@tonic-gate 	Efts = platform_get_eft_files();
2990Sstevel@tonic-gate 	lex_init(Efts, NULL, 0);
3000Sstevel@tonic-gate 	check_init();
3010Sstevel@tonic-gate 
3020Sstevel@tonic-gate 	/*
3030Sstevel@tonic-gate 	 *  If we read no .eft files, we can't do any
3040Sstevel@tonic-gate 	 *  diagnosing, so we just unload ourselves
3050Sstevel@tonic-gate 	 */
3060Sstevel@tonic-gate 	if (stats_counter_value(Filecount) == 0) {
3070Sstevel@tonic-gate 		(void) lex_fini();
3080Sstevel@tonic-gate 		call_finis();
3090Sstevel@tonic-gate 		fmd_hdl_debug(hdl, "no fault trees provided.");
3100Sstevel@tonic-gate 		fmd_hdl_unregister(hdl);
3110Sstevel@tonic-gate 		return;
3120Sstevel@tonic-gate 	}
3130Sstevel@tonic-gate 
3140Sstevel@tonic-gate 	yyparse();
3150Sstevel@tonic-gate 	(void) lex_fini();
3160Sstevel@tonic-gate 	tree_report();
3170Sstevel@tonic-gate 	if (count = out_errcount())
3180Sstevel@tonic-gate 		out(O_DIE, "%d language error%s encountered, exiting.",
3190Sstevel@tonic-gate 		    OUTS(count));
3200Sstevel@tonic-gate 
3210Sstevel@tonic-gate 	/* subscribe to events we expect to consume */
3220Sstevel@tonic-gate 	lut_walk(Ereportenames, (lut_cb)dosubscribe, NULL);
3236640Scth 	lut_walk(Ereportenames_discard, (lut_cb)dodiscardprint, NULL);
3240Sstevel@tonic-gate 
3252120Sgavinm 	/* subscribe to repair events so we can clear state on repair */
3262120Sgavinm 	fmd_hdl_subscribe(hdl, "list.repaired");
3272120Sgavinm 
3280Sstevel@tonic-gate 	/* open dictionaries referenced by all .eft files */
3290Sstevel@tonic-gate 	lut_walk(Dicts, (lut_cb)doopendict, (void *)0);
3300Sstevel@tonic-gate 
3310Sstevel@tonic-gate 	Verbose = fmd_prop_get_int32(hdl, "verbose");
3320Sstevel@tonic-gate 	Warn = fmd_prop_get_int32(hdl, "warn");
3330Sstevel@tonic-gate 	Hesitate = fmd_prop_get_int64(hdl, "hesitate");
3345204Sstephh 	Serd_Override = fmd_prop_get_string(hdl, "serd_override");
335814Sjrutt 	Max_fme = fmd_prop_get_int32(hdl, "maxfme");
3360Sstevel@tonic-gate 
3377197Sstephh 	out(O_DEBUG, "initialized, verbose %d warn %d maxfme %d",
3387197Sstephh 	    Verbose, Warn, Max_fme);
3390Sstevel@tonic-gate 
3404436Sstephh 	fme_istat_load(hdl);
3414436Sstephh 	fme_serd_load(hdl);
3424436Sstephh 
343*12967Sgavin.maltby@oracle.com 	out(O_VERB, "reconstituting any existing fmes");
3440Sstevel@tonic-gate 	while ((casep = fmd_case_next(hdl, casep)) != NULL) {
3450Sstevel@tonic-gate 		fme_restart(hdl, casep);
3460Sstevel@tonic-gate 	}
3470Sstevel@tonic-gate }
3480Sstevel@tonic-gate 
3490Sstevel@tonic-gate /*ARGSUSED*/
3500Sstevel@tonic-gate void
_fmd_fini(fmd_hdl_t * hdl)3510Sstevel@tonic-gate _fmd_fini(fmd_hdl_t *hdl)
3520Sstevel@tonic-gate {
3530Sstevel@tonic-gate 	call_finis();
3540Sstevel@tonic-gate }
355