49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby/*
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby * CDDL HEADER START
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby *
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby * The contents of this file are subject to the terms of the
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby * Common Development and Distribution License (the "License").
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby * You may not use this file except in compliance with the License.
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby *
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby * or http://www.opensolaris.org/os/licensing.
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby * See the License for the specific language governing permissions
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby * and limitations under the License.
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby *
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby * When distributing Covered Code, include this CDDL HEADER in each
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby * If applicable, add the following below this CDDL HEADER, with the
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby * fields enclosed by brackets "[]" replaced with your own identifying
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby * information: Portions Copyright [yyyy] [name of copyright owner]
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby *
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby * CDDL HEADER END
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby */
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby/*
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby */
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby/*
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby * Subscription event access interfaces.
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby */
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby#include <sys/types.h>
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby#include <limits.h>
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby#include <atomic.h>
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby#include <libsysevent.h>
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby#include <umem.h>
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby#include <fm/libfmevent.h>
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby#include <sys/fm/protocol.h>
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby#include "fmev_impl.h"
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby#define FMEV_API_ENTER(iep, v) \
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby fmev_api_enter(fmev_shdl_cmn(((iep)->ei_hdl)), LIBFMEVENT_VERSION_##v)
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltbytypedef struct {
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby uint32_t ei_magic; /* _FMEVMAGIC */
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby volatile uint32_t ei_refcnt; /* reference count */
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby fmev_shdl_t ei_hdl; /* handle received on */
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby nvlist_t *ei_nvl; /* (duped) sysevent attribute list */
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby uint64_t ei_fmtime[2]; /* embedded protocol event time */
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby} fmev_impl_t;
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby#define FMEV2IMPL(ev) ((fmev_impl_t *)(ev))
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby#define IMPL2FMEV(iep) ((fmev_t)(iep))
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby#define _FMEVMAGIC 0x466d4576 /* "FmEv" */
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby#define EVENT_VALID(iep) ((iep)->ei_magic == _FMEVMAGIC && \
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby (iep)->ei_refcnt > 0 && fmev_shdl_valid((iep)->ei_hdl))
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby#define FM_TIME_SEC 0
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby#define FM_TIME_NSEC 1
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby/*
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby * Transform a received sysevent_t into an fmev_t.
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby */
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltbyuint64_t fmev_bad_attr, fmev_bad_tod, fmev_bad_class;
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltbyfmev_t
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltbyfmev_sysev2fmev(fmev_shdl_t hdl, sysevent_t *sep, char **clsp, nvlist_t **nvlp)
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby{
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby fmev_impl_t *iep;
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby uint64_t *tod;
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby uint_t nelem;
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby if ((iep = fmev_shdl_alloc(hdl, sizeof (*iep))) == NULL)
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby return (NULL);
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby /*
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby * sysevent_get_attr_list duplicates the nvlist - we free it
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby * in fmev_free when the reference count hits zero.
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby */
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby if (sysevent_get_attr_list(sep, &iep->ei_nvl) != 0) {
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby fmev_shdl_free(hdl, iep, sizeof (*iep));
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby fmev_bad_attr++;
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby return (NULL);
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby }
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby *nvlp = iep->ei_nvl;
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby if (nvlist_lookup_string(iep->ei_nvl, FM_CLASS, clsp) != 0) {
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby nvlist_free(iep->ei_nvl);
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby fmev_shdl_free(hdl, iep, sizeof (*iep));
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby fmev_bad_class++;
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby return (NULL);
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby }
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby if (nvlist_lookup_uint64_array(iep->ei_nvl, "__tod", &tod,
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby &nelem) != 0 || nelem != 2) {
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby nvlist_free(iep->ei_nvl);
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby fmev_shdl_free(hdl, iep, sizeof (*iep));
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby fmev_bad_tod++;
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby return (NULL);
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby }
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby iep->ei_fmtime[FM_TIME_SEC] = tod[0];
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby iep->ei_fmtime[FM_TIME_NSEC] = tod[1];
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby /*
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby * Now remove the fmd-private __tod and __ttl members.
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby */
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby (void) nvlist_remove_all(iep->ei_nvl, "__tod");
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby (void) nvlist_remove_all(iep->ei_nvl, "__ttl");
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby iep->ei_magic = _FMEVMAGIC;
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby iep->ei_hdl = hdl;
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby iep->ei_refcnt = 1;
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby ASSERT(EVENT_VALID(iep));
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby return (IMPL2FMEV(iep));
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby}
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltbystatic void
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltbyfmev_free(fmev_impl_t *iep)
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby{
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby ASSERT(iep->ei_refcnt == 0);
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby nvlist_free(iep->ei_nvl);
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby fmev_shdl_free(iep->ei_hdl, iep, sizeof (*iep));
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby}
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltbyvoid
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltbyfmev_hold(fmev_t ev)
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby{
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby fmev_impl_t *iep = FMEV2IMPL(ev);
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby ASSERT(EVENT_VALID(iep));
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby (void) FMEV_API_ENTER(iep, 1);
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby atomic_inc_32(&iep->ei_refcnt);
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby}
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltbyvoid
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltbyfmev_rele(fmev_t ev)
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby{
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby fmev_impl_t *iep = FMEV2IMPL(ev);
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby ASSERT(EVENT_VALID(iep));
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby (void) FMEV_API_ENTER(iep, 1);
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby if (atomic_dec_32_nv(&iep->ei_refcnt) == 0)
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby fmev_free(iep);
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby}
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltbyfmev_t
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltbyfmev_dup(fmev_t ev)
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby{
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby fmev_impl_t *iep = FMEV2IMPL(ev);
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby fmev_impl_t *cp;
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby ASSERT(EVENT_VALID(iep));
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby if (!FMEV_API_ENTER(iep, 1))
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby return (NULL); /* fmev_errno set */
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby if (ev == NULL) {
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby (void) fmev_seterr(FMEVERR_API);
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby return (NULL);
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby }
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby if ((cp = fmev_shdl_alloc(iep->ei_hdl, sizeof (*iep))) == NULL) {
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby (void) fmev_seterr(FMEVERR_ALLOC);
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby return (NULL);
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby }
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby if (nvlist_dup(iep->ei_nvl, &cp->ei_nvl, 0) != 0) {
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby fmev_shdl_free(iep->ei_hdl, cp, sizeof (*cp));
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby (void) fmev_seterr(FMEVERR_ALLOC);
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby return (NULL);
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby }
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby cp->ei_magic = _FMEVMAGIC;
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby cp->ei_hdl = iep->ei_hdl;
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby cp->ei_refcnt = 1;
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby return (IMPL2FMEV(cp));
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby}
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltbynvlist_t *
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltbyfmev_attr_list(fmev_t ev)
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby{
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby fmev_impl_t *iep = FMEV2IMPL(ev);
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby ASSERT(EVENT_VALID(iep));
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby if (!FMEV_API_ENTER(iep, 1))
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby return (NULL); /* fmev_errno set */
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby if (ev == NULL) {
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby (void) fmev_seterr(FMEVERR_API);
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby return (NULL);
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby } else if (iep->ei_nvl == NULL) {
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby (void) fmev_seterr(FMEVERR_MALFORMED_EVENT);
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby return (NULL);
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby }
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby return (iep->ei_nvl);
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby}
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltbyconst char *
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltbyfmev_class(fmev_t ev)
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby{
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby fmev_impl_t *iep = FMEV2IMPL(ev);
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby const char *class;
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby ASSERT(EVENT_VALID(iep));
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby if (!FMEV_API_ENTER(iep, 1))
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby return (NULL); /* fmev_errno set */
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby if (ev == NULL) {
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby (void) fmev_seterr(FMEVERR_API);
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby return ("");
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby }
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby if (nvlist_lookup_string(iep->ei_nvl, FM_CLASS, (char **)&class) != 0 ||
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby *class == '\0') {
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby (void) fmev_seterr(FMEVERR_MALFORMED_EVENT);
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby return ("");
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby }
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby return (class);
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby}
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltbyfmev_err_t
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltbyfmev_timespec(fmev_t ev, struct timespec *tp)
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby{
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby fmev_impl_t *iep = FMEV2IMPL(ev);
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby uint64_t timetlimit;
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby ASSERT(EVENT_VALID(iep));
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby if (!FMEV_API_ENTER(iep, 1))
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby return (fmev_errno);
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby#ifdef _LP64
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby timetlimit = INT64_MAX;
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby#else
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby timetlimit = INT32_MAX;
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby#endif
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby if (iep->ei_fmtime[FM_TIME_SEC] > timetlimit)
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby return (FMEVERR_OVERFLOW);
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby tp->tv_sec = (time_t)iep->ei_fmtime[FM_TIME_SEC];
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby tp->tv_nsec = (long)iep->ei_fmtime[FM_TIME_NSEC];
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby return (FMEV_SUCCESS);
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby}
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltbyuint64_t
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltbyfmev_time_sec(fmev_t ev)
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby{
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby return (FMEV2IMPL(ev)->ei_fmtime[FM_TIME_SEC]);
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby}
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltbyuint64_t
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltbyfmev_time_nsec(fmev_t ev)
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby{
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby return (FMEV2IMPL(ev)->ei_fmtime[FM_TIME_NSEC]);
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby}
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltbystruct tm *
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltbyfmev_localtime(fmev_t ev, struct tm *tm)
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby{
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby time_t seconds;
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby seconds = (time_t)fmev_time_sec(ev);
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby return (localtime_r(&seconds, tm));
49b225e1cfa7bbf7738d4df0a03f18e3283426ebGavin Maltby}
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltbyfmev_shdl_t
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltbyfmev_ev2shdl(fmev_t ev)
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby{
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby fmev_impl_t *iep = FMEV2IMPL(ev);
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby if (!FMEV_API_ENTER(iep, 2))
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby return (NULL);
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby return (iep->ei_hdl);
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby}