17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
508f6c065Sgavinm * Common Development and Distribution License (the "License").
608f6c065Sgavinm * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
21d9638e54Smws
227c478bd9Sstevel@tonic-gate /*
23*f6e214c7SGavin Maltby * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
247c478bd9Sstevel@tonic-gate */
257c478bd9Sstevel@tonic-gate
26*f6e214c7SGavin Maltby #include <unistd.h>
277c478bd9Sstevel@tonic-gate #include <stdio.h>
287c478bd9Sstevel@tonic-gate #include <string.h>
297c478bd9Sstevel@tonic-gate #include <fm/fmd_api.h>
307c478bd9Sstevel@tonic-gate #include <libnvpair.h>
31e5ba14ffSstephh #include <fm/libtopo.h>
327c478bd9Sstevel@tonic-gate #include "out.h"
337c478bd9Sstevel@tonic-gate #include "stats.h"
347c478bd9Sstevel@tonic-gate #include "alloc.h"
357c478bd9Sstevel@tonic-gate #include "stable.h"
367c478bd9Sstevel@tonic-gate #include "literals.h"
377c478bd9Sstevel@tonic-gate #include "lut.h"
387c478bd9Sstevel@tonic-gate #include "esclex.h"
397c478bd9Sstevel@tonic-gate #include "tree.h"
407c478bd9Sstevel@tonic-gate #include "ipath.h"
417c478bd9Sstevel@tonic-gate #include "itree.h"
427aec1d6eScindi #include "iexpr.h"
437c478bd9Sstevel@tonic-gate #include "ptree.h"
447c478bd9Sstevel@tonic-gate #include "check.h"
457c478bd9Sstevel@tonic-gate #include "version.h"
467c478bd9Sstevel@tonic-gate #include "fme.h"
477c478bd9Sstevel@tonic-gate #include "eval.h"
487c478bd9Sstevel@tonic-gate #include "config.h"
497c478bd9Sstevel@tonic-gate #include "platform.h"
507c478bd9Sstevel@tonic-gate
517c478bd9Sstevel@tonic-gate /*
527c478bd9Sstevel@tonic-gate * eversholt diagnosis engine (eft.so) main entry points
537c478bd9Sstevel@tonic-gate */
547c478bd9Sstevel@tonic-gate
557c478bd9Sstevel@tonic-gate fmd_hdl_t *Hdl; /* handle in global for platform.c */
567c478bd9Sstevel@tonic-gate
577c478bd9Sstevel@tonic-gate int Debug = 1; /* turn on here and let fmd_hdl_debug() decide if really on */
587c478bd9Sstevel@tonic-gate hrtime_t Hesitate; /* hesitation time in ns */
59e5ba14ffSstephh char *Serd_Override; /* override for Serd engines */
607c478bd9Sstevel@tonic-gate int Verbose;
617c478bd9Sstevel@tonic-gate int Estats;
627c478bd9Sstevel@tonic-gate int Warn; /* zero -- eft.so should not issue language warnings */
637c478bd9Sstevel@tonic-gate char **Efts;
640cc1f05eSjrutt int Max_fme; /* Maximum number of open FMEs */
657c478bd9Sstevel@tonic-gate
667c478bd9Sstevel@tonic-gate /* stuff exported by yacc-generated parsers */
677c478bd9Sstevel@tonic-gate extern void yyparse(void);
687c478bd9Sstevel@tonic-gate extern int yydebug;
697c478bd9Sstevel@tonic-gate
707c478bd9Sstevel@tonic-gate extern struct lut *Dicts;
717c478bd9Sstevel@tonic-gate
727c478bd9Sstevel@tonic-gate extern void literals_init(void);
737c478bd9Sstevel@tonic-gate extern void literals_fini(void);
747c478bd9Sstevel@tonic-gate
7508f6c065Sgavinm struct eftsubr {
7608f6c065Sgavinm const char *prefix;
7708f6c065Sgavinm void (*hdlr)(fmd_hdl_t *, fmd_event_t *, nvlist_t *, const char *);
7808f6c065Sgavinm } eftsubrs[] = {
7908f6c065Sgavinm { "ereport.", fme_receive_external_report },
8008f6c065Sgavinm { "list.repaired", fme_receive_repair_list },
8108f6c065Sgavinm { NULL, NULL }
8208f6c065Sgavinm };
8308f6c065Sgavinm
847c478bd9Sstevel@tonic-gate /*ARGSUSED*/
857c478bd9Sstevel@tonic-gate static void
eft_recv(fmd_hdl_t * hdl,fmd_event_t * ep,nvlist_t * nvl,const char * class)867c478bd9Sstevel@tonic-gate eft_recv(fmd_hdl_t *hdl, fmd_event_t *ep, nvlist_t *nvl, const char *class)
877c478bd9Sstevel@tonic-gate {
8808f6c065Sgavinm struct eftsubr *sp = eftsubrs;
897c478bd9Sstevel@tonic-gate
9008f6c065Sgavinm while (sp->prefix != NULL) {
9108f6c065Sgavinm if (strncmp(class, sp->prefix, strlen(sp->prefix)) == 0)
9208f6c065Sgavinm break;
9308f6c065Sgavinm sp++;
9408f6c065Sgavinm }
957c478bd9Sstevel@tonic-gate
9608f6c065Sgavinm if (sp->prefix != NULL) {
9708f6c065Sgavinm (sp->hdlr)(hdl, ep, nvl, class);
9808f6c065Sgavinm } else {
9908f6c065Sgavinm out(O_DIE,
10008f6c065Sgavinm "eft_recv: event class \"%s\" does not match our "
10108f6c065Sgavinm "subscriptions", class);
10208f6c065Sgavinm }
1037c478bd9Sstevel@tonic-gate }
1047c478bd9Sstevel@tonic-gate
1057c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1067c478bd9Sstevel@tonic-gate static void
eft_timeout(fmd_hdl_t * hdl,id_t tid,void * arg)1077c478bd9Sstevel@tonic-gate eft_timeout(fmd_hdl_t *hdl, id_t tid, void *arg)
1087c478bd9Sstevel@tonic-gate {
1097c478bd9Sstevel@tonic-gate out(O_ALTFP|O_STAMP,
1107c478bd9Sstevel@tonic-gate "\neft.so timer %ld fired with arg %p", tid, arg);
1117c478bd9Sstevel@tonic-gate
1127c478bd9Sstevel@tonic-gate if (arg == NULL)
1137c478bd9Sstevel@tonic-gate return;
1147c478bd9Sstevel@tonic-gate
1157c478bd9Sstevel@tonic-gate fme_timer_fired(arg, tid);
1167c478bd9Sstevel@tonic-gate }
1177c478bd9Sstevel@tonic-gate
1187c478bd9Sstevel@tonic-gate static void
eft_close(fmd_hdl_t * hdl,fmd_case_t * fmcase)1197c478bd9Sstevel@tonic-gate eft_close(fmd_hdl_t *hdl, fmd_case_t *fmcase)
1207c478bd9Sstevel@tonic-gate {
1217c478bd9Sstevel@tonic-gate out(O_ALTFP, "eft_close called for case %s",
1227c478bd9Sstevel@tonic-gate fmd_case_uuid(hdl, fmcase));
1237c478bd9Sstevel@tonic-gate fme_close_case(hdl, fmcase);
1247c478bd9Sstevel@tonic-gate }
1257c478bd9Sstevel@tonic-gate
126e5ba14ffSstephh /*
127e5ba14ffSstephh * The "serd_override" property allows the N and T parameters of specified serd
128e5ba14ffSstephh * engines to be overridden. The property is a string consisting of one or more
129e5ba14ffSstephh * space separated triplets. Each triplet is of the form "name,N,T" where "name"
130e5ba14ffSstephh * is the name of the serd engine and N and T are the new paremeters to use.
131e5ba14ffSstephh * For example "serd.io.device.nonfatal,5,3h" would set the parameters for the
132e5ba14ffSstephh * serd.io.device.nonfatal engine to 5 in 3 hours.
133e5ba14ffSstephh */
1347c478bd9Sstevel@tonic-gate static const fmd_prop_t eft_props[] = {
1357c478bd9Sstevel@tonic-gate { "estats", FMD_TYPE_BOOL, "false" },
1367c478bd9Sstevel@tonic-gate { "hesitate", FMD_TYPE_INT64, "10000000000" },
137e5ba14ffSstephh { "serd_override", FMD_TYPE_STRING, NULL },
1387c478bd9Sstevel@tonic-gate { "verbose", FMD_TYPE_INT32, "0" },
1397c478bd9Sstevel@tonic-gate { "warn", FMD_TYPE_BOOL, "false" },
1407c478bd9Sstevel@tonic-gate { "status", FMD_TYPE_STRING, NULL },
1410cc1f05eSjrutt { "maxfme", FMD_TYPE_INT32, "0" },
1427c478bd9Sstevel@tonic-gate { NULL, 0, NULL }
1437c478bd9Sstevel@tonic-gate };
1447c478bd9Sstevel@tonic-gate
145e5ba14ffSstephh /*ARGSUSED*/
146e5ba14ffSstephh static void
eft_topo_change(fmd_hdl_t * hdl,topo_hdl_t * thp)147e5ba14ffSstephh eft_topo_change(fmd_hdl_t *hdl, topo_hdl_t *thp)
148e5ba14ffSstephh {
149e5ba14ffSstephh fme_receive_topology_change();
150e5ba14ffSstephh }
151e5ba14ffSstephh
1527c478bd9Sstevel@tonic-gate static const fmd_hdl_ops_t eft_ops = {
1537c478bd9Sstevel@tonic-gate eft_recv, /* fmdo_recv */
1547c478bd9Sstevel@tonic-gate eft_timeout, /* fmdo_timeout */
1557c478bd9Sstevel@tonic-gate eft_close, /* fmdo_close */
1567c478bd9Sstevel@tonic-gate NULL, /* fmdo_stats */
1577c478bd9Sstevel@tonic-gate NULL, /* fmdo_gc */
158e5ba14ffSstephh NULL, /* fmdo_send */
159e5ba14ffSstephh eft_topo_change /* fmdo_topo_change */
1607c478bd9Sstevel@tonic-gate };
1617c478bd9Sstevel@tonic-gate
1627c478bd9Sstevel@tonic-gate #define xstr(s) str(s)
1637c478bd9Sstevel@tonic-gate #define str(s) #s
1647c478bd9Sstevel@tonic-gate
1657c478bd9Sstevel@tonic-gate static const fmd_hdl_info_t fmd_info = {
1667c478bd9Sstevel@tonic-gate "eft diagnosis engine",
1677c478bd9Sstevel@tonic-gate xstr(VERSION_MAJOR) "." xstr(VERSION_MINOR),
1687c478bd9Sstevel@tonic-gate &eft_ops, eft_props
1697c478bd9Sstevel@tonic-gate };
1707c478bd9Sstevel@tonic-gate
1717c478bd9Sstevel@tonic-gate /*
1727c478bd9Sstevel@tonic-gate * ename_strdup -- like strdup but ename comes in and class string goes out
1737c478bd9Sstevel@tonic-gate */
1747c478bd9Sstevel@tonic-gate static char *
ename_strdup(struct node * np)1757c478bd9Sstevel@tonic-gate ename_strdup(struct node *np)
1767c478bd9Sstevel@tonic-gate {
1777c478bd9Sstevel@tonic-gate struct node *mynp;
1787c478bd9Sstevel@tonic-gate int len;
1797c478bd9Sstevel@tonic-gate char *buf;
1807c478bd9Sstevel@tonic-gate
1817c478bd9Sstevel@tonic-gate /* calculate length of buffer required */
1827c478bd9Sstevel@tonic-gate len = 0;
1837c478bd9Sstevel@tonic-gate for (mynp = np; mynp; mynp = mynp->u.name.next)
1847c478bd9Sstevel@tonic-gate len += strlen(mynp->u.name.s) + 1; /* +1 for dot or NULL */
1857c478bd9Sstevel@tonic-gate
1867c478bd9Sstevel@tonic-gate buf = MALLOC(len);
1877c478bd9Sstevel@tonic-gate buf[0] = '\0';
1887c478bd9Sstevel@tonic-gate
1897c478bd9Sstevel@tonic-gate /* now build the string */
1907c478bd9Sstevel@tonic-gate while (np) {
1917c478bd9Sstevel@tonic-gate (void) strcat(buf, np->u.name.s);
1927c478bd9Sstevel@tonic-gate np = np->u.name.next;
1937c478bd9Sstevel@tonic-gate if (np)
1947c478bd9Sstevel@tonic-gate (void) strcat(buf, ".");
1957c478bd9Sstevel@tonic-gate }
1967c478bd9Sstevel@tonic-gate
1977c478bd9Sstevel@tonic-gate return (buf);
1987c478bd9Sstevel@tonic-gate }
1997c478bd9Sstevel@tonic-gate
2007c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2017c478bd9Sstevel@tonic-gate static void
dosubscribe(struct node * lhs,struct node * rhs,void * arg)2027c478bd9Sstevel@tonic-gate dosubscribe(struct node *lhs, struct node *rhs, void *arg)
2037c478bd9Sstevel@tonic-gate {
2047c478bd9Sstevel@tonic-gate char *ename = ename_strdup(lhs);
2057c478bd9Sstevel@tonic-gate
2067c478bd9Sstevel@tonic-gate fmd_hdl_subscribe(Hdl, ename);
2077c478bd9Sstevel@tonic-gate FREE(ename);
2087c478bd9Sstevel@tonic-gate }
2097c478bd9Sstevel@tonic-gate
210602ca9eaScth /*ARGSUSED*/
211602ca9eaScth static void
dodiscardprint(struct node * lhs,struct node * rhs,void * arg)212602ca9eaScth dodiscardprint(struct node *lhs, struct node *rhs, void *arg)
213602ca9eaScth {
214602ca9eaScth char *ename = (char *)lhs;
215602ca9eaScth
216*f6e214c7SGavin Maltby out(O_VERB, "allow silent discard_if_config_unknown: \"%s\"", ename);
217602ca9eaScth }
218602ca9eaScth
2197c478bd9Sstevel@tonic-gate extern struct stats *Filecount;
2207c478bd9Sstevel@tonic-gate
2217c478bd9Sstevel@tonic-gate /*
2227c478bd9Sstevel@tonic-gate * Call all of the _fini() routines to clean up the exiting DE
2237c478bd9Sstevel@tonic-gate */
2247c478bd9Sstevel@tonic-gate void
call_finis(void)2257c478bd9Sstevel@tonic-gate call_finis(void)
2267c478bd9Sstevel@tonic-gate {
2277c478bd9Sstevel@tonic-gate platform_free_eft_files(Efts);
2287c478bd9Sstevel@tonic-gate Efts = NULL;
2297c478bd9Sstevel@tonic-gate platform_fini();
2307c478bd9Sstevel@tonic-gate fme_fini();
2317c478bd9Sstevel@tonic-gate itree_fini();
2327c478bd9Sstevel@tonic-gate ipath_fini();
2337aec1d6eScindi iexpr_fini();
2347aec1d6eScindi istat_fini();
235b5016cbbSstephh serd_fini();
2367c478bd9Sstevel@tonic-gate lex_free();
2377c478bd9Sstevel@tonic-gate check_fini();
2387c478bd9Sstevel@tonic-gate tree_fini();
2397c478bd9Sstevel@tonic-gate lut_fini();
2407c478bd9Sstevel@tonic-gate literals_fini();
2417c478bd9Sstevel@tonic-gate stable_fini();
2427c478bd9Sstevel@tonic-gate stats_fini();
2437c478bd9Sstevel@tonic-gate out_fini();
2447c478bd9Sstevel@tonic-gate alloc_fini();
2457c478bd9Sstevel@tonic-gate }
2467c478bd9Sstevel@tonic-gate
2477c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2487c478bd9Sstevel@tonic-gate static void
doopendict(const char * lhs,void * rhs,void * arg)2497c478bd9Sstevel@tonic-gate doopendict(const char *lhs, void *rhs, void *arg)
2507c478bd9Sstevel@tonic-gate {
251*f6e214c7SGavin Maltby out(O_VERB, "opendict: \"%s\"", lhs);
2527c478bd9Sstevel@tonic-gate fmd_hdl_opendict(Hdl, lhs);
2537c478bd9Sstevel@tonic-gate }
2547c478bd9Sstevel@tonic-gate
2557c478bd9Sstevel@tonic-gate void
_fmd_init(fmd_hdl_t * hdl)2567c478bd9Sstevel@tonic-gate _fmd_init(fmd_hdl_t *hdl)
2577c478bd9Sstevel@tonic-gate {
2587c478bd9Sstevel@tonic-gate fmd_case_t *casep = NULL;
2597c478bd9Sstevel@tonic-gate int count;
2607c478bd9Sstevel@tonic-gate char *fname;
2617c478bd9Sstevel@tonic-gate
2627c478bd9Sstevel@tonic-gate (void) fmd_hdl_register(hdl, FMD_API_VERSION, &fmd_info);
2637c478bd9Sstevel@tonic-gate
2647c478bd9Sstevel@tonic-gate /* keep handle for routines like out() which need it */
2657c478bd9Sstevel@tonic-gate Hdl = hdl;
2667c478bd9Sstevel@tonic-gate
267602ca9eaScth /* set up out(O_ALTFP) first things so it is available for debug */
2687c478bd9Sstevel@tonic-gate alloc_init();
2697c478bd9Sstevel@tonic-gate out_init("eft");
270602ca9eaScth if ((fname = fmd_prop_get_string(hdl, "status")) != NULL) {
271602ca9eaScth FILE *fp;
272602ca9eaScth
273602ca9eaScth if ((fp = fopen(fname, "a")) == NULL) {
274602ca9eaScth fmd_prop_free_string(hdl, fname);
275602ca9eaScth out(O_DIE|O_SYS, "status property file: %s", fname);
276602ca9eaScth }
277602ca9eaScth
278602ca9eaScth (void) setlinebuf(fp);
279602ca9eaScth out_altfp(fp);
280602ca9eaScth
281602ca9eaScth out(O_DEBUG, "appending status changes to \"%s\"", fname);
282602ca9eaScth fmd_prop_free_string(hdl, fname);
283602ca9eaScth
284602ca9eaScth out(O_ALTFP|O_STAMP, "\neft.so startup");
285602ca9eaScth }
286602ca9eaScth
287602ca9eaScth
288602ca9eaScth Estats = fmd_prop_get_int32(hdl, "estats");
2897c478bd9Sstevel@tonic-gate stats_init(Estats);
290602ca9eaScth
2917c478bd9Sstevel@tonic-gate stable_init(0);
2927c478bd9Sstevel@tonic-gate literals_init();
2937c478bd9Sstevel@tonic-gate platform_init();
2947c478bd9Sstevel@tonic-gate lut_init();
2957c478bd9Sstevel@tonic-gate tree_init();
2967c478bd9Sstevel@tonic-gate ipath_init();
2977aec1d6eScindi iexpr_init();
2987c478bd9Sstevel@tonic-gate Efts = platform_get_eft_files();
2997c478bd9Sstevel@tonic-gate lex_init(Efts, NULL, 0);
3007c478bd9Sstevel@tonic-gate check_init();
3017c478bd9Sstevel@tonic-gate
3027c478bd9Sstevel@tonic-gate /*
3037c478bd9Sstevel@tonic-gate * If we read no .eft files, we can't do any
3047c478bd9Sstevel@tonic-gate * diagnosing, so we just unload ourselves
3057c478bd9Sstevel@tonic-gate */
3067c478bd9Sstevel@tonic-gate if (stats_counter_value(Filecount) == 0) {
3077c478bd9Sstevel@tonic-gate (void) lex_fini();
3087c478bd9Sstevel@tonic-gate call_finis();
3097c478bd9Sstevel@tonic-gate fmd_hdl_debug(hdl, "no fault trees provided.");
3107c478bd9Sstevel@tonic-gate fmd_hdl_unregister(hdl);
3117c478bd9Sstevel@tonic-gate return;
3127c478bd9Sstevel@tonic-gate }
3137c478bd9Sstevel@tonic-gate
3147c478bd9Sstevel@tonic-gate yyparse();
3157c478bd9Sstevel@tonic-gate (void) lex_fini();
3167c478bd9Sstevel@tonic-gate tree_report();
3177c478bd9Sstevel@tonic-gate if (count = out_errcount())
3187c478bd9Sstevel@tonic-gate out(O_DIE, "%d language error%s encountered, exiting.",
3197c478bd9Sstevel@tonic-gate OUTS(count));
3207c478bd9Sstevel@tonic-gate
3217c478bd9Sstevel@tonic-gate /* subscribe to events we expect to consume */
3227c478bd9Sstevel@tonic-gate lut_walk(Ereportenames, (lut_cb)dosubscribe, NULL);
323602ca9eaScth lut_walk(Ereportenames_discard, (lut_cb)dodiscardprint, NULL);
3247c478bd9Sstevel@tonic-gate
32508f6c065Sgavinm /* subscribe to repair events so we can clear state on repair */
32608f6c065Sgavinm fmd_hdl_subscribe(hdl, "list.repaired");
32708f6c065Sgavinm
3287c478bd9Sstevel@tonic-gate /* open dictionaries referenced by all .eft files */
3297c478bd9Sstevel@tonic-gate lut_walk(Dicts, (lut_cb)doopendict, (void *)0);
3307c478bd9Sstevel@tonic-gate
3317c478bd9Sstevel@tonic-gate Verbose = fmd_prop_get_int32(hdl, "verbose");
3327c478bd9Sstevel@tonic-gate Warn = fmd_prop_get_int32(hdl, "warn");
3337c478bd9Sstevel@tonic-gate Hesitate = fmd_prop_get_int64(hdl, "hesitate");
334e5ba14ffSstephh Serd_Override = fmd_prop_get_string(hdl, "serd_override");
3350cc1f05eSjrutt Max_fme = fmd_prop_get_int32(hdl, "maxfme");
3367c478bd9Sstevel@tonic-gate
337b7d3956bSstephh out(O_DEBUG, "initialized, verbose %d warn %d maxfme %d",
338b7d3956bSstephh Verbose, Warn, Max_fme);
3397c478bd9Sstevel@tonic-gate
340b5016cbbSstephh fme_istat_load(hdl);
341b5016cbbSstephh fme_serd_load(hdl);
342b5016cbbSstephh
343*f6e214c7SGavin Maltby out(O_VERB, "reconstituting any existing fmes");
3447c478bd9Sstevel@tonic-gate while ((casep = fmd_case_next(hdl, casep)) != NULL) {
3457c478bd9Sstevel@tonic-gate fme_restart(hdl, casep);
3467c478bd9Sstevel@tonic-gate }
3477c478bd9Sstevel@tonic-gate }
3487c478bd9Sstevel@tonic-gate
3497c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3507c478bd9Sstevel@tonic-gate void
_fmd_fini(fmd_hdl_t * hdl)3517c478bd9Sstevel@tonic-gate _fmd_fini(fmd_hdl_t *hdl)
3527c478bd9Sstevel@tonic-gate {
3537c478bd9Sstevel@tonic-gate call_finis();
3547c478bd9Sstevel@tonic-gate }
355