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