xref: /illumos-gate/usr/src/lib/scsi/libsmp/common/smp_subr.c (revision 45ede40b2394db7967e59f19288fae9b62efd4aa)
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 /*
27  * Copyright 2019 Joyent, Inc.
28  */
29 
30 #include <sys/types.h>
31 
32 #include <stddef.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <strings.h>
36 #include <alloca.h>
37 #include <stdio.h>
38 #include <unistd.h>
39 #include <dlfcn.h>
40 #include <thread.h>
41 #include <pthread.h>
42 #include <ctype.h>
43 
44 #include <scsi/libsmp.h>
45 #include <scsi/libsmp_plugin.h>
46 #include "smp_impl.h"
47 
48 __thread smp_errno_t _smp_errno;
49 __thread char _smp_errmsg[LIBSMP_ERRMSGLEN];
50 
51 int
52 smp_assert(const char *expr, const char *file, int line)
53 {
54 	char *msg;
55 	size_t len;
56 
57 	len = snprintf(NULL, 0,
58 	    "ABORT: \"%s\", line %d: assertion failed: %s\n", file, line, expr);
59 
60 	msg = alloca(len + 1);
61 
62 	(void) snprintf(msg, len + 1,
63 	    "ABORT: \"%s\", line %d: assertion failed: %s\n", file, line, expr);
64 
65 	(void) write(STDERR_FILENO, msg, strlen(msg));
66 
67 	abort();
68 	/*NOTREACHED*/
69 }
70 
71 int
72 smp_set_errno(smp_errno_t err)
73 {
74 	_smp_errno = err;
75 	_smp_errmsg[0] = '\0';
76 
77 	return (-1);
78 }
79 
80 /*
81  * Internal routine for setting both _smp_errno and _smp_errmsg.  We save
82  * and restore the UNIX errno across this routing so the caller can use either
83  * smp_set_errno(), smp_error(), or smp_verror() without this value changing.
84  */
85 int
86 smp_verror(smp_errno_t err, const char *fmt, va_list ap)
87 {
88 	size_t n;
89 	char *errmsg;
90 
91 	/*
92 	 * To allow the existing error message to itself be used in an error
93 	 * message, we put the new error message into a buffer on the stack,
94 	 * and then copy it into lsh_errmsg.  We also need to set the errno,
95 	 * but because the call to smp_set_errno() is destructive to
96 	 * lsh_errmsg, we do this after we print into our temporary buffer
97 	 * (in case _smp_errmsg is part of the error message) and before we
98 	 * copy the temporary buffer on to _smp_errmsg (to prevent our new
99 	 * message from being nuked by the call to smp_set_errno()).
100 	 */
101 	errmsg = alloca(sizeof (_smp_errmsg));
102 	(void) vsnprintf(errmsg, sizeof (_smp_errmsg), fmt, ap);
103 	(void) smp_set_errno(err);
104 
105 	n = strlen(errmsg);
106 
107 	if (n != 0 && errmsg[n - 1] == '\n')
108 		errmsg[n - 1] = '\0';
109 
110 	bcopy(errmsg, _smp_errmsg, n + 1);
111 
112 	return (-1);
113 }
114 
115 int
116 smp_error(smp_errno_t err, const char *fmt, ...)
117 {
118 	va_list ap;
119 
120 	if (fmt == NULL)
121 		return (smp_set_errno(err));
122 
123 	va_start(ap, fmt);
124 	err = smp_verror(err, fmt, ap);
125 	va_end(ap);
126 
127 	return (err);
128 }
129 
130 smp_errno_t
131 smp_errno(void)
132 {
133 	return (_smp_errno);
134 }
135 
136 const char *
137 smp_errmsg(void)
138 {
139 	if (_smp_errmsg[0] == '\0')
140 		(void) strlcpy(_smp_errmsg, smp_strerror(_smp_errno),
141 		    sizeof (_smp_errmsg));
142 
143 	return (_smp_errmsg);
144 }
145 
146 /*ARGSUSED*/
147 void *
148 smp_alloc(size_t size)
149 {
150 	void *mem;
151 
152 	if (size == 0) {
153 		(void) smp_set_errno(ESMP_ZERO_LENGTH);
154 		return (NULL);
155 	}
156 
157 	if ((mem = malloc(size)) == NULL)
158 		(void) smp_set_errno(ESMP_NOMEM);
159 
160 	return (mem);
161 }
162 
163 void *
164 smp_zalloc(size_t size)
165 {
166 	void *mem;
167 
168 	if ((mem = smp_alloc(size)) == NULL)
169 		return (NULL);
170 
171 	bzero(mem, size);
172 
173 	return (mem);
174 }
175 
176 char *
177 smp_strdup(const char *str)
178 {
179 	size_t len = strlen(str);
180 	char *dup = smp_alloc(len + 1);
181 
182 	if (dup == NULL)
183 		return (NULL);
184 
185 	return (strcpy(dup, str));
186 }
187 
188 void
189 smp_free(void *ptr)
190 {
191 	free(ptr);
192 }
193 
194 /*
195  * Trim any leading and/or trailing spaces from the fixed-length string
196  * argument and return a newly-allocated copy of it.
197  */
198 char *
199 smp_trim_strdup(const char *str, size_t len)
200 {
201 	const char *p;
202 	char *r;
203 
204 	for (p = str; p - str < len && isspace(*p); p++)
205 		;
206 
207 	len -= (p - str);
208 
209 	if (len == 0)
210 		return (NULL);
211 
212 	for (str = p + len - 1; str > p && isspace(*str); str--, len--)
213 		;
214 
215 	if (len == 0)
216 		return (NULL);
217 
218 	r = smp_alloc(len + 1);
219 	if (r == NULL)
220 		return (NULL);
221 
222 	bcopy(p, r, len);
223 	r[len] = '\0';
224 
225 	return (r);
226 }
227 
228 int
229 smp_init(int version)
230 {
231 	if (version != LIBSMP_VERSION)
232 		return (smp_error(ESMP_VERSION,
233 		    "library version %d does not match requested version %d",
234 		    LIBSMP_VERSION, version));
235 
236 	smp_engine_init();
237 
238 	return (0);
239 }
240 
241 void
242 smp_fini(void)
243 {
244 	smp_engine_fini();
245 }
246