bsm_errno.c revision 195740
175899Sjoe/*-
275899Sjoe * Copyright (c) 2008 Apple Inc.
375899Sjoe * All rights reserved.
475899Sjoe *
575899Sjoe * Redistribution and use in source and binary forms, with or without
675899Sjoe * modification, are permitted provided that the following conditions
775899Sjoe * are met:
875899Sjoe * 1.  Redistributions of source code must retain the above copyright
975899Sjoe *     notice, this list of conditions and the following disclaimer.
1075899Sjoe * 2.  Redistributions in binary form must reproduce the above copyright
1175899Sjoe *     notice, this list of conditions and the following disclaimer in the
1275899Sjoe *     documentation and/or other materials provided with the distribution.
1375899Sjoe * 3.  Neither the name of Apple Inc. ("Apple") nor the names of
1475899Sjoe *     its contributors may be used to endorse or promote products derived
1575899Sjoe *     from this software without specific prior written permission.
1675899Sjoe *
1775899Sjoe * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND
1875899Sjoe * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1975899Sjoe * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2075899Sjoe * ARE DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR
21139126Sru * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2275899Sjoe * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2375899Sjoe * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24102344Sluigi * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25102344Sluigi * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
26102344Sluigi * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27102344Sluigi * POSSIBILITY OF SUCH DAMAGE.
2884312Sluigi *
2984312Sluigi * $P4: //depot/projects/trustedbsd/openbsm/libbsm/bsm_errno.c#19 $
3084312Sluigi */
3184312Sluigi
32102344Sluigi#include <sys/types.h>
33102344Sluigi
34102344Sluigi#include <config/config.h>
35102344Sluigi
3675899Sjoe#include <bsm/audit_errno.h>
3775899Sjoe#include <bsm/libbsm.h>
3875899Sjoe
3975899Sjoe#include <errno.h>
4075899Sjoe#include <string.h>
4175899Sjoe
4275899Sjoe/*
4375899Sjoe * Different operating systems use different numeric constants for different
4475899Sjoe * error numbers, and sometimes error numbers don't exist in more than one
4575899Sjoe * operating system.  These routines convert between BSM and local error
4675899Sjoe * number spaces, subject to the above realities.  BSM error numbers are
4775899Sjoe * stored in a single 8-bit character, so don't have a byte order.
4875899Sjoe *
4975899Sjoe * Don't include string definitions when this code is compiled into a kernel.
5075899Sjoe */
5175899Sjoestruct bsm_errno {
5275899Sjoe	int		 be_bsm_errno;
5375899Sjoe	int		 be_local_errno;
5475899Sjoe#if !defined(KERNEL) && !defined(_KERNEL)
5575899Sjoe	const char	*be_strerror;
5675899Sjoe#endif
5775899Sjoe};
5875899Sjoe
5975899Sjoe#define	ERRNO_NO_LOCAL_MAPPING	-600
6075899Sjoe
6175899Sjoe#if !defined(KERNEL) && !defined(_KERNEL)
6275899Sjoe#define	ES(x)	x
6375899Sjoe#else
6475899Sjoe#define	ES(x)
6575899Sjoe#endif
6675899Sjoe
6775899Sjoe/*
6875899Sjoe * Mapping table -- please maintain in numeric sorted order with respect to
6975899Sjoe * the BSM constant.  Today we do a linear lookup, but could switch to a
7075899Sjoe * binary search if it makes sense.  We only ifdef errors that aren't
7175899Sjoe * generally available, but it does make the table a lot more ugly.
7275899Sjoe *
7375899Sjoe * XXXRW: It would be nice to have a similar ordered table mapping to BSM
7475899Sjoe * constant from local constant, but the order of local constants varies by
7575899Sjoe * OS.  Really we need to build that table at compile-time but don't do that
7675899Sjoe * yet.
7775899Sjoe *
7875899Sjoe * XXXRW: We currently embed English-language error strings here, but should
7975899Sjoe * support catalogues; these are only used if the OS doesn't have an error
8075899Sjoe * string using strerror(3).
8175899Sjoe */
8275899Sjoestatic const struct bsm_errno bsm_errnos[] = {
8375899Sjoe	{ BSM_ERRNO_ESUCCESS, 0, ES("Success") },
8475899Sjoe	{ BSM_ERRNO_EPERM, EPERM, ES("Operation not permitted") },
8575899Sjoe	{ BSM_ERRNO_ENOENT, ENOENT, ES("No such file or directory") },
8675899Sjoe	{ BSM_ERRNO_ESRCH, ESRCH, ES("No such process") },
8775899Sjoe	{ BSM_ERRNO_EINTR, EINTR, ES("Interrupted system call") },
8875899Sjoe	{ BSM_ERRNO_EIO, EIO, ES("Input/output error") },
8975899Sjoe	{ BSM_ERRNO_ENXIO, ENXIO, ES("Device not configured") },
9075899Sjoe	{ BSM_ERRNO_E2BIG, E2BIG, ES("Argument list too long") },
9175899Sjoe	{ BSM_ERRNO_ENOEXEC, ENOEXEC, ES("Exec format error") },
9275899Sjoe	{ BSM_ERRNO_EBADF, EBADF, ES("Bad file descriptor") },
9375899Sjoe	{ BSM_ERRNO_ECHILD, ECHILD, ES("No child processes") },
9475899Sjoe	{ BSM_ERRNO_EAGAIN, EAGAIN, ES("Resource temporarily unavailable") },
9575899Sjoe	{ BSM_ERRNO_ENOMEM, ENOMEM, ES("Cannot allocate memory") },
9675899Sjoe	{ BSM_ERRNO_EACCES, EACCES, ES("Permission denied") },
9775899Sjoe	{ BSM_ERRNO_EFAULT, EFAULT, ES("Bad address") },
9883086Sluigi	{ BSM_ERRNO_ENOTBLK, ENOTBLK, ES("Block device required") },
9990661Sluigi	{ BSM_ERRNO_EBUSY, EBUSY, ES("Device busy") },
10075899Sjoe	{ BSM_ERRNO_EEXIST, EEXIST, ES("File exists") },
10175899Sjoe	{ BSM_ERRNO_EXDEV, EXDEV, ES("Cross-device link") },
10275899Sjoe	{ BSM_ERRNO_ENODEV, ENODEV, ES("Operation not supported by device") },
10375899Sjoe	{ BSM_ERRNO_ENOTDIR, ENOTDIR, ES("Not a directory") },
10475899Sjoe	{ BSM_ERRNO_EISDIR, EISDIR, ES("Is a directory") },
10575899Sjoe	{ BSM_ERRNO_EINVAL, EINVAL, ES("Invalid argument") },
10675899Sjoe	{ BSM_ERRNO_ENFILE, ENFILE, ES("Too many open files in system") },
10775899Sjoe	{ BSM_ERRNO_EMFILE, EMFILE, ES("Too many open files") },
10875899Sjoe	{ BSM_ERRNO_ENOTTY, ENOTTY, ES("Inappropriate ioctl for device") },
10975899Sjoe	{ BSM_ERRNO_ETXTBSY, ETXTBSY, ES("Text file busy") },
11075899Sjoe	{ BSM_ERRNO_EFBIG, EFBIG, ES("File too large") },
11175899Sjoe	{ BSM_ERRNO_ENOSPC, ENOSPC, ES("No space left on device") },
11277579Sru	{ BSM_ERRNO_ESPIPE, ESPIPE, ES("Illegal seek") },
11375899Sjoe	{ BSM_ERRNO_EROFS, EROFS, ES("Read-only file system") },
11475899Sjoe	{ BSM_ERRNO_EMLINK, EMLINK, ES("Too many links") },
11575899Sjoe	{ BSM_ERRNO_EPIPE, EPIPE, ES("Broken pipe") },
11675899Sjoe	{ BSM_ERRNO_EDOM, EDOM, ES("Numerical argument out of domain") },
11775899Sjoe	{ BSM_ERRNO_ERANGE, ERANGE, ES("Result too large") },
11875899Sjoe	{ BSM_ERRNO_ENOMSG, ENOMSG, ES("No message of desired type") },
11975899Sjoe	{ BSM_ERRNO_EIDRM, EIDRM, ES("Identifier removed") },
12075899Sjoe	{ BSM_ERRNO_ECHRNG,
12175899Sjoe#ifdef ECHRNG
12275899Sjoe	ECHRNG,
12375899Sjoe#else
12475899Sjoe	ERRNO_NO_LOCAL_MAPPING,
12575899Sjoe#endif
12675899Sjoe	ES("Channel number out of range") },
12775899Sjoe	{ BSM_ERRNO_EL2NSYNC,
12875899Sjoe#ifdef EL2NSYNC
12975899Sjoe	EL2NSYNC,
13075899Sjoe#else
13178544Sluigi	ERRNO_NO_LOCAL_MAPPING,
13275899Sjoe#endif
13375899Sjoe	ES("Level 2 not synchronized") },
13475899Sjoe	{ BSM_ERRNO_EL3HLT,
13575899Sjoe#ifdef EL3HLT
136155140Sluigi	EL3HLT,
13775899Sjoe#else
13875899Sjoe	ERRNO_NO_LOCAL_MAPPING,
13975899Sjoe#endif
14091938Sluigi	ES("Level 3 halted") },
14175899Sjoe	{ BSM_ERRNO_EL3RST,
14283086Sluigi#ifdef EL3RST
14375899Sjoe	EL3RST,
14475899Sjoe#else
14575899Sjoe	ERRNO_NO_LOCAL_MAPPING,
14675899Sjoe#endif
14775899Sjoe	ES("Level 3 reset") },
14875899Sjoe	{ BSM_ERRNO_ELNRNG,
14975899Sjoe#ifdef ELNRNG
15075899Sjoe	ELNRNG,
15175899Sjoe#else
15275899Sjoe	ERRNO_NO_LOCAL_MAPPING,
15377579Sru#endif
15475899Sjoe	ES("Link number out of range") },
15575899Sjoe	{ BSM_ERRNO_EUNATCH,
15675899Sjoe#ifdef EUNATCH
15775899Sjoe	EUNATCH,
15875899Sjoe#else
15975899Sjoe	ERRNO_NO_LOCAL_MAPPING,
16090661Sluigi#endif
16190661Sluigi	ES("Protocol driver not attached") },
16290661Sluigi	{ BSM_ERRNO_ENOCSI,
16375899Sjoe#ifdef ENOCSI
16475899Sjoe	ENOCSI,
16575899Sjoe#else
16675899Sjoe	ERRNO_NO_LOCAL_MAPPING,
16775899Sjoe#endif
168102344Sluigi	ES("No CSI structure available") },
169116688Sluigi	{ BSM_ERRNO_EL2HLT,
17075899Sjoe#ifdef EL2HLT
17175899Sjoe	EL2HLT,
17275899Sjoe#else
17384171Sluigi	ERRNO_NO_LOCAL_MAPPING,
17484171Sluigi#endif
17584171Sluigi	ES("Level 2 halted") },
176116688Sluigi	{ BSM_ERRNO_EDEADLK, EDEADLK, ES("Resource deadlock avoided") },
17775899Sjoe	{ BSM_ERRNO_ENOLCK, ENOLCK, ES("No locks available") },
178127350Sluigi	{ BSM_ERRNO_ECANCELED, ECANCELED, ES("Operation canceled") },
179127350Sluigi	{ BSM_ERRNO_ENOTSUP, ENOTSUP, ES("Operation not supported") },
18075899Sjoe	{ BSM_ERRNO_EDQUOT, EDQUOT, ES("Disc quota exceeded") },
181102344Sluigi	{ BSM_ERRNO_EBADE,
18275899Sjoe#ifdef EBADE
18395047Sru	EBADE,
184155140Sluigi#else
185	ERRNO_NO_LOCAL_MAPPING,
186#endif
187	ES("Invalid exchange") },
188	{ BSM_ERRNO_EBADR,
189#ifdef EBADR
190	EBADR,
191#else
192	ERRNO_NO_LOCAL_MAPPING,
193#endif
194	ES("Invalid request descriptor") },
195	{ BSM_ERRNO_EXFULL,
196#ifdef EXFULL
197	EXFULL,
198#else
199	ERRNO_NO_LOCAL_MAPPING,
200#endif
201	ES("Exchange full") },
202	{ BSM_ERRNO_ENOANO,
203#ifdef ENOANO
204	ENOANO,
205#else
206	ERRNO_NO_LOCAL_MAPPING,
207#endif
208	ES("No anode") },
209	{ BSM_ERRNO_EBADRQC,
210#ifdef EBADRQC
211	EBADRQC,
212#else
213	ERRNO_NO_LOCAL_MAPPING,
214#endif
215	ES("Invalid request descriptor") },
216	{ BSM_ERRNO_EBADSLT,
217#ifdef EBADSLT
218	EBADSLT,
219#else
220	ERRNO_NO_LOCAL_MAPPING,
221#endif
222	ES("Invalid slot") },
223	{ BSM_ERRNO_EDEADLOCK,
224#ifdef EDEADLOCK
225	EDEADLOCK,
226#else
227	ERRNO_NO_LOCAL_MAPPING,
228#endif
229	ES("Resource deadlock avoided") },
230	{ BSM_ERRNO_EBFONT,
231#ifdef EBFONT
232	EBFONT,
233#else
234	ERRNO_NO_LOCAL_MAPPING,
235#endif
236	ES("Bad font file format") },
237	{ BSM_ERRNO_EOWNERDEAD,
238#ifdef EOWNERDEAD
239	EOWNERDEAD,
240#else
241	ERRNO_NO_LOCAL_MAPPING,
242#endif
243	ES("Process died with the lock") },
244	{ BSM_ERRNO_ENOTRECOVERABLE,
245#ifdef ENOTRECOVERABLE
246	ENOTRECOVERABLE,
247#else
248	ERRNO_NO_LOCAL_MAPPING,
249#endif
250	ES("Lock is not recoverable") },
251	{ BSM_ERRNO_ENOSTR,
252#ifdef ENOSTR
253	ENOSTR,
254#else
255	ERRNO_NO_LOCAL_MAPPING,
256#endif
257	ES("Device not a stream") },
258	{ BSM_ERRNO_ENONET,
259#ifdef ENONET
260	ENONET,
261#else
262	ERRNO_NO_LOCAL_MAPPING,
263#endif
264	ES("Machine is not on the network") },
265	{ BSM_ERRNO_ENOPKG,
266#ifdef ENOPKG
267	ENOPKG,
268#else
269	ERRNO_NO_LOCAL_MAPPING,
270#endif
271	ES("Package not installed") },
272	{ BSM_ERRNO_EREMOTE, EREMOTE,
273	    ES("Too many levels of remote in path") },
274	{ BSM_ERRNO_ENOLINK,
275#ifdef ENOLINK
276	ENOLINK,
277#else
278	ERRNO_NO_LOCAL_MAPPING,
279#endif
280	ES("Link has been severed") },
281	{ BSM_ERRNO_EADV,
282#ifdef EADV
283	EADV,
284#else
285	ERRNO_NO_LOCAL_MAPPING,
286#endif
287	ES("Advertise error") },
288	{ BSM_ERRNO_ESRMNT,
289#ifdef ESRMNT
290	ESRMNT,
291#else
292	ERRNO_NO_LOCAL_MAPPING,
293#endif
294	ES("srmount error") },
295	{ BSM_ERRNO_ECOMM,
296#ifdef ECOMM
297	ECOMM,
298#else
299	ERRNO_NO_LOCAL_MAPPING,
300#endif
301	ES("Communication error on send") },
302	{ BSM_ERRNO_EPROTO,
303#ifdef EPROTO
304	EPROTO,
305#else
306	ERRNO_NO_LOCAL_MAPPING,
307#endif
308	ES("Protocol error") },
309	{ BSM_ERRNO_ELOCKUNMAPPED,
310#ifdef ELOCKUNMAPPED
311	ELOCKUNMAPPED,
312#else
313	ERRNO_NO_LOCAL_MAPPING,
314#endif
315	ES("Locked lock was unmapped") },
316	{ BSM_ERRNO_ENOTACTIVE,
317#ifdef ENOTACTIVE
318	ENOTACTIVE,
319#else
320	ERRNO_NO_LOCAL_MAPPING,
321#endif
322	ES("Facility is not active") },
323	{ BSM_ERRNO_EMULTIHOP,
324#ifdef EMULTIHOP
325	EMULTIHOP,
326#else
327	ERRNO_NO_LOCAL_MAPPING,
328#endif
329	ES("Multihop attempted") },
330	{ BSM_ERRNO_EBADMSG,
331#ifdef EBADMSG
332	EBADMSG,
333#else
334	ERRNO_NO_LOCAL_MAPPING,
335#endif
336	ES("Bad message") },
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,
341#ifdef ENOTUNIQ
342	ENOTUNIQ,
343#else
344	ERRNO_NO_LOCAL_MAPPING,
345#endif
346	ES("Given log name not unique") },
347	{ BSM_ERRNO_EBADFD,
348#ifdef EBADFD
349	EBADFD,
350#else
351	ERRNO_NO_LOCAL_MAPPING,
352#endif
353	ES("Given f.d. invalid for this operation") },
354	{ BSM_ERRNO_EREMCHG,
355#ifdef EREMCHG
356	EREMCHG,
357#else
358	ERRNO_NO_LOCAL_MAPPING,
359#endif
360	ES("Remote address changed") },
361	{ BSM_ERRNO_ELIBACC,
362#ifdef ELIBACC
363	ELIBACC,
364#else
365	ERRNO_NO_LOCAL_MAPPING,
366#endif
367	ES("Can't access a needed shared lib") },
368	{ BSM_ERRNO_ELIBBAD,
369#ifdef ELIBBAD
370	ELIBBAD,
371#else
372	ERRNO_NO_LOCAL_MAPPING,
373#endif
374	ES("Accessing a corrupted shared lib") },
375	{ BSM_ERRNO_ELIBSCN,
376#ifdef ELIBSCN
377	ELIBSCN,
378#else
379	ERRNO_NO_LOCAL_MAPPING,
380#endif
381	ES(".lib section in a.out corrupted") },
382	{ BSM_ERRNO_ELIBMAX,
383#ifdef ELIBMAX
384	ELIBMAX,
385#else
386	ERRNO_NO_LOCAL_MAPPING,
387#endif
388	ES("Attempting to link in too many libs") },
389	{ BSM_ERRNO_ELIBEXEC,
390#ifdef ELIBEXEC
391	ELIBEXEC,
392#else
393	ERRNO_NO_LOCAL_MAPPING,
394#endif
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,
400#ifdef ERESTART
401	ERESTART,
402#else
403	ERRNO_NO_LOCAL_MAPPING,
404#endif
405	ES("Restart syscall") },
406	{ BSM_ERRNO_ESTRPIPE,
407#ifdef ESTRPIPE
408	ESTRPIPE,
409#else
410	ERRNO_NO_LOCAL_MAPPING,
411#endif
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,
457#ifdef EPROCLIM
458	EPROCLIM,
459#else
460	ERRNO_NO_LOCAL_MAPPING,
461#endif
462	ES("Too many processes") },
463	{ BSM_ERRNO_EBADRPC,
464#ifdef EBADRPC
465	EBADRPC,
466#else
467	ERRNO_NO_LOCAL_MAPPING,
468#endif
469	ES("RPC struct is bad") },
470	{ BSM_ERRNO_ERPCMISMATCH,
471#ifdef ERPCMISMATCH
472	ERPCMISMATCH,
473#else
474	ERRNO_NO_LOCAL_MAPPING,
475#endif
476	ES("RPC version wrong") },
477	{ BSM_ERRNO_EPROGUNAVAIL,
478#ifdef EPROGUNAVAIL
479	EPROGUNAVAIL,
480#else
481	ERRNO_NO_LOCAL_MAPPING,
482#endif
483	ES("RPC prog. not avail") },
484	{ BSM_ERRNO_EPROGMISMATCH,
485#ifdef EPROGMISMATCH
486	EPROGMISMATCH,
487#else
488	ERRNO_NO_LOCAL_MAPPING,
489#endif
490	ES("RPC version wrong") },
491	{ BSM_ERRNO_EPROCUNAVAIL,
492#ifdef EPROCUNAVAIL
493	EPROCUNAVAIL,
494#else
495	ERRNO_NO_LOCAL_MAPPING,
496#endif
497	ES("Bad procedure for program") },
498	{ BSM_ERRNO_EFTYPE,
499#ifdef EFTYPE
500	EFTYPE,
501#else
502	ERRNO_NO_LOCAL_MAPPING,
503#endif
504	ES("Inappropriate file type or format") },
505	{ BSM_ERRNO_EAUTH,
506#ifdef EAUTH
507	EAUTH,
508#else
509	ERRNO_NO_LOCAL_MAPPING,
510#endif
511	ES("Authenticateion error") },
512	{ BSM_ERRNO_ENEEDAUTH,
513#ifdef ENEEDAUTH
514	ENEEDAUTH,
515#else
516	ERRNO_NO_LOCAL_MAPPING,
517#endif
518	ES("Need authenticator") },
519	{ BSM_ERRNO_ENOATTR,
520#ifdef ENOATTR
521	ENOATTR,
522#else
523	ERRNO_NO_LOCAL_MAPPING,
524#endif
525	ES("Attribute not found") },
526	{ BSM_ERRNO_EDOOFUS,
527#ifdef EDOOFUS
528	EDOOFUS,
529#else
530	ERRNO_NO_LOCAL_MAPPING,
531#endif
532	ES("Programming error") },
533	{ BSM_ERRNO_EJUSTRETURN,
534#ifdef EJUSTRETURN
535	EJUSTRETURN,
536#else
537	ERRNO_NO_LOCAL_MAPPING,
538#endif
539	ES("Just return") },
540	{ BSM_ERRNO_ENOIOCTL,
541#ifdef ENOIOCTL
542	ENOIOCTL,
543#else
544	ERRNO_NO_LOCAL_MAPPING,
545#endif
546	ES("ioctl not handled by this layer") },
547	{ BSM_ERRNO_EDIRIOCTL,
548#ifdef EDIRIOCTL
549	EDIRIOCTL,
550#else
551	ERRNO_NO_LOCAL_MAPPING,
552#endif
553	ES("do direct ioctl in GEOM") },
554	{ BSM_ERRNO_EPWROFF,
555#ifdef EPWROFF
556	EPWROFF,
557#else
558	ERRNO_NO_LOCAL_MAPPING,
559#endif
560	ES("Device power is off") },
561	{ BSM_ERRNO_EDEVERR,
562#ifdef EDEVERR
563	EDEVERR,
564#else
565	ERRNO_NO_LOCAL_MAPPING,
566#endif
567	ES("Device error") },
568	{ BSM_ERRNO_EBADEXEC,
569#ifdef EBADEXEC
570	EBADEXEC,
571#else
572	ERRNO_NO_LOCAL_MAPPING,
573#endif
574	ES("Bad executable") },
575	{ BSM_ERRNO_EBADARCH,
576#ifdef EBADARCH
577	EBADARCH,
578#else
579	ERRNO_NO_LOCAL_MAPPING,
580#endif
581	ES("Bad CPU type in executable") },
582	{ BSM_ERRNO_ESHLIBVERS,
583#ifdef ESHLIBVERS
584	ESHLIBVERS,
585#else
586	ERRNO_NO_LOCAL_MAPPING,
587#endif
588	ES("Shared library version mismatch") },
589	{ BSM_ERRNO_EBADMACHO,
590#ifdef EBADMACHO
591	EBADMACHO,
592#else
593	ERRNO_NO_LOCAL_MAPPING,
594#endif
595	ES("Malformed Macho file") },
596	{ BSM_ERRNO_EPOLICY,
597#ifdef EPOLICY
598	EPOLICY,
599#else
600	ERRNO_NO_LOCAL_MAPPING,
601#endif
602	ES("Operation failed by policy") },
603	{ BSM_ERRNO_EDOTDOT,
604#ifdef EDOTDOT
605	EDOTDOT,
606#else
607	ERRNO_NO_LOCAL_MAPPING,
608#endif
609	ES("RFS specific error") },
610	{ BSM_ERRNO_EUCLEAN,
611#ifdef EUCLEAN
612	EUCLEAN,
613#else
614	ERRNO_NO_LOCAL_MAPPING,
615#endif
616	ES("Structure needs cleaning") },
617	{ BSM_ERRNO_ENOTNAM,
618#ifdef ENOTNAM
619	ENOTNAM,
620#else
621	ERRNO_NO_LOCAL_MAPPING,
622#endif
623	ES("Not a XENIX named type file") },
624	{ BSM_ERRNO_ENAVAIL,
625#ifdef ENAVAIL
626	ENAVAIL,
627#else
628	ERRNO_NO_LOCAL_MAPPING,
629#endif
630	ES("No XENIX semaphores available") },
631	{ BSM_ERRNO_EISNAM,
632#ifdef EISNAM
633	EISNAM,
634#else
635	ERRNO_NO_LOCAL_MAPPING,
636#endif
637	ES("Is a named type file") },
638	{ BSM_ERRNO_EREMOTEIO,
639#ifdef EREMOTEIO
640	EREMOTEIO,
641#else
642	ERRNO_NO_LOCAL_MAPPING,
643#endif
644	ES("Remote I/O error") },
645	{ BSM_ERRNO_ENOMEDIUM,
646#ifdef ENOMEDIUM
647	ENOMEDIUM,
648#else
649	ERRNO_NO_LOCAL_MAPPING,
650#endif
651	ES("No medium found") },
652	{ BSM_ERRNO_EMEDIUMTYPE,
653#ifdef EMEDIUMTYPE
654	EMEDIUMTYPE,
655#else
656	ERRNO_NO_LOCAL_MAPPING,
657#endif
658	ES("Wrong medium type") },
659	{ BSM_ERRNO_ENOKEY,
660#ifdef ENOKEY
661	ENOKEY,
662#else
663	ERRNO_NO_LOCAL_MAPPING,
664#endif
665	ES("Required key not available") },
666	{ BSM_ERRNO_EKEYEXPIRED,
667#ifdef EKEEXPIRED
668	EKEYEXPIRED,
669#else
670	ERRNO_NO_LOCAL_MAPPING,
671#endif
672	ES("Key has expired") },
673	{ BSM_ERRNO_EKEYREVOKED,
674#ifdef EKEYREVOKED
675	EKEYREVOKED,
676#else
677	ERRNO_NO_LOCAL_MAPPING,
678#endif
679	ES("Key has been revoked") },
680	{ BSM_ERRNO_EKEYREJECTED,
681#ifdef EKEREJECTED
682	EKEYREJECTED,
683#else
684	ERRNO_NO_LOCAL_MAPPING,
685#endif
686	ES("Key was rejected by service") },
687};
688static const int bsm_errnos_count = sizeof(bsm_errnos) / sizeof(bsm_errnos[0]);
689
690static const struct bsm_errno *
691bsm_lookup_errno_local(int local_errno)
692{
693	int i;
694
695	for (i = 0; i < bsm_errnos_count; i++) {
696		if (bsm_errnos[i].be_local_errno == local_errno)
697			return (&bsm_errnos[i]);
698	}
699	return (NULL);
700}
701
702/*
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.
705 */
706u_char
707au_errno_to_bsm(int local_errno)
708{
709	const struct bsm_errno *bsme;
710
711	bsme = bsm_lookup_errno_local(local_errno);
712	if (bsme == NULL)
713		return (BSM_ERRNO_UNKNOWN);
714	return (bsme->be_bsm_errno);
715}
716
717static const struct bsm_errno *
718bsm_lookup_errno_bsm(u_char bsm_errno)
719{
720	int i;
721
722	for (i = 0; i < bsm_errnos_count; i++) {
723		if (bsm_errnos[i].be_bsm_errno == bsm_errno)
724			return (&bsm_errnos[i]);
725	}
726	return (NULL);
727}
728
729/*
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.
733 */
734int
735au_bsm_to_errno(u_char bsm_errno, int *errorp)
736{
737	const struct bsm_errno *bsme;
738
739	bsme = bsm_lookup_errno_bsm(bsm_errno);
740	if (bsme == NULL || bsme->be_local_errno == ERRNO_NO_LOCAL_MAPPING)
741		return (-1);
742	*errorp = bsme->be_local_errno;
743	return (0);
744}
745
746#if !defined(KERNEL) && !defined(_KERNEL)
747const char *
748au_strerror(u_char bsm_errno)
749{
750	const struct bsm_errno *bsme;
751
752	bsme = bsm_lookup_errno_bsm(bsm_errno);
753	if (bsme == NULL)
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);
758}
759#endif
760