f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby/*
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * CDDL HEADER START
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby *
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * The contents of this file are subject to the terms of the
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * Common Development and Distribution License (the "License").
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * You may not use this file except in compliance with the License.
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby *
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * or http://www.opensolaris.org/os/licensing.
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * See the License for the specific language governing permissions
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * and limitations under the License.
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby *
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * When distributing Covered Code, include this CDDL HEADER in each
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * If applicable, add the following below this CDDL HEADER, with the
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * fields enclosed by brackets "[]" replaced with your own identifying
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * information: Portions Copyright [yyyy] [name of copyright owner]
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby *
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * CDDL HEADER END
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby */
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby/*
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
2db6d663182655cb393dc2c15668bc9293364594Joshua M. Clulow * Copyright (c) 2013, Joyent, Inc. All rights reserved.
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby */
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby/*
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * Rendering functions for nvlist_prt that are of use to all types
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * of log.
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby */
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby#include <fmdump.h>
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby#include <stdio.h>
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby#include <strings.h>
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltbyextern topo_hdl_t *fmd_fmri_topo_hold(int);
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby/*
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * Can be appointed to be called for dumping all nvlist members of
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * an nvlist we ask to print with nvlist_prt. Return 0 if the
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * nvlist is not recognized as an fmri, and default formatting
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * will be applied; otherwise format as an fmri string and return 1.
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby */
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby/*ARGSUSED*/
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltbyint
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltbyfmdump_render_nvlist(nvlist_prtctl_t pctl, void *private, nvlist_t *nvl,
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby const char *name, nvlist_t *fmri)
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby{
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby topo_hdl_t *thp = fmd_fmri_topo_hold(TOPO_VERSION);
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby FILE *fp = nvlist_prtctl_getdest(pctl);
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby char *class, *fmristr = NULL;
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby uint8_t version;
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby int err;
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby if (nvlist_lookup_string(fmri, FM_FMRI_SCHEME, &class) != 0 ||
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby nvlist_lookup_uint8(fmri, FM_VERSION, &version) != 0)
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby return (0);
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby /*
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * Instead of hardcoding known FMRI classes here we'll try
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * topo_fmri_nvl2str which should fail gracefully for invalid
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * schemes (ie an nvlist that just happens to have the expected
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * class and version members but that isn't an FMRI).
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby */
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby if (topo_fmri_nvl2str(thp, fmri, &fmristr, &err) != 0 ||
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby fmristr == NULL)
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby return (0);
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby nvlist_prtctl_doindent(pctl, 1);
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby nvlist_prtctl_dofmt(pctl, NVLIST_FMT_MEMBER_NAME, name);
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby (void) fprintf(fp, "%s", fmristr);
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby topo_hdl_strfree(thp, fmristr);
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby return (1);
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby}
2db6d663182655cb393dc2c15668bc9293364594Joshua M. Clulow
2db6d663182655cb393dc2c15668bc9293364594Joshua M. Clulow/*
2db6d663182655cb393dc2c15668bc9293364594Joshua M. Clulow * Thin wrapper around libnvpair's inbuilt JSON routine. Simply dumps the
2db6d663182655cb393dc2c15668bc9293364594Joshua M. Clulow * entire log record nvlist without any reformatting.
2db6d663182655cb393dc2c15668bc9293364594Joshua M. Clulow */
2db6d663182655cb393dc2c15668bc9293364594Joshua M. Clulow
2db6d663182655cb393dc2c15668bc9293364594Joshua M. Clulow/*ARGSUSED*/
2db6d663182655cb393dc2c15668bc9293364594Joshua M. Clulowint
2db6d663182655cb393dc2c15668bc9293364594Joshua M. Clulowfmdump_print_json(fmd_log_t *lp, const fmd_log_record_t *rp, FILE *fp)
2db6d663182655cb393dc2c15668bc9293364594Joshua M. Clulow{
2db6d663182655cb393dc2c15668bc9293364594Joshua M. Clulow if (nvlist_print_json(fp, rp->rec_nvl) != 0 || fprintf(fp, "\n") < 0 ||
2db6d663182655cb393dc2c15668bc9293364594Joshua M. Clulow fflush(fp) != 0)
2db6d663182655cb393dc2c15668bc9293364594Joshua M. Clulow return (-1);
2db6d663182655cb393dc2c15668bc9293364594Joshua M. Clulow
2db6d663182655cb393dc2c15668bc9293364594Joshua M. Clulow return (0);
2db6d663182655cb393dc2c15668bc9293364594Joshua M. Clulow}