bsm_errno.c revision 186545
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 *
29186545Srwatson * $P4: //depot/projects/trustedbsd/openbsm/libbsm/bsm_errno.c#12 $
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.
48186545Srwatson */
49186545Srwatson
50186545Srwatsonstruct bsm_errors {
51186545Srwatson	int		 be_bsm_error;
52186545Srwatson	int		 be_os_error;
53186545Srwatson	const char	*be_strerror;
54186545Srwatson};
55186545Srwatson
56186545Srwatson#define	ERRNO_NO_LOCAL_MAPPING	-600
57186545Srwatson
58186545Srwatson/*
59186545Srwatson * Mapping table -- please maintain in numeric sorted order with respect to
60186545Srwatson * the BSM constant.  Today we do a linear lookup, but could switch to a
61186545Srwatson * binary search if it makes sense.  We only ifdef errors that aren't
62186545Srwatson * generally available, but it does make the table a lot more ugly.
63186545Srwatson *
64186545Srwatson * XXXRW: It would be nice to have a similar ordered table mapping to BSM
65186545Srwatson * constant from local constant, but the order of local constants varies by
66186545Srwatson * OS.  Really we need to build that table at compile-time but don't do that
67186545Srwatson * yet.
68186545Srwatson *
69186545Srwatson * XXXRW: We currently embed English-language error strings here, but should
70186545Srwatson * support catalogues; these are only used if the OS doesn't have an error
71186545Srwatson * string using strerror(3).
72186545Srwatson */
73186545Srwatsonstatic const struct bsm_errors bsm_errors[] = {
74186545Srwatson	{ BSM_ESUCCESS, 0, "Success" },
75186545Srwatson	{ BSM_EPERM, EPERM, "Operation not permitted" },
76186545Srwatson	{ BSM_ENOENT, ENOENT, "No such file or directory" },
77186545Srwatson	{ BSM_ESRCH, ESRCH, "No such process" },
78186545Srwatson	{ BSM_EINTR, EINTR, "Interrupted system call" },
79186545Srwatson	{ BSM_EIO, EIO, "Input/output error" },
80186545Srwatson	{ BSM_ENXIO, ENXIO, "Device not configured" },
81186545Srwatson	{ BSM_E2BIG, E2BIG, "Argument list too long" },
82186545Srwatson	{ BSM_ENOEXEC, ENOEXEC, "Exec format error" },
83186545Srwatson	{ BSM_EBADF, EBADF, "BAd file descriptor" },
84186545Srwatson	{ BSM_ECHILD, ECHILD, "No child processes" },
85186545Srwatson	{ BSM_EAGAIN, EAGAIN, "Resource temporarily unavailable" },
86186545Srwatson	{ BSM_ENOMEM, ENOMEM, "Cannot allocate memory" },
87186545Srwatson	{ BSM_EACCES, EACCES, "Permission denied" },
88186545Srwatson	{ BSM_EFAULT, EFAULT, "Bad address" },
89186545Srwatson	{ BSM_ENOTBLK, ENOTBLK, "Block device required" },
90186545Srwatson	{ BSM_EBUSY, EBUSY, "Device busy" },
91186545Srwatson	{ BSM_EEXIST, EEXIST, "File exists" },
92186545Srwatson	{ BSM_EXDEV, EXDEV, "Cross-device link" },
93186545Srwatson	{ BSM_ENODEV, ENODEV, "Operation not supported by device" },
94186545Srwatson	{ BSM_ENOTDIR, ENOTDIR, "Not a directory" },
95186545Srwatson	{ BSM_EISDIR, EISDIR, "Is a directory" },
96186545Srwatson	{ BSM_EINVAL, EINVAL, "Invalid argument" },
97186545Srwatson	{ BSM_ENFILE, ENFILE, "Too many open files in system" },
98186545Srwatson	{ BSM_EMFILE, EMFILE, "Too many open files" },
99186545Srwatson	{ BSM_ENOTTY, ENOTTY, "Inappropriate ioctl for device" },
100186545Srwatson	{ BSM_ETXTBSY, ETXTBSY, "Text file busy" },
101186545Srwatson	{ BSM_EFBIG, EFBIG, "File too large" },
102186545Srwatson	{ BSM_ENOSPC, ENOSPC, "No space left on device" },
103186545Srwatson	{ BSM_ESPIPE, ESPIPE, "Illegal seek" },
104186545Srwatson	{ BSM_EROFS, EROFS, "Read-only file system" },
105186545Srwatson	{ BSM_EMLINK, EMLINK, "Too many links" },
106186545Srwatson	{ BSM_EPIPE, EPIPE, "Broken pipe" },
107186545Srwatson	{ BSM_EDOM, EDOM, "Numerical argument out of domain" },
108186545Srwatson	{ BSM_ERANGE, ERANGE, "Result too large" },
109186545Srwatson	{ BSM_ENOMSG, ENOMSG, "No message of desired type" },
110186545Srwatson	{ BSM_EIDRM, EIDRM, "Identifier removed" },
111186545Srwatson	{ BSM_ECHRNG,
112186545Srwatson#ifdef ECHRNG
113186545Srwatson	ECHRNG,
114186545Srwatson#else
115186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
116186545Srwatson#endif
117186545Srwatson	"Channel number out of range" },
118186545Srwatson	{ BSM_EL2NSYNC,
119186545Srwatson#ifdef EL2NSYNC
120186545Srwatson	EL2NSYNC,
121186545Srwatson#else
122186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
123186545Srwatson#endif
124186545Srwatson	"Level 2 not synchronized" },
125186545Srwatson	{ BSM_EL3HLT,
126186545Srwatson#ifdef EL3HLT
127186545Srwatson	EL3HLT,
128186545Srwatson#else
129186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
130186545Srwatson#endif
131186545Srwatson	"Level 3 halted" },
132186545Srwatson	{ BSM_EL3RST,
133186545Srwatson#ifdef EL3RST
134186545Srwatson	EL3RST,
135186545Srwatson#else
136186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
137186545Srwatson#endif
138186545Srwatson	"Level 3 reset" },
139186545Srwatson	{ BSM_ELNRNG,
140186545Srwatson#ifdef ELNRNG
141186545Srwatson	ELNRNG,
142186545Srwatson#else
143186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
144186545Srwatson#endif
145186545Srwatson	"Link number out of range" },
146186545Srwatson	{ BSM_EUNATCH,
147186545Srwatson#ifdef EUNATCH
148186545Srwatson	EUNATCH,
149186545Srwatson#else
150186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
151186545Srwatson#endif
152186545Srwatson	"Protocol driver not attached" },
153186545Srwatson	{ BSM_ENOCSI,
154186545Srwatson#ifdef ENOCSI
155186545Srwatson	ENOCSI,
156186545Srwatson#else
157186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
158186545Srwatson#endif
159186545Srwatson	"No CSI structure available" },
160186545Srwatson	{ BSM_EL2HLT,
161186545Srwatson#ifdef EL2HLT
162186545Srwatson	EL2HLT,
163186545Srwatson#else
164186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
165186545Srwatson#endif
166186545Srwatson	"Level 2 halted" },
167186545Srwatson	{ BSM_EDEADLK, EDEADLK, "Resource deadlock avoided" },
168186545Srwatson	{ BSM_ENOLCK, ENOLCK, "No locks available" },
169186545Srwatson	{ BSM_ECANCELED, ECANCELED, "Operation canceled" },
170186545Srwatson	{ BSM_ENOTSUP, ENOTSUP, "Operation not supported" },
171186545Srwatson	{ BSM_EDQUOT, EDQUOT, "Disc quota exceeded" },
172186545Srwatson	{ BSM_EBADE,
173186545Srwatson#ifdef EBADE
174186545Srwatson	EBADE,
175186545Srwatson#else
176186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
177186545Srwatson#endif
178186545Srwatson	"Invalid exchange" },
179186545Srwatson	{ BSM_EBADR,
180186545Srwatson#ifdef EBADR
181186545Srwatson	EBADR,
182186545Srwatson#else
183186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
184186545Srwatson#endif
185186545Srwatson	"Invalid request descriptor" },
186186545Srwatson	{ BSM_EXFULL,
187186545Srwatson#ifdef EXFULL
188186545Srwatson	EXFULL,
189186545Srwatson#else
190186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
191186545Srwatson#endif
192186545Srwatson	"Exchange full" },
193186545Srwatson	{ BSM_ENOANO,
194186545Srwatson#ifdef ENOANO
195186545Srwatson	ENOANO,
196186545Srwatson#else
197186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
198186545Srwatson#endif
199186545Srwatson	"No anode" },
200186545Srwatson	{ BSM_EBADRQC,
201186545Srwatson#ifdef EBADRQC
202186545Srwatson	EBADRQC,
203186545Srwatson#else
204186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
205186545Srwatson#endif
206186545Srwatson	"Invalid request descriptor" },
207186545Srwatson	{ BSM_EBADSLT,
208186545Srwatson#ifdef EBADSLT
209186545Srwatson	EBADSLT,
210186545Srwatson#else
211186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
212186545Srwatson#endif
213186545Srwatson	"Invalid slot" },
214186545Srwatson	{ BSM_EDEADLOCK,
215186545Srwatson#ifdef EDEADLOCK
216186545Srwatson	EDEADLOCK,
217186545Srwatson#else
218186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
219186545Srwatson#endif
220186545Srwatson	"Resource deadlock avoided" },
221186545Srwatson	{ BSM_EBFONT,
222186545Srwatson#ifdef EBFONT
223186545Srwatson	EBFONT,
224186545Srwatson#else
225186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
226186545Srwatson#endif
227186545Srwatson	"Bad font file format" },
228186545Srwatson	{ BSM_EOWNERDEAD,
229186545Srwatson#ifdef EOWNERDEAD
230186545Srwatson	EOWNERDEAD,
231186545Srwatson#else
232186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
233186545Srwatson#endif
234186545Srwatson	"Process died with the lock" },
235186545Srwatson	{ BSM_ENOTRECOVERABLE,
236186545Srwatson#ifdef ENOTRECOVERABLE
237186545Srwatson	ENOTRECOVERABLE,
238186545Srwatson#else
239186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
240186545Srwatson#endif
241186545Srwatson	"Lock is not recoverable" },
242186545Srwatson	{ BSM_ENOSTR,
243186545Srwatson#ifdef ENOSTR
244186545Srwatson	ENOSTR,
245186545Srwatson#else
246186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
247186545Srwatson#endif
248186545Srwatson	"Device not a stream" },
249186545Srwatson	{ BSM_ENONET,
250186545Srwatson#ifdef ENONET
251186545Srwatson	ENONET,
252186545Srwatson#else
253186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
254186545Srwatson#endif
255186545Srwatson	"Machine is not on the network" },
256186545Srwatson	{ BSM_ENOPKG,
257186545Srwatson#ifdef ENOPKG
258186545Srwatson	ENOPKG,
259186545Srwatson#else
260186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
261186545Srwatson#endif
262186545Srwatson	"Package not installed" },
263186545Srwatson	{ BSM_EREMOTE, EREMOTE, "Too many levels of remote in path" },
264186545Srwatson	{ BSM_ENOLINK,
265186545Srwatson#ifdef ENOLINK
266186545Srwatson	ENOLINK,
267186545Srwatson#else
268186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
269186545Srwatson#endif
270186545Srwatson	"Link has been severed" },
271186545Srwatson	{ BSM_EADV,
272186545Srwatson#ifdef EADV
273186545Srwatson	EADV,
274186545Srwatson#else
275186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
276186545Srwatson#endif
277186545Srwatson	"Advertise error" },
278186545Srwatson	{ BSM_ESRMNT,
279186545Srwatson#ifdef ESRMNT
280186545Srwatson	ESRMNT,
281186545Srwatson#else
282186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
283186545Srwatson#endif
284186545Srwatson	"srmount error" },
285186545Srwatson	{ BSM_ECOMM,
286186545Srwatson#ifdef ECOMM
287186545Srwatson	ECOMM,
288186545Srwatson#else
289186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
290186545Srwatson#endif
291186545Srwatson	"Communication error on send" },
292186545Srwatson	{ BSM_EPROTO,
293186545Srwatson#ifdef EPROTO
294186545Srwatson	EPROTO,
295186545Srwatson#else
296186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
297186545Srwatson#endif
298186545Srwatson	"Protocol error" },
299186545Srwatson	{ BSM_ELOCKUNMAPPED,
300186545Srwatson#ifdef ELOCKUNMAPPED
301186545Srwatson	ELOCKUNMAPPED,
302186545Srwatson#else
303186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
304186545Srwatson#endif
305186545Srwatson	"Locked lock was unmapped" },
306186545Srwatson	{ BSM_ENOTACTIVE,
307186545Srwatson#ifdef ENOTACTIVE
308186545Srwatson	ENOTACTIVE,
309186545Srwatson#else
310186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
311186545Srwatson#endif
312186545Srwatson	"Facility is not active" },
313186545Srwatson	{ BSM_EMULTIHOP,
314186545Srwatson#ifdef EMULTIHOP
315186545Srwatson	EMULTIHOP,
316186545Srwatson#else
317186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
318186545Srwatson#endif
319186545Srwatson	"Multihop attempted" },
320186545Srwatson	{ BSM_EBADMSG,
321186545Srwatson#ifdef EBADMSG
322186545Srwatson	EBADMSG,
323186545Srwatson#else
324186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
325186545Srwatson#endif
326186545Srwatson	"Bad message" },
327186545Srwatson	{ BSM_ENAMETOOLONG, ENAMETOOLONG, "File name too long" },
328186545Srwatson	{ BSM_EOVERFLOW, EOVERFLOW, "Value too large to be stored in data type" },
329186545Srwatson	{ BSM_ENOTUNIQ,
330186545Srwatson#ifdef ENOTUNIQ
331186545Srwatson	ENOTUNIQ,
332186545Srwatson#else
333186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
334186545Srwatson#endif
335186545Srwatson	"Given log name not unique" },
336186545Srwatson	{ BSM_EBADFD,
337186545Srwatson#ifdef EBADFD
338186545Srwatson	EBADFD,
339186545Srwatson#else
340186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
341186545Srwatson#endif
342186545Srwatson	"Given f.d. invalid for this operation" },
343186545Srwatson	{ BSM_EREMCHG,
344186545Srwatson#ifdef EREMCHG
345186545Srwatson	EREMCHG,
346186545Srwatson#else
347186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
348186545Srwatson#endif
349186545Srwatson	"Remote address changed" },
350186545Srwatson	{ BSM_ELIBACC,
351186545Srwatson#ifdef ELIBACC
352186545Srwatson	ELIBACC,
353186545Srwatson#else
354186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
355186545Srwatson#endif
356186545Srwatson	"Can't access a needed shared lib" },
357186545Srwatson	{ BSM_ELIBBAD,
358186545Srwatson#ifdef ELIBBAD
359186545Srwatson	ELIBBAD,
360186545Srwatson#else
361186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
362186545Srwatson#endif
363186545Srwatson	"Accessing a corrupted shared lib" },
364186545Srwatson	{ BSM_ELIBSCN,
365186545Srwatson#ifdef ELIBSCN
366186545Srwatson	ELIBSCN,
367186545Srwatson#else
368186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
369186545Srwatson#endif
370186545Srwatson	".lib section in a.out corrupted" },
371186545Srwatson	{ BSM_ELIBMAX,
372186545Srwatson#ifdef ELIBMAX
373186545Srwatson	ELIBMAX,
374186545Srwatson#else
375186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
376186545Srwatson#endif
377186545Srwatson	"Attempting to link in too many libs" },
378186545Srwatson	{ BSM_ELIBEXEC,
379186545Srwatson#ifdef ELIBEXEC
380186545Srwatson	ELIBEXEC,
381186545Srwatson#else
382186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
383186545Srwatson#endif
384186545Srwatson	"Attempting to exec a shared library" },
385186545Srwatson	{ BSM_EILSEQ, EILSEQ, "Illegal byte sequence" },
386186545Srwatson	{ BSM_ENOSYS, ENOSYS, "Function not implemented" },
387186545Srwatson	{ BSM_ELOOP, ELOOP, "Too many levels of symbolic links" },
388186545Srwatson	{ BSM_ERESTART,
389186545Srwatson#ifdef ERESTART
390186545Srwatson	ERESTART,
391186545Srwatson#else
392186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
393186545Srwatson#endif
394186545Srwatson	"Restart syscall" },
395186545Srwatson	{ BSM_ESTRPIPE,
396186545Srwatson#ifdef ESTRPIPE
397186545Srwatson	ESTRPIPE,
398186545Srwatson#else
399186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
400186545Srwatson#endif
401186545Srwatson	"If pipe/FIFO, don't sleep in stream head" },
402186545Srwatson	{ BSM_ENOTEMPTY, ENOTEMPTY, "Directory not empty" },
403186545Srwatson	{ BSM_EUSERS, EUSERS, "Too many users" },
404186545Srwatson	{ BSM_ENOTSOCK, ENOTSOCK, "Socket operation on non-socket" },
405186545Srwatson	{ BSM_EDESTADDRREQ, EDESTADDRREQ, "Destination address required" },
406186545Srwatson	{ BSM_EMSGSIZE, EMSGSIZE, "Message too long" },
407186545Srwatson	{ BSM_EPROTOTYPE, EPROTOTYPE, "Protocol wrong type for socket" },
408186545Srwatson	{ BSM_ENOPROTOOPT, ENOPROTOOPT, "Protocol not available" },
409186545Srwatson	{ BSM_EPROTONOSUPPORT, EPROTONOSUPPORT, "Protocol not supported" },
410186545Srwatson	{ BSM_ESOCKTNOSUPPORT, ESOCKTNOSUPPORT, "Socket type not supported" },
411186545Srwatson	{ BSM_EOPNOTSUPP, EOPNOTSUPP, "Operation not supported" },
412186545Srwatson	{ BSM_EPFNOSUPPORT, EPFNOSUPPORT, "Protocol family not supported" },
413186545Srwatson	{ BSM_EAFNOSUPPORT, EAFNOSUPPORT, "Address family not supported by protocol family" },
414186545Srwatson	{ BSM_EADDRINUSE, EADDRINUSE, "Address already in use" },
415186545Srwatson	{ BSM_EADDRNOTAVAIL, EADDRNOTAVAIL, "Can't assign requested address" },
416186545Srwatson	{ BSM_ENETDOWN, ENETDOWN, "Network is down" },
417186545Srwatson	{ BSM_ENETRESET, ENETRESET, "Network dropped connection on reset" },
418186545Srwatson	{ BSM_ECONNABORTED, ECONNABORTED, "Software caused connection abort" },
419186545Srwatson	{ BSM_ECONNRESET, ECONNRESET, "Connection reset by peer" },
420186545Srwatson	{ BSM_ENOBUFS, ENOBUFS, "No buffer space available" },
421186545Srwatson	{ BSM_EISCONN, EISCONN, "Socket is already connected" },
422186545Srwatson	{ BSM_ENOTCONN, ENOTCONN, "Socket is not connected" },
423186545Srwatson	{ BSM_ESHUTDOWN, ESHUTDOWN, "Can't send after socket shutdown" },
424186545Srwatson	{ BSM_ETOOMANYREFS, ETOOMANYREFS, "Too many references: can't splice" },
425186545Srwatson	{ BSM_ETIMEDOUT, ETIMEDOUT, "Operation timed out" },
426186545Srwatson	{ BSM_ECONNREFUSED, ECONNREFUSED, "Connection refused" },
427186545Srwatson	{ BSM_EHOSTDOWN, EHOSTDOWN, "Host is down" },
428186545Srwatson	{ BSM_EHOSTUNREACH, EHOSTUNREACH, "No route to host" },
429186545Srwatson	{ BSM_EALREADY, EALREADY, "Operation already in progress" },
430186545Srwatson	{ BSM_EINPROGRESS, EINPROGRESS, "Operation now in progress" },
431186545Srwatson	{ BSM_ESTALE, ESTALE, "Stale NFS file handle" },
432186545Srwatson	{ BSM_EPWROFF,
433186545Srwatson#ifdef EPWROFF
434186545Srwatson	EPWROFF,
435186545Srwatson#else
436186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
437186545Srwatson#endif
438186545Srwatson	"Device power is off" },
439186545Srwatson	{ BSM_EDEVERR,
440186545Srwatson#ifdef EDEVERR
441186545Srwatson	EDEVERR,
442186545Srwatson#else
443186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
444186545Srwatson#endif
445186545Srwatson	"Device error" },
446186545Srwatson	{ BSM_EBADEXEC,
447186545Srwatson#ifdef EBADEXEC
448186545Srwatson	EBADEXEC,
449186545Srwatson#else
450186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
451186545Srwatson#endif
452186545Srwatson	"Bad executable" },
453186545Srwatson	{ BSM_EBADARCH,
454186545Srwatson#ifdef EBADARCH
455186545Srwatson	EBADARCH,
456186545Srwatson#else
457186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
458186545Srwatson#endif
459186545Srwatson	"Bad CPU type in executable" },
460186545Srwatson	{ BSM_ESHLIBVERS,
461186545Srwatson#ifdef ESHLIBVERS
462186545Srwatson	ESHLIBVERS,
463186545Srwatson#else
464186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
465186545Srwatson#endif
466186545Srwatson	"Shared library version mismatch" },
467186545Srwatson	{ BSM_EBADMACHO,
468186545Srwatson#ifdef EBADMACHO
469186545Srwatson	EBADMACHO,
470186545Srwatson#else
471186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
472186545Srwatson#endif
473186545Srwatson	"Malfored Macho file" },
474186545Srwatson	{ BSM_EPOLICY,
475186545Srwatson#ifdef EPOLICY
476186545Srwatson	EPOLICY,
477186545Srwatson#else
478186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
479186545Srwatson#endif
480186545Srwatson	"Operation failed by policy" },
481186545Srwatson	{ BSM_EDOTDOT,
482186545Srwatson#ifdef EDOTDOT
483186545Srwatson	EDOTDOT,
484186545Srwatson#else
485186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
486186545Srwatson#endif
487186545Srwatson	"RFS specific error" },
488186545Srwatson	{ BSM_EUCLEAN,
489186545Srwatson#ifdef EUCLEAN
490186545Srwatson	EUCLEAN,
491186545Srwatson#else
492186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
493186545Srwatson#endif
494186545Srwatson	"Structure needs cleaning" },
495186545Srwatson	{ BSM_ENOTNAM,
496186545Srwatson#ifdef ENOTNAM
497186545Srwatson	ENOTNAM,
498186545Srwatson#else
499186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
500186545Srwatson#endif
501186545Srwatson	"Not a XENIX named type file" },
502186545Srwatson	{ BSM_ENAVAIL,
503186545Srwatson#ifdef ENAVAIL
504186545Srwatson	ENAVAIL,
505186545Srwatson#else
506186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
507186545Srwatson#endif
508186545Srwatson	"No XENIX semaphores available" },
509186545Srwatson	{ BSM_EISNAM,
510186545Srwatson#ifdef EISNAM
511186545Srwatson	EISNAM,
512186545Srwatson#else
513186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
514186545Srwatson#endif
515186545Srwatson	"Is a named type file" },
516186545Srwatson	{ BSM_EREMOTEIO,
517186545Srwatson#ifdef EREMOTEIO
518186545Srwatson	EREMOTEIO,
519186545Srwatson#else
520186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
521186545Srwatson#endif
522186545Srwatson	"Remote I/O error" },
523186545Srwatson	{ BSM_ENOMEDIUM,
524186545Srwatson#ifdef ENOMEDIUM
525186545Srwatson	ENOMEDIUM,
526186545Srwatson#else
527186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
528186545Srwatson#endif
529186545Srwatson	"No medium found" },
530186545Srwatson	{ BSM_EMEDIUMTYPE,
531186545Srwatson#ifdef EMEDIUMTYPE
532186545Srwatson	EMEDIUMTYPE,
533186545Srwatson#else
534186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
535186545Srwatson#endif
536186545Srwatson	"Wrong medium type" },
537186545Srwatson	{ BSM_ENOKEY,
538186545Srwatson#ifdef ENOKEY
539186545Srwatson	ENOKEY,
540186545Srwatson#else
541186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
542186545Srwatson#endif
543186545Srwatson	"Required key not available" },
544186545Srwatson	{ BSM_EKEYEXPIRED,
545186545Srwatson#ifdef EKEEXPIRED
546186545Srwatson	EKEYEXPIRED,
547186545Srwatson#else
548186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
549186545Srwatson#endif
550186545Srwatson	"Key has expired" },
551186545Srwatson	{ BSM_EKEYREVOKED,
552186545Srwatson#ifdef EKEYREVOKED
553186545Srwatson	EKEYREVOKED,
554186545Srwatson#else
555186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
556186545Srwatson#endif
557186545Srwatson	"Key has been revoked" },
558186545Srwatson	{ BSM_EKEYREJECTED,
559186545Srwatson#ifdef EKEREJECTED
560186545Srwatson	EKEYREJECTED,
561186545Srwatson#else
562186545Srwatson	ERRNO_NO_LOCAL_MAPPING,
563186545Srwatson#endif
564186545Srwatson	"Key was rejected by service" },
565186545Srwatson};
566186545Srwatsonstatic const int bsm_errors_count = sizeof(bsm_errors) / sizeof(bsm_errors[0]);
567186545Srwatson
568186545Srwatsonstatic const struct bsm_errors *
569186545Srwatsonau_bsm_error_lookup_errno(int error)
570186545Srwatson{
571186545Srwatson	int i;
572186545Srwatson
573186545Srwatson	if (error == ERRNO_NO_LOCAL_MAPPING)
574186545Srwatson		return (NULL);
575186545Srwatson	for (i = 0; i < bsm_errors_count; i++) {
576186545Srwatson		if (bsm_errors[i].be_os_error == error)
577186545Srwatson			return (&bsm_errors[i]);
578186545Srwatson	}
579186545Srwatson	return (NULL);
580186545Srwatson}
581186545Srwatson
582186545Srwatsonstatic const struct bsm_errors *
583186545Srwatsonau_bsm_error_lookup_bsm(u_char bsm_error)
584186545Srwatson{
585186545Srwatson	int i;
586186545Srwatson
587186545Srwatson	for (i = 0; i < bsm_errors_count; i++) {
588186545Srwatson		if (bsm_errors[i].be_bsm_error == bsm_error)
589186545Srwatson			return (&bsm_errors[i]);
590186545Srwatson	}
591186545Srwatson	return (NULL);
592186545Srwatson}
593186545Srwatson
594186545Srwatson/*
595186545Srwatson * Converstion from a BSM error to a local error number may fail if either
596186545Srwatson * OpenBSM doesn't recognize the error on the wire, or because there is no
597186545Srwatson * appropriate local mapping.  However, we don't allow conversion to BSM to
598186545Srwatson * fail, we just convert to BSM_UKNOWNERR.
599186545Srwatson */
600186545Srwatsonint
601186545Srwatsonau_bsm_to_errno(u_char bsm_error, int *errorp)
602186545Srwatson{
603186545Srwatson	const struct bsm_errors *bsme;
604186545Srwatson
605186545Srwatson	bsme = au_bsm_error_lookup_bsm(bsm_error);
606186545Srwatson	if (bsme == NULL || bsme->be_os_error == ERRNO_NO_LOCAL_MAPPING)
607186545Srwatson		return (-1);
608186545Srwatson	*errorp = bsme->be_os_error;
609186545Srwatson	return (0);
610186545Srwatson}
611186545Srwatson
612186545Srwatsonu_char
613186545Srwatsonau_errno_to_bsm(int error)
614186545Srwatson{
615186545Srwatson	const struct bsm_errors *bsme;
616186545Srwatson
617186545Srwatson	/*
618186545Srwatson	 * We should never be passed this libbsm-internal constant, and
619186545Srwatson	 * because it is ambiguous we just return an error.
620186545Srwatson	 */
621186545Srwatson	if (error == ERRNO_NO_LOCAL_MAPPING)
622186545Srwatson		return (BSM_UNKNOWNERR);
623186545Srwatson	bsme = au_bsm_error_lookup_errno(error);
624186545Srwatson	if (bsme == NULL)
625186545Srwatson		return (BSM_UNKNOWNERR);
626186545Srwatson	return (bsme->be_bsm_error);
627186545Srwatson}
628186545Srwatson
629186545Srwatson#if !defined(KERNEL) && !defined(_KERNEL)
630186545Srwatsonconst char *
631186545Srwatsonau_strerror(u_char bsm_error)
632186545Srwatson{
633186545Srwatson	const struct bsm_errors *bsme;
634186545Srwatson
635186545Srwatson	bsme = au_bsm_error_lookup_bsm(bsm_error);
636186545Srwatson	if (bsme == NULL)
637186545Srwatson		return ("Unrecognized BSM error");
638186545Srwatson	if (bsme->be_os_error != ERRNO_NO_LOCAL_MAPPING)
639186545Srwatson		return (strerror(bsme->be_os_error));
640186545Srwatson	return (bsme->be_strerror);
641186545Srwatson}
642186545Srwatson#endif
643