Print this page
10703 smatch unreachable code checking needs reworking
Reviewed by: Toomas Soome <tsoome@me.com>
Reviewed by: Yuri Pankov <yuri.pankov@nexenta.com>
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/lib/scsi/libses/common/ses_subr.c
+++ new/usr/src/lib/scsi/libses/common/ses_subr.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
↓ open down ↓ |
16 lines elided |
↑ open up ↑ |
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21
22 22 /*
23 23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 24 * Use is subject to license terms.
25 25 */
26 26
27 -#pragma ident "%Z%%M% %I% %E% SMI"
27 +/*
28 + * Copyright 2019 Joyent, Inc.
29 + */
28 30
29 31 #include <scsi/libses.h>
30 32 #include "ses_impl.h"
31 33
32 34 __thread ses_errno_t _ses_errno;
33 35 __thread char _ses_errmsg[1024];
34 36 __thread char _ses_nverr_member[256];
35 37
36 38 static void ses_vpanic(const char *, va_list) __NORETURN;
37 39
38 40 static void
39 41 ses_vpanic(const char *fmt, va_list ap)
40 42 {
41 43 int oserr = errno;
42 44 char msg[BUFSIZ];
43 45 size_t len;
44 46
45 47 (void) snprintf(msg, sizeof (msg), "ABORT: ");
46 48 len = strlen(msg);
↓ open down ↓ |
9 lines elided |
↑ open up ↑ |
47 49 (void) vsnprintf(msg + len, sizeof (msg) - len, fmt, ap);
48 50
49 51 if (strchr(fmt, '\n') == NULL) {
50 52 len = strlen(msg);
51 53 (void) snprintf(msg + len, sizeof (msg) - len, ": %s\n",
52 54 strerror(oserr));
53 55 }
54 56
55 57 (void) write(STDERR_FILENO, msg, strlen(msg));
56 58
57 -abort:
58 59 abort();
59 - _exit(1);
60 60 }
61 61
62 62 /*PRINTFLIKE1*/
63 63 void
64 64 ses_panic(const char *fmt, ...)
65 65 {
66 66 va_list ap;
67 67
68 68 va_start(ap, fmt);
69 69 ses_vpanic(fmt, ap);
70 70 va_end(ap);
71 71 }
72 72
73 73 int
74 74 ses_assert(const char *expr, const char *file, int line)
75 75 {
76 76 ses_panic("\"%s\", line %d: assertion failed: %s\n", file, line, expr);
77 77
78 78 /*NOTREACHED*/
79 79 return (0);
80 80 }
81 81
82 82 int
83 83 nvlist_add_fixed_string(nvlist_t *nvl, const char *name,
84 84 const char *buf, size_t len)
85 85 {
86 86 char *str = alloca(len + 1);
87 87 bcopy(buf, str, len);
88 88 str[len] = '\0';
89 89
90 90 return (nvlist_add_string(nvl, name, str));
91 91 }
92 92
93 93 /*
94 94 * Like fixed_string, but clears any leading or trailing spaces.
95 95 */
96 96 int
97 97 nvlist_add_fixed_string_trunc(nvlist_t *nvl, const char *name,
98 98 const char *buf, size_t len)
99 99 {
100 100 while (buf[0] == ' ' && len > 0) {
101 101 buf++;
102 102 len--;
103 103 }
104 104
105 105 while (len > 0 && buf[len - 1] == ' ')
106 106 len--;
107 107
108 108 return (nvlist_add_fixed_string(nvl, name, buf, len));
109 109 }
110 110
111 111 ses_errno_t
112 112 ses_errno(void)
113 113 {
114 114 return (_ses_errno);
115 115 }
116 116
117 117 const char *
118 118 ses_errmsg(void)
119 119 {
120 120 if (_ses_errmsg[0] == '\0')
121 121 (void) snprintf(_ses_errmsg, sizeof (_ses_errmsg), "%s",
122 122 ses_strerror(_ses_errno));
123 123
124 124 return (_ses_errmsg);
125 125 }
126 126
127 127 const char *
128 128 ses_nv_error_member(void)
129 129 {
130 130 if (_ses_nverr_member[0] != '\0')
131 131 return (_ses_nverr_member);
132 132 else
133 133 return (NULL);
134 134 }
135 135
136 136 static int
137 137 __ses_set_errno(ses_errno_t err, const char *nvm)
138 138 {
139 139 if (nvm == NULL) {
140 140 _ses_nverr_member[0] = '\0';
141 141 } else {
142 142 (void) strlcpy(_ses_nverr_member, nvm,
143 143 sizeof (_ses_nverr_member));
144 144 }
145 145 _ses_errmsg[0] = '\0';
146 146 _ses_errno = err;
147 147
148 148 return (-1);
149 149 }
150 150
151 151 int
152 152 ses_set_errno(ses_errno_t err)
153 153 {
154 154 return (__ses_set_errno(err, NULL));
155 155 }
156 156
157 157 int
158 158 ses_set_nverrno(int err, const char *member)
159 159 {
160 160 ses_errno_t se = (err == ENOMEM || err == EAGAIN) ?
161 161 ESES_NOMEM : ESES_NVL;
162 162
163 163 /*
164 164 * If the error is ESES_NVL, then we should always have a member
165 165 * available. The only time 'member' is NULL is when nvlist_alloc()
166 166 * fails, which should only be possible if memory allocation fails.
167 167 */
168 168 assert(se == ESES_NOMEM || member != NULL);
169 169
170 170 return (__ses_set_errno(se, member));
171 171 }
172 172
173 173 static int
174 174 ses_verror(ses_errno_t err, const char *fmt, va_list ap)
175 175 {
176 176 int syserr = errno;
177 177 size_t n;
178 178 char *errmsg;
179 179
180 180 errmsg = alloca(sizeof (_ses_errmsg));
181 181 (void) vsnprintf(errmsg, sizeof (_ses_errmsg), fmt, ap);
182 182 (void) ses_set_errno(err);
183 183
184 184 n = strlen(errmsg);
185 185
186 186 while (n != 0 && errmsg[n - 1] == '\n')
187 187 errmsg[--n] = '\0';
188 188
189 189 bcopy(errmsg, _ses_errmsg, sizeof (_ses_errmsg));
190 190 errno = syserr;
191 191
192 192 return (-1);
193 193 }
194 194
195 195 static int
196 196 ses_vnverror(int err, const char *member, const char *fmt,
197 197 va_list ap)
198 198 {
199 199 int syserr = errno;
200 200 size_t n;
201 201 char *errmsg;
202 202
203 203 errmsg = alloca(sizeof (_ses_errmsg));
204 204 (void) vsnprintf(errmsg, sizeof (_ses_errmsg), fmt, ap);
205 205 (void) ses_set_nverrno(err, member);
206 206
207 207 n = strlen(errmsg);
208 208
209 209 while (n != 0 && errmsg[n - 1] == '\n')
210 210 errmsg[--n] = '\0';
211 211
212 212 (void) snprintf(errmsg + n, sizeof (_ses_errmsg) - n, ": %s",
213 213 strerror(err));
214 214
215 215 bcopy(errmsg, _ses_errmsg, sizeof (_ses_errmsg));
216 216 errno = syserr;
217 217
218 218 return (-1);
219 219 }
220 220
221 221 int
222 222 ses_error(ses_errno_t err, const char *fmt, ...)
223 223 {
224 224 va_list ap;
225 225 int rv;
226 226
227 227 va_start(ap, fmt);
228 228 rv = ses_verror(err, fmt, ap);
229 229 va_end(ap);
230 230
231 231 return (rv);
232 232 }
233 233
234 234 int
235 235 ses_nverror(int err, const char *member, const char *fmt, ...)
236 236 {
237 237 va_list ap;
238 238 int rv;
239 239
240 240 va_start(ap, fmt);
241 241 rv = ses_vnverror(err, member, fmt, ap);
242 242 va_end(ap);
243 243
244 244 return (rv);
245 245 }
246 246
247 247 int
248 248 ses_libscsi_error(libscsi_hdl_t *shp, const char *fmt, ...)
249 249 {
250 250 va_list ap;
251 251 char errmsg[LIBSES_ERRMSGLEN];
252 252 libscsi_errno_t se = libscsi_errno(shp);
253 253 ses_errno_t e;
254 254
255 255 switch (se) {
256 256 case ESCSI_NONE:
257 257 return (0);
258 258 case ESCSI_NOMEM:
259 259 e = ESES_NOMEM;
260 260 break;
261 261 case ESCSI_NOTSUP:
262 262 e = ESES_NOTSUP;
263 263 break;
264 264 case ESCSI_ZERO_LENGTH:
265 265 case ESCSI_VERSION:
266 266 case ESCSI_BADFLAGS:
267 267 case ESCSI_BOGUSFLAGS:
268 268 case ESCSI_BADLENGTH:
269 269 case ESCSI_NEEDBUF:
270 270 va_start(ap, fmt);
271 271 (void) vsnprintf(errmsg, sizeof (errmsg), fmt, ap);
272 272 va_end(ap);
273 273 ses_panic("%s: unexpected libscsi error %s: %s", errmsg,
274 274 libscsi_errname(se), libscsi_errmsg(shp));
275 275 break;
276 276 case ESCSI_UNKNOWN:
277 277 e = ESES_UNKNOWN;
278 278 break;
279 279 default:
280 280 e = ESES_LIBSCSI;
281 281 break;
282 282 }
283 283
284 284 va_start(ap, fmt);
285 285 (void) vsnprintf(errmsg, sizeof (errmsg), fmt, ap);
286 286 va_end(ap);
287 287
288 288 return (ses_error(e, "%s: %s", errmsg, libscsi_errmsg(shp)));
289 289 }
290 290
291 291 int
292 292 ses_scsi_error(libscsi_action_t *ap, const char *fmt, ...)
293 293 {
294 294 va_list args;
295 295 char errmsg[LIBSES_ERRMSGLEN];
296 296 uint64_t asc = 0, ascq = 0, key = 0;
297 297 const char *code, *keystr;
298 298
299 299 va_start(args, fmt);
300 300 (void) vsnprintf(errmsg, sizeof (errmsg), fmt, args);
301 301 va_end(args);
302 302
303 303 if (libscsi_action_parse_sense(ap, &key, &asc, &ascq, NULL) != 0)
304 304 return (ses_error(ESES_LIBSCSI,
305 305 "%s: SCSI status %d (no sense data available)", errmsg,
306 306 libscsi_action_get_status(ap)));
307 307
308 308 code = libscsi_sense_code_name(asc, ascq);
309 309 keystr = libscsi_sense_key_name(key);
310 310
311 311 return (ses_error(ESES_LIBSCSI, "%s: SCSI status %d sense key %llu "
312 312 "(%s) additional sense code 0x%llx/0x%llx (%s)", errmsg,
313 313 libscsi_action_get_status(ap), key, keystr ? keystr : "<unknown>",
314 314 asc, ascq, code ? code : "<unknown>"));
315 315 }
316 316
317 317 void *
318 318 ses_alloc(size_t sz)
319 319 {
320 320 void *p;
321 321
322 322 if (sz == 0)
323 323 ses_panic("attempted zero-length allocation");
324 324
325 325 if ((p = malloc(sz)) == NULL)
326 326 (void) ses_set_errno(ESES_NOMEM);
327 327
328 328 return (p);
329 329 }
330 330
331 331 void *
332 332 ses_zalloc(size_t sz)
333 333 {
334 334 void *p;
335 335
336 336 if ((p = ses_alloc(sz)) != NULL)
337 337 bzero(p, sz);
338 338
339 339 return (p);
340 340 }
341 341
342 342 char *
343 343 ses_strdup(const char *s)
344 344 {
345 345 char *p;
346 346 size_t len;
347 347
348 348 if (s == NULL)
349 349 ses_panic("attempted zero-length allocation");
350 350
351 351 len = strlen(s) + 1;
352 352
353 353 if ((p = ses_alloc(len)) != NULL)
354 354 bcopy(s, p, len);
355 355
356 356 return (p);
357 357 }
358 358
359 359 void *
360 360 ses_realloc(void *p, size_t sz)
361 361 {
362 362 if (sz == 0)
363 363 ses_panic("attempted zero-length allocation");
364 364
365 365 if ((p = realloc(p, sz)) == NULL)
366 366 (void) ses_set_errno(ESES_NOMEM);
367 367
368 368 return (p);
369 369 }
370 370
371 371 /*ARGSUSED*/
372 372 void
373 373 ses_free(void *p)
374 374 {
375 375 free(p);
376 376 }
↓ open down ↓ |
307 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX