Merge remote-tracking branch 'origin/master'
[unleashed/lotheac.git] / usr / src / lib / scsi / libsmp / common / smp_subr.c
blobf332c3c7bfc0de2012814d3daaf53598cea7e194
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
23 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
26 #include <sys/types.h>
28 #include <stddef.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <strings.h>
32 #include <alloca.h>
33 #include <stdio.h>
34 #include <unistd.h>
35 #include <dlfcn.h>
36 #include <thread.h>
37 #include <pthread.h>
38 #include <ctype.h>
40 #include <scsi/libsmp.h>
41 #include <scsi/libsmp_plugin.h>
42 #include "smp_impl.h"
44 __thread smp_errno_t _smp_errno;
45 __thread char _smp_errmsg[LIBSMP_ERRMSGLEN];
47 int
48 smp_assert(const char *expr, const char *file, int line)
50 char *msg;
51 size_t len;
53 len = snprintf(NULL, 0,
54 "ABORT: \"%s\", line %d: assertion failed: %s\n", file, line, expr);
56 msg = alloca(len + 1);
58 (void) snprintf(msg, len + 1,
59 "ABORT: \"%s\", line %d: assertion failed: %s\n", file, line, expr);
61 (void) write(STDERR_FILENO, msg, strlen(msg));
63 abort();
64 _exit(1);
66 /*NOTREACHED*/
67 return (0);
70 int
71 smp_set_errno(smp_errno_t err)
73 _smp_errno = err;
74 _smp_errmsg[0] = '\0';
76 return (-1);
80 * Internal routine for setting both _smp_errno and _smp_errmsg. We save
81 * and restore the UNIX errno across this routing so the caller can use either
82 * smp_set_errno(), smp_error(), or smp_verror() without this value changing.
84 int
85 smp_verror(smp_errno_t err, const char *fmt, va_list ap)
87 size_t n;
88 char *errmsg;
91 * To allow the existing error message to itself be used in an error
92 * message, we put the new error message into a buffer on the stack,
93 * and then copy it into lsh_errmsg. We also need to set the errno,
94 * but because the call to smp_set_errno() is destructive to
95 * lsh_errmsg, we do this after we print into our temporary buffer
96 * (in case _smp_errmsg is part of the error message) and before we
97 * copy the temporary buffer on to _smp_errmsg (to prevent our new
98 * message from being nuked by the call to smp_set_errno()).
100 errmsg = alloca(sizeof (_smp_errmsg));
101 (void) vsnprintf(errmsg, sizeof (_smp_errmsg), fmt, ap);
102 (void) smp_set_errno(err);
104 n = strlen(errmsg);
106 if (n != 0 && errmsg[n - 1] == '\n')
107 errmsg[n - 1] = '\0';
109 bcopy(errmsg, _smp_errmsg, n + 1);
111 return (-1);
115 smp_error(smp_errno_t err, const char *fmt, ...)
117 va_list ap;
119 if (fmt == NULL)
120 return (smp_set_errno(err));
122 va_start(ap, fmt);
123 err = smp_verror(err, fmt, ap);
124 va_end(ap);
126 return (err);
129 smp_errno_t
130 smp_errno(void)
132 return (_smp_errno);
135 const char *
136 smp_errmsg(void)
138 if (_smp_errmsg[0] == '\0')
139 (void) strlcpy(_smp_errmsg, smp_strerror(_smp_errno),
140 sizeof (_smp_errmsg));
142 return (_smp_errmsg);
145 /*ARGSUSED*/
146 void *
147 smp_alloc(size_t size)
149 void *mem;
151 if (size == 0) {
152 (void) smp_set_errno(ESMP_ZERO_LENGTH);
153 return (NULL);
156 if ((mem = malloc(size)) == NULL)
157 (void) smp_set_errno(ESMP_NOMEM);
159 return (mem);
162 void *
163 smp_zalloc(size_t size)
165 void *mem;
167 if ((mem = smp_alloc(size)) == NULL)
168 return (NULL);
170 bzero(mem, size);
172 return (mem);
175 char *
176 smp_strdup(const char *str)
178 size_t len = strlen(str);
179 char *dup = smp_alloc(len + 1);
181 if (dup == NULL)
182 return (NULL);
184 return (strcpy(dup, str));
187 void
188 smp_free(void *ptr)
190 free(ptr);
194 * Trim any leading and/or trailing spaces from the fixed-length string
195 * argument and return a newly-allocated copy of it.
197 char *
198 smp_trim_strdup(const char *str, size_t len)
200 const char *p;
201 char *r;
203 for (p = str; p - str < len && isspace(*p); p++)
206 len -= (p - str);
208 if (len == 0)
209 return (NULL);
211 for (str = p + len - 1; str > p && isspace(*str); str--, len--)
214 if (len == 0)
215 return (NULL);
217 r = smp_alloc(len + 1);
218 if (r == NULL)
219 return (NULL);
221 bcopy(p, r, len);
222 r[len] = '\0';
224 return (r);
228 smp_init(int version)
230 if (version != LIBSMP_VERSION)
231 return (smp_error(ESMP_VERSION,
232 "library version %d does not match requested version %d",
233 LIBSMP_VERSION, version));
235 smp_engine_init();
237 return (0);
240 void
241 smp_fini(void)
243 smp_engine_fini();