xref: /freebsd/contrib/openbsm/libbsm/bsm_errno.c (revision 25ecdc7d52770caf1c9b44b5ec11f468f6b636f3)
1 /*-
2  * Copyright (c) 2008 Apple Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1.  Redistributions of source code must retain the above copyright
9  *     notice, this list of conditions and the following disclaimer.
10  * 2.  Redistributions in binary form must reproduce the above copyright
11  *     notice, this list of conditions and the following disclaimer in the
12  *     documentation and/or other materials provided with the distribution.
13  * 3.  Neither the name of Apple Inc. ("Apple") nor the names of
14  *     its contributors may be used to endorse or promote products derived
15  *     from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR
21  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
26  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include <sys/types.h>
31 
32 #include <config/config.h>
33 
34 #include <bsm/audit_errno.h>
35 #include <bsm/libbsm.h>
36 
37 #include <errno.h>
38 #include <string.h>
39 
40 /*
41  * Different operating systems use different numeric constants for different
42  * error numbers, and sometimes error numbers don't exist in more than one
43  * operating system.  These routines convert between BSM and local error
44  * number spaces, subject to the above realities.  BSM error numbers are
45  * stored in a single 8-bit character, so don't have a byte order.
46  *
47  * Don't include string definitions when this code is compiled into a kernel.
48  */
49 struct bsm_errno {
50 	int		 be_bsm_errno;
51 	int		 be_local_errno;
52 #if !defined(KERNEL) && !defined(_KERNEL)
53 	const char	*be_strerror;
54 #endif
55 };
56 
57 #define	ERRNO_NO_LOCAL_MAPPING	-600
58 
59 #if !defined(KERNEL) && !defined(_KERNEL)
60 #define	ES(x)	x
61 #else
62 #define	ES(x)
63 #endif
64 
65 /*
66  * Mapping table -- please maintain in numeric sorted order with respect to
67  * the BSM constant.  Today we do a linear lookup, but could switch to a
68  * binary search if it makes sense.  We only ifdef errors that aren't
69  * generally available, but it does make the table a lot more ugly.
70  *
71  * XXXRW: It would be nice to have a similar ordered table mapping to BSM
72  * constant from local constant, but the order of local constants varies by
73  * OS.  Really we need to build that table at compile-time but don't do that
74  * yet.
75  *
76  * XXXRW: We currently embed English-language error strings here, but should
77  * support catalogues; these are only used if the OS doesn't have an error
78  * string using strerror(3).
79  */
80 static const struct bsm_errno bsm_errnos[] = {
81 	{ BSM_ERRNO_ESUCCESS, 0, ES("Success") },
82 	{ BSM_ERRNO_EPERM, EPERM, ES("Operation not permitted") },
83 	{ BSM_ERRNO_ENOENT, ENOENT, ES("No such file or directory") },
84 	{ BSM_ERRNO_ESRCH, ESRCH, ES("No such process") },
85 	{ BSM_ERRNO_EINTR, EINTR, ES("Interrupted system call") },
86 	{ BSM_ERRNO_EIO, EIO, ES("Input/output error") },
87 	{ BSM_ERRNO_ENXIO, ENXIO, ES("Device not configured") },
88 	{ BSM_ERRNO_E2BIG, E2BIG, ES("Argument list too long") },
89 	{ BSM_ERRNO_ENOEXEC, ENOEXEC, ES("Exec format error") },
90 	{ BSM_ERRNO_EBADF, EBADF, ES("Bad file descriptor") },
91 	{ BSM_ERRNO_ECHILD, ECHILD, ES("No child processes") },
92 	{ BSM_ERRNO_EAGAIN, EAGAIN, ES("Resource temporarily unavailable") },
93 	{ BSM_ERRNO_ENOMEM, ENOMEM, ES("Cannot allocate memory") },
94 	{ BSM_ERRNO_EACCES, EACCES, ES("Permission denied") },
95 	{ BSM_ERRNO_EFAULT, EFAULT, ES("Bad address") },
96 	{ BSM_ERRNO_ENOTBLK, ENOTBLK, ES("Block device required") },
97 	{ BSM_ERRNO_EBUSY, EBUSY, ES("Device busy") },
98 	{ BSM_ERRNO_EEXIST, EEXIST, ES("File exists") },
99 	{ BSM_ERRNO_EXDEV, EXDEV, ES("Cross-device link") },
100 	{ BSM_ERRNO_ENODEV, ENODEV, ES("Operation not supported by device") },
101 	{ BSM_ERRNO_ENOTDIR, ENOTDIR, ES("Not a directory") },
102 	{ BSM_ERRNO_EISDIR, EISDIR, ES("Is a directory") },
103 	{ BSM_ERRNO_EINVAL, EINVAL, ES("Invalid argument") },
104 	{ BSM_ERRNO_ENFILE, ENFILE, ES("Too many open files in system") },
105 	{ BSM_ERRNO_EMFILE, EMFILE, ES("Too many open files") },
106 	{ BSM_ERRNO_ENOTTY, ENOTTY, ES("Inappropriate ioctl for device") },
107 	{ BSM_ERRNO_ETXTBSY, ETXTBSY, ES("Text file busy") },
108 	{ BSM_ERRNO_EFBIG, EFBIG, ES("File too large") },
109 	{ BSM_ERRNO_ENOSPC, ENOSPC, ES("No space left on device") },
110 	{ BSM_ERRNO_ESPIPE, ESPIPE, ES("Illegal seek") },
111 	{ BSM_ERRNO_EROFS, EROFS, ES("Read-only file system") },
112 	{ BSM_ERRNO_EMLINK, EMLINK, ES("Too many links") },
113 	{ BSM_ERRNO_EPIPE, EPIPE, ES("Broken pipe") },
114 	{ BSM_ERRNO_EDOM, EDOM, ES("Numerical argument out of domain") },
115 	{ BSM_ERRNO_ERANGE, ERANGE, ES("Result too large") },
116 	{ BSM_ERRNO_ENOMSG, ENOMSG, ES("No message of desired type") },
117 	{ BSM_ERRNO_EIDRM, EIDRM, ES("Identifier removed") },
118 	{ BSM_ERRNO_ECHRNG,
119 #ifdef ECHRNG
120 	ECHRNG,
121 #else
122 	ERRNO_NO_LOCAL_MAPPING,
123 #endif
124 	ES("Channel number out of range") },
125 	{ BSM_ERRNO_EL2NSYNC,
126 #ifdef EL2NSYNC
127 	EL2NSYNC,
128 #else
129 	ERRNO_NO_LOCAL_MAPPING,
130 #endif
131 	ES("Level 2 not synchronized") },
132 	{ BSM_ERRNO_EL3HLT,
133 #ifdef EL3HLT
134 	EL3HLT,
135 #else
136 	ERRNO_NO_LOCAL_MAPPING,
137 #endif
138 	ES("Level 3 halted") },
139 	{ BSM_ERRNO_EL3RST,
140 #ifdef EL3RST
141 	EL3RST,
142 #else
143 	ERRNO_NO_LOCAL_MAPPING,
144 #endif
145 	ES("Level 3 reset") },
146 	{ BSM_ERRNO_ELNRNG,
147 #ifdef ELNRNG
148 	ELNRNG,
149 #else
150 	ERRNO_NO_LOCAL_MAPPING,
151 #endif
152 	ES("Link number out of range") },
153 	{ BSM_ERRNO_EUNATCH,
154 #ifdef EUNATCH
155 	EUNATCH,
156 #else
157 	ERRNO_NO_LOCAL_MAPPING,
158 #endif
159 	ES("Protocol driver not attached") },
160 	{ BSM_ERRNO_ENOCSI,
161 #ifdef ENOCSI
162 	ENOCSI,
163 #else
164 	ERRNO_NO_LOCAL_MAPPING,
165 #endif
166 	ES("No CSI structure available") },
167 	{ BSM_ERRNO_EL2HLT,
168 #ifdef EL2HLT
169 	EL2HLT,
170 #else
171 	ERRNO_NO_LOCAL_MAPPING,
172 #endif
173 	ES("Level 2 halted") },
174 	{ BSM_ERRNO_EDEADLK, EDEADLK, ES("Resource deadlock avoided") },
175 	{ BSM_ERRNO_ENOLCK, ENOLCK, ES("No locks available") },
176 	{ BSM_ERRNO_ECANCELED, ECANCELED, ES("Operation canceled") },
177 	{ BSM_ERRNO_ENOTSUP, ENOTSUP, ES("Operation not supported") },
178 	{ BSM_ERRNO_EDQUOT, EDQUOT, ES("Disc quota exceeded") },
179 	{ BSM_ERRNO_EBADE,
180 #ifdef EBADE
181 	EBADE,
182 #else
183 	ERRNO_NO_LOCAL_MAPPING,
184 #endif
185 	ES("Invalid exchange") },
186 	{ BSM_ERRNO_EBADR,
187 #ifdef EBADR
188 	EBADR,
189 #else
190 	ERRNO_NO_LOCAL_MAPPING,
191 #endif
192 	ES("Invalid request descriptor") },
193 	{ BSM_ERRNO_EXFULL,
194 #ifdef EXFULL
195 	EXFULL,
196 #else
197 	ERRNO_NO_LOCAL_MAPPING,
198 #endif
199 	ES("Exchange full") },
200 	{ BSM_ERRNO_ENOANO,
201 #ifdef ENOANO
202 	ENOANO,
203 #else
204 	ERRNO_NO_LOCAL_MAPPING,
205 #endif
206 	ES("No anode") },
207 	{ BSM_ERRNO_EBADRQC,
208 #ifdef EBADRQC
209 	EBADRQC,
210 #else
211 	ERRNO_NO_LOCAL_MAPPING,
212 #endif
213 	ES("Invalid request descriptor") },
214 	{ BSM_ERRNO_EBADSLT,
215 #ifdef EBADSLT
216 	EBADSLT,
217 #else
218 	ERRNO_NO_LOCAL_MAPPING,
219 #endif
220 	ES("Invalid slot") },
221 	{ BSM_ERRNO_EDEADLOCK,
222 #ifdef EDEADLOCK
223 	EDEADLOCK,
224 #else
225 	ERRNO_NO_LOCAL_MAPPING,
226 #endif
227 	ES("Resource deadlock avoided") },
228 	{ BSM_ERRNO_EBFONT,
229 #ifdef EBFONT
230 	EBFONT,
231 #else
232 	ERRNO_NO_LOCAL_MAPPING,
233 #endif
234 	ES("Bad font file format") },
235 	{ BSM_ERRNO_EOWNERDEAD,
236 #ifdef EOWNERDEAD
237 	EOWNERDEAD,
238 #else
239 	ERRNO_NO_LOCAL_MAPPING,
240 #endif
241 	ES("Process died with the lock") },
242 	{ BSM_ERRNO_EINTEGRITY,
243 #ifdef EINTEGRITY
244 	EINTEGRITY,
245 #else
246 	ERRNO_NO_LOCAL_MAPPING,
247 #endif
248 	ES("Integrity check failed") },
249 	{ BSM_ERRNO_ENOTRECOVERABLE,
250 #ifdef ENOTRECOVERABLE
251 	ENOTRECOVERABLE,
252 #else
253 	ERRNO_NO_LOCAL_MAPPING,
254 #endif
255 	ES("Lock is not recoverable") },
256 	{ BSM_ERRNO_ENOSTR,
257 #ifdef ENOSTR
258 	ENOSTR,
259 #else
260 	ERRNO_NO_LOCAL_MAPPING,
261 #endif
262 	ES("Device not a stream") },
263 	{ BSM_ERRNO_ENONET,
264 #ifdef ENONET
265 	ENONET,
266 #else
267 	ERRNO_NO_LOCAL_MAPPING,
268 #endif
269 	ES("Machine is not on the network") },
270 	{ BSM_ERRNO_ENOPKG,
271 #ifdef ENOPKG
272 	ENOPKG,
273 #else
274 	ERRNO_NO_LOCAL_MAPPING,
275 #endif
276 	ES("Package not installed") },
277 	{ BSM_ERRNO_EREMOTE, EREMOTE,
278 	    ES("Too many levels of remote in path") },
279 	{ BSM_ERRNO_ENOLINK,
280 #ifdef ENOLINK
281 	ENOLINK,
282 #else
283 	ERRNO_NO_LOCAL_MAPPING,
284 #endif
285 	ES("Link has been severed") },
286 	{ BSM_ERRNO_EADV,
287 #ifdef EADV
288 	EADV,
289 #else
290 	ERRNO_NO_LOCAL_MAPPING,
291 #endif
292 	ES("Advertise error") },
293 	{ BSM_ERRNO_ESRMNT,
294 #ifdef ESRMNT
295 	ESRMNT,
296 #else
297 	ERRNO_NO_LOCAL_MAPPING,
298 #endif
299 	ES("srmount error") },
300 	{ BSM_ERRNO_ECOMM,
301 #ifdef ECOMM
302 	ECOMM,
303 #else
304 	ERRNO_NO_LOCAL_MAPPING,
305 #endif
306 	ES("Communication error on send") },
307 	{ BSM_ERRNO_EPROTO,
308 #ifdef EPROTO
309 	EPROTO,
310 #else
311 	ERRNO_NO_LOCAL_MAPPING,
312 #endif
313 	ES("Protocol error") },
314 	{ BSM_ERRNO_ELOCKUNMAPPED,
315 #ifdef ELOCKUNMAPPED
316 	ELOCKUNMAPPED,
317 #else
318 	ERRNO_NO_LOCAL_MAPPING,
319 #endif
320 	ES("Locked lock was unmapped") },
321 	{ BSM_ERRNO_ENOTACTIVE,
322 #ifdef ENOTACTIVE
323 	ENOTACTIVE,
324 #else
325 	ERRNO_NO_LOCAL_MAPPING,
326 #endif
327 	ES("Facility is not active") },
328 	{ BSM_ERRNO_EMULTIHOP,
329 #ifdef EMULTIHOP
330 	EMULTIHOP,
331 #else
332 	ERRNO_NO_LOCAL_MAPPING,
333 #endif
334 	ES("Multihop attempted") },
335 	{ BSM_ERRNO_EBADMSG,
336 #ifdef EBADMSG
337 	EBADMSG,
338 #else
339 	ERRNO_NO_LOCAL_MAPPING,
340 #endif
341 	ES("Bad message") },
342 	{ BSM_ERRNO_ENAMETOOLONG, ENAMETOOLONG, ES("File name too long") },
343 	{ BSM_ERRNO_EOVERFLOW, EOVERFLOW,
344 	    ES("Value too large to be stored in data type") },
345 	{ BSM_ERRNO_ENOTUNIQ,
346 #ifdef ENOTUNIQ
347 	ENOTUNIQ,
348 #else
349 	ERRNO_NO_LOCAL_MAPPING,
350 #endif
351 	ES("Given log name not unique") },
352 	{ BSM_ERRNO_EBADFD,
353 #ifdef EBADFD
354 	EBADFD,
355 #else
356 	ERRNO_NO_LOCAL_MAPPING,
357 #endif
358 	ES("Given f.d. invalid for this operation") },
359 	{ BSM_ERRNO_EREMCHG,
360 #ifdef EREMCHG
361 	EREMCHG,
362 #else
363 	ERRNO_NO_LOCAL_MAPPING,
364 #endif
365 	ES("Remote address changed") },
366 	{ BSM_ERRNO_ELIBACC,
367 #ifdef ELIBACC
368 	ELIBACC,
369 #else
370 	ERRNO_NO_LOCAL_MAPPING,
371 #endif
372 	ES("Can't access a needed shared lib") },
373 	{ BSM_ERRNO_ELIBBAD,
374 #ifdef ELIBBAD
375 	ELIBBAD,
376 #else
377 	ERRNO_NO_LOCAL_MAPPING,
378 #endif
379 	ES("Accessing a corrupted shared lib") },
380 	{ BSM_ERRNO_ELIBSCN,
381 #ifdef ELIBSCN
382 	ELIBSCN,
383 #else
384 	ERRNO_NO_LOCAL_MAPPING,
385 #endif
386 	ES(".lib section in a.out corrupted") },
387 	{ BSM_ERRNO_ELIBMAX,
388 #ifdef ELIBMAX
389 	ELIBMAX,
390 #else
391 	ERRNO_NO_LOCAL_MAPPING,
392 #endif
393 	ES("Attempting to link in too many libs") },
394 	{ BSM_ERRNO_ELIBEXEC,
395 #ifdef ELIBEXEC
396 	ELIBEXEC,
397 #else
398 	ERRNO_NO_LOCAL_MAPPING,
399 #endif
400 	ES("Attempting to exec a shared library") },
401 	{ BSM_ERRNO_EILSEQ, EILSEQ, ES("Illegal byte sequence") },
402 	{ BSM_ERRNO_ENOSYS, ENOSYS, ES("Function not implemented") },
403 	{ BSM_ERRNO_ELOOP, ELOOP, ES("Too many levels of symbolic links") },
404 	{ BSM_ERRNO_ERESTART,
405 #ifdef ERESTART
406 	ERESTART,
407 #else
408 	ERRNO_NO_LOCAL_MAPPING,
409 #endif
410 	ES("Restart syscall") },
411 	{ BSM_ERRNO_ESTRPIPE,
412 #ifdef ESTRPIPE
413 	ESTRPIPE,
414 #else
415 	ERRNO_NO_LOCAL_MAPPING,
416 #endif
417 	ES("If pipe/FIFO, don't sleep in stream head") },
418 	{ BSM_ERRNO_ENOTEMPTY, ENOTEMPTY, ES("Directory not empty") },
419 	{ BSM_ERRNO_EUSERS, EUSERS, ES("Too many users") },
420 	{ BSM_ERRNO_ENOTSOCK, ENOTSOCK,
421 	    ES("Socket operation on non-socket") },
422 	{ BSM_ERRNO_EDESTADDRREQ, EDESTADDRREQ,
423 	    ES("Destination address required") },
424 	{ BSM_ERRNO_EMSGSIZE, EMSGSIZE, ES("Message too long") },
425 	{ BSM_ERRNO_EPROTOTYPE, EPROTOTYPE,
426 	    ES("Protocol wrong type for socket") },
427 	{ BSM_ERRNO_ENOPROTOOPT, ENOPROTOOPT, ES("Protocol not available") },
428 	{ BSM_ERRNO_EPROTONOSUPPORT, EPROTONOSUPPORT,
429 	    ES("Protocol not supported") },
430 	{ BSM_ERRNO_ESOCKTNOSUPPORT, ESOCKTNOSUPPORT,
431 	    ES("Socket type not supported") },
432 	{ BSM_ERRNO_EOPNOTSUPP, EOPNOTSUPP, ES("Operation not supported") },
433 	{ BSM_ERRNO_EPFNOSUPPORT, EPFNOSUPPORT,
434 	    ES("Protocol family not supported") },
435 	{ BSM_ERRNO_EAFNOSUPPORT, EAFNOSUPPORT,
436 	    ES("Address family not supported by protocol family") },
437 	{ BSM_ERRNO_EADDRINUSE, EADDRINUSE, ES("Address already in use") },
438 	{ BSM_ERRNO_EADDRNOTAVAIL, EADDRNOTAVAIL,
439 	    ES("Can't assign requested address") },
440 	{ BSM_ERRNO_ENETDOWN, ENETDOWN, ES("Network is down") },
441 	{ BSM_ERRNO_ENETRESET, ENETRESET,
442 	    ES("Network dropped connection on reset") },
443 	{ BSM_ERRNO_ECONNABORTED, ECONNABORTED,
444 	    ES("Software caused connection abort") },
445 	{ BSM_ERRNO_ECONNRESET, ECONNRESET, ES("Connection reset by peer") },
446 	{ BSM_ERRNO_ENOBUFS, ENOBUFS, ES("No buffer space available") },
447 	{ BSM_ERRNO_EISCONN, EISCONN, ES("Socket is already connected") },
448 	{ BSM_ERRNO_ENOTCONN, ENOTCONN, ES("Socket is not connected") },
449 	{ BSM_ERRNO_ESHUTDOWN, ESHUTDOWN,
450 	    ES("Can't send after socket shutdown") },
451 	{ BSM_ERRNO_ETOOMANYREFS, ETOOMANYREFS,
452 	    ES("Too many references: can't splice") },
453 	{ BSM_ERRNO_ETIMEDOUT, ETIMEDOUT, ES("Operation timed out") },
454 	{ BSM_ERRNO_ECONNREFUSED, ECONNREFUSED, ES("Connection refused") },
455 	{ BSM_ERRNO_EHOSTDOWN, EHOSTDOWN, ES("Host is down") },
456 	{ BSM_ERRNO_EHOSTUNREACH, EHOSTUNREACH, ES("No route to host") },
457 	{ BSM_ERRNO_EALREADY, EALREADY, ES("Operation already in progress") },
458 	{ BSM_ERRNO_EINPROGRESS, EINPROGRESS,
459 	    ES("Operation now in progress") },
460 	{ BSM_ERRNO_ESTALE, ESTALE, ES("Stale NFS file handle") },
461 	{ BSM_ERRNO_EPROCLIM,
462 #ifdef EPROCLIM
463 	EPROCLIM,
464 #else
465 	ERRNO_NO_LOCAL_MAPPING,
466 #endif
467 	ES("Too many processes") },
468 	{ BSM_ERRNO_EBADRPC,
469 #ifdef EBADRPC
470 	EBADRPC,
471 #else
472 	ERRNO_NO_LOCAL_MAPPING,
473 #endif
474 	ES("RPC struct is bad") },
475 	{ BSM_ERRNO_ERPCMISMATCH,
476 #ifdef ERPCMISMATCH
477 	ERPCMISMATCH,
478 #else
479 	ERRNO_NO_LOCAL_MAPPING,
480 #endif
481 	ES("RPC version wrong") },
482 	{ BSM_ERRNO_EPROGUNAVAIL,
483 #ifdef EPROGUNAVAIL
484 	EPROGUNAVAIL,
485 #else
486 	ERRNO_NO_LOCAL_MAPPING,
487 #endif
488 	ES("RPC prog. not avail") },
489 	{ BSM_ERRNO_EPROGMISMATCH,
490 #ifdef EPROGMISMATCH
491 	EPROGMISMATCH,
492 #else
493 	ERRNO_NO_LOCAL_MAPPING,
494 #endif
495 	ES("RPC version wrong") },
496 	{ BSM_ERRNO_EPROCUNAVAIL,
497 #ifdef EPROCUNAVAIL
498 	EPROCUNAVAIL,
499 #else
500 	ERRNO_NO_LOCAL_MAPPING,
501 #endif
502 	ES("Bad procedure for program") },
503 	{ BSM_ERRNO_EFTYPE,
504 #ifdef EFTYPE
505 	EFTYPE,
506 #else
507 	ERRNO_NO_LOCAL_MAPPING,
508 #endif
509 	ES("Inappropriate file type or format") },
510 	{ BSM_ERRNO_EAUTH,
511 #ifdef EAUTH
512 	EAUTH,
513 #else
514 	ERRNO_NO_LOCAL_MAPPING,
515 #endif
516 	ES("Authenticateion error") },
517 	{ BSM_ERRNO_ENEEDAUTH,
518 #ifdef ENEEDAUTH
519 	ENEEDAUTH,
520 #else
521 	ERRNO_NO_LOCAL_MAPPING,
522 #endif
523 	ES("Need authenticator") },
524 	{ BSM_ERRNO_ENOATTR,
525 #ifdef ENOATTR
526 	ENOATTR,
527 #else
528 	ERRNO_NO_LOCAL_MAPPING,
529 #endif
530 	ES("Attribute not found") },
531 	{ BSM_ERRNO_EDOOFUS,
532 #ifdef EDOOFUS
533 	EDOOFUS,
534 #else
535 	ERRNO_NO_LOCAL_MAPPING,
536 #endif
537 	ES("Programming error") },
538 	{ BSM_ERRNO_EJUSTRETURN,
539 #ifdef EJUSTRETURN
540 	EJUSTRETURN,
541 #else
542 	ERRNO_NO_LOCAL_MAPPING,
543 #endif
544 	ES("Just return") },
545 	{ BSM_ERRNO_ENOIOCTL,
546 #ifdef ENOIOCTL
547 	ENOIOCTL,
548 #else
549 	ERRNO_NO_LOCAL_MAPPING,
550 #endif
551 	ES("ioctl not handled by this layer") },
552 	{ BSM_ERRNO_EDIRIOCTL,
553 #ifdef EDIRIOCTL
554 	EDIRIOCTL,
555 #else
556 	ERRNO_NO_LOCAL_MAPPING,
557 #endif
558 	ES("do direct ioctl in GEOM") },
559 	{ BSM_ERRNO_EPWROFF,
560 #ifdef EPWROFF
561 	EPWROFF,
562 #else
563 	ERRNO_NO_LOCAL_MAPPING,
564 #endif
565 	ES("Device power is off") },
566 	{ BSM_ERRNO_EDEVERR,
567 #ifdef EDEVERR
568 	EDEVERR,
569 #else
570 	ERRNO_NO_LOCAL_MAPPING,
571 #endif
572 	ES("Device error") },
573 	{ BSM_ERRNO_EBADEXEC,
574 #ifdef EBADEXEC
575 	EBADEXEC,
576 #else
577 	ERRNO_NO_LOCAL_MAPPING,
578 #endif
579 	ES("Bad executable") },
580 	{ BSM_ERRNO_EBADARCH,
581 #ifdef EBADARCH
582 	EBADARCH,
583 #else
584 	ERRNO_NO_LOCAL_MAPPING,
585 #endif
586 	ES("Bad CPU type in executable") },
587 	{ BSM_ERRNO_ESHLIBVERS,
588 #ifdef ESHLIBVERS
589 	ESHLIBVERS,
590 #else
591 	ERRNO_NO_LOCAL_MAPPING,
592 #endif
593 	ES("Shared library version mismatch") },
594 	{ BSM_ERRNO_EBADMACHO,
595 #ifdef EBADMACHO
596 	EBADMACHO,
597 #else
598 	ERRNO_NO_LOCAL_MAPPING,
599 #endif
600 	ES("Malformed Macho file") },
601 	{ BSM_ERRNO_EPOLICY,
602 #ifdef EPOLICY
603 	EPOLICY,
604 #else
605 	ERRNO_NO_LOCAL_MAPPING,
606 #endif
607 	ES("Operation failed by policy") },
608 	{ BSM_ERRNO_EDOTDOT,
609 #ifdef EDOTDOT
610 	EDOTDOT,
611 #else
612 	ERRNO_NO_LOCAL_MAPPING,
613 #endif
614 	ES("RFS specific error") },
615 	{ BSM_ERRNO_EUCLEAN,
616 #ifdef EUCLEAN
617 	EUCLEAN,
618 #else
619 	ERRNO_NO_LOCAL_MAPPING,
620 #endif
621 	ES("Structure needs cleaning") },
622 	{ BSM_ERRNO_ENOTNAM,
623 #ifdef ENOTNAM
624 	ENOTNAM,
625 #else
626 	ERRNO_NO_LOCAL_MAPPING,
627 #endif
628 	ES("Not a XENIX named type file") },
629 	{ BSM_ERRNO_ENAVAIL,
630 #ifdef ENAVAIL
631 	ENAVAIL,
632 #else
633 	ERRNO_NO_LOCAL_MAPPING,
634 #endif
635 	ES("No XENIX semaphores available") },
636 	{ BSM_ERRNO_EISNAM,
637 #ifdef EISNAM
638 	EISNAM,
639 #else
640 	ERRNO_NO_LOCAL_MAPPING,
641 #endif
642 	ES("Is a named type file") },
643 	{ BSM_ERRNO_EREMOTEIO,
644 #ifdef EREMOTEIO
645 	EREMOTEIO,
646 #else
647 	ERRNO_NO_LOCAL_MAPPING,
648 #endif
649 	ES("Remote I/O error") },
650 	{ BSM_ERRNO_ENOMEDIUM,
651 #ifdef ENOMEDIUM
652 	ENOMEDIUM,
653 #else
654 	ERRNO_NO_LOCAL_MAPPING,
655 #endif
656 	ES("No medium found") },
657 	{ BSM_ERRNO_EMEDIUMTYPE,
658 #ifdef EMEDIUMTYPE
659 	EMEDIUMTYPE,
660 #else
661 	ERRNO_NO_LOCAL_MAPPING,
662 #endif
663 	ES("Wrong medium type") },
664 	{ BSM_ERRNO_ENOKEY,
665 #ifdef ENOKEY
666 	ENOKEY,
667 #else
668 	ERRNO_NO_LOCAL_MAPPING,
669 #endif
670 	ES("Required key not available") },
671 	{ BSM_ERRNO_EKEYEXPIRED,
672 #ifdef EKEYEXPIRED
673 	EKEYEXPIRED,
674 #else
675 	ERRNO_NO_LOCAL_MAPPING,
676 #endif
677 	ES("Key has expired") },
678 	{ BSM_ERRNO_EKEYREVOKED,
679 #ifdef EKEYREVOKED
680 	EKEYREVOKED,
681 #else
682 	ERRNO_NO_LOCAL_MAPPING,
683 #endif
684 	ES("Key has been revoked") },
685 	{ BSM_ERRNO_EKEYREJECTED,
686 #ifdef EKEYREJECTED
687 	EKEYREJECTED,
688 #else
689 	ERRNO_NO_LOCAL_MAPPING,
690 #endif
691 	ES("Key was rejected by service") },
692 	{ BSM_ERRNO_ENOTCAPABLE,
693 #ifdef ENOTCAPABLE
694 	ENOTCAPABLE,
695 #else
696 	ERRNO_NO_LOCAL_MAPPING,
697 #endif
698 	ES("Capabilities insufficient") },
699 	{ BSM_ERRNO_ECAPMODE,
700 #ifdef ECAPMODE
701 	ECAPMODE,
702 #else
703 	ERRNO_NO_LOCAL_MAPPING,
704 #endif
705 	ES("Not permitted in capability mode") },
706 };
707 static const int bsm_errnos_count = sizeof(bsm_errnos) / sizeof(bsm_errnos[0]);
708 
709 static const struct bsm_errno *
710 bsm_lookup_errno_local(int local_errno)
711 {
712 	int i;
713 
714 	for (i = 0; i < bsm_errnos_count; i++) {
715 		if (bsm_errnos[i].be_local_errno == local_errno)
716 			return (&bsm_errnos[i]);
717 	}
718 	return (NULL);
719 }
720 
721 /*
722  * Conversion to the BSM errno space isn't allowed to fail; we simply map to
723  * BSM_ERRNO_UNKNOWN and let the remote endpoint deal with it.
724  */
725 u_char
726 au_errno_to_bsm(int local_errno)
727 {
728 	const struct bsm_errno *bsme;
729 
730 	bsme = bsm_lookup_errno_local(local_errno);
731 	if (bsme == NULL)
732 		return (BSM_ERRNO_UNKNOWN);
733 	return (bsme->be_bsm_errno);
734 }
735 
736 static const struct bsm_errno *
737 bsm_lookup_errno_bsm(u_char bsm_errno)
738 {
739 	int i;
740 
741 	for (i = 0; i < bsm_errnos_count; i++) {
742 		if (bsm_errnos[i].be_bsm_errno == bsm_errno)
743 			return (&bsm_errnos[i]);
744 	}
745 	return (NULL);
746 }
747 
748 /*
749  * Converstion from a BSM error to a local error number may fail if either
750  * OpenBSM doesn't recognize the error on the wire, or because there is no
751  * appropriate local mapping.
752  */
753 int
754 au_bsm_to_errno(u_char bsm_errno, int *errorp)
755 {
756 	const struct bsm_errno *bsme;
757 
758 	bsme = bsm_lookup_errno_bsm(bsm_errno);
759 	if (bsme == NULL || bsme->be_local_errno == ERRNO_NO_LOCAL_MAPPING)
760 		return (-1);
761 	*errorp = bsme->be_local_errno;
762 	return (0);
763 }
764 
765 #if !defined(KERNEL) && !defined(_KERNEL)
766 const char *
767 au_strerror(u_char bsm_errno)
768 {
769 	const struct bsm_errno *bsme;
770 
771 	bsme = bsm_lookup_errno_bsm(bsm_errno);
772 	if (bsme == NULL)
773 		return ("Unrecognized BSM error");
774 	if (bsme->be_local_errno != ERRNO_NO_LOCAL_MAPPING)
775 		return (strerror(bsme->be_local_errno));
776 	return (bsme->be_strerror);
777 }
778 #endif
779