2 * Copyright (c) 2008 Apple Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
30 #include <sys/types.h>
32 #include <config/config.h>
34 #include <bsm/audit_errno.h>
35 #include <bsm/libbsm.h>
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.
47 * Don't include string definitions when this code is compiled into a kernel.
52 #if !defined(KERNEL) && !defined(_KERNEL)
53 const char *be_strerror;
57 #define ERRNO_NO_LOCAL_MAPPING -600
59 #if !defined(KERNEL) && !defined(_KERNEL)
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.
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
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).
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") },
122 ERRNO_NO_LOCAL_MAPPING,
124 ES("Channel number out of range") },
125 { BSM_ERRNO_EL2NSYNC,
129 ERRNO_NO_LOCAL_MAPPING,
131 ES("Level 2 not synchronized") },
136 ERRNO_NO_LOCAL_MAPPING,
138 ES("Level 3 halted") },
143 ERRNO_NO_LOCAL_MAPPING,
145 ES("Level 3 reset") },
150 ERRNO_NO_LOCAL_MAPPING,
152 ES("Link number out of range") },
157 ERRNO_NO_LOCAL_MAPPING,
159 ES("Protocol driver not attached") },
164 ERRNO_NO_LOCAL_MAPPING,
166 ES("No CSI structure available") },
171 ERRNO_NO_LOCAL_MAPPING,
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") },
183 ERRNO_NO_LOCAL_MAPPING,
185 ES("Invalid exchange") },
190 ERRNO_NO_LOCAL_MAPPING,
192 ES("Invalid request descriptor") },
197 ERRNO_NO_LOCAL_MAPPING,
199 ES("Exchange full") },
204 ERRNO_NO_LOCAL_MAPPING,
211 ERRNO_NO_LOCAL_MAPPING,
213 ES("Invalid request descriptor") },
218 ERRNO_NO_LOCAL_MAPPING,
220 ES("Invalid slot") },
221 { BSM_ERRNO_EDEADLOCK,
225 ERRNO_NO_LOCAL_MAPPING,
227 ES("Resource deadlock avoided") },
232 ERRNO_NO_LOCAL_MAPPING,
234 ES("Bad font file format") },
235 { BSM_ERRNO_EOWNERDEAD,
239 ERRNO_NO_LOCAL_MAPPING,
241 ES("Process died with the lock") },
242 { BSM_ERRNO_ENOTRECOVERABLE,
243 #ifdef ENOTRECOVERABLE
246 ERRNO_NO_LOCAL_MAPPING,
248 ES("Lock is not recoverable") },
253 ERRNO_NO_LOCAL_MAPPING,
255 ES("Device not a stream") },
260 ERRNO_NO_LOCAL_MAPPING,
262 ES("Machine is not on the network") },
267 ERRNO_NO_LOCAL_MAPPING,
269 ES("Package not installed") },
270 { BSM_ERRNO_EREMOTE, EREMOTE,
271 ES("Too many levels of remote in path") },
276 ERRNO_NO_LOCAL_MAPPING,
278 ES("Link has been severed") },
283 ERRNO_NO_LOCAL_MAPPING,
285 ES("Advertise error") },
290 ERRNO_NO_LOCAL_MAPPING,
292 ES("srmount error") },
297 ERRNO_NO_LOCAL_MAPPING,
299 ES("Communication error on send") },
304 ERRNO_NO_LOCAL_MAPPING,
306 ES("Protocol error") },
307 { BSM_ERRNO_ELOCKUNMAPPED,
311 ERRNO_NO_LOCAL_MAPPING,
313 ES("Locked lock was unmapped") },
314 { BSM_ERRNO_ENOTACTIVE,
318 ERRNO_NO_LOCAL_MAPPING,
320 ES("Facility is not active") },
321 { BSM_ERRNO_EMULTIHOP,
325 ERRNO_NO_LOCAL_MAPPING,
327 ES("Multihop attempted") },
332 ERRNO_NO_LOCAL_MAPPING,
335 { BSM_ERRNO_ENAMETOOLONG, ENAMETOOLONG, ES("File name too long") },
336 { BSM_ERRNO_EOVERFLOW, EOVERFLOW,
337 ES("Value too large to be stored in data type") },
338 { BSM_ERRNO_ENOTUNIQ,
342 ERRNO_NO_LOCAL_MAPPING,
344 ES("Given log name not unique") },
349 ERRNO_NO_LOCAL_MAPPING,
351 ES("Given f.d. invalid for this operation") },
356 ERRNO_NO_LOCAL_MAPPING,
358 ES("Remote address changed") },
363 ERRNO_NO_LOCAL_MAPPING,
365 ES("Can't access a needed shared lib") },
370 ERRNO_NO_LOCAL_MAPPING,
372 ES("Accessing a corrupted shared lib") },
377 ERRNO_NO_LOCAL_MAPPING,
379 ES(".lib section in a.out corrupted") },
384 ERRNO_NO_LOCAL_MAPPING,
386 ES("Attempting to link in too many libs") },
387 { BSM_ERRNO_ELIBEXEC,
391 ERRNO_NO_LOCAL_MAPPING,
393 ES("Attempting to exec a shared library") },
394 { BSM_ERRNO_EILSEQ, EILSEQ, ES("Illegal byte sequence") },
395 { BSM_ERRNO_ENOSYS, ENOSYS, ES("Function not implemented") },
396 { BSM_ERRNO_ELOOP, ELOOP, ES("Too many levels of symbolic links") },
397 { BSM_ERRNO_ERESTART,
401 ERRNO_NO_LOCAL_MAPPING,
403 ES("Restart syscall") },
404 { BSM_ERRNO_ESTRPIPE,
408 ERRNO_NO_LOCAL_MAPPING,
410 ES("If pipe/FIFO, don't sleep in stream head") },
411 { BSM_ERRNO_ENOTEMPTY, ENOTEMPTY, ES("Directory not empty") },
412 { BSM_ERRNO_EUSERS, EUSERS, ES("Too many users") },
413 { BSM_ERRNO_ENOTSOCK, ENOTSOCK,
414 ES("Socket operation on non-socket") },
415 { BSM_ERRNO_EDESTADDRREQ, EDESTADDRREQ,
416 ES("Destination address required") },
417 { BSM_ERRNO_EMSGSIZE, EMSGSIZE, ES("Message too long") },
418 { BSM_ERRNO_EPROTOTYPE, EPROTOTYPE,
419 ES("Protocol wrong type for socket") },
420 { BSM_ERRNO_ENOPROTOOPT, ENOPROTOOPT, ES("Protocol not available") },
421 { BSM_ERRNO_EPROTONOSUPPORT, EPROTONOSUPPORT,
422 ES("Protocol not supported") },
423 { BSM_ERRNO_ESOCKTNOSUPPORT, ESOCKTNOSUPPORT,
424 ES("Socket type not supported") },
425 { BSM_ERRNO_EOPNOTSUPP, EOPNOTSUPP, ES("Operation not supported") },
426 { BSM_ERRNO_EPFNOSUPPORT, EPFNOSUPPORT,
427 ES("Protocol family not supported") },
428 { BSM_ERRNO_EAFNOSUPPORT, EAFNOSUPPORT,
429 ES("Address family not supported by protocol family") },
430 { BSM_ERRNO_EADDRINUSE, EADDRINUSE, ES("Address already in use") },
431 { BSM_ERRNO_EADDRNOTAVAIL, EADDRNOTAVAIL,
432 ES("Can't assign requested address") },
433 { BSM_ERRNO_ENETDOWN, ENETDOWN, ES("Network is down") },
434 { BSM_ERRNO_ENETRESET, ENETRESET,
435 ES("Network dropped connection on reset") },
436 { BSM_ERRNO_ECONNABORTED, ECONNABORTED,
437 ES("Software caused connection abort") },
438 { BSM_ERRNO_ECONNRESET, ECONNRESET, ES("Connection reset by peer") },
439 { BSM_ERRNO_ENOBUFS, ENOBUFS, ES("No buffer space available") },
440 { BSM_ERRNO_EISCONN, EISCONN, ES("Socket is already connected") },
441 { BSM_ERRNO_ENOTCONN, ENOTCONN, ES("Socket is not connected") },
442 { BSM_ERRNO_ESHUTDOWN, ESHUTDOWN,
443 ES("Can't send after socket shutdown") },
444 { BSM_ERRNO_ETOOMANYREFS, ETOOMANYREFS,
445 ES("Too many references: can't splice") },
446 { BSM_ERRNO_ETIMEDOUT, ETIMEDOUT, ES("Operation timed out") },
447 { BSM_ERRNO_ECONNREFUSED, ECONNREFUSED, ES("Connection refused") },
448 { BSM_ERRNO_EHOSTDOWN, EHOSTDOWN, ES("Host is down") },
449 { BSM_ERRNO_EHOSTUNREACH, EHOSTUNREACH, ES("No route to host") },
450 { BSM_ERRNO_EALREADY, EALREADY, ES("Operation already in progress") },
451 { BSM_ERRNO_EINPROGRESS, EINPROGRESS,
452 ES("Operation now in progress") },
453 { BSM_ERRNO_ESTALE, ESTALE, ES("Stale NFS file handle") },
454 { BSM_ERRNO_EPROCLIM,
458 ERRNO_NO_LOCAL_MAPPING,
460 ES("Too many processes") },
465 ERRNO_NO_LOCAL_MAPPING,
467 ES("RPC struct is bad") },
468 { BSM_ERRNO_ERPCMISMATCH,
472 ERRNO_NO_LOCAL_MAPPING,
474 ES("RPC version wrong") },
475 { BSM_ERRNO_EPROGUNAVAIL,
479 ERRNO_NO_LOCAL_MAPPING,
481 ES("RPC prog. not avail") },
482 { BSM_ERRNO_EPROGMISMATCH,
486 ERRNO_NO_LOCAL_MAPPING,
488 ES("RPC version wrong") },
489 { BSM_ERRNO_EPROCUNAVAIL,
493 ERRNO_NO_LOCAL_MAPPING,
495 ES("Bad procedure for program") },
500 ERRNO_NO_LOCAL_MAPPING,
502 ES("Inappropriate file type or format") },
507 ERRNO_NO_LOCAL_MAPPING,
509 ES("Authenticateion error") },
510 { BSM_ERRNO_ENEEDAUTH,
514 ERRNO_NO_LOCAL_MAPPING,
516 ES("Need authenticator") },
521 ERRNO_NO_LOCAL_MAPPING,
523 ES("Attribute not found") },
528 ERRNO_NO_LOCAL_MAPPING,
530 ES("Programming error") },
531 { BSM_ERRNO_EJUSTRETURN,
535 ERRNO_NO_LOCAL_MAPPING,
538 { BSM_ERRNO_ENOIOCTL,
542 ERRNO_NO_LOCAL_MAPPING,
544 ES("ioctl not handled by this layer") },
545 { BSM_ERRNO_EDIRIOCTL,
549 ERRNO_NO_LOCAL_MAPPING,
551 ES("do direct ioctl in GEOM") },
556 ERRNO_NO_LOCAL_MAPPING,
558 ES("Device power is off") },
563 ERRNO_NO_LOCAL_MAPPING,
565 ES("Device error") },
566 { BSM_ERRNO_EBADEXEC,
570 ERRNO_NO_LOCAL_MAPPING,
572 ES("Bad executable") },
573 { BSM_ERRNO_EBADARCH,
577 ERRNO_NO_LOCAL_MAPPING,
579 ES("Bad CPU type in executable") },
580 { BSM_ERRNO_ESHLIBVERS,
584 ERRNO_NO_LOCAL_MAPPING,
586 ES("Shared library version mismatch") },
587 { BSM_ERRNO_EBADMACHO,
591 ERRNO_NO_LOCAL_MAPPING,
593 ES("Malformed Macho file") },
598 ERRNO_NO_LOCAL_MAPPING,
600 ES("Operation failed by policy") },
605 ERRNO_NO_LOCAL_MAPPING,
607 ES("RFS specific error") },
612 ERRNO_NO_LOCAL_MAPPING,
614 ES("Structure needs cleaning") },
619 ERRNO_NO_LOCAL_MAPPING,
621 ES("Not a XENIX named type file") },
626 ERRNO_NO_LOCAL_MAPPING,
628 ES("No XENIX semaphores available") },
633 ERRNO_NO_LOCAL_MAPPING,
635 ES("Is a named type file") },
636 { BSM_ERRNO_EREMOTEIO,
640 ERRNO_NO_LOCAL_MAPPING,
642 ES("Remote I/O error") },
643 { BSM_ERRNO_ENOMEDIUM,
647 ERRNO_NO_LOCAL_MAPPING,
649 ES("No medium found") },
650 { BSM_ERRNO_EMEDIUMTYPE,
654 ERRNO_NO_LOCAL_MAPPING,
656 ES("Wrong medium type") },
661 ERRNO_NO_LOCAL_MAPPING,
663 ES("Required key not available") },
664 { BSM_ERRNO_EKEYEXPIRED,
668 ERRNO_NO_LOCAL_MAPPING,
670 ES("Key has expired") },
671 { BSM_ERRNO_EKEYREVOKED,
675 ERRNO_NO_LOCAL_MAPPING,
677 ES("Key has been revoked") },
678 { BSM_ERRNO_EKEYREJECTED,
682 ERRNO_NO_LOCAL_MAPPING,
684 ES("Key was rejected by service") },
685 { BSM_ERRNO_ENOTCAPABLE,
689 ERRNO_NO_LOCAL_MAPPING,
691 ES("Capabilities insufficient") },
692 { BSM_ERRNO_ECAPMODE,
696 ERRNO_NO_LOCAL_MAPPING,
698 ES("Not permitted in capability mode") },
700 static const int bsm_errnos_count = sizeof(bsm_errnos) / sizeof(bsm_errnos[0]);
702 static const struct bsm_errno *
703 bsm_lookup_errno_local(int local_errno)
707 for (i = 0; i < bsm_errnos_count; i++) {
708 if (bsm_errnos[i].be_local_errno == local_errno)
709 return (&bsm_errnos[i]);
715 * Conversion to the BSM errno space isn't allowed to fail; we simply map to
716 * BSM_ERRNO_UNKNOWN and let the remote endpoint deal with it.
719 au_errno_to_bsm(int local_errno)
721 const struct bsm_errno *bsme;
723 bsme = bsm_lookup_errno_local(local_errno);
725 return (BSM_ERRNO_UNKNOWN);
726 return (bsme->be_bsm_errno);
729 static const struct bsm_errno *
730 bsm_lookup_errno_bsm(u_char bsm_errno)
734 for (i = 0; i < bsm_errnos_count; i++) {
735 if (bsm_errnos[i].be_bsm_errno == bsm_errno)
736 return (&bsm_errnos[i]);
742 * Converstion from a BSM error to a local error number may fail if either
743 * OpenBSM doesn't recognize the error on the wire, or because there is no
744 * appropriate local mapping.
747 au_bsm_to_errno(u_char bsm_errno, int *errorp)
749 const struct bsm_errno *bsme;
751 bsme = bsm_lookup_errno_bsm(bsm_errno);
752 if (bsme == NULL || bsme->be_local_errno == ERRNO_NO_LOCAL_MAPPING)
754 *errorp = bsme->be_local_errno;
758 #if !defined(KERNEL) && !defined(_KERNEL)
760 au_strerror(u_char bsm_errno)
762 const struct bsm_errno *bsme;
764 bsme = bsm_lookup_errno_bsm(bsm_errno);
766 return ("Unrecognized BSM error");
767 if (bsme->be_local_errno != ERRNO_NO_LOCAL_MAPPING)
768 return (strerror(bsme->be_local_errno));
769 return (bsme->be_strerror);