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