Debugging: Add code to print backtrace for guest on SIGSEGV
[nativeclient.git] / service_runtime / nacl_host_desc_common.c
blobb415ab986039829d8d2a1aa752692a1ee713b714
1 /*
2 * Copyright 2008, Google Inc.
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 are
7 * met:
8 *
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
14 * distribution.
15 * * Neither the name of Google Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 * NaCl Service Runtime. I/O Descriptor / Handle abstraction. Memory
34 * mapping using descriptors.
37 #include "native_client/include/portability.h"
39 #include "native_client/service_runtime/nacl_host_desc.h"
40 #include "native_client/service_runtime/nacl_log.h"
42 #include "native_client/service_runtime/include/sys/errno.h"
45 * If you are using this as a kernel-style return, remember that you
46 * should negate its return value.
48 int NaClXlateErrno(int errnum)
50 switch (errnum) {
52 * Unfortunately a macro cannot expand to contain #ifdef....
54 * TODO: All host-OS conditional errnos should map into a
55 * generic errno.
57 #define MAP(E) case E: do { return NACL_ABI_ ## E; } while (0)
58 MAP(EPERM);
59 MAP(ENOENT);
60 MAP(ESRCH);
61 MAP(EINTR);
62 MAP(EIO);
63 MAP(ENXIO);
64 MAP(E2BIG);
65 MAP(ENOEXEC);
66 MAP(EBADF);
67 MAP(ECHILD);
68 MAP(EAGAIN);
69 MAP(ENOMEM);
70 MAP(EACCES);
71 MAP(EFAULT);
72 MAP(EBUSY);
73 MAP(EEXIST);
74 MAP(EXDEV);
75 MAP(ENODEV);
76 MAP(ENOTDIR);
77 MAP(EISDIR);
78 MAP(EINVAL);
79 MAP(ENFILE);
80 MAP(EMFILE);
81 MAP(ENOTTY);
82 MAP(EFBIG);
83 MAP(ENOSPC);
84 MAP(ESPIPE);
85 MAP(EROFS);
86 MAP(EMLINK);
87 MAP(EPIPE);
88 MAP(ENAMETOOLONG);
89 MAP(ENOSYS);
90 MAP(EDQUOT);
91 MAP(EDOM);
92 MAP(ERANGE);
93 #ifdef ENOMSG
94 MAP(ENOMSG);
95 #endif
96 #ifdef ECHRNG
97 MAP(ECHRNG);
98 #endif
99 #ifdef EL3HLT
100 MAP(EL3HLT); /* not in osx */
101 #endif
102 #ifdef EL3RST
103 MAP(EL3RST); /* not in osx */
104 #endif
105 #ifdef EL3RNG
106 MAP(ELNRNG); /* not in osx */
107 #endif
108 #ifdef EUNATCH
109 MAP(EUNATCH);
110 #endif
111 #ifdef ENOCSI
112 MAP(ENOCSI);
113 #endif
114 #ifdef EL2HLT
115 MAP(EL2HLT);
116 #endif
117 MAP(EDEADLK);
118 MAP(ENOLCK);
119 #ifdef EBADE
120 MAP(EBADE);
121 #endif
122 #ifdef EBADR
123 MAP(EBADR);
124 #endif
125 #ifdef EXFULL
126 MAP(EXFULL);
127 #endif
128 #ifdef ENOANO
129 MAP(ENOANO);
130 #endif
131 #ifdef EBADRQC
132 MAP(EBADRQC);
133 #endif
134 #ifdef EBADSLT
135 MAP(EBADSLT);
136 #endif
137 #if defined(EDEADLOCK) && EDEADLK != EDEADLOCK
138 MAP(EDEADLOCK);
139 #endif
140 #ifdef EBFONT
141 MAP(EBFONT);
142 #endif
143 #ifdef ENOSTR
144 MAP(ENOSTR);
145 #endif
146 #ifdef ENODATA
147 MAP(ENODATA);
148 #endif
149 #ifdef ETIME
150 MAP(ETIME);
151 #endif
152 #ifdef ENOSR
153 MAP(ENOSR);
154 #endif
155 #ifdef ENONET
156 MAP(ENONET);
157 #endif
158 #ifdef ENOPKG
159 MAP(ENOPKG);
160 #endif
161 #ifdef EREMOTE
162 MAP(EREMOTE);
163 #endif
164 #ifdef ENOLINK
165 MAP(ENOLINK);
166 #endif
167 #ifdef EADV
168 MAP(EADV);
169 #endif
170 #ifdef ESRMNT
171 MAP(ESRMNT);
172 #endif
173 #ifdef ECOMM
174 MAP(ECOMM);
175 #endif
176 #ifdef EPROTO
177 MAP(EPROTO);
178 #endif
179 #ifdef EMULTIHOP
180 MAP(EMULTIHOP);
181 #endif
182 #ifdef ELBIN
183 MAP(ELBIN); /* newlib only? */
184 #endif
185 #ifdef EDOTDOT
186 MAP(EDOTDOT);
187 #endif
188 #ifdef EBADMSG
189 MAP(EBADMSG);
190 #endif
191 #ifdef EFTYPE
192 MAP(EFTYPE); /* osx has it; linux doesn't */
193 #endif
194 #ifdef ENOTUNIQ
195 MAP(ENOTUNIQ);
196 #endif
197 #ifdef EBADFD
198 MAP(EBADFD);
199 #endif
200 #ifdef EREMCHG
201 MAP(EREMCHG);
202 #endif
203 #ifdef ELIBACC
204 MAP(ELIBACC);
205 #endif
206 #ifdef ELIBBAD
207 MAP(ELIBBAD);
208 #endif
209 #ifdef ELIBSCN
210 MAP(ELIBSCN);
211 #endif
212 #ifdef ELIBMAX
213 MAP(ELIBMAX);
214 #endif
215 #ifdef ELIBEXEC
216 MAP(ELIBEXEC);
217 #endif
218 #ifdef ENMFILE
219 MAP(ENMFILE); /* newlib only? */
220 #endif
221 MAP(ENOTEMPTY);
222 #ifdef ELOOP
223 MAP(ELOOP);
224 #endif
225 #ifdef EOPNOTSUPP
226 MAP(EOPNOTSUPP);
227 #endif
228 #ifdef EPFNOSUPPORT
229 MAP(EPFNOSUPPORT);
230 #endif
231 #ifdef ECONNRESET
232 MAP(ECONNRESET);
233 #endif
234 #ifdef ENOBUFS
235 MAP(ENOBUFS);
236 #endif
237 #ifdef EAFNOSUPPORT
238 MAP(EAFNOSUPPORT);
239 #endif
240 #ifdef EPROTOTYPE
241 MAP(EPROTOTYPE);
242 #endif
243 #ifdef ENOTSOCK
244 MAP(ENOTSOCK);
245 #endif
246 #ifdef ENOPROTOOPT
247 MAP(ENOPROTOOPT);
248 #endif
249 #ifdef ESHUTDOWN
250 MAP(ESHUTDOWN);
251 #endif
252 #ifdef ECONNREFUSED
253 MAP(ECONNREFUSED);
254 #endif
255 #ifdef EADDRINUSE
256 MAP(EADDRINUSE);
257 #endif
258 #ifdef ECONNABORTED
259 MAP(ECONNABORTED);
260 #endif
261 #ifdef ENETUNREACH
262 MAP(ENETUNREACH);
263 #endif
264 #ifdef ENETDOWN
265 MAP(ENETDOWN);
266 #endif
267 #ifdef ETIMEDOUT
268 MAP(ETIMEDOUT);
269 #endif
270 #ifdef EHOSTDOWN
271 MAP(EHOSTDOWN);
272 #endif
273 #ifdef EHOSTUNREACH
274 MAP(EHOSTUNREACH);
275 #endif
276 #ifdef EINPROGRESS
277 MAP(EINPROGRESS);
278 #endif
279 #ifdef EALREADY
280 MAP(EALREADY);
281 #endif
282 #ifdef EDESTADDRREQ
283 MAP(EDESTADDRREQ);
284 #endif
285 #ifdef EPROTONOSUPPORT
286 MAP(EPROTONOSUPPORT);
287 #endif
288 #ifdef ESOCKTNOSUPPORT
289 MAP(ESOCKTNOSUPPORT);
290 #endif
291 #ifdef EADDRNOTAVAIL
292 MAP(EADDRNOTAVAIL);
293 #endif
294 #ifdef ENETRESET
295 MAP(ENETRESET);
296 #endif
297 #ifdef EISCONN
298 MAP(EISCONN);
299 #endif
300 #ifdef ENOTCONN
301 MAP(ENOTCONN);
302 #endif
303 #ifdef ETOOMANYREFS
304 MAP(ETOOMANYREFS);
305 #endif
306 #ifdef EPROCLIM
307 MAP(EPROCLIM); /* osx has this; linux does not */
309 * if we allow fork, we will need to map EAGAIN from fork to EPROCLIM,
310 * so NaClXlateErrno would not be stateless.
312 #endif
313 #ifdef EUSERS
314 MAP(EUSERS);
315 #endif
316 #ifdef ESTALE
317 MAP(ESTALE);
318 #endif
319 #if ENOTSUP != EOPNOTSUPP
320 MAP(ENOTSUP);
321 #endif
322 #ifdef ENOMEDIUM
323 MAP(ENOMEDIUM);
324 #endif
325 #ifdef ENOSHARE
326 MAP(ENOSHARE); /* newlib only? */
327 #endif
328 #ifdef ECASECLASH
329 MAP(ECASECLASH); /* newlib only? */
330 #endif
331 MAP(EILSEQ);
332 #ifdef EOVERFLOW
333 MAP(EOVERFLOW);
334 #endif
335 #ifdef ECANCELED
336 MAP(ECANCELED);
337 #endif
338 #ifdef EL2NSYNC
339 MAP(EL2NSYNC);
340 #endif
341 #ifdef EIDRM
342 MAP(EIDRM);
343 #endif
344 #ifdef EMSGSIZE
345 MAP(EMSGSIZE);
346 #endif
347 #undef MAP
349 return NACL_ABI_EINVAL; /* catch all */
353 * If you are using this as a kernel-style return, remember that you
354 * should negate its return value.
356 int NaClXlateNaClSyncStatus(NaClSyncStatus status)
358 switch (status) {
359 #define MAP(S, E) case S: do { return E; } while (0)
360 MAP(NACL_SYNC_OK, 0);
361 MAP(NACL_SYNC_INTERNAL_ERROR, NACL_ABI_EINVAL); /* generic error */
362 MAP(NACL_SYNC_BUSY, NACL_ABI_EBUSY);
363 MAP(NACL_SYNC_MUTEX_INVALID, NACL_ABI_EBADF);
364 MAP(NACL_SYNC_MUTEX_DEADLOCK, NACL_ABI_EINVAL);
365 MAP(NACL_SYNC_MUTEX_PERMISSION, NACL_ABI_EPERM);
366 MAP(NACL_SYNC_MUTEX_INTERRUPTED, NACL_ABI_EINTR);
367 /* TODO: should it be NACL_ABI_ETIMEDOUT? */
368 MAP(NACL_SYNC_CONDVAR_TIMEDOUT, NACL_ABI_ETIME);
369 MAP(NACL_SYNC_CONDVAR_INTR, NACL_ABI_EINTR);
370 MAP(NACL_SYNC_SEM_INTERRUPTED, NACL_ABI_EINTR);
371 MAP(NACL_SYNC_SEM_RANGE_ERROR, NACL_ABI_ERANGE);
372 #undef MAP
374 return NACL_ABI_EINVAL; /* catch all */
378 struct NaClHostDesc *NaClHostDescPosixMake(int posix_d,
379 int mode)
381 struct NaClHostDesc *nhdp;
383 nhdp = malloc(sizeof *nhdp);
384 if (NULL == nhdp) {
385 NaClLog(LOG_FATAL, "NaClHostDescPosixMake(%d,0x%x): malloc failed\n",
386 posix_d, mode);
388 if (!NaClHostDescPosixTake(nhdp, posix_d, mode)) {
389 NaClLog(LOG_FATAL, "NaClHostDescPosixMake(%d,0x%x): Take failed\n",
390 posix_d, mode);
392 return nhdp;