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.
29 * $P4: //depot/projects/trustedbsd/openbsm/libbsm/bsm_errno.c#19 $
32 #include <sys/types.h>
34 #include <config/config.h>
36 #include <bsm/audit_errno.h>
37 #include <bsm/libbsm.h>
43 * Different operating systems use different numeric constants for different
44 * error numbers, and sometimes error numbers don't exist in more than one
45 * operating system. These routines convert between BSM and local error
46 * number spaces, subject to the above realities. BSM error numbers are
47 * stored in a single 8-bit character, so don't have a byte order.
49 * Don't include string definitions when this code is compiled into a kernel.
54 #if !defined(KERNEL) && !defined(_KERNEL)
55 const char *be_strerror;
59 #define ERRNO_NO_LOCAL_MAPPING -600
61 #if !defined(KERNEL) && !defined(_KERNEL)
68 * Mapping table -- please maintain in numeric sorted order with respect to
69 * the BSM constant. Today we do a linear lookup, but could switch to a
70 * binary search if it makes sense. We only ifdef errors that aren't
71 * generally available, but it does make the table a lot more ugly.
73 * XXXRW: It would be nice to have a similar ordered table mapping to BSM
74 * constant from local constant, but the order of local constants varies by
75 * OS. Really we need to build that table at compile-time but don't do that
78 * XXXRW: We currently embed English-language error strings here, but should
79 * support catalogues; these are only used if the OS doesn't have an error
80 * string using strerror(3).
82 static const struct bsm_errno bsm_errnos[] = {
83 { BSM_ERRNO_ESUCCESS, 0, ES("Success") },
84 { BSM_ERRNO_EPERM, EPERM, ES("Operation not permitted") },
85 { BSM_ERRNO_ENOENT, ENOENT, ES("No such file or directory") },
86 { BSM_ERRNO_ESRCH, ESRCH, ES("No such process") },
87 { BSM_ERRNO_EINTR, EINTR, ES("Interrupted system call") },
88 { BSM_ERRNO_EIO, EIO, ES("Input/output error") },
89 { BSM_ERRNO_ENXIO, ENXIO, ES("Device not configured") },
90 { BSM_ERRNO_E2BIG, E2BIG, ES("Argument list too long") },
91 { BSM_ERRNO_ENOEXEC, ENOEXEC, ES("Exec format error") },
92 { BSM_ERRNO_EBADF, EBADF, ES("Bad file descriptor") },
93 { BSM_ERRNO_ECHILD, ECHILD, ES("No child processes") },
94 { BSM_ERRNO_EAGAIN, EAGAIN, ES("Resource temporarily unavailable") },
95 { BSM_ERRNO_ENOMEM, ENOMEM, ES("Cannot allocate memory") },
96 { BSM_ERRNO_EACCES, EACCES, ES("Permission denied") },
97 { BSM_ERRNO_EFAULT, EFAULT, ES("Bad address") },
98 { BSM_ERRNO_ENOTBLK, ENOTBLK, ES("Block device required") },
99 { BSM_ERRNO_EBUSY, EBUSY, ES("Device busy") },
100 { BSM_ERRNO_EEXIST, EEXIST, ES("File exists") },
101 { BSM_ERRNO_EXDEV, EXDEV, ES("Cross-device link") },
102 { BSM_ERRNO_ENODEV, ENODEV, ES("Operation not supported by device") },
103 { BSM_ERRNO_ENOTDIR, ENOTDIR, ES("Not a directory") },
104 { BSM_ERRNO_EISDIR, EISDIR, ES("Is a directory") },
105 { BSM_ERRNO_EINVAL, EINVAL, ES("Invalid argument") },
106 { BSM_ERRNO_ENFILE, ENFILE, ES("Too many open files in system") },
107 { BSM_ERRNO_EMFILE, EMFILE, ES("Too many open files") },
108 { BSM_ERRNO_ENOTTY, ENOTTY, ES("Inappropriate ioctl for device") },
109 { BSM_ERRNO_ETXTBSY, ETXTBSY, ES("Text file busy") },
110 { BSM_ERRNO_EFBIG, EFBIG, ES("File too large") },
111 { BSM_ERRNO_ENOSPC, ENOSPC, ES("No space left on device") },
112 { BSM_ERRNO_ESPIPE, ESPIPE, ES("Illegal seek") },
113 { BSM_ERRNO_EROFS, EROFS, ES("Read-only file system") },
114 { BSM_ERRNO_EMLINK, EMLINK, ES("Too many links") },
115 { BSM_ERRNO_EPIPE, EPIPE, ES("Broken pipe") },
116 { BSM_ERRNO_EDOM, EDOM, ES("Numerical argument out of domain") },
117 { BSM_ERRNO_ERANGE, ERANGE, ES("Result too large") },
118 { BSM_ERRNO_ENOMSG, ENOMSG, ES("No message of desired type") },
119 { BSM_ERRNO_EIDRM, EIDRM, ES("Identifier removed") },
124 ERRNO_NO_LOCAL_MAPPING,
126 ES("Channel number out of range") },
127 { BSM_ERRNO_EL2NSYNC,
131 ERRNO_NO_LOCAL_MAPPING,
133 ES("Level 2 not synchronized") },
138 ERRNO_NO_LOCAL_MAPPING,
140 ES("Level 3 halted") },
145 ERRNO_NO_LOCAL_MAPPING,
147 ES("Level 3 reset") },
152 ERRNO_NO_LOCAL_MAPPING,
154 ES("Link number out of range") },
159 ERRNO_NO_LOCAL_MAPPING,
161 ES("Protocol driver not attached") },
166 ERRNO_NO_LOCAL_MAPPING,
168 ES("No CSI structure available") },
173 ERRNO_NO_LOCAL_MAPPING,
175 ES("Level 2 halted") },
176 { BSM_ERRNO_EDEADLK, EDEADLK, ES("Resource deadlock avoided") },
177 { BSM_ERRNO_ENOLCK, ENOLCK, ES("No locks available") },
178 { BSM_ERRNO_ECANCELED, ECANCELED, ES("Operation canceled") },
179 { BSM_ERRNO_ENOTSUP, ENOTSUP, ES("Operation not supported") },
180 { BSM_ERRNO_EDQUOT, EDQUOT, ES("Disc quota exceeded") },
185 ERRNO_NO_LOCAL_MAPPING,
187 ES("Invalid exchange") },
192 ERRNO_NO_LOCAL_MAPPING,
194 ES("Invalid request descriptor") },
199 ERRNO_NO_LOCAL_MAPPING,
201 ES("Exchange full") },
206 ERRNO_NO_LOCAL_MAPPING,
213 ERRNO_NO_LOCAL_MAPPING,
215 ES("Invalid request descriptor") },
220 ERRNO_NO_LOCAL_MAPPING,
222 ES("Invalid slot") },
223 { BSM_ERRNO_EDEADLOCK,
227 ERRNO_NO_LOCAL_MAPPING,
229 ES("Resource deadlock avoided") },
234 ERRNO_NO_LOCAL_MAPPING,
236 ES("Bad font file format") },
237 { BSM_ERRNO_EOWNERDEAD,
241 ERRNO_NO_LOCAL_MAPPING,
243 ES("Process died with the lock") },
244 { BSM_ERRNO_ENOTRECOVERABLE,
245 #ifdef ENOTRECOVERABLE
248 ERRNO_NO_LOCAL_MAPPING,
250 ES("Lock is not recoverable") },
255 ERRNO_NO_LOCAL_MAPPING,
257 ES("Device not a stream") },
262 ERRNO_NO_LOCAL_MAPPING,
264 ES("Machine is not on the network") },
269 ERRNO_NO_LOCAL_MAPPING,
271 ES("Package not installed") },
272 { BSM_ERRNO_EREMOTE, EREMOTE,
273 ES("Too many levels of remote in path") },
278 ERRNO_NO_LOCAL_MAPPING,
280 ES("Link has been severed") },
285 ERRNO_NO_LOCAL_MAPPING,
287 ES("Advertise error") },
292 ERRNO_NO_LOCAL_MAPPING,
294 ES("srmount error") },
299 ERRNO_NO_LOCAL_MAPPING,
301 ES("Communication error on send") },
306 ERRNO_NO_LOCAL_MAPPING,
308 ES("Protocol error") },
309 { BSM_ERRNO_ELOCKUNMAPPED,
313 ERRNO_NO_LOCAL_MAPPING,
315 ES("Locked lock was unmapped") },
316 { BSM_ERRNO_ENOTACTIVE,
320 ERRNO_NO_LOCAL_MAPPING,
322 ES("Facility is not active") },
323 { BSM_ERRNO_EMULTIHOP,
327 ERRNO_NO_LOCAL_MAPPING,
329 ES("Multihop attempted") },
334 ERRNO_NO_LOCAL_MAPPING,
337 { BSM_ERRNO_ENAMETOOLONG, ENAMETOOLONG, ES("File name too long") },
338 { BSM_ERRNO_EOVERFLOW, EOVERFLOW,
339 ES("Value too large to be stored in data type") },
340 { BSM_ERRNO_ENOTUNIQ,
344 ERRNO_NO_LOCAL_MAPPING,
346 ES("Given log name not unique") },
351 ERRNO_NO_LOCAL_MAPPING,
353 ES("Given f.d. invalid for this operation") },
358 ERRNO_NO_LOCAL_MAPPING,
360 ES("Remote address changed") },
365 ERRNO_NO_LOCAL_MAPPING,
367 ES("Can't access a needed shared lib") },
372 ERRNO_NO_LOCAL_MAPPING,
374 ES("Accessing a corrupted shared lib") },
379 ERRNO_NO_LOCAL_MAPPING,
381 ES(".lib section in a.out corrupted") },
386 ERRNO_NO_LOCAL_MAPPING,
388 ES("Attempting to link in too many libs") },
389 { BSM_ERRNO_ELIBEXEC,
393 ERRNO_NO_LOCAL_MAPPING,
395 ES("Attempting to exec a shared library") },
396 { BSM_ERRNO_EILSEQ, EILSEQ, ES("Illegal byte sequence") },
397 { BSM_ERRNO_ENOSYS, ENOSYS, ES("Function not implemented") },
398 { BSM_ERRNO_ELOOP, ELOOP, ES("Too many levels of symbolic links") },
399 { BSM_ERRNO_ERESTART,
403 ERRNO_NO_LOCAL_MAPPING,
405 ES("Restart syscall") },
406 { BSM_ERRNO_ESTRPIPE,
410 ERRNO_NO_LOCAL_MAPPING,
412 ES("If pipe/FIFO, don't sleep in stream head") },
413 { BSM_ERRNO_ENOTEMPTY, ENOTEMPTY, ES("Directory not empty") },
414 { BSM_ERRNO_EUSERS, EUSERS, ES("Too many users") },
415 { BSM_ERRNO_ENOTSOCK, ENOTSOCK,
416 ES("Socket operation on non-socket") },
417 { BSM_ERRNO_EDESTADDRREQ, EDESTADDRREQ,
418 ES("Destination address required") },
419 { BSM_ERRNO_EMSGSIZE, EMSGSIZE, ES("Message too long") },
420 { BSM_ERRNO_EPROTOTYPE, EPROTOTYPE,
421 ES("Protocol wrong type for socket") },
422 { BSM_ERRNO_ENOPROTOOPT, ENOPROTOOPT, ES("Protocol not available") },
423 { BSM_ERRNO_EPROTONOSUPPORT, EPROTONOSUPPORT,
424 ES("Protocol not supported") },
425 { BSM_ERRNO_ESOCKTNOSUPPORT, ESOCKTNOSUPPORT,
426 ES("Socket type not supported") },
427 { BSM_ERRNO_EOPNOTSUPP, EOPNOTSUPP, ES("Operation not supported") },
428 { BSM_ERRNO_EPFNOSUPPORT, EPFNOSUPPORT,
429 ES("Protocol family not supported") },
430 { BSM_ERRNO_EAFNOSUPPORT, EAFNOSUPPORT,
431 ES("Address family not supported by protocol family") },
432 { BSM_ERRNO_EADDRINUSE, EADDRINUSE, ES("Address already in use") },
433 { BSM_ERRNO_EADDRNOTAVAIL, EADDRNOTAVAIL,
434 ES("Can't assign requested address") },
435 { BSM_ERRNO_ENETDOWN, ENETDOWN, ES("Network is down") },
436 { BSM_ERRNO_ENETRESET, ENETRESET,
437 ES("Network dropped connection on reset") },
438 { BSM_ERRNO_ECONNABORTED, ECONNABORTED,
439 ES("Software caused connection abort") },
440 { BSM_ERRNO_ECONNRESET, ECONNRESET, ES("Connection reset by peer") },
441 { BSM_ERRNO_ENOBUFS, ENOBUFS, ES("No buffer space available") },
442 { BSM_ERRNO_EISCONN, EISCONN, ES("Socket is already connected") },
443 { BSM_ERRNO_ENOTCONN, ENOTCONN, ES("Socket is not connected") },
444 { BSM_ERRNO_ESHUTDOWN, ESHUTDOWN,
445 ES("Can't send after socket shutdown") },
446 { BSM_ERRNO_ETOOMANYREFS, ETOOMANYREFS,
447 ES("Too many references: can't splice") },
448 { BSM_ERRNO_ETIMEDOUT, ETIMEDOUT, ES("Operation timed out") },
449 { BSM_ERRNO_ECONNREFUSED, ECONNREFUSED, ES("Connection refused") },
450 { BSM_ERRNO_EHOSTDOWN, EHOSTDOWN, ES("Host is down") },
451 { BSM_ERRNO_EHOSTUNREACH, EHOSTUNREACH, ES("No route to host") },
452 { BSM_ERRNO_EALREADY, EALREADY, ES("Operation already in progress") },
453 { BSM_ERRNO_EINPROGRESS, EINPROGRESS,
454 ES("Operation now in progress") },
455 { BSM_ERRNO_ESTALE, ESTALE, ES("Stale NFS file handle") },
456 { BSM_ERRNO_EPROCLIM,
460 ERRNO_NO_LOCAL_MAPPING,
462 ES("Too many processes") },
467 ERRNO_NO_LOCAL_MAPPING,
469 ES("RPC struct is bad") },
470 { BSM_ERRNO_ERPCMISMATCH,
474 ERRNO_NO_LOCAL_MAPPING,
476 ES("RPC version wrong") },
477 { BSM_ERRNO_EPROGUNAVAIL,
481 ERRNO_NO_LOCAL_MAPPING,
483 ES("RPC prog. not avail") },
484 { BSM_ERRNO_EPROGMISMATCH,
488 ERRNO_NO_LOCAL_MAPPING,
490 ES("RPC version wrong") },
491 { BSM_ERRNO_EPROCUNAVAIL,
495 ERRNO_NO_LOCAL_MAPPING,
497 ES("Bad procedure for program") },
502 ERRNO_NO_LOCAL_MAPPING,
504 ES("Inappropriate file type or format") },
509 ERRNO_NO_LOCAL_MAPPING,
511 ES("Authenticateion error") },
512 { BSM_ERRNO_ENEEDAUTH,
516 ERRNO_NO_LOCAL_MAPPING,
518 ES("Need authenticator") },
523 ERRNO_NO_LOCAL_MAPPING,
525 ES("Attribute not found") },
530 ERRNO_NO_LOCAL_MAPPING,
532 ES("Programming error") },
533 { BSM_ERRNO_EJUSTRETURN,
537 ERRNO_NO_LOCAL_MAPPING,
540 { BSM_ERRNO_ENOIOCTL,
544 ERRNO_NO_LOCAL_MAPPING,
546 ES("ioctl not handled by this layer") },
547 { BSM_ERRNO_EDIRIOCTL,
551 ERRNO_NO_LOCAL_MAPPING,
553 ES("do direct ioctl in GEOM") },
558 ERRNO_NO_LOCAL_MAPPING,
560 ES("Device power is off") },
565 ERRNO_NO_LOCAL_MAPPING,
567 ES("Device error") },
568 { BSM_ERRNO_EBADEXEC,
572 ERRNO_NO_LOCAL_MAPPING,
574 ES("Bad executable") },
575 { BSM_ERRNO_EBADARCH,
579 ERRNO_NO_LOCAL_MAPPING,
581 ES("Bad CPU type in executable") },
582 { BSM_ERRNO_ESHLIBVERS,
586 ERRNO_NO_LOCAL_MAPPING,
588 ES("Shared library version mismatch") },
589 { BSM_ERRNO_EBADMACHO,
593 ERRNO_NO_LOCAL_MAPPING,
595 ES("Malformed Macho file") },
600 ERRNO_NO_LOCAL_MAPPING,
602 ES("Operation failed by policy") },
607 ERRNO_NO_LOCAL_MAPPING,
609 ES("RFS specific error") },
614 ERRNO_NO_LOCAL_MAPPING,
616 ES("Structure needs cleaning") },
621 ERRNO_NO_LOCAL_MAPPING,
623 ES("Not a XENIX named type file") },
628 ERRNO_NO_LOCAL_MAPPING,
630 ES("No XENIX semaphores available") },
635 ERRNO_NO_LOCAL_MAPPING,
637 ES("Is a named type file") },
638 { BSM_ERRNO_EREMOTEIO,
642 ERRNO_NO_LOCAL_MAPPING,
644 ES("Remote I/O error") },
645 { BSM_ERRNO_ENOMEDIUM,
649 ERRNO_NO_LOCAL_MAPPING,
651 ES("No medium found") },
652 { BSM_ERRNO_EMEDIUMTYPE,
656 ERRNO_NO_LOCAL_MAPPING,
658 ES("Wrong medium type") },
663 ERRNO_NO_LOCAL_MAPPING,
665 ES("Required key not available") },
666 { BSM_ERRNO_EKEYEXPIRED,
670 ERRNO_NO_LOCAL_MAPPING,
672 ES("Key has expired") },
673 { BSM_ERRNO_EKEYREVOKED,
677 ERRNO_NO_LOCAL_MAPPING,
679 ES("Key has been revoked") },
680 { BSM_ERRNO_EKEYREJECTED,
684 ERRNO_NO_LOCAL_MAPPING,
686 ES("Key was rejected by service") },
688 static const int bsm_errnos_count = sizeof(bsm_errnos) / sizeof(bsm_errnos[0]);
690 static const struct bsm_errno *
691 bsm_lookup_errno_local(int local_errno)
695 for (i = 0; i < bsm_errnos_count; i++) {
696 if (bsm_errnos[i].be_local_errno == local_errno)
697 return (&bsm_errnos[i]);
703 * Conversion to the BSM errno space isn't allowed to fail; we simply map to
704 * BSM_ERRNO_UNKNOWN and let the remote endpoint deal with it.
707 au_errno_to_bsm(int local_errno)
709 const struct bsm_errno *bsme;
711 bsme = bsm_lookup_errno_local(local_errno);
713 return (BSM_ERRNO_UNKNOWN);
714 return (bsme->be_bsm_errno);
717 static const struct bsm_errno *
718 bsm_lookup_errno_bsm(u_char bsm_errno)
722 for (i = 0; i < bsm_errnos_count; i++) {
723 if (bsm_errnos[i].be_bsm_errno == bsm_errno)
724 return (&bsm_errnos[i]);
730 * Converstion from a BSM error to a local error number may fail if either
731 * OpenBSM doesn't recognize the error on the wire, or because there is no
732 * appropriate local mapping.
735 au_bsm_to_errno(u_char bsm_errno, int *errorp)
737 const struct bsm_errno *bsme;
739 bsme = bsm_lookup_errno_bsm(bsm_errno);
740 if (bsme == NULL || bsme->be_local_errno == ERRNO_NO_LOCAL_MAPPING)
742 *errorp = bsme->be_local_errno;
746 #if !defined(KERNEL) && !defined(_KERNEL)
748 au_strerror(u_char bsm_errno)
750 const struct bsm_errno *bsme;
752 bsme = bsm_lookup_errno_bsm(bsm_errno);
754 return ("Unrecognized BSM error");
755 if (bsme->be_local_errno != ERRNO_NO_LOCAL_MAPPING)
756 return (strerror(bsme->be_local_errno));
757 return (bsme->be_strerror);