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 *
29244390Srwatson * $P4: //depot/projects/trustedbsd/openbsm/libbsm/bsm_errno.c#22 $
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") },
456195740Srwatson	{ BSM_ERRNO_EPROCLIM,
457195740Srwatson#ifdef EPROCLIM
458195740Srwatson	EPROCLIM,
459195740Srwatson#else
460195740Srwatson	ERRNO_NO_LOCAL_MAPPING,
461195740Srwatson#endif
462195740Srwatson	ES("Too many processes") },
463195740Srwatson	{ BSM_ERRNO_EBADRPC,
464195740Srwatson#ifdef EBADRPC
465195740Srwatson	EBADRPC,
466195740Srwatson#else
467195740Srwatson	ERRNO_NO_LOCAL_MAPPING,
468195740Srwatson#endif
469195740Srwatson	ES("RPC struct is bad") },
470195740Srwatson	{ BSM_ERRNO_ERPCMISMATCH,
471195740Srwatson#ifdef ERPCMISMATCH
472195740Srwatson	ERPCMISMATCH,
473195740Srwatson#else
474195740Srwatson	ERRNO_NO_LOCAL_MAPPING,
475195740Srwatson#endif
476195740Srwatson	ES("RPC version wrong") },
477195740Srwatson	{ BSM_ERRNO_EPROGUNAVAIL,
478195740Srwatson#ifdef EPROGUNAVAIL
479195740Srwatson	EPROGUNAVAIL,
480195740Srwatson#else
481195740Srwatson	ERRNO_NO_LOCAL_MAPPING,
482195740Srwatson#endif
483195740Srwatson	ES("RPC prog. not avail") },
484195740Srwatson	{ BSM_ERRNO_EPROGMISMATCH,
485195740Srwatson#ifdef EPROGMISMATCH
486195740Srwatson	EPROGMISMATCH,
487195740Srwatson#else
488195740Srwatson	ERRNO_NO_LOCAL_MAPPING,
489195740Srwatson#endif
490195740Srwatson	ES("RPC version wrong") },
491195740Srwatson	{ BSM_ERRNO_EPROCUNAVAIL,
492195740Srwatson#ifdef EPROCUNAVAIL
493195740Srwatson	EPROCUNAVAIL,
494195740Srwatson#else
495195740Srwatson	ERRNO_NO_LOCAL_MAPPING,
496195740Srwatson#endif
497195740Srwatson	ES("Bad procedure for program") },
498195740Srwatson	{ BSM_ERRNO_EFTYPE,
499195740Srwatson#ifdef EFTYPE
500195740Srwatson	EFTYPE,
501195740Srwatson#else
502195740Srwatson	ERRNO_NO_LOCAL_MAPPING,
503195740Srwatson#endif
504195740Srwatson	ES("Inappropriate file type or format") },
505195740Srwatson	{ BSM_ERRNO_EAUTH,
506195740Srwatson#ifdef EAUTH
507195740Srwatson	EAUTH,
508195740Srwatson#else
509195740Srwatson	ERRNO_NO_LOCAL_MAPPING,
510195740Srwatson#endif
511195740Srwatson	ES("Authenticateion error") },
512195740Srwatson	{ BSM_ERRNO_ENEEDAUTH,
513195740Srwatson#ifdef ENEEDAUTH
514195740Srwatson	ENEEDAUTH,
515195740Srwatson#else
516195740Srwatson	ERRNO_NO_LOCAL_MAPPING,
517195740Srwatson#endif
518195740Srwatson	ES("Need authenticator") },
519195740Srwatson	{ BSM_ERRNO_ENOATTR,
520195740Srwatson#ifdef ENOATTR
521195740Srwatson	ENOATTR,
522195740Srwatson#else
523195740Srwatson	ERRNO_NO_LOCAL_MAPPING,
524195740Srwatson#endif
525195740Srwatson	ES("Attribute not found") },
526195740Srwatson	{ BSM_ERRNO_EDOOFUS,
527195740Srwatson#ifdef EDOOFUS
528195740Srwatson	EDOOFUS,
529195740Srwatson#else
530195740Srwatson	ERRNO_NO_LOCAL_MAPPING,
531195740Srwatson#endif
532195740Srwatson	ES("Programming error") },
533195740Srwatson	{ BSM_ERRNO_EJUSTRETURN,
534195740Srwatson#ifdef EJUSTRETURN
535195740Srwatson	EJUSTRETURN,
536195740Srwatson#else
537195740Srwatson	ERRNO_NO_LOCAL_MAPPING,
538195740Srwatson#endif
539195740Srwatson	ES("Just return") },
540195740Srwatson	{ BSM_ERRNO_ENOIOCTL,
541195740Srwatson#ifdef ENOIOCTL
542195740Srwatson	ENOIOCTL,
543195740Srwatson#else
544195740Srwatson	ERRNO_NO_LOCAL_MAPPING,
545195740Srwatson#endif
546195740Srwatson	ES("ioctl not handled by this layer") },
547195740Srwatson	{ BSM_ERRNO_EDIRIOCTL,
548195740Srwatson#ifdef EDIRIOCTL
549195740Srwatson	EDIRIOCTL,
550195740Srwatson#else
551195740Srwatson	ERRNO_NO_LOCAL_MAPPING,
552195740Srwatson#endif
553195740Srwatson	ES("do direct ioctl in GEOM") },
554187214Srwatson	{ BSM_ERRNO_EPWROFF,
555186545Srwatson#ifdef EPWROFF
556186545Srwatson	EPWROFF,
557186545Srwatson#else
558186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
559186545Srwatson#endif
560187214Srwatson	ES("Device power is off") },
561187214Srwatson	{ BSM_ERRNO_EDEVERR,
562186545Srwatson#ifdef EDEVERR
563186545Srwatson	EDEVERR,
564186545Srwatson#else
565186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
566186545Srwatson#endif
567187214Srwatson	ES("Device error") },
568187214Srwatson	{ BSM_ERRNO_EBADEXEC,
569186545Srwatson#ifdef EBADEXEC
570186545Srwatson	EBADEXEC,
571186545Srwatson#else
572186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
573186545Srwatson#endif
574187214Srwatson	ES("Bad executable") },
575187214Srwatson	{ BSM_ERRNO_EBADARCH,
576186545Srwatson#ifdef EBADARCH
577186545Srwatson	EBADARCH,
578186545Srwatson#else
579186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
580186545Srwatson#endif
581187214Srwatson	ES("Bad CPU type in executable") },
582187214Srwatson	{ BSM_ERRNO_ESHLIBVERS,
583186545Srwatson#ifdef ESHLIBVERS
584186545Srwatson	ESHLIBVERS,
585186545Srwatson#else
586186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
587186545Srwatson#endif
588187214Srwatson	ES("Shared library version mismatch") },
589187214Srwatson	{ BSM_ERRNO_EBADMACHO,
590186545Srwatson#ifdef EBADMACHO
591186545Srwatson	EBADMACHO,
592186545Srwatson#else
593186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
594186545Srwatson#endif
595189279Srwatson	ES("Malformed Macho file") },
596187214Srwatson	{ BSM_ERRNO_EPOLICY,
597186545Srwatson#ifdef EPOLICY
598186545Srwatson	EPOLICY,
599186545Srwatson#else
600186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
601186545Srwatson#endif
602187214Srwatson	ES("Operation failed by policy") },
603187214Srwatson	{ BSM_ERRNO_EDOTDOT,
604186545Srwatson#ifdef EDOTDOT
605186545Srwatson	EDOTDOT,
606186545Srwatson#else
607186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
608186545Srwatson#endif
609187214Srwatson	ES("RFS specific error") },
610187214Srwatson	{ BSM_ERRNO_EUCLEAN,
611186545Srwatson#ifdef EUCLEAN
612186545Srwatson	EUCLEAN,
613186545Srwatson#else
614186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
615186545Srwatson#endif
616187214Srwatson	ES("Structure needs cleaning") },
617187214Srwatson	{ BSM_ERRNO_ENOTNAM,
618186545Srwatson#ifdef ENOTNAM
619186545Srwatson	ENOTNAM,
620186545Srwatson#else
621186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
622186545Srwatson#endif
623187214Srwatson	ES("Not a XENIX named type file") },
624187214Srwatson	{ BSM_ERRNO_ENAVAIL,
625186545Srwatson#ifdef ENAVAIL
626186545Srwatson	ENAVAIL,
627186545Srwatson#else
628186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
629186545Srwatson#endif
630187214Srwatson	ES("No XENIX semaphores available") },
631187214Srwatson	{ BSM_ERRNO_EISNAM,
632186545Srwatson#ifdef EISNAM
633186545Srwatson	EISNAM,
634186545Srwatson#else
635186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
636186545Srwatson#endif
637187214Srwatson	ES("Is a named type file") },
638187214Srwatson	{ BSM_ERRNO_EREMOTEIO,
639186545Srwatson#ifdef EREMOTEIO
640186545Srwatson	EREMOTEIO,
641186545Srwatson#else
642186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
643186545Srwatson#endif
644187214Srwatson	ES("Remote I/O error") },
645187214Srwatson	{ BSM_ERRNO_ENOMEDIUM,
646186545Srwatson#ifdef ENOMEDIUM
647186545Srwatson	ENOMEDIUM,
648186545Srwatson#else
649186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
650186545Srwatson#endif
651187214Srwatson	ES("No medium found") },
652187214Srwatson	{ BSM_ERRNO_EMEDIUMTYPE,
653186545Srwatson#ifdef EMEDIUMTYPE
654186545Srwatson	EMEDIUMTYPE,
655186545Srwatson#else
656186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
657186545Srwatson#endif
658187214Srwatson	ES("Wrong medium type") },
659187214Srwatson	{ BSM_ERRNO_ENOKEY,
660186545Srwatson#ifdef ENOKEY
661186545Srwatson	ENOKEY,
662186545Srwatson#else
663186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
664186545Srwatson#endif
665187214Srwatson	ES("Required key not available") },
666187214Srwatson	{ BSM_ERRNO_EKEYEXPIRED,
667244390Srwatson#ifdef EKEYEXPIRED
668186545Srwatson	EKEYEXPIRED,
669186545Srwatson#else
670186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
671186545Srwatson#endif
672187214Srwatson	ES("Key has expired") },
673187214Srwatson	{ BSM_ERRNO_EKEYREVOKED,
674186545Srwatson#ifdef EKEYREVOKED
675186545Srwatson	EKEYREVOKED,
676186545Srwatson#else
677186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
678186545Srwatson#endif
679187214Srwatson	ES("Key has been revoked") },
680187214Srwatson	{ BSM_ERRNO_EKEYREJECTED,
681244390Srwatson#ifdef EKEYREJECTED
682186545Srwatson	EKEYREJECTED,
683186545Srwatson#else
684186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
685186545Srwatson#endif
686187214Srwatson	ES("Key was rejected by service") },
687244390Srwatson	{ BSM_ERRNO_ENOTCAPABLE,
688244390Srwatson#ifdef ENOTCAPABLE
689244390Srwatson	ENOTCAPABLE,
690244390Srwatson#else
691244390Srwatson	ERRNO_NO_LOCAL_MAPPING,
692244390Srwatson#endif
693244390Srwatson	ES("Capabilities insufficient") },
694244390Srwatson	{ BSM_ERRNO_ECAPMODE,
695244390Srwatson#ifdef ECAPMODE
696244390Srwatson	ECAPMODE,
697244390Srwatson#else
698244390Srwatson	ERRNO_NO_LOCAL_MAPPING,
699244390Srwatson#endif
700244390Srwatson	ES("Not permitted in capability mode") },
701186545Srwatson};
702187214Srwatsonstatic const int bsm_errnos_count = sizeof(bsm_errnos) / sizeof(bsm_errnos[0]);
703186545Srwatson
704187214Srwatsonstatic const struct bsm_errno *
705187214Srwatsonbsm_lookup_errno_local(int local_errno)
706186545Srwatson{
707186545Srwatson	int i;
708186545Srwatson
709187214Srwatson	for (i = 0; i < bsm_errnos_count; i++) {
710187214Srwatson		if (bsm_errnos[i].be_local_errno == local_errno)
711187214Srwatson			return (&bsm_errnos[i]);
712186545Srwatson	}
713186545Srwatson	return (NULL);
714186545Srwatson}
715186545Srwatson
716187214Srwatson/*
717187214Srwatson * Conversion to the BSM errno space isn't allowed to fail; we simply map to
718187214Srwatson * BSM_ERRNO_UNKNOWN and let the remote endpoint deal with it.
719187214Srwatson */
720187214Srwatsonu_char
721187214Srwatsonau_errno_to_bsm(int local_errno)
722186545Srwatson{
723187214Srwatson	const struct bsm_errno *bsme;
724187214Srwatson
725187214Srwatson	bsme = bsm_lookup_errno_local(local_errno);
726187214Srwatson	if (bsme == NULL)
727187214Srwatson		return (BSM_ERRNO_UNKNOWN);
728187214Srwatson	return (bsme->be_bsm_errno);
729187214Srwatson}
730187214Srwatson
731187214Srwatsonstatic const struct bsm_errno *
732187214Srwatsonbsm_lookup_errno_bsm(u_char bsm_errno)
733187214Srwatson{
734186545Srwatson	int i;
735186545Srwatson
736187214Srwatson	for (i = 0; i < bsm_errnos_count; i++) {
737187214Srwatson		if (bsm_errnos[i].be_bsm_errno == bsm_errno)
738187214Srwatson			return (&bsm_errnos[i]);
739186545Srwatson	}
740186545Srwatson	return (NULL);
741186545Srwatson}
742186545Srwatson
743186545Srwatson/*
744186545Srwatson * Converstion from a BSM error to a local error number may fail if either
745186545Srwatson * OpenBSM doesn't recognize the error on the wire, or because there is no
746187214Srwatson * appropriate local mapping.
747186545Srwatson */
748186545Srwatsonint
749187214Srwatsonau_bsm_to_errno(u_char bsm_errno, int *errorp)
750186545Srwatson{
751187214Srwatson	const struct bsm_errno *bsme;
752186545Srwatson
753187214Srwatson	bsme = bsm_lookup_errno_bsm(bsm_errno);
754187214Srwatson	if (bsme == NULL || bsme->be_local_errno == ERRNO_NO_LOCAL_MAPPING)
755186545Srwatson		return (-1);
756187214Srwatson	*errorp = bsme->be_local_errno;
757186545Srwatson	return (0);
758186545Srwatson}
759186545Srwatson
760186545Srwatson#if !defined(KERNEL) && !defined(_KERNEL)
761186545Srwatsonconst char *
762187214Srwatsonau_strerror(u_char bsm_errno)
763186545Srwatson{
764187214Srwatson	const struct bsm_errno *bsme;
765186545Srwatson
766187214Srwatson	bsme = bsm_lookup_errno_bsm(bsm_errno);
767186545Srwatson	if (bsme == NULL)
768186545Srwatson		return ("Unrecognized BSM error");
769187214Srwatson	if (bsme->be_local_errno != ERRNO_NO_LOCAL_MAPPING)
770187214Srwatson		return (strerror(bsme->be_local_errno));
771186545Srwatson	return (bsme->be_strerror);
772186545Srwatson}
773186545Srwatson#endif
774