xref: /titanic_44/usr/src/cmd/fm/fmadm/common/faulty.c (revision 321502cd0930b1eb6d4805e17f16234f3e3ff4b2)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <sys/types.h>
29 #include <fmadm.h>
30 #include <errno.h>
31 #include <limits.h>
32 #include <strings.h>
33 #include <stdio.h>
34 #include <unistd.h>
35 #include <sys/wait.h>
36 #include <sys/stat.h>
37 #include <fcntl.h>
38 #include <fm/fmd_log.h>
39 #include <sys/fm/protocol.h>
40 #include <fm/libtopo.h>
41 #include <fm/fmd_adm.h>
42 #include <dlfcn.h>
43 #include <sys/systeminfo.h>
44 #include <sys/utsname.h>
45 #include <libintl.h>
46 #include <locale.h>
47 #include <sys/smbios.h>
48 #include <libdevinfo.h>
49 #include <stdlib.h>
50 
51 #define	offsetof(s, m)	((size_t)(&(((s*)0)->m)))
52 
53 /*
54  * catalog_setup() must be called to setup support functions.
55  * Fault records are added to catalog by calling add_fault_record_to_catalog()
56  * records are stored in order of importance to the system.
57  * If -g flag is set or not_suppressed is not set and the class fru, fault,
58  * type are the same then details are merged into an existing record, with uuid
59  * records are stored in time order.
60  * For each record information is extracted from nvlist and merged into linked
61  * list each is checked for identical records for which percentage certainty are
62  * added together.
63  * print_catalog() is called to print out catalog and release external resources
64  *
65  *                         /---------------\
66  *	status_rec_list -> |               | -|
67  *                         \---------------/
68  *                                \/
69  *                         /---------------\    /-------\    /-------\
70  *      status_fru_list    | status_record | -> | uurec | -> | uurec | -|
71  *            \/           |               | |- |       | <- |       |
72  *      /-------------\    |               |    \-------/    \-------/
73  *      |             | -> |               |       \/           \/
74  *      \-------------/    |               |    /-------\    /-------\
75  *            \/           |               | -> | asru  | -> | asru  |
76  *            ---          |               |    |       | <- |       |
77  *                         |               |    \-------/    \-------/
78  *      status_asru_list   |  class        |
79  *            \/           |  resource     |    /-------\    /-------\
80  *      /-------------\    |  fru          | -> | list  | -> | list  |
81  *      |             | -> |  serial       |    |       | <- |       |
82  *      \-------------/    |               |    \-------/    \-------/
83  *            \/           \---------------/
84  *            ---               \/    /\
85  *                         /---------------\
86  *                         | status_record |
87  *                         \---------------/
88  *
89  * Fmadm faulty takes a number of options which affect the format of the
90  * output displayed. By default, the display reports the FRU and ASRU along
91  * with other information on per-case basis as in the example below.
92  *
93  * --------------- ------------------------------------  -------------- -------
94  * TIME            EVENT-ID                              MSG-ID         SEVERITY
95  * --------------- ------------------------------------  -------------- -------
96  * Sep 21 10:01:36 d482f935-5c8f-e9ab-9f25-d0aaafec1e6c  AMD-8000-2F    Major
97  *
98  * Fault class	: fault.memory.dimm_sb
99  * Affects	: mem:///motherboard=0/chip=0/memory-controller=0/dimm=0/rank=0
100  *		    degraded but still in service
101  * FRU		: "CPU 0 DIMM 0" (hc://.../memory-controller=0/dimm=0)
102  *		    faulty
103  *
104  * Description	: The number of errors associated with this memory module has
105  *		exceeded acceptable levels.  Refer to
106  *		http://sun.com/msg/AMD-8000-2F for more information.
107  *
108  * Response	: Pages of memory associated with this memory module are being
109  *		removed from service as errors are reported.
110  *
111  * Impact	: Total system memory capacity will be reduced as pages are
112  *		retired.
113  *
114  * Action	: Schedule a repair procedure to replace the affected memory
115  *		module.  Use fmdump -v -u <EVENT_ID> to identify the module.
116  *
117  * The -v flag is similar, but adds some additonal information such as the
118  * resource. The -s flag is also similar but just gives the top line summary.
119  * All these options (ie without the -f or -r flags) use the print_catalog()
120  * function to do the display.
121  *
122  * The -f flag changes the output so that it appears sorted on a per-fru basis.
123  * The output is somewhat cut down compared to the default output. If -f is
124  * used, then print_fru() is used to print the output.
125  *
126  * -----------------------------------------------------------------------------
127  * "SLOT 2" (hc://.../hostbridge=3/pciexrc=3/pciexbus=4/pciexdev=0) faulty
128  * 5ca4aeb3-36...f6be-c2e8166dc484 2 suspects in this FRU total certainty 100%
129  *
130  * Description	: A problem was detected for a PCI device.
131  *		Refer to http://sun.com/msg/PCI-8000-7J for more information.
132  *
133  * Response	: One or more device instances may be disabled
134  *
135  * Impact	: Possible loss of services provided by the device instances
136  *		associated with this fault
137  *
138  * Action	: Schedule a repair procedure to replace the affected device.
139  * 		Use fmdump -v -u <EVENT_ID> to identify the device or contact
140  *		Sun for support.
141  *
142  * The -r flag changes the output so that it appears sorted on a per-asru basis.
143  * The output is very much cut down compared to the default output, just giving
144  * the asru fmri and state. Here print_asru() is used to print the output.
145  *
146  * mem:///motherboard=0/chip=0/memory-controller=0/dimm=0/rank=0	degraded
147  *
148  * For all fmadm faulty options, the sequence of events is
149  *
150  * 1) Walk through all the cases in the system using fmd_adm_case_iter() and
151  * for each case call dfault_rec(). This will call add_fault_record_to_catalog()
152  * This will extract the data from the nvlist and call catalog_new_record() to
153  * save the data away in various linked lists in the catalogue.
154  *
155  * 2) Once this is done, the data can be supplemented by using
156  * fmd_adm_rsrc_iter(). However this is now only necessary for the -i option.
157  *
158  * 3) Finally print_catalog(), print_fru() or print_asru() are called as
159  * appropriate to display the information from the catalogue sorted in the
160  * requested way.
161  *
162  */
163 
164 typedef struct name_list {
165 	struct name_list *next;
166 	struct name_list *prev;
167 	char *name;
168 	uint8_t pct;
169 	uint8_t max_pct;
170 	ushort_t count;
171 	int status;
172 	char *label;
173 } name_list_t;
174 
175 typedef struct ari_list {
176 	char *ari_uuid;
177 	struct ari_list *next;
178 } ari_list_t;
179 
180 typedef struct uurec {
181 	struct uurec *next;
182 	struct uurec *prev;
183 	char *uuid;
184 	ari_list_t *ari_uuid_list;
185 	name_list_t *asru;
186 	uint64_t sec;
187 } uurec_t;
188 
189 typedef struct uurec_select {
190 	struct uurec_select *next;
191 	char *uuid;
192 } uurec_select_t;
193 
194 typedef struct host_id {
195 	char *chassis;
196 	char *server;
197 	char *platform;
198 } hostid_t;
199 
200 typedef struct host_id_list {
201 	hostid_t hostid;
202 	struct host_id_list *next;
203 } host_id_list_t;
204 
205 typedef struct status_record {
206 	hostid_t *host;
207 	int nrecs;
208 	uurec_t *uurec;
209 	char *severity;			/* in C locale */
210 	char *msgid;
211 	name_list_t *class;
212 	name_list_t *resource;
213 	name_list_t *asru;
214 	name_list_t *fru;
215 	name_list_t *serial;
216 	char *url;
217 	uint8_t not_suppressed;
218 } status_record_t;
219 
220 typedef struct sr_list {
221 	struct sr_list *next;
222 	struct sr_list *prev;
223 	struct status_record *status_record;
224 } sr_list_t;
225 
226 typedef struct resource_list {
227 	struct resource_list *next;
228 	struct resource_list *prev;
229 	sr_list_t *status_rec_list;
230 	char *resource;
231 	uint8_t not_suppressed;
232 	uint8_t max_pct;
233 } resource_list_t;
234 
235 typedef struct tgetlabel_data {
236 	char *label;
237 	char *fru;
238 } tgetlabel_data_t;
239 
240 sr_list_t *status_rec_list;
241 resource_list_t *status_fru_list;
242 resource_list_t *status_asru_list;
243 
244 static char *locale;
245 static char *nlspath;
246 static int max_display;
247 static int max_fault = 0;
248 static topo_hdl_t *topo_handle;
249 static char *topo_handle_uuid;
250 static host_id_list_t *host_list;
251 static int n_server;
252 static int opt_g;
253 
254 static char *
255 format_date(char *buf, size_t len, uint64_t sec)
256 {
257 	if (sec > LONG_MAX) {
258 		(void) fprintf(stderr,
259 		    "record time is too large for 32-bit utility\n");
260 		(void) snprintf(buf, len, "0x%llx", sec);
261 	} else {
262 		time_t tod = (time_t)sec;
263 		(void) strftime(buf, len, "%b %d %T", localtime(&tod));
264 	}
265 
266 	return (buf);
267 }
268 
269 static hostid_t *
270 find_hostid_in_list(char *platform, char *chassis, char *server)
271 {
272 	hostid_t *rt = NULL;
273 	host_id_list_t *hostp;
274 
275 	if (platform == NULL)
276 		platform = "-";
277 	if (server == NULL)
278 		server = "-";
279 	hostp = host_list;
280 	while (hostp) {
281 		if (hostp->hostid.platform &&
282 		    strcmp(hostp->hostid.platform, platform) == 0 &&
283 		    hostp->hostid.server &&
284 		    strcmp(hostp->hostid.server, server) == 0 &&
285 		    (chassis == NULL || hostp->hostid.chassis == NULL ||
286 		    strcmp(chassis, hostp->hostid.chassis) == 0)) {
287 			rt = &hostp->hostid;
288 			break;
289 		}
290 		hostp = hostp->next;
291 	}
292 	if (rt == NULL) {
293 		hostp = malloc(sizeof (host_id_list_t));
294 		hostp->hostid.platform = strdup(platform);
295 		hostp->hostid.server = strdup(server);
296 		hostp->hostid.chassis = chassis ? strdup(chassis) : NULL;
297 		hostp->next = host_list;
298 		host_list = hostp;
299 		rt = &hostp->hostid;
300 		n_server++;
301 	}
302 	return (rt);
303 }
304 
305 static hostid_t *
306 find_hostid(nvlist_t *nvl)
307 {
308 	char *platform = NULL, *chassis = NULL, *server = NULL;
309 	nvlist_t *auth, *fmri;
310 	hostid_t *rt = NULL;
311 
312 	if (nvlist_lookup_nvlist(nvl, FM_SUSPECT_DE, &fmri) == 0 &&
313 	    nvlist_lookup_nvlist(fmri, FM_FMRI_AUTHORITY, &auth) == 0) {
314 		(void) nvlist_lookup_string(auth, FM_FMRI_AUTH_PRODUCT,
315 		    &platform);
316 		(void) nvlist_lookup_string(auth, FM_FMRI_AUTH_SERVER, &server);
317 		(void) nvlist_lookup_string(auth, FM_FMRI_AUTH_CHASSIS,
318 		    &chassis);
319 		rt = find_hostid_in_list(platform, chassis, server);
320 	}
321 	return (rt);
322 }
323 
324 static void
325 catalog_setup(void)
326 {
327 	char *tp;
328 	int pl;
329 
330 	/*
331 	 * All FMA event dictionaries use msgfmt(1) message objects to produce
332 	 * messages, even for the C locale.  We therefore want to use dgettext
333 	 * for all message lookups, but its defined behavior in the C locale is
334 	 * to return the input string.  Since our input strings are event codes
335 	 * and not format strings, this doesn't help us.  We resolve this nit
336 	 * by setting NLSPATH to a non-existent file: the presence of NLSPATH
337 	 * is defined to force dgettext(3C) to do a full lookup even for C.
338 	 */
339 	nlspath = getenv("NLSPATH");
340 	if (nlspath == NULL)
341 		putenv("NLSPATH=/usr/lib/fm/fmd/fmd.cat");
342 	else {
343 		pl = strlen(nlspath) + sizeof ("NLSPATH=") + 1;
344 		tp = malloc(pl);
345 		(void) snprintf(tp, pl, "NLSPATH=%s", nlspath);
346 		nlspath = tp;
347 	}
348 
349 	locale = setlocale(LC_MESSAGES, "");
350 }
351 
352 static char *
353 get_dict_url(char *id)
354 {
355 	char *url = "http://sun.com/msg/";
356 	int msz = sizeof (url) + strlen(id) + 1;
357 	char *cp;
358 
359 	cp = malloc(msz);
360 	(void) snprintf(cp, msz, "%s%s", url, id);
361 	return (cp);
362 }
363 
364 static char *
365 get_dict_msg(char *id, char *idx, int unknown, int translate)
366 {
367 	char mbuf[128];
368 	char *msg;
369 	char dbuf[32];
370 	char *p;
371 	int restore_env = 0;
372 	int restore_locale = 0;
373 
374 	p = strchr(id, '-');
375 	if (p == NULL || p == id || (p - id) >= 32) {
376 		msg = mbuf;
377 	} else {
378 		strncpy(dbuf, id, (size_t)(p - id));
379 		dbuf[(size_t)(p - id)] = 0;
380 
381 		(void) snprintf(mbuf, sizeof (mbuf), "%s.%s", id, idx);
382 		if (translate == 0 || nlspath == NULL) {
383 			(void) setlocale(LC_MESSAGES, "C");
384 			restore_locale = 1;
385 		}
386 		bindtextdomain("FMD", "/usr/lib/locale");
387 		msg = dgettext(dbuf, mbuf);
388 		if (msg == mbuf) {
389 			(void) setlocale(LC_MESSAGES, "C");
390 			restore_locale = 1;
391 			msg = dgettext(dbuf, mbuf);
392 		}
393 		if (msg == mbuf) {
394 			putenv("NLSPATH=/usr/lib/fm/fmd/fmd.cat");
395 			restore_env = 1;
396 			(void) setlocale(LC_MESSAGES, "C");
397 			msg = dgettext(dbuf, mbuf);
398 		}
399 		if (restore_locale)
400 			(void) setlocale(LC_MESSAGES, locale);
401 		if (restore_env && nlspath)
402 			putenv(nlspath);
403 	}
404 	if (msg == mbuf) {
405 		if (unknown)
406 			msg = "unknown";
407 		else
408 			msg = NULL;
409 	}
410 	return (msg);
411 }
412 
413 /*
414  * compare two fru strings which are made up of substrings seperated by '/'
415  * return true if every substring is the same in the two strings, or if a
416  * substring is null in one.
417  */
418 
419 static int
420 frucmp(char *f1, char *f2)
421 {
422 	char c1, c2;
423 	int i = 0;
424 
425 	for (;;) {
426 		c1 = *f1;
427 		c2 = *f2;
428 		if (c1 == c2) {
429 			i = (c1 == '/') ? 0 : i + 1;
430 		} else if (i == 0) {
431 			if (c1 == '/') {
432 				do {
433 					f2++;
434 				} while ((c2 = *f2) != 0 && c2 != '/');
435 				if (c2 == NULL)
436 					break;
437 			} else if (c2 == '/') {
438 				do {
439 					f1++;
440 				} while ((c1 = *f1) != 0 && c1 != '/');
441 				if (c1 == NULL)
442 					break;
443 			} else
444 				break;
445 		} else
446 			break;
447 		if (c1 == NULL)
448 			return (0);
449 		f1++;
450 		f2++;
451 	}
452 	return (1);
453 }
454 
455 static int
456 tgetlabel(topo_hdl_t *thp, tnode_t *node, void *arg)
457 {
458 	int err;
459 	char *fru_name, *lname;
460 	nvlist_t *fru = NULL;
461 	int rt = TOPO_WALK_NEXT;
462 	tgetlabel_data_t *tdp = (tgetlabel_data_t *)arg;
463 
464 	if (topo_node_fru(node, &fru, NULL, &err) == 0) {
465 		if (topo_fmri_nvl2str(thp, fru, &fru_name, &err) == 0) {
466 			if (frucmp(tdp->fru, fru_name) == 0 &&
467 			    topo_node_label(node, &lname, &err) == 0) {
468 				tdp->label = strdup(lname);
469 				topo_hdl_strfree(thp, lname);
470 				rt = TOPO_WALK_TERMINATE;
471 			}
472 			topo_hdl_strfree(thp, fru_name);
473 		}
474 		nvlist_free(fru);
475 	}
476 	return (rt);
477 }
478 
479 static void
480 label_get_topo(void)
481 {
482 	int err;
483 
484 	topo_handle = topo_open(TOPO_VERSION, 0, &err);
485 	if (topo_handle) {
486 		topo_handle_uuid = topo_snap_hold(topo_handle, NULL, &err);
487 	}
488 }
489 
490 static void
491 label_release_topo(void)
492 {
493 	if (topo_handle_uuid)
494 		topo_hdl_strfree(topo_handle, topo_handle_uuid);
495 	if (topo_handle) {
496 		topo_snap_release(topo_handle);
497 		topo_close(topo_handle);
498 	}
499 }
500 
501 static char *
502 get_fmri_label(char *fru)
503 {
504 	topo_walk_t *twp;
505 	tgetlabel_data_t td;
506 	int err;
507 
508 	td.label = NULL;
509 	td.fru = fru;
510 	if (topo_handle == NULL)
511 		label_get_topo();
512 	if (topo_handle_uuid) {
513 		twp = topo_walk_init(topo_handle, FM_FMRI_SCHEME_HC,
514 		    tgetlabel, &td, &err);
515 		if (twp) {
516 			topo_walk_step(twp, TOPO_WALK_CHILD);
517 			topo_walk_fini(twp);
518 		}
519 	}
520 	return (td.label);
521 }
522 
523 static char *
524 get_nvl2str_topo(nvlist_t *nvl)
525 {
526 	char *name = NULL;
527 	char *tname;
528 	int err;
529 	char *scheme = NULL;
530 	char *mod_name = NULL;
531 	char buf[128];
532 
533 	if (topo_handle == NULL)
534 		label_get_topo();
535 	if (topo_fmri_nvl2str(topo_handle, nvl, &tname, &err) == 0) {
536 		name = strdup(tname);
537 		topo_hdl_strfree(topo_handle, tname);
538 	} else {
539 		(void) nvlist_lookup_string(nvl, FM_FMRI_SCHEME, &scheme);
540 		(void) nvlist_lookup_string(nvl, FM_FMRI_MOD_NAME, &mod_name);
541 		if (scheme && strcmp(scheme, FM_FMRI_SCHEME_FMD) == 0 &&
542 		    mod_name) {
543 			(void) snprintf(buf, sizeof (buf), "%s:///module/%s",
544 			    scheme, mod_name);
545 			name = strdup(buf);
546 		}
547 	}
548 	return (name);
549 }
550 
551 static int
552 set_priority(char *s)
553 {
554 	int rt = 0;
555 
556 	if (s) {
557 		if (strcmp(s, "Minor") == 0)
558 			rt = 1;
559 		else if (strcmp(s, "Major") == 0)
560 			rt = 10;
561 		else if (strcmp(s, "Critical") == 0)
562 			rt = 100;
563 	}
564 	return (rt);
565 }
566 
567 static int
568 cmp_priority(char *s1, char *s2, uint64_t t1, uint64_t t2, uint8_t p1,
569     uint8_t p2)
570 {
571 	int r1, r2;
572 	int rt;
573 
574 	r1 = set_priority(s1);
575 	r2 = set_priority(s2);
576 	rt = r1 - r2;
577 	if (rt == 0) {
578 		if (t1 > t2)
579 			rt = 1;
580 		else if (t1 < t2)
581 			rt = -1;
582 		else
583 			rt = p1 - p2;
584 	}
585 	return (rt);
586 }
587 
588 /*
589  * merge two lists into one, by comparing enties in new and moving into list if
590  * name is not there or free off memory for names which are already there
591  * add_pct indicates if pct is the sum or highest pct
592  */
593 static name_list_t *
594 merge_name_list(name_list_t **list, name_list_t *new, int add_pct)
595 {
596 	name_list_t *lp, *np, *sp, *rt = NULL;
597 	int max_pct;
598 
599 	rt = *list;
600 	np = new;
601 	while (np) {
602 		lp = *list;
603 		while (lp) {
604 			if (strcmp(lp->name, np->name) == 0)
605 				break;
606 			lp = lp->next;
607 			if (lp == *list)
608 				lp = NULL;
609 		}
610 		if (np->next == new)
611 			sp = NULL;
612 		else
613 			sp = np->next;
614 		if (lp) {
615 			lp->status |= (np->status & FM_SUSPECT_FAULTY);
616 			if (add_pct) {
617 				lp->pct += np->pct;
618 				lp->count += np->count;
619 			} else if (np->pct > lp->pct) {
620 				lp->pct = np->pct;
621 			}
622 			max_pct = np->max_pct;
623 			if (np->label)
624 				free(np->label);
625 			free(np->name);
626 			free(np);
627 			np = NULL;
628 			if (max_pct > lp->max_pct) {
629 				lp->max_pct = max_pct;
630 				if (lp->max_pct > lp->prev->max_pct &&
631 				    lp != *list) {
632 					lp->prev->next = lp->next;
633 					lp->next->prev = lp->prev;
634 					np = lp;
635 				}
636 			}
637 		}
638 		if (np) {
639 			lp = *list;
640 			if (lp) {
641 				if (np->max_pct > lp->max_pct) {
642 					np->next = lp;
643 					np->prev = lp->prev;
644 					lp->prev->next = np;
645 					lp->prev = np;
646 					*list = np;
647 					rt = np;
648 				} else {
649 					lp = lp->next;
650 					while (lp != *list &&
651 					    np->max_pct < lp->max_pct) {
652 						lp = lp->next;
653 					}
654 					np->next = lp;
655 					np->prev = lp->prev;
656 					lp->prev->next = np;
657 					lp->prev = np;
658 				}
659 			} else {
660 				*list = np;
661 				np->next = np;
662 				np->prev = np;
663 				rt = np;
664 			}
665 		}
666 		np = sp;
667 	}
668 	return (rt);
669 }
670 
671 /*
672  * compare entries in two lists return true if the two lists have identical
673  * content. The two lists may not have entries in the same order, so we compare
674  * the size of the list as well as trying to find every entry from one list in
675  * the other.
676  */
677 static int
678 cmp_name_list(name_list_t *lxp1, name_list_t *lxp2)
679 {
680 	name_list_t *lp1, *lp2;
681 	int l1 = 0, l2 = 0, common = 0;
682 
683 	lp2 = lxp2;
684 	while (lp2) {
685 		l2++;
686 		lp2 = lp2->next;
687 		if (lp2 == lxp2)
688 			break;
689 	}
690 	lp1 = lxp1;
691 	while (lp1) {
692 		l1++;
693 		lp2 = lxp2;
694 		while (lp2) {
695 			if (strcmp(lp2->name, lp1->name) == 0) {
696 				common++;
697 				break;
698 			}
699 			lp2 = lp2->next;
700 			if (lp2 == lxp2)
701 				break;
702 		}
703 		lp1 = lp1->next;
704 		if (lp1 == lxp1)
705 			break;
706 	}
707 	if (l1 == l2 && l2 == common)
708 		return (0);
709 	else
710 		return (1);
711 }
712 
713 static name_list_t *
714 alloc_name_list(char *name, uint8_t pct)
715 {
716 	name_list_t *nlp;
717 
718 	nlp = malloc(sizeof (*nlp));
719 	nlp->name = strdup(name);
720 	nlp->pct = pct;
721 	nlp->max_pct = pct;
722 	nlp->count = 1;
723 	nlp->next = nlp;
724 	nlp->prev = nlp;
725 	nlp->status = 0;
726 	nlp->label = NULL;
727 	return (nlp);
728 }
729 
730 static void
731 free_name_list(name_list_t *list)
732 {
733 	name_list_t *next = list;
734 	name_list_t *lp;
735 
736 	if (list) {
737 		do {
738 			lp = next;
739 			next = lp->next;
740 			if (lp->label)
741 				free(lp->label);
742 			free(lp->name);
743 			free(lp);
744 		} while (next != list);
745 	}
746 }
747 
748 static status_record_t *
749 new_record_init(uurec_t *uurec_p, char *msgid, name_list_t *class,
750     name_list_t *fru, name_list_t *asru, name_list_t *resource,
751     name_list_t *serial, const char *url, boolean_t not_suppressed,
752     hostid_t *hostid)
753 {
754 	status_record_t *status_rec_p;
755 
756 	status_rec_p = (status_record_t *)malloc(sizeof (status_record_t));
757 	status_rec_p->nrecs = 1;
758 	status_rec_p->host = hostid;
759 	status_rec_p->uurec = uurec_p;
760 	uurec_p->next = NULL;
761 	uurec_p->prev = NULL;
762 	uurec_p->asru = asru;
763 	status_rec_p->severity = get_dict_msg(msgid, "severity", 1, 0);
764 	status_rec_p->class = class;
765 	status_rec_p->fru = fru;
766 	status_rec_p->asru = asru;
767 	status_rec_p->resource = resource;
768 	status_rec_p->serial = serial;
769 	status_rec_p->url = url ? strdup(url) : NULL;
770 	status_rec_p->msgid = strdup(msgid);
771 	status_rec_p->not_suppressed = not_suppressed;
772 	return (status_rec_p);
773 }
774 
775 /*
776  * add record to given list maintaining order higher priority first.
777  */
778 static void
779 add_rec_list(status_record_t *status_rec_p, sr_list_t **list_pp)
780 {
781 	sr_list_t *tp, *np, *sp;
782 	int order;
783 	uint64_t sec;
784 
785 	np = malloc(sizeof (sr_list_t));
786 	np->status_record = status_rec_p;
787 	sec = status_rec_p->uurec->sec;
788 	if ((sp = *list_pp) == NULL) {
789 		*list_pp = np;
790 		np->next = np;
791 		np->prev = np;
792 	} else {
793 		/* insert new record in front of lower priority */
794 		tp = sp;
795 		order = cmp_priority(status_rec_p->severity,
796 		    sp->status_record->severity, sec,
797 		    tp->status_record->uurec->sec, 0, 0);
798 		if (order > 0) {
799 			*list_pp = np;
800 		} else {
801 			tp = sp->next;
802 			while (tp != sp &&
803 			    cmp_priority(status_rec_p->severity,
804 			    tp->status_record->severity, sec,
805 			    tp->status_record->uurec->sec, 0, 0)) {
806 				tp = tp->next;
807 			}
808 		}
809 		np->next = tp;
810 		np->prev = tp->prev;
811 		tp->prev->next = np;
812 		tp->prev = np;
813 	}
814 }
815 
816 static void
817 add_resource(status_record_t *status_rec_p, resource_list_t **rp,
818     resource_list_t *np)
819 {
820 	int order;
821 	uint64_t sec;
822 	resource_list_t *sp, *tp;
823 	status_record_t *srp;
824 	char *severity = status_rec_p->severity;
825 
826 	add_rec_list(status_rec_p, &np->status_rec_list);
827 	if ((sp = *rp) == NULL) {
828 		np->next = np;
829 		np->prev = np;
830 		*rp = np;
831 	} else {
832 		/*
833 		 * insert new record in front of lower priority
834 		 */
835 		tp = sp->next;
836 		srp = sp->status_rec_list->status_record;
837 		sec = status_rec_p->uurec->sec;
838 		order = cmp_priority(severity, srp->severity, sec,
839 		    srp->uurec->sec, np->max_pct, sp->max_pct);
840 		if (order > 0) {
841 			*rp = np;
842 		} else {
843 			srp = tp->status_rec_list->status_record;
844 			while (tp != sp &&
845 			    cmp_priority(severity, srp->severity, sec,
846 			    srp->uurec->sec, np->max_pct, sp->max_pct) < 0) {
847 				tp = tp->next;
848 				srp = tp->status_rec_list->status_record;
849 			}
850 		}
851 		np->next = tp;
852 		np->prev = tp->prev;
853 		tp->prev->next = np;
854 		tp->prev = np;
855 	}
856 }
857 
858 static void
859 add_resource_list(status_record_t *status_rec_p, name_list_t *fp,
860     resource_list_t **rpp)
861 {
862 	int order;
863 	resource_list_t *np, *end;
864 	status_record_t *srp;
865 
866 	np = *rpp;
867 	end = np;
868 	while (np) {
869 		if (strcmp(fp->name, np->resource) == 0) {
870 			np->not_suppressed |= status_rec_p->not_suppressed;
871 			srp = np->status_rec_list->status_record;
872 			order = cmp_priority(status_rec_p->severity,
873 			    srp->severity, status_rec_p->uurec->sec,
874 			    srp->uurec->sec, fp->max_pct, np->max_pct);
875 			if (order > 0 && np != end) {
876 				/*
877 				 * remove from list and add again using
878 				 * new priority
879 				 */
880 				np->prev->next = np->next;
881 				np->next->prev = np->prev;
882 				add_resource(status_rec_p,
883 				    rpp, np);
884 			} else {
885 				add_rec_list(status_rec_p,
886 				    &np->status_rec_list);
887 			}
888 			break;
889 		}
890 		np = np->next;
891 		if (np == end) {
892 			np = NULL;
893 			break;
894 		}
895 	}
896 	if (np == NULL) {
897 		np = malloc(sizeof (resource_list_t));
898 		np->resource = fp->name;
899 		np->not_suppressed = status_rec_p->not_suppressed;
900 		np->status_rec_list = NULL;
901 		np->max_pct = fp->max_pct;
902 		add_resource(status_rec_p, rpp, np);
903 	}
904 }
905 
906 static void
907 add_list(status_record_t *status_rec_p, name_list_t *listp,
908     resource_list_t **glistp)
909 {
910 	name_list_t *fp, *end;
911 
912 	fp = listp;
913 	end = fp;
914 	while (fp) {
915 		add_resource_list(status_rec_p, fp, glistp);
916 		fp = fp->next;
917 		if (fp == end)
918 			break;
919 	}
920 }
921 
922 /*
923  * add record to rec, fru and asru lists.
924  */
925 static void
926 catalog_new_record(uurec_t *uurec_p, char *msgid, name_list_t *class,
927     name_list_t *fru, name_list_t *asru, name_list_t *resource,
928     name_list_t *serial, const char *url, boolean_t not_suppressed,
929     hostid_t *hostid)
930 {
931 	status_record_t *status_rec_p;
932 
933 	status_rec_p = new_record_init(uurec_p, msgid, class, fru, asru,
934 	    resource, serial, url, not_suppressed, hostid);
935 	add_rec_list(status_rec_p, &status_rec_list);
936 	if (status_rec_p->fru)
937 		add_list(status_rec_p, status_rec_p->fru, &status_fru_list);
938 	if (status_rec_p->asru)
939 		add_list(status_rec_p, status_rec_p->asru, &status_asru_list);
940 }
941 
942 /*
943  * add uuid and diagnoses time to an existing record for similar fault on the
944  * same fru
945  */
946 static void
947 catalog_merge_record(status_record_t *status_rec_p, uurec_t *uurec_p,
948     name_list_t *asru, name_list_t *resource, name_list_t *serial,
949     const char *url, boolean_t not_suppressed)
950 {
951 	uurec_t *uurec1_p;
952 
953 	status_rec_p->nrecs++;
954 	/* add uurec in time order */
955 	if (status_rec_p->uurec->sec > uurec_p->sec) {
956 		uurec_p->next = status_rec_p->uurec;
957 		uurec_p->prev = NULL;
958 		status_rec_p->uurec = uurec_p;
959 	} else {
960 		uurec1_p = status_rec_p->uurec;
961 		while (uurec1_p->next && uurec1_p->next->sec <= uurec_p->sec)
962 			uurec1_p = uurec1_p->next;
963 		if (uurec1_p->next)
964 			uurec1_p->next->prev = uurec_p;
965 		uurec_p->next = uurec1_p->next;
966 		uurec_p->prev = uurec1_p;
967 		uurec1_p->next = uurec_p;
968 	}
969 	if (status_rec_p->url == NULL && url != NULL)
970 		status_rec_p->url = strdup(url);
971 	status_rec_p->not_suppressed |= not_suppressed;
972 	uurec_p->asru = merge_name_list(&status_rec_p->asru, asru, 0);
973 	(void) merge_name_list(&status_rec_p->resource, resource, 0);
974 	(void) merge_name_list(&status_rec_p->serial, serial, 0);
975 }
976 
977 static status_record_t *
978 record_in_catalog(name_list_t *class, name_list_t *fru,
979     char *msgid, hostid_t *host)
980 {
981 	sr_list_t *status_rec_p;
982 	status_record_t *srp = NULL;
983 
984 	status_rec_p = status_rec_list;
985 	while (status_rec_p) {
986 		srp = status_rec_p->status_record;
987 		if (host == srp->host &&
988 		    cmp_name_list(class, srp->class) == 0 &&
989 		    cmp_name_list(fru, srp->fru) == 0 &&
990 		    strcmp(msgid, srp->msgid) == 0)
991 			break;
992 		if (status_rec_p->next == status_rec_list) {
993 			srp = NULL;
994 			break;
995 		} else {
996 			status_rec_p = status_rec_p->next;
997 		}
998 	}
999 	return (srp);
1000 }
1001 
1002 static void
1003 get_serial_no(nvlist_t *nvl, name_list_t **serial_p, uint8_t pct)
1004 {
1005 	char *name;
1006 	char *serial = NULL;
1007 	char **lserial = NULL;
1008 	uint64_t serint;
1009 	name_list_t *nlp;
1010 	int j;
1011 	uint_t nelem;
1012 	char buf[64];
1013 
1014 	if (nvlist_lookup_string(nvl, FM_FMRI_SCHEME, &name) == 0) {
1015 		if (strcmp(name, FM_FMRI_SCHEME_CPU) == 0) {
1016 			if (nvlist_lookup_uint64(nvl, FM_FMRI_CPU_SERIAL_ID,
1017 			    &serint) == 0) {
1018 				(void) snprintf(buf, sizeof (buf), "%llX",
1019 				    serint);
1020 				nlp = alloc_name_list(buf, pct);
1021 				(void) merge_name_list(serial_p, nlp, 1);
1022 			}
1023 		} else if (strcmp(name, FM_FMRI_SCHEME_MEM) == 0) {
1024 			if (nvlist_lookup_string_array(nvl,
1025 			    FM_FMRI_MEM_SERIAL_ID, &lserial, &nelem) == 0) {
1026 				nlp = alloc_name_list(lserial[0], pct);
1027 				for (j = 1; j < nelem; j++) {
1028 					name_list_t *n1lp;
1029 					n1lp = alloc_name_list(lserial[j], pct);
1030 					(void) merge_name_list(&nlp, n1lp, 1);
1031 				}
1032 				(void) merge_name_list(serial_p, nlp, 1);
1033 			}
1034 		} else if (strcmp(name, FM_FMRI_SCHEME_HC) == 0) {
1035 			if (nvlist_lookup_string(nvl, FM_FMRI_HC_SERIAL_ID,
1036 			    &serial) == 0) {
1037 				nlp = alloc_name_list(serial, pct);
1038 				(void) merge_name_list(serial_p, nlp, 1);
1039 			}
1040 		}
1041 	}
1042 }
1043 
1044 static void
1045 extract_record_info(nvlist_t *nvl, name_list_t **class_p,
1046     name_list_t **fru_p, name_list_t **serial_p,
1047     name_list_t **resource_p, name_list_t **asru_p, uint8_t status)
1048 {
1049 	nvlist_t *lfru, *lasru, *rsrc;
1050 	name_list_t *nlp;
1051 	char *name;
1052 	uint8_t lpct = 0;
1053 	char *lclass = NULL;
1054 	char *label;
1055 
1056 	(void) nvlist_lookup_uint8(nvl, FM_FAULT_CERTAINTY, &lpct);
1057 	if (nvlist_lookup_string(nvl, FM_CLASS, &lclass) == 0) {
1058 		nlp = alloc_name_list(lclass, lpct);
1059 		(void) merge_name_list(class_p, nlp, 1);
1060 	}
1061 	if (nvlist_lookup_nvlist(nvl, FM_FAULT_FRU, &lfru) == 0) {
1062 		name = get_nvl2str_topo(lfru);
1063 		if (name != NULL) {
1064 			nlp = alloc_name_list(name, lpct);
1065 			free(name);
1066 			if (nvlist_lookup_string(nvl, FM_FAULT_LOCATION,
1067 			    &label) == 0)
1068 				nlp->label = strdup(label);
1069 			(void) merge_name_list(fru_p, nlp, 1);
1070 		}
1071 		get_serial_no(lfru, serial_p, lpct);
1072 	}
1073 	if (nvlist_lookup_nvlist(nvl, FM_FAULT_ASRU, &lasru) == 0) {
1074 		name = get_nvl2str_topo(lasru);
1075 		if (name != NULL) {
1076 			nlp = alloc_name_list(name, lpct);
1077 			nlp->status = status & ~FM_SUSPECT_NOT_PRESENT;
1078 			free(name);
1079 			(void) merge_name_list(asru_p, nlp, 1);
1080 		}
1081 		get_serial_no(lasru, serial_p, lpct);
1082 	}
1083 	if (nvlist_lookup_nvlist(nvl, FM_FAULT_RESOURCE, &rsrc) == 0) {
1084 		name = get_nvl2str_topo(rsrc);
1085 		if (name != NULL) {
1086 			nlp = alloc_name_list(name, lpct);
1087 			free(name);
1088 			(void) merge_name_list(resource_p, nlp, 1);
1089 		}
1090 	}
1091 }
1092 
1093 static void
1094 add_fault_record_to_catalog(nvlist_t *nvl, uint64_t sec, char *uuid,
1095     const char *url)
1096 {
1097 	char *msgid = "-";
1098 	uint_t i, size = 0;
1099 	name_list_t *class = NULL, *resource = NULL;
1100 	name_list_t *asru = NULL, *fru = NULL, *serial = NULL;
1101 	nvlist_t **nva;
1102 	uint8_t *ba;
1103 	status_record_t *status_rec_p;
1104 	uurec_t *uurec_p;
1105 	hostid_t *host;
1106 	boolean_t not_suppressed = 1;
1107 	boolean_t any_present = 0;
1108 
1109 	(void) nvlist_lookup_string(nvl, FM_SUSPECT_DIAG_CODE, &msgid);
1110 	(void) nvlist_lookup_uint32(nvl, FM_SUSPECT_FAULT_SZ, &size);
1111 	(void) nvlist_lookup_boolean_value(nvl, FM_SUSPECT_MESSAGE,
1112 	    &not_suppressed);
1113 
1114 	if (size != 0) {
1115 		(void) nvlist_lookup_nvlist_array(nvl, FM_SUSPECT_FAULT_LIST,
1116 		    &nva, &size);
1117 		(void) nvlist_lookup_uint8_array(nvl, FM_SUSPECT_FAULT_STATUS,
1118 		    &ba, &size);
1119 		for (i = 0; i < size; i++) {
1120 			extract_record_info(nva[i], &class, &fru, &serial,
1121 			    &resource, &asru, ba[i]);
1122 			if (!(ba[i] & FM_SUSPECT_NOT_PRESENT) &&
1123 			    (ba[i] & FM_SUSPECT_FAULTY))
1124 				any_present = 1;
1125 		}
1126 		/*
1127 		 * also suppress if no resources present
1128 		 */
1129 		if (any_present == 0)
1130 			not_suppressed = 0;
1131 	}
1132 
1133 	uurec_p = (uurec_t *)malloc(sizeof (uurec_t));
1134 	uurec_p->uuid = strdup(uuid);
1135 	uurec_p->sec = sec;
1136 	uurec_p->ari_uuid_list = NULL;
1137 	host = find_hostid(nvl);
1138 	if (not_suppressed && !opt_g)
1139 		status_rec_p = NULL;
1140 	else
1141 		status_rec_p = record_in_catalog(class, fru, msgid, host);
1142 	if (status_rec_p) {
1143 		catalog_merge_record(status_rec_p, uurec_p, asru, resource,
1144 		    serial, url, not_suppressed);
1145 		free_name_list(class);
1146 		free_name_list(fru);
1147 	} else {
1148 		catalog_new_record(uurec_p, msgid, class, fru, asru,
1149 		    resource, serial, url, not_suppressed, host);
1150 	}
1151 }
1152 
1153 static void
1154 update_asru_state_in_catalog(const char *uuid, const char *ari_uuid)
1155 {
1156 	sr_list_t *srp;
1157 	uurec_t *uurp;
1158 	ari_list_t *ari_list;
1159 
1160 	srp = status_rec_list;
1161 	if (srp) {
1162 		for (;;) {
1163 			uurp = srp->status_record->uurec;
1164 			while (uurp) {
1165 				if (strcmp(uuid, uurp->uuid) == 0) {
1166 					ari_list = (ari_list_t *)
1167 					    malloc(sizeof (ari_list_t));
1168 					ari_list->ari_uuid = strdup(ari_uuid);
1169 					ari_list->next = uurp->ari_uuid_list;
1170 					uurp->ari_uuid_list = ari_list;
1171 					return;
1172 				}
1173 				uurp = uurp->next;
1174 			}
1175 			if (srp->next == status_rec_list)
1176 				break;
1177 			srp = srp->next;
1178 		}
1179 	}
1180 }
1181 
1182 static void
1183 print_line(char *label, char *buf)
1184 {
1185 	char *cp, *ep, *wp;
1186 	char c;
1187 	int i;
1188 	int lsz;
1189 	char *padding;
1190 
1191 	lsz = strlen(label);
1192 	padding = malloc(lsz + 1);
1193 	for (i = 0; i < lsz; i++)
1194 		padding[i] = ' ';
1195 	padding[i] = 0;
1196 	cp = buf;
1197 	ep = buf;
1198 	c = *ep;
1199 	(void) printf("\n");
1200 	while (c) {
1201 		i = lsz;
1202 		wp = NULL;
1203 		while ((c = *ep) != NULL && (wp == NULL || i < 80)) {
1204 			if (c == ' ')
1205 				wp = ep;
1206 			else if (c == '\n') {
1207 				i = 0;
1208 				*ep = 0;
1209 				do {
1210 					ep++;
1211 				} while ((c = *ep) != NULL && c == ' ');
1212 				break;
1213 			}
1214 			ep++;
1215 			i++;
1216 		}
1217 		if (i >= 80 && wp) {
1218 			*wp = 0;
1219 			ep = wp + 1;
1220 			c = *ep;
1221 		}
1222 		(void) printf("%s%s\n", label, cp);
1223 		cp = ep;
1224 		label = padding;
1225 	}
1226 	free(padding);
1227 }
1228 
1229 static void
1230 print_dict_info(char *msgid, char *url)
1231 {
1232 	const char *cp;
1233 	char *l_url;
1234 	char *buf;
1235 	int bufsz;
1236 
1237 	cp = get_dict_msg(msgid, "description", 0, 1);
1238 	if (cp) {
1239 		if (url)
1240 			l_url = url;
1241 		else
1242 			l_url = get_dict_url(msgid);
1243 		bufsz = strlen(cp) + strlen(l_url) + 1;
1244 		buf = malloc(bufsz);
1245 		(void) snprintf(buf, bufsz, cp, l_url);
1246 		print_line(dgettext("FMD", "Description : "), buf);
1247 		free(buf);
1248 		if (!url)
1249 			free(l_url);
1250 	}
1251 	cp = get_dict_msg(msgid, "response", 0, 1);
1252 	if (cp) {
1253 		buf = strdup(cp);
1254 		print_line(dgettext("FMD", "Response    : "), buf);
1255 		free(buf);
1256 	}
1257 	cp = get_dict_msg(msgid, "impact", 0, 1);
1258 	if (cp) {
1259 		buf = strdup(cp);
1260 		print_line(dgettext("FMD", "Impact      : "), buf);
1261 		free(buf);
1262 	}
1263 	cp = get_dict_msg(msgid, "action", 0, 1);
1264 	if (cp) {
1265 		buf = strdup(cp);
1266 		print_line(dgettext("FMD", "Action      : "), buf);
1267 		free(buf);
1268 	}
1269 }
1270 
1271 static void
1272 print_name(name_list_t *list, char *(func)(char *), char *padding, int *np,
1273     int pct, int full)
1274 {
1275 	char *name, *fru = NULL;
1276 
1277 	name = list->name;
1278 	if (func)
1279 		fru = func(list->name);
1280 	if (fru) {
1281 		(void) printf("%s \"%s\" (%s)", padding, fru, name);
1282 		*np += 1;
1283 		free(fru);
1284 	} else {
1285 		(void) printf("%s %s", padding, name);
1286 		*np += 1;
1287 	}
1288 	if (list->pct && pct > 0 && pct < 100) {
1289 		if (list->count > 1) {
1290 			if (full) {
1291 				(void) printf(" %d @ %s %d%%\n", list->count,
1292 				    dgettext("FMD", "max"),
1293 				    list->max_pct);
1294 			} else {
1295 				(void) printf(" %s %d%%\n",
1296 				    dgettext("FMD", "max"),
1297 				    list->max_pct);
1298 			}
1299 		} else {
1300 			(void) printf(" %d%%\n", list->pct);
1301 		}
1302 	} else {
1303 		(void) printf("\n");
1304 	}
1305 }
1306 
1307 static void
1308 print_asru_status(int status, char *label)
1309 {
1310 	char *msg = NULL;
1311 
1312 	switch (status) {
1313 	case 0:
1314 		msg = dgettext("FMD", "ok and in service");
1315 		break;
1316 	case FM_SUSPECT_FAULTY:
1317 		msg = dgettext("FMD", "degraded but still in service");
1318 		break;
1319 	case FM_SUSPECT_UNUSABLE:
1320 		msg = dgettext("FMD", "unknown, not present or disabled");
1321 		break;
1322 	case FM_SUSPECT_FAULTY | FM_SUSPECT_UNUSABLE:
1323 		msg = dgettext("FMD", "faulted and taken out of service");
1324 		break;
1325 	default:
1326 		break;
1327 	}
1328 	if (msg) {
1329 		(void) printf("%s     %s\n", label, msg);
1330 	}
1331 }
1332 
1333 static void
1334 print_name_list(name_list_t *list, char *label, char *(func)(char *),
1335     int limit, int pct, void (func1)(int, char *), int full)
1336 {
1337 	char *name, *fru = NULL;
1338 	char *padding;
1339 	int i, j, l, n;
1340 	name_list_t *end = list;
1341 
1342 	l = strlen(label);
1343 	padding = malloc(l + 1);
1344 	for (i = 0; i < l; i++)
1345 		padding[i] = ' ';
1346 	padding[l] = 0;
1347 	(void) printf("%s", label);
1348 	name = list->name;
1349 	if (func == NULL)
1350 		(void) printf(" %s", name);
1351 	else if (list->label)
1352 		(void) printf(" \"%s\" (%s)", list->label, name);
1353 	else {
1354 		fru = func(list->name);
1355 		if (fru) {
1356 			(void) printf(" \"%s\" (%s)", fru, name);
1357 			free(fru);
1358 		} else
1359 			(void) printf(" %s", name);
1360 	}
1361 	if (list->pct && pct > 0 && pct < 100) {
1362 		if (list->count > 1) {
1363 			if (full) {
1364 				(void) printf(" %d @ %s %d%%\n", list->count,
1365 				    dgettext("FMD", "max"), list->max_pct);
1366 			} else {
1367 				(void) printf(" %s %d%%\n",
1368 				    dgettext("FMD", "max"), list->max_pct);
1369 			}
1370 		} else {
1371 			(void) printf(" %d%%\n", list->pct);
1372 		}
1373 	} else {
1374 		(void) printf("\n");
1375 	}
1376 	if (func1)
1377 		func1(list->status, padding);
1378 	n = 1;
1379 	j = 0;
1380 	while ((list = list->next) != end) {
1381 		if (limit == 0 || n < limit) {
1382 			print_name(list, func, padding, &n, pct, full);
1383 			if (func1)
1384 				func1(list->status, padding);
1385 		} else
1386 			j++;
1387 	}
1388 	if (j == 1) {
1389 		print_name(list->prev, func, padding, &n, pct, full);
1390 	} else if (j > 1) {
1391 		(void) printf("%s... %d %s\n", padding, j,
1392 		    dgettext("FMD", "more entries suppressed,"
1393 		    " use -v option for full list"));
1394 	}
1395 	free(padding);
1396 }
1397 
1398 static int
1399 asru_same_status(name_list_t *list)
1400 {
1401 	name_list_t *end = list;
1402 	int status = list->status;
1403 
1404 	while ((list = list->next) != end) {
1405 		if (status == -1) {
1406 			status = list->status;
1407 			continue;
1408 		}
1409 		if (list->status != -1 && status != list->status) {
1410 			status = -1;
1411 			break;
1412 		}
1413 	}
1414 	return (status);
1415 }
1416 
1417 static int
1418 serial_in_fru(name_list_t *fru, name_list_t *serial)
1419 {
1420 	name_list_t *sp = serial;
1421 	name_list_t *fp;
1422 	int nserial = 0;
1423 	int found = 0;
1424 	char buf[128];
1425 
1426 	while (sp) {
1427 		fp = fru;
1428 		nserial++;
1429 		(void) snprintf(buf, sizeof (buf), "serial=%s", sp->name);
1430 		buf[sizeof (buf) - 1] = 0;
1431 		while (fp) {
1432 			if (strstr(fp->name, buf) != NULL) {
1433 				found++;
1434 				break;
1435 			}
1436 			fp = fp->next;
1437 			if (fp == fru)
1438 				break;
1439 		}
1440 		sp = sp->next;
1441 		if (sp == serial)
1442 			break;
1443 	}
1444 	return (found == nserial ? 1 : 0);
1445 }
1446 
1447 static void
1448 print_server_name(hostid_t *host, char *label)
1449 {
1450 	(void) printf("%s %s %s %s\n", label, host->server, host->platform,
1451 	    host->chassis ? host->chassis : "");
1452 }
1453 
1454 static void
1455 print_sup_record(status_record_t *srp, int opt_i, int full)
1456 {
1457 	char buf[32];
1458 	uurec_t *uurp = srp->uurec;
1459 	int n, j, k, max;
1460 	int status;
1461 	ari_list_t *ari_list;
1462 
1463 	n = 0;
1464 	max = max_fault;
1465 	if (max < 0) {
1466 		max = 0;
1467 	}
1468 	j = max / 2;
1469 	max -= j;
1470 	k = srp->nrecs - max;
1471 	while ((uurp = uurp->next) != NULL) {
1472 		if (full || n < j || n >= k || max_fault == 0 ||
1473 		    srp->nrecs == max_fault+1) {
1474 			if (opt_i) {
1475 				ari_list = uurp->ari_uuid_list;
1476 				while (ari_list) {
1477 					(void) printf("%-15s %s\n",
1478 					    format_date(buf, sizeof (buf),
1479 					    uurp->sec), ari_list->ari_uuid);
1480 					ari_list = ari_list->next;
1481 				}
1482 			} else {
1483 				(void) printf("%-15s %s\n",
1484 				    format_date(buf, sizeof (buf), uurp->sec),
1485 				    uurp->uuid);
1486 			}
1487 		} else if (n == j)
1488 			(void) printf("... %d %s\n", srp->nrecs - max_fault,
1489 			    dgettext("FMD", "more entries suppressed"));
1490 		n++;
1491 	}
1492 	(void) printf("\n");
1493 	if (n_server > 1)
1494 		print_server_name(srp->host, dgettext("FMD", "Host        :"));
1495 	if (srp->class)
1496 		print_name_list(srp->class,
1497 		    dgettext("FMD", "Fault class :"), NULL, 0, srp->class->pct,
1498 		    NULL, full);
1499 	if (srp->asru) {
1500 		status = asru_same_status(srp->asru);
1501 		if (status != -1) {
1502 			print_name_list(srp->asru,
1503 			    dgettext("FMD", "Affects     :"), NULL,
1504 			    full ? 0 : max_display, 0, NULL, full);
1505 			print_asru_status(status, "             ");
1506 		} else
1507 			print_name_list(srp->asru,
1508 			    dgettext("FMD", "Affects     :"), NULL,
1509 			    full ? 0 : max_display, 0, print_asru_status, full);
1510 	}
1511 	if (full || srp->fru == NULL) {
1512 		if (srp->resource) {
1513 			print_name_list(srp->resource,
1514 			    dgettext("FMD", "Problem in  :"),
1515 			    NULL, full ? 0 : max_display, 0, NULL, full);
1516 		}
1517 	}
1518 	if (srp->fru) {
1519 		print_name_list(srp->fru, dgettext("FMD", "FRU         :"),
1520 		    get_fmri_label, 0,
1521 		    srp->fru->pct == 100 ? 100 : srp->fru->max_pct,
1522 		    NULL, full);
1523 	}
1524 	if (srp->serial && !serial_in_fru(srp->fru, srp->serial) &&
1525 	    !serial_in_fru(srp->asru, srp->serial)) {
1526 		print_name_list(srp->serial, dgettext("FMD", "Serial ID.  :"),
1527 		    NULL, 0, 0, NULL, full);
1528 	}
1529 	print_dict_info(srp->msgid, srp->url);
1530 	(void) printf("\n");
1531 }
1532 
1533 static void
1534 print_status_record(status_record_t *srp, int summary, int opt_i, int full)
1535 {
1536 	char buf[32];
1537 	uurec_t *uurp = srp->uurec;
1538 	char *severity;
1539 	static int header = 0;
1540 	char *head;
1541 	ari_list_t *ari_list;
1542 
1543 	if (nlspath)
1544 		severity = get_dict_msg(srp->msgid, "severity", 1, 1);
1545 	else
1546 		severity = srp->severity;
1547 
1548 	if (!summary || !header) {
1549 		if (opt_i) {
1550 			head = "--------------- "
1551 			    "------------------------------------  "
1552 			    "-------------- ---------\n"
1553 			    "TIME            CACHE-ID"
1554 			    "                              MSG-ID"
1555 			    "         SEVERITY\n--------------- "
1556 			    "------------------------------------ "
1557 			    " -------------- ---------";
1558 		} else {
1559 			head = "--------------- "
1560 			    "------------------------------------  "
1561 			    "-------------- ---------\n"
1562 			    "TIME            EVENT-ID"
1563 			    "                              MSG-ID"
1564 			    "         SEVERITY\n--------------- "
1565 			    "------------------------------------ "
1566 			    " -------------- ---------";
1567 		}
1568 		(void) printf("%s\n", dgettext("FMD", head));
1569 		header = 1;
1570 	}
1571 	if (opt_i) {
1572 		ari_list = uurp->ari_uuid_list;
1573 		while (ari_list) {
1574 			(void) printf("%-15s %-37s %-14s %-9s\n",
1575 			    format_date(buf, sizeof (buf), uurp->sec),
1576 			    ari_list->ari_uuid, srp->msgid, severity);
1577 			ari_list = ari_list->next;
1578 		}
1579 	} else {
1580 		(void) printf("%-15s %-37s %-14s %-9s\n",
1581 		    format_date(buf, sizeof (buf), uurp->sec),
1582 		    uurp->uuid, srp->msgid, severity);
1583 	}
1584 
1585 	if (!summary)
1586 		print_sup_record(srp, opt_i, full);
1587 }
1588 
1589 static void
1590 print_catalog(int summary, int opt_a, int full, int opt_i, int page_feed)
1591 {
1592 	status_record_t *srp;
1593 	sr_list_t *slp;
1594 
1595 	slp = status_rec_list;
1596 	if (slp) {
1597 		for (;;) {
1598 			srp = slp->status_record;
1599 			if (opt_a || srp->not_suppressed) {
1600 				if (page_feed)
1601 					(void) printf("\f\n");
1602 				print_status_record(srp, summary, opt_i, full);
1603 			}
1604 			if (slp->next == status_rec_list)
1605 				break;
1606 			slp = slp->next;
1607 		}
1608 	}
1609 }
1610 
1611 static name_list_t *
1612 find_fru(status_record_t *srp, char *resource)
1613 {
1614 	name_list_t *rt = NULL;
1615 	name_list_t *fru = srp->fru;
1616 
1617 	while (fru) {
1618 		if (strcmp(resource, fru->name) == 0) {
1619 			rt = fru;
1620 			break;
1621 		}
1622 		fru = fru->next;
1623 		if (fru == srp->fru)
1624 			break;
1625 	}
1626 	return (rt);
1627 }
1628 
1629 static void
1630 print_fru_line(name_list_t *fru, char *uuid)
1631 {
1632 	if (fru->pct == 100) {
1633 		(void) printf("%s %d %s %d%%\n", uuid, fru->count,
1634 		    dgettext("FMD", "suspects in this FRU total certainty"),
1635 		    100);
1636 	} else {
1637 		(void) printf("%s %d %s %d%%\n", uuid, fru->count,
1638 		    dgettext("FMD", "suspects in this FRU max certainty"),
1639 		    fru->max_pct);
1640 	}
1641 }
1642 
1643 static void
1644 print_fru(int summary, int opt_a, int opt_i, int page_feed)
1645 {
1646 	resource_list_t *tp = status_fru_list;
1647 	status_record_t *srp;
1648 	sr_list_t *slp, *end;
1649 	char *msgid, *fru_label;
1650 	uurec_t *uurp;
1651 	name_list_t *fru;
1652 	ari_list_t *ari_list;
1653 
1654 	while (tp) {
1655 		if (opt_a || tp->not_suppressed) {
1656 			if (page_feed)
1657 				(void) printf("\f\n");
1658 			if (!summary)
1659 				(void) printf("-----------------------------"
1660 				    "---------------------------------------"
1661 				    "----------\n");
1662 			slp = tp->status_rec_list;
1663 			end = slp;
1664 			do {
1665 				srp = slp->status_record;
1666 				fru = find_fru(srp, tp->resource);
1667 				if (fru) {
1668 					if (fru->label)
1669 						(void) printf("\"%s\" (%s)\n",
1670 						    fru->label, fru->name);
1671 					else if (fru_label =
1672 					    get_fmri_label(fru->name)) {
1673 						(void) printf("\"%s\" (%s)\n",
1674 						    fru_label, fru->name);
1675 						free(fru_label);
1676 					} else
1677 						(void) printf("%s\n",
1678 						    fru->name);
1679 					break;
1680 				}
1681 				slp = slp->next;
1682 			} while (slp != end);
1683 
1684 			slp = tp->status_rec_list;
1685 			end = slp;
1686 			do {
1687 				srp = slp->status_record;
1688 				uurp = srp->uurec;
1689 				fru = find_fru(srp, tp->resource);
1690 				if (fru) {
1691 					if (opt_i) {
1692 						ari_list = uurp->ari_uuid_list;
1693 						while (ari_list) {
1694 							print_fru_line(fru,
1695 							    ari_list->ari_uuid);
1696 							ari_list =
1697 							    ari_list->next;
1698 						}
1699 					} else {
1700 						print_fru_line(fru, uurp->uuid);
1701 					}
1702 				}
1703 				slp = slp->next;
1704 			} while (slp != end);
1705 			if (!summary) {
1706 				slp = tp->status_rec_list;
1707 				end = slp;
1708 				srp = slp->status_record;
1709 				if (srp->serial &&
1710 				    !serial_in_fru(srp->fru, srp->serial)) {
1711 					print_name_list(srp->serial,
1712 					    dgettext("FMD", "Serial ID.  :"),
1713 					    NULL, 0, 0, NULL, 1);
1714 				}
1715 				msgid = NULL;
1716 				do {
1717 					if (msgid == NULL ||
1718 					    strcmp(msgid, srp->msgid) != 0) {
1719 						msgid = srp->msgid;
1720 						print_dict_info(srp->msgid,
1721 						    srp->url);
1722 					}
1723 					slp = slp->next;
1724 				} while (slp != end);
1725 			}
1726 		}
1727 		tp = tp->next;
1728 		if (tp == status_fru_list)
1729 			break;
1730 	}
1731 }
1732 
1733 static void
1734 print_asru(int opt_a)
1735 {
1736 	resource_list_t *tp = status_asru_list;
1737 	status_record_t *srp;
1738 	sr_list_t *slp, *end;
1739 	char *msg;
1740 	int status;
1741 	name_list_t *asru;
1742 
1743 	while (tp) {
1744 		if (opt_a || tp->not_suppressed) {
1745 			status = 0;
1746 			slp = tp->status_rec_list;
1747 			end = slp;
1748 			do {
1749 				srp = slp->status_record;
1750 				asru = srp->asru;
1751 				while (asru) {
1752 					if (strcmp(tp->resource,
1753 					    asru->name) == 0)
1754 						status |= asru->status;
1755 					asru = asru->next;
1756 					if (asru == srp->asru)
1757 						break;
1758 				}
1759 				slp = slp->next;
1760 			} while (slp != end);
1761 			switch (status) {
1762 			case 0:
1763 				msg = dgettext("FMD", "ok");
1764 				break;
1765 			case FM_SUSPECT_FAULTY:
1766 				msg = dgettext("FMD", "degraded");
1767 				break;
1768 			case FM_SUSPECT_UNUSABLE:
1769 				msg = dgettext("FMD", "unknown");
1770 				break;
1771 			case FM_SUSPECT_FAULTY | FM_SUSPECT_UNUSABLE:
1772 				msg = dgettext("FMD", "faulted");
1773 				break;
1774 			default:
1775 				msg = "";
1776 				break;
1777 			}
1778 			(void) printf("%-69s %s\n", tp->resource, msg);
1779 		}
1780 		tp = tp->next;
1781 		if (tp == status_asru_list)
1782 			break;
1783 	}
1784 }
1785 
1786 static int
1787 uuid_in_list(char *uuid, uurec_select_t *uurecp)
1788 {
1789 	while (uurecp) {
1790 		if (strcmp(uuid, uurecp->uuid) == 0)
1791 			return (1);
1792 		uurecp = uurecp->next;
1793 	}
1794 	return (0);
1795 }
1796 
1797 static int
1798 dfault_rec(const fmd_adm_caseinfo_t *acp, void *arg)
1799 {
1800 	int64_t *diag_time;
1801 	uint_t nelem;
1802 	int rt = 0;
1803 	char *uuid = "-";
1804 	uurec_select_t *uurecp = (uurec_select_t *)arg;
1805 
1806 	if (nvlist_lookup_int64_array(acp->aci_event, FM_SUSPECT_DIAG_TIME,
1807 	    &diag_time, &nelem) == 0 && nelem >= 2) {
1808 		(void) nvlist_lookup_string(acp->aci_event, FM_SUSPECT_UUID,
1809 		    &uuid);
1810 		if (uurecp == NULL || uuid_in_list(uuid, uurecp))
1811 			add_fault_record_to_catalog(acp->aci_event, *diag_time,
1812 			    uuid, acp->aci_url);
1813 	} else {
1814 		rt = -1;
1815 	}
1816 	return (rt);
1817 }
1818 
1819 /*ARGSUSED*/
1820 static int
1821 dstatus_rec(const fmd_adm_rsrcinfo_t *ari, void *unused)
1822 {
1823 	update_asru_state_in_catalog(ari->ari_case, ari->ari_uuid);
1824 	return (0);
1825 }
1826 
1827 static int
1828 get_cases_from_fmd(fmd_adm_t *adm, uurec_select_t *uurecp, int opt_i)
1829 {
1830 	int rt = FMADM_EXIT_SUCCESS;
1831 
1832 	/*
1833 	 * These calls may fail with Protocol error if message payload is to big
1834 	 */
1835 	if (fmd_adm_case_iter(adm, NULL, dfault_rec, uurecp) != 0)
1836 		die("failed to get case list from fmd");
1837 	if (opt_i && fmd_adm_rsrc_iter(adm, 1, dstatus_rec, NULL) != 0)
1838 		die("failed to get case status from fmd");
1839 	return (rt);
1840 }
1841 
1842 /*
1843  * fmadm faulty command
1844  *
1845  *	-a		show hidden fault records
1846  *	-f		show faulty fru's
1847  *	-g		force grouping of similar faults on the same fru
1848  *	-n		number of fault records to display
1849  *	-p		pipe output through pager
1850  *	-r		show faulty asru's
1851  *	-s		print summary of first fault
1852  *	-u		print listed uuid's only
1853  *	-v		full output
1854  */
1855 
1856 int
1857 cmd_faulty(fmd_adm_t *adm, int argc, char *argv[])
1858 {
1859 	int opt_a = 0, opt_v = 0, opt_p = 0, opt_s = 0, opt_r = 0, opt_f = 0;
1860 	int opt_i = 0;
1861 	char *pager;
1862 	FILE *fp;
1863 	int rt, c, stat;
1864 	uurec_select_t *tp;
1865 	uurec_select_t *uurecp = NULL;
1866 
1867 	catalog_setup();
1868 	while ((c = getopt(argc, argv, "afgin:prsu:v")) != EOF) {
1869 		switch (c) {
1870 		case 'a':
1871 			opt_a++;
1872 			break;
1873 		case 'f':
1874 			opt_f++;
1875 			break;
1876 		case 'g':
1877 			opt_g++;
1878 			break;
1879 		case 'i':
1880 			opt_i++;
1881 			break;
1882 		case 'n':
1883 			max_fault = atoi(optarg);
1884 			break;
1885 		case 'p':
1886 			opt_p++;
1887 			break;
1888 		case 'r':
1889 			opt_r++;
1890 			break;
1891 		case 's':
1892 			opt_s++;
1893 			break;
1894 		case 'u':
1895 			tp = (uurec_select_t *)malloc(sizeof (uurec_select_t));
1896 			tp->uuid = optarg;
1897 			tp->next = uurecp;
1898 			uurecp = tp;
1899 			opt_a = 1;
1900 			break;
1901 		case 'v':
1902 			opt_v++;
1903 			break;
1904 		default:
1905 			return (FMADM_EXIT_USAGE);
1906 		}
1907 	}
1908 	if (optind < argc)
1909 		return (FMADM_EXIT_USAGE);
1910 
1911 	rt = get_cases_from_fmd(adm, uurecp, opt_i);
1912 	if (opt_p) {
1913 		if ((pager = getenv("PAGER")) == NULL)
1914 			pager = "/usr/bin/more";
1915 		fp = popen(pager, "w");
1916 		if (fp == NULL) {
1917 			rt = FMADM_EXIT_ERROR;
1918 			opt_p = 0;
1919 		} else {
1920 			dup2(fileno(fp), 1);
1921 			setbuf(stdout, NULL);
1922 			(void) fclose(fp);
1923 		}
1924 	}
1925 	max_display = max_fault;
1926 	if (opt_f)
1927 		print_fru(opt_s, opt_a, opt_i, opt_p && !opt_s);
1928 	if (opt_r)
1929 		print_asru(opt_a);
1930 	if (opt_f == 0 && opt_r == 0)
1931 		print_catalog(opt_s, opt_a, opt_v, opt_i, opt_p && !opt_s);
1932 	label_release_topo();
1933 	if (opt_p) {
1934 		(void) fclose(stdout);
1935 		(void) wait(&stat);
1936 	}
1937 	return (rt);
1938 }
1939 
1940 int
1941 cmd_flush(fmd_adm_t *adm, int argc, char *argv[])
1942 {
1943 	int i, status = FMADM_EXIT_SUCCESS;
1944 
1945 	if (argc < 2 || (i = getopt(argc, argv, "")) != EOF)
1946 		return (FMADM_EXIT_USAGE);
1947 
1948 	for (i = 1; i < argc; i++) {
1949 		if (fmd_adm_rsrc_flush(adm, argv[i]) != 0) {
1950 			warn("failed to flush %s", argv[i]);
1951 			status = FMADM_EXIT_ERROR;
1952 		} else
1953 			note("flushed resource history for %s\n", argv[i]);
1954 	}
1955 
1956 	return (status);
1957 }
1958 
1959 int
1960 cmd_repair(fmd_adm_t *adm, int argc, char *argv[])
1961 {
1962 	int err;
1963 
1964 	if (getopt(argc, argv, "") != EOF)
1965 		return (FMADM_EXIT_USAGE);
1966 
1967 	if (argc - optind != 1)
1968 		return (FMADM_EXIT_USAGE);
1969 
1970 	/*
1971 	 * For now, we assume that if the input string contains a colon, it is
1972 	 * an FMRI and if it does not it is a UUID.  If things get more complex
1973 	 * in the future with multiple UUID formats, an FMRI parser can be
1974 	 * added here to differentiate the input argument appropriately.
1975 	 */
1976 	if (strchr(argv[optind], ':') != NULL)
1977 		err = fmd_adm_rsrc_repair(adm, argv[optind]);
1978 	else
1979 		err = fmd_adm_case_repair(adm, argv[optind]);
1980 
1981 	if (err != 0)
1982 		die("failed to record repair to %s", argv[optind]);
1983 
1984 	note("recorded repair to %s\n", argv[optind]);
1985 	return (FMADM_EXIT_SUCCESS);
1986 }
1987