xref: /freebsd/lib/libsysdecode/errno.c (revision ea825d02749f382c3f7e17f28247f20a48733eab)
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 <sys/acl.h>
32 #include <sys/wait.h>
33 #include <errno.h>
34 #include <limits.h>
35 #include <stdbool.h>
36 #include <stdio.h>
37 #include <sysdecode.h>
38 
39 #if defined(__i386__) || defined(__amd64__)
40 /*
41  * Linux syscalls return negative errno's, we do positive and map them
42  * Reference:
43  *   FreeBSD: src/sys/sys/errno.h
44  *   Linux:   linux-2.6.17.8/include/asm-generic/errno-base.h
45  *            linux-2.6.17.8/include/asm-generic/errno.h
46  */
47 static int bsd_to_linux_errno[ELAST + 1] = {
48 	-0,  -1,  -2,  -3,  -4,  -5,  -6,  -7,  -8,  -9,
49 	-10, -35, -12, -13, -14, -15, -16, -17, -18, -19,
50 	-20, -21, -22, -23, -24, -25, -26, -27, -28, -29,
51 	-30, -31, -32, -33, -34, -11,-115,-114, -88, -89,
52 	-90, -91, -92, -93, -94, -95, -96, -97, -98, -99,
53 	-100,-101,-102,-103,-104,-105,-106,-107,-108,-109,
54 	-110,-111, -40, -36,-112,-113, -39, -11, -87,-122,
55 	-116, -66,  -6,  -6,  -6,  -6,  -6, -37, -38,  -9,
56 	  -6,  -6, -43, -42, -75,-125, -84, -95, -16, -74,
57 	 -72, -67, -71
58 };
59 #endif
60 
61 #if defined(__aarch64__) || defined(__amd64__)
62 #include <contrib/cloudabi/cloudabi_types_common.h>
63 
64 static const int cloudabi_errno_table[] = {
65 	[CLOUDABI_E2BIG]		= E2BIG,
66 	[CLOUDABI_EACCES]		= EACCES,
67 	[CLOUDABI_EADDRINUSE]		= EADDRINUSE,
68 	[CLOUDABI_EADDRNOTAVAIL]	= EADDRNOTAVAIL,
69 	[CLOUDABI_EAFNOSUPPORT]		= EAFNOSUPPORT,
70 	[CLOUDABI_EAGAIN]		= EAGAIN,
71 	[CLOUDABI_EALREADY]		= EALREADY,
72 	[CLOUDABI_EBADF]		= EBADF,
73 	[CLOUDABI_EBADMSG]		= EBADMSG,
74 	[CLOUDABI_EBUSY]		= EBUSY,
75 	[CLOUDABI_ECANCELED]		= ECANCELED,
76 	[CLOUDABI_ECHILD]		= ECHILD,
77 	[CLOUDABI_ECONNABORTED]		= ECONNABORTED,
78 	[CLOUDABI_ECONNREFUSED]		= ECONNREFUSED,
79 	[CLOUDABI_ECONNRESET]		= ECONNRESET,
80 	[CLOUDABI_EDEADLK]		= EDEADLK,
81 	[CLOUDABI_EDESTADDRREQ]		= EDESTADDRREQ,
82 	[CLOUDABI_EDOM]			= EDOM,
83 	[CLOUDABI_EDQUOT]		= EDQUOT,
84 	[CLOUDABI_EEXIST]		= EEXIST,
85 	[CLOUDABI_EFAULT]		= EFAULT,
86 	[CLOUDABI_EFBIG]		= EFBIG,
87 	[CLOUDABI_EHOSTUNREACH]		= EHOSTUNREACH,
88 	[CLOUDABI_EIDRM]		= EIDRM,
89 	[CLOUDABI_EILSEQ]		= EILSEQ,
90 	[CLOUDABI_EINPROGRESS]		= EINPROGRESS,
91 	[CLOUDABI_EINTR]		= EINTR,
92 	[CLOUDABI_EINVAL]		= EINVAL,
93 	[CLOUDABI_EIO]			= EIO,
94 	[CLOUDABI_EISCONN]		= EISCONN,
95 	[CLOUDABI_EISDIR]		= EISDIR,
96 	[CLOUDABI_ELOOP]		= ELOOP,
97 	[CLOUDABI_EMFILE]		= EMFILE,
98 	[CLOUDABI_EMLINK]		= EMLINK,
99 	[CLOUDABI_EMSGSIZE]		= EMSGSIZE,
100 	[CLOUDABI_EMULTIHOP]		= EMULTIHOP,
101 	[CLOUDABI_ENAMETOOLONG]		= ENAMETOOLONG,
102 	[CLOUDABI_ENETDOWN]		= ENETDOWN,
103 	[CLOUDABI_ENETRESET]		= ENETRESET,
104 	[CLOUDABI_ENETUNREACH]		= ENETUNREACH,
105 	[CLOUDABI_ENFILE]		= ENFILE,
106 	[CLOUDABI_ENOBUFS]		= ENOBUFS,
107 	[CLOUDABI_ENODEV]		= ENODEV,
108 	[CLOUDABI_ENOENT]		= ENOENT,
109 	[CLOUDABI_ENOEXEC]		= ENOEXEC,
110 	[CLOUDABI_ENOLCK]		= ENOLCK,
111 	[CLOUDABI_ENOLINK]		= ENOLINK,
112 	[CLOUDABI_ENOMEM]		= ENOMEM,
113 	[CLOUDABI_ENOMSG]		= ENOMSG,
114 	[CLOUDABI_ENOPROTOOPT]		= ENOPROTOOPT,
115 	[CLOUDABI_ENOSPC]		= ENOSPC,
116 	[CLOUDABI_ENOSYS]		= ENOSYS,
117 	[CLOUDABI_ENOTCONN]		= ENOTCONN,
118 	[CLOUDABI_ENOTDIR]		= ENOTDIR,
119 	[CLOUDABI_ENOTEMPTY]		= ENOTEMPTY,
120 	[CLOUDABI_ENOTRECOVERABLE]	= ENOTRECOVERABLE,
121 	[CLOUDABI_ENOTSOCK]		= ENOTSOCK,
122 	[CLOUDABI_ENOTSUP]		= ENOTSUP,
123 	[CLOUDABI_ENOTTY]		= ENOTTY,
124 	[CLOUDABI_ENXIO]		= ENXIO,
125 	[CLOUDABI_EOVERFLOW]		= EOVERFLOW,
126 	[CLOUDABI_EOWNERDEAD]		= EOWNERDEAD,
127 	[CLOUDABI_EPERM]		= EPERM,
128 	[CLOUDABI_EPIPE]		= EPIPE,
129 	[CLOUDABI_EPROTO]		= EPROTO,
130 	[CLOUDABI_EPROTONOSUPPORT]	= EPROTONOSUPPORT,
131 	[CLOUDABI_EPROTOTYPE]		= EPROTOTYPE,
132 	[CLOUDABI_ERANGE]		= ERANGE,
133 	[CLOUDABI_EROFS]		= EROFS,
134 	[CLOUDABI_ESPIPE]		= ESPIPE,
135 	[CLOUDABI_ESRCH]		= ESRCH,
136 	[CLOUDABI_ESTALE]		= ESTALE,
137 	[CLOUDABI_ETIMEDOUT]		= ETIMEDOUT,
138 	[CLOUDABI_ETXTBSY]		= ETXTBSY,
139 	[CLOUDABI_EXDEV]		= EXDEV,
140 	[CLOUDABI_ENOTCAPABLE]		= ENOTCAPABLE,
141 };
142 #endif
143 
144 int
145 sysdecode_abi_to_freebsd_errno(enum sysdecode_abi abi, int error)
146 {
147 
148 	switch (abi) {
149 	case SYSDECODE_ABI_FREEBSD:
150 	case SYSDECODE_ABI_FREEBSD32:
151 		return (error);
152 #if defined(__i386__) || defined(__amd64__)
153 	case SYSDECODE_ABI_LINUX:
154 	case SYSDECODE_ABI_LINUX32: {
155 		unsigned int i;
156 
157 		/*
158 		 * This is imprecise since it returns the first
159 		 * matching errno.
160 		 */
161 		for (i = 0; i < nitems(bsd_to_linux_errno); i++) {
162 			if (error == bsd_to_linux_errno[i])
163 				return (i);
164 		}
165 		break;
166 	}
167 #endif
168 #if defined(__aarch64__) || defined(__amd64__)
169 	case SYSDECODE_ABI_CLOUDABI64:
170 		if (error >= 0 &&
171 		    (unsigned int)error < nitems(cloudabi_errno_table))
172 			return (cloudabi_errno_table[error]);
173 		break;
174 #endif
175 	default:
176 		break;
177 	}
178 	return (INT_MAX);
179 }
180 
181 int
182 sysdecode_freebsd_to_abi_errno(enum sysdecode_abi abi, int error)
183 {
184 
185 	switch (abi) {
186 	case SYSDECODE_ABI_FREEBSD:
187 	case SYSDECODE_ABI_FREEBSD32:
188 		return (error);
189 #if defined(__i386__) || defined(__amd64__)
190 	case SYSDECODE_ABI_LINUX:
191 	case SYSDECODE_ABI_LINUX32:
192 		if (error >= 0 && error <= ELAST)
193 			return (bsd_to_linux_errno[error]);
194 		break;
195 #endif
196 #if defined(__aarch64__) || defined(__amd64__)
197 	case SYSDECODE_ABI_CLOUDABI64: {
198 		unsigned int i;
199 
200 		for (i = 0; i < nitems(cloudabi_errno_table); i++) {
201 			if (error == cloudabi_errno_table[i])
202 				return (i);
203 		}
204 		break;
205 	}
206 #endif
207 	default:
208 		break;
209 	}
210 	return (INT_MAX);
211 }
212 
213