2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A
2N/A/*
2N/A * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
2N/A */
2N/A
2N/A/*
2N/A * Subscription event access interfaces.
2N/A */
2N/A
2N/A#include <sys/types.h>
2N/A#include <pthread.h>
2N/A#include <umem.h>
2N/A#include <fm/libfmevent.h>
2N/A
2N/A#include "fmev_impl.h"
2N/A
2N/Astatic pthread_key_t fmev_tsdkey = PTHREAD_ONCE_KEY_NP;
2N/Astatic int key_inited;
2N/A
2N/A/*
2N/A * Thread and handle specific data.
2N/A */
2N/Astruct fmev_tsd {
2N/A fmev_err_t ts_lasterr;
2N/A};
2N/A
2N/Astatic void
2N/Afmev_tsd_destructor(void *data)
2N/A{
2N/A umem_free(data, sizeof (struct fmev_tsd));
2N/A}
2N/A
2N/A/*
2N/A * Called only from fmev_shdl_init. Check we are opening a valid version
2N/A * of the ABI.
2N/A */
2N/Aint
2N/Afmev_api_init(struct fmev_hdl_cmn *hc)
2N/A{
2N/A uint32_t v = hc->hc_api_vers;
2N/A int rc;
2N/A
2N/A if (!fmev_api_enter((struct fmev_hdl_cmn *)fmev_api_init, 0))
2N/A return (0);
2N/A
2N/A switch (v) {
2N/A case LIBFMEVENT_VERSION_1:
2N/A case LIBFMEVENT_VERSION_2:
2N/A case LIBFMEVENT_VERSION_3:
2N/A rc = 1;
2N/A break;
2N/A
2N/A default:
2N/A if (key_inited)
2N/A (void) fmev_seterr(FMEVERR_VERSION_MISMATCH);
2N/A rc = 0;
2N/A break;
2N/A }
2N/A
2N/A return (rc);
2N/A}
2N/A
2N/A/*
2N/A * On entry to other libfmevent API members we call fmev_api_enter.
2N/A * Some thread-specific data is used to keep a per-thread error value.
2N/A * The version opened must be no greater than the latest version but can
2N/A * be older. The ver_intro is the api version at which the interface
2N/A * was added - the caller must have opened at least this version.
2N/A */
2N/Aint
2N/Afmev_api_enter(struct fmev_hdl_cmn *hc, uint32_t ver_intro)
2N/A{
2N/A uint32_t v;
2N/A struct fmev_tsd *tsd;
2N/A
2N/A /* Initialize key on first visit */
2N/A if (!key_inited) {
2N/A (void) pthread_key_create_once_np(&fmev_tsdkey,
2N/A fmev_tsd_destructor);
2N/A key_inited = 1;
2N/A }
2N/A
2N/A /*
2N/A * Allocate TSD for error value for this thread. It is only
2N/A * freed if/when the thread exits.
2N/A */
2N/A if ((tsd = pthread_getspecific(fmev_tsdkey)) == NULL) {
2N/A if ((tsd = umem_alloc(sizeof (*tsd), UMEM_DEFAULT)) == NULL ||
2N/A pthread_setspecific(fmev_tsdkey, (const void *)tsd) != 0) {
2N/A if (tsd)
2N/A umem_free(tsd, sizeof (*tsd));
2N/A return (0); /* no error set, but what can we do */
2N/A }
2N/A }
2N/A
2N/A tsd->ts_lasterr = 0;
2N/A
2N/A if (hc == (struct fmev_hdl_cmn *)fmev_api_init)
2N/A return (1); /* special case from fmev_api_init only */
2N/A
2N/A if (hc == NULL || hc->hc_magic != _FMEV_SHMAGIC) {
2N/A tsd->ts_lasterr = FMEVERR_API;
2N/A return (0);
2N/A }
2N/A
2N/A v = hc->hc_api_vers; /* API version opened */
2N/A
2N/A /* Enforce version adherence. */
2N/A if (ver_intro > v || v > LIBFMEVENT_VERSION_LATEST ||
2N/A ver_intro > LIBFMEVENT_VERSION_LATEST) {
2N/A tsd->ts_lasterr = FMEVERR_VERSION_MISMATCH;
2N/A return (0);
2N/A }
2N/A
2N/A return (1);
2N/A}
2N/A
2N/A/*
2N/A * Called on any fmev_shdl_fini. Free the TSD for this thread. If this
2N/A * thread makes other API calls for other open handles, or opens a new
2N/A * handle, then TSD will be allocated again in fmev_api_enter.
2N/A */
2N/Avoid
2N/Afmev_api_freetsd(void)
2N/A{
2N/A struct fmev_tsd *tsd;
2N/A
2N/A if ((tsd = pthread_getspecific(fmev_tsdkey)) != NULL) {
2N/A (void) pthread_setspecific(fmev_tsdkey, NULL);
2N/A fmev_tsd_destructor((void *)tsd);
2N/A }
2N/A}
2N/A
2N/A/*
2N/A * To return an error condition an API member first sets the error type
2N/A * with a call to fmev_seterr and then returns NULL or whatever it wants.
2N/A * The caller can then retrieve the per-thread error type using fmev_errno
2N/A * or format it with fmev_strerr.
2N/A */
2N/Afmev_err_t
2N/Afmev_seterr(fmev_err_t error)
2N/A{
2N/A struct fmev_tsd *tsd;
2N/A
2N/A ASSERT(key_inited);
2N/A
2N/A if ((tsd = pthread_getspecific(fmev_tsdkey)) != NULL)
2N/A tsd->ts_lasterr = error;
2N/A
2N/A return (error);
2N/A}
2N/A
2N/A/*
2N/A * fmev_errno is a macro defined in terms of the following function. It
2N/A * can be used to dereference the last error value on the current thread;
2N/A * it must not be used to assign to fmev_errno.
2N/A */
2N/A
2N/Aconst fmev_err_t apierr = FMEVERR_API;
2N/Aconst fmev_err_t unknownerr = FMEVERR_UNKNOWN;
2N/A
2N/Aconst fmev_err_t *
2N/A__fmev_errno(void)
2N/A{
2N/A struct fmev_tsd *tsd;
2N/A
2N/A if (!key_inited)
2N/A return (&apierr);
2N/A
2N/A if ((tsd = pthread_getspecific(fmev_tsdkey)) == NULL)
2N/A return (&unknownerr);
2N/A
2N/A return ((const fmev_err_t *)&tsd->ts_lasterr);
2N/A}
2N/A
2N/Avoid *
2N/Adflt_alloc(size_t sz)
2N/A{
2N/A return (umem_alloc(sz, UMEM_DEFAULT));
2N/A}
2N/A
2N/Avoid *
2N/Adflt_zalloc(size_t sz)
2N/A{
2N/A return (umem_zalloc(sz, UMEM_DEFAULT));
2N/A}
2N/A
2N/Avoid
2N/Adflt_free(void *buf, size_t sz)
2N/A{
2N/A umem_free(buf, sz);
2N/A}