xref: /titanic_54/usr/src/cmd/fm/modules/common/eversholt/fme.c (revision aab83bb83be7342f6cfccaed8d5fe0b2f404855d)
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
55f25dc2aSgavinm  * Common Development and Distribution License (the "License").
65f25dc2aSgavinm  * 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  */
215f25dc2aSgavinm 
227c478bd9Sstevel@tonic-gate /*
23f5961f52SAdrian Frost  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
2433f5ff17SMilan Jurik  * Copyright 2012 Milan Jurik. All rights reserved.
257c478bd9Sstevel@tonic-gate  *
267c478bd9Sstevel@tonic-gate  * fme.c -- fault management exercise module
277c478bd9Sstevel@tonic-gate  *
287c478bd9Sstevel@tonic-gate  * this module provides the simulated fault management exercise.
297c478bd9Sstevel@tonic-gate  */
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #include <stdio.h>
327c478bd9Sstevel@tonic-gate #include <stdlib.h>
337c478bd9Sstevel@tonic-gate #include <string.h>
347c478bd9Sstevel@tonic-gate #include <strings.h>
357c478bd9Sstevel@tonic-gate #include <ctype.h>
367c478bd9Sstevel@tonic-gate #include <alloca.h>
377c478bd9Sstevel@tonic-gate #include <libnvpair.h>
387c478bd9Sstevel@tonic-gate #include <sys/fm/protocol.h>
397c478bd9Sstevel@tonic-gate #include <fm/fmd_api.h>
407c478bd9Sstevel@tonic-gate #include "alloc.h"
417c478bd9Sstevel@tonic-gate #include "out.h"
427c478bd9Sstevel@tonic-gate #include "stats.h"
437c478bd9Sstevel@tonic-gate #include "stable.h"
447c478bd9Sstevel@tonic-gate #include "literals.h"
457c478bd9Sstevel@tonic-gate #include "lut.h"
467c478bd9Sstevel@tonic-gate #include "tree.h"
477c478bd9Sstevel@tonic-gate #include "ptree.h"
487c478bd9Sstevel@tonic-gate #include "itree.h"
497c478bd9Sstevel@tonic-gate #include "ipath.h"
507c478bd9Sstevel@tonic-gate #include "fme.h"
517c478bd9Sstevel@tonic-gate #include "evnv.h"
527c478bd9Sstevel@tonic-gate #include "eval.h"
537c478bd9Sstevel@tonic-gate #include "config.h"
547c478bd9Sstevel@tonic-gate #include "platform.h"
55e5ba14ffSstephh #include "esclex.h"
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate /* imported from eft.c... */
587c478bd9Sstevel@tonic-gate extern hrtime_t Hesitate;
59e5ba14ffSstephh extern char *Serd_Override;
607c478bd9Sstevel@tonic-gate extern nv_alloc_t Eft_nv_hdl;
610cc1f05eSjrutt extern int Max_fme;
627aec1d6eScindi extern fmd_hdl_t *Hdl;
637aec1d6eScindi 
647aec1d6eScindi static int Istat_need_save;
65b5016cbbSstephh static int Serd_need_save;
6608f6c065Sgavinm void istat_save(void);
67b5016cbbSstephh void serd_save(void);
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate /* fme under construction is global so we can free it on module abort */
707c478bd9Sstevel@tonic-gate static struct fme *Nfmep;
717c478bd9Sstevel@tonic-gate 
7232d4e834STarik Soydan static int Undiag_reason = UD_VAL_UNKNOWN;
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate static int Nextid = 0;
757c478bd9Sstevel@tonic-gate 
760cc1f05eSjrutt static int Open_fme_count = 0;	/* Count of open FMEs */
770cc1f05eSjrutt 
787c478bd9Sstevel@tonic-gate /* list of fault management exercises underway */
797c478bd9Sstevel@tonic-gate static struct fme {
807c478bd9Sstevel@tonic-gate 	struct fme *next;		/* next exercise */
817c478bd9Sstevel@tonic-gate 	unsigned long long ull;		/* time when fme was created */
827c478bd9Sstevel@tonic-gate 	int id;				/* FME id */
83e5ba14ffSstephh 	struct config *config;		/* cooked configuration data */
847c478bd9Sstevel@tonic-gate 	struct lut *eventtree;		/* propagation tree for this FME */
857c478bd9Sstevel@tonic-gate 	/*
867c478bd9Sstevel@tonic-gate 	 * The initial error report that created this FME is kept in
877c478bd9Sstevel@tonic-gate 	 * two forms.  e0 points to the instance tree node and is used
887c478bd9Sstevel@tonic-gate 	 * by fme_eval() as the starting point for the inference
897c478bd9Sstevel@tonic-gate 	 * algorithm.  e0r is the event handle FMD passed to us when
907c478bd9Sstevel@tonic-gate 	 * the ereport first arrived and is used when setting timers,
917c478bd9Sstevel@tonic-gate 	 * which are always relative to the time of this initial
927c478bd9Sstevel@tonic-gate 	 * report.
937c478bd9Sstevel@tonic-gate 	 */
947c478bd9Sstevel@tonic-gate 	struct event *e0;
957c478bd9Sstevel@tonic-gate 	fmd_event_t *e0r;
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate 	id_t    timer;			/* for setting an fmd time-out */
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate 	struct event *ecurrent;		/* ereport under consideration */
1007c478bd9Sstevel@tonic-gate 	struct event *suspects;		/* current suspect list */
1017c478bd9Sstevel@tonic-gate 	struct event *psuspects;	/* previous suspect list */
1027c478bd9Sstevel@tonic-gate 	int nsuspects;			/* count of suspects */
1037c478bd9Sstevel@tonic-gate 	int posted_suspects;		/* true if we've posted a diagnosis */
1047c478bd9Sstevel@tonic-gate 	int uniqobs;			/* number of unique events observed */
1057c478bd9Sstevel@tonic-gate 	int peek;			/* just peeking, don't track suspects */
1060cc1f05eSjrutt 	int overflow;			/* true if overflow FME */
1077c478bd9Sstevel@tonic-gate 	enum fme_state {
1087c478bd9Sstevel@tonic-gate 		FME_NOTHING = 5000,	/* not evaluated yet */
1097c478bd9Sstevel@tonic-gate 		FME_WAIT,		/* need to wait for more info */
1107c478bd9Sstevel@tonic-gate 		FME_CREDIBLE,		/* suspect list is credible */
1117aec1d6eScindi 		FME_DISPROVED,		/* no valid suspects found */
1127aec1d6eScindi 		FME_DEFERRED		/* don't know yet (k-count not met) */
1137c478bd9Sstevel@tonic-gate 	} state;
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate 	unsigned long long pull;	/* time passed since created */
1167c478bd9Sstevel@tonic-gate 	unsigned long long wull;	/* wait until this time for re-eval */
1177c478bd9Sstevel@tonic-gate 	struct event *observations;	/* observation list */
1187c478bd9Sstevel@tonic-gate 	struct lut *globals;		/* values of global variables */
1197c478bd9Sstevel@tonic-gate 	/* fmd interfacing */
1207c478bd9Sstevel@tonic-gate 	fmd_hdl_t *hdl;			/* handle for talking with fmd */
1217c478bd9Sstevel@tonic-gate 	fmd_case_t *fmcase;		/* what fmd 'case' we associate with */
1227c478bd9Sstevel@tonic-gate 	/* stats */
1237c478bd9Sstevel@tonic-gate 	struct stats *Rcount;
1247c478bd9Sstevel@tonic-gate 	struct stats *Hcallcount;
1257c478bd9Sstevel@tonic-gate 	struct stats *Rcallcount;
1267c478bd9Sstevel@tonic-gate 	struct stats *Ccallcount;
1277c478bd9Sstevel@tonic-gate 	struct stats *Ecallcount;
1287c478bd9Sstevel@tonic-gate 	struct stats *Tcallcount;
1297c478bd9Sstevel@tonic-gate 	struct stats *Marrowcount;
1307c478bd9Sstevel@tonic-gate 	struct stats *diags;
1317c478bd9Sstevel@tonic-gate } *FMElist, *EFMElist, *ClosedFMEs;
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate static struct case_list {
1347c478bd9Sstevel@tonic-gate 	fmd_case_t *fmcase;
1357c478bd9Sstevel@tonic-gate 	struct case_list *next;
1367c478bd9Sstevel@tonic-gate } *Undiagablecaselist;
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate static void fme_eval(struct fme *fmep, fmd_event_t *ffep);
1397c478bd9Sstevel@tonic-gate static enum fme_state hypothesise(struct fme *fmep, struct event *ep,
1407aec1d6eScindi 	unsigned long long at_latest_by, unsigned long long *pdelay);
1417c478bd9Sstevel@tonic-gate static struct node *eventprop_lookup(struct event *ep, const char *propname);
1427c478bd9Sstevel@tonic-gate static struct node *pathstring2epnamenp(char *path);
143b5016cbbSstephh static void publish_undiagnosable(fmd_hdl_t *hdl, fmd_event_t *ffep,
144705e9f42SStephen Hanson 	fmd_case_t *fmcase, nvlist_t *detector, char *arg);
145705e9f42SStephen Hanson static char *undiag_2reason_str(int ud, char *arg);
14632d4e834STarik Soydan static const char *undiag_2defect_str(int ud);
1477c478bd9Sstevel@tonic-gate static void restore_suspects(struct fme *fmep);
1487c478bd9Sstevel@tonic-gate static void save_suspects(struct fme *fmep);
1497c478bd9Sstevel@tonic-gate static void destroy_fme(struct fme *f);
1507c478bd9Sstevel@tonic-gate static void fme_receive_report(fmd_hdl_t *hdl, fmd_event_t *ffep,
1517c478bd9Sstevel@tonic-gate     const char *eventstring, const struct ipath *ipp, nvlist_t *nvl);
15208f6c065Sgavinm static void istat_counter_reset_cb(struct istat_entry *entp,
15308f6c065Sgavinm     struct stats *statp, const struct ipath *ipp);
154e5ba14ffSstephh static void istat_counter_topo_chg_cb(struct istat_entry *entp,
155e5ba14ffSstephh     struct stats *statp, void *unused);
156b5016cbbSstephh static void serd_reset_cb(struct serd_entry *entp, void *unused,
157b5016cbbSstephh     const struct ipath *ipp);
158e5ba14ffSstephh static void serd_topo_chg_cb(struct serd_entry *entp, void *unused,
159e5ba14ffSstephh     void *unused2);
160b5016cbbSstephh static void destroy_fme_bufs(struct fme *fp);
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate static struct fme *
alloc_fme(void)1637c478bd9Sstevel@tonic-gate alloc_fme(void)
1647c478bd9Sstevel@tonic-gate {
1657c478bd9Sstevel@tonic-gate 	struct fme *fmep;
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 	fmep = MALLOC(sizeof (*fmep));
1687c478bd9Sstevel@tonic-gate 	bzero(fmep, sizeof (*fmep));
1697c478bd9Sstevel@tonic-gate 	return (fmep);
1707c478bd9Sstevel@tonic-gate }
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate /*
1737c478bd9Sstevel@tonic-gate  * fme_ready -- called when all initialization of the FME (except for
1747c478bd9Sstevel@tonic-gate  *	stats) has completed successfully.  Adds the fme to global lists
1757c478bd9Sstevel@tonic-gate  *	and establishes its stats.
1767c478bd9Sstevel@tonic-gate  */
1777c478bd9Sstevel@tonic-gate static struct fme *
fme_ready(struct fme * fmep)1787c478bd9Sstevel@tonic-gate fme_ready(struct fme *fmep)
1797c478bd9Sstevel@tonic-gate {
1807c478bd9Sstevel@tonic-gate 	char nbuf[100];
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 	Nfmep = NULL;	/* don't need to free this on module abort now */
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 	if (EFMElist) {
1857c478bd9Sstevel@tonic-gate 		EFMElist->next = fmep;
1867c478bd9Sstevel@tonic-gate 		EFMElist = fmep;
1877c478bd9Sstevel@tonic-gate 	} else
1887c478bd9Sstevel@tonic-gate 		FMElist = EFMElist = fmep;
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate 	(void) sprintf(nbuf, "fme%d.Rcount", fmep->id);
1917c478bd9Sstevel@tonic-gate 	fmep->Rcount = stats_new_counter(nbuf, "ereports received", 0);
1927c478bd9Sstevel@tonic-gate 	(void) sprintf(nbuf, "fme%d.Hcall", fmep->id);
1937c478bd9Sstevel@tonic-gate 	fmep->Hcallcount = stats_new_counter(nbuf, "calls to hypothesise()", 1);
1947c478bd9Sstevel@tonic-gate 	(void) sprintf(nbuf, "fme%d.Rcall", fmep->id);
1957c478bd9Sstevel@tonic-gate 	fmep->Rcallcount = stats_new_counter(nbuf,
1967c478bd9Sstevel@tonic-gate 	    "calls to requirements_test()", 1);
1977c478bd9Sstevel@tonic-gate 	(void) sprintf(nbuf, "fme%d.Ccall", fmep->id);
1987c478bd9Sstevel@tonic-gate 	fmep->Ccallcount = stats_new_counter(nbuf, "calls to causes_test()", 1);
1997c478bd9Sstevel@tonic-gate 	(void) sprintf(nbuf, "fme%d.Ecall", fmep->id);
2007c478bd9Sstevel@tonic-gate 	fmep->Ecallcount =
2017c478bd9Sstevel@tonic-gate 	    stats_new_counter(nbuf, "calls to effects_test()", 1);
2027c478bd9Sstevel@tonic-gate 	(void) sprintf(nbuf, "fme%d.Tcall", fmep->id);
2037c478bd9Sstevel@tonic-gate 	fmep->Tcallcount = stats_new_counter(nbuf, "calls to triggered()", 1);
2047c478bd9Sstevel@tonic-gate 	(void) sprintf(nbuf, "fme%d.Marrow", fmep->id);
2057c478bd9Sstevel@tonic-gate 	fmep->Marrowcount = stats_new_counter(nbuf,
2067c478bd9Sstevel@tonic-gate 	    "arrows marked by mark_arrows()", 1);
2077c478bd9Sstevel@tonic-gate 	(void) sprintf(nbuf, "fme%d.diags", fmep->id);
2087c478bd9Sstevel@tonic-gate 	fmep->diags = stats_new_counter(nbuf, "suspect lists diagnosed", 0);
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 	out(O_ALTFP|O_VERB2, "newfme: config snapshot contains...");
211e5ba14ffSstephh 	config_print(O_ALTFP|O_VERB2, fmep->config);
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate 	return (fmep);
2147c478bd9Sstevel@tonic-gate }
2157c478bd9Sstevel@tonic-gate 
216b5016cbbSstephh extern void ipath_dummy_lut(struct arrow *);
217b5016cbbSstephh extern struct lut *itree_create_dummy(const char *, const struct ipath *);
218b5016cbbSstephh 
219b5016cbbSstephh /* ARGSUSED */
220b5016cbbSstephh static void
set_needed_arrows(struct event * ep,struct event * ep2,struct fme * fmep)221b5016cbbSstephh set_needed_arrows(struct event *ep, struct event *ep2, struct fme *fmep)
222b5016cbbSstephh {
223b5016cbbSstephh 	struct bubble *bp;
224b5016cbbSstephh 	struct arrowlist *ap;
225b5016cbbSstephh 
226b5016cbbSstephh 	for (bp = itree_next_bubble(ep, NULL); bp;
227b5016cbbSstephh 	    bp = itree_next_bubble(ep, bp)) {
228b5016cbbSstephh 		if (bp->t != B_FROM)
229b5016cbbSstephh 			continue;
230b5016cbbSstephh 		for (ap = itree_next_arrow(bp, NULL); ap;
231b5016cbbSstephh 		    ap = itree_next_arrow(bp, ap)) {
232b5016cbbSstephh 			ap->arrowp->pnode->u.arrow.needed = 1;
233b5016cbbSstephh 			ipath_dummy_lut(ap->arrowp);
234b5016cbbSstephh 		}
235b5016cbbSstephh 	}
236b5016cbbSstephh }
237b5016cbbSstephh 
238b5016cbbSstephh /* ARGSUSED */
239b5016cbbSstephh static void
unset_needed_arrows(struct event * ep,struct event * ep2,struct fme * fmep)240b5016cbbSstephh unset_needed_arrows(struct event *ep, struct event *ep2, struct fme *fmep)
241b5016cbbSstephh {
242b5016cbbSstephh 	struct bubble *bp;
243b5016cbbSstephh 	struct arrowlist *ap;
244b5016cbbSstephh 
245b5016cbbSstephh 	for (bp = itree_next_bubble(ep, NULL); bp;
246b5016cbbSstephh 	    bp = itree_next_bubble(ep, bp)) {
247b5016cbbSstephh 		if (bp->t != B_FROM)
248b5016cbbSstephh 			continue;
249b5016cbbSstephh 		for (ap = itree_next_arrow(bp, NULL); ap;
250b5016cbbSstephh 		    ap = itree_next_arrow(bp, ap))
251b5016cbbSstephh 			ap->arrowp->pnode->u.arrow.needed = 0;
252b5016cbbSstephh 	}
253b5016cbbSstephh }
254b5016cbbSstephh 
255b5016cbbSstephh static void globals_destructor(void *left, void *right, void *arg);
256b5016cbbSstephh static void clear_arrows(struct event *ep, struct event *ep2, struct fme *fmep);
257b5016cbbSstephh 
258705e9f42SStephen Hanson static boolean_t
prune_propagations(const char * e0class,const struct ipath * e0ipp)259b5016cbbSstephh prune_propagations(const char *e0class, const struct ipath *e0ipp)
260b5016cbbSstephh {
261b5016cbbSstephh 	char nbuf[100];
262b5016cbbSstephh 	unsigned long long my_delay = TIMEVAL_EVENTUALLY;
263b5016cbbSstephh 	extern struct lut *Usednames;
264b5016cbbSstephh 
265b5016cbbSstephh 	Nfmep = alloc_fme();
266b5016cbbSstephh 	Nfmep->id = Nextid;
267b5016cbbSstephh 	Nfmep->state = FME_NOTHING;
268b5016cbbSstephh 	Nfmep->eventtree = itree_create_dummy(e0class, e0ipp);
269b5016cbbSstephh 	if ((Nfmep->e0 =
270b5016cbbSstephh 	    itree_lookup(Nfmep->eventtree, e0class, e0ipp)) == NULL) {
271b5016cbbSstephh 		itree_free(Nfmep->eventtree);
272b5016cbbSstephh 		FREE(Nfmep);
273b5016cbbSstephh 		Nfmep = NULL;
274705e9f42SStephen Hanson 		return (B_FALSE);
275b5016cbbSstephh 	}
276b5016cbbSstephh 	Nfmep->ecurrent = Nfmep->observations = Nfmep->e0;
277b5016cbbSstephh 	Nfmep->e0->count++;
278b5016cbbSstephh 
279b5016cbbSstephh 	(void) sprintf(nbuf, "fme%d.Rcount", Nfmep->id);
280b5016cbbSstephh 	Nfmep->Rcount = stats_new_counter(nbuf, "ereports received", 0);
281b5016cbbSstephh 	(void) sprintf(nbuf, "fme%d.Hcall", Nfmep->id);
282b5016cbbSstephh 	Nfmep->Hcallcount =
283b5016cbbSstephh 	    stats_new_counter(nbuf, "calls to hypothesise()", 1);
284b5016cbbSstephh 	(void) sprintf(nbuf, "fme%d.Rcall", Nfmep->id);
285b5016cbbSstephh 	Nfmep->Rcallcount = stats_new_counter(nbuf,
286b5016cbbSstephh 	    "calls to requirements_test()", 1);
287b5016cbbSstephh 	(void) sprintf(nbuf, "fme%d.Ccall", Nfmep->id);
288b5016cbbSstephh 	Nfmep->Ccallcount =
289b5016cbbSstephh 	    stats_new_counter(nbuf, "calls to causes_test()", 1);
290b5016cbbSstephh 	(void) sprintf(nbuf, "fme%d.Ecall", Nfmep->id);
291b5016cbbSstephh 	Nfmep->Ecallcount =
292b5016cbbSstephh 	    stats_new_counter(nbuf, "calls to effects_test()", 1);
293b5016cbbSstephh 	(void) sprintf(nbuf, "fme%d.Tcall", Nfmep->id);
294b5016cbbSstephh 	Nfmep->Tcallcount = stats_new_counter(nbuf, "calls to triggered()", 1);
295b5016cbbSstephh 	(void) sprintf(nbuf, "fme%d.Marrow", Nfmep->id);
296b5016cbbSstephh 	Nfmep->Marrowcount = stats_new_counter(nbuf,
297b5016cbbSstephh 	    "arrows marked by mark_arrows()", 1);
298b5016cbbSstephh 	(void) sprintf(nbuf, "fme%d.diags", Nfmep->id);
299b5016cbbSstephh 	Nfmep->diags = stats_new_counter(nbuf, "suspect lists diagnosed", 0);
300b5016cbbSstephh 
301b5016cbbSstephh 	Nfmep->peek = 1;
302b5016cbbSstephh 	lut_walk(Nfmep->eventtree, (lut_cb)unset_needed_arrows, (void *)Nfmep);
303b5016cbbSstephh 	lut_free(Usednames, NULL, NULL);
304b5016cbbSstephh 	Usednames = NULL;
305b5016cbbSstephh 	lut_walk(Nfmep->eventtree, (lut_cb)clear_arrows, (void *)Nfmep);
306b5016cbbSstephh 	(void) hypothesise(Nfmep, Nfmep->e0, Nfmep->ull, &my_delay);
307b5016cbbSstephh 	itree_prune(Nfmep->eventtree);
308b5016cbbSstephh 	lut_walk(Nfmep->eventtree, (lut_cb)set_needed_arrows, (void *)Nfmep);
309b5016cbbSstephh 
310b5016cbbSstephh 	stats_delete(Nfmep->Rcount);
311b5016cbbSstephh 	stats_delete(Nfmep->Hcallcount);
312b5016cbbSstephh 	stats_delete(Nfmep->Rcallcount);
313b5016cbbSstephh 	stats_delete(Nfmep->Ccallcount);
314b5016cbbSstephh 	stats_delete(Nfmep->Ecallcount);
315b5016cbbSstephh 	stats_delete(Nfmep->Tcallcount);
316b5016cbbSstephh 	stats_delete(Nfmep->Marrowcount);
317b5016cbbSstephh 	stats_delete(Nfmep->diags);
318b5016cbbSstephh 	itree_free(Nfmep->eventtree);
319b5016cbbSstephh 	lut_free(Nfmep->globals, globals_destructor, NULL);
320b5016cbbSstephh 	FREE(Nfmep);
321705e9f42SStephen Hanson 	return (B_TRUE);
322b5016cbbSstephh }
323b5016cbbSstephh 
3247c478bd9Sstevel@tonic-gate static struct fme *
newfme(const char * e0class,const struct ipath * e0ipp,fmd_hdl_t * hdl,fmd_case_t * fmcase,fmd_event_t * ffep,nvlist_t * nvl)325b5016cbbSstephh newfme(const char *e0class, const struct ipath *e0ipp, fmd_hdl_t *hdl,
326705e9f42SStephen Hanson 	fmd_case_t *fmcase, fmd_event_t *ffep, nvlist_t *nvl)
3277c478bd9Sstevel@tonic-gate {
3287c478bd9Sstevel@tonic-gate 	struct cfgdata *cfgdata;
329b5016cbbSstephh 	int init_size;
330b5016cbbSstephh 	extern int alloc_total();
331705e9f42SStephen Hanson 	nvlist_t *detector = NULL;
332705e9f42SStephen Hanson 	char *pathstr;
333705e9f42SStephen Hanson 	char *arg;
3347c478bd9Sstevel@tonic-gate 
335705e9f42SStephen Hanson 	/*
336705e9f42SStephen Hanson 	 * First check if e0ipp is actually in the topology so we can give a
337705e9f42SStephen Hanson 	 * more useful error message.
338705e9f42SStephen Hanson 	 */
339705e9f42SStephen Hanson 	ipathlastcomp(e0ipp);
340705e9f42SStephen Hanson 	pathstr = ipath2str(NULL, e0ipp);
341705e9f42SStephen Hanson 	cfgdata = config_snapshot();
342705e9f42SStephen Hanson 	platform_units_translate(0, cfgdata->cooked, NULL, NULL,
343705e9f42SStephen Hanson 	    &detector, pathstr);
344705e9f42SStephen Hanson 	FREE(pathstr);
345705e9f42SStephen Hanson 	structconfig_free(cfgdata->cooked);
346705e9f42SStephen Hanson 	config_free(cfgdata);
347705e9f42SStephen Hanson 	if (detector == NULL) {
348f5961f52SAdrian Frost 		/* See if class permits silent discard on unknown component. */
349f5961f52SAdrian Frost 		if (lut_lookup(Ereportenames_discard, (void *)e0class, NULL)) {
350f5961f52SAdrian Frost 			out(O_ALTFP|O_VERB2, "Unable to map \"%s\" ereport "
351f5961f52SAdrian Frost 			    "to component path, but silent discard allowed.",
352f5961f52SAdrian Frost 			    e0class);
353f5961f52SAdrian Frost 		} else {
354705e9f42SStephen Hanson 			Undiag_reason = UD_VAL_BADEVENTPATH;
355705e9f42SStephen Hanson 			(void) nvlist_lookup_nvlist(nvl, FM_EREPORT_DETECTOR,
356705e9f42SStephen Hanson 			    &detector);
357705e9f42SStephen Hanson 			arg = ipath2str(e0class, e0ipp);
358705e9f42SStephen Hanson 			publish_undiagnosable(hdl, ffep, fmcase, detector, arg);
359705e9f42SStephen Hanson 			FREE(arg);
360f5961f52SAdrian Frost 		}
361705e9f42SStephen Hanson 		return (NULL);
362705e9f42SStephen Hanson 	}
363705e9f42SStephen Hanson 
364705e9f42SStephen Hanson 	/*
365705e9f42SStephen Hanson 	 * Next run a quick first pass of the rules with a dummy config. This
366705e9f42SStephen Hanson 	 * allows us to prune those rules which can't possibly cause this
367705e9f42SStephen Hanson 	 * ereport.
368705e9f42SStephen Hanson 	 */
369705e9f42SStephen Hanson 	if (!prune_propagations(e0class, e0ipp)) {
370705e9f42SStephen Hanson 		/*
371705e9f42SStephen Hanson 		 * The fault class must have been in the rules or we would
372705e9f42SStephen Hanson 		 * not have registered for it (and got a "nosub"), and the
373705e9f42SStephen Hanson 		 * pathname must be in the topology or we would have failed the
374705e9f42SStephen Hanson 		 * previous test. So to get here means the combination of
375705e9f42SStephen Hanson 		 * class and pathname in the ereport must be invalid.
376705e9f42SStephen Hanson 		 */
377705e9f42SStephen Hanson 		Undiag_reason = UD_VAL_BADEVENTCLASS;
378705e9f42SStephen Hanson 		arg = ipath2str(e0class, e0ipp);
379705e9f42SStephen Hanson 		publish_undiagnosable(hdl, ffep, fmcase, detector, arg);
380705e9f42SStephen Hanson 		nvlist_free(detector);
381705e9f42SStephen Hanson 		FREE(arg);
382705e9f42SStephen Hanson 		return (NULL);
383705e9f42SStephen Hanson 	}
384705e9f42SStephen Hanson 
385705e9f42SStephen Hanson 	/*
386705e9f42SStephen Hanson 	 * Now go ahead and create the real fme using the pruned rules.
387705e9f42SStephen Hanson 	 */
388b5016cbbSstephh 	init_size = alloc_total();
389b5016cbbSstephh 	out(O_ALTFP|O_STAMP, "start config_snapshot using %d bytes", init_size);
39035f59e50SStephen Hanson 	nvlist_free(detector);
39135f59e50SStephen Hanson 	pathstr = ipath2str(NULL, e0ipp);
39232d4e834STarik Soydan 	cfgdata = config_snapshot();
39335f59e50SStephen Hanson 	platform_units_translate(0, cfgdata->cooked, NULL, NULL,
39435f59e50SStephen Hanson 	    &detector, pathstr);
39535f59e50SStephen Hanson 	FREE(pathstr);
396b5016cbbSstephh 	platform_save_config(hdl, fmcase);
397b5016cbbSstephh 	out(O_ALTFP|O_STAMP, "config_snapshot added %d bytes",
398b5016cbbSstephh 	    alloc_total() - init_size);
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate 	Nfmep = alloc_fme();
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate 	Nfmep->id = Nextid++;
403e5ba14ffSstephh 	Nfmep->config = cfgdata->cooked;
404e5ba14ffSstephh 	config_free(cfgdata);
4057c478bd9Sstevel@tonic-gate 	Nfmep->posted_suspects = 0;
4067c478bd9Sstevel@tonic-gate 	Nfmep->uniqobs = 0;
4077c478bd9Sstevel@tonic-gate 	Nfmep->state = FME_NOTHING;
4087c478bd9Sstevel@tonic-gate 	Nfmep->pull = 0ULL;
4090cc1f05eSjrutt 	Nfmep->overflow = 0;
4107c478bd9Sstevel@tonic-gate 
411b5016cbbSstephh 	Nfmep->fmcase = fmcase;
412b5016cbbSstephh 	Nfmep->hdl = hdl;
4137c478bd9Sstevel@tonic-gate 
414e5ba14ffSstephh 	if ((Nfmep->eventtree = itree_create(Nfmep->config)) == NULL) {
41532d4e834STarik Soydan 		Undiag_reason = UD_VAL_INSTFAIL;
416705e9f42SStephen Hanson 		arg = ipath2str(e0class, e0ipp);
417705e9f42SStephen Hanson 		publish_undiagnosable(hdl, ffep, fmcase, detector, arg);
418705e9f42SStephen Hanson 		nvlist_free(detector);
419705e9f42SStephen Hanson 		FREE(arg);
420e5ba14ffSstephh 		structconfig_free(Nfmep->config);
421b5016cbbSstephh 		destroy_fme_bufs(Nfmep);
4227c478bd9Sstevel@tonic-gate 		FREE(Nfmep);
4237c478bd9Sstevel@tonic-gate 		Nfmep = NULL;
4247c478bd9Sstevel@tonic-gate 		return (NULL);
4257c478bd9Sstevel@tonic-gate 	}
4267c478bd9Sstevel@tonic-gate 
4277c478bd9Sstevel@tonic-gate 	itree_ptree(O_ALTFP|O_VERB2, Nfmep->eventtree);
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate 	if ((Nfmep->e0 =
4307c478bd9Sstevel@tonic-gate 	    itree_lookup(Nfmep->eventtree, e0class, e0ipp)) == NULL) {
43132d4e834STarik Soydan 		Undiag_reason = UD_VAL_BADEVENTI;
432705e9f42SStephen Hanson 		arg = ipath2str(e0class, e0ipp);
433705e9f42SStephen Hanson 		publish_undiagnosable(hdl, ffep, fmcase, detector, arg);
434705e9f42SStephen Hanson 		nvlist_free(detector);
435705e9f42SStephen Hanson 		FREE(arg);
4367c478bd9Sstevel@tonic-gate 		itree_free(Nfmep->eventtree);
437e5ba14ffSstephh 		structconfig_free(Nfmep->config);
438b5016cbbSstephh 		destroy_fme_bufs(Nfmep);
4397c478bd9Sstevel@tonic-gate 		FREE(Nfmep);
4407c478bd9Sstevel@tonic-gate 		Nfmep = NULL;
4417c478bd9Sstevel@tonic-gate 		return (NULL);
4427c478bd9Sstevel@tonic-gate 	}
4437c478bd9Sstevel@tonic-gate 
444705e9f42SStephen Hanson 	nvlist_free(detector);
4457c478bd9Sstevel@tonic-gate 	return (fme_ready(Nfmep));
4467c478bd9Sstevel@tonic-gate }
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate void
fme_fini(void)4497c478bd9Sstevel@tonic-gate fme_fini(void)
4507c478bd9Sstevel@tonic-gate {
4517c478bd9Sstevel@tonic-gate 	struct fme *sfp, *fp;
4527c478bd9Sstevel@tonic-gate 	struct case_list *ucasep, *nextcasep;
4537c478bd9Sstevel@tonic-gate 
4547c478bd9Sstevel@tonic-gate 	ucasep = Undiagablecaselist;
4557c478bd9Sstevel@tonic-gate 	while (ucasep != NULL) {
4567c478bd9Sstevel@tonic-gate 		nextcasep = ucasep->next;
4577c478bd9Sstevel@tonic-gate 		FREE(ucasep);
4587c478bd9Sstevel@tonic-gate 		ucasep = nextcasep;
4597c478bd9Sstevel@tonic-gate 	}
4607c478bd9Sstevel@tonic-gate 	Undiagablecaselist = NULL;
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate 	/* clean up closed fmes */
4637c478bd9Sstevel@tonic-gate 	fp = ClosedFMEs;
4647c478bd9Sstevel@tonic-gate 	while (fp != NULL) {
4657c478bd9Sstevel@tonic-gate 		sfp = fp->next;
4667c478bd9Sstevel@tonic-gate 		destroy_fme(fp);
4677c478bd9Sstevel@tonic-gate 		fp = sfp;
4687c478bd9Sstevel@tonic-gate 	}
4697c478bd9Sstevel@tonic-gate 	ClosedFMEs = NULL;
4707c478bd9Sstevel@tonic-gate 
4717c478bd9Sstevel@tonic-gate 	fp = FMElist;
4727c478bd9Sstevel@tonic-gate 	while (fp != NULL) {
4737c478bd9Sstevel@tonic-gate 		sfp = fp->next;
4747c478bd9Sstevel@tonic-gate 		destroy_fme(fp);
4757c478bd9Sstevel@tonic-gate 		fp = sfp;
4767c478bd9Sstevel@tonic-gate 	}
4777c478bd9Sstevel@tonic-gate 	FMElist = EFMElist = NULL;
4787c478bd9Sstevel@tonic-gate 
4797c478bd9Sstevel@tonic-gate 	/* if we were in the middle of creating an fme, free it now */
4807c478bd9Sstevel@tonic-gate 	if (Nfmep) {
4817c478bd9Sstevel@tonic-gate 		destroy_fme(Nfmep);
4827c478bd9Sstevel@tonic-gate 		Nfmep = NULL;
4837c478bd9Sstevel@tonic-gate 	}
4847c478bd9Sstevel@tonic-gate }
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate /*
4877c478bd9Sstevel@tonic-gate  * Allocated space for a buffer name.  20 bytes allows for
4887c478bd9Sstevel@tonic-gate  * a ridiculous 9,999,999 unique observations.
4897c478bd9Sstevel@tonic-gate  */
4907c478bd9Sstevel@tonic-gate #define	OBBUFNMSZ 20
4917c478bd9Sstevel@tonic-gate 
4927c478bd9Sstevel@tonic-gate /*
4937c478bd9Sstevel@tonic-gate  *  serialize_observation
4947c478bd9Sstevel@tonic-gate  *
4957c478bd9Sstevel@tonic-gate  *  Create a recoverable version of the current observation
4967c478bd9Sstevel@tonic-gate  *  (f->ecurrent).  We keep a serialized version of each unique
4977c478bd9Sstevel@tonic-gate  *  observation in order that we may resume correctly the fme in the
4987c478bd9Sstevel@tonic-gate  *  correct state if eft or fmd crashes and we're restarted.
4997c478bd9Sstevel@tonic-gate  */
5007c478bd9Sstevel@tonic-gate static void
serialize_observation(struct fme * fp,const char * cls,const struct ipath * ipp)5017c478bd9Sstevel@tonic-gate serialize_observation(struct fme *fp, const char *cls, const struct ipath *ipp)
5027c478bd9Sstevel@tonic-gate {
5037c478bd9Sstevel@tonic-gate 	size_t pkdlen;
5047c478bd9Sstevel@tonic-gate 	char tmpbuf[OBBUFNMSZ];
5057c478bd9Sstevel@tonic-gate 	char *pkd = NULL;
5067c478bd9Sstevel@tonic-gate 	char *estr;
5077c478bd9Sstevel@tonic-gate 
5087c478bd9Sstevel@tonic-gate 	(void) snprintf(tmpbuf, OBBUFNMSZ, "observed%d", fp->uniqobs);
5097c478bd9Sstevel@tonic-gate 	estr = ipath2str(cls, ipp);
5107c478bd9Sstevel@tonic-gate 	fmd_buf_create(fp->hdl, fp->fmcase, tmpbuf, strlen(estr) + 1);
5117c478bd9Sstevel@tonic-gate 	fmd_buf_write(fp->hdl, fp->fmcase, tmpbuf, (void *)estr,
5127c478bd9Sstevel@tonic-gate 	    strlen(estr) + 1);
5137c478bd9Sstevel@tonic-gate 	FREE(estr);
5147c478bd9Sstevel@tonic-gate 
5157c478bd9Sstevel@tonic-gate 	if (fp->ecurrent != NULL && fp->ecurrent->nvp != NULL) {
5167c478bd9Sstevel@tonic-gate 		(void) snprintf(tmpbuf,
5177c478bd9Sstevel@tonic-gate 		    OBBUFNMSZ, "observed%d.nvp", fp->uniqobs);
5187c478bd9Sstevel@tonic-gate 		if (nvlist_xpack(fp->ecurrent->nvp,
5197c478bd9Sstevel@tonic-gate 		    &pkd, &pkdlen, NV_ENCODE_XDR, &Eft_nv_hdl) != 0)
5207c478bd9Sstevel@tonic-gate 			out(O_DIE|O_SYS, "pack of observed nvl failed");
5217c478bd9Sstevel@tonic-gate 		fmd_buf_create(fp->hdl, fp->fmcase, tmpbuf, pkdlen);
5227c478bd9Sstevel@tonic-gate 		fmd_buf_write(fp->hdl, fp->fmcase, tmpbuf, (void *)pkd, pkdlen);
5237c478bd9Sstevel@tonic-gate 		FREE(pkd);
5247c478bd9Sstevel@tonic-gate 	}
5257c478bd9Sstevel@tonic-gate 
5267c478bd9Sstevel@tonic-gate 	fp->uniqobs++;
5277c478bd9Sstevel@tonic-gate 	fmd_buf_write(fp->hdl, fp->fmcase, WOBUF_NOBS, (void *)&fp->uniqobs,
5287c478bd9Sstevel@tonic-gate 	    sizeof (fp->uniqobs));
5297c478bd9Sstevel@tonic-gate }
5307c478bd9Sstevel@tonic-gate 
5317c478bd9Sstevel@tonic-gate /*
5327c478bd9Sstevel@tonic-gate  *  init_fme_bufs -- We keep several bits of state about an fme for
5337c478bd9Sstevel@tonic-gate  *	use if eft or fmd crashes and we're restarted.
5347c478bd9Sstevel@tonic-gate  */
5357c478bd9Sstevel@tonic-gate static void
init_fme_bufs(struct fme * fp)5367c478bd9Sstevel@tonic-gate init_fme_bufs(struct fme *fp)
5377c478bd9Sstevel@tonic-gate {
5387c478bd9Sstevel@tonic-gate 	fmd_buf_create(fp->hdl, fp->fmcase, WOBUF_PULL, sizeof (fp->pull));
5397c478bd9Sstevel@tonic-gate 	fmd_buf_write(fp->hdl, fp->fmcase, WOBUF_PULL, (void *)&fp->pull,
5407c478bd9Sstevel@tonic-gate 	    sizeof (fp->pull));
5417c478bd9Sstevel@tonic-gate 
5427c478bd9Sstevel@tonic-gate 	fmd_buf_create(fp->hdl, fp->fmcase, WOBUF_ID, sizeof (fp->id));
5437c478bd9Sstevel@tonic-gate 	fmd_buf_write(fp->hdl, fp->fmcase, WOBUF_ID, (void *)&fp->id,
5447c478bd9Sstevel@tonic-gate 	    sizeof (fp->id));
5457c478bd9Sstevel@tonic-gate 
5467c478bd9Sstevel@tonic-gate 	fmd_buf_create(fp->hdl, fp->fmcase, WOBUF_NOBS, sizeof (fp->uniqobs));
5477c478bd9Sstevel@tonic-gate 	fmd_buf_write(fp->hdl, fp->fmcase, WOBUF_NOBS, (void *)&fp->uniqobs,
5487c478bd9Sstevel@tonic-gate 	    sizeof (fp->uniqobs));
5497c478bd9Sstevel@tonic-gate 
5507c478bd9Sstevel@tonic-gate 	fmd_buf_create(fp->hdl, fp->fmcase, WOBUF_POSTD,
5517c478bd9Sstevel@tonic-gate 	    sizeof (fp->posted_suspects));
5527c478bd9Sstevel@tonic-gate 	fmd_buf_write(fp->hdl, fp->fmcase, WOBUF_POSTD,
5537c478bd9Sstevel@tonic-gate 	    (void *)&fp->posted_suspects, sizeof (fp->posted_suspects));
5547c478bd9Sstevel@tonic-gate }
5557c478bd9Sstevel@tonic-gate 
5567c478bd9Sstevel@tonic-gate static void
destroy_fme_bufs(struct fme * fp)5577c478bd9Sstevel@tonic-gate destroy_fme_bufs(struct fme *fp)
5587c478bd9Sstevel@tonic-gate {
5597c478bd9Sstevel@tonic-gate 	char tmpbuf[OBBUFNMSZ];
5607c478bd9Sstevel@tonic-gate 	int o;
5617c478bd9Sstevel@tonic-gate 
562b5016cbbSstephh 	platform_restore_config(fp->hdl, fp->fmcase);
5637c478bd9Sstevel@tonic-gate 	fmd_buf_destroy(fp->hdl, fp->fmcase, WOBUF_CFGLEN);
5647c478bd9Sstevel@tonic-gate 	fmd_buf_destroy(fp->hdl, fp->fmcase, WOBUF_CFG);
5657c478bd9Sstevel@tonic-gate 	fmd_buf_destroy(fp->hdl, fp->fmcase, WOBUF_PULL);
5667c478bd9Sstevel@tonic-gate 	fmd_buf_destroy(fp->hdl, fp->fmcase, WOBUF_ID);
5677c478bd9Sstevel@tonic-gate 	fmd_buf_destroy(fp->hdl, fp->fmcase, WOBUF_POSTD);
5687c478bd9Sstevel@tonic-gate 	fmd_buf_destroy(fp->hdl, fp->fmcase, WOBUF_NOBS);
5697c478bd9Sstevel@tonic-gate 
5707c478bd9Sstevel@tonic-gate 	for (o = 0; o < fp->uniqobs; o++) {
5717c478bd9Sstevel@tonic-gate 		(void) snprintf(tmpbuf, OBBUFNMSZ, "observed%d", o);
5727c478bd9Sstevel@tonic-gate 		fmd_buf_destroy(fp->hdl, fp->fmcase, tmpbuf);
5737c478bd9Sstevel@tonic-gate 		(void) snprintf(tmpbuf, OBBUFNMSZ, "observed%d.nvp", o);
5747c478bd9Sstevel@tonic-gate 		fmd_buf_destroy(fp->hdl, fp->fmcase, tmpbuf);
5757c478bd9Sstevel@tonic-gate 	}
5767c478bd9Sstevel@tonic-gate }
5777c478bd9Sstevel@tonic-gate 
5787c478bd9Sstevel@tonic-gate /*
5797c478bd9Sstevel@tonic-gate  * reconstitute_observations -- convert a case's serialized observations
5807c478bd9Sstevel@tonic-gate  *	back into struct events.  Returns zero if all observations are
5817c478bd9Sstevel@tonic-gate  *	successfully reconstituted.
5827c478bd9Sstevel@tonic-gate  */
5837c478bd9Sstevel@tonic-gate static int
reconstitute_observations(struct fme * fmep)5847c478bd9Sstevel@tonic-gate reconstitute_observations(struct fme *fmep)
5857c478bd9Sstevel@tonic-gate {
5867c478bd9Sstevel@tonic-gate 	struct event *ep;
5877c478bd9Sstevel@tonic-gate 	struct node *epnamenp = NULL;
5887c478bd9Sstevel@tonic-gate 	size_t pkdlen;
5897c478bd9Sstevel@tonic-gate 	char *pkd = NULL;
5907c478bd9Sstevel@tonic-gate 	char *tmpbuf = alloca(OBBUFNMSZ);
5917c478bd9Sstevel@tonic-gate 	char *sepptr;
5927c478bd9Sstevel@tonic-gate 	char *estr;
5937c478bd9Sstevel@tonic-gate 	int ocnt;
5947c478bd9Sstevel@tonic-gate 	int elen;
5957c478bd9Sstevel@tonic-gate 
5967c478bd9Sstevel@tonic-gate 	for (ocnt = 0; ocnt < fmep->uniqobs; ocnt++) {
5977c478bd9Sstevel@tonic-gate 		(void) snprintf(tmpbuf, OBBUFNMSZ, "observed%d", ocnt);
5987c478bd9Sstevel@tonic-gate 		elen = fmd_buf_size(fmep->hdl, fmep->fmcase, tmpbuf);
5997c478bd9Sstevel@tonic-gate 		if (elen == 0) {
6007c478bd9Sstevel@tonic-gate 			out(O_ALTFP,
6017c478bd9Sstevel@tonic-gate 			    "reconstitute_observation: no %s buffer found.",
6027c478bd9Sstevel@tonic-gate 			    tmpbuf);
60332d4e834STarik Soydan 			Undiag_reason = UD_VAL_MISSINGOBS;
6047c478bd9Sstevel@tonic-gate 			break;
6057c478bd9Sstevel@tonic-gate 		}
6067c478bd9Sstevel@tonic-gate 
6077c478bd9Sstevel@tonic-gate 		estr = MALLOC(elen);
6087c478bd9Sstevel@tonic-gate 		fmd_buf_read(fmep->hdl, fmep->fmcase, tmpbuf, estr, elen);
6097c478bd9Sstevel@tonic-gate 		sepptr = strchr(estr, '@');
6107c478bd9Sstevel@tonic-gate 		if (sepptr == NULL) {
6117c478bd9Sstevel@tonic-gate 			out(O_ALTFP,
6127c478bd9Sstevel@tonic-gate 			    "reconstitute_observation: %s: "
6137c478bd9Sstevel@tonic-gate 			    "missing @ separator in %s.",
6147c478bd9Sstevel@tonic-gate 			    tmpbuf, estr);
61532d4e834STarik Soydan 			Undiag_reason = UD_VAL_MISSINGPATH;
6167c478bd9Sstevel@tonic-gate 			FREE(estr);
6177c478bd9Sstevel@tonic-gate 			break;
6187c478bd9Sstevel@tonic-gate 		}
6197c478bd9Sstevel@tonic-gate 
6207c478bd9Sstevel@tonic-gate 		*sepptr = '\0';
6217c478bd9Sstevel@tonic-gate 		if ((epnamenp = pathstring2epnamenp(sepptr + 1)) == NULL) {
6227c478bd9Sstevel@tonic-gate 			out(O_ALTFP,
6237c478bd9Sstevel@tonic-gate 			    "reconstitute_observation: %s: "
6247c478bd9Sstevel@tonic-gate 			    "trouble converting path string \"%s\" "
6257c478bd9Sstevel@tonic-gate 			    "to internal representation.",
6267c478bd9Sstevel@tonic-gate 			    tmpbuf, sepptr + 1);
62732d4e834STarik Soydan 			Undiag_reason = UD_VAL_MISSINGPATH;
6287c478bd9Sstevel@tonic-gate 			FREE(estr);
6297c478bd9Sstevel@tonic-gate 			break;
6307c478bd9Sstevel@tonic-gate 		}
6317c478bd9Sstevel@tonic-gate 
6327c478bd9Sstevel@tonic-gate 		/* construct the event */
6337c478bd9Sstevel@tonic-gate 		ep = itree_lookup(fmep->eventtree,
6347c478bd9Sstevel@tonic-gate 		    stable(estr), ipath(epnamenp));
6357c478bd9Sstevel@tonic-gate 		if (ep == NULL) {
6367c478bd9Sstevel@tonic-gate 			out(O_ALTFP,
6377c478bd9Sstevel@tonic-gate 			    "reconstitute_observation: %s: "
6387c478bd9Sstevel@tonic-gate 			    "lookup of  \"%s\" in itree failed.",
6397c478bd9Sstevel@tonic-gate 			    tmpbuf, ipath2str(estr, ipath(epnamenp)));
64032d4e834STarik Soydan 			Undiag_reason = UD_VAL_BADOBS;
6417c478bd9Sstevel@tonic-gate 			tree_free(epnamenp);
6427c478bd9Sstevel@tonic-gate 			FREE(estr);
6437c478bd9Sstevel@tonic-gate 			break;
6447c478bd9Sstevel@tonic-gate 		}
6457c478bd9Sstevel@tonic-gate 		tree_free(epnamenp);
6467c478bd9Sstevel@tonic-gate 
6477c478bd9Sstevel@tonic-gate 		/*
6487c478bd9Sstevel@tonic-gate 		 * We may or may not have a saved nvlist for the observation
6497c478bd9Sstevel@tonic-gate 		 */
6507c478bd9Sstevel@tonic-gate 		(void) snprintf(tmpbuf, OBBUFNMSZ, "observed%d.nvp", ocnt);
6517c478bd9Sstevel@tonic-gate 		pkdlen = fmd_buf_size(fmep->hdl, fmep->fmcase, tmpbuf);
6527c478bd9Sstevel@tonic-gate 		if (pkdlen != 0) {
6537c478bd9Sstevel@tonic-gate 			pkd = MALLOC(pkdlen);
6547c478bd9Sstevel@tonic-gate 			fmd_buf_read(fmep->hdl,
6557c478bd9Sstevel@tonic-gate 			    fmep->fmcase, tmpbuf, pkd, pkdlen);
6567aec1d6eScindi 			ASSERT(ep->nvp == NULL);
6577c478bd9Sstevel@tonic-gate 			if (nvlist_xunpack(pkd,
6587c478bd9Sstevel@tonic-gate 			    pkdlen, &ep->nvp, &Eft_nv_hdl) != 0)
6597c478bd9Sstevel@tonic-gate 				out(O_DIE|O_SYS, "pack of observed nvl failed");
6607c478bd9Sstevel@tonic-gate 			FREE(pkd);
6617c478bd9Sstevel@tonic-gate 		}
6627c478bd9Sstevel@tonic-gate 
6637c478bd9Sstevel@tonic-gate 		if (ocnt == 0)
6647c478bd9Sstevel@tonic-gate 			fmep->e0 = ep;
6657c478bd9Sstevel@tonic-gate 
6667c478bd9Sstevel@tonic-gate 		FREE(estr);
6677c478bd9Sstevel@tonic-gate 		fmep->ecurrent = ep;
6687c478bd9Sstevel@tonic-gate 		ep->count++;
6697c478bd9Sstevel@tonic-gate 
6707c478bd9Sstevel@tonic-gate 		/* link it into list of observations seen */
6717c478bd9Sstevel@tonic-gate 		ep->observations = fmep->observations;
6727c478bd9Sstevel@tonic-gate 		fmep->observations = ep;
6737c478bd9Sstevel@tonic-gate 	}
6747c478bd9Sstevel@tonic-gate 
6757c478bd9Sstevel@tonic-gate 	if (ocnt == fmep->uniqobs) {
6767c478bd9Sstevel@tonic-gate 		(void) fme_ready(fmep);
6777c478bd9Sstevel@tonic-gate 		return (0);
6787c478bd9Sstevel@tonic-gate 	}
6797c478bd9Sstevel@tonic-gate 
6807c478bd9Sstevel@tonic-gate 	return (1);
6817c478bd9Sstevel@tonic-gate }
6827c478bd9Sstevel@tonic-gate 
6837c478bd9Sstevel@tonic-gate /*
6847c478bd9Sstevel@tonic-gate  * restart_fme -- called during eft initialization.  Reconstitutes
6857c478bd9Sstevel@tonic-gate  *	an in-progress fme.
6867c478bd9Sstevel@tonic-gate  */
6877c478bd9Sstevel@tonic-gate void
fme_restart(fmd_hdl_t * hdl,fmd_case_t * inprogress)6887c478bd9Sstevel@tonic-gate fme_restart(fmd_hdl_t *hdl, fmd_case_t *inprogress)
6897c478bd9Sstevel@tonic-gate {
6907c478bd9Sstevel@tonic-gate 	nvlist_t *defect;
6917c478bd9Sstevel@tonic-gate 	struct case_list *bad;
6927c478bd9Sstevel@tonic-gate 	struct fme *fmep;
693e5ba14ffSstephh 	struct cfgdata *cfgdata;
6947c478bd9Sstevel@tonic-gate 	size_t rawsz;
695b5016cbbSstephh 	struct event *ep;
696b5016cbbSstephh 	char *tmpbuf = alloca(OBBUFNMSZ);
697b5016cbbSstephh 	char *sepptr;
698b5016cbbSstephh 	char *estr;
699b5016cbbSstephh 	int elen;
700b5016cbbSstephh 	struct node *epnamenp = NULL;
701b5016cbbSstephh 	int init_size;
702b5016cbbSstephh 	extern int alloc_total();
703705e9f42SStephen Hanson 	char *reason;
704b5016cbbSstephh 
705b5016cbbSstephh 	/*
706b5016cbbSstephh 	 * ignore solved or closed cases
707b5016cbbSstephh 	 */
708b5016cbbSstephh 	if (fmd_case_solved(hdl, inprogress) ||
709b5016cbbSstephh 	    fmd_case_closed(hdl, inprogress))
710b5016cbbSstephh 		return;
7117c478bd9Sstevel@tonic-gate 
7127c478bd9Sstevel@tonic-gate 	fmep = alloc_fme();
7137c478bd9Sstevel@tonic-gate 	fmep->fmcase = inprogress;
7147c478bd9Sstevel@tonic-gate 	fmep->hdl = hdl;
7157c478bd9Sstevel@tonic-gate 
7169f8ca725Sstephh 	if (fmd_buf_size(hdl, inprogress, WOBUF_POSTD) == 0) {
7179f8ca725Sstephh 		out(O_ALTFP, "restart_fme: no saved posted status");
71832d4e834STarik Soydan 		Undiag_reason = UD_VAL_MISSINGINFO;
7199f8ca725Sstephh 		goto badcase;
7209f8ca725Sstephh 	} else {
7219f8ca725Sstephh 		fmd_buf_read(hdl, inprogress, WOBUF_POSTD,
7229f8ca725Sstephh 		    (void *)&fmep->posted_suspects,
7239f8ca725Sstephh 		    sizeof (fmep->posted_suspects));
7249f8ca725Sstephh 	}
7259f8ca725Sstephh 
726b5016cbbSstephh 	if (fmd_buf_size(hdl, inprogress, WOBUF_ID) == 0) {
727b5016cbbSstephh 		out(O_ALTFP, "restart_fme: no saved id");
72832d4e834STarik Soydan 		Undiag_reason = UD_VAL_MISSINGINFO;
7299f8ca725Sstephh 		goto badcase;
730b5016cbbSstephh 	} else {
731b5016cbbSstephh 		fmd_buf_read(hdl, inprogress, WOBUF_ID, (void *)&fmep->id,
732b5016cbbSstephh 		    sizeof (fmep->id));
733b5016cbbSstephh 	}
734b5016cbbSstephh 	if (Nextid <= fmep->id)
735b5016cbbSstephh 		Nextid = fmep->id + 1;
736b5016cbbSstephh 
737b5016cbbSstephh 	out(O_ALTFP, "Replay FME %d", fmep->id);
7389f8ca725Sstephh 
7397c478bd9Sstevel@tonic-gate 	if (fmd_buf_size(hdl, inprogress, WOBUF_CFGLEN) != sizeof (size_t)) {
7407c478bd9Sstevel@tonic-gate 		out(O_ALTFP, "restart_fme: No config data");
74132d4e834STarik Soydan 		Undiag_reason = UD_VAL_MISSINGINFO;
7427c478bd9Sstevel@tonic-gate 		goto badcase;
7437c478bd9Sstevel@tonic-gate 	}
7447c478bd9Sstevel@tonic-gate 	fmd_buf_read(hdl, inprogress, WOBUF_CFGLEN, (void *)&rawsz,
7457c478bd9Sstevel@tonic-gate 	    sizeof (size_t));
7467c478bd9Sstevel@tonic-gate 
7477c478bd9Sstevel@tonic-gate 	if ((fmep->e0r = fmd_case_getprincipal(hdl, inprogress)) == NULL) {
7487c478bd9Sstevel@tonic-gate 		out(O_ALTFP, "restart_fme: No event zero");
74932d4e834STarik Soydan 		Undiag_reason = UD_VAL_MISSINGZERO;
7507c478bd9Sstevel@tonic-gate 		goto badcase;
7517c478bd9Sstevel@tonic-gate 	}
7527c478bd9Sstevel@tonic-gate 
753b5016cbbSstephh 	if (fmd_buf_size(hdl, inprogress, WOBUF_PULL) == 0) {
754b5016cbbSstephh 		out(O_ALTFP, "restart_fme: no saved wait time");
75532d4e834STarik Soydan 		Undiag_reason = UD_VAL_MISSINGINFO;
756b5016cbbSstephh 		goto badcase;
757b5016cbbSstephh 	} else {
758b5016cbbSstephh 		fmd_buf_read(hdl, inprogress, WOBUF_PULL, (void *)&fmep->pull,
759b5016cbbSstephh 		    sizeof (fmep->pull));
760b5016cbbSstephh 	}
761b5016cbbSstephh 
762b5016cbbSstephh 	if (fmd_buf_size(hdl, inprogress, WOBUF_NOBS) == 0) {
763b5016cbbSstephh 		out(O_ALTFP, "restart_fme: no count of observations");
76432d4e834STarik Soydan 		Undiag_reason = UD_VAL_MISSINGINFO;
765b5016cbbSstephh 		goto badcase;
766b5016cbbSstephh 	} else {
767b5016cbbSstephh 		fmd_buf_read(hdl, inprogress, WOBUF_NOBS,
768b5016cbbSstephh 		    (void *)&fmep->uniqobs, sizeof (fmep->uniqobs));
769b5016cbbSstephh 	}
770b5016cbbSstephh 
771b5016cbbSstephh 	(void) snprintf(tmpbuf, OBBUFNMSZ, "observed0");
772b5016cbbSstephh 	elen = fmd_buf_size(fmep->hdl, fmep->fmcase, tmpbuf);
773b5016cbbSstephh 	if (elen == 0) {
774b5016cbbSstephh 		out(O_ALTFP, "reconstitute_observation: no %s buffer found.",
775b5016cbbSstephh 		    tmpbuf);
77632d4e834STarik Soydan 		Undiag_reason = UD_VAL_MISSINGOBS;
777b5016cbbSstephh 		goto badcase;
778b5016cbbSstephh 	}
779b5016cbbSstephh 	estr = MALLOC(elen);
780b5016cbbSstephh 	fmd_buf_read(fmep->hdl, fmep->fmcase, tmpbuf, estr, elen);
781b5016cbbSstephh 	sepptr = strchr(estr, '@');
782b5016cbbSstephh 	if (sepptr == NULL) {
783b5016cbbSstephh 		out(O_ALTFP, "reconstitute_observation: %s: "
784b5016cbbSstephh 		    "missing @ separator in %s.",
785b5016cbbSstephh 		    tmpbuf, estr);
78632d4e834STarik Soydan 		Undiag_reason = UD_VAL_MISSINGPATH;
787b5016cbbSstephh 		FREE(estr);
788b5016cbbSstephh 		goto badcase;
789b5016cbbSstephh 	}
790b5016cbbSstephh 	*sepptr = '\0';
791b5016cbbSstephh 	if ((epnamenp = pathstring2epnamenp(sepptr + 1)) == NULL) {
792b5016cbbSstephh 		out(O_ALTFP, "reconstitute_observation: %s: "
793b5016cbbSstephh 		    "trouble converting path string \"%s\" "
794b5016cbbSstephh 		    "to internal representation.", tmpbuf, sepptr + 1);
79532d4e834STarik Soydan 		Undiag_reason = UD_VAL_MISSINGPATH;
796b5016cbbSstephh 		FREE(estr);
797b5016cbbSstephh 		goto badcase;
798b5016cbbSstephh 	}
799705e9f42SStephen Hanson 	(void) prune_propagations(stable(estr), ipath(epnamenp));
800b5016cbbSstephh 	tree_free(epnamenp);
801b5016cbbSstephh 	FREE(estr);
802b5016cbbSstephh 
803b5016cbbSstephh 	init_size = alloc_total();
804b5016cbbSstephh 	out(O_ALTFP|O_STAMP, "start config_restore using %d bytes", init_size);
8057c478bd9Sstevel@tonic-gate 	cfgdata = MALLOC(sizeof (struct cfgdata));
8067c478bd9Sstevel@tonic-gate 	cfgdata->cooked = NULL;
8077c478bd9Sstevel@tonic-gate 	cfgdata->devcache = NULL;
808602ca9eaScth 	cfgdata->devidcache = NULL;
80944ed9dbbSStephen Hanson 	cfgdata->tpcache = NULL;
8107c478bd9Sstevel@tonic-gate 	cfgdata->cpucache = NULL;
811b5016cbbSstephh 	cfgdata->raw_refcnt = 1;
8127c478bd9Sstevel@tonic-gate 
8137c478bd9Sstevel@tonic-gate 	if (rawsz > 0) {
8147c478bd9Sstevel@tonic-gate 		if (fmd_buf_size(hdl, inprogress, WOBUF_CFG) != rawsz) {
8157c478bd9Sstevel@tonic-gate 			out(O_ALTFP, "restart_fme: Config data size mismatch");
81632d4e834STarik Soydan 			Undiag_reason = UD_VAL_CFGMISMATCH;
8177c478bd9Sstevel@tonic-gate 			goto badcase;
8187c478bd9Sstevel@tonic-gate 		}
8197c478bd9Sstevel@tonic-gate 		cfgdata->begin = MALLOC(rawsz);
8207c478bd9Sstevel@tonic-gate 		cfgdata->end = cfgdata->nextfree = cfgdata->begin + rawsz;
8217c478bd9Sstevel@tonic-gate 		fmd_buf_read(hdl,
8227c478bd9Sstevel@tonic-gate 		    inprogress, WOBUF_CFG, cfgdata->begin, rawsz);
8237c478bd9Sstevel@tonic-gate 	} else {
8247c478bd9Sstevel@tonic-gate 		cfgdata->begin = cfgdata->end = cfgdata->nextfree = NULL;
8257c478bd9Sstevel@tonic-gate 	}
8267c478bd9Sstevel@tonic-gate 
8277c478bd9Sstevel@tonic-gate 	config_cook(cfgdata);
828e5ba14ffSstephh 	fmep->config = cfgdata->cooked;
829e5ba14ffSstephh 	config_free(cfgdata);
830b5016cbbSstephh 	out(O_ALTFP|O_STAMP, "config_restore added %d bytes",
831b5016cbbSstephh 	    alloc_total() - init_size);
832b5016cbbSstephh 
833e5ba14ffSstephh 	if ((fmep->eventtree = itree_create(fmep->config)) == NULL) {
8347c478bd9Sstevel@tonic-gate 		/* case not properly saved or irretrievable */
8357c478bd9Sstevel@tonic-gate 		out(O_ALTFP, "restart_fme: NULL instance tree");
83632d4e834STarik Soydan 		Undiag_reason = UD_VAL_INSTFAIL;
8377c478bd9Sstevel@tonic-gate 		goto badcase;
8387c478bd9Sstevel@tonic-gate 	}
8397c478bd9Sstevel@tonic-gate 
8407c478bd9Sstevel@tonic-gate 	itree_ptree(O_ALTFP|O_VERB2, fmep->eventtree);
8417c478bd9Sstevel@tonic-gate 
8427c478bd9Sstevel@tonic-gate 	if (reconstitute_observations(fmep) != 0)
8437c478bd9Sstevel@tonic-gate 		goto badcase;
8447c478bd9Sstevel@tonic-gate 
845b5016cbbSstephh 	out(O_ALTFP|O_NONL, "FME %d replay observations: ", fmep->id);
846b5016cbbSstephh 	for (ep = fmep->observations; ep; ep = ep->observations) {
847b5016cbbSstephh 		out(O_ALTFP|O_NONL, " ");
848b5016cbbSstephh 		itree_pevent_brief(O_ALTFP|O_NONL, ep);
849b5016cbbSstephh 	}
850b5016cbbSstephh 	out(O_ALTFP, NULL);
851b5016cbbSstephh 
8520cc1f05eSjrutt 	Open_fme_count++;
8530cc1f05eSjrutt 
8547c478bd9Sstevel@tonic-gate 	/* give the diagnosis algorithm a shot at the new FME state */
8559f8ca725Sstephh 	fme_eval(fmep, fmep->e0r);
8567c478bd9Sstevel@tonic-gate 	return;
8577c478bd9Sstevel@tonic-gate 
8587c478bd9Sstevel@tonic-gate badcase:
8597c478bd9Sstevel@tonic-gate 	if (fmep->eventtree != NULL)
8607c478bd9Sstevel@tonic-gate 		itree_free(fmep->eventtree);
861e5ba14ffSstephh 	if (fmep->config)
862e5ba14ffSstephh 		structconfig_free(fmep->config);
8637c478bd9Sstevel@tonic-gate 	destroy_fme_bufs(fmep);
8647c478bd9Sstevel@tonic-gate 	FREE(fmep);
8657c478bd9Sstevel@tonic-gate 
8667c478bd9Sstevel@tonic-gate 	/*
8677c478bd9Sstevel@tonic-gate 	 * Since we're unable to restart the case, add it to the undiagable
8687c478bd9Sstevel@tonic-gate 	 * list and solve and close it as appropriate.
8697c478bd9Sstevel@tonic-gate 	 */
8707c478bd9Sstevel@tonic-gate 	bad = MALLOC(sizeof (struct case_list));
8717c478bd9Sstevel@tonic-gate 	bad->next = NULL;
8727c478bd9Sstevel@tonic-gate 
8737c478bd9Sstevel@tonic-gate 	if (Undiagablecaselist != NULL)
8747c478bd9Sstevel@tonic-gate 		bad->next = Undiagablecaselist;
8757c478bd9Sstevel@tonic-gate 	Undiagablecaselist = bad;
8767c478bd9Sstevel@tonic-gate 	bad->fmcase = inprogress;
8777c478bd9Sstevel@tonic-gate 
8789f8ca725Sstephh 	out(O_ALTFP|O_NONL, "[case %s (unable to restart), ",
8797c478bd9Sstevel@tonic-gate 	    fmd_case_uuid(hdl, bad->fmcase));
8807c478bd9Sstevel@tonic-gate 
8817c478bd9Sstevel@tonic-gate 	if (fmd_case_solved(hdl, bad->fmcase)) {
8829f8ca725Sstephh 		out(O_ALTFP|O_NONL, "already solved, ");
8837c478bd9Sstevel@tonic-gate 	} else {
8849f8ca725Sstephh 		out(O_ALTFP|O_NONL, "solving, ");
88532d4e834STarik Soydan 		defect = fmd_nvl_create_fault(hdl,
88632d4e834STarik Soydan 		    undiag_2defect_str(Undiag_reason), 100, NULL, NULL, NULL);
887705e9f42SStephen Hanson 		reason = undiag_2reason_str(Undiag_reason, NULL);
888705e9f42SStephen Hanson 		(void) nvlist_add_string(defect, UNDIAG_REASON, reason);
889705e9f42SStephen Hanson 		FREE(reason);
8907c478bd9Sstevel@tonic-gate 		fmd_case_add_suspect(hdl, bad->fmcase, defect);
8917c478bd9Sstevel@tonic-gate 		fmd_case_solve(hdl, bad->fmcase);
89232d4e834STarik Soydan 		Undiag_reason = UD_VAL_UNKNOWN;
8937c478bd9Sstevel@tonic-gate 	}
8947c478bd9Sstevel@tonic-gate 
8957c478bd9Sstevel@tonic-gate 	if (fmd_case_closed(hdl, bad->fmcase)) {
8967c478bd9Sstevel@tonic-gate 		out(O_ALTFP, "already closed ]");
8977c478bd9Sstevel@tonic-gate 	} else {
8987c478bd9Sstevel@tonic-gate 		out(O_ALTFP, "closing ]");
8997c478bd9Sstevel@tonic-gate 		fmd_case_close(hdl, bad->fmcase);
9007c478bd9Sstevel@tonic-gate 	}
9017c478bd9Sstevel@tonic-gate }
9027c478bd9Sstevel@tonic-gate 
9037aec1d6eScindi /*ARGSUSED*/
9047aec1d6eScindi static void
globals_destructor(void * left,void * right,void * arg)9057aec1d6eScindi globals_destructor(void *left, void *right, void *arg)
9067aec1d6eScindi {
9077aec1d6eScindi 	struct evalue *evp = (struct evalue *)right;
9087aec1d6eScindi 	if (evp->t == NODEPTR)
90980ab886dSwesolows 		tree_free((struct node *)(uintptr_t)evp->v);
910837416c3Scy152378 	evp->v = (uintptr_t)NULL;
9117aec1d6eScindi 	FREE(evp);
9127aec1d6eScindi }
9137aec1d6eScindi 
9147c478bd9Sstevel@tonic-gate void
destroy_fme(struct fme * f)9157c478bd9Sstevel@tonic-gate destroy_fme(struct fme *f)
9167c478bd9Sstevel@tonic-gate {
9177c478bd9Sstevel@tonic-gate 	stats_delete(f->Rcount);
9187c478bd9Sstevel@tonic-gate 	stats_delete(f->Hcallcount);
9197c478bd9Sstevel@tonic-gate 	stats_delete(f->Rcallcount);
9207c478bd9Sstevel@tonic-gate 	stats_delete(f->Ccallcount);
9217c478bd9Sstevel@tonic-gate 	stats_delete(f->Ecallcount);
9227c478bd9Sstevel@tonic-gate 	stats_delete(f->Tcallcount);
9237c478bd9Sstevel@tonic-gate 	stats_delete(f->Marrowcount);
9247c478bd9Sstevel@tonic-gate 	stats_delete(f->diags);
9257c478bd9Sstevel@tonic-gate 
9269f8ca725Sstephh 	if (f->eventtree != NULL)
9277c478bd9Sstevel@tonic-gate 		itree_free(f->eventtree);
928e5ba14ffSstephh 	if (f->config)
929e5ba14ffSstephh 		structconfig_free(f->config);
9307aec1d6eScindi 	lut_free(f->globals, globals_destructor, NULL);
9317c478bd9Sstevel@tonic-gate 	FREE(f);
9327c478bd9Sstevel@tonic-gate }
9337c478bd9Sstevel@tonic-gate 
9347c478bd9Sstevel@tonic-gate static const char *
fme_state2str(enum fme_state s)9357c478bd9Sstevel@tonic-gate fme_state2str(enum fme_state s)
9367c478bd9Sstevel@tonic-gate {
9377c478bd9Sstevel@tonic-gate 	switch (s) {
9387c478bd9Sstevel@tonic-gate 	case FME_NOTHING:	return ("NOTHING");
9397c478bd9Sstevel@tonic-gate 	case FME_WAIT:		return ("WAIT");
9407c478bd9Sstevel@tonic-gate 	case FME_CREDIBLE:	return ("CREDIBLE");
9417c478bd9Sstevel@tonic-gate 	case FME_DISPROVED:	return ("DISPROVED");
9427aec1d6eScindi 	case FME_DEFERRED:	return ("DEFERRED");
9437c478bd9Sstevel@tonic-gate 	default:		return ("UNKNOWN");
9447c478bd9Sstevel@tonic-gate 	}
9457c478bd9Sstevel@tonic-gate }
9467c478bd9Sstevel@tonic-gate 
9477c478bd9Sstevel@tonic-gate static int
is_problem(enum nametype t)9487c478bd9Sstevel@tonic-gate is_problem(enum nametype t)
9497c478bd9Sstevel@tonic-gate {
9507c478bd9Sstevel@tonic-gate 	return (t == N_FAULT || t == N_DEFECT || t == N_UPSET);
9517c478bd9Sstevel@tonic-gate }
9527c478bd9Sstevel@tonic-gate 
9537c478bd9Sstevel@tonic-gate static int
is_defect(enum nametype t)9547c478bd9Sstevel@tonic-gate is_defect(enum nametype t)
9557c478bd9Sstevel@tonic-gate {
9567c478bd9Sstevel@tonic-gate 	return (t == N_DEFECT);
9577c478bd9Sstevel@tonic-gate }
9587c478bd9Sstevel@tonic-gate 
9597c478bd9Sstevel@tonic-gate static int
is_upset(enum nametype t)9607c478bd9Sstevel@tonic-gate is_upset(enum nametype t)
9617c478bd9Sstevel@tonic-gate {
9627c478bd9Sstevel@tonic-gate 	return (t == N_UPSET);
9637c478bd9Sstevel@tonic-gate }
9647c478bd9Sstevel@tonic-gate 
9657c478bd9Sstevel@tonic-gate static void
fme_print(int flags,struct fme * fmep)9667c478bd9Sstevel@tonic-gate fme_print(int flags, struct fme *fmep)
9677c478bd9Sstevel@tonic-gate {
9687c478bd9Sstevel@tonic-gate 	struct event *ep;
9697c478bd9Sstevel@tonic-gate 
9707c478bd9Sstevel@tonic-gate 	out(flags, "Fault Management Exercise %d", fmep->id);
9717c478bd9Sstevel@tonic-gate 	out(flags, "\t       State: %s", fme_state2str(fmep->state));
9727c478bd9Sstevel@tonic-gate 	out(flags|O_NONL, "\t  Start time: ");
9737c478bd9Sstevel@tonic-gate 	ptree_timeval(flags|O_NONL, &fmep->ull);
9747c478bd9Sstevel@tonic-gate 	out(flags, NULL);
9757c478bd9Sstevel@tonic-gate 	if (fmep->wull) {
9767c478bd9Sstevel@tonic-gate 		out(flags|O_NONL, "\t   Wait time: ");
9777c478bd9Sstevel@tonic-gate 		ptree_timeval(flags|O_NONL, &fmep->wull);
9787c478bd9Sstevel@tonic-gate 		out(flags, NULL);
9797c478bd9Sstevel@tonic-gate 	}
9807c478bd9Sstevel@tonic-gate 	out(flags|O_NONL, "\t          E0: ");
9817c478bd9Sstevel@tonic-gate 	if (fmep->e0)
9827c478bd9Sstevel@tonic-gate 		itree_pevent_brief(flags|O_NONL, fmep->e0);
9837c478bd9Sstevel@tonic-gate 	else
9847c478bd9Sstevel@tonic-gate 		out(flags|O_NONL, "NULL");
9857c478bd9Sstevel@tonic-gate 	out(flags, NULL);
9867c478bd9Sstevel@tonic-gate 	out(flags|O_NONL, "\tObservations:");
9877c478bd9Sstevel@tonic-gate 	for (ep = fmep->observations; ep; ep = ep->observations) {
9887c478bd9Sstevel@tonic-gate 		out(flags|O_NONL, " ");
9897c478bd9Sstevel@tonic-gate 		itree_pevent_brief(flags|O_NONL, ep);
9907c478bd9Sstevel@tonic-gate 	}
9917c478bd9Sstevel@tonic-gate 	out(flags, NULL);
9927c478bd9Sstevel@tonic-gate 	out(flags|O_NONL, "\tSuspect list:");
9937c478bd9Sstevel@tonic-gate 	for (ep = fmep->suspects; ep; ep = ep->suspects) {
9947c478bd9Sstevel@tonic-gate 		out(flags|O_NONL, " ");
9957c478bd9Sstevel@tonic-gate 		itree_pevent_brief(flags|O_NONL, ep);
9967c478bd9Sstevel@tonic-gate 	}
9977c478bd9Sstevel@tonic-gate 	out(flags, NULL);
9989f8ca725Sstephh 	if (fmep->eventtree != NULL) {
9997c478bd9Sstevel@tonic-gate 		out(flags|O_VERB2, "\t        Tree:");
10007c478bd9Sstevel@tonic-gate 		itree_ptree(flags|O_VERB2, fmep->eventtree);
10017c478bd9Sstevel@tonic-gate 	}
10029f8ca725Sstephh }
10037c478bd9Sstevel@tonic-gate 
10047c478bd9Sstevel@tonic-gate static struct node *
pathstring2epnamenp(char * path)10057c478bd9Sstevel@tonic-gate pathstring2epnamenp(char *path)
10067c478bd9Sstevel@tonic-gate {
10077c478bd9Sstevel@tonic-gate 	char *sep = "/";
10087c478bd9Sstevel@tonic-gate 	struct node *ret;
10097c478bd9Sstevel@tonic-gate 	char *ptr;
10107c478bd9Sstevel@tonic-gate 
10117c478bd9Sstevel@tonic-gate 	if ((ptr = strtok(path, sep)) == NULL)
10127c478bd9Sstevel@tonic-gate 		out(O_DIE, "pathstring2epnamenp: invalid empty class");
10137c478bd9Sstevel@tonic-gate 
10147c478bd9Sstevel@tonic-gate 	ret = tree_iname(stable(ptr), NULL, 0);
10157c478bd9Sstevel@tonic-gate 
10167c478bd9Sstevel@tonic-gate 	while ((ptr = strtok(NULL, sep)) != NULL)
10177c478bd9Sstevel@tonic-gate 		ret = tree_name_append(ret,
10187c478bd9Sstevel@tonic-gate 		    tree_iname(stable(ptr), NULL, 0));
10197c478bd9Sstevel@tonic-gate 
10207c478bd9Sstevel@tonic-gate 	return (ret);
10217c478bd9Sstevel@tonic-gate }
10227c478bd9Sstevel@tonic-gate 
10237c478bd9Sstevel@tonic-gate /*
10247c478bd9Sstevel@tonic-gate  * for a given upset sp, increment the corresponding SERD engine.  if the
10257c478bd9Sstevel@tonic-gate  * SERD engine trips, return the ename and ipp of the resulting ereport.
10267c478bd9Sstevel@tonic-gate  * returns true if engine tripped and *enamep and *ippp were filled in.
10277c478bd9Sstevel@tonic-gate  */
10287c478bd9Sstevel@tonic-gate static int
serd_eval(struct fme * fmep,fmd_hdl_t * hdl,fmd_event_t * ffep,fmd_case_t * fmcase,struct event * sp,const char ** enamep,const struct ipath ** ippp)10297aec1d6eScindi serd_eval(struct fme *fmep, fmd_hdl_t *hdl, fmd_event_t *ffep,
10307aec1d6eScindi     fmd_case_t *fmcase, struct event *sp, const char **enamep,
10317aec1d6eScindi     const struct ipath **ippp)
10327c478bd9Sstevel@tonic-gate {
10337c478bd9Sstevel@tonic-gate 	struct node *serdinst;
10347c478bd9Sstevel@tonic-gate 	char *serdname;
1035049e2ea4SScott Davenport 	char *serdresource;
10368e7248e5SStephen Hanson 	char *serdclass;
10377aec1d6eScindi 	struct node *nid;
1038b5016cbbSstephh 	struct serd_entry *newentp;
1039049e2ea4SScott Davenport 	int i, serdn = -1, serdincrement = 1, len = 0;
10408e7248e5SStephen Hanson 	char *serdsuffix = NULL, *serdt = NULL;
1041b7d3956bSstephh 	struct evalue *ep;
10427c478bd9Sstevel@tonic-gate 
10437c478bd9Sstevel@tonic-gate 	ASSERT(sp->t == N_UPSET);
10447c478bd9Sstevel@tonic-gate 	ASSERT(ffep != NULL);
10457c478bd9Sstevel@tonic-gate 
1046b7d3956bSstephh 	if ((ep = (struct evalue *)lut_lookup(sp->serdprops,
1047b7d3956bSstephh 	    (void *)"n", (lut_cmp)strcmp)) != NULL) {
1048b7d3956bSstephh 		ASSERT(ep->t == UINT64);
1049b7d3956bSstephh 		serdn = (int)ep->v;
1050b7d3956bSstephh 	}
1051b7d3956bSstephh 	if ((ep = (struct evalue *)lut_lookup(sp->serdprops,
1052b7d3956bSstephh 	    (void *)"t", (lut_cmp)strcmp)) != NULL) {
1053b7d3956bSstephh 		ASSERT(ep->t == STRING);
1054b7d3956bSstephh 		serdt = (char *)(uintptr_t)ep->v;
1055b7d3956bSstephh 	}
1056b7d3956bSstephh 	if ((ep = (struct evalue *)lut_lookup(sp->serdprops,
1057b7d3956bSstephh 	    (void *)"suffix", (lut_cmp)strcmp)) != NULL) {
1058b7d3956bSstephh 		ASSERT(ep->t == STRING);
1059b7d3956bSstephh 		serdsuffix = (char *)(uintptr_t)ep->v;
1060b7d3956bSstephh 	}
1061b7d3956bSstephh 	if ((ep = (struct evalue *)lut_lookup(sp->serdprops,
1062b7d3956bSstephh 	    (void *)"increment", (lut_cmp)strcmp)) != NULL) {
1063b7d3956bSstephh 		ASSERT(ep->t == UINT64);
1064b7d3956bSstephh 		serdincrement = (int)ep->v;
1065b7d3956bSstephh 	}
1066b7d3956bSstephh 
10677c478bd9Sstevel@tonic-gate 	/*
10687c478bd9Sstevel@tonic-gate 	 * obtain instanced SERD engine from the upset sp.  from this
10697c478bd9Sstevel@tonic-gate 	 * derive serdname, the string used to identify the SERD engine.
10707c478bd9Sstevel@tonic-gate 	 */
10717c478bd9Sstevel@tonic-gate 	serdinst = eventprop_lookup(sp, L_engine);
10727c478bd9Sstevel@tonic-gate 
10737c478bd9Sstevel@tonic-gate 	if (serdinst == NULL)
1074b7d3956bSstephh 		return (-1);
10757c478bd9Sstevel@tonic-gate 
10768e7248e5SStephen Hanson 	len = strlen(serdinst->u.stmt.np->u.event.ename->u.name.s) + 1;
1077049e2ea4SScott Davenport 	if (serdsuffix != NULL)
1078049e2ea4SScott Davenport 		len += strlen(serdsuffix);
10798e7248e5SStephen Hanson 	serdclass = MALLOC(len);
10808e7248e5SStephen Hanson 	if (serdsuffix != NULL)
10818e7248e5SStephen Hanson 		(void) snprintf(serdclass, len, "%s%s",
10828e7248e5SStephen Hanson 		    serdinst->u.stmt.np->u.event.ename->u.name.s, serdsuffix);
10838e7248e5SStephen Hanson 	else
10848e7248e5SStephen Hanson 		(void) snprintf(serdclass, len, "%s",
10858e7248e5SStephen Hanson 		    serdinst->u.stmt.np->u.event.ename->u.name.s);
10868e7248e5SStephen Hanson 	serdresource = ipath2str(NULL,
10878e7248e5SStephen Hanson 	    ipath(serdinst->u.stmt.np->u.event.epname));
10888e7248e5SStephen Hanson 	len += strlen(serdresource) + 1;
10898e7248e5SStephen Hanson 	serdname = MALLOC(len);
10908e7248e5SStephen Hanson 	(void) snprintf(serdname, len, "%s@%s", serdclass, serdresource);
1091049e2ea4SScott Davenport 	FREE(serdresource);
1092b7d3956bSstephh 
10937aec1d6eScindi 	/* handle serd engine "id" property, if there is one */
10947aec1d6eScindi 	if ((nid =
10957aec1d6eScindi 	    lut_lookup(serdinst->u.stmt.lutp, (void *)L_id, NULL)) != NULL) {
10967aec1d6eScindi 		struct evalue *gval;
10977aec1d6eScindi 		char suffixbuf[200];
10987aec1d6eScindi 		char *suffix;
10997aec1d6eScindi 		char *nserdname;
11007aec1d6eScindi 		size_t nname;
11017aec1d6eScindi 
11027aec1d6eScindi 		out(O_ALTFP|O_NONL, "serd \"%s\" id: ", serdname);
11037aec1d6eScindi 		ptree_name_iter(O_ALTFP|O_NONL, nid);
11047aec1d6eScindi 
11057aec1d6eScindi 		ASSERTinfo(nid->t == T_GLOBID, ptree_nodetype2str(nid->t));
11067aec1d6eScindi 
11077aec1d6eScindi 		if ((gval = lut_lookup(fmep->globals,
11087aec1d6eScindi 		    (void *)nid->u.globid.s, NULL)) == NULL) {
11097aec1d6eScindi 			out(O_ALTFP, " undefined");
11107aec1d6eScindi 		} else if (gval->t == UINT64) {
11117aec1d6eScindi 			out(O_ALTFP, " %llu", gval->v);
11127aec1d6eScindi 			(void) sprintf(suffixbuf, "%llu", gval->v);
11137aec1d6eScindi 			suffix = suffixbuf;
11147aec1d6eScindi 		} else {
111580ab886dSwesolows 			out(O_ALTFP, " \"%s\"", (char *)(uintptr_t)gval->v);
111680ab886dSwesolows 			suffix = (char *)(uintptr_t)gval->v;
11177aec1d6eScindi 		}
11187aec1d6eScindi 
11197aec1d6eScindi 		nname = strlen(serdname) + strlen(suffix) + 2;
11207aec1d6eScindi 		nserdname = MALLOC(nname);
11217aec1d6eScindi 		(void) snprintf(nserdname, nname, "%s:%s", serdname, suffix);
11227aec1d6eScindi 		FREE(serdname);
11237aec1d6eScindi 		serdname = nserdname;
11247aec1d6eScindi 	}
11257aec1d6eScindi 
1126b7d3956bSstephh 	/*
1127b7d3956bSstephh 	 * if the engine is empty, and we have an override for n/t then
1128b7d3956bSstephh 	 * destroy and recreate it.
1129b7d3956bSstephh 	 */
1130b7d3956bSstephh 	if ((serdn != -1 || serdt != NULL) && fmd_serd_exists(hdl, serdname) &&
1131b7d3956bSstephh 	    fmd_serd_empty(hdl, serdname))
1132b7d3956bSstephh 		fmd_serd_destroy(hdl, serdname);
1133b7d3956bSstephh 
11347c478bd9Sstevel@tonic-gate 	if (!fmd_serd_exists(hdl, serdname)) {
11357c478bd9Sstevel@tonic-gate 		struct node *nN, *nT;
1136e5ba14ffSstephh 		const char *s;
1137e5ba14ffSstephh 		struct node *nodep;
1138e5ba14ffSstephh 		struct config *cp;
1139e5ba14ffSstephh 		char *path;
1140e5ba14ffSstephh 		uint_t nval;
1141e5ba14ffSstephh 		hrtime_t tval;
1142e5ba14ffSstephh 		int i;
1143e5ba14ffSstephh 		char *ptr;
1144e5ba14ffSstephh 		int got_n_override = 0, got_t_override = 0;
11457c478bd9Sstevel@tonic-gate 
11467c478bd9Sstevel@tonic-gate 		/* no SERD engine yet, so create it */
1147e5ba14ffSstephh 		nodep = serdinst->u.stmt.np->u.event.epname;
1148e5ba14ffSstephh 		path = ipath2str(NULL, ipath(nodep));
1149e5ba14ffSstephh 		cp = config_lookup(fmep->config, path, 0);
1150e5ba14ffSstephh 		FREE((void *)path);
11517c478bd9Sstevel@tonic-gate 
1152e5ba14ffSstephh 		/*
1153e5ba14ffSstephh 		 * We allow serd paramaters to be overridden, either from
1154e5ba14ffSstephh 		 * eft.conf file values (if Serd_Override is set) or from
1155e5ba14ffSstephh 		 * driver properties (for "serd.io.device" engines).
1156e5ba14ffSstephh 		 */
1157e5ba14ffSstephh 		if (Serd_Override != NULL) {
1158e5ba14ffSstephh 			char *save_ptr, *ptr1, *ptr2, *ptr3;
1159e5ba14ffSstephh 			ptr3 = save_ptr = STRDUP(Serd_Override);
1160e5ba14ffSstephh 			while (*ptr3 != '\0') {
1161e5ba14ffSstephh 				ptr1 = strchr(ptr3, ',');
1162e5ba14ffSstephh 				*ptr1 = '\0';
11638e7248e5SStephen Hanson 				if (strcmp(ptr3, serdclass) == 0) {
1164e5ba14ffSstephh 					ptr2 =  strchr(ptr1 + 1, ',');
1165e5ba14ffSstephh 					*ptr2 = '\0';
1166e5ba14ffSstephh 					nval = atoi(ptr1 + 1);
1167e5ba14ffSstephh 					out(O_ALTFP, "serd override %s_n %d",
11688e7248e5SStephen Hanson 					    serdclass, nval);
1169e5ba14ffSstephh 					ptr3 =  strchr(ptr2 + 1, ' ');
1170e5ba14ffSstephh 					if (ptr3)
1171e5ba14ffSstephh 						*ptr3 = '\0';
1172e5ba14ffSstephh 					ptr = STRDUP(ptr2 + 1);
1173e5ba14ffSstephh 					out(O_ALTFP, "serd override %s_t %s",
11748e7248e5SStephen Hanson 					    serdclass, ptr);
1175e5ba14ffSstephh 					got_n_override = 1;
1176e5ba14ffSstephh 					got_t_override = 1;
1177e5ba14ffSstephh 					break;
1178e5ba14ffSstephh 				} else {
1179e5ba14ffSstephh 					ptr2 =  strchr(ptr1 + 1, ',');
1180e5ba14ffSstephh 					ptr3 =  strchr(ptr2 + 1, ' ');
1181e5ba14ffSstephh 					if (ptr3 == NULL)
1182e5ba14ffSstephh 						break;
1183e5ba14ffSstephh 				}
1184e5ba14ffSstephh 				ptr3++;
1185e5ba14ffSstephh 			}
1186e5ba14ffSstephh 			FREE(save_ptr);
1187e5ba14ffSstephh 		}
1188e5ba14ffSstephh 
1189e5ba14ffSstephh 		if (cp && got_n_override == 0) {
1190e5ba14ffSstephh 			/*
11918e7248e5SStephen Hanson 			 * convert serd engine class into property name
1192e5ba14ffSstephh 			 */
11938e7248e5SStephen Hanson 			char *prop_name = MALLOC(strlen(serdclass) + 3);
11948e7248e5SStephen Hanson 			for (i = 0; i < strlen(serdclass); i++) {
11958e7248e5SStephen Hanson 				if (serdclass[i] == '.')
11968e7248e5SStephen Hanson 					prop_name[i] = '_';
1197e5ba14ffSstephh 				else
11988e7248e5SStephen Hanson 					prop_name[i] = serdclass[i];
1199e5ba14ffSstephh 			}
12008e7248e5SStephen Hanson 			prop_name[i++] = '_';
12018e7248e5SStephen Hanson 			prop_name[i++] = 'n';
12028e7248e5SStephen Hanson 			prop_name[i] = '\0';
12038e7248e5SStephen Hanson 			if (s = config_getprop(cp, prop_name)) {
1204e5ba14ffSstephh 				nval = atoi(s);
12058e7248e5SStephen Hanson 				out(O_ALTFP, "serd override %s_n %s",
12068e7248e5SStephen Hanson 				    serdclass, s);
1207e5ba14ffSstephh 				got_n_override = 1;
1208e5ba14ffSstephh 			}
12098e7248e5SStephen Hanson 			prop_name[i - 1] = 't';
12108e7248e5SStephen Hanson 			if (s = config_getprop(cp, prop_name)) {
1211e5ba14ffSstephh 				ptr = STRDUP(s);
12128e7248e5SStephen Hanson 				out(O_ALTFP, "serd override %s_t %s",
12138e7248e5SStephen Hanson 				    serdclass, s);
1214e5ba14ffSstephh 				got_t_override = 1;
1215e5ba14ffSstephh 			}
12168e7248e5SStephen Hanson 			FREE(prop_name);
1217e5ba14ffSstephh 		}
1218e5ba14ffSstephh 
1219b7d3956bSstephh 		if (serdn != -1 && got_n_override == 0) {
1220b7d3956bSstephh 			nval = serdn;
12218e7248e5SStephen Hanson 			out(O_ALTFP, "serd override %s_n %d", serdclass, serdn);
1222b7d3956bSstephh 			got_n_override = 1;
1223b7d3956bSstephh 		}
1224b7d3956bSstephh 		if (serdt != NULL && got_t_override == 0) {
1225b7d3956bSstephh 			ptr = STRDUP(serdt);
12268e7248e5SStephen Hanson 			out(O_ALTFP, "serd override %s_t %s", serdclass, serdt);
1227b7d3956bSstephh 			got_t_override = 1;
1228b7d3956bSstephh 		}
1229b7d3956bSstephh 
1230e5ba14ffSstephh 		if (!got_n_override) {
1231e5ba14ffSstephh 			nN = lut_lookup(serdinst->u.stmt.lutp, (void *)L_N,
1232e5ba14ffSstephh 			    NULL);
12337c478bd9Sstevel@tonic-gate 			ASSERT(nN->t == T_NUM);
1234e5ba14ffSstephh 			nval = (uint_t)nN->u.ull;
1235e5ba14ffSstephh 		}
1236e5ba14ffSstephh 		if (!got_t_override) {
1237e5ba14ffSstephh 			nT = lut_lookup(serdinst->u.stmt.lutp, (void *)L_T,
1238e5ba14ffSstephh 			    NULL);
12397c478bd9Sstevel@tonic-gate 			ASSERT(nT->t == T_TIMEVAL);
1240e5ba14ffSstephh 			tval = (hrtime_t)nT->u.ull;
1241e5ba14ffSstephh 		} else {
1242e5ba14ffSstephh 			const unsigned long long *ullp;
1243e5ba14ffSstephh 			const char *suffix;
1244e5ba14ffSstephh 			int len;
12457c478bd9Sstevel@tonic-gate 
1246e5ba14ffSstephh 			len = strspn(ptr, "0123456789");
1247e5ba14ffSstephh 			suffix = stable(&ptr[len]);
1248e5ba14ffSstephh 			ullp = (unsigned long long *)lut_lookup(Timesuffixlut,
1249e5ba14ffSstephh 			    (void *)suffix, NULL);
1250e5ba14ffSstephh 			ptr[len] = '\0';
1251b7d3956bSstephh 			tval = strtoull(ptr, NULL, 0) * (ullp ? *ullp : 1ll);
1252e5ba14ffSstephh 			FREE(ptr);
1253e5ba14ffSstephh 		}
1254e5ba14ffSstephh 		fmd_serd_create(hdl, serdname, nval, tval);
12557c478bd9Sstevel@tonic-gate 	}
12567c478bd9Sstevel@tonic-gate 
1257b5016cbbSstephh 	newentp = MALLOC(sizeof (*newentp));
12588e7248e5SStephen Hanson 	newentp->ename = stable(serdclass);
12598e7248e5SStephen Hanson 	FREE(serdclass);
1260b5016cbbSstephh 	newentp->ipath = ipath(serdinst->u.stmt.np->u.event.epname);
1261b5016cbbSstephh 	newentp->hdl = hdl;
1262b5016cbbSstephh 	if (lut_lookup(SerdEngines, newentp, (lut_cmp)serd_cmp) == NULL) {
1263b5016cbbSstephh 		SerdEngines = lut_add(SerdEngines, (void *)newentp,
1264e5ba14ffSstephh 		    (void *)newentp, (lut_cmp)serd_cmp);
1265b5016cbbSstephh 		Serd_need_save = 1;
1266b5016cbbSstephh 		serd_save();
1267b5016cbbSstephh 	} else {
1268b5016cbbSstephh 		FREE(newentp);
1269b5016cbbSstephh 	}
1270b5016cbbSstephh 
12717c478bd9Sstevel@tonic-gate 
12727c478bd9Sstevel@tonic-gate 	/*
12737c478bd9Sstevel@tonic-gate 	 * increment SERD engine.  if engine fires, reset serd
1274b7d3956bSstephh 	 * engine and return trip_strcode if required.
12757c478bd9Sstevel@tonic-gate 	 */
1276b7d3956bSstephh 	for (i = 0; i < serdincrement; i++) {
12777c478bd9Sstevel@tonic-gate 		if (fmd_serd_record(hdl, serdname, ffep)) {
12783e8d8e18Sdb35262 			fmd_case_add_serd(hdl, fmcase, serdname);
12797c478bd9Sstevel@tonic-gate 			fmd_serd_reset(hdl, serdname);
1280b7d3956bSstephh 
1281b7d3956bSstephh 			if (ippp) {
1282b7d3956bSstephh 				struct node *tripinst =
1283b7d3956bSstephh 				    lut_lookup(serdinst->u.stmt.lutp,
1284b7d3956bSstephh 				    (void *)L_trip, NULL);
1285b7d3956bSstephh 				ASSERT(tripinst != NULL);
1286b7d3956bSstephh 				*enamep = tripinst->u.event.ename->u.name.s;
1287b7d3956bSstephh 				*ippp = ipath(tripinst->u.event.epname);
1288b7d3956bSstephh 				out(O_ALTFP|O_NONL,
1289b7d3956bSstephh 				    "[engine fired: %s, sending: ", serdname);
12907c478bd9Sstevel@tonic-gate 				ipath_print(O_ALTFP|O_NONL, *enamep, *ippp);
12917c478bd9Sstevel@tonic-gate 				out(O_ALTFP, "]");
1292b7d3956bSstephh 			} else {
1293b7d3956bSstephh 				out(O_ALTFP, "[engine fired: %s, no trip]",
1294b7d3956bSstephh 				    serdname);
1295b7d3956bSstephh 			}
12967c478bd9Sstevel@tonic-gate 			FREE(serdname);
12977c478bd9Sstevel@tonic-gate 			return (1);
12987c478bd9Sstevel@tonic-gate 		}
1299b7d3956bSstephh 	}
13007c478bd9Sstevel@tonic-gate 
13017c478bd9Sstevel@tonic-gate 	FREE(serdname);
13027c478bd9Sstevel@tonic-gate 	return (0);
13037c478bd9Sstevel@tonic-gate }
13047c478bd9Sstevel@tonic-gate 
13057c478bd9Sstevel@tonic-gate /*
13067c478bd9Sstevel@tonic-gate  * search a suspect list for upsets.  feed each upset to serd_eval() and
13077c478bd9Sstevel@tonic-gate  * build up tripped[], an array of ereports produced by the firing of
13087c478bd9Sstevel@tonic-gate  * any SERD engines.  then feed each ereport back into
13097c478bd9Sstevel@tonic-gate  * fme_receive_report().
13107c478bd9Sstevel@tonic-gate  *
13117c478bd9Sstevel@tonic-gate  * returns ntrip, the number of these ereports produced.
13127c478bd9Sstevel@tonic-gate  */
13137c478bd9Sstevel@tonic-gate static int
upsets_eval(struct fme * fmep,fmd_event_t * ffep)13147c478bd9Sstevel@tonic-gate upsets_eval(struct fme *fmep, fmd_event_t *ffep)
13157c478bd9Sstevel@tonic-gate {
13167c478bd9Sstevel@tonic-gate 	/* we build an array of tripped ereports that we send ourselves */
13177c478bd9Sstevel@tonic-gate 	struct {
13187c478bd9Sstevel@tonic-gate 		const char *ename;
13197c478bd9Sstevel@tonic-gate 		const struct ipath *ipp;
13207c478bd9Sstevel@tonic-gate 	} *tripped;
13217c478bd9Sstevel@tonic-gate 	struct event *sp;
13227c478bd9Sstevel@tonic-gate 	int ntrip, nupset, i;
13237c478bd9Sstevel@tonic-gate 
13247c478bd9Sstevel@tonic-gate 	/*
13257c478bd9Sstevel@tonic-gate 	 * count the number of upsets to determine the upper limit on
13267c478bd9Sstevel@tonic-gate 	 * expected trip ereport strings.  remember that one upset can
13277c478bd9Sstevel@tonic-gate 	 * lead to at most one ereport.
13287c478bd9Sstevel@tonic-gate 	 */
13297c478bd9Sstevel@tonic-gate 	nupset = 0;
13307c478bd9Sstevel@tonic-gate 	for (sp = fmep->suspects; sp; sp = sp->suspects) {
13317c478bd9Sstevel@tonic-gate 		if (sp->t == N_UPSET)
13327c478bd9Sstevel@tonic-gate 			nupset++;
13337c478bd9Sstevel@tonic-gate 	}
13347c478bd9Sstevel@tonic-gate 
13357c478bd9Sstevel@tonic-gate 	if (nupset == 0)
13367c478bd9Sstevel@tonic-gate 		return (0);
13377c478bd9Sstevel@tonic-gate 
13387c478bd9Sstevel@tonic-gate 	/*
13397c478bd9Sstevel@tonic-gate 	 * get to this point if we have upsets and expect some trip
13407c478bd9Sstevel@tonic-gate 	 * ereports
13417c478bd9Sstevel@tonic-gate 	 */
13427c478bd9Sstevel@tonic-gate 	tripped = alloca(sizeof (*tripped) * nupset);
13437c478bd9Sstevel@tonic-gate 	bzero((void *)tripped, sizeof (*tripped) * nupset);
13447c478bd9Sstevel@tonic-gate 
13457c478bd9Sstevel@tonic-gate 	ntrip = 0;
13467c478bd9Sstevel@tonic-gate 	for (sp = fmep->suspects; sp; sp = sp->suspects)
13473e8d8e18Sdb35262 		if (sp->t == N_UPSET &&
13487aec1d6eScindi 		    serd_eval(fmep, fmep->hdl, ffep, fmep->fmcase, sp,
1349b7d3956bSstephh 		    &tripped[ntrip].ename, &tripped[ntrip].ipp) == 1)
13507c478bd9Sstevel@tonic-gate 			ntrip++;
13517c478bd9Sstevel@tonic-gate 
135227134bdaSstephh 	for (i = 0; i < ntrip; i++) {
135327134bdaSstephh 		struct event *ep, *nep;
135427134bdaSstephh 		struct fme *nfmep;
135527134bdaSstephh 		fmd_case_t *fmcase;
135627134bdaSstephh 		const struct ipath *ipp;
135727134bdaSstephh 		const char *eventstring;
135827134bdaSstephh 		int prev_verbose;
135927134bdaSstephh 		unsigned long long my_delay = TIMEVAL_EVENTUALLY;
136027134bdaSstephh 		enum fme_state state;
136127134bdaSstephh 
136227134bdaSstephh 		/*
136327134bdaSstephh 		 * First try and evaluate a case with the trip ereport plus
136427134bdaSstephh 		 * all the other ereports that cause the trip. If that fails
136527134bdaSstephh 		 * to evaluate then try again with just this ereport on its own.
136627134bdaSstephh 		 */
136727134bdaSstephh 		out(O_ALTFP|O_NONL, "fme_receive_report_serd: ");
136827134bdaSstephh 		ipath_print(O_ALTFP|O_NONL, tripped[i].ename, tripped[i].ipp);
136927134bdaSstephh 		out(O_ALTFP|O_STAMP, NULL);
137027134bdaSstephh 		ep = fmep->e0;
137127134bdaSstephh 		eventstring = ep->enode->u.event.ename->u.name.s;
137227134bdaSstephh 		ipp = ep->ipp;
137327134bdaSstephh 
137427134bdaSstephh 		/*
137527134bdaSstephh 		 * create a duplicate fme and case
137627134bdaSstephh 		 */
137727134bdaSstephh 		fmcase = fmd_case_open(fmep->hdl, NULL);
137827134bdaSstephh 		out(O_ALTFP|O_NONL, "duplicate fme for event [");
137927134bdaSstephh 		ipath_print(O_ALTFP|O_NONL, eventstring, ipp);
138027134bdaSstephh 		out(O_ALTFP, " ]");
1381705e9f42SStephen Hanson 
138227134bdaSstephh 		if ((nfmep = newfme(eventstring, ipp, fmep->hdl,
1383705e9f42SStephen Hanson 		    fmcase, ffep, ep->nvp)) == NULL) {
138427134bdaSstephh 			out(O_ALTFP|O_NONL, "[");
138527134bdaSstephh 			ipath_print(O_ALTFP|O_NONL, eventstring, ipp);
138627134bdaSstephh 			out(O_ALTFP, " CANNOT DIAGNOSE]");
138727134bdaSstephh 			continue;
138827134bdaSstephh 		}
1389705e9f42SStephen Hanson 
139027134bdaSstephh 		Open_fme_count++;
139127134bdaSstephh 		nfmep->pull = fmep->pull;
139227134bdaSstephh 		init_fme_bufs(nfmep);
139327134bdaSstephh 		out(O_ALTFP|O_NONL, "[");
139427134bdaSstephh 		ipath_print(O_ALTFP|O_NONL, eventstring, ipp);
139527134bdaSstephh 		out(O_ALTFP, " created FME%d, case %s]", nfmep->id,
139627134bdaSstephh 		    fmd_case_uuid(nfmep->hdl, nfmep->fmcase));
139727134bdaSstephh 		if (ffep) {
139827134bdaSstephh 			fmd_case_setprincipal(nfmep->hdl, nfmep->fmcase, ffep);
1399b7d3956bSstephh 			fmd_case_add_ereport(nfmep->hdl, nfmep->fmcase, ffep);
140027134bdaSstephh 			nfmep->e0r = ffep;
140127134bdaSstephh 		}
140227134bdaSstephh 
140327134bdaSstephh 		/*
140427134bdaSstephh 		 * add the original ereports
140527134bdaSstephh 		 */
140627134bdaSstephh 		for (ep = fmep->observations; ep; ep = ep->observations) {
140727134bdaSstephh 			eventstring = ep->enode->u.event.ename->u.name.s;
140827134bdaSstephh 			ipp = ep->ipp;
140927134bdaSstephh 			out(O_ALTFP|O_NONL, "adding event [");
141027134bdaSstephh 			ipath_print(O_ALTFP|O_NONL, eventstring, ipp);
141127134bdaSstephh 			out(O_ALTFP, " ]");
141227134bdaSstephh 			nep = itree_lookup(nfmep->eventtree, eventstring, ipp);
141327134bdaSstephh 			if (nep->count++ == 0) {
141427134bdaSstephh 				nep->observations = nfmep->observations;
141527134bdaSstephh 				nfmep->observations = nep;
141627134bdaSstephh 				serialize_observation(nfmep, eventstring, ipp);
141727134bdaSstephh 				nep->nvp = evnv_dupnvl(ep->nvp);
141827134bdaSstephh 			}
1419b7d3956bSstephh 			if (ep->ffep && ep->ffep != ffep)
142027134bdaSstephh 				fmd_case_add_ereport(nfmep->hdl, nfmep->fmcase,
1421b7d3956bSstephh 				    ep->ffep);
142227134bdaSstephh 			stats_counter_bump(nfmep->Rcount);
142327134bdaSstephh 		}
142427134bdaSstephh 
142527134bdaSstephh 		/*
142627134bdaSstephh 		 * add the serd trigger ereport
142727134bdaSstephh 		 */
142827134bdaSstephh 		if ((ep = itree_lookup(nfmep->eventtree, tripped[i].ename,
142927134bdaSstephh 		    tripped[i].ipp)) == NULL) {
143027134bdaSstephh 			/*
143127134bdaSstephh 			 * The trigger ereport is not in the instance tree. It
143227134bdaSstephh 			 * was presumably removed by prune_propagations() as
143327134bdaSstephh 			 * this combination of events is not present in the
143427134bdaSstephh 			 * rules.
143527134bdaSstephh 			 */
143627134bdaSstephh 			out(O_ALTFP, "upsets_eval: e0 not in instance tree");
143732d4e834STarik Soydan 			Undiag_reason = UD_VAL_BADEVENTI;
143827134bdaSstephh 			goto retry_lone_ereport;
143927134bdaSstephh 		}
144027134bdaSstephh 		out(O_ALTFP|O_NONL, "adding event [");
144127134bdaSstephh 		ipath_print(O_ALTFP|O_NONL, tripped[i].ename, tripped[i].ipp);
144227134bdaSstephh 		out(O_ALTFP, " ]");
144327134bdaSstephh 		nfmep->ecurrent = ep;
144427134bdaSstephh 		ep->nvp = NULL;
144527134bdaSstephh 		ep->count = 1;
144627134bdaSstephh 		ep->observations = nfmep->observations;
144727134bdaSstephh 		nfmep->observations = ep;
144827134bdaSstephh 
144927134bdaSstephh 		/*
145027134bdaSstephh 		 * just peek first.
145127134bdaSstephh 		 */
145227134bdaSstephh 		nfmep->peek = 1;
145327134bdaSstephh 		prev_verbose = Verbose;
145427134bdaSstephh 		if (Debug == 0)
145527134bdaSstephh 			Verbose = 0;
145627134bdaSstephh 		lut_walk(nfmep->eventtree, (lut_cb)clear_arrows, (void *)nfmep);
145727134bdaSstephh 		state = hypothesise(nfmep, nfmep->e0, nfmep->ull, &my_delay);
145827134bdaSstephh 		nfmep->peek = 0;
145927134bdaSstephh 		Verbose = prev_verbose;
146027134bdaSstephh 		if (state == FME_DISPROVED) {
146127134bdaSstephh 			out(O_ALTFP, "upsets_eval: hypothesis disproved");
146232d4e834STarik Soydan 			Undiag_reason = UD_VAL_UNSOLVD;
146327134bdaSstephh retry_lone_ereport:
146427134bdaSstephh 			/*
146527134bdaSstephh 			 * However the trigger ereport on its own might be
146627134bdaSstephh 			 * diagnosable, so check for that. Undo the new fme
146727134bdaSstephh 			 * and case we just created and call fme_receive_report.
146827134bdaSstephh 			 */
146927134bdaSstephh 			out(O_ALTFP|O_NONL, "[");
147027134bdaSstephh 			ipath_print(O_ALTFP|O_NONL, tripped[i].ename,
147127134bdaSstephh 			    tripped[i].ipp);
147227134bdaSstephh 			out(O_ALTFP, " retrying with just trigger ereport]");
147327134bdaSstephh 			itree_free(nfmep->eventtree);
147427134bdaSstephh 			nfmep->eventtree = NULL;
147527134bdaSstephh 			structconfig_free(nfmep->config);
147627134bdaSstephh 			nfmep->config = NULL;
147727134bdaSstephh 			destroy_fme_bufs(nfmep);
147827134bdaSstephh 			fmd_case_close(nfmep->hdl, nfmep->fmcase);
14797aec1d6eScindi 			fme_receive_report(fmep->hdl, ffep,
14807c478bd9Sstevel@tonic-gate 			    tripped[i].ename, tripped[i].ipp, NULL);
148127134bdaSstephh 			continue;
148227134bdaSstephh 		}
148327134bdaSstephh 
148427134bdaSstephh 		/*
148527134bdaSstephh 		 * and evaluate
148627134bdaSstephh 		 */
148727134bdaSstephh 		serialize_observation(nfmep, tripped[i].ename, tripped[i].ipp);
148827134bdaSstephh 		fme_eval(nfmep, ffep);
148927134bdaSstephh 	}
14907c478bd9Sstevel@tonic-gate 
14917c478bd9Sstevel@tonic-gate 	return (ntrip);
14927c478bd9Sstevel@tonic-gate }
14937c478bd9Sstevel@tonic-gate 
14947c478bd9Sstevel@tonic-gate /*
14957c478bd9Sstevel@tonic-gate  * fme_receive_external_report -- call when an external ereport comes in
14967c478bd9Sstevel@tonic-gate  *
14977c478bd9Sstevel@tonic-gate  * this routine just converts the relevant information from the ereport
14987c478bd9Sstevel@tonic-gate  * into a format used internally and passes it on to fme_receive_report().
14997c478bd9Sstevel@tonic-gate  */
15007c478bd9Sstevel@tonic-gate void
fme_receive_external_report(fmd_hdl_t * hdl,fmd_event_t * ffep,nvlist_t * nvl,const char * class)15017c478bd9Sstevel@tonic-gate fme_receive_external_report(fmd_hdl_t *hdl, fmd_event_t *ffep, nvlist_t *nvl,
1502602ca9eaScth     const char *class)
15037c478bd9Sstevel@tonic-gate {
1504602ca9eaScth 	struct node		*epnamenp;
1505602ca9eaScth 	fmd_case_t		*fmcase;
15067c478bd9Sstevel@tonic-gate 	const struct ipath	*ipp;
1507705e9f42SStephen Hanson 	nvlist_t		*detector = NULL;
15087c478bd9Sstevel@tonic-gate 
1509602ca9eaScth 	class = stable(class);
1510b5016cbbSstephh 
1511602ca9eaScth 	/* Get the component path from the ereport */
1512602ca9eaScth 	epnamenp = platform_getpath(nvl);
1513602ca9eaScth 
1514602ca9eaScth 	/* See if we ended up without a path. */
1515602ca9eaScth 	if (epnamenp == NULL) {
1516602ca9eaScth 		/* See if class permits silent discard on unknown component. */
1517602ca9eaScth 		if (lut_lookup(Ereportenames_discard, (void *)class, NULL)) {
1518602ca9eaScth 			out(O_ALTFP|O_VERB2, "Unable to map \"%s\" ereport "
1519602ca9eaScth 			    "to component path, but silent discard allowed.",
1520602ca9eaScth 			    class);
1521602ca9eaScth 		} else {
1522602ca9eaScth 			/*
1523602ca9eaScth 			 * XFILE: Failure to find a component is bad unless
1524602ca9eaScth 			 * 'discard_if_config_unknown=1' was specified in the
1525602ca9eaScth 			 * ereport definition. Indicate undiagnosable.
1526602ca9eaScth 			 */
152732d4e834STarik Soydan 			Undiag_reason = UD_VAL_NOPATH;
1528b5016cbbSstephh 			fmcase = fmd_case_open(hdl, NULL);
1529705e9f42SStephen Hanson 
1530705e9f42SStephen Hanson 			/*
1531705e9f42SStephen Hanson 			 * We don't have a component path here (which means that
1532705e9f42SStephen Hanson 			 * the detector was not in hc-scheme and couldn't be
1533705e9f42SStephen Hanson 			 * converted to hc-scheme. Report the raw detector as
1534705e9f42SStephen Hanson 			 * the suspect resource if there is one.
1535705e9f42SStephen Hanson 			 */
1536705e9f42SStephen Hanson 			(void) nvlist_lookup_nvlist(nvl, FM_EREPORT_DETECTOR,
1537705e9f42SStephen Hanson 			    &detector);
1538705e9f42SStephen Hanson 			publish_undiagnosable(hdl, ffep, fmcase, detector,
1539705e9f42SStephen Hanson 			    (char *)class);
1540602ca9eaScth 		}
15417c478bd9Sstevel@tonic-gate 		return;
15427c478bd9Sstevel@tonic-gate 	}
15437c478bd9Sstevel@tonic-gate 
15447c478bd9Sstevel@tonic-gate 	ipp = ipath(epnamenp);
15457c478bd9Sstevel@tonic-gate 	tree_free(epnamenp);
1546602ca9eaScth 	fme_receive_report(hdl, ffep, class, ipp, nvl);
15477c478bd9Sstevel@tonic-gate }
15487c478bd9Sstevel@tonic-gate 
154908f6c065Sgavinm /*ARGSUSED*/
155008f6c065Sgavinm void
fme_receive_repair_list(fmd_hdl_t * hdl,fmd_event_t * ffep,nvlist_t * nvl,const char * eventstring)155108f6c065Sgavinm fme_receive_repair_list(fmd_hdl_t *hdl, fmd_event_t *ffep, nvlist_t *nvl,
155208f6c065Sgavinm     const char *eventstring)
155308f6c065Sgavinm {
155408f6c065Sgavinm 	char *uuid;
155508f6c065Sgavinm 	nvlist_t **nva;
155608f6c065Sgavinm 	uint_t nvc;
155708f6c065Sgavinm 	const struct ipath *ipp;
155808f6c065Sgavinm 
155908f6c065Sgavinm 	if (nvlist_lookup_string(nvl, FM_SUSPECT_UUID, &uuid) != 0 ||
156008f6c065Sgavinm 	    nvlist_lookup_nvlist_array(nvl, FM_SUSPECT_FAULT_LIST,
156108f6c065Sgavinm 	    &nva, &nvc) != 0) {
156208f6c065Sgavinm 		out(O_ALTFP, "No uuid or fault list for list.repaired event");
156308f6c065Sgavinm 		return;
156408f6c065Sgavinm 	}
156508f6c065Sgavinm 
156608f6c065Sgavinm 	out(O_ALTFP, "Processing list.repaired from case %s", uuid);
156708f6c065Sgavinm 
156808f6c065Sgavinm 	while (nvc-- != 0) {
156908f6c065Sgavinm 		/*
1570b5016cbbSstephh 		 * Reset any istat or serd engine associated with this path.
157108f6c065Sgavinm 		 */
157208f6c065Sgavinm 		char *path;
157308f6c065Sgavinm 
157408f6c065Sgavinm 		if ((ipp = platform_fault2ipath(*nva++)) == NULL)
157508f6c065Sgavinm 			continue;
157608f6c065Sgavinm 
157708f6c065Sgavinm 		path = ipath2str(NULL, ipp);
157808f6c065Sgavinm 		out(O_ALTFP, "fme_receive_repair_list: resetting state for %s",
157908f6c065Sgavinm 		    path);
158008f6c065Sgavinm 		FREE(path);
158108f6c065Sgavinm 
158208f6c065Sgavinm 		lut_walk(Istats, (lut_cb)istat_counter_reset_cb, (void *)ipp);
158308f6c065Sgavinm 		istat_save();
158408f6c065Sgavinm 
1585b5016cbbSstephh 		lut_walk(SerdEngines, (lut_cb)serd_reset_cb, (void *)ipp);
1586b5016cbbSstephh 		serd_save();
158708f6c065Sgavinm 	}
158808f6c065Sgavinm }
158908f6c065Sgavinm 
1590e5ba14ffSstephh /*ARGSUSED*/
1591e5ba14ffSstephh void
fme_receive_topology_change(void)1592e5ba14ffSstephh fme_receive_topology_change(void)
1593e5ba14ffSstephh {
1594e5ba14ffSstephh 	lut_walk(Istats, (lut_cb)istat_counter_topo_chg_cb, NULL);
1595e5ba14ffSstephh 	istat_save();
1596e5ba14ffSstephh 
1597e5ba14ffSstephh 	lut_walk(SerdEngines, (lut_cb)serd_topo_chg_cb, NULL);
1598e5ba14ffSstephh 	serd_save();
1599e5ba14ffSstephh }
1600e5ba14ffSstephh 
16017aec1d6eScindi static int mark_arrows(struct fme *fmep, struct event *ep, int mark,
160200d0963fSdilpreet     unsigned long long at_latest_by, unsigned long long *pdelay, int keep);
16037aec1d6eScindi 
16047aec1d6eScindi /* ARGSUSED */
16057aec1d6eScindi static void
clear_arrows(struct event * ep,struct event * ep2,struct fme * fmep)16067aec1d6eScindi clear_arrows(struct event *ep, struct event *ep2, struct fme *fmep)
16077aec1d6eScindi {
16087aec1d6eScindi 	struct bubble *bp;
16097aec1d6eScindi 	struct arrowlist *ap;
16107aec1d6eScindi 
16117aec1d6eScindi 	ep->cached_state = 0;
161200d0963fSdilpreet 	ep->keep_in_tree = 0;
16137aec1d6eScindi 	for (bp = itree_next_bubble(ep, NULL); bp;
16147aec1d6eScindi 	    bp = itree_next_bubble(ep, bp)) {
16157aec1d6eScindi 		if (bp->t != B_FROM)
16167aec1d6eScindi 			continue;
16177aec1d6eScindi 		bp->mark = 0;
16187aec1d6eScindi 		for (ap = itree_next_arrow(bp, NULL); ap;
16197aec1d6eScindi 		    ap = itree_next_arrow(bp, ap))
16207aec1d6eScindi 			ap->arrowp->mark = 0;
16217aec1d6eScindi 	}
16227aec1d6eScindi }
16237aec1d6eScindi 
16247c478bd9Sstevel@tonic-gate static void
fme_receive_report(fmd_hdl_t * hdl,fmd_event_t * ffep,const char * eventstring,const struct ipath * ipp,nvlist_t * nvl)16257c478bd9Sstevel@tonic-gate fme_receive_report(fmd_hdl_t *hdl, fmd_event_t *ffep,
16267c478bd9Sstevel@tonic-gate     const char *eventstring, const struct ipath *ipp, nvlist_t *nvl)
16277c478bd9Sstevel@tonic-gate {
16287c478bd9Sstevel@tonic-gate 	struct event *ep;
16297c478bd9Sstevel@tonic-gate 	struct fme *fmep = NULL;
16300cc1f05eSjrutt 	struct fme *ofmep = NULL;
16310cc1f05eSjrutt 	struct fme *cfmep, *svfmep;
16327c478bd9Sstevel@tonic-gate 	int matched = 0;
16330cc1f05eSjrutt 	nvlist_t *defect;
1634b5016cbbSstephh 	fmd_case_t *fmcase;
1635705e9f42SStephen Hanson 	char *reason;
16367c478bd9Sstevel@tonic-gate 
16377c478bd9Sstevel@tonic-gate 	out(O_ALTFP|O_NONL, "fme_receive_report: ");
16387c478bd9Sstevel@tonic-gate 	ipath_print(O_ALTFP|O_NONL, eventstring, ipp);
16397c478bd9Sstevel@tonic-gate 	out(O_ALTFP|O_STAMP, NULL);
16407c478bd9Sstevel@tonic-gate 
16417c478bd9Sstevel@tonic-gate 	/* decide which FME it goes to */
16427c478bd9Sstevel@tonic-gate 	for (fmep = FMElist; fmep; fmep = fmep->next) {
16437c478bd9Sstevel@tonic-gate 		int prev_verbose;
16447c478bd9Sstevel@tonic-gate 		unsigned long long my_delay = TIMEVAL_EVENTUALLY;
16457c478bd9Sstevel@tonic-gate 		enum fme_state state;
16467aec1d6eScindi 		nvlist_t *pre_peek_nvp = NULL;
16477c478bd9Sstevel@tonic-gate 
16480cc1f05eSjrutt 		if (fmep->overflow) {
16490cc1f05eSjrutt 			if (!(fmd_case_closed(fmep->hdl, fmep->fmcase)))
16500cc1f05eSjrutt 				ofmep = fmep;
16510cc1f05eSjrutt 
16520cc1f05eSjrutt 			continue;
16530cc1f05eSjrutt 		}
16540cc1f05eSjrutt 
1655d96ce684Sstephh 		/*
1656d96ce684Sstephh 		 * ignore solved or closed cases
1657d96ce684Sstephh 		 */
1658d96ce684Sstephh 		if (fmep->posted_suspects ||
1659d96ce684Sstephh 		    fmd_case_solved(fmep->hdl, fmep->fmcase) ||
1660d96ce684Sstephh 		    fmd_case_closed(fmep->hdl, fmep->fmcase))
1661d96ce684Sstephh 			continue;
1662d96ce684Sstephh 
16637c478bd9Sstevel@tonic-gate 		/* look up event in event tree for this FME */
16647c478bd9Sstevel@tonic-gate 		if ((ep = itree_lookup(fmep->eventtree,
16657c478bd9Sstevel@tonic-gate 		    eventstring, ipp)) == NULL)
16667c478bd9Sstevel@tonic-gate 			continue;
16677c478bd9Sstevel@tonic-gate 
16687c478bd9Sstevel@tonic-gate 		/* note observation */
16697c478bd9Sstevel@tonic-gate 		fmep->ecurrent = ep;
16707c478bd9Sstevel@tonic-gate 		if (ep->count++ == 0) {
16717c478bd9Sstevel@tonic-gate 			/* link it into list of observations seen */
16727c478bd9Sstevel@tonic-gate 			ep->observations = fmep->observations;
16737c478bd9Sstevel@tonic-gate 			fmep->observations = ep;
16747c478bd9Sstevel@tonic-gate 			ep->nvp = evnv_dupnvl(nvl);
16757aec1d6eScindi 		} else {
16767aec1d6eScindi 			/* use new payload values for peek */
16777aec1d6eScindi 			pre_peek_nvp = ep->nvp;
16787aec1d6eScindi 			ep->nvp = evnv_dupnvl(nvl);
16797c478bd9Sstevel@tonic-gate 		}
16807c478bd9Sstevel@tonic-gate 
16817c478bd9Sstevel@tonic-gate 		/* tell hypothesise() not to mess with suspect list */
16827c478bd9Sstevel@tonic-gate 		fmep->peek = 1;
16837c478bd9Sstevel@tonic-gate 
16847c478bd9Sstevel@tonic-gate 		/* don't want this to be verbose (unless Debug is set) */
16857c478bd9Sstevel@tonic-gate 		prev_verbose = Verbose;
16867c478bd9Sstevel@tonic-gate 		if (Debug == 0)
16877c478bd9Sstevel@tonic-gate 			Verbose = 0;
16887c478bd9Sstevel@tonic-gate 
16897aec1d6eScindi 		lut_walk(fmep->eventtree, (lut_cb)clear_arrows, (void *)fmep);
16907aec1d6eScindi 		state = hypothesise(fmep, fmep->e0, fmep->ull, &my_delay);
16917c478bd9Sstevel@tonic-gate 
16927c478bd9Sstevel@tonic-gate 		fmep->peek = 0;
16937c478bd9Sstevel@tonic-gate 
16947c478bd9Sstevel@tonic-gate 		/* put verbose flag back */
16957c478bd9Sstevel@tonic-gate 		Verbose = prev_verbose;
16967c478bd9Sstevel@tonic-gate 
16977c478bd9Sstevel@tonic-gate 		if (state != FME_DISPROVED) {
16987c478bd9Sstevel@tonic-gate 			/* found an FME that explains the ereport */
16997c478bd9Sstevel@tonic-gate 			matched++;
17007c478bd9Sstevel@tonic-gate 			out(O_ALTFP|O_NONL, "[");
17017c478bd9Sstevel@tonic-gate 			ipath_print(O_ALTFP|O_NONL, eventstring, ipp);
17027c478bd9Sstevel@tonic-gate 			out(O_ALTFP, " explained by FME%d]", fmep->id);
17037c478bd9Sstevel@tonic-gate 
17047aec1d6eScindi 			nvlist_free(pre_peek_nvp);
17057aec1d6eScindi 
17067c478bd9Sstevel@tonic-gate 			if (ep->count == 1)
17077c478bd9Sstevel@tonic-gate 				serialize_observation(fmep, eventstring, ipp);
17087c478bd9Sstevel@tonic-gate 
1709b7d3956bSstephh 			if (ffep) {
17107c478bd9Sstevel@tonic-gate 				fmd_case_add_ereport(hdl, fmep->fmcase, ffep);
1711b7d3956bSstephh 				ep->ffep = ffep;
1712b7d3956bSstephh 			}
17137c478bd9Sstevel@tonic-gate 
17147c478bd9Sstevel@tonic-gate 			stats_counter_bump(fmep->Rcount);
17157c478bd9Sstevel@tonic-gate 
17167c478bd9Sstevel@tonic-gate 			/* re-eval FME */
17177c478bd9Sstevel@tonic-gate 			fme_eval(fmep, ffep);
17187c478bd9Sstevel@tonic-gate 		} else {
17197c478bd9Sstevel@tonic-gate 
17207c478bd9Sstevel@tonic-gate 			/* not a match, undo noting of observation */
17217c478bd9Sstevel@tonic-gate 			fmep->ecurrent = NULL;
17227c478bd9Sstevel@tonic-gate 			if (--ep->count == 0) {
17237c478bd9Sstevel@tonic-gate 				/* unlink it from observations */
17247c478bd9Sstevel@tonic-gate 				fmep->observations = ep->observations;
17257c478bd9Sstevel@tonic-gate 				ep->observations = NULL;
17267c478bd9Sstevel@tonic-gate 				nvlist_free(ep->nvp);
17277c478bd9Sstevel@tonic-gate 				ep->nvp = NULL;
17287aec1d6eScindi 			} else {
17297aec1d6eScindi 				nvlist_free(ep->nvp);
17307aec1d6eScindi 				ep->nvp = pre_peek_nvp;
17317c478bd9Sstevel@tonic-gate 			}
17327c478bd9Sstevel@tonic-gate 		}
17337c478bd9Sstevel@tonic-gate 	}
17347c478bd9Sstevel@tonic-gate 
17357c478bd9Sstevel@tonic-gate 	if (matched)
17367c478bd9Sstevel@tonic-gate 		return;	/* explained by at least one existing FME */
17377c478bd9Sstevel@tonic-gate 
17387c478bd9Sstevel@tonic-gate 	/* clean up closed fmes */
17390cc1f05eSjrutt 	cfmep = ClosedFMEs;
17400cc1f05eSjrutt 	while (cfmep != NULL) {
17410cc1f05eSjrutt 		svfmep = cfmep->next;
17420cc1f05eSjrutt 		destroy_fme(cfmep);
17430cc1f05eSjrutt 		cfmep = svfmep;
17447c478bd9Sstevel@tonic-gate 	}
17457c478bd9Sstevel@tonic-gate 	ClosedFMEs = NULL;
17467c478bd9Sstevel@tonic-gate 
17470cc1f05eSjrutt 	if (ofmep) {
17480cc1f05eSjrutt 		out(O_ALTFP|O_NONL, "[");
17490cc1f05eSjrutt 		ipath_print(O_ALTFP|O_NONL, eventstring, ipp);
17500cc1f05eSjrutt 		out(O_ALTFP, " ADDING TO OVERFLOW FME]");
17510cc1f05eSjrutt 		if (ffep)
17520cc1f05eSjrutt 			fmd_case_add_ereport(hdl, ofmep->fmcase, ffep);
17530cc1f05eSjrutt 
17540cc1f05eSjrutt 		return;
17550cc1f05eSjrutt 
17560cc1f05eSjrutt 	} else if (Max_fme && (Open_fme_count >= Max_fme)) {
17570cc1f05eSjrutt 		out(O_ALTFP|O_NONL, "[");
17580cc1f05eSjrutt 		ipath_print(O_ALTFP|O_NONL, eventstring, ipp);
17590cc1f05eSjrutt 		out(O_ALTFP, " MAX OPEN FME REACHED]");
1760b5016cbbSstephh 
1761b5016cbbSstephh 		fmcase = fmd_case_open(hdl, NULL);
1762b5016cbbSstephh 
17630cc1f05eSjrutt 		/* Create overflow fme */
1764705e9f42SStephen Hanson 		if ((fmep = newfme(eventstring, ipp, hdl, fmcase, ffep,
1765705e9f42SStephen Hanson 		    nvl)) == NULL) {
17660cc1f05eSjrutt 			out(O_ALTFP|O_NONL, "[");
17670cc1f05eSjrutt 			ipath_print(O_ALTFP|O_NONL, eventstring, ipp);
17680cc1f05eSjrutt 			out(O_ALTFP, " CANNOT OPEN OVERFLOW FME]");
17690cc1f05eSjrutt 			return;
17700cc1f05eSjrutt 		}
17710cc1f05eSjrutt 
17720cc1f05eSjrutt 		Open_fme_count++;
17730cc1f05eSjrutt 
17740cc1f05eSjrutt 		init_fme_bufs(fmep);
17750cc1f05eSjrutt 		fmep->overflow = B_TRUE;
17760cc1f05eSjrutt 
17770cc1f05eSjrutt 		if (ffep)
17780cc1f05eSjrutt 			fmd_case_add_ereport(hdl, fmep->fmcase, ffep);
17790cc1f05eSjrutt 
178032d4e834STarik Soydan 		Undiag_reason = UD_VAL_MAXFME;
178132d4e834STarik Soydan 		defect = fmd_nvl_create_fault(hdl,
178232d4e834STarik Soydan 		    undiag_2defect_str(Undiag_reason), 100, NULL, NULL, NULL);
1783705e9f42SStephen Hanson 		reason = undiag_2reason_str(Undiag_reason, NULL);
1784705e9f42SStephen Hanson 		(void) nvlist_add_string(defect, UNDIAG_REASON, reason);
1785705e9f42SStephen Hanson 		FREE(reason);
17860cc1f05eSjrutt 		fmd_case_add_suspect(hdl, fmep->fmcase, defect);
17870cc1f05eSjrutt 		fmd_case_solve(hdl, fmep->fmcase);
178832d4e834STarik Soydan 		Undiag_reason = UD_VAL_UNKNOWN;
17890cc1f05eSjrutt 		return;
17900cc1f05eSjrutt 	}
17910cc1f05eSjrutt 
1792b5016cbbSstephh 	/* open a case */
1793b5016cbbSstephh 	fmcase = fmd_case_open(hdl, NULL);
1794b5016cbbSstephh 
17957c478bd9Sstevel@tonic-gate 	/* start a new FME */
1796705e9f42SStephen Hanson 	if ((fmep = newfme(eventstring, ipp, hdl, fmcase, ffep, nvl)) == NULL) {
17977c478bd9Sstevel@tonic-gate 		out(O_ALTFP|O_NONL, "[");
17987c478bd9Sstevel@tonic-gate 		ipath_print(O_ALTFP|O_NONL, eventstring, ipp);
17997c478bd9Sstevel@tonic-gate 		out(O_ALTFP, " CANNOT DIAGNOSE]");
18007c478bd9Sstevel@tonic-gate 		return;
18017c478bd9Sstevel@tonic-gate 	}
18027c478bd9Sstevel@tonic-gate 
18030cc1f05eSjrutt 	Open_fme_count++;
18040cc1f05eSjrutt 
18057c478bd9Sstevel@tonic-gate 	init_fme_bufs(fmep);
18067c478bd9Sstevel@tonic-gate 
18077c478bd9Sstevel@tonic-gate 	out(O_ALTFP|O_NONL, "[");
18087c478bd9Sstevel@tonic-gate 	ipath_print(O_ALTFP|O_NONL, eventstring, ipp);
18097c478bd9Sstevel@tonic-gate 	out(O_ALTFP, " created FME%d, case %s]", fmep->id,
18107c478bd9Sstevel@tonic-gate 	    fmd_case_uuid(hdl, fmep->fmcase));
18117c478bd9Sstevel@tonic-gate 
18127c478bd9Sstevel@tonic-gate 	ep = fmep->e0;
18137c478bd9Sstevel@tonic-gate 	ASSERT(ep != NULL);
18147c478bd9Sstevel@tonic-gate 
18157c478bd9Sstevel@tonic-gate 	/* note observation */
18167c478bd9Sstevel@tonic-gate 	fmep->ecurrent = ep;
18177c478bd9Sstevel@tonic-gate 	if (ep->count++ == 0) {
18187c478bd9Sstevel@tonic-gate 		/* link it into list of observations seen */
18197c478bd9Sstevel@tonic-gate 		ep->observations = fmep->observations;
18207c478bd9Sstevel@tonic-gate 		fmep->observations = ep;
18217c478bd9Sstevel@tonic-gate 		ep->nvp = evnv_dupnvl(nvl);
18227c478bd9Sstevel@tonic-gate 		serialize_observation(fmep, eventstring, ipp);
18237aec1d6eScindi 	} else {
18247aec1d6eScindi 		/* new payload overrides any previous */
18257aec1d6eScindi 		nvlist_free(ep->nvp);
18267aec1d6eScindi 		ep->nvp = evnv_dupnvl(nvl);
18277c478bd9Sstevel@tonic-gate 	}
18287c478bd9Sstevel@tonic-gate 
18297c478bd9Sstevel@tonic-gate 	stats_counter_bump(fmep->Rcount);
18307c478bd9Sstevel@tonic-gate 
18317c478bd9Sstevel@tonic-gate 	if (ffep) {
18327c478bd9Sstevel@tonic-gate 		fmd_case_add_ereport(hdl, fmep->fmcase, ffep);
18337c478bd9Sstevel@tonic-gate 		fmd_case_setprincipal(hdl, fmep->fmcase, ffep);
18347c478bd9Sstevel@tonic-gate 		fmep->e0r = ffep;
1835b7d3956bSstephh 		ep->ffep = ffep;
18367c478bd9Sstevel@tonic-gate 	}
18377c478bd9Sstevel@tonic-gate 
18387c478bd9Sstevel@tonic-gate 	/* give the diagnosis algorithm a shot at the new FME state */
18397c478bd9Sstevel@tonic-gate 	fme_eval(fmep, ffep);
18407c478bd9Sstevel@tonic-gate }
18417c478bd9Sstevel@tonic-gate 
18427c478bd9Sstevel@tonic-gate void
fme_status(int flags)18437c478bd9Sstevel@tonic-gate fme_status(int flags)
18447c478bd9Sstevel@tonic-gate {
18457c478bd9Sstevel@tonic-gate 	struct fme *fmep;
18467c478bd9Sstevel@tonic-gate 
18477c478bd9Sstevel@tonic-gate 	if (FMElist == NULL) {
18487c478bd9Sstevel@tonic-gate 		out(flags, "No fault management exercises underway.");
18497c478bd9Sstevel@tonic-gate 		return;
18507c478bd9Sstevel@tonic-gate 	}
18517c478bd9Sstevel@tonic-gate 
18527c478bd9Sstevel@tonic-gate 	for (fmep = FMElist; fmep; fmep = fmep->next)
18537c478bd9Sstevel@tonic-gate 		fme_print(flags, fmep);
18547c478bd9Sstevel@tonic-gate }
18557c478bd9Sstevel@tonic-gate 
18567c478bd9Sstevel@tonic-gate /*
18577c478bd9Sstevel@tonic-gate  * "indent" routines used mostly for nicely formatted debug output, but also
18587c478bd9Sstevel@tonic-gate  * for sanity checking for infinite recursion bugs.
18597c478bd9Sstevel@tonic-gate  */
18607c478bd9Sstevel@tonic-gate 
18617c478bd9Sstevel@tonic-gate #define	MAX_INDENT 1024
18627c478bd9Sstevel@tonic-gate static const char *indent_s[MAX_INDENT];
18637c478bd9Sstevel@tonic-gate static int current_indent;
18647c478bd9Sstevel@tonic-gate 
18657c478bd9Sstevel@tonic-gate static void
indent_push(const char * s)18667c478bd9Sstevel@tonic-gate indent_push(const char *s)
18677c478bd9Sstevel@tonic-gate {
18687c478bd9Sstevel@tonic-gate 	if (current_indent < MAX_INDENT)
18697c478bd9Sstevel@tonic-gate 		indent_s[current_indent++] = s;
18707c478bd9Sstevel@tonic-gate 	else
18717c478bd9Sstevel@tonic-gate 		out(O_DIE, "unexpected recursion depth (%d)", current_indent);
18727c478bd9Sstevel@tonic-gate }
18737c478bd9Sstevel@tonic-gate 
18747c478bd9Sstevel@tonic-gate static void
indent_set(const char * s)18757c478bd9Sstevel@tonic-gate indent_set(const char *s)
18767c478bd9Sstevel@tonic-gate {
18777c478bd9Sstevel@tonic-gate 	current_indent = 0;
18787c478bd9Sstevel@tonic-gate 	indent_push(s);
18797c478bd9Sstevel@tonic-gate }
18807c478bd9Sstevel@tonic-gate 
18817c478bd9Sstevel@tonic-gate static void
indent_pop(void)18827c478bd9Sstevel@tonic-gate indent_pop(void)
18837c478bd9Sstevel@tonic-gate {
18847c478bd9Sstevel@tonic-gate 	if (current_indent > 0)
18857c478bd9Sstevel@tonic-gate 		current_indent--;
18867c478bd9Sstevel@tonic-gate 	else
18877c478bd9Sstevel@tonic-gate 		out(O_DIE, "recursion underflow");
18887c478bd9Sstevel@tonic-gate }
18897c478bd9Sstevel@tonic-gate 
18907c478bd9Sstevel@tonic-gate static void
indent(void)18917c478bd9Sstevel@tonic-gate indent(void)
18927c478bd9Sstevel@tonic-gate {
18937c478bd9Sstevel@tonic-gate 	int i;
18947c478bd9Sstevel@tonic-gate 	if (!Verbose)
18957c478bd9Sstevel@tonic-gate 		return;
18967c478bd9Sstevel@tonic-gate 	for (i = 0; i < current_indent; i++)
18977c478bd9Sstevel@tonic-gate 		out(O_ALTFP|O_VERB|O_NONL, indent_s[i]);
18987c478bd9Sstevel@tonic-gate }
18997c478bd9Sstevel@tonic-gate 
19007c478bd9Sstevel@tonic-gate #define	SLNEW		1
19017c478bd9Sstevel@tonic-gate #define	SLCHANGED	2
19027c478bd9Sstevel@tonic-gate #define	SLWAIT		3
19037c478bd9Sstevel@tonic-gate #define	SLDISPROVED	4
19047c478bd9Sstevel@tonic-gate 
19057c478bd9Sstevel@tonic-gate static void
print_suspects(int circumstance,struct fme * fmep)19067c478bd9Sstevel@tonic-gate print_suspects(int circumstance, struct fme *fmep)
19077c478bd9Sstevel@tonic-gate {
19087c478bd9Sstevel@tonic-gate 	struct event *ep;
19097c478bd9Sstevel@tonic-gate 
19107c478bd9Sstevel@tonic-gate 	out(O_ALTFP|O_NONL, "[");
19117c478bd9Sstevel@tonic-gate 	if (circumstance == SLCHANGED) {
19127c478bd9Sstevel@tonic-gate 		out(O_ALTFP|O_NONL, "FME%d diagnosis changed. state: %s, "
19137c478bd9Sstevel@tonic-gate 		    "suspect list:", fmep->id, fme_state2str(fmep->state));
19147c478bd9Sstevel@tonic-gate 	} else if (circumstance == SLWAIT) {
1915b5016cbbSstephh 		out(O_ALTFP|O_NONL, "FME%d set wait timer %ld ", fmep->id,
1916b5016cbbSstephh 		    fmep->timer);
19177c478bd9Sstevel@tonic-gate 		ptree_timeval(O_ALTFP|O_NONL, &fmep->wull);
19187c478bd9Sstevel@tonic-gate 	} else if (circumstance == SLDISPROVED) {
19197c478bd9Sstevel@tonic-gate 		out(O_ALTFP|O_NONL, "FME%d DIAGNOSIS UNKNOWN", fmep->id);
19207c478bd9Sstevel@tonic-gate 	} else {
19217c478bd9Sstevel@tonic-gate 		out(O_ALTFP|O_NONL, "FME%d DIAGNOSIS PRODUCED:", fmep->id);
19227c478bd9Sstevel@tonic-gate 	}
19237c478bd9Sstevel@tonic-gate 
19247c478bd9Sstevel@tonic-gate 	if (circumstance == SLWAIT || circumstance == SLDISPROVED) {
19257c478bd9Sstevel@tonic-gate 		out(O_ALTFP, "]");
19267c478bd9Sstevel@tonic-gate 		return;
19277c478bd9Sstevel@tonic-gate 	}
19287c478bd9Sstevel@tonic-gate 
19297c478bd9Sstevel@tonic-gate 	for (ep = fmep->suspects; ep; ep = ep->suspects) {
19307c478bd9Sstevel@tonic-gate 		out(O_ALTFP|O_NONL, " ");
19317c478bd9Sstevel@tonic-gate 		itree_pevent_brief(O_ALTFP|O_NONL, ep);
19327c478bd9Sstevel@tonic-gate 	}
19337c478bd9Sstevel@tonic-gate 	out(O_ALTFP, "]");
19347c478bd9Sstevel@tonic-gate }
19357c478bd9Sstevel@tonic-gate 
19367c478bd9Sstevel@tonic-gate static struct node *
eventprop_lookup(struct event * ep,const char * propname)19377c478bd9Sstevel@tonic-gate eventprop_lookup(struct event *ep, const char *propname)
19387c478bd9Sstevel@tonic-gate {
19397c478bd9Sstevel@tonic-gate 	return (lut_lookup(ep->props, (void *)propname, NULL));
19407c478bd9Sstevel@tonic-gate }
19417c478bd9Sstevel@tonic-gate 
19427c478bd9Sstevel@tonic-gate #define	MAXDIGITIDX	23
19437c478bd9Sstevel@tonic-gate static char numbuf[MAXDIGITIDX + 1];
19447c478bd9Sstevel@tonic-gate 
19457c478bd9Sstevel@tonic-gate static int
node2uint(struct node * n,uint_t * valp)19467c478bd9Sstevel@tonic-gate node2uint(struct node *n, uint_t *valp)
19477c478bd9Sstevel@tonic-gate {
19487c478bd9Sstevel@tonic-gate 	struct evalue value;
19497c478bd9Sstevel@tonic-gate 	struct lut *globals = NULL;
19507c478bd9Sstevel@tonic-gate 
19517c478bd9Sstevel@tonic-gate 	if (n == NULL)
19527c478bd9Sstevel@tonic-gate 		return (1);
19537c478bd9Sstevel@tonic-gate 
19547c478bd9Sstevel@tonic-gate 	/*
19557c478bd9Sstevel@tonic-gate 	 * check value.v since we are being asked to convert an unsigned
19567c478bd9Sstevel@tonic-gate 	 * long long int to an unsigned int
19577c478bd9Sstevel@tonic-gate 	 */
19587c478bd9Sstevel@tonic-gate 	if (! eval_expr(n, NULL, NULL, &globals, NULL, NULL, 0, &value) ||
19597c478bd9Sstevel@tonic-gate 	    value.t != UINT64 || value.v > (1ULL << 32))
19607c478bd9Sstevel@tonic-gate 		return (1);
19617c478bd9Sstevel@tonic-gate 
19627c478bd9Sstevel@tonic-gate 	*valp = (uint_t)value.v;
19637c478bd9Sstevel@tonic-gate 
19647c478bd9Sstevel@tonic-gate 	return (0);
19657c478bd9Sstevel@tonic-gate }
19667c478bd9Sstevel@tonic-gate 
19677c478bd9Sstevel@tonic-gate static nvlist_t *
node2fmri(struct node * n)19687c478bd9Sstevel@tonic-gate node2fmri(struct node *n)
19697c478bd9Sstevel@tonic-gate {
19707c478bd9Sstevel@tonic-gate 	nvlist_t **pa, *f, *p;
19717c478bd9Sstevel@tonic-gate 	struct node *nc;
19727c478bd9Sstevel@tonic-gate 	uint_t depth = 0;
19737c478bd9Sstevel@tonic-gate 	char *numstr, *nullbyte;
19747c478bd9Sstevel@tonic-gate 	char *failure;
19757c478bd9Sstevel@tonic-gate 	int err, i;
19767c478bd9Sstevel@tonic-gate 
19777c478bd9Sstevel@tonic-gate 	/* XXX do we need to be able to handle a non-T_NAME node? */
19787c478bd9Sstevel@tonic-gate 	if (n == NULL || n->t != T_NAME)
19797c478bd9Sstevel@tonic-gate 		return (NULL);
19807c478bd9Sstevel@tonic-gate 
19817c478bd9Sstevel@tonic-gate 	for (nc = n; nc != NULL; nc = nc->u.name.next) {
19827c478bd9Sstevel@tonic-gate 		if (nc->u.name.child == NULL || nc->u.name.child->t != T_NUM)
19837c478bd9Sstevel@tonic-gate 			break;
19847c478bd9Sstevel@tonic-gate 		depth++;
19857c478bd9Sstevel@tonic-gate 	}
19867c478bd9Sstevel@tonic-gate 
19877c478bd9Sstevel@tonic-gate 	if (nc != NULL) {
19887c478bd9Sstevel@tonic-gate 		/* We bailed early, something went wrong */
19897c478bd9Sstevel@tonic-gate 		return (NULL);
19907c478bd9Sstevel@tonic-gate 	}
19917c478bd9Sstevel@tonic-gate 
19927c478bd9Sstevel@tonic-gate 	if ((err = nvlist_xalloc(&f, NV_UNIQUE_NAME, &Eft_nv_hdl)) != 0)
19937c478bd9Sstevel@tonic-gate 		out(O_DIE|O_SYS, "alloc of fmri nvl failed");
19947c478bd9Sstevel@tonic-gate 	pa = alloca(depth * sizeof (nvlist_t *));
19957c478bd9Sstevel@tonic-gate 	for (i = 0; i < depth; i++)
19967c478bd9Sstevel@tonic-gate 		pa[i] = NULL;
19977c478bd9Sstevel@tonic-gate 
19987c478bd9Sstevel@tonic-gate 	err = nvlist_add_string(f, FM_FMRI_SCHEME, FM_FMRI_SCHEME_HC);
19997c478bd9Sstevel@tonic-gate 	err |= nvlist_add_uint8(f, FM_VERSION, FM_HC_SCHEME_VERSION);
20007c478bd9Sstevel@tonic-gate 	err |= nvlist_add_string(f, FM_FMRI_HC_ROOT, "");
20017c478bd9Sstevel@tonic-gate 	err |= nvlist_add_uint32(f, FM_FMRI_HC_LIST_SZ, depth);
20027c478bd9Sstevel@tonic-gate 	if (err != 0) {
20037c478bd9Sstevel@tonic-gate 		failure = "basic construction of FMRI failed";
20047c478bd9Sstevel@tonic-gate 		goto boom;
20057c478bd9Sstevel@tonic-gate 	}
20067c478bd9Sstevel@tonic-gate 
20077c478bd9Sstevel@tonic-gate 	numbuf[MAXDIGITIDX] = '\0';
20087c478bd9Sstevel@tonic-gate 	nullbyte = &numbuf[MAXDIGITIDX];
20097c478bd9Sstevel@tonic-gate 	i = 0;
20107c478bd9Sstevel@tonic-gate 
20117c478bd9Sstevel@tonic-gate 	for (nc = n; nc != NULL; nc = nc->u.name.next) {
20127c478bd9Sstevel@tonic-gate 		err = nvlist_xalloc(&p, NV_UNIQUE_NAME, &Eft_nv_hdl);
20137c478bd9Sstevel@tonic-gate 		if (err != 0) {
20147c478bd9Sstevel@tonic-gate 			failure = "alloc of an hc-pair failed";
20157c478bd9Sstevel@tonic-gate 			goto boom;
20167c478bd9Sstevel@tonic-gate 		}
20177c478bd9Sstevel@tonic-gate 		err = nvlist_add_string(p, FM_FMRI_HC_NAME, nc->u.name.s);
20187c478bd9Sstevel@tonic-gate 		numstr = ulltostr(nc->u.name.child->u.ull, nullbyte);
20197c478bd9Sstevel@tonic-gate 		err |= nvlist_add_string(p, FM_FMRI_HC_ID, numstr);
20207c478bd9Sstevel@tonic-gate 		if (err != 0) {
20217c478bd9Sstevel@tonic-gate 			failure = "construction of an hc-pair failed";
20227c478bd9Sstevel@tonic-gate 			goto boom;
20237c478bd9Sstevel@tonic-gate 		}
20247c478bd9Sstevel@tonic-gate 		pa[i++] = p;
20257c478bd9Sstevel@tonic-gate 	}
20267c478bd9Sstevel@tonic-gate 
20277c478bd9Sstevel@tonic-gate 	err = nvlist_add_nvlist_array(f, FM_FMRI_HC_LIST, pa, depth);
20287c478bd9Sstevel@tonic-gate 	if (err == 0) {
20297c478bd9Sstevel@tonic-gate 		for (i = 0; i < depth; i++)
20307c478bd9Sstevel@tonic-gate 			nvlist_free(pa[i]);
20317c478bd9Sstevel@tonic-gate 		return (f);
20327c478bd9Sstevel@tonic-gate 	}
20337c478bd9Sstevel@tonic-gate 	failure = "addition of hc-pair array to FMRI failed";
20347c478bd9Sstevel@tonic-gate 
20357c478bd9Sstevel@tonic-gate boom:
20367c478bd9Sstevel@tonic-gate 	for (i = 0; i < depth; i++)
20377c478bd9Sstevel@tonic-gate 		nvlist_free(pa[i]);
20387c478bd9Sstevel@tonic-gate 	nvlist_free(f);
20397c478bd9Sstevel@tonic-gate 	out(O_DIE, "%s", failure);
20407c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
204180ab886dSwesolows 	return (NULL);
20427c478bd9Sstevel@tonic-gate }
20437c478bd9Sstevel@tonic-gate 
2044e5ba14ffSstephh /* an ipath cache entry is an array of these, with s==NULL at the end */
2045e5ba14ffSstephh struct ipath {
2046e5ba14ffSstephh 	const char *s;	/* component name (in stable) */
2047e5ba14ffSstephh 	int i;		/* instance number */
2048e5ba14ffSstephh };
2049e5ba14ffSstephh 
2050e5ba14ffSstephh static nvlist_t *
ipath2fmri(struct ipath * ipath)2051e5ba14ffSstephh ipath2fmri(struct ipath *ipath)
2052e5ba14ffSstephh {
2053e5ba14ffSstephh 	nvlist_t **pa, *f, *p;
2054e5ba14ffSstephh 	uint_t depth = 0;
2055e5ba14ffSstephh 	char *numstr, *nullbyte;
2056e5ba14ffSstephh 	char *failure;
2057e5ba14ffSstephh 	int err, i;
2058e5ba14ffSstephh 	struct ipath *ipp;
2059e5ba14ffSstephh 
2060e5ba14ffSstephh 	for (ipp = ipath; ipp->s != NULL; ipp++)
2061e5ba14ffSstephh 		depth++;
2062e5ba14ffSstephh 
2063e5ba14ffSstephh 	if ((err = nvlist_xalloc(&f, NV_UNIQUE_NAME, &Eft_nv_hdl)) != 0)
2064e5ba14ffSstephh 		out(O_DIE|O_SYS, "alloc of fmri nvl failed");
2065e5ba14ffSstephh 	pa = alloca(depth * sizeof (nvlist_t *));
2066e5ba14ffSstephh 	for (i = 0; i < depth; i++)
2067e5ba14ffSstephh 		pa[i] = NULL;
2068e5ba14ffSstephh 
2069e5ba14ffSstephh 	err = nvlist_add_string(f, FM_FMRI_SCHEME, FM_FMRI_SCHEME_HC);
2070e5ba14ffSstephh 	err |= nvlist_add_uint8(f, FM_VERSION, FM_HC_SCHEME_VERSION);
2071e5ba14ffSstephh 	err |= nvlist_add_string(f, FM_FMRI_HC_ROOT, "");
2072e5ba14ffSstephh 	err |= nvlist_add_uint32(f, FM_FMRI_HC_LIST_SZ, depth);
2073e5ba14ffSstephh 	if (err != 0) {
2074e5ba14ffSstephh 		failure = "basic construction of FMRI failed";
2075e5ba14ffSstephh 		goto boom;
2076e5ba14ffSstephh 	}
2077e5ba14ffSstephh 
2078e5ba14ffSstephh 	numbuf[MAXDIGITIDX] = '\0';
2079e5ba14ffSstephh 	nullbyte = &numbuf[MAXDIGITIDX];
2080e5ba14ffSstephh 	i = 0;
2081e5ba14ffSstephh 
2082e5ba14ffSstephh 	for (ipp = ipath; ipp->s != NULL; ipp++) {
2083e5ba14ffSstephh 		err = nvlist_xalloc(&p, NV_UNIQUE_NAME, &Eft_nv_hdl);
2084e5ba14ffSstephh 		if (err != 0) {
2085e5ba14ffSstephh 			failure = "alloc of an hc-pair failed";
2086e5ba14ffSstephh 			goto boom;
2087e5ba14ffSstephh 		}
2088e5ba14ffSstephh 		err = nvlist_add_string(p, FM_FMRI_HC_NAME, ipp->s);
2089e5ba14ffSstephh 		numstr = ulltostr(ipp->i, nullbyte);
2090e5ba14ffSstephh 		err |= nvlist_add_string(p, FM_FMRI_HC_ID, numstr);
2091e5ba14ffSstephh 		if (err != 0) {
2092e5ba14ffSstephh 			failure = "construction of an hc-pair failed";
2093e5ba14ffSstephh 			goto boom;
2094e5ba14ffSstephh 		}
2095e5ba14ffSstephh 		pa[i++] = p;
2096e5ba14ffSstephh 	}
2097e5ba14ffSstephh 
2098e5ba14ffSstephh 	err = nvlist_add_nvlist_array(f, FM_FMRI_HC_LIST, pa, depth);
2099e5ba14ffSstephh 	if (err == 0) {
2100e5ba14ffSstephh 		for (i = 0; i < depth; i++)
2101e5ba14ffSstephh 			nvlist_free(pa[i]);
2102e5ba14ffSstephh 		return (f);
2103e5ba14ffSstephh 	}
2104e5ba14ffSstephh 	failure = "addition of hc-pair array to FMRI failed";
2105e5ba14ffSstephh 
2106e5ba14ffSstephh boom:
2107e5ba14ffSstephh 	for (i = 0; i < depth; i++)
2108e5ba14ffSstephh 		nvlist_free(pa[i]);
2109e5ba14ffSstephh 	nvlist_free(f);
2110e5ba14ffSstephh 	out(O_DIE, "%s", failure);
2111e5ba14ffSstephh 	/*NOTREACHED*/
2112e5ba14ffSstephh 	return (NULL);
2113e5ba14ffSstephh }
2114e5ba14ffSstephh 
21157c478bd9Sstevel@tonic-gate static uint8_t
percentof(uint_t part,uint_t whole)21167c478bd9Sstevel@tonic-gate percentof(uint_t part, uint_t whole)
21177c478bd9Sstevel@tonic-gate {
21187c478bd9Sstevel@tonic-gate 	unsigned long long p = part * 1000;
21197c478bd9Sstevel@tonic-gate 
21207c478bd9Sstevel@tonic-gate 	return ((p / whole / 10) + (((p / whole % 10) >= 5) ? 1 : 0));
21217c478bd9Sstevel@tonic-gate }
21227c478bd9Sstevel@tonic-gate 
212380ab886dSwesolows struct rsl {
21247c478bd9Sstevel@tonic-gate 	struct event *suspect;
21257c478bd9Sstevel@tonic-gate 	nvlist_t *asru;
21267c478bd9Sstevel@tonic-gate 	nvlist_t *fru;
21277c478bd9Sstevel@tonic-gate 	nvlist_t *rsrc;
21287c478bd9Sstevel@tonic-gate };
21297c478bd9Sstevel@tonic-gate 
2130b7d3956bSstephh static void publish_suspects(struct fme *fmep, struct rsl *srl);
2131b7d3956bSstephh 
21327c478bd9Sstevel@tonic-gate /*
21337c478bd9Sstevel@tonic-gate  *  rslfree -- free internal members of struct rsl not expected to be
21347c478bd9Sstevel@tonic-gate  *	freed elsewhere.
21357c478bd9Sstevel@tonic-gate  */
21367c478bd9Sstevel@tonic-gate static void
rslfree(struct rsl * freeme)21377c478bd9Sstevel@tonic-gate rslfree(struct rsl *freeme)
21387c478bd9Sstevel@tonic-gate {
21397c478bd9Sstevel@tonic-gate 	nvlist_free(freeme->asru);
21407c478bd9Sstevel@tonic-gate 	nvlist_free(freeme->fru);
2141*aab83bb8SJosef 'Jeff' Sipek 	if (freeme->rsrc != freeme->asru)
21427c478bd9Sstevel@tonic-gate 		nvlist_free(freeme->rsrc);
21437c478bd9Sstevel@tonic-gate }
21447c478bd9Sstevel@tonic-gate 
21457c478bd9Sstevel@tonic-gate /*
21467c478bd9Sstevel@tonic-gate  *  rslcmp -- compare two rsl structures.  Use the following
21477c478bd9Sstevel@tonic-gate  *	comparisons to establish cardinality:
21487c478bd9Sstevel@tonic-gate  *
21497c478bd9Sstevel@tonic-gate  *	1. Name of the suspect's class. (simple strcmp)
21507c478bd9Sstevel@tonic-gate  *	2. Name of the suspect's ASRU. (trickier, since nvlist)
21517c478bd9Sstevel@tonic-gate  *
21527c478bd9Sstevel@tonic-gate  */
21537c478bd9Sstevel@tonic-gate static int
rslcmp(const void * a,const void * b)21547c478bd9Sstevel@tonic-gate rslcmp(const void *a, const void *b)
21557c478bd9Sstevel@tonic-gate {
21567c478bd9Sstevel@tonic-gate 	struct rsl *r1 = (struct rsl *)a;
21577c478bd9Sstevel@tonic-gate 	struct rsl *r2 = (struct rsl *)b;
21587c478bd9Sstevel@tonic-gate 	int rv;
21597c478bd9Sstevel@tonic-gate 
21607c478bd9Sstevel@tonic-gate 	rv = strcmp(r1->suspect->enode->u.event.ename->u.name.s,
21617c478bd9Sstevel@tonic-gate 	    r2->suspect->enode->u.event.ename->u.name.s);
21627c478bd9Sstevel@tonic-gate 	if (rv != 0)
21637c478bd9Sstevel@tonic-gate 		return (rv);
21647c478bd9Sstevel@tonic-gate 
2165b7d3956bSstephh 	if (r1->rsrc == NULL && r2->rsrc == NULL)
21667c478bd9Sstevel@tonic-gate 		return (0);
2167b7d3956bSstephh 	if (r1->rsrc == NULL)
21687c478bd9Sstevel@tonic-gate 		return (-1);
2169b7d3956bSstephh 	if (r2->rsrc == NULL)
21707c478bd9Sstevel@tonic-gate 		return (1);
2171b7d3956bSstephh 	return (evnv_cmpnvl(r1->rsrc, r2->rsrc, 0));
21727c478bd9Sstevel@tonic-gate }
21737c478bd9Sstevel@tonic-gate 
21747c478bd9Sstevel@tonic-gate /*
21757c478bd9Sstevel@tonic-gate  * get_resources -- for a given suspect, determine what ASRU, FRU and
21767c478bd9Sstevel@tonic-gate  *     RSRC nvlists should be advertised in the final suspect list.
21777c478bd9Sstevel@tonic-gate  */
21787c478bd9Sstevel@tonic-gate void
get_resources(struct event * sp,struct rsl * rsrcs,struct config * croot)21797c478bd9Sstevel@tonic-gate get_resources(struct event *sp, struct rsl *rsrcs, struct config *croot)
21807c478bd9Sstevel@tonic-gate {
21817c478bd9Sstevel@tonic-gate 	struct node *asrudef, *frudef;
21827c478bd9Sstevel@tonic-gate 	nvlist_t *asru, *fru;
21837c478bd9Sstevel@tonic-gate 	nvlist_t *rsrc = NULL;
21847c478bd9Sstevel@tonic-gate 	char *pathstr;
21857c478bd9Sstevel@tonic-gate 
21867c478bd9Sstevel@tonic-gate 	/*
21877c478bd9Sstevel@tonic-gate 	 * First find any ASRU and/or FRU defined in the
21887c478bd9Sstevel@tonic-gate 	 * initial fault tree.
21897c478bd9Sstevel@tonic-gate 	 */
21907c478bd9Sstevel@tonic-gate 	asrudef = eventprop_lookup(sp, L_ASRU);
21917c478bd9Sstevel@tonic-gate 	frudef = eventprop_lookup(sp, L_FRU);
21927c478bd9Sstevel@tonic-gate 
21937c478bd9Sstevel@tonic-gate 	/*
21947c478bd9Sstevel@tonic-gate 	 * Create FMRIs based on those definitions
21957c478bd9Sstevel@tonic-gate 	 */
21967c478bd9Sstevel@tonic-gate 	asru = node2fmri(asrudef);
21977c478bd9Sstevel@tonic-gate 	fru = node2fmri(frudef);
21987c478bd9Sstevel@tonic-gate 	pathstr = ipath2str(NULL, sp->ipp);
21997c478bd9Sstevel@tonic-gate 
22007c478bd9Sstevel@tonic-gate 	/*
22017c478bd9Sstevel@tonic-gate 	 *  Allow for platform translations of the FMRIs
22027c478bd9Sstevel@tonic-gate 	 */
22037c478bd9Sstevel@tonic-gate 	platform_units_translate(is_defect(sp->t), croot, &asru, &fru, &rsrc,
22047c478bd9Sstevel@tonic-gate 	    pathstr);
22057c478bd9Sstevel@tonic-gate 
22067c478bd9Sstevel@tonic-gate 	FREE(pathstr);
22077c478bd9Sstevel@tonic-gate 	rsrcs->suspect = sp;
22087c478bd9Sstevel@tonic-gate 	rsrcs->asru = asru;
22097c478bd9Sstevel@tonic-gate 	rsrcs->fru = fru;
22107c478bd9Sstevel@tonic-gate 	rsrcs->rsrc = rsrc;
22117c478bd9Sstevel@tonic-gate }
22127c478bd9Sstevel@tonic-gate 
22137c478bd9Sstevel@tonic-gate /*
22147c478bd9Sstevel@tonic-gate  * trim_suspects -- prior to publishing, we may need to remove some
22157c478bd9Sstevel@tonic-gate  *    suspects from the list.  If we're auto-closing upsets, we don't
22167c478bd9Sstevel@tonic-gate  *    want any of those in the published list.  If the ASRUs for multiple
22177c478bd9Sstevel@tonic-gate  *    defects resolve to the same ASRU (driver) we only want to publish
22187c478bd9Sstevel@tonic-gate  *    that as a single suspect.
22197c478bd9Sstevel@tonic-gate  */
2220b7d3956bSstephh static int
trim_suspects(struct fme * fmep,struct rsl * begin,struct rsl * begin2,fmd_event_t * ffep)2221b7d3956bSstephh trim_suspects(struct fme *fmep, struct rsl *begin, struct rsl *begin2,
22228e7248e5SStephen Hanson     fmd_event_t *ffep)
22237c478bd9Sstevel@tonic-gate {
22247c478bd9Sstevel@tonic-gate 	struct event *ep;
2225b7d3956bSstephh 	struct rsl *rp = begin;
2226b7d3956bSstephh 	struct rsl *rp2 = begin2;
2227b7d3956bSstephh 	int mess_zero_count = 0;
2228b7d3956bSstephh 	int serd_rval;
2229b7d3956bSstephh 	uint_t messval;
22307c478bd9Sstevel@tonic-gate 
2231b7d3956bSstephh 	/* remove any unwanted upsets and populate our array */
22327c478bd9Sstevel@tonic-gate 	for (ep = fmep->psuspects; ep; ep = ep->psuspects) {
2233b7d3956bSstephh 		if (is_upset(ep->t))
22347c478bd9Sstevel@tonic-gate 			continue;
2235b7d3956bSstephh 		serd_rval = serd_eval(fmep, fmep->hdl, ffep, fmep->fmcase, ep,
2236b7d3956bSstephh 		    NULL, NULL);
2237b7d3956bSstephh 		if (serd_rval == 0)
2238b7d3956bSstephh 			continue;
2239b7d3956bSstephh 		if (node2uint(eventprop_lookup(ep, L_message),
2240b7d3956bSstephh 		    &messval) == 0 && messval == 0) {
2241b7d3956bSstephh 			get_resources(ep, rp2, fmep->config);
2242b7d3956bSstephh 			rp2++;
2243b7d3956bSstephh 			mess_zero_count++;
2244b7d3956bSstephh 		} else {
2245e5ba14ffSstephh 			get_resources(ep, rp, fmep->config);
22467c478bd9Sstevel@tonic-gate 			rp++;
22477c478bd9Sstevel@tonic-gate 			fmep->nsuspects++;
22487c478bd9Sstevel@tonic-gate 		}
2249b7d3956bSstephh 	}
2250b7d3956bSstephh 	return (mess_zero_count);
22517c478bd9Sstevel@tonic-gate }
22527c478bd9Sstevel@tonic-gate 
22537aec1d6eScindi /*
22547aec1d6eScindi  * addpayloadprop -- add a payload prop to a problem
22557aec1d6eScindi  */
22567aec1d6eScindi static void
addpayloadprop(const char * lhs,struct evalue * rhs,nvlist_t * fault)22577aec1d6eScindi addpayloadprop(const char *lhs, struct evalue *rhs, nvlist_t *fault)
22587aec1d6eScindi {
2259b7d3956bSstephh 	nvlist_t *rsrc, *hcs;
2260b7d3956bSstephh 
22617aec1d6eScindi 	ASSERT(fault != NULL);
22627aec1d6eScindi 	ASSERT(lhs != NULL);
22637aec1d6eScindi 	ASSERT(rhs != NULL);
22647aec1d6eScindi 
2265b7d3956bSstephh 	if (nvlist_lookup_nvlist(fault, FM_FAULT_RESOURCE, &rsrc) != 0)
2266b7d3956bSstephh 		out(O_DIE, "cannot add payloadprop \"%s\" to fault", lhs);
2267b7d3956bSstephh 
2268b7d3956bSstephh 	if (nvlist_lookup_nvlist(rsrc, FM_FMRI_HC_SPECIFIC, &hcs) != 0) {
2269b7d3956bSstephh 		out(O_ALTFP|O_VERB2, "addpayloadprop: create hc_specific");
2270b7d3956bSstephh 		if (nvlist_xalloc(&hcs, NV_UNIQUE_NAME, &Eft_nv_hdl) != 0)
2271b7d3956bSstephh 			out(O_DIE,
2272b7d3956bSstephh 			    "cannot add payloadprop \"%s\" to fault", lhs);
2273b7d3956bSstephh 		if (nvlist_add_nvlist(rsrc, FM_FMRI_HC_SPECIFIC, hcs) != 0)
2274b7d3956bSstephh 			out(O_DIE,
2275b7d3956bSstephh 			    "cannot add payloadprop \"%s\" to fault", lhs);
2276b7d3956bSstephh 		nvlist_free(hcs);
2277b7d3956bSstephh 		if (nvlist_lookup_nvlist(rsrc, FM_FMRI_HC_SPECIFIC, &hcs) != 0)
2278b7d3956bSstephh 			out(O_DIE,
2279b7d3956bSstephh 			    "cannot add payloadprop \"%s\" to fault", lhs);
2280b7d3956bSstephh 	} else
2281b7d3956bSstephh 		out(O_ALTFP|O_VERB2, "addpayloadprop: reuse hc_specific");
2282b7d3956bSstephh 
22837aec1d6eScindi 	if (rhs->t == UINT64) {
22847aec1d6eScindi 		out(O_ALTFP|O_VERB2, "addpayloadprop: %s=%llu", lhs, rhs->v);
22857aec1d6eScindi 
2286b7d3956bSstephh 		if (nvlist_add_uint64(hcs, lhs, rhs->v) != 0)
22877aec1d6eScindi 			out(O_DIE,
22887aec1d6eScindi 			    "cannot add payloadprop \"%s\" to fault", lhs);
22897aec1d6eScindi 	} else {
22907aec1d6eScindi 		out(O_ALTFP|O_VERB2, "addpayloadprop: %s=\"%s\"",
229180ab886dSwesolows 		    lhs, (char *)(uintptr_t)rhs->v);
22927aec1d6eScindi 
2293b7d3956bSstephh 		if (nvlist_add_string(hcs, lhs, (char *)(uintptr_t)rhs->v) != 0)
22947aec1d6eScindi 			out(O_DIE,
22957aec1d6eScindi 			    "cannot add payloadprop \"%s\" to fault", lhs);
22967aec1d6eScindi 	}
22977aec1d6eScindi }
22987aec1d6eScindi 
22997aec1d6eScindi static char *Istatbuf;
23007aec1d6eScindi static char *Istatbufptr;
23017aec1d6eScindi static int Istatsz;
23027aec1d6eScindi 
23037aec1d6eScindi /*
23047aec1d6eScindi  * istataddsize -- calculate size of istat and add it to Istatsz
23057aec1d6eScindi  */
23067aec1d6eScindi /*ARGSUSED2*/
23077aec1d6eScindi static void
istataddsize(const struct istat_entry * lhs,struct stats * rhs,void * arg)23087aec1d6eScindi istataddsize(const struct istat_entry *lhs, struct stats *rhs, void *arg)
23097aec1d6eScindi {
23107aec1d6eScindi 	int val;
23117aec1d6eScindi 
23127aec1d6eScindi 	ASSERT(lhs != NULL);
23137aec1d6eScindi 	ASSERT(rhs != NULL);
23147aec1d6eScindi 
23157aec1d6eScindi 	if ((val = stats_counter_value(rhs)) == 0)
23167aec1d6eScindi 		return;	/* skip zero-valued stats */
23177aec1d6eScindi 
23187aec1d6eScindi 	/* count up the size of the stat name */
23197aec1d6eScindi 	Istatsz += ipath2strlen(lhs->ename, lhs->ipath);
23207aec1d6eScindi 	Istatsz++;	/* for the trailing NULL byte */
23217aec1d6eScindi 
23227aec1d6eScindi 	/* count up the size of the stat value */
23237aec1d6eScindi 	Istatsz += snprintf(NULL, 0, "%d", val);
23247aec1d6eScindi 	Istatsz++;	/* for the trailing NULL byte */
23257aec1d6eScindi }
23267aec1d6eScindi 
23277aec1d6eScindi /*
23287aec1d6eScindi  * istat2str -- serialize an istat, writing result to *Istatbufptr
23297aec1d6eScindi  */
23307aec1d6eScindi /*ARGSUSED2*/
23317aec1d6eScindi static void
istat2str(const struct istat_entry * lhs,struct stats * rhs,void * arg)23327aec1d6eScindi istat2str(const struct istat_entry *lhs, struct stats *rhs, void *arg)
23337aec1d6eScindi {
23347aec1d6eScindi 	char *str;
23357aec1d6eScindi 	int len;
23367aec1d6eScindi 	int val;
23377aec1d6eScindi 
23387aec1d6eScindi 	ASSERT(lhs != NULL);
23397aec1d6eScindi 	ASSERT(rhs != NULL);
23407aec1d6eScindi 
23417aec1d6eScindi 	if ((val = stats_counter_value(rhs)) == 0)
23427aec1d6eScindi 		return;	/* skip zero-valued stats */
23437aec1d6eScindi 
23447aec1d6eScindi 	/* serialize the stat name */
23457aec1d6eScindi 	str = ipath2str(lhs->ename, lhs->ipath);
23467aec1d6eScindi 	len = strlen(str);
23477aec1d6eScindi 
23487aec1d6eScindi 	ASSERT(Istatbufptr + len + 1 < &Istatbuf[Istatsz]);
23497aec1d6eScindi 	(void) strlcpy(Istatbufptr, str, &Istatbuf[Istatsz] - Istatbufptr);
23507aec1d6eScindi 	Istatbufptr += len;
23517aec1d6eScindi 	FREE(str);
23527aec1d6eScindi 	*Istatbufptr++ = '\0';
23537aec1d6eScindi 
23547aec1d6eScindi 	/* serialize the stat value */
23557aec1d6eScindi 	Istatbufptr += snprintf(Istatbufptr, &Istatbuf[Istatsz] - Istatbufptr,
23567aec1d6eScindi 	    "%d", val);
23577aec1d6eScindi 	*Istatbufptr++ = '\0';
23587aec1d6eScindi 
23597aec1d6eScindi 	ASSERT(Istatbufptr <= &Istatbuf[Istatsz]);
23607aec1d6eScindi }
23617aec1d6eScindi 
23627aec1d6eScindi void
istat_save()23637aec1d6eScindi istat_save()
23647aec1d6eScindi {
23657aec1d6eScindi 	if (Istat_need_save == 0)
23667aec1d6eScindi 		return;
23677aec1d6eScindi 
23687aec1d6eScindi 	/* figure out how big the serialzed info is */
23697aec1d6eScindi 	Istatsz = 0;
23707aec1d6eScindi 	lut_walk(Istats, (lut_cb)istataddsize, NULL);
23717aec1d6eScindi 
23727aec1d6eScindi 	if (Istatsz == 0) {
23737aec1d6eScindi 		/* no stats to save */
23747aec1d6eScindi 		fmd_buf_destroy(Hdl, NULL, WOBUF_ISTATS);
23757aec1d6eScindi 		return;
23767aec1d6eScindi 	}
23777aec1d6eScindi 
23787aec1d6eScindi 	/* create the serialized buffer */
23797aec1d6eScindi 	Istatbufptr = Istatbuf = MALLOC(Istatsz);
23807aec1d6eScindi 	lut_walk(Istats, (lut_cb)istat2str, NULL);
23817aec1d6eScindi 
23827aec1d6eScindi 	/* clear out current saved stats */
23837aec1d6eScindi 	fmd_buf_destroy(Hdl, NULL, WOBUF_ISTATS);
23847aec1d6eScindi 
23857aec1d6eScindi 	/* write out the new version */
23867aec1d6eScindi 	fmd_buf_write(Hdl, NULL, WOBUF_ISTATS, Istatbuf, Istatsz);
23877aec1d6eScindi 	FREE(Istatbuf);
23887aec1d6eScindi 
23897aec1d6eScindi 	Istat_need_save = 0;
23907aec1d6eScindi }
23917aec1d6eScindi 
23927aec1d6eScindi int
istat_cmp(struct istat_entry * ent1,struct istat_entry * ent2)23937aec1d6eScindi istat_cmp(struct istat_entry *ent1, struct istat_entry *ent2)
23947aec1d6eScindi {
23957aec1d6eScindi 	if (ent1->ename != ent2->ename)
23967aec1d6eScindi 		return (ent2->ename - ent1->ename);
23977aec1d6eScindi 	if (ent1->ipath != ent2->ipath)
23987aec1d6eScindi 		return ((char *)ent2->ipath - (char *)ent1->ipath);
23997aec1d6eScindi 
24007aec1d6eScindi 	return (0);
24017aec1d6eScindi }
24027aec1d6eScindi 
24037aec1d6eScindi /*
24047aec1d6eScindi  * istat-verify -- verify the component associated with a stat still exists
24057aec1d6eScindi  *
24067aec1d6eScindi  * if the component no longer exists, this routine resets the stat and
24077aec1d6eScindi  * returns 0.  if the component still exists, it returns 1.
24087aec1d6eScindi  */
24097aec1d6eScindi static int
istat_verify(struct node * snp,struct istat_entry * entp)24107aec1d6eScindi istat_verify(struct node *snp, struct istat_entry *entp)
24117aec1d6eScindi {
24127aec1d6eScindi 	struct stats *statp;
24137aec1d6eScindi 	nvlist_t *fmri;
24147aec1d6eScindi 
24157aec1d6eScindi 	fmri = node2fmri(snp->u.event.epname);
24167aec1d6eScindi 	if (platform_path_exists(fmri)) {
24177aec1d6eScindi 		nvlist_free(fmri);
24187aec1d6eScindi 		return (1);
24197aec1d6eScindi 	}
24207aec1d6eScindi 	nvlist_free(fmri);
24217aec1d6eScindi 
24227aec1d6eScindi 	/* component no longer in system.  zero out the associated stats */
24237aec1d6eScindi 	if ((statp = (struct stats *)
24247aec1d6eScindi 	    lut_lookup(Istats, entp, (lut_cmp)istat_cmp)) == NULL ||
24257aec1d6eScindi 	    stats_counter_value(statp) == 0)
24267aec1d6eScindi 		return (0);	/* stat is already reset */
24277aec1d6eScindi 
24287aec1d6eScindi 	Istat_need_save = 1;
24297aec1d6eScindi 	stats_counter_reset(statp);
24307aec1d6eScindi 	return (0);
24317aec1d6eScindi }
24327aec1d6eScindi 
24337aec1d6eScindi static void
istat_bump(struct node * snp,int n)24347aec1d6eScindi istat_bump(struct node *snp, int n)
24357aec1d6eScindi {
24367aec1d6eScindi 	struct stats *statp;
24377aec1d6eScindi 	struct istat_entry ent;
24387aec1d6eScindi 
24397aec1d6eScindi 	ASSERT(snp != NULL);
24407aec1d6eScindi 	ASSERTinfo(snp->t == T_EVENT, ptree_nodetype2str(snp->t));
24417aec1d6eScindi 	ASSERT(snp->u.event.epname != NULL);
24427aec1d6eScindi 
24437aec1d6eScindi 	/* class name should be hoisted into a single stable entry */
24447aec1d6eScindi 	ASSERT(snp->u.event.ename->u.name.next == NULL);
24457aec1d6eScindi 	ent.ename = snp->u.event.ename->u.name.s;
24467aec1d6eScindi 	ent.ipath = ipath(snp->u.event.epname);
24477aec1d6eScindi 
24487aec1d6eScindi 	if (!istat_verify(snp, &ent)) {
24497aec1d6eScindi 		/* component no longer exists in system, nothing to do */
24507aec1d6eScindi 		return;
24517aec1d6eScindi 	}
24527aec1d6eScindi 
24537aec1d6eScindi 	if ((statp = (struct stats *)
24547aec1d6eScindi 	    lut_lookup(Istats, &ent, (lut_cmp)istat_cmp)) == NULL) {
24557aec1d6eScindi 		/* need to create the counter */
24567aec1d6eScindi 		int cnt = 0;
24577aec1d6eScindi 		struct node *np;
24587aec1d6eScindi 		char *sname;
24597aec1d6eScindi 		char *snamep;
24607aec1d6eScindi 		struct istat_entry *newentp;
24617aec1d6eScindi 
24627aec1d6eScindi 		/* count up the size of the stat name */
24637aec1d6eScindi 		np = snp->u.event.ename;
24647aec1d6eScindi 		while (np != NULL) {
24657aec1d6eScindi 			cnt += strlen(np->u.name.s);
24667aec1d6eScindi 			cnt++;	/* for the '.' or '@' */
24677aec1d6eScindi 			np = np->u.name.next;
24687aec1d6eScindi 		}
24697aec1d6eScindi 		np = snp->u.event.epname;
24707aec1d6eScindi 		while (np != NULL) {
24717aec1d6eScindi 			cnt += snprintf(NULL, 0, "%s%llu",
24727aec1d6eScindi 			    np->u.name.s, np->u.name.child->u.ull);
24737aec1d6eScindi 			cnt++;	/* for the '/' or trailing NULL byte */
24747aec1d6eScindi 			np = np->u.name.next;
24757aec1d6eScindi 		}
24767aec1d6eScindi 
24777aec1d6eScindi 		/* build the stat name */
24787aec1d6eScindi 		snamep = sname = alloca(cnt);
24797aec1d6eScindi 		np = snp->u.event.ename;
24807aec1d6eScindi 		while (np != NULL) {
24817aec1d6eScindi 			snamep += snprintf(snamep, &sname[cnt] - snamep,
24827aec1d6eScindi 			    "%s", np->u.name.s);
24837aec1d6eScindi 			np = np->u.name.next;
24847aec1d6eScindi 			if (np)
24857aec1d6eScindi 				*snamep++ = '.';
24867aec1d6eScindi 		}
24877aec1d6eScindi 		*snamep++ = '@';
24887aec1d6eScindi 		np = snp->u.event.epname;
24897aec1d6eScindi 		while (np != NULL) {
24907aec1d6eScindi 			snamep += snprintf(snamep, &sname[cnt] - snamep,
24917aec1d6eScindi 			    "%s%llu", np->u.name.s, np->u.name.child->u.ull);
24927aec1d6eScindi 			np = np->u.name.next;
24937aec1d6eScindi 			if (np)
24947aec1d6eScindi 				*snamep++ = '/';
24957aec1d6eScindi 		}
24967aec1d6eScindi 		*snamep++ = '\0';
24977aec1d6eScindi 
24987aec1d6eScindi 		/* create the new stat & add it to our list */
24997aec1d6eScindi 		newentp = MALLOC(sizeof (*newentp));
25007aec1d6eScindi 		*newentp = ent;
25017aec1d6eScindi 		statp = stats_new_counter(NULL, sname, 0);
25027aec1d6eScindi 		Istats = lut_add(Istats, (void *)newentp, (void *)statp,
25037aec1d6eScindi 		    (lut_cmp)istat_cmp);
25047aec1d6eScindi 	}
25057aec1d6eScindi 
25067aec1d6eScindi 	/* if n is non-zero, set that value instead of bumping */
25077aec1d6eScindi 	if (n) {
25087aec1d6eScindi 		stats_counter_reset(statp);
25097aec1d6eScindi 		stats_counter_add(statp, n);
25107aec1d6eScindi 	} else
25117aec1d6eScindi 		stats_counter_bump(statp);
25127aec1d6eScindi 	Istat_need_save = 1;
25138a40a695Sgavinm 
25148a40a695Sgavinm 	ipath_print(O_ALTFP|O_VERB2, ent.ename, ent.ipath);
25158a40a695Sgavinm 	out(O_ALTFP|O_VERB2, " %s to value %d", n ? "set" : "incremented",
25168a40a695Sgavinm 	    stats_counter_value(statp));
25177aec1d6eScindi }
25187aec1d6eScindi 
25197aec1d6eScindi /*ARGSUSED*/
25207aec1d6eScindi static void
istat_destructor(void * left,void * right,void * arg)25217aec1d6eScindi istat_destructor(void *left, void *right, void *arg)
25227aec1d6eScindi {
25237aec1d6eScindi 	struct istat_entry *entp = (struct istat_entry *)left;
25247aec1d6eScindi 	struct stats *statp = (struct stats *)right;
25257aec1d6eScindi 	FREE(entp);
25267aec1d6eScindi 	stats_delete(statp);
25277aec1d6eScindi }
25287aec1d6eScindi 
252908f6c065Sgavinm /*
253008f6c065Sgavinm  * Callback used in a walk of the Istats to reset matching stat counters.
253108f6c065Sgavinm  */
253208f6c065Sgavinm static void
istat_counter_reset_cb(struct istat_entry * entp,struct stats * statp,const struct ipath * ipp)253308f6c065Sgavinm istat_counter_reset_cb(struct istat_entry *entp, struct stats *statp,
253408f6c065Sgavinm     const struct ipath *ipp)
253508f6c065Sgavinm {
253608f6c065Sgavinm 	char *path;
253708f6c065Sgavinm 
253808f6c065Sgavinm 	if (entp->ipath == ipp) {
253908f6c065Sgavinm 		path = ipath2str(entp->ename, ipp);
254008f6c065Sgavinm 		out(O_ALTFP, "istat_counter_reset_cb: resetting %s", path);
254108f6c065Sgavinm 		FREE(path);
254208f6c065Sgavinm 		stats_counter_reset(statp);
254308f6c065Sgavinm 		Istat_need_save = 1;
254408f6c065Sgavinm 	}
254508f6c065Sgavinm }
254608f6c065Sgavinm 
2547e5ba14ffSstephh /*ARGSUSED*/
2548e5ba14ffSstephh static void
istat_counter_topo_chg_cb(struct istat_entry * entp,struct stats * statp,void * unused)2549e5ba14ffSstephh istat_counter_topo_chg_cb(struct istat_entry *entp, struct stats *statp,
2550e5ba14ffSstephh     void *unused)
2551e5ba14ffSstephh {
2552e5ba14ffSstephh 	char *path;
2553e5ba14ffSstephh 	nvlist_t *fmri;
2554e5ba14ffSstephh 
2555e5ba14ffSstephh 	fmri = ipath2fmri((struct ipath *)(entp->ipath));
2556e5ba14ffSstephh 	if (!platform_path_exists(fmri)) {
2557e5ba14ffSstephh 		path = ipath2str(entp->ename, entp->ipath);
2558e5ba14ffSstephh 		out(O_ALTFP, "istat_counter_topo_chg_cb: not present %s", path);
2559e5ba14ffSstephh 		FREE(path);
2560e5ba14ffSstephh 		stats_counter_reset(statp);
2561e5ba14ffSstephh 		Istat_need_save = 1;
2562e5ba14ffSstephh 	}
2563e5ba14ffSstephh 	nvlist_free(fmri);
2564e5ba14ffSstephh }
2565e5ba14ffSstephh 
25667aec1d6eScindi void
istat_fini(void)25677aec1d6eScindi istat_fini(void)
25687aec1d6eScindi {
25697aec1d6eScindi 	lut_free(Istats, istat_destructor, NULL);
25707aec1d6eScindi }
25717aec1d6eScindi 
2572b5016cbbSstephh static char *Serdbuf;
2573b5016cbbSstephh static char *Serdbufptr;
2574b5016cbbSstephh static int Serdsz;
2575b5016cbbSstephh 
2576b5016cbbSstephh /*
2577b5016cbbSstephh  * serdaddsize -- calculate size of serd and add it to Serdsz
2578b5016cbbSstephh  */
2579b5016cbbSstephh /*ARGSUSED*/
2580b5016cbbSstephh static void
serdaddsize(const struct serd_entry * lhs,struct stats * rhs,void * arg)2581b5016cbbSstephh serdaddsize(const struct serd_entry *lhs, struct stats *rhs, void *arg)
2582b5016cbbSstephh {
2583b5016cbbSstephh 	ASSERT(lhs != NULL);
2584b5016cbbSstephh 
2585b5016cbbSstephh 	/* count up the size of the stat name */
2586b5016cbbSstephh 	Serdsz += ipath2strlen(lhs->ename, lhs->ipath);
2587b5016cbbSstephh 	Serdsz++;	/* for the trailing NULL byte */
2588b5016cbbSstephh }
2589b5016cbbSstephh 
2590b5016cbbSstephh /*
2591b5016cbbSstephh  * serd2str -- serialize a serd engine, writing result to *Serdbufptr
2592b5016cbbSstephh  */
2593b5016cbbSstephh /*ARGSUSED*/
2594b5016cbbSstephh static void
serd2str(const struct serd_entry * lhs,struct stats * rhs,void * arg)2595b5016cbbSstephh serd2str(const struct serd_entry *lhs, struct stats *rhs, void *arg)
2596b5016cbbSstephh {
2597b5016cbbSstephh 	char *str;
2598b5016cbbSstephh 	int len;
2599b5016cbbSstephh 
2600b5016cbbSstephh 	ASSERT(lhs != NULL);
2601b5016cbbSstephh 
2602b5016cbbSstephh 	/* serialize the serd engine name */
2603b5016cbbSstephh 	str = ipath2str(lhs->ename, lhs->ipath);
2604b5016cbbSstephh 	len = strlen(str);
2605b5016cbbSstephh 
2606b5016cbbSstephh 	ASSERT(Serdbufptr + len + 1 <= &Serdbuf[Serdsz]);
2607b5016cbbSstephh 	(void) strlcpy(Serdbufptr, str, &Serdbuf[Serdsz] - Serdbufptr);
2608b5016cbbSstephh 	Serdbufptr += len;
2609b5016cbbSstephh 	FREE(str);
2610b5016cbbSstephh 	*Serdbufptr++ = '\0';
2611b5016cbbSstephh 	ASSERT(Serdbufptr <= &Serdbuf[Serdsz]);
2612b5016cbbSstephh }
2613b5016cbbSstephh 
2614b5016cbbSstephh void
serd_save()2615b5016cbbSstephh serd_save()
2616b5016cbbSstephh {
2617b5016cbbSstephh 	if (Serd_need_save == 0)
2618b5016cbbSstephh 		return;
2619b5016cbbSstephh 
2620b5016cbbSstephh 	/* figure out how big the serialzed info is */
2621b5016cbbSstephh 	Serdsz = 0;
2622b5016cbbSstephh 	lut_walk(SerdEngines, (lut_cb)serdaddsize, NULL);
2623b5016cbbSstephh 
2624b5016cbbSstephh 	if (Serdsz == 0) {
2625b5016cbbSstephh 		/* no serd engines to save */
2626b5016cbbSstephh 		fmd_buf_destroy(Hdl, NULL, WOBUF_SERDS);
2627b5016cbbSstephh 		return;
2628b5016cbbSstephh 	}
2629b5016cbbSstephh 
2630b5016cbbSstephh 	/* create the serialized buffer */
2631b5016cbbSstephh 	Serdbufptr = Serdbuf = MALLOC(Serdsz);
2632b5016cbbSstephh 	lut_walk(SerdEngines, (lut_cb)serd2str, NULL);
2633b5016cbbSstephh 
2634b5016cbbSstephh 	/* clear out current saved stats */
2635b5016cbbSstephh 	fmd_buf_destroy(Hdl, NULL, WOBUF_SERDS);
2636b5016cbbSstephh 
2637b5016cbbSstephh 	/* write out the new version */
2638b5016cbbSstephh 	fmd_buf_write(Hdl, NULL, WOBUF_SERDS, Serdbuf, Serdsz);
2639b5016cbbSstephh 	FREE(Serdbuf);
2640b5016cbbSstephh 	Serd_need_save = 0;
2641b5016cbbSstephh }
2642b5016cbbSstephh 
2643b5016cbbSstephh int
serd_cmp(struct serd_entry * ent1,struct serd_entry * ent2)2644b5016cbbSstephh serd_cmp(struct serd_entry *ent1, struct serd_entry *ent2)
2645b5016cbbSstephh {
2646b5016cbbSstephh 	if (ent1->ename != ent2->ename)
2647b5016cbbSstephh 		return (ent2->ename - ent1->ename);
2648b5016cbbSstephh 	if (ent1->ipath != ent2->ipath)
2649b5016cbbSstephh 		return ((char *)ent2->ipath - (char *)ent1->ipath);
2650b5016cbbSstephh 
2651b5016cbbSstephh 	return (0);
2652b5016cbbSstephh }
2653b5016cbbSstephh 
2654b5016cbbSstephh void
fme_serd_load(fmd_hdl_t * hdl)2655b5016cbbSstephh fme_serd_load(fmd_hdl_t *hdl)
2656b5016cbbSstephh {
2657b5016cbbSstephh 	int sz;
2658b5016cbbSstephh 	char *sbuf;
2659b5016cbbSstephh 	char *sepptr;
2660b5016cbbSstephh 	char *ptr;
2661b5016cbbSstephh 	struct serd_entry *newentp;
2662b5016cbbSstephh 	struct node *epname;
2663b5016cbbSstephh 	nvlist_t *fmri;
2664b5016cbbSstephh 	char *namestring;
2665b5016cbbSstephh 
2666b5016cbbSstephh 	if ((sz = fmd_buf_size(hdl, NULL, WOBUF_SERDS)) == 0)
2667b5016cbbSstephh 		return;
2668b5016cbbSstephh 	sbuf = alloca(sz);
2669b5016cbbSstephh 	fmd_buf_read(hdl, NULL, WOBUF_SERDS, sbuf, sz);
2670b5016cbbSstephh 	ptr = sbuf;
2671b5016cbbSstephh 	while (ptr < &sbuf[sz]) {
2672b5016cbbSstephh 		sepptr = strchr(ptr, '@');
2673b5016cbbSstephh 		*sepptr = '\0';
2674b5016cbbSstephh 		namestring = ptr;
2675b5016cbbSstephh 		sepptr++;
2676b5016cbbSstephh 		ptr = sepptr;
2677b5016cbbSstephh 		ptr += strlen(ptr);
2678b5016cbbSstephh 		ptr++;	/* move past the '\0' separating paths */
2679b5016cbbSstephh 		epname = pathstring2epnamenp(sepptr);
2680b5016cbbSstephh 		fmri = node2fmri(epname);
2681b5016cbbSstephh 		if (platform_path_exists(fmri)) {
2682b5016cbbSstephh 			newentp = MALLOC(sizeof (*newentp));
2683b5016cbbSstephh 			newentp->hdl = hdl;
2684b5016cbbSstephh 			newentp->ipath = ipath(epname);
2685b5016cbbSstephh 			newentp->ename = stable(namestring);
2686b5016cbbSstephh 			SerdEngines = lut_add(SerdEngines, (void *)newentp,
2687e5ba14ffSstephh 			    (void *)newentp, (lut_cmp)serd_cmp);
2688b5016cbbSstephh 		} else
2689b5016cbbSstephh 			Serd_need_save = 1;
2690e5ba14ffSstephh 		tree_free(epname);
2691b5016cbbSstephh 		nvlist_free(fmri);
2692b5016cbbSstephh 	}
2693b5016cbbSstephh 	/* save it back again in case some of the paths no longer exist */
2694b5016cbbSstephh 	serd_save();
2695b5016cbbSstephh }
2696b5016cbbSstephh 
2697b5016cbbSstephh /*ARGSUSED*/
2698b5016cbbSstephh static void
serd_destructor(void * left,void * right,void * arg)2699b5016cbbSstephh serd_destructor(void *left, void *right, void *arg)
2700b5016cbbSstephh {
2701b5016cbbSstephh 	struct serd_entry *entp = (struct serd_entry *)left;
2702b5016cbbSstephh 	FREE(entp);
2703b5016cbbSstephh }
2704b5016cbbSstephh 
2705b5016cbbSstephh /*
2706b5016cbbSstephh  * Callback used in a walk of the SerdEngines to reset matching serd engines.
2707b5016cbbSstephh  */
2708b5016cbbSstephh /*ARGSUSED*/
2709b5016cbbSstephh static void
serd_reset_cb(struct serd_entry * entp,void * unused,const struct ipath * ipp)2710b5016cbbSstephh serd_reset_cb(struct serd_entry *entp, void *unused, const struct ipath *ipp)
2711b5016cbbSstephh {
2712b5016cbbSstephh 	char *path;
2713b5016cbbSstephh 
2714b5016cbbSstephh 	if (entp->ipath == ipp) {
2715b5016cbbSstephh 		path = ipath2str(entp->ename, ipp);
2716b5016cbbSstephh 		out(O_ALTFP, "serd_reset_cb: resetting %s", path);
2717b5016cbbSstephh 		fmd_serd_reset(entp->hdl, path);
2718b5016cbbSstephh 		FREE(path);
2719b5016cbbSstephh 		Serd_need_save = 1;
2720b5016cbbSstephh 	}
2721b5016cbbSstephh }
2722b5016cbbSstephh 
2723e5ba14ffSstephh /*ARGSUSED*/
2724e5ba14ffSstephh static void
serd_topo_chg_cb(struct serd_entry * entp,void * unused,void * unused2)2725e5ba14ffSstephh serd_topo_chg_cb(struct serd_entry *entp, void *unused, void *unused2)
2726e5ba14ffSstephh {
2727e5ba14ffSstephh 	char *path;
2728e5ba14ffSstephh 	nvlist_t *fmri;
2729e5ba14ffSstephh 
2730e5ba14ffSstephh 	fmri = ipath2fmri((struct ipath *)(entp->ipath));
2731e5ba14ffSstephh 	if (!platform_path_exists(fmri)) {
2732e5ba14ffSstephh 		path = ipath2str(entp->ename, entp->ipath);
2733e5ba14ffSstephh 		out(O_ALTFP, "serd_topo_chg_cb: not present %s", path);
2734e5ba14ffSstephh 		fmd_serd_reset(entp->hdl, path);
2735e5ba14ffSstephh 		FREE(path);
2736e5ba14ffSstephh 		Serd_need_save = 1;
2737e5ba14ffSstephh 	}
2738e5ba14ffSstephh 	nvlist_free(fmri);
2739e5ba14ffSstephh }
2740e5ba14ffSstephh 
2741b5016cbbSstephh void
serd_fini(void)2742b5016cbbSstephh serd_fini(void)
2743b5016cbbSstephh {
2744b5016cbbSstephh 	lut_free(SerdEngines, serd_destructor, NULL);
2745b5016cbbSstephh }
2746b5016cbbSstephh 
27477c478bd9Sstevel@tonic-gate static void
publish_suspects(struct fme * fmep,struct rsl * srl)2748b7d3956bSstephh publish_suspects(struct fme *fmep, struct rsl *srl)
27497c478bd9Sstevel@tonic-gate {
27507c478bd9Sstevel@tonic-gate 	struct rsl *rp;
27517c478bd9Sstevel@tonic-gate 	nvlist_t *fault;
27527c478bd9Sstevel@tonic-gate 	uint8_t cert;
27537c478bd9Sstevel@tonic-gate 	uint_t *frs;
27548e7248e5SStephen Hanson 	uint_t frsum, fr;
27557aec1d6eScindi 	uint_t messval;
2756b7d3956bSstephh 	uint_t retireval;
2757b7d3956bSstephh 	uint_t responseval;
27587aec1d6eScindi 	struct node *snp;
27597c478bd9Sstevel@tonic-gate 	int frcnt, fridx;
27607aec1d6eScindi 	boolean_t allfaulty = B_TRUE;
2761b7d3956bSstephh 	struct rsl *erl = srl + fmep->nsuspects - 1;
27627c478bd9Sstevel@tonic-gate 
27637c478bd9Sstevel@tonic-gate 	/*
2764b7d3956bSstephh 	 * sort the array
27657c478bd9Sstevel@tonic-gate 	 */
2766b7d3956bSstephh 	qsort(srl, fmep->nsuspects, sizeof (struct rsl), rslcmp);
27677c478bd9Sstevel@tonic-gate 
27687c478bd9Sstevel@tonic-gate 	/* sum the fitrates */
27697c478bd9Sstevel@tonic-gate 	frs = alloca(fmep->nsuspects * sizeof (uint_t));
27707c478bd9Sstevel@tonic-gate 	fridx = frcnt = frsum = 0;
27717c478bd9Sstevel@tonic-gate 
27727c478bd9Sstevel@tonic-gate 	for (rp = srl; rp <= erl; rp++) {
27737c478bd9Sstevel@tonic-gate 		struct node *n;
27747c478bd9Sstevel@tonic-gate 
27757c478bd9Sstevel@tonic-gate 		n = eventprop_lookup(rp->suspect, L_FITrate);
27767c478bd9Sstevel@tonic-gate 		if (node2uint(n, &fr) != 0) {
27777c478bd9Sstevel@tonic-gate 			out(O_DEBUG|O_NONL, "event ");
27787c478bd9Sstevel@tonic-gate 			ipath_print(O_DEBUG|O_NONL,
2779d96ce684Sstephh 			    rp->suspect->enode->u.event.ename->u.name.s,
2780d96ce684Sstephh 			    rp->suspect->ipp);
2781f6e214c7SGavin Maltby 			out(O_VERB, " has no FITrate (using 1)");
27827c478bd9Sstevel@tonic-gate 			fr = 1;
27837c478bd9Sstevel@tonic-gate 		} else if (fr == 0) {
27847c478bd9Sstevel@tonic-gate 			out(O_DEBUG|O_NONL, "event ");
27857c478bd9Sstevel@tonic-gate 			ipath_print(O_DEBUG|O_NONL,
2786d96ce684Sstephh 			    rp->suspect->enode->u.event.ename->u.name.s,
2787d96ce684Sstephh 			    rp->suspect->ipp);
2788f6e214c7SGavin Maltby 			out(O_VERB, " has zero FITrate (using 1)");
27897c478bd9Sstevel@tonic-gate 			fr = 1;
27907c478bd9Sstevel@tonic-gate 		}
27917c478bd9Sstevel@tonic-gate 
27927c478bd9Sstevel@tonic-gate 		frs[fridx++] = fr;
27937c478bd9Sstevel@tonic-gate 		frsum += fr;
27947c478bd9Sstevel@tonic-gate 		frcnt++;
27957c478bd9Sstevel@tonic-gate 	}
27967c478bd9Sstevel@tonic-gate 
27977c478bd9Sstevel@tonic-gate 	/* Add them in reverse order of our sort, as fmd reverses order */
27987c478bd9Sstevel@tonic-gate 	for (rp = erl; rp >= srl; rp--) {
27997c478bd9Sstevel@tonic-gate 		cert = percentof(frs[--fridx], frsum);
28007c478bd9Sstevel@tonic-gate 		fault = fmd_nvl_create_fault(fmep->hdl,
28017c478bd9Sstevel@tonic-gate 		    rp->suspect->enode->u.event.ename->u.name.s,
28027c478bd9Sstevel@tonic-gate 		    cert,
28037c478bd9Sstevel@tonic-gate 		    rp->asru,
28047c478bd9Sstevel@tonic-gate 		    rp->fru,
28057c478bd9Sstevel@tonic-gate 		    rp->rsrc);
28067c478bd9Sstevel@tonic-gate 		if (fault == NULL)
28077c478bd9Sstevel@tonic-gate 			out(O_DIE, "fault creation failed");
28087aec1d6eScindi 		/* if "message" property exists, add it to the fault */
28097aec1d6eScindi 		if (node2uint(eventprop_lookup(rp->suspect, L_message),
28107aec1d6eScindi 		    &messval) == 0) {
28117aec1d6eScindi 
28127aec1d6eScindi 			out(O_ALTFP,
28137aec1d6eScindi 			    "[FME%d, %s adds message=%d to suspect list]",
28147aec1d6eScindi 			    fmep->id,
28157aec1d6eScindi 			    rp->suspect->enode->u.event.ename->u.name.s,
28167aec1d6eScindi 			    messval);
28177aec1d6eScindi 			if (nvlist_add_boolean_value(fault,
28187aec1d6eScindi 			    FM_SUSPECT_MESSAGE,
28197aec1d6eScindi 			    (messval) ? B_TRUE : B_FALSE) != 0) {
28207aec1d6eScindi 				out(O_DIE, "cannot add no-message to fault");
28217aec1d6eScindi 			}
28227aec1d6eScindi 		}
2823b7d3956bSstephh 
2824b7d3956bSstephh 		/* if "retire" property exists, add it to the fault */
2825b7d3956bSstephh 		if (node2uint(eventprop_lookup(rp->suspect, L_retire),
2826b7d3956bSstephh 		    &retireval) == 0) {
2827b7d3956bSstephh 
2828b7d3956bSstephh 			out(O_ALTFP,
2829b7d3956bSstephh 			    "[FME%d, %s adds retire=%d to suspect list]",
2830b7d3956bSstephh 			    fmep->id,
2831b7d3956bSstephh 			    rp->suspect->enode->u.event.ename->u.name.s,
2832b7d3956bSstephh 			    retireval);
2833b7d3956bSstephh 			if (nvlist_add_boolean_value(fault,
2834b7d3956bSstephh 			    FM_SUSPECT_RETIRE,
2835b7d3956bSstephh 			    (retireval) ? B_TRUE : B_FALSE) != 0) {
2836b7d3956bSstephh 				out(O_DIE, "cannot add no-retire to fault");
2837b7d3956bSstephh 			}
2838b7d3956bSstephh 		}
2839b7d3956bSstephh 
2840b7d3956bSstephh 		/* if "response" property exists, add it to the fault */
2841b7d3956bSstephh 		if (node2uint(eventprop_lookup(rp->suspect, L_response),
2842b7d3956bSstephh 		    &responseval) == 0) {
2843b7d3956bSstephh 
2844b7d3956bSstephh 			out(O_ALTFP,
2845b7d3956bSstephh 			    "[FME%d, %s adds response=%d to suspect list]",
2846b7d3956bSstephh 			    fmep->id,
2847b7d3956bSstephh 			    rp->suspect->enode->u.event.ename->u.name.s,
2848b7d3956bSstephh 			    responseval);
2849b7d3956bSstephh 			if (nvlist_add_boolean_value(fault,
2850b7d3956bSstephh 			    FM_SUSPECT_RESPONSE,
2851b7d3956bSstephh 			    (responseval) ? B_TRUE : B_FALSE) != 0) {
2852b7d3956bSstephh 				out(O_DIE, "cannot add no-response to fault");
2853b7d3956bSstephh 			}
2854b7d3956bSstephh 		}
2855b7d3956bSstephh 
28567aec1d6eScindi 		/* add any payload properties */
28577aec1d6eScindi 		lut_walk(rp->suspect->payloadprops,
28587aec1d6eScindi 		    (lut_cb)addpayloadprop, (void *)fault);
28597c478bd9Sstevel@tonic-gate 		rslfree(rp);
28605f25dc2aSgavinm 
28615f25dc2aSgavinm 		/*
28625f25dc2aSgavinm 		 * If "action" property exists, evaluate it;  this must be done
2863b7d3956bSstephh 		 * before the allfaulty check below since some actions may
286425c6ff4bSstephh 		 * modify the asru to be used in fmd_nvl_fmri_has_fault.  This
28655f25dc2aSgavinm 		 * needs to be restructured if any new actions are introduced
28665f25dc2aSgavinm 		 * that have effects that we do not want to be visible if
28675f25dc2aSgavinm 		 * we decide not to publish in the dupclose check below.
28685f25dc2aSgavinm 		 */
28697aec1d6eScindi 		if ((snp = eventprop_lookup(rp->suspect, L_action)) != NULL) {
28707aec1d6eScindi 			struct evalue evalue;
28717aec1d6eScindi 
28727aec1d6eScindi 			out(O_ALTFP|O_NONL,
28737aec1d6eScindi 			    "[FME%d, %s action ", fmep->id,
28747aec1d6eScindi 			    rp->suspect->enode->u.event.ename->u.name.s);
28757aec1d6eScindi 			ptree_name_iter(O_ALTFP|O_NONL, snp);
28767aec1d6eScindi 			out(O_ALTFP, "]");
28777aec1d6eScindi 			Action_nvl = fault;
28787aec1d6eScindi 			(void) eval_expr(snp, NULL, NULL, NULL, NULL,
28797aec1d6eScindi 			    NULL, 0, &evalue);
28807aec1d6eScindi 		}
28815f25dc2aSgavinm 
2882b7d3956bSstephh 		fmd_case_add_suspect(fmep->hdl, fmep->fmcase, fault);
2883b7d3956bSstephh 
28847aec1d6eScindi 		/*
2885567cc2e6Sstephh 		 * check if the asru is already marked as "faulty".
28867aec1d6eScindi 		 */
2887567cc2e6Sstephh 		if (allfaulty) {
28887aec1d6eScindi 			nvlist_t *asru;
28897aec1d6eScindi 
2890b7d3956bSstephh 			out(O_ALTFP|O_VERB, "FME%d dup check ", fmep->id);
28917aec1d6eScindi 			itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, rp->suspect);
28927aec1d6eScindi 			out(O_ALTFP|O_VERB|O_NONL, " ");
28937aec1d6eScindi 			if (nvlist_lookup_nvlist(fault,
28947aec1d6eScindi 			    FM_FAULT_ASRU, &asru) != 0) {
28957aec1d6eScindi 				out(O_ALTFP|O_VERB, "NULL asru");
28967aec1d6eScindi 				allfaulty = B_FALSE;
289725c6ff4bSstephh 			} else if (fmd_nvl_fmri_has_fault(fmep->hdl, asru,
289825c6ff4bSstephh 			    FMD_HAS_FAULT_ASRU, NULL)) {
28997aec1d6eScindi 				out(O_ALTFP|O_VERB, "faulty");
29007aec1d6eScindi 			} else {
29017aec1d6eScindi 				out(O_ALTFP|O_VERB, "not faulty");
29027aec1d6eScindi 				allfaulty = B_FALSE;
29037aec1d6eScindi 			}
29047aec1d6eScindi 		}
29057aec1d6eScindi 
29067aec1d6eScindi 	}
29075f25dc2aSgavinm 
2908567cc2e6Sstephh 	if (!allfaulty) {
2909567cc2e6Sstephh 		/*
2910567cc2e6Sstephh 		 * don't update the count stat if all asrus are already
2911567cc2e6Sstephh 		 * present and unrepaired in the asru cache
2912567cc2e6Sstephh 		 */
29135f25dc2aSgavinm 		for (rp = erl; rp >= srl; rp--) {
29145f25dc2aSgavinm 			struct event *suspect = rp->suspect;
29155f25dc2aSgavinm 
29165f25dc2aSgavinm 			if (suspect == NULL)
29175f25dc2aSgavinm 				continue;
29185f25dc2aSgavinm 
29195f25dc2aSgavinm 			/* if "count" exists, increment the appropriate stat */
29205f25dc2aSgavinm 			if ((snp = eventprop_lookup(suspect,
29215f25dc2aSgavinm 			    L_count)) != NULL) {
29225f25dc2aSgavinm 				out(O_ALTFP|O_NONL,
29235f25dc2aSgavinm 				    "[FME%d, %s count ", fmep->id,
29245f25dc2aSgavinm 				    suspect->enode->u.event.ename->u.name.s);
29255f25dc2aSgavinm 				ptree_name_iter(O_ALTFP|O_NONL, snp);
29265f25dc2aSgavinm 				out(O_ALTFP, "]");
29275f25dc2aSgavinm 				istat_bump(snp, 0);
29285f25dc2aSgavinm 
29295f25dc2aSgavinm 			}
29305f25dc2aSgavinm 		}
29315f25dc2aSgavinm 		istat_save();	/* write out any istat changes */
2932567cc2e6Sstephh 	}
29337c478bd9Sstevel@tonic-gate }
29347c478bd9Sstevel@tonic-gate 
293532d4e834STarik Soydan static const char *
undiag_2defect_str(int ud)293632d4e834STarik Soydan undiag_2defect_str(int ud)
293732d4e834STarik Soydan {
293832d4e834STarik Soydan 	switch (ud) {
293932d4e834STarik Soydan 	case UD_VAL_MISSINGINFO:
294032d4e834STarik Soydan 	case UD_VAL_MISSINGOBS:
294132d4e834STarik Soydan 	case UD_VAL_MISSINGPATH:
294232d4e834STarik Soydan 	case UD_VAL_MISSINGZERO:
294332d4e834STarik Soydan 	case UD_VAL_BADOBS:
294432d4e834STarik Soydan 	case UD_VAL_CFGMISMATCH:
294532d4e834STarik Soydan 		return (UNDIAG_DEFECT_CHKPT);
294632d4e834STarik Soydan 
294732d4e834STarik Soydan 	case UD_VAL_BADEVENTI:
2948705e9f42SStephen Hanson 	case UD_VAL_BADEVENTPATH:
2949705e9f42SStephen Hanson 	case UD_VAL_BADEVENTCLASS:
295032d4e834STarik Soydan 	case UD_VAL_INSTFAIL:
295132d4e834STarik Soydan 	case UD_VAL_NOPATH:
295232d4e834STarik Soydan 	case UD_VAL_UNSOLVD:
295332d4e834STarik Soydan 		return (UNDIAG_DEFECT_FME);
295432d4e834STarik Soydan 
295532d4e834STarik Soydan 	case UD_VAL_MAXFME:
295632d4e834STarik Soydan 		return (UNDIAG_DEFECT_LIMIT);
295732d4e834STarik Soydan 
295832d4e834STarik Soydan 	case UD_VAL_UNKNOWN:
295932d4e834STarik Soydan 	default:
296032d4e834STarik Soydan 		return (UNDIAG_DEFECT_UNKNOWN);
296132d4e834STarik Soydan 	}
296232d4e834STarik Soydan }
296332d4e834STarik Soydan 
2964705e9f42SStephen Hanson static const char *
undiag_2fault_str(int ud)2965705e9f42SStephen Hanson undiag_2fault_str(int ud)
296632d4e834STarik Soydan {
296732d4e834STarik Soydan 	switch (ud) {
296832d4e834STarik Soydan 	case UD_VAL_BADEVENTI:
2969705e9f42SStephen Hanson 	case UD_VAL_BADEVENTPATH:
2970705e9f42SStephen Hanson 	case UD_VAL_BADEVENTCLASS:
297132d4e834STarik Soydan 	case UD_VAL_INSTFAIL:
297232d4e834STarik Soydan 	case UD_VAL_NOPATH:
297332d4e834STarik Soydan 	case UD_VAL_UNSOLVD:
2974705e9f42SStephen Hanson 		return (UNDIAG_FAULT_FME);
2975705e9f42SStephen Hanson 	default:
2976705e9f42SStephen Hanson 		return (NULL);
2977705e9f42SStephen Hanson 	}
2978705e9f42SStephen Hanson }
2979705e9f42SStephen Hanson 
2980705e9f42SStephen Hanson static char *
undiag_2reason_str(int ud,char * arg)2981705e9f42SStephen Hanson undiag_2reason_str(int ud, char *arg)
2982705e9f42SStephen Hanson {
2983705e9f42SStephen Hanson 	const char *ptr;
2984705e9f42SStephen Hanson 	char *buf;
2985705e9f42SStephen Hanson 	int with_arg = 0;
2986705e9f42SStephen Hanson 
2987705e9f42SStephen Hanson 	switch (ud) {
2988705e9f42SStephen Hanson 	case UD_VAL_BADEVENTPATH:
2989705e9f42SStephen Hanson 		ptr = UD_STR_BADEVENTPATH;
2990705e9f42SStephen Hanson 		with_arg = 1;
2991705e9f42SStephen Hanson 		break;
2992705e9f42SStephen Hanson 	case UD_VAL_BADEVENTCLASS:
2993705e9f42SStephen Hanson 		ptr = UD_STR_BADEVENTCLASS;
2994705e9f42SStephen Hanson 		with_arg = 1;
2995705e9f42SStephen Hanson 		break;
2996705e9f42SStephen Hanson 	case UD_VAL_BADEVENTI:
2997705e9f42SStephen Hanson 		ptr = UD_STR_BADEVENTI;
2998705e9f42SStephen Hanson 		with_arg = 1;
2999705e9f42SStephen Hanson 		break;
3000705e9f42SStephen Hanson 	case UD_VAL_BADOBS:
3001705e9f42SStephen Hanson 		ptr = UD_STR_BADOBS;
3002705e9f42SStephen Hanson 		break;
3003705e9f42SStephen Hanson 	case UD_VAL_CFGMISMATCH:
3004705e9f42SStephen Hanson 		ptr = UD_STR_CFGMISMATCH;
3005705e9f42SStephen Hanson 		break;
3006705e9f42SStephen Hanson 	case UD_VAL_INSTFAIL:
3007705e9f42SStephen Hanson 		ptr = UD_STR_INSTFAIL;
3008705e9f42SStephen Hanson 		with_arg = 1;
3009705e9f42SStephen Hanson 		break;
3010705e9f42SStephen Hanson 	case UD_VAL_MAXFME:
3011705e9f42SStephen Hanson 		ptr = UD_STR_MAXFME;
3012705e9f42SStephen Hanson 		break;
3013705e9f42SStephen Hanson 	case UD_VAL_MISSINGINFO:
3014705e9f42SStephen Hanson 		ptr = UD_STR_MISSINGINFO;
3015705e9f42SStephen Hanson 		break;
3016705e9f42SStephen Hanson 	case UD_VAL_MISSINGOBS:
3017705e9f42SStephen Hanson 		ptr = UD_STR_MISSINGOBS;
3018705e9f42SStephen Hanson 		break;
3019705e9f42SStephen Hanson 	case UD_VAL_MISSINGPATH:
3020705e9f42SStephen Hanson 		ptr = UD_STR_MISSINGPATH;
3021705e9f42SStephen Hanson 		break;
3022705e9f42SStephen Hanson 	case UD_VAL_MISSINGZERO:
3023705e9f42SStephen Hanson 		ptr = UD_STR_MISSINGZERO;
3024705e9f42SStephen Hanson 		break;
3025705e9f42SStephen Hanson 	case UD_VAL_NOPATH:
3026705e9f42SStephen Hanson 		ptr = UD_STR_NOPATH;
3027705e9f42SStephen Hanson 		with_arg = 1;
3028705e9f42SStephen Hanson 		break;
3029705e9f42SStephen Hanson 	case UD_VAL_UNSOLVD:
3030705e9f42SStephen Hanson 		ptr = UD_STR_UNSOLVD;
3031705e9f42SStephen Hanson 		break;
303232d4e834STarik Soydan 	case UD_VAL_UNKNOWN:
303332d4e834STarik Soydan 	default:
3034705e9f42SStephen Hanson 		ptr = UD_STR_UNKNOWN;
3035705e9f42SStephen Hanson 		break;
303632d4e834STarik Soydan 	}
3037705e9f42SStephen Hanson 	if (with_arg) {
3038705e9f42SStephen Hanson 		buf = MALLOC(strlen(ptr) + strlen(arg) - 1);
3039705e9f42SStephen Hanson 		(void) sprintf(buf, ptr, arg);
3040705e9f42SStephen Hanson 	} else {
3041705e9f42SStephen Hanson 		buf = MALLOC(strlen(ptr) + 1);
3042705e9f42SStephen Hanson 		(void) sprintf(buf, ptr);
3043705e9f42SStephen Hanson 	}
3044705e9f42SStephen Hanson 	return (buf);
304532d4e834STarik Soydan }
304632d4e834STarik Soydan 
30477c478bd9Sstevel@tonic-gate static void
publish_undiagnosable(fmd_hdl_t * hdl,fmd_event_t * ffep,fmd_case_t * fmcase,nvlist_t * detector,char * arg)3048705e9f42SStephen Hanson publish_undiagnosable(fmd_hdl_t *hdl, fmd_event_t *ffep, fmd_case_t *fmcase,
3049705e9f42SStephen Hanson     nvlist_t *detector, char *arg)
30507c478bd9Sstevel@tonic-gate {
30517c478bd9Sstevel@tonic-gate 	struct case_list *newcase;
3052705e9f42SStephen Hanson 	nvlist_t *defect, *fault;
3053705e9f42SStephen Hanson 	const char *faultstr;
3054705e9f42SStephen Hanson 	char *reason = undiag_2reason_str(Undiag_reason, arg);
30557c478bd9Sstevel@tonic-gate 
30567c478bd9Sstevel@tonic-gate 	out(O_ALTFP,
30577c478bd9Sstevel@tonic-gate 	    "[undiagnosable ereport received, "
3058705e9f42SStephen Hanson 	    "creating and closing a new case (%s)]", reason);
30597c478bd9Sstevel@tonic-gate 
30607c478bd9Sstevel@tonic-gate 	newcase = MALLOC(sizeof (struct case_list));
30617c478bd9Sstevel@tonic-gate 	newcase->next = NULL;
3062b5016cbbSstephh 	newcase->fmcase = fmcase;
30637c478bd9Sstevel@tonic-gate 	if (Undiagablecaselist != NULL)
30647c478bd9Sstevel@tonic-gate 		newcase->next = Undiagablecaselist;
30657c478bd9Sstevel@tonic-gate 	Undiagablecaselist = newcase;
30667c478bd9Sstevel@tonic-gate 
30677c478bd9Sstevel@tonic-gate 	if (ffep != NULL)
30687c478bd9Sstevel@tonic-gate 		fmd_case_add_ereport(hdl, newcase->fmcase, ffep);
30697c478bd9Sstevel@tonic-gate 
3070705e9f42SStephen Hanson 	/* add defect */
307132d4e834STarik Soydan 	defect = fmd_nvl_create_fault(hdl,
3072705e9f42SStephen Hanson 	    undiag_2defect_str(Undiag_reason), 50, NULL, NULL, detector);
3073705e9f42SStephen Hanson 	(void) nvlist_add_string(defect, UNDIAG_REASON, reason);
3074705e9f42SStephen Hanson 	(void) nvlist_add_boolean_value(defect, FM_SUSPECT_RETIRE, B_FALSE);
3075705e9f42SStephen Hanson 	(void) nvlist_add_boolean_value(defect, FM_SUSPECT_RESPONSE, B_FALSE);
30767c478bd9Sstevel@tonic-gate 	fmd_case_add_suspect(hdl, newcase->fmcase, defect);
30777c478bd9Sstevel@tonic-gate 
3078705e9f42SStephen Hanson 	/* add fault if appropriate */
3079705e9f42SStephen Hanson 	faultstr = undiag_2fault_str(Undiag_reason);
3080705e9f42SStephen Hanson 	if (faultstr != NULL) {
3081705e9f42SStephen Hanson 		fault = fmd_nvl_create_fault(hdl, faultstr, 50, NULL, NULL,
3082705e9f42SStephen Hanson 		    detector);
3083705e9f42SStephen Hanson 		(void) nvlist_add_string(fault, UNDIAG_REASON, reason);
3084705e9f42SStephen Hanson 		(void) nvlist_add_boolean_value(fault, FM_SUSPECT_RETIRE,
3085705e9f42SStephen Hanson 		    B_FALSE);
3086705e9f42SStephen Hanson 		(void) nvlist_add_boolean_value(fault, FM_SUSPECT_RESPONSE,
3087705e9f42SStephen Hanson 		    B_FALSE);
3088705e9f42SStephen Hanson 		fmd_case_add_suspect(hdl, newcase->fmcase, fault);
3089705e9f42SStephen Hanson 	}
3090705e9f42SStephen Hanson 	FREE(reason);
3091705e9f42SStephen Hanson 
3092705e9f42SStephen Hanson 	/* solve and close case */
30937c478bd9Sstevel@tonic-gate 	fmd_case_solve(hdl, newcase->fmcase);
30947c478bd9Sstevel@tonic-gate 	fmd_case_close(hdl, newcase->fmcase);
309532d4e834STarik Soydan 	Undiag_reason = UD_VAL_UNKNOWN;
30967c478bd9Sstevel@tonic-gate }
30977c478bd9Sstevel@tonic-gate 
30987c478bd9Sstevel@tonic-gate static void
fme_undiagnosable(struct fme * f)30997c478bd9Sstevel@tonic-gate fme_undiagnosable(struct fme *f)
31007c478bd9Sstevel@tonic-gate {
3101705e9f42SStephen Hanson 	nvlist_t *defect, *fault, *detector = NULL;
3102705e9f42SStephen Hanson 	struct event *ep;
3103705e9f42SStephen Hanson 	char *pathstr;
3104705e9f42SStephen Hanson 	const char *faultstr;
3105705e9f42SStephen Hanson 	char *reason = undiag_2reason_str(Undiag_reason, NULL);
31067c478bd9Sstevel@tonic-gate 
31077c478bd9Sstevel@tonic-gate 	out(O_ALTFP, "[solving/closing FME%d, case %s (%s)]",
3108705e9f42SStephen Hanson 	    f->id, fmd_case_uuid(f->hdl, f->fmcase), reason);
31097c478bd9Sstevel@tonic-gate 
3110705e9f42SStephen Hanson 	for (ep = f->observations; ep; ep = ep->observations) {
3111705e9f42SStephen Hanson 
3112705e9f42SStephen Hanson 		if (ep->ffep != f->e0r)
3113705e9f42SStephen Hanson 			fmd_case_add_ereport(f->hdl, f->fmcase, ep->ffep);
3114705e9f42SStephen Hanson 
3115705e9f42SStephen Hanson 		pathstr = ipath2str(NULL, ipath(platform_getpath(ep->nvp)));
3116705e9f42SStephen Hanson 		platform_units_translate(0, f->config, NULL, NULL, &detector,
3117705e9f42SStephen Hanson 		    pathstr);
3118705e9f42SStephen Hanson 		FREE(pathstr);
3119705e9f42SStephen Hanson 
3120705e9f42SStephen Hanson 		/* add defect */
312132d4e834STarik Soydan 		defect = fmd_nvl_create_fault(f->hdl,
3122705e9f42SStephen Hanson 		    undiag_2defect_str(Undiag_reason), 50 / f->uniqobs,
3123705e9f42SStephen Hanson 		    NULL, NULL, detector);
3124705e9f42SStephen Hanson 		(void) nvlist_add_string(defect, UNDIAG_REASON, reason);
3125705e9f42SStephen Hanson 		(void) nvlist_add_boolean_value(defect, FM_SUSPECT_RETIRE,
3126705e9f42SStephen Hanson 		    B_FALSE);
3127705e9f42SStephen Hanson 		(void) nvlist_add_boolean_value(defect, FM_SUSPECT_RESPONSE,
3128705e9f42SStephen Hanson 		    B_FALSE);
31297c478bd9Sstevel@tonic-gate 		fmd_case_add_suspect(f->hdl, f->fmcase, defect);
3130705e9f42SStephen Hanson 
3131705e9f42SStephen Hanson 		/* add fault if appropriate */
3132705e9f42SStephen Hanson 		faultstr = undiag_2fault_str(Undiag_reason);
3133705e9f42SStephen Hanson 		if (faultstr == NULL)
3134705e9f42SStephen Hanson 			continue;
3135705e9f42SStephen Hanson 		fault = fmd_nvl_create_fault(f->hdl, faultstr, 50 / f->uniqobs,
3136705e9f42SStephen Hanson 		    NULL, NULL, detector);
3137705e9f42SStephen Hanson 		(void) nvlist_add_string(fault, UNDIAG_REASON, reason);
3138705e9f42SStephen Hanson 		(void) nvlist_add_boolean_value(fault, FM_SUSPECT_RETIRE,
3139705e9f42SStephen Hanson 		    B_FALSE);
3140705e9f42SStephen Hanson 		(void) nvlist_add_boolean_value(fault, FM_SUSPECT_RESPONSE,
3141705e9f42SStephen Hanson 		    B_FALSE);
3142705e9f42SStephen Hanson 		fmd_case_add_suspect(f->hdl, f->fmcase, fault);
3143705e9f42SStephen Hanson 		nvlist_free(detector);
3144705e9f42SStephen Hanson 	}
3145705e9f42SStephen Hanson 	FREE(reason);
31467c478bd9Sstevel@tonic-gate 	fmd_case_solve(f->hdl, f->fmcase);
31477c478bd9Sstevel@tonic-gate 	fmd_case_close(f->hdl, f->fmcase);
314832d4e834STarik Soydan 	Undiag_reason = UD_VAL_UNKNOWN;
31497c478bd9Sstevel@tonic-gate }
31507c478bd9Sstevel@tonic-gate 
31517c478bd9Sstevel@tonic-gate /*
31527c478bd9Sstevel@tonic-gate  * fme_close_case
31537c478bd9Sstevel@tonic-gate  *
31547c478bd9Sstevel@tonic-gate  *	Find the requested case amongst our fmes and close it.  Free up
31557c478bd9Sstevel@tonic-gate  *	the related fme.
31567c478bd9Sstevel@tonic-gate  */
31577c478bd9Sstevel@tonic-gate void
fme_close_case(fmd_hdl_t * hdl,fmd_case_t * fmcase)31587c478bd9Sstevel@tonic-gate fme_close_case(fmd_hdl_t *hdl, fmd_case_t *fmcase)
31597c478bd9Sstevel@tonic-gate {
31607c478bd9Sstevel@tonic-gate 	struct case_list *ucasep, *prevcasep = NULL;
31617c478bd9Sstevel@tonic-gate 	struct fme *prev = NULL;
31627c478bd9Sstevel@tonic-gate 	struct fme *fmep;
31637c478bd9Sstevel@tonic-gate 
31647c478bd9Sstevel@tonic-gate 	for (ucasep = Undiagablecaselist; ucasep; ucasep = ucasep->next) {
31657c478bd9Sstevel@tonic-gate 		if (fmcase != ucasep->fmcase) {
31667c478bd9Sstevel@tonic-gate 			prevcasep = ucasep;
31677c478bd9Sstevel@tonic-gate 			continue;
31687c478bd9Sstevel@tonic-gate 		}
31697c478bd9Sstevel@tonic-gate 
31707c478bd9Sstevel@tonic-gate 		if (prevcasep == NULL)
31717c478bd9Sstevel@tonic-gate 			Undiagablecaselist = Undiagablecaselist->next;
31727c478bd9Sstevel@tonic-gate 		else
31737c478bd9Sstevel@tonic-gate 			prevcasep->next = ucasep->next;
31747c478bd9Sstevel@tonic-gate 
31757c478bd9Sstevel@tonic-gate 		FREE(ucasep);
31767c478bd9Sstevel@tonic-gate 		return;
31777c478bd9Sstevel@tonic-gate 	}
31787c478bd9Sstevel@tonic-gate 
31797c478bd9Sstevel@tonic-gate 	for (fmep = FMElist; fmep; fmep = fmep->next) {
31807c478bd9Sstevel@tonic-gate 		if (fmep->hdl == hdl && fmep->fmcase == fmcase)
31817c478bd9Sstevel@tonic-gate 			break;
31827c478bd9Sstevel@tonic-gate 		prev = fmep;
31837c478bd9Sstevel@tonic-gate 	}
31847c478bd9Sstevel@tonic-gate 
31857c478bd9Sstevel@tonic-gate 	if (fmep == NULL) {
31867c478bd9Sstevel@tonic-gate 		out(O_WARN, "Eft asked to close unrecognized case [%s].",
31877c478bd9Sstevel@tonic-gate 		    fmd_case_uuid(hdl, fmcase));
31887c478bd9Sstevel@tonic-gate 		return;
31897c478bd9Sstevel@tonic-gate 	}
31907c478bd9Sstevel@tonic-gate 
31917c478bd9Sstevel@tonic-gate 	if (EFMElist == fmep)
31927c478bd9Sstevel@tonic-gate 		EFMElist = prev;
31937c478bd9Sstevel@tonic-gate 
31947c478bd9Sstevel@tonic-gate 	if (prev == NULL)
31957c478bd9Sstevel@tonic-gate 		FMElist = FMElist->next;
31967c478bd9Sstevel@tonic-gate 	else
31977c478bd9Sstevel@tonic-gate 		prev->next = fmep->next;
31987c478bd9Sstevel@tonic-gate 
31997c478bd9Sstevel@tonic-gate 	fmep->next = NULL;
32007c478bd9Sstevel@tonic-gate 
32017c478bd9Sstevel@tonic-gate 	/* Get rid of any timer this fme has set */
32027c478bd9Sstevel@tonic-gate 	if (fmep->wull != 0)
32037c478bd9Sstevel@tonic-gate 		fmd_timer_remove(fmep->hdl, fmep->timer);
32047c478bd9Sstevel@tonic-gate 
32057c478bd9Sstevel@tonic-gate 	if (ClosedFMEs == NULL) {
32067c478bd9Sstevel@tonic-gate 		ClosedFMEs = fmep;
32077c478bd9Sstevel@tonic-gate 	} else {
32087c478bd9Sstevel@tonic-gate 		fmep->next = ClosedFMEs;
32097c478bd9Sstevel@tonic-gate 		ClosedFMEs = fmep;
32107c478bd9Sstevel@tonic-gate 	}
32110cc1f05eSjrutt 
32120cc1f05eSjrutt 	Open_fme_count--;
32130cc1f05eSjrutt 
32140cc1f05eSjrutt 	/* See if we can close the overflow FME */
32150cc1f05eSjrutt 	if (Open_fme_count <= Max_fme) {
32160cc1f05eSjrutt 		for (fmep = FMElist; fmep; fmep = fmep->next) {
32170cc1f05eSjrutt 			if (fmep->overflow && !(fmd_case_closed(fmep->hdl,
32180cc1f05eSjrutt 			    fmep->fmcase)))
32190cc1f05eSjrutt 				break;
32200cc1f05eSjrutt 		}
32210cc1f05eSjrutt 
32220cc1f05eSjrutt 		if (fmep != NULL)
32230cc1f05eSjrutt 			fmd_case_close(fmep->hdl, fmep->fmcase);
32240cc1f05eSjrutt 	}
32257c478bd9Sstevel@tonic-gate }
32267c478bd9Sstevel@tonic-gate 
32277c478bd9Sstevel@tonic-gate /*
32287c478bd9Sstevel@tonic-gate  * fme_set_timer()
32297c478bd9Sstevel@tonic-gate  *	If the time we need to wait for the given FME is less than the
32307c478bd9Sstevel@tonic-gate  *	current timer, kick that old timer out and establish a new one.
32317c478bd9Sstevel@tonic-gate  */
32327aec1d6eScindi static int
fme_set_timer(struct fme * fmep,unsigned long long wull)32337c478bd9Sstevel@tonic-gate fme_set_timer(struct fme *fmep, unsigned long long wull)
32347c478bd9Sstevel@tonic-gate {
32357c478bd9Sstevel@tonic-gate 	out(O_ALTFP|O_VERB|O_NONL, " fme_set_timer: request to wait ");
32367c478bd9Sstevel@tonic-gate 	ptree_timeval(O_ALTFP|O_VERB, &wull);
32377c478bd9Sstevel@tonic-gate 
32387c478bd9Sstevel@tonic-gate 	if (wull <= fmep->pull) {
32397c478bd9Sstevel@tonic-gate 		out(O_ALTFP|O_VERB|O_NONL, "already have waited at least ");
32407c478bd9Sstevel@tonic-gate 		ptree_timeval(O_ALTFP|O_VERB, &fmep->pull);
32417c478bd9Sstevel@tonic-gate 		out(O_ALTFP|O_VERB, NULL);
32427c478bd9Sstevel@tonic-gate 		/* we've waited at least wull already, don't need timer */
32437aec1d6eScindi 		return (0);
32447c478bd9Sstevel@tonic-gate 	}
32457c478bd9Sstevel@tonic-gate 
32467c478bd9Sstevel@tonic-gate 	out(O_ALTFP|O_VERB|O_NONL, " currently ");
32477c478bd9Sstevel@tonic-gate 	if (fmep->wull != 0) {
32487c478bd9Sstevel@tonic-gate 		out(O_ALTFP|O_VERB|O_NONL, "waiting ");
32497c478bd9Sstevel@tonic-gate 		ptree_timeval(O_ALTFP|O_VERB, &fmep->wull);
32507c478bd9Sstevel@tonic-gate 		out(O_ALTFP|O_VERB, NULL);
32517c478bd9Sstevel@tonic-gate 	} else {
32527c478bd9Sstevel@tonic-gate 		out(O_ALTFP|O_VERB|O_NONL, "not waiting");
32537c478bd9Sstevel@tonic-gate 		out(O_ALTFP|O_VERB, NULL);
32547c478bd9Sstevel@tonic-gate 	}
32557c478bd9Sstevel@tonic-gate 
32567c478bd9Sstevel@tonic-gate 	if (fmep->wull != 0)
32577c478bd9Sstevel@tonic-gate 		if (wull >= fmep->wull)
32587c478bd9Sstevel@tonic-gate 			/* New timer would fire later than established timer */
32597aec1d6eScindi 			return (0);
32607c478bd9Sstevel@tonic-gate 
32617aec1d6eScindi 	if (fmep->wull != 0) {
32627c478bd9Sstevel@tonic-gate 		fmd_timer_remove(fmep->hdl, fmep->timer);
32637aec1d6eScindi 	}
32647c478bd9Sstevel@tonic-gate 
32657c478bd9Sstevel@tonic-gate 	fmep->timer = fmd_timer_install(fmep->hdl, (void *)fmep,
32667c478bd9Sstevel@tonic-gate 	    fmep->e0r, wull);
32677c478bd9Sstevel@tonic-gate 	out(O_ALTFP|O_VERB, "timer set, id is %ld", fmep->timer);
32687c478bd9Sstevel@tonic-gate 	fmep->wull = wull;
32697aec1d6eScindi 	return (1);
32707c478bd9Sstevel@tonic-gate }
32717c478bd9Sstevel@tonic-gate 
32727c478bd9Sstevel@tonic-gate void
fme_timer_fired(struct fme * fmep,id_t tid)32737c478bd9Sstevel@tonic-gate fme_timer_fired(struct fme *fmep, id_t tid)
32747c478bd9Sstevel@tonic-gate {
32757c478bd9Sstevel@tonic-gate 	struct fme *ffmep = NULL;
32767c478bd9Sstevel@tonic-gate 
32777c478bd9Sstevel@tonic-gate 	for (ffmep = FMElist; ffmep; ffmep = ffmep->next)
32787c478bd9Sstevel@tonic-gate 		if (ffmep == fmep)
32797c478bd9Sstevel@tonic-gate 			break;
32807c478bd9Sstevel@tonic-gate 
32817c478bd9Sstevel@tonic-gate 	if (ffmep == NULL) {
32827c478bd9Sstevel@tonic-gate 		out(O_WARN, "Timer fired for an FME (%p) not in FMEs list.",
32837c478bd9Sstevel@tonic-gate 		    (void *)fmep);
32847c478bd9Sstevel@tonic-gate 		return;
32857c478bd9Sstevel@tonic-gate 	}
32867c478bd9Sstevel@tonic-gate 
3287b5016cbbSstephh 	out(O_ALTFP|O_VERB, "Timer fired %lx", tid);
32887c478bd9Sstevel@tonic-gate 	fmep->pull = fmep->wull;
32897c478bd9Sstevel@tonic-gate 	fmep->wull = 0;
32907c478bd9Sstevel@tonic-gate 	fmd_buf_write(fmep->hdl, fmep->fmcase,
32917c478bd9Sstevel@tonic-gate 	    WOBUF_PULL, (void *)&fmep->pull, sizeof (fmep->pull));
3292b5016cbbSstephh 
329300d0963fSdilpreet 	fme_eval(fmep, fmep->e0r);
32947c478bd9Sstevel@tonic-gate }
32957c478bd9Sstevel@tonic-gate 
32967c478bd9Sstevel@tonic-gate /*
32977c478bd9Sstevel@tonic-gate  * Preserve the fme's suspect list in its psuspects list, NULLing the
32987c478bd9Sstevel@tonic-gate  * suspects list in the meantime.
32997c478bd9Sstevel@tonic-gate  */
33007c478bd9Sstevel@tonic-gate static void
save_suspects(struct fme * fmep)33017c478bd9Sstevel@tonic-gate save_suspects(struct fme *fmep)
33027c478bd9Sstevel@tonic-gate {
33037c478bd9Sstevel@tonic-gate 	struct event *ep;
33047c478bd9Sstevel@tonic-gate 	struct event *nextep;
33057c478bd9Sstevel@tonic-gate 
33067c478bd9Sstevel@tonic-gate 	/* zero out the previous suspect list */
33077c478bd9Sstevel@tonic-gate 	for (ep = fmep->psuspects; ep; ep = nextep) {
33087c478bd9Sstevel@tonic-gate 		nextep = ep->psuspects;
33097c478bd9Sstevel@tonic-gate 		ep->psuspects = NULL;
33107c478bd9Sstevel@tonic-gate 	}
33117c478bd9Sstevel@tonic-gate 	fmep->psuspects = NULL;
33127c478bd9Sstevel@tonic-gate 
33137c478bd9Sstevel@tonic-gate 	/* zero out the suspect list, copying it to previous suspect list */
33147c478bd9Sstevel@tonic-gate 	fmep->psuspects = fmep->suspects;
33157c478bd9Sstevel@tonic-gate 	for (ep = fmep->suspects; ep; ep = nextep) {
33167c478bd9Sstevel@tonic-gate 		nextep = ep->suspects;
33177c478bd9Sstevel@tonic-gate 		ep->psuspects = ep->suspects;
33187c478bd9Sstevel@tonic-gate 		ep->suspects = NULL;
33197c478bd9Sstevel@tonic-gate 		ep->is_suspect = 0;
33207c478bd9Sstevel@tonic-gate 	}
33217c478bd9Sstevel@tonic-gate 	fmep->suspects = NULL;
33227c478bd9Sstevel@tonic-gate 	fmep->nsuspects = 0;
33237c478bd9Sstevel@tonic-gate }
33247c478bd9Sstevel@tonic-gate 
33257c478bd9Sstevel@tonic-gate /*
33267c478bd9Sstevel@tonic-gate  * Retrieve the fme's suspect list from its psuspects list.
33277c478bd9Sstevel@tonic-gate  */
33287c478bd9Sstevel@tonic-gate static void
restore_suspects(struct fme * fmep)33297c478bd9Sstevel@tonic-gate restore_suspects(struct fme *fmep)
33307c478bd9Sstevel@tonic-gate {
33317c478bd9Sstevel@tonic-gate 	struct event *ep;
33327c478bd9Sstevel@tonic-gate 	struct event *nextep;
33337c478bd9Sstevel@tonic-gate 
33348e7248e5SStephen Hanson 	fmep->nsuspects = 0;
33357c478bd9Sstevel@tonic-gate 	fmep->suspects = fmep->psuspects;
33367c478bd9Sstevel@tonic-gate 	for (ep = fmep->psuspects; ep; ep = nextep) {
33377c478bd9Sstevel@tonic-gate 		fmep->nsuspects++;
33387c478bd9Sstevel@tonic-gate 		nextep = ep->psuspects;
33397c478bd9Sstevel@tonic-gate 		ep->suspects = ep->psuspects;
33407c478bd9Sstevel@tonic-gate 	}
33417c478bd9Sstevel@tonic-gate }
33427c478bd9Sstevel@tonic-gate 
33437c478bd9Sstevel@tonic-gate /*
33447c478bd9Sstevel@tonic-gate  * this is what we use to call the Emrys prototype code instead of main()
33457c478bd9Sstevel@tonic-gate  */
33467c478bd9Sstevel@tonic-gate static void
fme_eval(struct fme * fmep,fmd_event_t * ffep)33477c478bd9Sstevel@tonic-gate fme_eval(struct fme *fmep, fmd_event_t *ffep)
33487c478bd9Sstevel@tonic-gate {
33497c478bd9Sstevel@tonic-gate 	struct event *ep;
33507c478bd9Sstevel@tonic-gate 	unsigned long long my_delay = TIMEVAL_EVENTUALLY;
3351b7d3956bSstephh 	struct rsl *srl = NULL;
3352b7d3956bSstephh 	struct rsl *srl2 = NULL;
3353b7d3956bSstephh 	int mess_zero_count;
3354b7d3956bSstephh 	int rpcnt;
33557c478bd9Sstevel@tonic-gate 
33567c478bd9Sstevel@tonic-gate 	save_suspects(fmep);
33577c478bd9Sstevel@tonic-gate 
3358b5016cbbSstephh 	out(O_ALTFP, "Evaluate FME %d", fmep->id);
33597c478bd9Sstevel@tonic-gate 	indent_set("  ");
33607c478bd9Sstevel@tonic-gate 
33617aec1d6eScindi 	lut_walk(fmep->eventtree, (lut_cb)clear_arrows, (void *)fmep);
33627aec1d6eScindi 	fmep->state = hypothesise(fmep, fmep->e0, fmep->ull, &my_delay);
33637c478bd9Sstevel@tonic-gate 
3364b5016cbbSstephh 	out(O_ALTFP|O_NONL, "FME%d state: %s, suspect list:", fmep->id,
33657c478bd9Sstevel@tonic-gate 	    fme_state2str(fmep->state));
33667c478bd9Sstevel@tonic-gate 	for (ep = fmep->suspects; ep; ep = ep->suspects) {
3367b5016cbbSstephh 		out(O_ALTFP|O_NONL, " ");
3368b5016cbbSstephh 		itree_pevent_brief(O_ALTFP|O_NONL, ep);
33697c478bd9Sstevel@tonic-gate 	}
3370b5016cbbSstephh 	out(O_ALTFP, NULL);
33717c478bd9Sstevel@tonic-gate 
33727c478bd9Sstevel@tonic-gate 	switch (fmep->state) {
33737c478bd9Sstevel@tonic-gate 	case FME_CREDIBLE:
33747c478bd9Sstevel@tonic-gate 		print_suspects(SLNEW, fmep);
33757c478bd9Sstevel@tonic-gate 		(void) upsets_eval(fmep, ffep);
3376d96ce684Sstephh 
3377d96ce684Sstephh 		/*
3378d96ce684Sstephh 		 * we may have already posted suspects in upsets_eval() which
3379d96ce684Sstephh 		 * can recurse into fme_eval() again. If so then just return.
3380d96ce684Sstephh 		 */
3381d96ce684Sstephh 		if (fmep->posted_suspects)
3382d96ce684Sstephh 			return;
3383d96ce684Sstephh 
3384b7d3956bSstephh 		stats_counter_bump(fmep->diags);
3385b7d3956bSstephh 		rpcnt = fmep->nsuspects;
3386b7d3956bSstephh 		save_suspects(fmep);
3387b7d3956bSstephh 
3388b7d3956bSstephh 		/*
3389b7d3956bSstephh 		 * create two lists, one for "message=1" faults and one for
3390b7d3956bSstephh 		 * "message=0" faults. If we have a mixture we will generate
3391b7d3956bSstephh 		 * two separate suspect lists.
3392b7d3956bSstephh 		 */
3393b7d3956bSstephh 		srl = MALLOC(rpcnt * sizeof (struct rsl));
3394b7d3956bSstephh 		bzero(srl, rpcnt * sizeof (struct rsl));
3395b7d3956bSstephh 		srl2 = MALLOC(rpcnt * sizeof (struct rsl));
3396b7d3956bSstephh 		bzero(srl2, rpcnt * sizeof (struct rsl));
33978e7248e5SStephen Hanson 		mess_zero_count = trim_suspects(fmep, srl, srl2, ffep);
3398b7d3956bSstephh 
3399b7d3956bSstephh 		/*
3400b7d3956bSstephh 		 * If the resulting suspect list has no members, we're
3401b7d3956bSstephh 		 * done so simply close the case. Otherwise sort and publish.
3402b7d3956bSstephh 		 */
3403b7d3956bSstephh 		if (fmep->nsuspects == 0 && mess_zero_count == 0) {
3404b7d3956bSstephh 			out(O_ALTFP,
3405b7d3956bSstephh 			    "[FME%d, case %s (all suspects are upsets)]",
3406b7d3956bSstephh 			    fmep->id, fmd_case_uuid(fmep->hdl, fmep->fmcase));
3407b7d3956bSstephh 			fmd_case_close(fmep->hdl, fmep->fmcase);
3408b7d3956bSstephh 		} else if (fmep->nsuspects != 0 && mess_zero_count == 0) {
3409b7d3956bSstephh 			publish_suspects(fmep, srl);
3410b7d3956bSstephh 			out(O_ALTFP, "[solving FME%d, case %s]", fmep->id,
3411b7d3956bSstephh 			    fmd_case_uuid(fmep->hdl, fmep->fmcase));
3412b7d3956bSstephh 			fmd_case_solve(fmep->hdl, fmep->fmcase);
3413b7d3956bSstephh 		} else if (fmep->nsuspects == 0 && mess_zero_count != 0) {
3414b7d3956bSstephh 			fmep->nsuspects = mess_zero_count;
3415b7d3956bSstephh 			publish_suspects(fmep, srl2);
3416b7d3956bSstephh 			out(O_ALTFP, "[solving FME%d, case %s]", fmep->id,
3417b7d3956bSstephh 			    fmd_case_uuid(fmep->hdl, fmep->fmcase));
3418b7d3956bSstephh 			fmd_case_solve(fmep->hdl, fmep->fmcase);
3419b7d3956bSstephh 		} else {
3420b7d3956bSstephh 			struct event *obsp;
3421b7d3956bSstephh 			struct fme *nfmep;
3422b7d3956bSstephh 
3423b7d3956bSstephh 			publish_suspects(fmep, srl);
3424b7d3956bSstephh 			out(O_ALTFP, "[solving FME%d, case %s]", fmep->id,
3425b7d3956bSstephh 			    fmd_case_uuid(fmep->hdl, fmep->fmcase));
3426b7d3956bSstephh 			fmd_case_solve(fmep->hdl, fmep->fmcase);
3427b7d3956bSstephh 
3428b7d3956bSstephh 			/*
3429b7d3956bSstephh 			 * Got both message=0 and message=1 so create a
3430b7d3956bSstephh 			 * duplicate case. Also need a temporary duplicate fme
3431b7d3956bSstephh 			 * structure for use by publish_suspects().
3432b7d3956bSstephh 			 */
3433b7d3956bSstephh 			nfmep = alloc_fme();
3434b7d3956bSstephh 			nfmep->id =  Nextid++;
3435b7d3956bSstephh 			nfmep->hdl = fmep->hdl;
3436b7d3956bSstephh 			nfmep->nsuspects = mess_zero_count;
3437b7d3956bSstephh 			nfmep->fmcase = fmd_case_open(fmep->hdl, NULL);
3438b7d3956bSstephh 			out(O_ALTFP|O_STAMP,
3439b7d3956bSstephh 			    "[creating parallel FME%d, case %s]", nfmep->id,
3440b7d3956bSstephh 			    fmd_case_uuid(nfmep->hdl, nfmep->fmcase));
3441b7d3956bSstephh 			Open_fme_count++;
3442b7d3956bSstephh 			if (ffep) {
3443b7d3956bSstephh 				fmd_case_setprincipal(nfmep->hdl,
3444b7d3956bSstephh 				    nfmep->fmcase, ffep);
3445b7d3956bSstephh 				fmd_case_add_ereport(nfmep->hdl,
3446b7d3956bSstephh 				    nfmep->fmcase, ffep);
3447b7d3956bSstephh 			}
3448b7d3956bSstephh 			for (obsp = fmep->observations; obsp;
3449b7d3956bSstephh 			    obsp = obsp->observations)
3450b7d3956bSstephh 				if (obsp->ffep && obsp->ffep != ffep)
3451b7d3956bSstephh 					fmd_case_add_ereport(nfmep->hdl,
3452b7d3956bSstephh 					    nfmep->fmcase, obsp->ffep);
3453b7d3956bSstephh 
3454b7d3956bSstephh 			publish_suspects(nfmep, srl2);
3455b7d3956bSstephh 			out(O_ALTFP, "[solving FME%d, case %s]", nfmep->id,
3456b7d3956bSstephh 			    fmd_case_uuid(nfmep->hdl, nfmep->fmcase));
3457b7d3956bSstephh 			fmd_case_solve(nfmep->hdl, nfmep->fmcase);
3458b7d3956bSstephh 			FREE(nfmep);
3459b7d3956bSstephh 		}
3460b7d3956bSstephh 		FREE(srl);
3461b7d3956bSstephh 		FREE(srl2);
3462b7d3956bSstephh 		restore_suspects(fmep);
3463b7d3956bSstephh 
34647c478bd9Sstevel@tonic-gate 		fmep->posted_suspects = 1;
34657c478bd9Sstevel@tonic-gate 		fmd_buf_write(fmep->hdl, fmep->fmcase,
34667c478bd9Sstevel@tonic-gate 		    WOBUF_POSTD,
34677c478bd9Sstevel@tonic-gate 		    (void *)&fmep->posted_suspects,
34687c478bd9Sstevel@tonic-gate 		    sizeof (fmep->posted_suspects));
3469d96ce684Sstephh 
3470d96ce684Sstephh 		/*
3471d96ce684Sstephh 		 * Now the suspects have been posted, we can clear up
3472d96ce684Sstephh 		 * the instance tree as we won't be looking at it again.
3473d96ce684Sstephh 		 * Also cancel the timer as the case is now solved.
3474d96ce684Sstephh 		 */
3475d96ce684Sstephh 		if (fmep->wull != 0) {
3476d96ce684Sstephh 			fmd_timer_remove(fmep->hdl, fmep->timer);
3477d96ce684Sstephh 			fmep->wull = 0;
34787c478bd9Sstevel@tonic-gate 		}
34797c478bd9Sstevel@tonic-gate 		break;
34807c478bd9Sstevel@tonic-gate 
34817c478bd9Sstevel@tonic-gate 	case FME_WAIT:
34827c478bd9Sstevel@tonic-gate 		ASSERT(my_delay > fmep->ull);
34837aec1d6eScindi 		(void) fme_set_timer(fmep, my_delay);
34847c478bd9Sstevel@tonic-gate 		print_suspects(SLWAIT, fmep);
3485b5016cbbSstephh 		itree_prune(fmep->eventtree);
3486b5016cbbSstephh 		return;
34877c478bd9Sstevel@tonic-gate 
34887c478bd9Sstevel@tonic-gate 	case FME_DISPROVED:
34897c478bd9Sstevel@tonic-gate 		print_suspects(SLDISPROVED, fmep);
349032d4e834STarik Soydan 		Undiag_reason = UD_VAL_UNSOLVD;
34917c478bd9Sstevel@tonic-gate 		fme_undiagnosable(fmep);
34927c478bd9Sstevel@tonic-gate 		break;
34937c478bd9Sstevel@tonic-gate 	}
34947c478bd9Sstevel@tonic-gate 
34959f8ca725Sstephh 	itree_free(fmep->eventtree);
34969f8ca725Sstephh 	fmep->eventtree = NULL;
3497e5ba14ffSstephh 	structconfig_free(fmep->config);
3498e5ba14ffSstephh 	fmep->config = NULL;
3499b5016cbbSstephh 	destroy_fme_bufs(fmep);
35009f8ca725Sstephh }
35017c478bd9Sstevel@tonic-gate 
35027c478bd9Sstevel@tonic-gate static void indent(void);
35037c478bd9Sstevel@tonic-gate static int triggered(struct fme *fmep, struct event *ep, int mark);
35047c478bd9Sstevel@tonic-gate static enum fme_state effects_test(struct fme *fmep,
35057aec1d6eScindi     struct event *fault_event, unsigned long long at_latest_by,
35067aec1d6eScindi     unsigned long long *pdelay);
35077c478bd9Sstevel@tonic-gate static enum fme_state requirements_test(struct fme *fmep, struct event *ep,
35087aec1d6eScindi     unsigned long long at_latest_by, unsigned long long *pdelay);
35097c478bd9Sstevel@tonic-gate static enum fme_state causes_test(struct fme *fmep, struct event *ep,
35107c478bd9Sstevel@tonic-gate     unsigned long long at_latest_by, unsigned long long *pdelay);
35117c478bd9Sstevel@tonic-gate 
35127c478bd9Sstevel@tonic-gate static int
checkconstraints(struct fme * fmep,struct arrow * arrowp)35137aec1d6eScindi checkconstraints(struct fme *fmep, struct arrow *arrowp)
35147aec1d6eScindi {
35157aec1d6eScindi 	struct constraintlist *ctp;
35167aec1d6eScindi 	struct evalue value;
3517b5016cbbSstephh 	char *sep = "";
35187aec1d6eScindi 
35197aec1d6eScindi 	if (arrowp->forever_false) {
35207aec1d6eScindi 		indent();
35217aec1d6eScindi 		out(O_ALTFP|O_VERB|O_NONL, "  Forever false constraint: ");
35227aec1d6eScindi 		for (ctp = arrowp->constraints; ctp != NULL; ctp = ctp->next) {
35237aec1d6eScindi 			out(O_ALTFP|O_VERB|O_NONL, sep);
35247aec1d6eScindi 			ptree(O_ALTFP|O_VERB|O_NONL, ctp->cnode, 1, 0);
35257aec1d6eScindi 			sep = ", ";
35267aec1d6eScindi 		}
35277aec1d6eScindi 		out(O_ALTFP|O_VERB, NULL);
35287aec1d6eScindi 		return (0);
35297aec1d6eScindi 	}
3530b5016cbbSstephh 	if (arrowp->forever_true) {
3531b5016cbbSstephh 		indent();
3532b5016cbbSstephh 		out(O_ALTFP|O_VERB|O_NONL, "  Forever true constraint: ");
3533b5016cbbSstephh 		for (ctp = arrowp->constraints; ctp != NULL; ctp = ctp->next) {
3534b5016cbbSstephh 			out(O_ALTFP|O_VERB|O_NONL, sep);
3535b5016cbbSstephh 			ptree(O_ALTFP|O_VERB|O_NONL, ctp->cnode, 1, 0);
3536b5016cbbSstephh 			sep = ", ";
3537b5016cbbSstephh 		}
3538b5016cbbSstephh 		out(O_ALTFP|O_VERB, NULL);
3539b5016cbbSstephh 		return (1);
3540b5016cbbSstephh 	}
35417aec1d6eScindi 
35427aec1d6eScindi 	for (ctp = arrowp->constraints; ctp != NULL; ctp = ctp->next) {
35437aec1d6eScindi 		if (eval_expr(ctp->cnode, NULL, NULL,
3544e5ba14ffSstephh 		    &fmep->globals, fmep->config,
35457aec1d6eScindi 		    arrowp, 0, &value)) {
35467aec1d6eScindi 			/* evaluation successful */
35477aec1d6eScindi 			if (value.t == UNDEFINED || value.v == 0) {
35487aec1d6eScindi 				/* known false */
35497aec1d6eScindi 				arrowp->forever_false = 1;
35507aec1d6eScindi 				indent();
35517aec1d6eScindi 				out(O_ALTFP|O_VERB|O_NONL,
35527aec1d6eScindi 				    "  False constraint: ");
35537aec1d6eScindi 				ptree(O_ALTFP|O_VERB|O_NONL, ctp->cnode, 1, 0);
35547aec1d6eScindi 				out(O_ALTFP|O_VERB, NULL);
35557aec1d6eScindi 				return (0);
35567aec1d6eScindi 			}
35577aec1d6eScindi 		} else {
35587aec1d6eScindi 			/* evaluation unsuccessful -- unknown value */
35597aec1d6eScindi 			indent();
35607aec1d6eScindi 			out(O_ALTFP|O_VERB|O_NONL,
35617aec1d6eScindi 			    "  Deferred constraint: ");
35627aec1d6eScindi 			ptree(O_ALTFP|O_VERB|O_NONL, ctp->cnode, 1, 0);
35637aec1d6eScindi 			out(O_ALTFP|O_VERB, NULL);
3564b5016cbbSstephh 			return (1);
35657aec1d6eScindi 		}
35667aec1d6eScindi 	}
35677aec1d6eScindi 	/* known true */
3568b5016cbbSstephh 	arrowp->forever_true = 1;
3569b5016cbbSstephh 	indent();
3570b5016cbbSstephh 	out(O_ALTFP|O_VERB|O_NONL, "  True constraint: ");
3571b5016cbbSstephh 	for (ctp = arrowp->constraints; ctp != NULL; ctp = ctp->next) {
3572b5016cbbSstephh 		out(O_ALTFP|O_VERB|O_NONL, sep);
3573b5016cbbSstephh 		ptree(O_ALTFP|O_VERB|O_NONL, ctp->cnode, 1, 0);
3574b5016cbbSstephh 		sep = ", ";
3575b5016cbbSstephh 	}
3576b5016cbbSstephh 	out(O_ALTFP|O_VERB, NULL);
35777aec1d6eScindi 	return (1);
35787aec1d6eScindi }
35797aec1d6eScindi 
35807aec1d6eScindi static int
triggered(struct fme * fmep,struct event * ep,int mark)35817c478bd9Sstevel@tonic-gate triggered(struct fme *fmep, struct event *ep, int mark)
35827c478bd9Sstevel@tonic-gate {
35837c478bd9Sstevel@tonic-gate 	struct bubble *bp;
35847c478bd9Sstevel@tonic-gate 	struct arrowlist *ap;
35857c478bd9Sstevel@tonic-gate 	int count = 0;
35867c478bd9Sstevel@tonic-gate 
35877c478bd9Sstevel@tonic-gate 	stats_counter_bump(fmep->Tcallcount);
35887c478bd9Sstevel@tonic-gate 	for (bp = itree_next_bubble(ep, NULL); bp;
35897c478bd9Sstevel@tonic-gate 	    bp = itree_next_bubble(ep, bp)) {
35907c478bd9Sstevel@tonic-gate 		if (bp->t != B_TO)
35917c478bd9Sstevel@tonic-gate 			continue;
35927c478bd9Sstevel@tonic-gate 		for (ap = itree_next_arrow(bp, NULL); ap;
35937c478bd9Sstevel@tonic-gate 		    ap = itree_next_arrow(bp, ap)) {
35947c478bd9Sstevel@tonic-gate 			/* check count of marks against K in the bubble */
35957aec1d6eScindi 			if ((ap->arrowp->mark & mark) &&
35967c478bd9Sstevel@tonic-gate 			    ++count >= bp->nork)
35977c478bd9Sstevel@tonic-gate 				return (1);
35987c478bd9Sstevel@tonic-gate 		}
35997c478bd9Sstevel@tonic-gate 	}
36007c478bd9Sstevel@tonic-gate 	return (0);
36017c478bd9Sstevel@tonic-gate }
36027c478bd9Sstevel@tonic-gate 
36037aec1d6eScindi static int
mark_arrows(struct fme * fmep,struct event * ep,int mark,unsigned long long at_latest_by,unsigned long long * pdelay,int keep)36047aec1d6eScindi mark_arrows(struct fme *fmep, struct event *ep, int mark,
360500d0963fSdilpreet     unsigned long long at_latest_by, unsigned long long *pdelay, int keep)
36067c478bd9Sstevel@tonic-gate {
36077c478bd9Sstevel@tonic-gate 	struct bubble *bp;
36087c478bd9Sstevel@tonic-gate 	struct arrowlist *ap;
36097aec1d6eScindi 	unsigned long long overall_delay = TIMEVAL_EVENTUALLY;
36107aec1d6eScindi 	unsigned long long my_delay;
36117aec1d6eScindi 	enum fme_state result;
36127aec1d6eScindi 	int retval = 0;
36137c478bd9Sstevel@tonic-gate 
36147c478bd9Sstevel@tonic-gate 	for (bp = itree_next_bubble(ep, NULL); bp;
36157c478bd9Sstevel@tonic-gate 	    bp = itree_next_bubble(ep, bp)) {
36167c478bd9Sstevel@tonic-gate 		if (bp->t != B_FROM)
36177c478bd9Sstevel@tonic-gate 			continue;
36187c478bd9Sstevel@tonic-gate 		stats_counter_bump(fmep->Marrowcount);
36197c478bd9Sstevel@tonic-gate 		for (ap = itree_next_arrow(bp, NULL); ap;
36207c478bd9Sstevel@tonic-gate 		    ap = itree_next_arrow(bp, ap)) {
36217aec1d6eScindi 			struct event *ep2 = ap->arrowp->head->myevent;
36227c478bd9Sstevel@tonic-gate 			/*
36237aec1d6eScindi 			 * if we're clearing marks, we can avoid doing
36247aec1d6eScindi 			 * all that work evaluating constraints.
36257c478bd9Sstevel@tonic-gate 			 */
36267aec1d6eScindi 			if (mark == 0) {
3627b5016cbbSstephh 				if (ap->arrowp->arrow_marked == 0)
3628b5016cbbSstephh 					continue;
3629b5016cbbSstephh 				ap->arrowp->arrow_marked = 0;
36307aec1d6eScindi 				ap->arrowp->mark &= ~EFFECTS_COUNTER;
363100d0963fSdilpreet 				if (keep && (ep2->cached_state &
363200d0963fSdilpreet 				    (WAIT_EFFECT|CREDIBLE_EFFECT|PARENT_WAIT)))
363300d0963fSdilpreet 					ep2->keep_in_tree = 1;
36347aec1d6eScindi 				ep2->cached_state &=
36357aec1d6eScindi 				    ~(WAIT_EFFECT|CREDIBLE_EFFECT|PARENT_WAIT);
363600d0963fSdilpreet 				(void) mark_arrows(fmep, ep2, mark, 0, NULL,
363700d0963fSdilpreet 				    keep);
36387aec1d6eScindi 				continue;
36397c478bd9Sstevel@tonic-gate 			}
3640b5016cbbSstephh 			ap->arrowp->arrow_marked = 1;
36417aec1d6eScindi 			if (ep2->cached_state & REQMNTS_DISPROVED) {
36427c478bd9Sstevel@tonic-gate 				indent();
36437c478bd9Sstevel@tonic-gate 				out(O_ALTFP|O_VERB|O_NONL,
36447aec1d6eScindi 				    "  ALREADY DISPROVED ");
36457aec1d6eScindi 				itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep2);
36467c478bd9Sstevel@tonic-gate 				out(O_ALTFP|O_VERB, NULL);
36477c478bd9Sstevel@tonic-gate 				continue;
36487c478bd9Sstevel@tonic-gate 			}
36497aec1d6eScindi 			if (ep2->cached_state & WAIT_EFFECT) {
36507aec1d6eScindi 				indent();
36517aec1d6eScindi 				out(O_ALTFP|O_VERB|O_NONL,
36527aec1d6eScindi 				    "  ALREADY EFFECTS WAIT ");
36537aec1d6eScindi 				itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep2);
36547aec1d6eScindi 				out(O_ALTFP|O_VERB, NULL);
36557aec1d6eScindi 				continue;
36567aec1d6eScindi 			}
36577aec1d6eScindi 			if (ep2->cached_state & CREDIBLE_EFFECT) {
36587aec1d6eScindi 				indent();
36597aec1d6eScindi 				out(O_ALTFP|O_VERB|O_NONL,
36607aec1d6eScindi 				    "  ALREADY EFFECTS CREDIBLE ");
36617aec1d6eScindi 				itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep2);
36627aec1d6eScindi 				out(O_ALTFP|O_VERB, NULL);
36637aec1d6eScindi 				continue;
36647aec1d6eScindi 			}
36657aec1d6eScindi 			if ((ep2->cached_state & PARENT_WAIT) &&
36667aec1d6eScindi 			    (mark & PARENT_WAIT)) {
36677aec1d6eScindi 				indent();
36687aec1d6eScindi 				out(O_ALTFP|O_VERB|O_NONL,
36697aec1d6eScindi 				    "  ALREADY PARENT EFFECTS WAIT ");
36707aec1d6eScindi 				itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep2);
36717aec1d6eScindi 				out(O_ALTFP|O_VERB, NULL);
36727aec1d6eScindi 				continue;
36737aec1d6eScindi 			}
36747aec1d6eScindi 			platform_set_payloadnvp(ep2->nvp);
367500d0963fSdilpreet 			if (checkconstraints(fmep, ap->arrowp) == 0) {
36767aec1d6eScindi 				platform_set_payloadnvp(NULL);
36777aec1d6eScindi 				indent();
36787aec1d6eScindi 				out(O_ALTFP|O_VERB|O_NONL,
36797aec1d6eScindi 				    "  CONSTRAINTS FAIL ");
36807aec1d6eScindi 				itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep2);
36817aec1d6eScindi 				out(O_ALTFP|O_VERB, NULL);
36827aec1d6eScindi 				continue;
36837aec1d6eScindi 			}
36847aec1d6eScindi 			platform_set_payloadnvp(NULL);
36857aec1d6eScindi 			ap->arrowp->mark |= EFFECTS_COUNTER;
36867aec1d6eScindi 			if (!triggered(fmep, ep2, EFFECTS_COUNTER)) {
36877aec1d6eScindi 				indent();
36887aec1d6eScindi 				out(O_ALTFP|O_VERB|O_NONL,
36897aec1d6eScindi 				    "  K-COUNT NOT YET MET ");
36907aec1d6eScindi 				itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep2);
36917aec1d6eScindi 				out(O_ALTFP|O_VERB, NULL);
36927aec1d6eScindi 				continue;
36937aec1d6eScindi 			}
36947aec1d6eScindi 			ep2->cached_state &= ~PARENT_WAIT;
3695d96ce684Sstephh 			/*
3696d96ce684Sstephh 			 * if we've reached an ereport and no propagation time
3697d96ce684Sstephh 			 * is specified, use the Hesitate value
3698d96ce684Sstephh 			 */
3699d96ce684Sstephh 			if (ep2->t == N_EREPORT && at_latest_by == 0ULL &&
3700d96ce684Sstephh 			    ap->arrowp->maxdelay == 0ULL) {
3701d96ce684Sstephh 				out(O_ALTFP|O_VERB|O_NONL, "  default wait ");
3702d96ce684Sstephh 				itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep2);
3703d96ce684Sstephh 				out(O_ALTFP|O_VERB, NULL);
3704b5016cbbSstephh 				result = requirements_test(fmep, ep2, Hesitate,
3705b5016cbbSstephh 				    &my_delay);
3706d96ce684Sstephh 			} else {
3707d96ce684Sstephh 				result = requirements_test(fmep, ep2,
3708d96ce684Sstephh 				    at_latest_by + ap->arrowp->maxdelay,
3709d96ce684Sstephh 				    &my_delay);
3710d96ce684Sstephh 			}
37117aec1d6eScindi 			if (result == FME_WAIT) {
37127aec1d6eScindi 				retval = WAIT_EFFECT;
37137aec1d6eScindi 				if (overall_delay > my_delay)
37147aec1d6eScindi 					overall_delay = my_delay;
37157aec1d6eScindi 				ep2->cached_state |= WAIT_EFFECT;
37167aec1d6eScindi 				indent();
37177aec1d6eScindi 				out(O_ALTFP|O_VERB|O_NONL, "  EFFECTS WAIT ");
37187aec1d6eScindi 				itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep2);
37197aec1d6eScindi 				out(O_ALTFP|O_VERB, NULL);
37207aec1d6eScindi 				indent_push("  E");
37217aec1d6eScindi 				if (mark_arrows(fmep, ep2, PARENT_WAIT,
372200d0963fSdilpreet 				    at_latest_by, &my_delay, 0) ==
372300d0963fSdilpreet 				    WAIT_EFFECT) {
37247aec1d6eScindi 					retval = WAIT_EFFECT;
37257aec1d6eScindi 					if (overall_delay > my_delay)
37267aec1d6eScindi 						overall_delay = my_delay;
37277aec1d6eScindi 				}
37287aec1d6eScindi 				indent_pop();
37297aec1d6eScindi 			} else if (result == FME_DISPROVED) {
37307aec1d6eScindi 				indent();
37317aec1d6eScindi 				out(O_ALTFP|O_VERB|O_NONL,
37327aec1d6eScindi 				    "  EFFECTS DISPROVED ");
37337aec1d6eScindi 				itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep2);
37347aec1d6eScindi 				out(O_ALTFP|O_VERB, NULL);
37357aec1d6eScindi 			} else {
37367aec1d6eScindi 				ep2->cached_state |= mark;
37377aec1d6eScindi 				indent();
37387aec1d6eScindi 				if (mark == CREDIBLE_EFFECT)
37397aec1d6eScindi 					out(O_ALTFP|O_VERB|O_NONL,
37407aec1d6eScindi 					    "  EFFECTS CREDIBLE ");
37417aec1d6eScindi 				else
37427aec1d6eScindi 					out(O_ALTFP|O_VERB|O_NONL,
37437aec1d6eScindi 					    "  PARENT EFFECTS WAIT ");
37447aec1d6eScindi 				itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep2);
37457aec1d6eScindi 				out(O_ALTFP|O_VERB, NULL);
37467aec1d6eScindi 				indent_push("  E");
37477aec1d6eScindi 				if (mark_arrows(fmep, ep2, mark, at_latest_by,
374800d0963fSdilpreet 				    &my_delay, 0) == WAIT_EFFECT) {
37497aec1d6eScindi 					retval = WAIT_EFFECT;
37507aec1d6eScindi 					if (overall_delay > my_delay)
37517aec1d6eScindi 						overall_delay = my_delay;
37527aec1d6eScindi 				}
37537aec1d6eScindi 				indent_pop();
37547c478bd9Sstevel@tonic-gate 			}
37557c478bd9Sstevel@tonic-gate 		}
37567c478bd9Sstevel@tonic-gate 	}
37577aec1d6eScindi 	if (retval == WAIT_EFFECT)
37587aec1d6eScindi 		*pdelay = overall_delay;
37597aec1d6eScindi 	return (retval);
37607c478bd9Sstevel@tonic-gate }
37617c478bd9Sstevel@tonic-gate 
37627c478bd9Sstevel@tonic-gate static enum fme_state
effects_test(struct fme * fmep,struct event * fault_event,unsigned long long at_latest_by,unsigned long long * pdelay)37637aec1d6eScindi effects_test(struct fme *fmep, struct event *fault_event,
37647aec1d6eScindi     unsigned long long at_latest_by, unsigned long long *pdelay)
37657c478bd9Sstevel@tonic-gate {
37667c478bd9Sstevel@tonic-gate 	struct event *error_event;
37677c478bd9Sstevel@tonic-gate 	enum fme_state return_value = FME_CREDIBLE;
37687aec1d6eScindi 	unsigned long long overall_delay = TIMEVAL_EVENTUALLY;
37697aec1d6eScindi 	unsigned long long my_delay;
37707c478bd9Sstevel@tonic-gate 
37717c478bd9Sstevel@tonic-gate 	stats_counter_bump(fmep->Ecallcount);
37727c478bd9Sstevel@tonic-gate 	indent_push("  E");
37737c478bd9Sstevel@tonic-gate 	indent();
37747c478bd9Sstevel@tonic-gate 	out(O_ALTFP|O_VERB|O_NONL, "->");
37757c478bd9Sstevel@tonic-gate 	itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, fault_event);
37767c478bd9Sstevel@tonic-gate 	out(O_ALTFP|O_VERB, NULL);
37777c478bd9Sstevel@tonic-gate 
3778d96ce684Sstephh 	if (mark_arrows(fmep, fault_event, CREDIBLE_EFFECT, at_latest_by,
3779d96ce684Sstephh 	    &my_delay, 0) == WAIT_EFFECT) {
3780d96ce684Sstephh 		return_value = FME_WAIT;
3781d96ce684Sstephh 		if (overall_delay > my_delay)
3782d96ce684Sstephh 			overall_delay = my_delay;
3783d96ce684Sstephh 	}
37847c478bd9Sstevel@tonic-gate 	for (error_event = fmep->observations;
37857c478bd9Sstevel@tonic-gate 	    error_event; error_event = error_event->observations) {
37867c478bd9Sstevel@tonic-gate 		indent();
37877c478bd9Sstevel@tonic-gate 		out(O_ALTFP|O_VERB|O_NONL, " ");
37887c478bd9Sstevel@tonic-gate 		itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, error_event);
37897aec1d6eScindi 		if (!(error_event->cached_state & CREDIBLE_EFFECT)) {
37907aec1d6eScindi 			if (error_event->cached_state &
37917aec1d6eScindi 			    (PARENT_WAIT|WAIT_EFFECT)) {
37927aec1d6eScindi 				out(O_ALTFP|O_VERB, " NOT YET triggered");
37937aec1d6eScindi 				continue;
37947aec1d6eScindi 			}
37957c478bd9Sstevel@tonic-gate 			return_value = FME_DISPROVED;
37967c478bd9Sstevel@tonic-gate 			out(O_ALTFP|O_VERB, " NOT triggered");
37977c478bd9Sstevel@tonic-gate 			break;
37987c478bd9Sstevel@tonic-gate 		} else {
37997c478bd9Sstevel@tonic-gate 			out(O_ALTFP|O_VERB, " triggered");
38007c478bd9Sstevel@tonic-gate 		}
38017c478bd9Sstevel@tonic-gate 	}
380200d0963fSdilpreet 	if (return_value == FME_DISPROVED) {
380300d0963fSdilpreet 		(void) mark_arrows(fmep, fault_event, 0, 0, NULL, 0);
380400d0963fSdilpreet 	} else {
380500d0963fSdilpreet 		fault_event->keep_in_tree = 1;
380600d0963fSdilpreet 		(void) mark_arrows(fmep, fault_event, 0, 0, NULL, 1);
380700d0963fSdilpreet 	}
38087c478bd9Sstevel@tonic-gate 
38097c478bd9Sstevel@tonic-gate 	indent();
38107aec1d6eScindi 	out(O_ALTFP|O_VERB|O_NONL, "<-EFFECTS %s ",
38117aec1d6eScindi 	    fme_state2str(return_value));
38127c478bd9Sstevel@tonic-gate 	itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, fault_event);
38137c478bd9Sstevel@tonic-gate 	out(O_ALTFP|O_VERB, NULL);
38147c478bd9Sstevel@tonic-gate 	indent_pop();
38157aec1d6eScindi 	if (return_value == FME_WAIT)
38167aec1d6eScindi 		*pdelay = overall_delay;
38177c478bd9Sstevel@tonic-gate 	return (return_value);
38187c478bd9Sstevel@tonic-gate }
38197c478bd9Sstevel@tonic-gate 
38207c478bd9Sstevel@tonic-gate static enum fme_state
requirements_test(struct fme * fmep,struct event * ep,unsigned long long at_latest_by,unsigned long long * pdelay)38217c478bd9Sstevel@tonic-gate requirements_test(struct fme *fmep, struct event *ep,
38227aec1d6eScindi     unsigned long long at_latest_by, unsigned long long *pdelay)
38237c478bd9Sstevel@tonic-gate {
38247c478bd9Sstevel@tonic-gate 	int waiting_events;
38257c478bd9Sstevel@tonic-gate 	int credible_events;
38267aec1d6eScindi 	int deferred_events;
38277c478bd9Sstevel@tonic-gate 	enum fme_state return_value = FME_CREDIBLE;
38287c478bd9Sstevel@tonic-gate 	unsigned long long overall_delay = TIMEVAL_EVENTUALLY;
38297c478bd9Sstevel@tonic-gate 	unsigned long long arrow_delay;
38307c478bd9Sstevel@tonic-gate 	unsigned long long my_delay;
38317c478bd9Sstevel@tonic-gate 	struct event *ep2;
38327c478bd9Sstevel@tonic-gate 	struct bubble *bp;
38337c478bd9Sstevel@tonic-gate 	struct arrowlist *ap;
38347c478bd9Sstevel@tonic-gate 
38357aec1d6eScindi 	if (ep->cached_state & REQMNTS_CREDIBLE) {
38367aec1d6eScindi 		indent();
38377aec1d6eScindi 		out(O_ALTFP|O_VERB|O_NONL, "  REQMNTS ALREADY CREDIBLE ");
38387aec1d6eScindi 		itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
38397aec1d6eScindi 		out(O_ALTFP|O_VERB, NULL);
38407aec1d6eScindi 		return (FME_CREDIBLE);
38417aec1d6eScindi 	}
38427aec1d6eScindi 	if (ep->cached_state & REQMNTS_DISPROVED) {
38437aec1d6eScindi 		indent();
38447aec1d6eScindi 		out(O_ALTFP|O_VERB|O_NONL, "  REQMNTS ALREADY DISPROVED ");
38457aec1d6eScindi 		itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
38467aec1d6eScindi 		out(O_ALTFP|O_VERB, NULL);
38477aec1d6eScindi 		return (FME_DISPROVED);
38487aec1d6eScindi 	}
38497aec1d6eScindi 	if (ep->cached_state & REQMNTS_WAIT) {
38507aec1d6eScindi 		indent();
38517aec1d6eScindi 		*pdelay = ep->cached_delay;
38527aec1d6eScindi 		out(O_ALTFP|O_VERB|O_NONL, "  REQMNTS ALREADY WAIT ");
38537aec1d6eScindi 		itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
38547aec1d6eScindi 		out(O_ALTFP|O_VERB|O_NONL, ", wait for: ");
38557aec1d6eScindi 		ptree_timeval(O_ALTFP|O_VERB|O_NONL, &at_latest_by);
38567aec1d6eScindi 		out(O_ALTFP|O_VERB, NULL);
38577aec1d6eScindi 		return (FME_WAIT);
38587aec1d6eScindi 	}
38597c478bd9Sstevel@tonic-gate 	stats_counter_bump(fmep->Rcallcount);
38607c478bd9Sstevel@tonic-gate 	indent_push("  R");
38617c478bd9Sstevel@tonic-gate 	indent();
38627c478bd9Sstevel@tonic-gate 	out(O_ALTFP|O_VERB|O_NONL, "->");
38637c478bd9Sstevel@tonic-gate 	itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
38647c478bd9Sstevel@tonic-gate 	out(O_ALTFP|O_VERB|O_NONL, ", at latest by: ");
38657c478bd9Sstevel@tonic-gate 	ptree_timeval(O_ALTFP|O_VERB|O_NONL, &at_latest_by);
38667c478bd9Sstevel@tonic-gate 	out(O_ALTFP|O_VERB, NULL);
38677c478bd9Sstevel@tonic-gate 
38687c478bd9Sstevel@tonic-gate 	if (ep->t == N_EREPORT) {
38697c478bd9Sstevel@tonic-gate 		if (ep->count == 0) {
38707c478bd9Sstevel@tonic-gate 			if (fmep->pull >= at_latest_by) {
38717c478bd9Sstevel@tonic-gate 				return_value = FME_DISPROVED;
38727c478bd9Sstevel@tonic-gate 			} else {
38737aec1d6eScindi 				ep->cached_delay = *pdelay = at_latest_by;
38747c478bd9Sstevel@tonic-gate 				return_value = FME_WAIT;
38757c478bd9Sstevel@tonic-gate 			}
38767c478bd9Sstevel@tonic-gate 		}
38777c478bd9Sstevel@tonic-gate 
38787c478bd9Sstevel@tonic-gate 		indent();
38797c478bd9Sstevel@tonic-gate 		switch (return_value) {
38807c478bd9Sstevel@tonic-gate 		case FME_CREDIBLE:
38817aec1d6eScindi 			ep->cached_state |= REQMNTS_CREDIBLE;
38827aec1d6eScindi 			out(O_ALTFP|O_VERB|O_NONL, "<-REQMNTS CREDIBLE ");
38837c478bd9Sstevel@tonic-gate 			itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
38847c478bd9Sstevel@tonic-gate 			break;
38857c478bd9Sstevel@tonic-gate 		case FME_DISPROVED:
38867aec1d6eScindi 			ep->cached_state |= REQMNTS_DISPROVED;
38877aec1d6eScindi 			out(O_ALTFP|O_VERB|O_NONL, "<-REQMNTS DISPROVED ");
38887c478bd9Sstevel@tonic-gate 			itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
38897c478bd9Sstevel@tonic-gate 			break;
38907c478bd9Sstevel@tonic-gate 		case FME_WAIT:
38917aec1d6eScindi 			ep->cached_state |= REQMNTS_WAIT;
38927aec1d6eScindi 			out(O_ALTFP|O_VERB|O_NONL, "<-REQMNTS WAIT ");
38937c478bd9Sstevel@tonic-gate 			itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
38947c478bd9Sstevel@tonic-gate 			out(O_ALTFP|O_VERB|O_NONL, " to ");
38957c478bd9Sstevel@tonic-gate 			ptree_timeval(O_ALTFP|O_VERB|O_NONL, &at_latest_by);
38967c478bd9Sstevel@tonic-gate 			break;
38977c478bd9Sstevel@tonic-gate 		default:
38987c478bd9Sstevel@tonic-gate 			out(O_DIE, "requirements_test: unexpected fme_state");
38997c478bd9Sstevel@tonic-gate 			break;
39007c478bd9Sstevel@tonic-gate 		}
39017c478bd9Sstevel@tonic-gate 		out(O_ALTFP|O_VERB, NULL);
39027c478bd9Sstevel@tonic-gate 		indent_pop();
39037c478bd9Sstevel@tonic-gate 
39047c478bd9Sstevel@tonic-gate 		return (return_value);
39057c478bd9Sstevel@tonic-gate 	}
39067c478bd9Sstevel@tonic-gate 
39077c478bd9Sstevel@tonic-gate 	/* this event is not a report, descend the tree */
39087c478bd9Sstevel@tonic-gate 	for (bp = itree_next_bubble(ep, NULL); bp;
39097c478bd9Sstevel@tonic-gate 	    bp = itree_next_bubble(ep, bp)) {
39107aec1d6eScindi 		int n;
39117aec1d6eScindi 
39127c478bd9Sstevel@tonic-gate 		if (bp->t != B_FROM)
39137c478bd9Sstevel@tonic-gate 			continue;
39147c478bd9Sstevel@tonic-gate 
39157aec1d6eScindi 		n = bp->nork;
39167aec1d6eScindi 
39177c478bd9Sstevel@tonic-gate 		credible_events = 0;
39187c478bd9Sstevel@tonic-gate 		waiting_events = 0;
39197aec1d6eScindi 		deferred_events = 0;
39207c478bd9Sstevel@tonic-gate 		arrow_delay = TIMEVAL_EVENTUALLY;
39217c478bd9Sstevel@tonic-gate 		/*
39227c478bd9Sstevel@tonic-gate 		 * n is -1 for 'A' so adjust it.
39237c478bd9Sstevel@tonic-gate 		 * XXX just count up the arrows for now.
39247c478bd9Sstevel@tonic-gate 		 */
39257c478bd9Sstevel@tonic-gate 		if (n < 0) {
39267c478bd9Sstevel@tonic-gate 			n = 0;
39277c478bd9Sstevel@tonic-gate 			for (ap = itree_next_arrow(bp, NULL); ap;
39287c478bd9Sstevel@tonic-gate 			    ap = itree_next_arrow(bp, ap))
39297c478bd9Sstevel@tonic-gate 				n++;
39307c478bd9Sstevel@tonic-gate 			indent();
39317c478bd9Sstevel@tonic-gate 			out(O_ALTFP|O_VERB, " Bubble Counted N=%d", n);
39327c478bd9Sstevel@tonic-gate 		} else {
39337c478bd9Sstevel@tonic-gate 			indent();
39347c478bd9Sstevel@tonic-gate 			out(O_ALTFP|O_VERB, " Bubble N=%d", n);
39357c478bd9Sstevel@tonic-gate 		}
39367c478bd9Sstevel@tonic-gate 
39377aec1d6eScindi 		if (n == 0)
39387aec1d6eScindi 			continue;
39397aec1d6eScindi 		if (!(bp->mark & (BUBBLE_ELIDED|BUBBLE_OK))) {
39407aec1d6eScindi 			for (ap = itree_next_arrow(bp, NULL); ap;
39417aec1d6eScindi 			    ap = itree_next_arrow(bp, ap)) {
39427aec1d6eScindi 				ep2 = ap->arrowp->head->myevent;
39437aec1d6eScindi 				platform_set_payloadnvp(ep2->nvp);
3944b7d3956bSstephh 				(void) checkconstraints(fmep, ap->arrowp);
39458cffa125SStephen Hanson 				if (!ap->arrowp->forever_false) {
39467aec1d6eScindi 					/*
3947b7d3956bSstephh 					 * if all arrows are invalidated by the
39487aec1d6eScindi 					 * constraints, then we should elide the
39497aec1d6eScindi 					 * whole bubble to be consistant with
39507aec1d6eScindi 					 * the tree creation time behaviour
39517aec1d6eScindi 					 */
3952b7d3956bSstephh 					bp->mark |= BUBBLE_OK;
39537aec1d6eScindi 					platform_set_payloadnvp(NULL);
39547aec1d6eScindi 					break;
39557aec1d6eScindi 				}
39567aec1d6eScindi 				platform_set_payloadnvp(NULL);
39577aec1d6eScindi 			}
39587aec1d6eScindi 		}
39597c478bd9Sstevel@tonic-gate 		for (ap = itree_next_arrow(bp, NULL); ap;
39607c478bd9Sstevel@tonic-gate 		    ap = itree_next_arrow(bp, ap)) {
39617c478bd9Sstevel@tonic-gate 			ep2 = ap->arrowp->head->myevent;
39627c478bd9Sstevel@tonic-gate 			if (n <= credible_events)
39637c478bd9Sstevel@tonic-gate 				break;
39647c478bd9Sstevel@tonic-gate 
39657aec1d6eScindi 			ap->arrowp->mark |= REQMNTS_COUNTER;
39667aec1d6eScindi 			if (triggered(fmep, ep2, REQMNTS_COUNTER))
39677c478bd9Sstevel@tonic-gate 				/* XXX adding max timevals! */
39687c478bd9Sstevel@tonic-gate 				switch (requirements_test(fmep, ep2,
39697c478bd9Sstevel@tonic-gate 				    at_latest_by + ap->arrowp->maxdelay,
39707aec1d6eScindi 				    &my_delay)) {
39717aec1d6eScindi 				case FME_DEFERRED:
39727aec1d6eScindi 					deferred_events++;
39737aec1d6eScindi 					break;
39747c478bd9Sstevel@tonic-gate 				case FME_CREDIBLE:
39757c478bd9Sstevel@tonic-gate 					credible_events++;
39767c478bd9Sstevel@tonic-gate 					break;
39777c478bd9Sstevel@tonic-gate 				case FME_DISPROVED:
39787c478bd9Sstevel@tonic-gate 					break;
39797c478bd9Sstevel@tonic-gate 				case FME_WAIT:
39807c478bd9Sstevel@tonic-gate 					if (my_delay < arrow_delay)
39817c478bd9Sstevel@tonic-gate 						arrow_delay = my_delay;
39827c478bd9Sstevel@tonic-gate 					waiting_events++;
39837c478bd9Sstevel@tonic-gate 					break;
39847c478bd9Sstevel@tonic-gate 				default:
39857c478bd9Sstevel@tonic-gate 					out(O_DIE,
39867c478bd9Sstevel@tonic-gate 					"Bug in requirements_test.");
39877c478bd9Sstevel@tonic-gate 				}
39887c478bd9Sstevel@tonic-gate 			else
39897aec1d6eScindi 				deferred_events++;
39907c478bd9Sstevel@tonic-gate 		}
3991b7d3956bSstephh 		if (!(bp->mark & BUBBLE_OK) && waiting_events == 0) {
3992b7d3956bSstephh 			bp->mark |= BUBBLE_ELIDED;
3993b7d3956bSstephh 			continue;
3994b7d3956bSstephh 		}
39957c478bd9Sstevel@tonic-gate 		indent();
39967c478bd9Sstevel@tonic-gate 		out(O_ALTFP|O_VERB, " Credible: %d Waiting %d",
39977aec1d6eScindi 		    credible_events + deferred_events, waiting_events);
39987aec1d6eScindi 		if (credible_events + deferred_events + waiting_events < n) {
39997c478bd9Sstevel@tonic-gate 			/* Can never meet requirements */
40007aec1d6eScindi 			ep->cached_state |= REQMNTS_DISPROVED;
40017c478bd9Sstevel@tonic-gate 			indent();
40027aec1d6eScindi 			out(O_ALTFP|O_VERB|O_NONL, "<-REQMNTS DISPROVED ");
40037c478bd9Sstevel@tonic-gate 			itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
40047c478bd9Sstevel@tonic-gate 			out(O_ALTFP|O_VERB, NULL);
40057c478bd9Sstevel@tonic-gate 			indent_pop();
40067c478bd9Sstevel@tonic-gate 			return (FME_DISPROVED);
40077c478bd9Sstevel@tonic-gate 		}
40087aec1d6eScindi 		if (credible_events + deferred_events < n) {
40097aec1d6eScindi 			/* will have to wait */
40107c478bd9Sstevel@tonic-gate 			/* wait time is shortest known */
40117c478bd9Sstevel@tonic-gate 			if (arrow_delay < overall_delay)
40127c478bd9Sstevel@tonic-gate 				overall_delay = arrow_delay;
40137c478bd9Sstevel@tonic-gate 			return_value = FME_WAIT;
40147aec1d6eScindi 		} else if (credible_events < n) {
40157aec1d6eScindi 			if (return_value != FME_WAIT)
40167aec1d6eScindi 				return_value = FME_DEFERRED;
40177c478bd9Sstevel@tonic-gate 		}
40187c478bd9Sstevel@tonic-gate 	}
40197c478bd9Sstevel@tonic-gate 
40207c478bd9Sstevel@tonic-gate 	/*
40217aec1d6eScindi 	 * don't mark as FME_DEFERRED. If this event isn't reached by another
40227aec1d6eScindi 	 * path, then this will be considered FME_CREDIBLE. But if it is
40237aec1d6eScindi 	 * reached by a different path so the K-count is met, then might
40247aec1d6eScindi 	 * get overridden by FME_WAIT or FME_DISPROVED.
40257c478bd9Sstevel@tonic-gate 	 */
40267aec1d6eScindi 	if (return_value == FME_WAIT) {
40277aec1d6eScindi 		ep->cached_state |= REQMNTS_WAIT;
40287aec1d6eScindi 		ep->cached_delay = *pdelay = overall_delay;
40297aec1d6eScindi 	} else if (return_value == FME_CREDIBLE) {
40307aec1d6eScindi 		ep->cached_state |= REQMNTS_CREDIBLE;
40317aec1d6eScindi 	}
40327c478bd9Sstevel@tonic-gate 	indent();
40337aec1d6eScindi 	out(O_ALTFP|O_VERB|O_NONL, "<-REQMNTS %s ",
40347aec1d6eScindi 	    fme_state2str(return_value));
40357c478bd9Sstevel@tonic-gate 	itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
40367c478bd9Sstevel@tonic-gate 	out(O_ALTFP|O_VERB, NULL);
40377c478bd9Sstevel@tonic-gate 	indent_pop();
40387c478bd9Sstevel@tonic-gate 	return (return_value);
40397c478bd9Sstevel@tonic-gate }
40407c478bd9Sstevel@tonic-gate 
40417c478bd9Sstevel@tonic-gate static enum fme_state
causes_test(struct fme * fmep,struct event * ep,unsigned long long at_latest_by,unsigned long long * pdelay)40427c478bd9Sstevel@tonic-gate causes_test(struct fme *fmep, struct event *ep,
40437c478bd9Sstevel@tonic-gate     unsigned long long at_latest_by, unsigned long long *pdelay)
40447c478bd9Sstevel@tonic-gate {
40457c478bd9Sstevel@tonic-gate 	unsigned long long overall_delay = TIMEVAL_EVENTUALLY;
40467c478bd9Sstevel@tonic-gate 	unsigned long long my_delay;
40477c478bd9Sstevel@tonic-gate 	int credible_results = 0;
40487c478bd9Sstevel@tonic-gate 	int waiting_results = 0;
40497c478bd9Sstevel@tonic-gate 	enum fme_state fstate;
40507c478bd9Sstevel@tonic-gate 	struct event *tail_event;
40517c478bd9Sstevel@tonic-gate 	struct bubble *bp;
40527c478bd9Sstevel@tonic-gate 	struct arrowlist *ap;
40537c478bd9Sstevel@tonic-gate 	int k = 1;
40547c478bd9Sstevel@tonic-gate 
40557c478bd9Sstevel@tonic-gate 	stats_counter_bump(fmep->Ccallcount);
40567c478bd9Sstevel@tonic-gate 	indent_push("  C");
40577c478bd9Sstevel@tonic-gate 	indent();
40587c478bd9Sstevel@tonic-gate 	out(O_ALTFP|O_VERB|O_NONL, "->");
40597c478bd9Sstevel@tonic-gate 	itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
40607c478bd9Sstevel@tonic-gate 	out(O_ALTFP|O_VERB, NULL);
40617c478bd9Sstevel@tonic-gate 
40627c478bd9Sstevel@tonic-gate 	for (bp = itree_next_bubble(ep, NULL); bp;
40637c478bd9Sstevel@tonic-gate 	    bp = itree_next_bubble(ep, bp)) {
40647c478bd9Sstevel@tonic-gate 		if (bp->t != B_TO)
40657c478bd9Sstevel@tonic-gate 			continue;
40667c478bd9Sstevel@tonic-gate 		k = bp->nork;	/* remember the K value */
40677c478bd9Sstevel@tonic-gate 		for (ap = itree_next_arrow(bp, NULL); ap;
40687c478bd9Sstevel@tonic-gate 		    ap = itree_next_arrow(bp, ap)) {
40697c478bd9Sstevel@tonic-gate 			int do_not_follow = 0;
40707aec1d6eScindi 
40717aec1d6eScindi 			/*
40727aec1d6eScindi 			 * if we get to the same event multiple times
40737aec1d6eScindi 			 * only worry about the first one.
40747aec1d6eScindi 			 */
40757aec1d6eScindi 			if (ap->arrowp->tail->myevent->cached_state &
40767aec1d6eScindi 			    CAUSES_TESTED) {
40777aec1d6eScindi 				indent();
40787aec1d6eScindi 				out(O_ALTFP|O_VERB|O_NONL,
40797aec1d6eScindi 				    "  causes test already run for ");
40807aec1d6eScindi 				itree_pevent_brief(O_ALTFP|O_VERB|O_NONL,
40817aec1d6eScindi 				    ap->arrowp->tail->myevent);
40827aec1d6eScindi 				out(O_ALTFP|O_VERB, NULL);
40837aec1d6eScindi 				continue;
40847aec1d6eScindi 			}
40857aec1d6eScindi 
40867c478bd9Sstevel@tonic-gate 			/*
40877c478bd9Sstevel@tonic-gate 			 * see if false constraint prevents us
40887c478bd9Sstevel@tonic-gate 			 * from traversing this arrow
40897c478bd9Sstevel@tonic-gate 			 */
40907c478bd9Sstevel@tonic-gate 			platform_set_payloadnvp(ep->nvp);
409100d0963fSdilpreet 			if (checkconstraints(fmep, ap->arrowp) == 0)
40927c478bd9Sstevel@tonic-gate 				do_not_follow = 1;
40937c478bd9Sstevel@tonic-gate 			platform_set_payloadnvp(NULL);
40947c478bd9Sstevel@tonic-gate 			if (do_not_follow) {
40957c478bd9Sstevel@tonic-gate 				indent();
40967c478bd9Sstevel@tonic-gate 				out(O_ALTFP|O_VERB|O_NONL,
40977c478bd9Sstevel@tonic-gate 				    "  False arrow from ");
40987c478bd9Sstevel@tonic-gate 				itree_pevent_brief(O_ALTFP|O_VERB|O_NONL,
40997c478bd9Sstevel@tonic-gate 				    ap->arrowp->tail->myevent);
41007c478bd9Sstevel@tonic-gate 				out(O_ALTFP|O_VERB, NULL);
41017c478bd9Sstevel@tonic-gate 				continue;
41027c478bd9Sstevel@tonic-gate 			}
41037c478bd9Sstevel@tonic-gate 
41047aec1d6eScindi 			ap->arrowp->tail->myevent->cached_state |=
41057aec1d6eScindi 			    CAUSES_TESTED;
41067c478bd9Sstevel@tonic-gate 			tail_event = ap->arrowp->tail->myevent;
41077aec1d6eScindi 			fstate = hypothesise(fmep, tail_event, at_latest_by,
41087aec1d6eScindi 			    &my_delay);
41097c478bd9Sstevel@tonic-gate 
41107c478bd9Sstevel@tonic-gate 			switch (fstate) {
41117c478bd9Sstevel@tonic-gate 			case FME_WAIT:
41127c478bd9Sstevel@tonic-gate 				if (my_delay < overall_delay)
41137c478bd9Sstevel@tonic-gate 					overall_delay = my_delay;
41147c478bd9Sstevel@tonic-gate 				waiting_results++;
41157c478bd9Sstevel@tonic-gate 				break;
41167c478bd9Sstevel@tonic-gate 			case FME_CREDIBLE:
41177c478bd9Sstevel@tonic-gate 				credible_results++;
41187c478bd9Sstevel@tonic-gate 				break;
41197c478bd9Sstevel@tonic-gate 			case FME_DISPROVED:
41207c478bd9Sstevel@tonic-gate 				break;
41217c478bd9Sstevel@tonic-gate 			default:
41227c478bd9Sstevel@tonic-gate 				out(O_DIE, "Bug in causes_test");
41237c478bd9Sstevel@tonic-gate 			}
41247c478bd9Sstevel@tonic-gate 		}
41257c478bd9Sstevel@tonic-gate 	}
41267c478bd9Sstevel@tonic-gate 	/* compare against K */
41277c478bd9Sstevel@tonic-gate 	if (credible_results + waiting_results < k) {
41287c478bd9Sstevel@tonic-gate 		indent();
41297aec1d6eScindi 		out(O_ALTFP|O_VERB|O_NONL, "<-CAUSES DISPROVED ");
41307c478bd9Sstevel@tonic-gate 		itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
41317c478bd9Sstevel@tonic-gate 		out(O_ALTFP|O_VERB, NULL);
41327c478bd9Sstevel@tonic-gate 		indent_pop();
41337c478bd9Sstevel@tonic-gate 		return (FME_DISPROVED);
41347c478bd9Sstevel@tonic-gate 	}
41357c478bd9Sstevel@tonic-gate 	if (waiting_results != 0) {
41367c478bd9Sstevel@tonic-gate 		*pdelay = overall_delay;
41377c478bd9Sstevel@tonic-gate 		indent();
41387aec1d6eScindi 		out(O_ALTFP|O_VERB|O_NONL, "<-CAUSES WAIT ");
41397c478bd9Sstevel@tonic-gate 		itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
41407c478bd9Sstevel@tonic-gate 		out(O_ALTFP|O_VERB|O_NONL, " to ");
41417c478bd9Sstevel@tonic-gate 		ptree_timeval(O_ALTFP|O_VERB|O_NONL, &at_latest_by);
41427c478bd9Sstevel@tonic-gate 		out(O_ALTFP|O_VERB, NULL);
41437c478bd9Sstevel@tonic-gate 		indent_pop();
41447c478bd9Sstevel@tonic-gate 		return (FME_WAIT);
41457c478bd9Sstevel@tonic-gate 	}
41467c478bd9Sstevel@tonic-gate 	indent();
41477aec1d6eScindi 	out(O_ALTFP|O_VERB|O_NONL, "<-CAUSES CREDIBLE ");
41487c478bd9Sstevel@tonic-gate 	itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
41497c478bd9Sstevel@tonic-gate 	out(O_ALTFP|O_VERB, NULL);
41507c478bd9Sstevel@tonic-gate 	indent_pop();
41517c478bd9Sstevel@tonic-gate 	return (FME_CREDIBLE);
41527c478bd9Sstevel@tonic-gate }
41537c478bd9Sstevel@tonic-gate 
41547c478bd9Sstevel@tonic-gate static enum fme_state
hypothesise(struct fme * fmep,struct event * ep,unsigned long long at_latest_by,unsigned long long * pdelay)41557c478bd9Sstevel@tonic-gate hypothesise(struct fme *fmep, struct event *ep,
41567aec1d6eScindi 	unsigned long long at_latest_by, unsigned long long *pdelay)
41577c478bd9Sstevel@tonic-gate {
41587c478bd9Sstevel@tonic-gate 	enum fme_state rtr, otr;
41597c478bd9Sstevel@tonic-gate 	unsigned long long my_delay;
41607c478bd9Sstevel@tonic-gate 	unsigned long long overall_delay = TIMEVAL_EVENTUALLY;
41617c478bd9Sstevel@tonic-gate 
41627c478bd9Sstevel@tonic-gate 	stats_counter_bump(fmep->Hcallcount);
41637c478bd9Sstevel@tonic-gate 	indent_push("  H");
41647c478bd9Sstevel@tonic-gate 	indent();
41657c478bd9Sstevel@tonic-gate 	out(O_ALTFP|O_VERB|O_NONL, "->");
41667c478bd9Sstevel@tonic-gate 	itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
41677c478bd9Sstevel@tonic-gate 	out(O_ALTFP|O_VERB|O_NONL, ", at latest by: ");
41687c478bd9Sstevel@tonic-gate 	ptree_timeval(O_ALTFP|O_VERB|O_NONL, &at_latest_by);
41697c478bd9Sstevel@tonic-gate 	out(O_ALTFP|O_VERB, NULL);
41707c478bd9Sstevel@tonic-gate 
41717aec1d6eScindi 	rtr = requirements_test(fmep, ep, at_latest_by, &my_delay);
41727c478bd9Sstevel@tonic-gate 	if ((rtr == FME_WAIT) && (my_delay < overall_delay))
41737c478bd9Sstevel@tonic-gate 		overall_delay = my_delay;
41747c478bd9Sstevel@tonic-gate 	if (rtr != FME_DISPROVED) {
41757c478bd9Sstevel@tonic-gate 		if (is_problem(ep->t)) {
41767aec1d6eScindi 			otr = effects_test(fmep, ep, at_latest_by, &my_delay);
41777c478bd9Sstevel@tonic-gate 			if (otr != FME_DISPROVED) {
4178b5016cbbSstephh 				if (fmep->peek == 0 && ep->is_suspect == 0) {
41797c478bd9Sstevel@tonic-gate 					ep->suspects = fmep->suspects;
4180b5016cbbSstephh 					ep->is_suspect = 1;
41817c478bd9Sstevel@tonic-gate 					fmep->suspects = ep;
41827c478bd9Sstevel@tonic-gate 					fmep->nsuspects++;
41837c478bd9Sstevel@tonic-gate 				}
41847c478bd9Sstevel@tonic-gate 			}
41857c478bd9Sstevel@tonic-gate 		} else
41867c478bd9Sstevel@tonic-gate 			otr = causes_test(fmep, ep, at_latest_by, &my_delay);
41877c478bd9Sstevel@tonic-gate 		if ((otr == FME_WAIT) && (my_delay < overall_delay))
41887c478bd9Sstevel@tonic-gate 			overall_delay = my_delay;
41897c478bd9Sstevel@tonic-gate 		if ((otr != FME_DISPROVED) &&
41907c478bd9Sstevel@tonic-gate 		    ((rtr == FME_WAIT) || (otr == FME_WAIT)))
41917c478bd9Sstevel@tonic-gate 			*pdelay = overall_delay;
41927c478bd9Sstevel@tonic-gate 	}
41937c478bd9Sstevel@tonic-gate 	if (rtr == FME_DISPROVED) {
41947c478bd9Sstevel@tonic-gate 		indent();
41957c478bd9Sstevel@tonic-gate 		out(O_ALTFP|O_VERB|O_NONL, "<-DISPROVED ");
41967c478bd9Sstevel@tonic-gate 		itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
41977c478bd9Sstevel@tonic-gate 		out(O_ALTFP|O_VERB, " (doesn't meet requirements)");
41987c478bd9Sstevel@tonic-gate 		indent_pop();
41997c478bd9Sstevel@tonic-gate 		return (FME_DISPROVED);
42007c478bd9Sstevel@tonic-gate 	}
42017c478bd9Sstevel@tonic-gate 	if ((otr == FME_DISPROVED) && is_problem(ep->t)) {
42027c478bd9Sstevel@tonic-gate 		indent();
42037c478bd9Sstevel@tonic-gate 		out(O_ALTFP|O_VERB|O_NONL, "<-DISPROVED ");
42047c478bd9Sstevel@tonic-gate 		itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
42057c478bd9Sstevel@tonic-gate 		out(O_ALTFP|O_VERB, " (doesn't explain all reports)");
42067c478bd9Sstevel@tonic-gate 		indent_pop();
42077c478bd9Sstevel@tonic-gate 		return (FME_DISPROVED);
42087c478bd9Sstevel@tonic-gate 	}
42097c478bd9Sstevel@tonic-gate 	if (otr == FME_DISPROVED) {
42107c478bd9Sstevel@tonic-gate 		indent();
42117c478bd9Sstevel@tonic-gate 		out(O_ALTFP|O_VERB|O_NONL, "<-DISPROVED ");
42127c478bd9Sstevel@tonic-gate 		itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
42137c478bd9Sstevel@tonic-gate 		out(O_ALTFP|O_VERB, " (causes are not credible)");
42147c478bd9Sstevel@tonic-gate 		indent_pop();
42157c478bd9Sstevel@tonic-gate 		return (FME_DISPROVED);
42167c478bd9Sstevel@tonic-gate 	}
42177c478bd9Sstevel@tonic-gate 	if ((rtr == FME_WAIT) || (otr == FME_WAIT)) {
42187c478bd9Sstevel@tonic-gate 		indent();
42197c478bd9Sstevel@tonic-gate 		out(O_ALTFP|O_VERB|O_NONL, "<-WAIT ");
42207c478bd9Sstevel@tonic-gate 		itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
42217c478bd9Sstevel@tonic-gate 		out(O_ALTFP|O_VERB|O_NONL, " to ");
42227c478bd9Sstevel@tonic-gate 		ptree_timeval(O_ALTFP|O_VERB|O_NONL, &overall_delay);
42237c478bd9Sstevel@tonic-gate 		out(O_ALTFP|O_VERB, NULL);
42247c478bd9Sstevel@tonic-gate 		indent_pop();
42257c478bd9Sstevel@tonic-gate 		return (FME_WAIT);
42267c478bd9Sstevel@tonic-gate 	}
42277c478bd9Sstevel@tonic-gate 	indent();
42287c478bd9Sstevel@tonic-gate 	out(O_ALTFP|O_VERB|O_NONL, "<-CREDIBLE ");
42297c478bd9Sstevel@tonic-gate 	itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
42307c478bd9Sstevel@tonic-gate 	out(O_ALTFP|O_VERB, NULL);
42317c478bd9Sstevel@tonic-gate 	indent_pop();
42327c478bd9Sstevel@tonic-gate 	return (FME_CREDIBLE);
42337c478bd9Sstevel@tonic-gate }
42347aec1d6eScindi 
42357aec1d6eScindi /*
42367aec1d6eScindi  * fme_istat_load -- reconstitute any persistent istats
42377aec1d6eScindi  */
42387aec1d6eScindi void
fme_istat_load(fmd_hdl_t * hdl)42397aec1d6eScindi fme_istat_load(fmd_hdl_t *hdl)
42407aec1d6eScindi {
42417aec1d6eScindi 	int sz;
42427aec1d6eScindi 	char *sbuf;
42437aec1d6eScindi 	char *ptr;
42447aec1d6eScindi 
42457aec1d6eScindi 	if ((sz = fmd_buf_size(hdl, NULL, WOBUF_ISTATS)) == 0) {
42467aec1d6eScindi 		out(O_ALTFP, "fme_istat_load: No stats");
42477aec1d6eScindi 		return;
42487aec1d6eScindi 	}
42497aec1d6eScindi 
42507aec1d6eScindi 	sbuf = alloca(sz);
42517aec1d6eScindi 
42527aec1d6eScindi 	fmd_buf_read(hdl, NULL, WOBUF_ISTATS, sbuf, sz);
42537aec1d6eScindi 
42547aec1d6eScindi 	/*
42557aec1d6eScindi 	 * pick apart the serialized stats
42567aec1d6eScindi 	 *
42577aec1d6eScindi 	 * format is:
42587aec1d6eScindi 	 *	<class-name>, '@', <path>, '\0', <value>, '\0'
42597aec1d6eScindi 	 * for example:
42607aec1d6eScindi 	 *	"stat.first@stat0/path0\02\0stat.second@stat0/path1\023\0"
42617aec1d6eScindi 	 *
42627aec1d6eScindi 	 * since this is parsing our own serialized data, any parsing issues
42637aec1d6eScindi 	 * are fatal, so we check for them all with ASSERT() below.
42647aec1d6eScindi 	 */
42657aec1d6eScindi 	ptr = sbuf;
42667aec1d6eScindi 	while (ptr < &sbuf[sz]) {
42677aec1d6eScindi 		char *sepptr;
42687aec1d6eScindi 		struct node *np;
42697aec1d6eScindi 		int val;
42707aec1d6eScindi 
42717aec1d6eScindi 		sepptr = strchr(ptr, '@');
42727aec1d6eScindi 		ASSERT(sepptr != NULL);
42737aec1d6eScindi 		*sepptr = '\0';
42747aec1d6eScindi 
42757aec1d6eScindi 		/* construct the event */
42767aec1d6eScindi 		np = newnode(T_EVENT, NULL, 0);
42777aec1d6eScindi 		np->u.event.ename = newnode(T_NAME, NULL, 0);
42787aec1d6eScindi 		np->u.event.ename->u.name.t = N_STAT;
42797aec1d6eScindi 		np->u.event.ename->u.name.s = stable(ptr);
42807aec1d6eScindi 		np->u.event.ename->u.name.it = IT_ENAME;
42817aec1d6eScindi 		np->u.event.ename->u.name.last = np->u.event.ename;
42827aec1d6eScindi 
42837aec1d6eScindi 		ptr = sepptr + 1;
42847aec1d6eScindi 		ASSERT(ptr < &sbuf[sz]);
42857aec1d6eScindi 		ptr += strlen(ptr);
42867aec1d6eScindi 		ptr++;	/* move past the '\0' separating path from value */
42877aec1d6eScindi 		ASSERT(ptr < &sbuf[sz]);
42887aec1d6eScindi 		ASSERT(isdigit(*ptr));
42897aec1d6eScindi 		val = atoi(ptr);
42907aec1d6eScindi 		ASSERT(val > 0);
42917aec1d6eScindi 		ptr += strlen(ptr);
42927aec1d6eScindi 		ptr++;	/* move past the final '\0' for this entry */
42937aec1d6eScindi 
42947aec1d6eScindi 		np->u.event.epname = pathstring2epnamenp(sepptr + 1);
42957aec1d6eScindi 		ASSERT(np->u.event.epname != NULL);
42967aec1d6eScindi 
42977aec1d6eScindi 		istat_bump(np, val);
42987aec1d6eScindi 		tree_free(np);
42997aec1d6eScindi 	}
43007aec1d6eScindi 
43017aec1d6eScindi 	istat_save();
43027aec1d6eScindi }
4303