xref: /titanic_52/usr/src/lib/scsi/libsmp/common/smp_subr.c (revision ac88567a7a5bb7f01cf22cf366bc9d6203e24d7a)
1 /*
2  * CDDL HEADER START
3  *
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.
7  *
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.
12  *
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]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
24  */
25 
26 #include <sys/types.h>
27 
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>
39 
40 #include <scsi/libsmp.h>
41 #include <scsi/libsmp_plugin.h>
42 #include "smp_impl.h"
43 
44 __thread smp_errno_t _smp_errno;
45 __thread char _smp_errmsg[LIBSMP_ERRMSGLEN];
46 
47 int
48 smp_assert(const char *expr, const char *file, int line)
49 {
50 	char *msg;
51 	size_t len;
52 
53 	len = snprintf(NULL, 0,
54 	    "ABORT: \"%s\", line %d: assertion failed: %s\n", file, line, expr);
55 
56 	msg = alloca(len + 1);
57 
58 	(void) snprintf(msg, len + 1,
59 	    "ABORT: \"%s\", line %d: assertion failed: %s\n", file, line, expr);
60 
61 	(void) write(STDERR_FILENO, msg, strlen(msg));
62 
63 	abort();
64 	_exit(1);
65 
66 	/*NOTREACHED*/
67 	return (0);
68 }
69 
70 int
71 smp_set_errno(smp_errno_t err)
72 {
73 	_smp_errno = err;
74 	_smp_errmsg[0] = '\0';
75 
76 	return (-1);
77 }
78 
79 /*
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.
83  */
84 int
85 smp_verror(smp_errno_t err, const char *fmt, va_list ap)
86 {
87 	size_t n;
88 	char *errmsg;
89 
90 	/*
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()).
99 	 */
100 	errmsg = alloca(sizeof (_smp_errmsg));
101 	(void) vsnprintf(errmsg, sizeof (_smp_errmsg), fmt, ap);
102 	(void) smp_set_errno(err);
103 
104 	n = strlen(errmsg);
105 
106 	if (n != 0 && errmsg[n - 1] == '\n')
107 		errmsg[n - 1] = '\0';
108 
109 	bcopy(errmsg, _smp_errmsg, n + 1);
110 
111 	return (-1);
112 }
113 
114 int
115 smp_error(smp_errno_t err, const char *fmt, ...)
116 {
117 	va_list ap;
118 
119 	if (fmt == NULL)
120 		return (smp_set_errno(err));
121 
122 	va_start(ap, fmt);
123 	err = smp_verror(err, fmt, ap);
124 	va_end(ap);
125 
126 	return (err);
127 }
128 
129 smp_errno_t
130 smp_errno(void)
131 {
132 	return (_smp_errno);
133 }
134 
135 const char *
136 smp_errmsg(void)
137 {
138 	if (_smp_errmsg[0] == '\0')
139 		(void) strlcpy(_smp_errmsg, smp_strerror(_smp_errno),
140 		    sizeof (_smp_errmsg));
141 
142 	return (_smp_errmsg);
143 }
144 
145 /*ARGSUSED*/
146 void *
147 smp_alloc(size_t size)
148 {
149 	void *mem;
150 
151 	if (size == 0) {
152 		(void) smp_set_errno(ESMP_ZERO_LENGTH);
153 		return (NULL);
154 	}
155 
156 	if ((mem = malloc(size)) == NULL)
157 		(void) smp_set_errno(ESMP_NOMEM);
158 
159 	return (mem);
160 }
161 
162 void *
163 smp_zalloc(size_t size)
164 {
165 	void *mem;
166 
167 	if ((mem = smp_alloc(size)) == NULL)
168 		return (NULL);
169 
170 	bzero(mem, size);
171 
172 	return (mem);
173 }
174 
175 char *
176 smp_strdup(const char *str)
177 {
178 	size_t len = strlen(str);
179 	char *dup = smp_alloc(len + 1);
180 
181 	if (dup == NULL)
182 		return (NULL);
183 
184 	return (strcpy(dup, str));
185 }
186 
187 void
188 smp_free(void *ptr)
189 {
190 	free(ptr);
191 }
192 
193 /*
194  * Trim any leading and/or trailing spaces from the fixed-length string
195  * argument and return a newly-allocated copy of it.
196  */
197 char *
198 smp_trim_strdup(const char *str, size_t len)
199 {
200 	const char *p;
201 	char *r;
202 
203 	for (p = str; p - str < len && isspace(*p); p++)
204 		;
205 
206 	len -= (p - str);
207 
208 	if (len == 0)
209 		return (NULL);
210 
211 	for (str = p + len - 1; str > p && isspace(*str); str--, len--)
212 		;
213 
214 	if (len == 0)
215 		return (NULL);
216 
217 	r = smp_alloc(len + 1);
218 	if (r == NULL)
219 		return (NULL);
220 
221 	bcopy(p, r, len);
222 	r[len] = '\0';
223 
224 	return (r);
225 }
226 
227 int
228 smp_init(int version)
229 {
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));
234 
235 	smp_engine_init();
236 
237 	return (0);
238 }
239 
240 void
241 smp_fini(void)
242 {
243 	smp_engine_fini();
244 }
245