1*12126SHyon.Kim@Sun.COM /*
2*12126SHyon.Kim@Sun.COM * CDDL HEADER START
3*12126SHyon.Kim@Sun.COM *
4*12126SHyon.Kim@Sun.COM * The contents of this file are subject to the terms of the
5*12126SHyon.Kim@Sun.COM * Common Development and Distribution License (the "License").
6*12126SHyon.Kim@Sun.COM * You may not use this file except in compliance with the License.
7*12126SHyon.Kim@Sun.COM *
8*12126SHyon.Kim@Sun.COM * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*12126SHyon.Kim@Sun.COM * or http://www.opensolaris.org/os/licensing.
10*12126SHyon.Kim@Sun.COM * See the License for the specific language governing permissions
11*12126SHyon.Kim@Sun.COM * and limitations under the License.
12*12126SHyon.Kim@Sun.COM *
13*12126SHyon.Kim@Sun.COM * When distributing Covered Code, include this CDDL HEADER in each
14*12126SHyon.Kim@Sun.COM * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*12126SHyon.Kim@Sun.COM * If applicable, add the following below this CDDL HEADER, with the
16*12126SHyon.Kim@Sun.COM * fields enclosed by brackets "[]" replaced with your own identifying
17*12126SHyon.Kim@Sun.COM * information: Portions Copyright [yyyy] [name of copyright owner]
18*12126SHyon.Kim@Sun.COM *
19*12126SHyon.Kim@Sun.COM * CDDL HEADER END
20*12126SHyon.Kim@Sun.COM */
21*12126SHyon.Kim@Sun.COM
22*12126SHyon.Kim@Sun.COM /*
23*12126SHyon.Kim@Sun.COM * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
24*12126SHyon.Kim@Sun.COM */
25*12126SHyon.Kim@Sun.COM
26*12126SHyon.Kim@Sun.COM #include <sys/types.h>
27*12126SHyon.Kim@Sun.COM
28*12126SHyon.Kim@Sun.COM #include <stddef.h>
29*12126SHyon.Kim@Sun.COM #include <stdlib.h>
30*12126SHyon.Kim@Sun.COM #include <string.h>
31*12126SHyon.Kim@Sun.COM #include <strings.h>
32*12126SHyon.Kim@Sun.COM #include <alloca.h>
33*12126SHyon.Kim@Sun.COM #include <stdio.h>
34*12126SHyon.Kim@Sun.COM #include <unistd.h>
35*12126SHyon.Kim@Sun.COM #include <dlfcn.h>
36*12126SHyon.Kim@Sun.COM #include <thread.h>
37*12126SHyon.Kim@Sun.COM #include <pthread.h>
38*12126SHyon.Kim@Sun.COM #include <ctype.h>
39*12126SHyon.Kim@Sun.COM
40*12126SHyon.Kim@Sun.COM #include <scsi/libsmp.h>
41*12126SHyon.Kim@Sun.COM #include <scsi/libsmp_plugin.h>
42*12126SHyon.Kim@Sun.COM #include "smp_impl.h"
43*12126SHyon.Kim@Sun.COM
44*12126SHyon.Kim@Sun.COM __thread smp_errno_t _smp_errno;
45*12126SHyon.Kim@Sun.COM __thread char _smp_errmsg[LIBSMP_ERRMSGLEN];
46*12126SHyon.Kim@Sun.COM
47*12126SHyon.Kim@Sun.COM int
smp_assert(const char * expr,const char * file,int line)48*12126SHyon.Kim@Sun.COM smp_assert(const char *expr, const char *file, int line)
49*12126SHyon.Kim@Sun.COM {
50*12126SHyon.Kim@Sun.COM char *msg;
51*12126SHyon.Kim@Sun.COM size_t len;
52*12126SHyon.Kim@Sun.COM
53*12126SHyon.Kim@Sun.COM len = snprintf(NULL, 0,
54*12126SHyon.Kim@Sun.COM "ABORT: \"%s\", line %d: assertion failed: %s\n", file, line, expr);
55*12126SHyon.Kim@Sun.COM
56*12126SHyon.Kim@Sun.COM msg = alloca(len + 1);
57*12126SHyon.Kim@Sun.COM
58*12126SHyon.Kim@Sun.COM (void) snprintf(msg, len + 1,
59*12126SHyon.Kim@Sun.COM "ABORT: \"%s\", line %d: assertion failed: %s\n", file, line, expr);
60*12126SHyon.Kim@Sun.COM
61*12126SHyon.Kim@Sun.COM (void) write(STDERR_FILENO, msg, strlen(msg));
62*12126SHyon.Kim@Sun.COM
63*12126SHyon.Kim@Sun.COM abort();
64*12126SHyon.Kim@Sun.COM _exit(1);
65*12126SHyon.Kim@Sun.COM
66*12126SHyon.Kim@Sun.COM /*NOTREACHED*/
67*12126SHyon.Kim@Sun.COM return (0);
68*12126SHyon.Kim@Sun.COM }
69*12126SHyon.Kim@Sun.COM
70*12126SHyon.Kim@Sun.COM int
smp_set_errno(smp_errno_t err)71*12126SHyon.Kim@Sun.COM smp_set_errno(smp_errno_t err)
72*12126SHyon.Kim@Sun.COM {
73*12126SHyon.Kim@Sun.COM _smp_errno = err;
74*12126SHyon.Kim@Sun.COM _smp_errmsg[0] = '\0';
75*12126SHyon.Kim@Sun.COM
76*12126SHyon.Kim@Sun.COM return (-1);
77*12126SHyon.Kim@Sun.COM }
78*12126SHyon.Kim@Sun.COM
79*12126SHyon.Kim@Sun.COM /*
80*12126SHyon.Kim@Sun.COM * Internal routine for setting both _smp_errno and _smp_errmsg. We save
81*12126SHyon.Kim@Sun.COM * and restore the UNIX errno across this routing so the caller can use either
82*12126SHyon.Kim@Sun.COM * smp_set_errno(), smp_error(), or smp_verror() without this value changing.
83*12126SHyon.Kim@Sun.COM */
84*12126SHyon.Kim@Sun.COM int
smp_verror(smp_errno_t err,const char * fmt,va_list ap)85*12126SHyon.Kim@Sun.COM smp_verror(smp_errno_t err, const char *fmt, va_list ap)
86*12126SHyon.Kim@Sun.COM {
87*12126SHyon.Kim@Sun.COM size_t n;
88*12126SHyon.Kim@Sun.COM char *errmsg;
89*12126SHyon.Kim@Sun.COM
90*12126SHyon.Kim@Sun.COM /*
91*12126SHyon.Kim@Sun.COM * To allow the existing error message to itself be used in an error
92*12126SHyon.Kim@Sun.COM * message, we put the new error message into a buffer on the stack,
93*12126SHyon.Kim@Sun.COM * and then copy it into lsh_errmsg. We also need to set the errno,
94*12126SHyon.Kim@Sun.COM * but because the call to smp_set_errno() is destructive to
95*12126SHyon.Kim@Sun.COM * lsh_errmsg, we do this after we print into our temporary buffer
96*12126SHyon.Kim@Sun.COM * (in case _smp_errmsg is part of the error message) and before we
97*12126SHyon.Kim@Sun.COM * copy the temporary buffer on to _smp_errmsg (to prevent our new
98*12126SHyon.Kim@Sun.COM * message from being nuked by the call to smp_set_errno()).
99*12126SHyon.Kim@Sun.COM */
100*12126SHyon.Kim@Sun.COM errmsg = alloca(sizeof (_smp_errmsg));
101*12126SHyon.Kim@Sun.COM (void) vsnprintf(errmsg, sizeof (_smp_errmsg), fmt, ap);
102*12126SHyon.Kim@Sun.COM (void) smp_set_errno(err);
103*12126SHyon.Kim@Sun.COM
104*12126SHyon.Kim@Sun.COM n = strlen(errmsg);
105*12126SHyon.Kim@Sun.COM
106*12126SHyon.Kim@Sun.COM if (n != 0 && errmsg[n - 1] == '\n')
107*12126SHyon.Kim@Sun.COM errmsg[n - 1] = '\0';
108*12126SHyon.Kim@Sun.COM
109*12126SHyon.Kim@Sun.COM bcopy(errmsg, _smp_errmsg, n + 1);
110*12126SHyon.Kim@Sun.COM
111*12126SHyon.Kim@Sun.COM return (-1);
112*12126SHyon.Kim@Sun.COM }
113*12126SHyon.Kim@Sun.COM
114*12126SHyon.Kim@Sun.COM int
smp_error(smp_errno_t err,const char * fmt,...)115*12126SHyon.Kim@Sun.COM smp_error(smp_errno_t err, const char *fmt, ...)
116*12126SHyon.Kim@Sun.COM {
117*12126SHyon.Kim@Sun.COM va_list ap;
118*12126SHyon.Kim@Sun.COM
119*12126SHyon.Kim@Sun.COM if (fmt == NULL)
120*12126SHyon.Kim@Sun.COM return (smp_set_errno(err));
121*12126SHyon.Kim@Sun.COM
122*12126SHyon.Kim@Sun.COM va_start(ap, fmt);
123*12126SHyon.Kim@Sun.COM err = smp_verror(err, fmt, ap);
124*12126SHyon.Kim@Sun.COM va_end(ap);
125*12126SHyon.Kim@Sun.COM
126*12126SHyon.Kim@Sun.COM return (err);
127*12126SHyon.Kim@Sun.COM }
128*12126SHyon.Kim@Sun.COM
129*12126SHyon.Kim@Sun.COM smp_errno_t
smp_errno(void)130*12126SHyon.Kim@Sun.COM smp_errno(void)
131*12126SHyon.Kim@Sun.COM {
132*12126SHyon.Kim@Sun.COM return (_smp_errno);
133*12126SHyon.Kim@Sun.COM }
134*12126SHyon.Kim@Sun.COM
135*12126SHyon.Kim@Sun.COM const char *
smp_errmsg(void)136*12126SHyon.Kim@Sun.COM smp_errmsg(void)
137*12126SHyon.Kim@Sun.COM {
138*12126SHyon.Kim@Sun.COM if (_smp_errmsg[0] == '\0')
139*12126SHyon.Kim@Sun.COM (void) strlcpy(_smp_errmsg, smp_strerror(_smp_errno),
140*12126SHyon.Kim@Sun.COM sizeof (_smp_errmsg));
141*12126SHyon.Kim@Sun.COM
142*12126SHyon.Kim@Sun.COM return (_smp_errmsg);
143*12126SHyon.Kim@Sun.COM }
144*12126SHyon.Kim@Sun.COM
145*12126SHyon.Kim@Sun.COM /*ARGSUSED*/
146*12126SHyon.Kim@Sun.COM void *
smp_alloc(size_t size)147*12126SHyon.Kim@Sun.COM smp_alloc(size_t size)
148*12126SHyon.Kim@Sun.COM {
149*12126SHyon.Kim@Sun.COM void *mem;
150*12126SHyon.Kim@Sun.COM
151*12126SHyon.Kim@Sun.COM if (size == 0) {
152*12126SHyon.Kim@Sun.COM (void) smp_set_errno(ESMP_ZERO_LENGTH);
153*12126SHyon.Kim@Sun.COM return (NULL);
154*12126SHyon.Kim@Sun.COM }
155*12126SHyon.Kim@Sun.COM
156*12126SHyon.Kim@Sun.COM if ((mem = malloc(size)) == NULL)
157*12126SHyon.Kim@Sun.COM (void) smp_set_errno(ESMP_NOMEM);
158*12126SHyon.Kim@Sun.COM
159*12126SHyon.Kim@Sun.COM return (mem);
160*12126SHyon.Kim@Sun.COM }
161*12126SHyon.Kim@Sun.COM
162*12126SHyon.Kim@Sun.COM void *
smp_zalloc(size_t size)163*12126SHyon.Kim@Sun.COM smp_zalloc(size_t size)
164*12126SHyon.Kim@Sun.COM {
165*12126SHyon.Kim@Sun.COM void *mem;
166*12126SHyon.Kim@Sun.COM
167*12126SHyon.Kim@Sun.COM if ((mem = smp_alloc(size)) == NULL)
168*12126SHyon.Kim@Sun.COM return (NULL);
169*12126SHyon.Kim@Sun.COM
170*12126SHyon.Kim@Sun.COM bzero(mem, size);
171*12126SHyon.Kim@Sun.COM
172*12126SHyon.Kim@Sun.COM return (mem);
173*12126SHyon.Kim@Sun.COM }
174*12126SHyon.Kim@Sun.COM
175*12126SHyon.Kim@Sun.COM char *
smp_strdup(const char * str)176*12126SHyon.Kim@Sun.COM smp_strdup(const char *str)
177*12126SHyon.Kim@Sun.COM {
178*12126SHyon.Kim@Sun.COM size_t len = strlen(str);
179*12126SHyon.Kim@Sun.COM char *dup = smp_alloc(len + 1);
180*12126SHyon.Kim@Sun.COM
181*12126SHyon.Kim@Sun.COM if (dup == NULL)
182*12126SHyon.Kim@Sun.COM return (NULL);
183*12126SHyon.Kim@Sun.COM
184*12126SHyon.Kim@Sun.COM return (strcpy(dup, str));
185*12126SHyon.Kim@Sun.COM }
186*12126SHyon.Kim@Sun.COM
187*12126SHyon.Kim@Sun.COM void
smp_free(void * ptr)188*12126SHyon.Kim@Sun.COM smp_free(void *ptr)
189*12126SHyon.Kim@Sun.COM {
190*12126SHyon.Kim@Sun.COM free(ptr);
191*12126SHyon.Kim@Sun.COM }
192*12126SHyon.Kim@Sun.COM
193*12126SHyon.Kim@Sun.COM /*
194*12126SHyon.Kim@Sun.COM * Trim any leading and/or trailing spaces from the fixed-length string
195*12126SHyon.Kim@Sun.COM * argument and return a newly-allocated copy of it.
196*12126SHyon.Kim@Sun.COM */
197*12126SHyon.Kim@Sun.COM char *
smp_trim_strdup(const char * str,size_t len)198*12126SHyon.Kim@Sun.COM smp_trim_strdup(const char *str, size_t len)
199*12126SHyon.Kim@Sun.COM {
200*12126SHyon.Kim@Sun.COM const char *p;
201*12126SHyon.Kim@Sun.COM char *r;
202*12126SHyon.Kim@Sun.COM
203*12126SHyon.Kim@Sun.COM for (p = str; p - str < len && isspace(*p); p++)
204*12126SHyon.Kim@Sun.COM ;
205*12126SHyon.Kim@Sun.COM
206*12126SHyon.Kim@Sun.COM len -= (p - str);
207*12126SHyon.Kim@Sun.COM
208*12126SHyon.Kim@Sun.COM if (len == 0)
209*12126SHyon.Kim@Sun.COM return (NULL);
210*12126SHyon.Kim@Sun.COM
211*12126SHyon.Kim@Sun.COM for (str = p + len - 1; str > p && isspace(*str); str--, len--)
212*12126SHyon.Kim@Sun.COM ;
213*12126SHyon.Kim@Sun.COM
214*12126SHyon.Kim@Sun.COM if (len == 0)
215*12126SHyon.Kim@Sun.COM return (NULL);
216*12126SHyon.Kim@Sun.COM
217*12126SHyon.Kim@Sun.COM r = smp_alloc(len + 1);
218*12126SHyon.Kim@Sun.COM if (r == NULL)
219*12126SHyon.Kim@Sun.COM return (NULL);
220*12126SHyon.Kim@Sun.COM
221*12126SHyon.Kim@Sun.COM bcopy(p, r, len);
222*12126SHyon.Kim@Sun.COM r[len] = '\0';
223*12126SHyon.Kim@Sun.COM
224*12126SHyon.Kim@Sun.COM return (r);
225*12126SHyon.Kim@Sun.COM }
226*12126SHyon.Kim@Sun.COM
227*12126SHyon.Kim@Sun.COM int
smp_init(int version)228*12126SHyon.Kim@Sun.COM smp_init(int version)
229*12126SHyon.Kim@Sun.COM {
230*12126SHyon.Kim@Sun.COM if (version != LIBSMP_VERSION)
231*12126SHyon.Kim@Sun.COM return (smp_error(ESMP_VERSION,
232*12126SHyon.Kim@Sun.COM "library version %d does not match requested version %d",
233*12126SHyon.Kim@Sun.COM LIBSMP_VERSION, version));
234*12126SHyon.Kim@Sun.COM
235*12126SHyon.Kim@Sun.COM smp_engine_init();
236*12126SHyon.Kim@Sun.COM
237*12126SHyon.Kim@Sun.COM return (0);
238*12126SHyon.Kim@Sun.COM }
239*12126SHyon.Kim@Sun.COM
240*12126SHyon.Kim@Sun.COM void
smp_fini(void)241*12126SHyon.Kim@Sun.COM smp_fini(void)
242*12126SHyon.Kim@Sun.COM {
243*12126SHyon.Kim@Sun.COM smp_engine_fini();
244*12126SHyon.Kim@Sun.COM }
245