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) 2010, 2012, Oracle and/or its affiliates. All rights reserved.
2N/A */
2N/A
2N/A#include <sys/types.h>
2N/A
2N/A#include <stddef.h>
2N/A#include <stdlib.h>
2N/A#include <string.h>
2N/A#include <strings.h>
2N/A#include <alloca.h>
2N/A#include <stdio.h>
2N/A#include <unistd.h>
2N/A#include <dlfcn.h>
2N/A#include <thread.h>
2N/A#include <pthread.h>
2N/A#include <ctype.h>
2N/A
2N/A#include <scsi/libsmp.h>
2N/A#include <scsi/libsmp_plugin.h>
2N/A#include "smp_impl.h"
2N/A
2N/A__thread smp_errno_t _smp_errno;
2N/A__thread char _smp_errmsg[LIBSMP_ERRMSGLEN];
2N/A
2N/Aint
2N/Asmp_assert(const char *expr, const char *file, int line)
2N/A{
2N/A char *msg;
2N/A size_t len;
2N/A
2N/A len = snprintf(NULL, 0,
2N/A "ABORT: \"%s\", line %d: assertion failed: %s\n", file, line, expr);
2N/A
2N/A msg = alloca(len + 1);
2N/A
2N/A (void) snprintf(msg, len + 1,
2N/A "ABORT: \"%s\", line %d: assertion failed: %s\n", file, line, expr);
2N/A
2N/A (void) write(STDERR_FILENO, msg, strlen(msg));
2N/A
2N/A abort();
2N/A
2N/A /*NOTREACHED*/
2N/A _exit(1);
2N/A
2N/A /*NOTREACHED*/
2N/A return (0);
2N/A}
2N/A
2N/Aint
2N/Asmp_set_errno(smp_errno_t err)
2N/A{
2N/A _smp_errno = err;
2N/A _smp_errmsg[0] = '\0';
2N/A
2N/A return (-1);
2N/A}
2N/A
2N/A/*
2N/A * Internal routine for setting both _smp_errno and _smp_errmsg. We save
2N/A * and restore the UNIX errno across this routing so the caller can use either
2N/A * smp_set_errno(), smp_error(), or smp_verror() without this value changing.
2N/A */
2N/Aint
2N/Asmp_verror(smp_errno_t err, const char *fmt, va_list ap)
2N/A{
2N/A size_t n;
2N/A char *errmsg;
2N/A
2N/A /*
2N/A * To allow the existing error message to itself be used in an error
2N/A * message, we put the new error message into a buffer on the stack,
2N/A * and then copy it into lsh_errmsg. We also need to set the errno,
2N/A * but because the call to smp_set_errno() is destructive to
2N/A * lsh_errmsg, we do this after we print into our temporary buffer
2N/A * (in case _smp_errmsg is part of the error message) and before we
2N/A * copy the temporary buffer on to _smp_errmsg (to prevent our new
2N/A * message from being nuked by the call to smp_set_errno()).
2N/A */
2N/A errmsg = alloca(sizeof (_smp_errmsg));
2N/A (void) vsnprintf(errmsg, sizeof (_smp_errmsg), fmt, ap);
2N/A (void) smp_set_errno(err);
2N/A
2N/A n = strlen(errmsg);
2N/A
2N/A if (n != 0 && errmsg[n - 1] == '\n')
2N/A errmsg[n - 1] = '\0';
2N/A
2N/A bcopy(errmsg, _smp_errmsg, n + 1);
2N/A
2N/A return (-1);
2N/A}
2N/A
2N/Aint
2N/Asmp_error(smp_errno_t err, const char *fmt, ...)
2N/A{
2N/A va_list ap;
2N/A
2N/A if (fmt == NULL)
2N/A return (smp_set_errno(err));
2N/A
2N/A va_start(ap, fmt);
2N/A err = smp_verror(err, fmt, ap);
2N/A va_end(ap);
2N/A
2N/A return (err);
2N/A}
2N/A
2N/Asmp_errno_t
2N/Asmp_errno(void)
2N/A{
2N/A return (_smp_errno);
2N/A}
2N/A
2N/Aconst char *
2N/Asmp_errmsg(void)
2N/A{
2N/A if (_smp_errmsg[0] == '\0')
2N/A (void) strlcpy(_smp_errmsg, smp_strerror(_smp_errno),
2N/A sizeof (_smp_errmsg));
2N/A
2N/A return (_smp_errmsg);
2N/A}
2N/A
2N/A/*ARGSUSED*/
2N/Avoid *
2N/Asmp_alloc(size_t size)
2N/A{
2N/A void *mem;
2N/A
2N/A if (size == 0) {
2N/A (void) smp_set_errno(ESMP_ZERO_LENGTH);
2N/A return (NULL);
2N/A }
2N/A
2N/A if ((mem = malloc(size)) == NULL)
2N/A (void) smp_set_errno(ESMP_NOMEM);
2N/A
2N/A return (mem);
2N/A}
2N/A
2N/Avoid *
2N/Asmp_zalloc(size_t size)
2N/A{
2N/A void *mem;
2N/A
2N/A if ((mem = smp_alloc(size)) == NULL)
2N/A return (NULL);
2N/A
2N/A bzero(mem, size);
2N/A
2N/A return (mem);
2N/A}
2N/A
2N/Achar *
2N/Asmp_strdup(const char *str)
2N/A{
2N/A size_t len = strlen(str);
2N/A char *dup = smp_alloc(len + 1);
2N/A
2N/A if (dup == NULL)
2N/A return (NULL);
2N/A
2N/A return (strcpy(dup, str));
2N/A}
2N/A
2N/Avoid
2N/Asmp_free(void *ptr)
2N/A{
2N/A free(ptr);
2N/A}
2N/A
2N/A/*
2N/A * Trim any leading and/or trailing spaces from the fixed-length string
2N/A * argument and return a newly-allocated copy of it.
2N/A */
2N/Achar *
2N/Asmp_trim_strdup(const char *str, size_t len)
2N/A{
2N/A const char *p;
2N/A char *r;
2N/A
2N/A for (p = str; p - str < len && isspace(*p); p++)
2N/A ;
2N/A
2N/A len -= (p - str);
2N/A
2N/A if (len == 0)
2N/A return (NULL);
2N/A
2N/A for (str = p + len - 1; str > p && isspace(*str); str--, len--)
2N/A ;
2N/A
2N/A if (len == 0)
2N/A return (NULL);
2N/A
2N/A r = smp_alloc(len + 1);
2N/A if (r == NULL)
2N/A return (NULL);
2N/A
2N/A bcopy(p, r, len);
2N/A r[len] = '\0';
2N/A
2N/A return (r);
2N/A}
2N/A
2N/Aint
2N/Asmp_init(int version)
2N/A{
2N/A if (version != LIBSMP_VERSION)
2N/A return (smp_error(ESMP_VERSION,
2N/A "library version %d does not match requested version %d",
2N/A LIBSMP_VERSION, version));
2N/A
2N/A smp_engine_init();
2N/A
2N/A return (0);
2N/A}
2N/A
2N/Avoid
2N/Asmp_fini(void)
2N/A{
2N/A smp_engine_fini();
2N/A}