1 : /*
2 : * Copyright 2008 The Native Client Authors. All rights reserved.
3 : * Use of this source code is governed by a BSD-style license that can
4 : * be found in the LICENSE file.
5 : */
6 :
7 : /*
8 : * NaCl Service Runtime. I/O Descriptor / Handle abstraction. Memory
9 : * mapping using descriptors.
10 : */
11 : #include <errno.h>
12 :
13 : #include "native_client/src/include/nacl_platform.h"
14 : #include "native_client/src/include/portability.h"
15 :
16 : #include "native_client/src/shared/platform/nacl_host_desc.h"
17 : #include "native_client/src/shared/platform/nacl_log.h"
18 :
19 : #include "native_client/src/trusted/service_runtime/include/bits/mman.h"
20 : #include "native_client/src/trusted/service_runtime/include/sys/errno.h"
21 :
22 : /*
23 : * If you are using this as a kernel-style return, remember that you
24 : * should negate its return value.
25 : */
26 6 : int NaClXlateErrno(int errnum) {
27 6 : switch (errnum) {
28 : /*
29 : * Unfortunately a macro cannot expand to contain #ifdef....
30 : *
31 : * TODO(bsy): All host-OS conditional errnos should map into a
32 : * generic errno.
33 : */
34 : #define MAP(E) case E: do { return NACL_ABI_ ## E; } while (0)
35 0 : MAP(EPERM);
36 4 : MAP(ENOENT);
37 0 : MAP(ESRCH);
38 0 : MAP(EINTR);
39 0 : MAP(EIO);
40 0 : MAP(ENXIO);
41 0 : MAP(E2BIG);
42 0 : MAP(ENOEXEC);
43 0 : MAP(EBADF);
44 0 : MAP(ECHILD);
45 0 : MAP(EAGAIN);
46 0 : MAP(ENOMEM);
47 0 : MAP(EACCES);
48 0 : MAP(EFAULT);
49 0 : MAP(EBUSY);
50 0 : MAP(EEXIST);
51 0 : MAP(EXDEV);
52 0 : MAP(ENODEV);
53 0 : MAP(ENOTDIR);
54 0 : MAP(EISDIR);
55 2 : MAP(EINVAL);
56 0 : MAP(ENFILE);
57 0 : MAP(EMFILE);
58 0 : MAP(ENOTTY);
59 0 : MAP(EFBIG);
60 0 : MAP(ENOSPC);
61 0 : MAP(ESPIPE);
62 0 : MAP(EROFS);
63 0 : MAP(EMLINK);
64 0 : MAP(EPIPE);
65 0 : MAP(ENAMETOOLONG);
66 0 : MAP(ENOSYS);
67 : #ifdef EDQUOT
68 0 : MAP(EDQUOT);
69 : #endif
70 0 : MAP(EDOM);
71 0 : MAP(ERANGE);
72 : #ifdef ENOMSG
73 0 : MAP(ENOMSG);
74 : #endif
75 : #ifdef ECHRNG
76 : MAP(ECHRNG);
77 : #endif
78 : #ifdef EL3HLT
79 : MAP(EL3HLT); /* not in osx */
80 : #endif
81 : #ifdef EL3RST
82 : MAP(EL3RST); /* not in osx */
83 : #endif
84 : #ifdef EL3RNG
85 : MAP(ELNRNG); /* not in osx */
86 : #endif
87 : #ifdef EUNATCH
88 : MAP(EUNATCH);
89 : #endif
90 : #ifdef ENOCSI
91 : MAP(ENOCSI);
92 : #endif
93 : #ifdef EL2HLT
94 : MAP(EL2HLT);
95 : #endif
96 0 : MAP(EDEADLK);
97 0 : MAP(ENOLCK);
98 : #ifdef EBADE
99 : MAP(EBADE);
100 : #endif
101 : #ifdef EBADR
102 : MAP(EBADR);
103 : #endif
104 : #ifdef EXFULL
105 : MAP(EXFULL);
106 : #endif
107 : #ifdef ENOANO
108 : MAP(ENOANO);
109 : #endif
110 : #ifdef EBADRQC
111 : MAP(EBADRQC);
112 : #endif
113 : #ifdef EBADSLT
114 : MAP(EBADSLT);
115 : #endif
116 : #if defined(EDEADLOCK) && EDEADLK != EDEADLOCK
117 : MAP(EDEADLOCK);
118 : #endif
119 : #ifdef EBFONT
120 : MAP(EBFONT);
121 : #endif
122 : #ifdef ENOSTR
123 0 : MAP(ENOSTR);
124 : #endif
125 : #ifdef ENODATA
126 0 : MAP(ENODATA);
127 : #endif
128 : #ifdef ETIME
129 0 : MAP(ETIME);
130 : #endif
131 : #ifdef ENOSR
132 0 : MAP(ENOSR);
133 : #endif
134 : #ifdef ENONET
135 : MAP(ENONET);
136 : #endif
137 : #ifdef ENOPKG
138 : MAP(ENOPKG);
139 : #endif
140 : #ifdef EREMOTE
141 0 : MAP(EREMOTE);
142 : #endif
143 : #ifdef ENOLINK
144 0 : MAP(ENOLINK);
145 : #endif
146 : #ifdef EADV
147 : MAP(EADV);
148 : #endif
149 : #ifdef ESRMNT
150 : MAP(ESRMNT);
151 : #endif
152 : #ifdef ECOMM
153 : MAP(ECOMM);
154 : #endif
155 : #ifdef EPROTO
156 0 : MAP(EPROTO);
157 : #endif
158 : #ifdef EMULTIHOP
159 0 : MAP(EMULTIHOP);
160 : #endif
161 : #ifdef ELBIN
162 : MAP(ELBIN); /* newlib only? */
163 : #endif
164 : #ifdef EDOTDOT
165 : MAP(EDOTDOT);
166 : #endif
167 : #ifdef EBADMSG
168 0 : MAP(EBADMSG);
169 : #endif
170 : #ifdef EFTYPE
171 0 : MAP(EFTYPE); /* osx has it; linux doesn't */
172 : #endif
173 : #ifdef ENOTUNIQ
174 : MAP(ENOTUNIQ);
175 : #endif
176 : #ifdef EBADFD
177 : MAP(EBADFD);
178 : #endif
179 : #ifdef EREMCHG
180 : MAP(EREMCHG);
181 : #endif
182 : #ifdef ELIBACC
183 : MAP(ELIBACC);
184 : #endif
185 : #ifdef ELIBBAD
186 : MAP(ELIBBAD);
187 : #endif
188 : #ifdef ELIBSCN
189 : MAP(ELIBSCN);
190 : #endif
191 : #ifdef ELIBMAX
192 : MAP(ELIBMAX);
193 : #endif
194 : #ifdef ELIBEXEC
195 : MAP(ELIBEXEC);
196 : #endif
197 : #ifdef ENMFILE
198 : MAP(ENMFILE); /* newlib only? */
199 : #endif
200 0 : MAP(ENOTEMPTY);
201 : #ifdef ELOOP
202 0 : MAP(ELOOP);
203 : #endif
204 : #ifdef EOPNOTSUPP
205 0 : MAP(EOPNOTSUPP);
206 : #endif
207 : #ifdef EPFNOSUPPORT
208 0 : MAP(EPFNOSUPPORT);
209 : #endif
210 : #ifdef ECONNRESET
211 0 : MAP(ECONNRESET);
212 : #endif
213 : #ifdef ENOBUFS
214 0 : MAP(ENOBUFS);
215 : #endif
216 : #ifdef EAFNOSUPPORT
217 0 : MAP(EAFNOSUPPORT);
218 : #endif
219 : #ifdef EPROTOTYPE
220 0 : MAP(EPROTOTYPE);
221 : #endif
222 : #ifdef ENOTSOCK
223 0 : MAP(ENOTSOCK);
224 : #endif
225 : #ifdef ENOPROTOOPT
226 0 : MAP(ENOPROTOOPT);
227 : #endif
228 : #ifdef ESHUTDOWN
229 0 : MAP(ESHUTDOWN);
230 : #endif
231 : #ifdef ECONNREFUSED
232 0 : MAP(ECONNREFUSED);
233 : #endif
234 : #ifdef EADDRINUSE
235 0 : MAP(EADDRINUSE);
236 : #endif
237 : #ifdef ECONNABORTED
238 0 : MAP(ECONNABORTED);
239 : #endif
240 : #ifdef ENETUNREACH
241 0 : MAP(ENETUNREACH);
242 : #endif
243 : #ifdef ENETDOWN
244 0 : MAP(ENETDOWN);
245 : #endif
246 : #ifdef ETIMEDOUT
247 0 : MAP(ETIMEDOUT);
248 : #endif
249 : #ifdef EHOSTDOWN
250 0 : MAP(EHOSTDOWN);
251 : #endif
252 : #ifdef EHOSTUNREACH
253 0 : MAP(EHOSTUNREACH);
254 : #endif
255 : #ifdef EINPROGRESS
256 0 : MAP(EINPROGRESS);
257 : #endif
258 : #ifdef EALREADY
259 0 : MAP(EALREADY);
260 : #endif
261 : #ifdef EDESTADDRREQ
262 0 : MAP(EDESTADDRREQ);
263 : #endif
264 : #ifdef EPROTONOSUPPORT
265 0 : MAP(EPROTONOSUPPORT);
266 : #endif
267 : #ifdef ESOCKTNOSUPPORT
268 0 : MAP(ESOCKTNOSUPPORT);
269 : #endif
270 : #ifdef EADDRNOTAVAIL
271 0 : MAP(EADDRNOTAVAIL);
272 : #endif
273 : #ifdef ENETRESET
274 0 : MAP(ENETRESET);
275 : #endif
276 : #ifdef EISCONN
277 0 : MAP(EISCONN);
278 : #endif
279 : #ifdef ENOTCONN
280 0 : MAP(ENOTCONN);
281 : #endif
282 : #ifdef ETOOMANYREFS
283 0 : MAP(ETOOMANYREFS);
284 : #endif
285 : #ifdef EPROCLIM
286 0 : MAP(EPROCLIM); /* osx has this; linux does not */
287 : /*
288 : * if we allow fork, we will need to map EAGAIN from fork to EPROCLIM,
289 : * so NaClXlateErrno would not be stateless.
290 : */
291 : #endif
292 : #ifdef EUSERS
293 0 : MAP(EUSERS);
294 : #endif
295 : #ifdef ESTALE
296 0 : MAP(ESTALE);
297 : #endif
298 : #if ENOTSUP != EOPNOTSUPP
299 0 : MAP(ENOTSUP);
300 : #endif
301 : #ifdef ENOMEDIUM
302 : MAP(ENOMEDIUM);
303 : #endif
304 : #ifdef ENOSHARE
305 : MAP(ENOSHARE); /* newlib only? */
306 : #endif
307 : #ifdef ECASECLASH
308 : MAP(ECASECLASH); /* newlib only? */
309 : #endif
310 0 : MAP(EILSEQ);
311 : #ifdef EOVERFLOW
312 0 : MAP(EOVERFLOW);
313 : #endif
314 : #ifdef ECANCELED
315 0 : MAP(ECANCELED);
316 : #endif
317 : #ifdef EL2NSYNC
318 : MAP(EL2NSYNC);
319 : #endif
320 : #ifdef EIDRM
321 0 : MAP(EIDRM);
322 : #endif
323 : #ifdef EMSGSIZE
324 0 : MAP(EMSGSIZE);
325 : #endif
326 : #undef MAP
327 : }
328 0 : return NACL_ABI_EINVAL; /* catch all */
329 : }
330 :
331 : /*
332 : * If you are using this as a kernel-style return, remember that you
333 : * should negate its return value.
334 : */
335 0 : int NaClXlateNaClSyncStatus(NaClSyncStatus status) {
336 0 : switch (status) {
337 : #define MAP(S, E) case S: do { return E; } while (0)
338 0 : MAP(NACL_SYNC_OK, 0);
339 0 : MAP(NACL_SYNC_INTERNAL_ERROR, NACL_ABI_EINVAL); /* generic error */
340 0 : MAP(NACL_SYNC_BUSY, NACL_ABI_EBUSY);
341 0 : MAP(NACL_SYNC_MUTEX_INVALID, NACL_ABI_EBADF);
342 0 : MAP(NACL_SYNC_MUTEX_DEADLOCK, NACL_ABI_EINVAL);
343 0 : MAP(NACL_SYNC_MUTEX_PERMISSION, NACL_ABI_EPERM);
344 0 : MAP(NACL_SYNC_MUTEX_INTERRUPTED, NACL_ABI_EINTR);
345 0 : MAP(NACL_SYNC_CONDVAR_TIMEDOUT, NACL_ABI_ETIMEDOUT);
346 0 : MAP(NACL_SYNC_CONDVAR_INTR, NACL_ABI_EINTR);
347 0 : MAP(NACL_SYNC_SEM_INTERRUPTED, NACL_ABI_EINTR);
348 0 : MAP(NACL_SYNC_SEM_RANGE_ERROR, NACL_ABI_ERANGE);
349 : #undef MAP
350 : }
351 0 : return NACL_ABI_EINVAL; /* catch all */
352 : }
353 :
354 :
355 : struct NaClHostDesc *NaClHostDescPosixMake(int posix_d,
356 67 : int flags) {
357 : struct NaClHostDesc *nhdp;
358 : int error;
359 :
360 67 : nhdp = malloc(sizeof *nhdp);
361 67 : if (NULL == nhdp) {
362 0 : NaClLog(LOG_FATAL, "NaClHostDescPosixMake(%d,0x%x): malloc failed\n",
363 : posix_d, flags);
364 : }
365 67 : if (0 != (error = NaClHostDescPosixTake(nhdp, posix_d, flags))) {
366 0 : NaClLog(LOG_FATAL,
367 : "NaClHostDescPosixMake(%d,0x%x): Take failed, error %da\n",
368 : posix_d, flags, error);
369 : }
370 67 : return nhdp;
371 : }
372 :
373 :
374 7 : int NaClProtMap(int abi_prot) {
375 : int host_os_prot;
376 :
377 7 : host_os_prot = 0;
378 : #define M(H) do { \
379 : if (0 != (abi_prot & NACL_ABI_ ## H)) { \
380 : host_os_prot |= H; \
381 : } \
382 : } while (0)
383 7 : M(PROT_READ);
384 7 : M(PROT_WRITE);
385 7 : M(PROT_EXEC);
386 : #if PROT_NONE != 0
387 : # error "NaClProtMap: PROT_NONE is not zero -- are mprotect flags bit values?"
388 : #endif
389 7 : return host_os_prot;
390 : #undef M
391 : }
|