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_EINTEGRITY,
246 ERRNO_NO_LOCAL_MAPPING,
248 ES("Integrity check failed") },
249 { BSM_ERRNO_ENOTRECOVERABLE,
250 #ifdef ENOTRECOVERABLE
253 ERRNO_NO_LOCAL_MAPPING,
255 ES("Lock is not recoverable") },
260 ERRNO_NO_LOCAL_MAPPING,
262 ES("Device not a stream") },
267 ERRNO_NO_LOCAL_MAPPING,
269 ES("Machine is not on the network") },
274 ERRNO_NO_LOCAL_MAPPING,
276 ES("Package not installed") },
277 { BSM_ERRNO_EREMOTE, EREMOTE,
278 ES("Too many levels of remote in path") },
283 ERRNO_NO_LOCAL_MAPPING,
285 ES("Link has been severed") },
290 ERRNO_NO_LOCAL_MAPPING,
292 ES("Advertise error") },
297 ERRNO_NO_LOCAL_MAPPING,
299 ES("srmount error") },
304 ERRNO_NO_LOCAL_MAPPING,
306 ES("Communication error on send") },
311 ERRNO_NO_LOCAL_MAPPING,
313 ES("Protocol error") },
314 { BSM_ERRNO_ELOCKUNMAPPED,
318 ERRNO_NO_LOCAL_MAPPING,
320 ES("Locked lock was unmapped") },
321 { BSM_ERRNO_ENOTACTIVE,
325 ERRNO_NO_LOCAL_MAPPING,
327 ES("Facility is not active") },
328 { BSM_ERRNO_EMULTIHOP,
332 ERRNO_NO_LOCAL_MAPPING,
334 ES("Multihop attempted") },
339 ERRNO_NO_LOCAL_MAPPING,
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,
349 ERRNO_NO_LOCAL_MAPPING,
351 ES("Given log name not unique") },
356 ERRNO_NO_LOCAL_MAPPING,
358 ES("Given f.d. invalid for this operation") },
363 ERRNO_NO_LOCAL_MAPPING,
365 ES("Remote address changed") },
370 ERRNO_NO_LOCAL_MAPPING,
372 ES("Can't access a needed shared lib") },
377 ERRNO_NO_LOCAL_MAPPING,
379 ES("Accessing a corrupted shared lib") },
384 ERRNO_NO_LOCAL_MAPPING,
386 ES(".lib section in a.out corrupted") },
391 ERRNO_NO_LOCAL_MAPPING,
393 ES("Attempting to link in too many libs") },
394 { BSM_ERRNO_ELIBEXEC,
398 ERRNO_NO_LOCAL_MAPPING,
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,
408 ERRNO_NO_LOCAL_MAPPING,
410 ES("Restart syscall") },
411 { BSM_ERRNO_ESTRPIPE,
415 ERRNO_NO_LOCAL_MAPPING,
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,
465 ERRNO_NO_LOCAL_MAPPING,
467 ES("Too many processes") },
472 ERRNO_NO_LOCAL_MAPPING,
474 ES("RPC struct is bad") },
475 { BSM_ERRNO_ERPCMISMATCH,
479 ERRNO_NO_LOCAL_MAPPING,
481 ES("RPC version wrong") },
482 { BSM_ERRNO_EPROGUNAVAIL,
486 ERRNO_NO_LOCAL_MAPPING,
488 ES("RPC prog. not avail") },
489 { BSM_ERRNO_EPROGMISMATCH,
493 ERRNO_NO_LOCAL_MAPPING,
495 ES("RPC version wrong") },
496 { BSM_ERRNO_EPROCUNAVAIL,
500 ERRNO_NO_LOCAL_MAPPING,
502 ES("Bad procedure for program") },
507 ERRNO_NO_LOCAL_MAPPING,
509 ES("Inappropriate file type or format") },
514 ERRNO_NO_LOCAL_MAPPING,
516 ES("Authenticateion error") },
517 { BSM_ERRNO_ENEEDAUTH,
521 ERRNO_NO_LOCAL_MAPPING,
523 ES("Need authenticator") },
528 ERRNO_NO_LOCAL_MAPPING,
530 ES("Attribute not found") },
535 ERRNO_NO_LOCAL_MAPPING,
537 ES("Programming error") },
538 { BSM_ERRNO_EJUSTRETURN,
542 ERRNO_NO_LOCAL_MAPPING,
545 { BSM_ERRNO_ENOIOCTL,
549 ERRNO_NO_LOCAL_MAPPING,
551 ES("ioctl not handled by this layer") },
552 { BSM_ERRNO_EDIRIOCTL,
556 ERRNO_NO_LOCAL_MAPPING,
558 ES("do direct ioctl in GEOM") },
563 ERRNO_NO_LOCAL_MAPPING,
565 ES("Device power is off") },
570 ERRNO_NO_LOCAL_MAPPING,
572 ES("Device error") },
573 { BSM_ERRNO_EBADEXEC,
577 ERRNO_NO_LOCAL_MAPPING,
579 ES("Bad executable") },
580 { BSM_ERRNO_EBADARCH,
584 ERRNO_NO_LOCAL_MAPPING,
586 ES("Bad CPU type in executable") },
587 { BSM_ERRNO_ESHLIBVERS,
591 ERRNO_NO_LOCAL_MAPPING,
593 ES("Shared library version mismatch") },
594 { BSM_ERRNO_EBADMACHO,
598 ERRNO_NO_LOCAL_MAPPING,
600 ES("Malformed Macho file") },
605 ERRNO_NO_LOCAL_MAPPING,
607 ES("Operation failed by policy") },
612 ERRNO_NO_LOCAL_MAPPING,
614 ES("RFS specific error") },
619 ERRNO_NO_LOCAL_MAPPING,
621 ES("Structure needs cleaning") },
626 ERRNO_NO_LOCAL_MAPPING,
628 ES("Not a XENIX named type file") },
633 ERRNO_NO_LOCAL_MAPPING,
635 ES("No XENIX semaphores available") },
640 ERRNO_NO_LOCAL_MAPPING,
642 ES("Is a named type file") },
643 { BSM_ERRNO_EREMOTEIO,
647 ERRNO_NO_LOCAL_MAPPING,
649 ES("Remote I/O error") },
650 { BSM_ERRNO_ENOMEDIUM,
654 ERRNO_NO_LOCAL_MAPPING,
656 ES("No medium found") },
657 { BSM_ERRNO_EMEDIUMTYPE,
661 ERRNO_NO_LOCAL_MAPPING,
663 ES("Wrong medium type") },
668 ERRNO_NO_LOCAL_MAPPING,
670 ES("Required key not available") },
671 { BSM_ERRNO_EKEYEXPIRED,
675 ERRNO_NO_LOCAL_MAPPING,
677 ES("Key has expired") },
678 { BSM_ERRNO_EKEYREVOKED,
682 ERRNO_NO_LOCAL_MAPPING,
684 ES("Key has been revoked") },
685 { BSM_ERRNO_EKEYREJECTED,
689 ERRNO_NO_LOCAL_MAPPING,
691 ES("Key was rejected by service") },
692 { BSM_ERRNO_ENOTCAPABLE,
696 ERRNO_NO_LOCAL_MAPPING,
698 ES("Capabilities insufficient") },
699 { BSM_ERRNO_ECAPMODE,
703 ERRNO_NO_LOCAL_MAPPING,
705 ES("Not permitted in capability mode") },
707 static const int bsm_errnos_count = sizeof(bsm_errnos) / sizeof(bsm_errnos[0]);
709 static const struct bsm_errno *
710 bsm_lookup_errno_local(int local_errno)
714 for (i = 0; i < bsm_errnos_count; i++) {
715 if (bsm_errnos[i].be_local_errno == local_errno)
716 return (&bsm_errnos[i]);
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.
726 au_errno_to_bsm(int local_errno)
728 const struct bsm_errno *bsme;
730 bsme = bsm_lookup_errno_local(local_errno);
732 return (BSM_ERRNO_UNKNOWN);
733 return (bsme->be_bsm_errno);
736 static const struct bsm_errno *
737 bsm_lookup_errno_bsm(u_char bsm_errno)
741 for (i = 0; i < bsm_errnos_count; i++) {
742 if (bsm_errnos[i].be_bsm_errno == bsm_errno)
743 return (&bsm_errnos[i]);
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.
754 au_bsm_to_errno(u_char bsm_errno, int *errorp)
756 const struct bsm_errno *bsme;
758 bsme = bsm_lookup_errno_bsm(bsm_errno);
759 if (bsme == NULL || bsme->be_local_errno == ERRNO_NO_LOCAL_MAPPING)
761 *errorp = bsme->be_local_errno;
765 #if !defined(KERNEL) && !defined(_KERNEL)
767 au_strerror(u_char bsm_errno)
769 const struct bsm_errno *bsme;
771 bsme = bsm_lookup_errno_bsm(bsm_errno);
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);