xref: /titanic_51/usr/src/cmd/fm/fmdump/common/nvlrender.c (revision 2db6d663182655cb393dc2c15668bc9293364594)
1f6e214c7SGavin Maltby /*
2f6e214c7SGavin Maltby  * CDDL HEADER START
3f6e214c7SGavin Maltby  *
4f6e214c7SGavin Maltby  * The contents of this file are subject to the terms of the
5f6e214c7SGavin Maltby  * Common Development and Distribution License (the "License").
6f6e214c7SGavin Maltby  * You may not use this file except in compliance with the License.
7f6e214c7SGavin Maltby  *
8f6e214c7SGavin Maltby  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9f6e214c7SGavin Maltby  * or http://www.opensolaris.org/os/licensing.
10f6e214c7SGavin Maltby  * See the License for the specific language governing permissions
11f6e214c7SGavin Maltby  * and limitations under the License.
12f6e214c7SGavin Maltby  *
13f6e214c7SGavin Maltby  * When distributing Covered Code, include this CDDL HEADER in each
14f6e214c7SGavin Maltby  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15f6e214c7SGavin Maltby  * If applicable, add the following below this CDDL HEADER, with the
16f6e214c7SGavin Maltby  * fields enclosed by brackets "[]" replaced with your own identifying
17f6e214c7SGavin Maltby  * information: Portions Copyright [yyyy] [name of copyright owner]
18f6e214c7SGavin Maltby  *
19f6e214c7SGavin Maltby  * CDDL HEADER END
20f6e214c7SGavin Maltby  */
21f6e214c7SGavin Maltby /*
22f6e214c7SGavin Maltby  * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
23*2db6d663SJoshua M. Clulow  * Copyright (c) 2013, Joyent, Inc. All rights reserved.
24f6e214c7SGavin Maltby  */
25f6e214c7SGavin Maltby 
26f6e214c7SGavin Maltby /*
27f6e214c7SGavin Maltby  * Rendering functions for nvlist_prt that are of use to all types
28f6e214c7SGavin Maltby  * of log.
29f6e214c7SGavin Maltby  */
30f6e214c7SGavin Maltby 
31f6e214c7SGavin Maltby #include <fmdump.h>
32f6e214c7SGavin Maltby #include <stdio.h>
33f6e214c7SGavin Maltby #include <strings.h>
34f6e214c7SGavin Maltby 
35f6e214c7SGavin Maltby extern topo_hdl_t *fmd_fmri_topo_hold(int);
36f6e214c7SGavin Maltby 
37f6e214c7SGavin Maltby /*
38f6e214c7SGavin Maltby  * Can be appointed to be called for dumping all nvlist members of
39f6e214c7SGavin Maltby  * an nvlist we ask to print with nvlist_prt.  Return 0 if the
40f6e214c7SGavin Maltby  * nvlist is not recognized as an fmri, and default formatting
41f6e214c7SGavin Maltby  * will be applied; otherwise format as an fmri string and return 1.
42f6e214c7SGavin Maltby  */
43f6e214c7SGavin Maltby 
44f6e214c7SGavin Maltby /*ARGSUSED*/
45f6e214c7SGavin Maltby int
46f6e214c7SGavin Maltby fmdump_render_nvlist(nvlist_prtctl_t pctl, void *private, nvlist_t *nvl,
47f6e214c7SGavin Maltby     const char *name, nvlist_t *fmri)
48f6e214c7SGavin Maltby {
49f6e214c7SGavin Maltby 	topo_hdl_t *thp = fmd_fmri_topo_hold(TOPO_VERSION);
50f6e214c7SGavin Maltby 	FILE *fp = nvlist_prtctl_getdest(pctl);
51f6e214c7SGavin Maltby 	char *class, *fmristr = NULL;
52f6e214c7SGavin Maltby 	uint8_t version;
53f6e214c7SGavin Maltby 	int err;
54f6e214c7SGavin Maltby 
55f6e214c7SGavin Maltby 	if (nvlist_lookup_string(fmri, FM_FMRI_SCHEME, &class) != 0 ||
56f6e214c7SGavin Maltby 	    nvlist_lookup_uint8(fmri, FM_VERSION, &version) != 0)
57f6e214c7SGavin Maltby 		return (0);
58f6e214c7SGavin Maltby 
59f6e214c7SGavin Maltby 	/*
60f6e214c7SGavin Maltby 	 * Instead of hardcoding known FMRI classes here we'll try
61f6e214c7SGavin Maltby 	 * topo_fmri_nvl2str which should fail gracefully for invalid
62f6e214c7SGavin Maltby 	 * schemes (ie an nvlist that just happens to have the expected
63f6e214c7SGavin Maltby 	 * class and version members but that isn't an FMRI).
64f6e214c7SGavin Maltby 	 */
65f6e214c7SGavin Maltby 	if (topo_fmri_nvl2str(thp, fmri, &fmristr, &err) != 0 ||
66f6e214c7SGavin Maltby 	    fmristr == NULL)
67f6e214c7SGavin Maltby 		return (0);
68f6e214c7SGavin Maltby 
69f6e214c7SGavin Maltby 	nvlist_prtctl_doindent(pctl, 1);
70f6e214c7SGavin Maltby 	nvlist_prtctl_dofmt(pctl, NVLIST_FMT_MEMBER_NAME, name);
71f6e214c7SGavin Maltby 	(void) fprintf(fp, "%s", fmristr);
72f6e214c7SGavin Maltby 	topo_hdl_strfree(thp, fmristr);
73f6e214c7SGavin Maltby 
74f6e214c7SGavin Maltby 	return (1);
75f6e214c7SGavin Maltby }
76*2db6d663SJoshua M. Clulow 
77*2db6d663SJoshua M. Clulow /*
78*2db6d663SJoshua M. Clulow  * Thin wrapper around libnvpair's inbuilt JSON routine.  Simply dumps the
79*2db6d663SJoshua M. Clulow  * entire log record nvlist without any reformatting.
80*2db6d663SJoshua M. Clulow  */
81*2db6d663SJoshua M. Clulow 
82*2db6d663SJoshua M. Clulow /*ARGSUSED*/
83*2db6d663SJoshua M. Clulow int
84*2db6d663SJoshua M. Clulow fmdump_print_json(fmd_log_t *lp, const fmd_log_record_t *rp, FILE *fp)
85*2db6d663SJoshua M. Clulow {
86*2db6d663SJoshua M. Clulow 	if (nvlist_print_json(fp, rp->rec_nvl) != 0 || fprintf(fp, "\n") < 0 ||
87*2db6d663SJoshua M. Clulow 	    fflush(fp) != 0)
88*2db6d663SJoshua M. Clulow 		return (-1);
89*2db6d663SJoshua M. Clulow 
90*2db6d663SJoshua M. Clulow 	return (0);
91*2db6d663SJoshua M. Clulow }
92