bsm_errno.c revision 189279
1186545Srwatson/*-
2186545Srwatson * Copyright (c) 2008 Apple Inc.
3186545Srwatson * All rights reserved.
4186545Srwatson *
5186545Srwatson * Redistribution and use in source and binary forms, with or without
6186545Srwatson * modification, are permitted provided that the following conditions
7186545Srwatson * are met:
8186545Srwatson * 1.  Redistributions of source code must retain the above copyright
9186545Srwatson *     notice, this list of conditions and the following disclaimer.
10186545Srwatson * 2.  Redistributions in binary form must reproduce the above copyright
11186545Srwatson *     notice, this list of conditions and the following disclaimer in the
12186545Srwatson *     documentation and/or other materials provided with the distribution.
13186545Srwatson * 3.  Neither the name of Apple Inc. ("Apple") nor the names of
14186545Srwatson *     its contributors may be used to endorse or promote products derived
15186545Srwatson *     from this software without specific prior written permission.
16186545Srwatson *
17186545Srwatson * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND
18186545Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19186545Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20186545Srwatson * ARE DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR
21186545Srwatson * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22186545Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23186545Srwatson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24186545Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25186545Srwatson * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
26186545Srwatson * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27186545Srwatson * POSSIBILITY OF SUCH DAMAGE.
28186545Srwatson *
29189279Srwatson * $P4: //depot/projects/trustedbsd/openbsm/libbsm/bsm_errno.c#17 $
30186545Srwatson */
31186545Srwatson
32186545Srwatson#include <sys/types.h>
33186545Srwatson
34186545Srwatson#include <config/config.h>
35186545Srwatson
36186545Srwatson#include <bsm/audit_errno.h>
37186545Srwatson#include <bsm/libbsm.h>
38186545Srwatson
39186545Srwatson#include <errno.h>
40186545Srwatson#include <string.h>
41186545Srwatson
42186545Srwatson/*
43186545Srwatson * Different operating systems use different numeric constants for different
44186545Srwatson * error numbers, and sometimes error numbers don't exist in more than one
45186545Srwatson * operating system.  These routines convert between BSM and local error
46186545Srwatson * number spaces, subject to the above realities.  BSM error numbers are
47186545Srwatson * stored in a single 8-bit character, so don't have a byte order.
48187214Srwatson *
49187214Srwatson * Don't include string definitions when this code is compiled into a kernel.
50186545Srwatson */
51187214Srwatsonstruct bsm_errno {
52187214Srwatson	int		 be_bsm_errno;
53187214Srwatson	int		 be_local_errno;
54187214Srwatson#if !defined(KERNEL) && !defined(_KERNEL)
55186545Srwatson	const char	*be_strerror;
56187214Srwatson#endif
57186545Srwatson};
58186545Srwatson
59186545Srwatson#define	ERRNO_NO_LOCAL_MAPPING	-600
60186545Srwatson
61187214Srwatson#if !defined(KERNEL) && !defined(_KERNEL)
62187214Srwatson#define	ES(x)	x
63187214Srwatson#else
64187214Srwatson#define	ES(x)
65187214Srwatson#endif
66187214Srwatson
67186545Srwatson/*
68186545Srwatson * Mapping table -- please maintain in numeric sorted order with respect to
69186545Srwatson * the BSM constant.  Today we do a linear lookup, but could switch to a
70186545Srwatson * binary search if it makes sense.  We only ifdef errors that aren't
71186545Srwatson * generally available, but it does make the table a lot more ugly.
72186545Srwatson *
73186545Srwatson * XXXRW: It would be nice to have a similar ordered table mapping to BSM
74186545Srwatson * constant from local constant, but the order of local constants varies by
75186545Srwatson * OS.  Really we need to build that table at compile-time but don't do that
76186545Srwatson * yet.
77186545Srwatson *
78186545Srwatson * XXXRW: We currently embed English-language error strings here, but should
79186545Srwatson * support catalogues; these are only used if the OS doesn't have an error
80186545Srwatson * string using strerror(3).
81186545Srwatson */
82187214Srwatsonstatic const struct bsm_errno bsm_errnos[] = {
83187214Srwatson	{ BSM_ERRNO_ESUCCESS, 0, ES("Success") },
84187214Srwatson	{ BSM_ERRNO_EPERM, EPERM, ES("Operation not permitted") },
85187214Srwatson	{ BSM_ERRNO_ENOENT, ENOENT, ES("No such file or directory") },
86187214Srwatson	{ BSM_ERRNO_ESRCH, ESRCH, ES("No such process") },
87187214Srwatson	{ BSM_ERRNO_EINTR, EINTR, ES("Interrupted system call") },
88187214Srwatson	{ BSM_ERRNO_EIO, EIO, ES("Input/output error") },
89187214Srwatson	{ BSM_ERRNO_ENXIO, ENXIO, ES("Device not configured") },
90187214Srwatson	{ BSM_ERRNO_E2BIG, E2BIG, ES("Argument list too long") },
91187214Srwatson	{ BSM_ERRNO_ENOEXEC, ENOEXEC, ES("Exec format error") },
92187214Srwatson	{ BSM_ERRNO_EBADF, EBADF, ES("Bad file descriptor") },
93187214Srwatson	{ BSM_ERRNO_ECHILD, ECHILD, ES("No child processes") },
94187214Srwatson	{ BSM_ERRNO_EAGAIN, EAGAIN, ES("Resource temporarily unavailable") },
95187214Srwatson	{ BSM_ERRNO_ENOMEM, ENOMEM, ES("Cannot allocate memory") },
96187214Srwatson	{ BSM_ERRNO_EACCES, EACCES, ES("Permission denied") },
97187214Srwatson	{ BSM_ERRNO_EFAULT, EFAULT, ES("Bad address") },
98187214Srwatson	{ BSM_ERRNO_ENOTBLK, ENOTBLK, ES("Block device required") },
99187214Srwatson	{ BSM_ERRNO_EBUSY, EBUSY, ES("Device busy") },
100187214Srwatson	{ BSM_ERRNO_EEXIST, EEXIST, ES("File exists") },
101187214Srwatson	{ BSM_ERRNO_EXDEV, EXDEV, ES("Cross-device link") },
102187214Srwatson	{ BSM_ERRNO_ENODEV, ENODEV, ES("Operation not supported by device") },
103187214Srwatson	{ BSM_ERRNO_ENOTDIR, ENOTDIR, ES("Not a directory") },
104187214Srwatson	{ BSM_ERRNO_EISDIR, EISDIR, ES("Is a directory") },
105187214Srwatson	{ BSM_ERRNO_EINVAL, EINVAL, ES("Invalid argument") },
106187214Srwatson	{ BSM_ERRNO_ENFILE, ENFILE, ES("Too many open files in system") },
107187214Srwatson	{ BSM_ERRNO_EMFILE, EMFILE, ES("Too many open files") },
108187214Srwatson	{ BSM_ERRNO_ENOTTY, ENOTTY, ES("Inappropriate ioctl for device") },
109187214Srwatson	{ BSM_ERRNO_ETXTBSY, ETXTBSY, ES("Text file busy") },
110187214Srwatson	{ BSM_ERRNO_EFBIG, EFBIG, ES("File too large") },
111187214Srwatson	{ BSM_ERRNO_ENOSPC, ENOSPC, ES("No space left on device") },
112187214Srwatson	{ BSM_ERRNO_ESPIPE, ESPIPE, ES("Illegal seek") },
113187214Srwatson	{ BSM_ERRNO_EROFS, EROFS, ES("Read-only file system") },
114187214Srwatson	{ BSM_ERRNO_EMLINK, EMLINK, ES("Too many links") },
115187214Srwatson	{ BSM_ERRNO_EPIPE, EPIPE, ES("Broken pipe") },
116187214Srwatson	{ BSM_ERRNO_EDOM, EDOM, ES("Numerical argument out of domain") },
117187214Srwatson	{ BSM_ERRNO_ERANGE, ERANGE, ES("Result too large") },
118187214Srwatson	{ BSM_ERRNO_ENOMSG, ENOMSG, ES("No message of desired type") },
119187214Srwatson	{ BSM_ERRNO_EIDRM, EIDRM, ES("Identifier removed") },
120187214Srwatson	{ BSM_ERRNO_ECHRNG,
121186545Srwatson#ifdef ECHRNG
122186545Srwatson	ECHRNG,
123186545Srwatson#else
124186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
125186545Srwatson#endif
126187214Srwatson	ES("Channel number out of range") },
127187214Srwatson	{ BSM_ERRNO_EL2NSYNC,
128186545Srwatson#ifdef EL2NSYNC
129186545Srwatson	EL2NSYNC,
130186545Srwatson#else
131186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
132186545Srwatson#endif
133187214Srwatson	ES("Level 2 not synchronized") },
134187214Srwatson	{ BSM_ERRNO_EL3HLT,
135186545Srwatson#ifdef EL3HLT
136186545Srwatson	EL3HLT,
137186545Srwatson#else
138186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
139186545Srwatson#endif
140187214Srwatson	ES("Level 3 halted") },
141187214Srwatson	{ BSM_ERRNO_EL3RST,
142186545Srwatson#ifdef EL3RST
143186545Srwatson	EL3RST,
144186545Srwatson#else
145186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
146186545Srwatson#endif
147187214Srwatson	ES("Level 3 reset") },
148187214Srwatson	{ BSM_ERRNO_ELNRNG,
149186545Srwatson#ifdef ELNRNG
150186545Srwatson	ELNRNG,
151186545Srwatson#else
152186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
153186545Srwatson#endif
154187214Srwatson	ES("Link number out of range") },
155187214Srwatson	{ BSM_ERRNO_EUNATCH,
156186545Srwatson#ifdef EUNATCH
157186545Srwatson	EUNATCH,
158186545Srwatson#else
159186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
160186545Srwatson#endif
161187214Srwatson	ES("Protocol driver not attached") },
162187214Srwatson	{ BSM_ERRNO_ENOCSI,
163186545Srwatson#ifdef ENOCSI
164186545Srwatson	ENOCSI,
165186545Srwatson#else
166186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
167186545Srwatson#endif
168187214Srwatson	ES("No CSI structure available") },
169187214Srwatson	{ BSM_ERRNO_EL2HLT,
170186545Srwatson#ifdef EL2HLT
171186545Srwatson	EL2HLT,
172186545Srwatson#else
173186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
174186545Srwatson#endif
175187214Srwatson	ES("Level 2 halted") },
176187214Srwatson	{ BSM_ERRNO_EDEADLK, EDEADLK, ES("Resource deadlock avoided") },
177187214Srwatson	{ BSM_ERRNO_ENOLCK, ENOLCK, ES("No locks available") },
178187214Srwatson	{ BSM_ERRNO_ECANCELED, ECANCELED, ES("Operation canceled") },
179187214Srwatson	{ BSM_ERRNO_ENOTSUP, ENOTSUP, ES("Operation not supported") },
180187214Srwatson	{ BSM_ERRNO_EDQUOT, EDQUOT, ES("Disc quota exceeded") },
181187214Srwatson	{ BSM_ERRNO_EBADE,
182186545Srwatson#ifdef EBADE
183186545Srwatson	EBADE,
184186545Srwatson#else
185186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
186186545Srwatson#endif
187187214Srwatson	ES("Invalid exchange") },
188187214Srwatson	{ BSM_ERRNO_EBADR,
189186545Srwatson#ifdef EBADR
190186545Srwatson	EBADR,
191186545Srwatson#else
192186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
193186545Srwatson#endif
194187214Srwatson	ES("Invalid request descriptor") },
195187214Srwatson	{ BSM_ERRNO_EXFULL,
196186545Srwatson#ifdef EXFULL
197186545Srwatson	EXFULL,
198186545Srwatson#else
199186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
200186545Srwatson#endif
201187214Srwatson	ES("Exchange full") },
202187214Srwatson	{ BSM_ERRNO_ENOANO,
203186545Srwatson#ifdef ENOANO
204186545Srwatson	ENOANO,
205186545Srwatson#else
206186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
207186545Srwatson#endif
208187214Srwatson	ES("No anode") },
209187214Srwatson	{ BSM_ERRNO_EBADRQC,
210186545Srwatson#ifdef EBADRQC
211186545Srwatson	EBADRQC,
212186545Srwatson#else
213186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
214186545Srwatson#endif
215187214Srwatson	ES("Invalid request descriptor") },
216187214Srwatson	{ BSM_ERRNO_EBADSLT,
217186545Srwatson#ifdef EBADSLT
218186545Srwatson	EBADSLT,
219186545Srwatson#else
220186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
221186545Srwatson#endif
222187214Srwatson	ES("Invalid slot") },
223187214Srwatson	{ BSM_ERRNO_EDEADLOCK,
224186545Srwatson#ifdef EDEADLOCK
225186545Srwatson	EDEADLOCK,
226186545Srwatson#else
227186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
228186545Srwatson#endif
229187214Srwatson	ES("Resource deadlock avoided") },
230187214Srwatson	{ BSM_ERRNO_EBFONT,
231186545Srwatson#ifdef EBFONT
232186545Srwatson	EBFONT,
233186545Srwatson#else
234186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
235186545Srwatson#endif
236187214Srwatson	ES("Bad font file format") },
237187214Srwatson	{ BSM_ERRNO_EOWNERDEAD,
238186545Srwatson#ifdef EOWNERDEAD
239186545Srwatson	EOWNERDEAD,
240186545Srwatson#else
241186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
242186545Srwatson#endif
243187214Srwatson	ES("Process died with the lock") },
244187214Srwatson	{ BSM_ERRNO_ENOTRECOVERABLE,
245186545Srwatson#ifdef ENOTRECOVERABLE
246186545Srwatson	ENOTRECOVERABLE,
247186545Srwatson#else
248186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
249186545Srwatson#endif
250187214Srwatson	ES("Lock is not recoverable") },
251187214Srwatson	{ BSM_ERRNO_ENOSTR,
252186545Srwatson#ifdef ENOSTR
253186545Srwatson	ENOSTR,
254186545Srwatson#else
255186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
256186545Srwatson#endif
257187214Srwatson	ES("Device not a stream") },
258187214Srwatson	{ BSM_ERRNO_ENONET,
259186545Srwatson#ifdef ENONET
260186545Srwatson	ENONET,
261186545Srwatson#else
262186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
263186545Srwatson#endif
264187214Srwatson	ES("Machine is not on the network") },
265187214Srwatson	{ BSM_ERRNO_ENOPKG,
266186545Srwatson#ifdef ENOPKG
267186545Srwatson	ENOPKG,
268186545Srwatson#else
269186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
270186545Srwatson#endif
271187214Srwatson	ES("Package not installed") },
272187214Srwatson	{ BSM_ERRNO_EREMOTE, EREMOTE,
273187214Srwatson	    ES("Too many levels of remote in path") },
274187214Srwatson	{ BSM_ERRNO_ENOLINK,
275186545Srwatson#ifdef ENOLINK
276186545Srwatson	ENOLINK,
277186545Srwatson#else
278186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
279186545Srwatson#endif
280187214Srwatson	ES("Link has been severed") },
281187214Srwatson	{ BSM_ERRNO_EADV,
282186545Srwatson#ifdef EADV
283186545Srwatson	EADV,
284186545Srwatson#else
285186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
286186545Srwatson#endif
287187214Srwatson	ES("Advertise error") },
288187214Srwatson	{ BSM_ERRNO_ESRMNT,
289186545Srwatson#ifdef ESRMNT
290186545Srwatson	ESRMNT,
291186545Srwatson#else
292186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
293186545Srwatson#endif
294187214Srwatson	ES("srmount error") },
295187214Srwatson	{ BSM_ERRNO_ECOMM,
296186545Srwatson#ifdef ECOMM
297186545Srwatson	ECOMM,
298186545Srwatson#else
299186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
300186545Srwatson#endif
301187214Srwatson	ES("Communication error on send") },
302187214Srwatson	{ BSM_ERRNO_EPROTO,
303186545Srwatson#ifdef EPROTO
304186545Srwatson	EPROTO,
305186545Srwatson#else
306186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
307186545Srwatson#endif
308187214Srwatson	ES("Protocol error") },
309187214Srwatson	{ BSM_ERRNO_ELOCKUNMAPPED,
310186545Srwatson#ifdef ELOCKUNMAPPED
311186545Srwatson	ELOCKUNMAPPED,
312186545Srwatson#else
313186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
314186545Srwatson#endif
315187214Srwatson	ES("Locked lock was unmapped") },
316187214Srwatson	{ BSM_ERRNO_ENOTACTIVE,
317186545Srwatson#ifdef ENOTACTIVE
318186545Srwatson	ENOTACTIVE,
319186545Srwatson#else
320186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
321186545Srwatson#endif
322187214Srwatson	ES("Facility is not active") },
323187214Srwatson	{ BSM_ERRNO_EMULTIHOP,
324186545Srwatson#ifdef EMULTIHOP
325186545Srwatson	EMULTIHOP,
326186545Srwatson#else
327186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
328186545Srwatson#endif
329187214Srwatson	ES("Multihop attempted") },
330187214Srwatson	{ BSM_ERRNO_EBADMSG,
331186545Srwatson#ifdef EBADMSG
332186545Srwatson	EBADMSG,
333186545Srwatson#else
334186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
335186545Srwatson#endif
336187214Srwatson	ES("Bad message") },
337187214Srwatson	{ BSM_ERRNO_ENAMETOOLONG, ENAMETOOLONG, ES("File name too long") },
338187214Srwatson	{ BSM_ERRNO_EOVERFLOW, EOVERFLOW,
339187214Srwatson	    ES("Value too large to be stored in data type") },
340187214Srwatson	{ BSM_ERRNO_ENOTUNIQ,
341186545Srwatson#ifdef ENOTUNIQ
342186545Srwatson	ENOTUNIQ,
343186545Srwatson#else
344186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
345186545Srwatson#endif
346187214Srwatson	ES("Given log name not unique") },
347187214Srwatson	{ BSM_ERRNO_EBADFD,
348186545Srwatson#ifdef EBADFD
349186545Srwatson	EBADFD,
350186545Srwatson#else
351186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
352186545Srwatson#endif
353187214Srwatson	ES("Given f.d. invalid for this operation") },
354187214Srwatson	{ BSM_ERRNO_EREMCHG,
355186545Srwatson#ifdef EREMCHG
356186545Srwatson	EREMCHG,
357186545Srwatson#else
358186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
359186545Srwatson#endif
360187214Srwatson	ES("Remote address changed") },
361187214Srwatson	{ BSM_ERRNO_ELIBACC,
362186545Srwatson#ifdef ELIBACC
363186545Srwatson	ELIBACC,
364186545Srwatson#else
365186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
366186545Srwatson#endif
367187214Srwatson	ES("Can't access a needed shared lib") },
368187214Srwatson	{ BSM_ERRNO_ELIBBAD,
369186545Srwatson#ifdef ELIBBAD
370186545Srwatson	ELIBBAD,
371186545Srwatson#else
372186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
373186545Srwatson#endif
374187214Srwatson	ES("Accessing a corrupted shared lib") },
375187214Srwatson	{ BSM_ERRNO_ELIBSCN,
376186545Srwatson#ifdef ELIBSCN
377186545Srwatson	ELIBSCN,
378186545Srwatson#else
379186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
380186545Srwatson#endif
381187214Srwatson	ES(".lib section in a.out corrupted") },
382187214Srwatson	{ BSM_ERRNO_ELIBMAX,
383186545Srwatson#ifdef ELIBMAX
384186545Srwatson	ELIBMAX,
385186545Srwatson#else
386186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
387186545Srwatson#endif
388187214Srwatson	ES("Attempting to link in too many libs") },
389187214Srwatson	{ BSM_ERRNO_ELIBEXEC,
390186545Srwatson#ifdef ELIBEXEC
391186545Srwatson	ELIBEXEC,
392186545Srwatson#else
393186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
394186545Srwatson#endif
395187214Srwatson	ES("Attempting to exec a shared library") },
396187214Srwatson	{ BSM_ERRNO_EILSEQ, EILSEQ, ES("Illegal byte sequence") },
397187214Srwatson	{ BSM_ERRNO_ENOSYS, ENOSYS, ES("Function not implemented") },
398187214Srwatson	{ BSM_ERRNO_ELOOP, ELOOP, ES("Too many levels of symbolic links") },
399187214Srwatson	{ BSM_ERRNO_ERESTART,
400186545Srwatson#ifdef ERESTART
401186545Srwatson	ERESTART,
402186545Srwatson#else
403186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
404186545Srwatson#endif
405187214Srwatson	ES("Restart syscall") },
406187214Srwatson	{ BSM_ERRNO_ESTRPIPE,
407186545Srwatson#ifdef ESTRPIPE
408186545Srwatson	ESTRPIPE,
409186545Srwatson#else
410186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
411186545Srwatson#endif
412187214Srwatson	ES("If pipe/FIFO, don't sleep in stream head") },
413187214Srwatson	{ BSM_ERRNO_ENOTEMPTY, ENOTEMPTY, ES("Directory not empty") },
414187214Srwatson	{ BSM_ERRNO_EUSERS, EUSERS, ES("Too many users") },
415187214Srwatson	{ BSM_ERRNO_ENOTSOCK, ENOTSOCK,
416187214Srwatson	    ES("Socket operation on non-socket") },
417187214Srwatson	{ BSM_ERRNO_EDESTADDRREQ, EDESTADDRREQ,
418187214Srwatson	    ES("Destination address required") },
419187214Srwatson	{ BSM_ERRNO_EMSGSIZE, EMSGSIZE, ES("Message too long") },
420187214Srwatson	{ BSM_ERRNO_EPROTOTYPE, EPROTOTYPE,
421187214Srwatson	    ES("Protocol wrong type for socket") },
422187214Srwatson	{ BSM_ERRNO_ENOPROTOOPT, ENOPROTOOPT, ES("Protocol not available") },
423187214Srwatson	{ BSM_ERRNO_EPROTONOSUPPORT, EPROTONOSUPPORT,
424187214Srwatson	    ES("Protocol not supported") },
425187214Srwatson	{ BSM_ERRNO_ESOCKTNOSUPPORT, ESOCKTNOSUPPORT,
426187214Srwatson	    ES("Socket type not supported") },
427187214Srwatson	{ BSM_ERRNO_EOPNOTSUPP, EOPNOTSUPP, ES("Operation not supported") },
428187214Srwatson	{ BSM_ERRNO_EPFNOSUPPORT, EPFNOSUPPORT,
429187214Srwatson	    ES("Protocol family not supported") },
430187214Srwatson	{ BSM_ERRNO_EAFNOSUPPORT, EAFNOSUPPORT,
431187214Srwatson	    ES("Address family not supported by protocol family") },
432187214Srwatson	{ BSM_ERRNO_EADDRINUSE, EADDRINUSE, ES("Address already in use") },
433187214Srwatson	{ BSM_ERRNO_EADDRNOTAVAIL, EADDRNOTAVAIL,
434187214Srwatson	    ES("Can't assign requested address") },
435187214Srwatson	{ BSM_ERRNO_ENETDOWN, ENETDOWN, ES("Network is down") },
436187214Srwatson	{ BSM_ERRNO_ENETRESET, ENETRESET,
437187214Srwatson	    ES("Network dropped connection on reset") },
438187214Srwatson	{ BSM_ERRNO_ECONNABORTED, ECONNABORTED,
439187214Srwatson	    ES("Software caused connection abort") },
440187214Srwatson	{ BSM_ERRNO_ECONNRESET, ECONNRESET, ES("Connection reset by peer") },
441187214Srwatson	{ BSM_ERRNO_ENOBUFS, ENOBUFS, ES("No buffer space available") },
442187214Srwatson	{ BSM_ERRNO_EISCONN, EISCONN, ES("Socket is already connected") },
443187214Srwatson	{ BSM_ERRNO_ENOTCONN, ENOTCONN, ES("Socket is not connected") },
444187214Srwatson	{ BSM_ERRNO_ESHUTDOWN, ESHUTDOWN,
445187214Srwatson	    ES("Can't send after socket shutdown") },
446187214Srwatson	{ BSM_ERRNO_ETOOMANYREFS, ETOOMANYREFS,
447187214Srwatson	    ES("Too many references: can't splice") },
448187214Srwatson	{ BSM_ERRNO_ETIMEDOUT, ETIMEDOUT, ES("Operation timed out") },
449187214Srwatson	{ BSM_ERRNO_ECONNREFUSED, ECONNREFUSED, ES("Connection refused") },
450187214Srwatson	{ BSM_ERRNO_EHOSTDOWN, EHOSTDOWN, ES("Host is down") },
451187214Srwatson	{ BSM_ERRNO_EHOSTUNREACH, EHOSTUNREACH, ES("No route to host") },
452187214Srwatson	{ BSM_ERRNO_EALREADY, EALREADY, ES("Operation already in progress") },
453187214Srwatson	{ BSM_ERRNO_EINPROGRESS, EINPROGRESS,
454187214Srwatson	    ES("Operation now in progress") },
455187214Srwatson	{ BSM_ERRNO_ESTALE, ESTALE, ES("Stale NFS file handle") },
456187214Srwatson	{ BSM_ERRNO_EPWROFF,
457186545Srwatson#ifdef EPWROFF
458186545Srwatson	EPWROFF,
459186545Srwatson#else
460186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
461186545Srwatson#endif
462187214Srwatson	ES("Device power is off") },
463187214Srwatson	{ BSM_ERRNO_EDEVERR,
464186545Srwatson#ifdef EDEVERR
465186545Srwatson	EDEVERR,
466186545Srwatson#else
467186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
468186545Srwatson#endif
469187214Srwatson	ES("Device error") },
470187214Srwatson	{ BSM_ERRNO_EBADEXEC,
471186545Srwatson#ifdef EBADEXEC
472186545Srwatson	EBADEXEC,
473186545Srwatson#else
474186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
475186545Srwatson#endif
476187214Srwatson	ES("Bad executable") },
477187214Srwatson	{ BSM_ERRNO_EBADARCH,
478186545Srwatson#ifdef EBADARCH
479186545Srwatson	EBADARCH,
480186545Srwatson#else
481186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
482186545Srwatson#endif
483187214Srwatson	ES("Bad CPU type in executable") },
484187214Srwatson	{ BSM_ERRNO_ESHLIBVERS,
485186545Srwatson#ifdef ESHLIBVERS
486186545Srwatson	ESHLIBVERS,
487186545Srwatson#else
488186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
489186545Srwatson#endif
490187214Srwatson	ES("Shared library version mismatch") },
491187214Srwatson	{ BSM_ERRNO_EBADMACHO,
492186545Srwatson#ifdef EBADMACHO
493186545Srwatson	EBADMACHO,
494186545Srwatson#else
495186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
496186545Srwatson#endif
497189279Srwatson	ES("Malformed Macho file") },
498187214Srwatson	{ BSM_ERRNO_EPOLICY,
499186545Srwatson#ifdef EPOLICY
500186545Srwatson	EPOLICY,
501186545Srwatson#else
502186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
503186545Srwatson#endif
504187214Srwatson	ES("Operation failed by policy") },
505187214Srwatson	{ BSM_ERRNO_EDOTDOT,
506186545Srwatson#ifdef EDOTDOT
507186545Srwatson	EDOTDOT,
508186545Srwatson#else
509186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
510186545Srwatson#endif
511187214Srwatson	ES("RFS specific error") },
512187214Srwatson	{ BSM_ERRNO_EUCLEAN,
513186545Srwatson#ifdef EUCLEAN
514186545Srwatson	EUCLEAN,
515186545Srwatson#else
516186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
517186545Srwatson#endif
518187214Srwatson	ES("Structure needs cleaning") },
519187214Srwatson	{ BSM_ERRNO_ENOTNAM,
520186545Srwatson#ifdef ENOTNAM
521186545Srwatson	ENOTNAM,
522186545Srwatson#else
523186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
524186545Srwatson#endif
525187214Srwatson	ES("Not a XENIX named type file") },
526187214Srwatson	{ BSM_ERRNO_ENAVAIL,
527186545Srwatson#ifdef ENAVAIL
528186545Srwatson	ENAVAIL,
529186545Srwatson#else
530186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
531186545Srwatson#endif
532187214Srwatson	ES("No XENIX semaphores available") },
533187214Srwatson	{ BSM_ERRNO_EISNAM,
534186545Srwatson#ifdef EISNAM
535186545Srwatson	EISNAM,
536186545Srwatson#else
537186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
538186545Srwatson#endif
539187214Srwatson	ES("Is a named type file") },
540187214Srwatson	{ BSM_ERRNO_EREMOTEIO,
541186545Srwatson#ifdef EREMOTEIO
542186545Srwatson	EREMOTEIO,
543186545Srwatson#else
544186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
545186545Srwatson#endif
546187214Srwatson	ES("Remote I/O error") },
547187214Srwatson	{ BSM_ERRNO_ENOMEDIUM,
548186545Srwatson#ifdef ENOMEDIUM
549186545Srwatson	ENOMEDIUM,
550186545Srwatson#else
551186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
552186545Srwatson#endif
553187214Srwatson	ES("No medium found") },
554187214Srwatson	{ BSM_ERRNO_EMEDIUMTYPE,
555186545Srwatson#ifdef EMEDIUMTYPE
556186545Srwatson	EMEDIUMTYPE,
557186545Srwatson#else
558186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
559186545Srwatson#endif
560187214Srwatson	ES("Wrong medium type") },
561187214Srwatson	{ BSM_ERRNO_ENOKEY,
562186545Srwatson#ifdef ENOKEY
563186545Srwatson	ENOKEY,
564186545Srwatson#else
565186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
566186545Srwatson#endif
567187214Srwatson	ES("Required key not available") },
568187214Srwatson	{ BSM_ERRNO_EKEYEXPIRED,
569186545Srwatson#ifdef EKEEXPIRED
570186545Srwatson	EKEYEXPIRED,
571186545Srwatson#else
572186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
573186545Srwatson#endif
574187214Srwatson	ES("Key has expired") },
575187214Srwatson	{ BSM_ERRNO_EKEYREVOKED,
576186545Srwatson#ifdef EKEYREVOKED
577186545Srwatson	EKEYREVOKED,
578186545Srwatson#else
579186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
580186545Srwatson#endif
581187214Srwatson	ES("Key has been revoked") },
582187214Srwatson	{ BSM_ERRNO_EKEYREJECTED,
583186545Srwatson#ifdef EKEREJECTED
584186545Srwatson	EKEYREJECTED,
585186545Srwatson#else
586186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
587186545Srwatson#endif
588187214Srwatson	ES("Key was rejected by service") },
589186545Srwatson};
590187214Srwatsonstatic const int bsm_errnos_count = sizeof(bsm_errnos) / sizeof(bsm_errnos[0]);
591186545Srwatson
592187214Srwatsonstatic const struct bsm_errno *
593187214Srwatsonbsm_lookup_errno_local(int local_errno)
594186545Srwatson{
595186545Srwatson	int i;
596186545Srwatson
597187214Srwatson	for (i = 0; i < bsm_errnos_count; i++) {
598187214Srwatson		if (bsm_errnos[i].be_local_errno == local_errno)
599187214Srwatson			return (&bsm_errnos[i]);
600186545Srwatson	}
601186545Srwatson	return (NULL);
602186545Srwatson}
603186545Srwatson
604187214Srwatson/*
605187214Srwatson * Conversion to the BSM errno space isn't allowed to fail; we simply map to
606187214Srwatson * BSM_ERRNO_UNKNOWN and let the remote endpoint deal with it.
607187214Srwatson */
608187214Srwatsonu_char
609187214Srwatsonau_errno_to_bsm(int local_errno)
610186545Srwatson{
611187214Srwatson	const struct bsm_errno *bsme;
612187214Srwatson
613187214Srwatson	bsme = bsm_lookup_errno_local(local_errno);
614187214Srwatson	if (bsme == NULL)
615187214Srwatson		return (BSM_ERRNO_UNKNOWN);
616187214Srwatson	return (bsme->be_bsm_errno);
617187214Srwatson}
618187214Srwatson
619187214Srwatsonstatic const struct bsm_errno *
620187214Srwatsonbsm_lookup_errno_bsm(u_char bsm_errno)
621187214Srwatson{
622186545Srwatson	int i;
623186545Srwatson
624187214Srwatson	for (i = 0; i < bsm_errnos_count; i++) {
625187214Srwatson		if (bsm_errnos[i].be_bsm_errno == bsm_errno)
626187214Srwatson			return (&bsm_errnos[i]);
627186545Srwatson	}
628186545Srwatson	return (NULL);
629186545Srwatson}
630186545Srwatson
631186545Srwatson/*
632186545Srwatson * Converstion from a BSM error to a local error number may fail if either
633186545Srwatson * OpenBSM doesn't recognize the error on the wire, or because there is no
634187214Srwatson * appropriate local mapping.
635186545Srwatson */
636186545Srwatsonint
637187214Srwatsonau_bsm_to_errno(u_char bsm_errno, int *errorp)
638186545Srwatson{
639187214Srwatson	const struct bsm_errno *bsme;
640186545Srwatson
641187214Srwatson	bsme = bsm_lookup_errno_bsm(bsm_errno);
642187214Srwatson	if (bsme == NULL || bsme->be_local_errno == ERRNO_NO_LOCAL_MAPPING)
643186545Srwatson		return (-1);
644187214Srwatson	*errorp = bsme->be_local_errno;
645186545Srwatson	return (0);
646186545Srwatson}
647186545Srwatson
648186545Srwatson#if !defined(KERNEL) && !defined(_KERNEL)
649186545Srwatsonconst char *
650187214Srwatsonau_strerror(u_char bsm_errno)
651186545Srwatson{
652187214Srwatson	const struct bsm_errno *bsme;
653186545Srwatson
654187214Srwatson	bsme = bsm_lookup_errno_bsm(bsm_errno);
655186545Srwatson	if (bsme == NULL)
656186545Srwatson		return ("Unrecognized BSM error");
657187214Srwatson	if (bsme->be_local_errno != ERRNO_NO_LOCAL_MAPPING)
658187214Srwatson		return (strerror(bsme->be_local_errno));
659186545Srwatson	return (bsme->be_strerror);
660186545Srwatson}
661186545Srwatson#endif
662