xref: /freebsd/lib/libsysdecode/errno.c (revision 8ef24a0d4b28fe230e20637f56869cc4148cd2ca)
1 /*-
2  * Copyright (c) 2015 John H. Baldwin <jhb@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <sys/param.h>
31 #include <errno.h>
32 #include <limits.h>
33 #include <stdio.h>
34 #include <sysdecode.h>
35 
36 #if defined(__i386__) || defined(__amd64__)
37 /*
38  * Linux syscalls return negative errno's, we do positive and map them
39  * Reference:
40  *   FreeBSD: src/sys/sys/errno.h
41  *   Linux:   linux-2.6.17.8/include/asm-generic/errno-base.h
42  *            linux-2.6.17.8/include/asm-generic/errno.h
43  */
44 static int bsd_to_linux_errno[ELAST + 1] = {
45 	-0,  -1,  -2,  -3,  -4,  -5,  -6,  -7,  -8,  -9,
46 	-10, -35, -12, -13, -14, -15, -16, -17, -18, -19,
47 	-20, -21, -22, -23, -24, -25, -26, -27, -28, -29,
48 	-30, -31, -32, -33, -34, -11,-115,-114, -88, -89,
49 	-90, -91, -92, -93, -94, -95, -96, -97, -98, -99,
50 	-100,-101,-102,-103,-104,-105,-106,-107,-108,-109,
51 	-110,-111, -40, -36,-112,-113, -39, -11, -87,-122,
52 	-116, -66,  -6,  -6,  -6,  -6,  -6, -37, -38,  -9,
53 	  -6,  -6, -43, -42, -75,-125, -84, -95, -16, -74,
54 	 -72, -67, -71
55 };
56 #endif
57 
58 #if defined(__aarch64__) || defined(__amd64__)
59 #include <contrib/cloudabi/cloudabi_types_common.h>
60 
61 static const int cloudabi_errno_table[] = {
62 	[CLOUDABI_E2BIG]		= E2BIG,
63 	[CLOUDABI_EACCES]		= EACCES,
64 	[CLOUDABI_EADDRINUSE]		= EADDRINUSE,
65 	[CLOUDABI_EADDRNOTAVAIL]	= EADDRNOTAVAIL,
66 	[CLOUDABI_EAFNOSUPPORT]		= EAFNOSUPPORT,
67 	[CLOUDABI_EAGAIN]		= EAGAIN,
68 	[CLOUDABI_EALREADY]		= EALREADY,
69 	[CLOUDABI_EBADF]		= EBADF,
70 	[CLOUDABI_EBADMSG]		= EBADMSG,
71 	[CLOUDABI_EBUSY]		= EBUSY,
72 	[CLOUDABI_ECANCELED]		= ECANCELED,
73 	[CLOUDABI_ECHILD]		= ECHILD,
74 	[CLOUDABI_ECONNABORTED]		= ECONNABORTED,
75 	[CLOUDABI_ECONNREFUSED]		= ECONNREFUSED,
76 	[CLOUDABI_ECONNRESET]		= ECONNRESET,
77 	[CLOUDABI_EDEADLK]		= EDEADLK,
78 	[CLOUDABI_EDESTADDRREQ]		= EDESTADDRREQ,
79 	[CLOUDABI_EDOM]			= EDOM,
80 	[CLOUDABI_EDQUOT]		= EDQUOT,
81 	[CLOUDABI_EEXIST]		= EEXIST,
82 	[CLOUDABI_EFAULT]		= EFAULT,
83 	[CLOUDABI_EFBIG]		= EFBIG,
84 	[CLOUDABI_EHOSTUNREACH]		= EHOSTUNREACH,
85 	[CLOUDABI_EIDRM]		= EIDRM,
86 	[CLOUDABI_EILSEQ]		= EILSEQ,
87 	[CLOUDABI_EINPROGRESS]		= EINPROGRESS,
88 	[CLOUDABI_EINTR]		= EINTR,
89 	[CLOUDABI_EINVAL]		= EINVAL,
90 	[CLOUDABI_EIO]			= EIO,
91 	[CLOUDABI_EISCONN]		= EISCONN,
92 	[CLOUDABI_EISDIR]		= EISDIR,
93 	[CLOUDABI_ELOOP]		= ELOOP,
94 	[CLOUDABI_EMFILE]		= EMFILE,
95 	[CLOUDABI_EMLINK]		= EMLINK,
96 	[CLOUDABI_EMSGSIZE]		= EMSGSIZE,
97 	[CLOUDABI_EMULTIHOP]		= EMULTIHOP,
98 	[CLOUDABI_ENAMETOOLONG]		= ENAMETOOLONG,
99 	[CLOUDABI_ENETDOWN]		= ENETDOWN,
100 	[CLOUDABI_ENETRESET]		= ENETRESET,
101 	[CLOUDABI_ENETUNREACH]		= ENETUNREACH,
102 	[CLOUDABI_ENFILE]		= ENFILE,
103 	[CLOUDABI_ENOBUFS]		= ENOBUFS,
104 	[CLOUDABI_ENODEV]		= ENODEV,
105 	[CLOUDABI_ENOENT]		= ENOENT,
106 	[CLOUDABI_ENOEXEC]		= ENOEXEC,
107 	[CLOUDABI_ENOLCK]		= ENOLCK,
108 	[CLOUDABI_ENOLINK]		= ENOLINK,
109 	[CLOUDABI_ENOMEM]		= ENOMEM,
110 	[CLOUDABI_ENOMSG]		= ENOMSG,
111 	[CLOUDABI_ENOPROTOOPT]		= ENOPROTOOPT,
112 	[CLOUDABI_ENOSPC]		= ENOSPC,
113 	[CLOUDABI_ENOSYS]		= ENOSYS,
114 	[CLOUDABI_ENOTCONN]		= ENOTCONN,
115 	[CLOUDABI_ENOTDIR]		= ENOTDIR,
116 	[CLOUDABI_ENOTEMPTY]		= ENOTEMPTY,
117 	[CLOUDABI_ENOTRECOVERABLE]	= ENOTRECOVERABLE,
118 	[CLOUDABI_ENOTSOCK]		= ENOTSOCK,
119 	[CLOUDABI_ENOTSUP]		= ENOTSUP,
120 	[CLOUDABI_ENOTTY]		= ENOTTY,
121 	[CLOUDABI_ENXIO]		= ENXIO,
122 	[CLOUDABI_EOVERFLOW]		= EOVERFLOW,
123 	[CLOUDABI_EOWNERDEAD]		= EOWNERDEAD,
124 	[CLOUDABI_EPERM]		= EPERM,
125 	[CLOUDABI_EPIPE]		= EPIPE,
126 	[CLOUDABI_EPROTO]		= EPROTO,
127 	[CLOUDABI_EPROTONOSUPPORT]	= EPROTONOSUPPORT,
128 	[CLOUDABI_EPROTOTYPE]		= EPROTOTYPE,
129 	[CLOUDABI_ERANGE]		= ERANGE,
130 	[CLOUDABI_EROFS]		= EROFS,
131 	[CLOUDABI_ESPIPE]		= ESPIPE,
132 	[CLOUDABI_ESRCH]		= ESRCH,
133 	[CLOUDABI_ESTALE]		= ESTALE,
134 	[CLOUDABI_ETIMEDOUT]		= ETIMEDOUT,
135 	[CLOUDABI_ETXTBSY]		= ETXTBSY,
136 	[CLOUDABI_EXDEV]		= EXDEV,
137 	[CLOUDABI_ENOTCAPABLE]		= ENOTCAPABLE,
138 };
139 #endif
140 
141 int
142 sysdecode_abi_to_freebsd_errno(enum sysdecode_abi abi, int error)
143 {
144 
145 	switch (abi) {
146 	case SYSDECODE_ABI_FREEBSD:
147 	case SYSDECODE_ABI_FREEBSD32:
148 		return (error);
149 #if defined(__i386__) || defined(__amd64__)
150 	case SYSDECODE_ABI_LINUX:
151 	case SYSDECODE_ABI_LINUX32: {
152 		unsigned int i;
153 
154 		/*
155 		 * This is imprecise since it returns the first
156 		 * matching errno.
157 		 */
158 		for (i = 0; i < nitems(bsd_to_linux_errno); i++) {
159 			if (error == bsd_to_linux_errno[i])
160 				return (i);
161 		}
162 		break;
163 	}
164 #endif
165 #if defined(__aarch64__) || defined(__amd64__)
166 	case SYSDECODE_ABI_CLOUDABI64:
167 		if (error >= 0 &&
168 		    (unsigned int)error < nitems(cloudabi_errno_table))
169 			return (cloudabi_errno_table[error]);
170 		break;
171 #endif
172 	default:
173 		break;
174 	}
175 	return (INT_MAX);
176 }
177 
178 int
179 sysdecode_freebsd_to_abi_errno(enum sysdecode_abi abi, int error)
180 {
181 
182 	switch (abi) {
183 	case SYSDECODE_ABI_FREEBSD:
184 	case SYSDECODE_ABI_FREEBSD32:
185 		return (error);
186 #if defined(__i386__) || defined(__amd64__)
187 	case SYSDECODE_ABI_LINUX:
188 	case SYSDECODE_ABI_LINUX32:
189 		if (error >= 0 && error <= ELAST)
190 			return (bsd_to_linux_errno[error]);
191 		break;
192 #endif
193 #if defined(__aarch64__) || defined(__amd64__)
194 	case SYSDECODE_ABI_CLOUDABI64: {
195 		unsigned int i;
196 
197 		for (i = 0; i < nitems(cloudabi_errno_table); i++) {
198 			if (error == cloudabi_errno_table[i])
199 				return (i);
200 		}
201 		break;
202 	}
203 #endif
204 	default:
205 		break;
206 	}
207 	return (INT_MAX);
208 }
209 
210