]> git.proxmox.com Git - wasi-libc.git/blob - libc-bottom-half/cloudlibc/src/common/errno.h
9135c69fac000ccdebc7a77d7d4bf43503934f66
[wasi-libc.git] / libc-bottom-half / cloudlibc / src / common / errno.h
1 // Copyright (c) 2015-2016 Nuxi, https://nuxi.nl/
2 //
3 // SPDX-License-Identifier: BSD-2-Clause
4
5 #ifndef COMMON_ERRNO_H
6 #define COMMON_ERRNO_H
7
8 #include <wasi/core.h>
9
10 #ifdef __wasilibc_unmodified_upstream
11 // Translates ENOTCAPABLE to ENOTDIR if not a directory.
12 static inline __wasi_errno_t errno_fixup_directory(__wasi_fd_t fd,
13 __wasi_errno_t error) {
14 if (error == __WASI_ENOTCAPABLE) {
15 __wasi_fdstat_t fds;
16 if (__wasi_fd_stat_get(fd, &fds) == 0 &&
17 fds.fs_filetype != __WASI_FILETYPE_DIRECTORY)
18 return __WASI_ENOTDIR;
19 }
20 return error;
21 }
22 #else
23 // WASI syscalls should just return ENOTDIR if that's what the problem is.
24 static inline __wasi_errno_t errno_fixup_directory(__wasi_fd_t fd,
25 __wasi_errno_t error) {
26 return error;
27 }
28 #endif
29
30 #ifdef __wasilibc_unmodified_upstream // posix_spawn etc.
31 // Translates ENOTCAPABLE to EBADF if a regular file or EACCES otherwise.
32 static inline __wasi_errno_t errno_fixup_executable(__wasi_fd_t fd,
33 __wasi_errno_t error) {
34 if (error == __WASI_ENOTCAPABLE) {
35 __wasi_fdstat_t fds;
36 if (__wasi_fd_stat_get(fd, &fds) == 0)
37 return fds.fs_filetype == __WASI_FILETYPE_REGULAR_FILE
38 ? __WASI_EBADF
39 : __WASI_EACCES;
40 }
41 return error;
42 }
43 #endif
44
45 #ifdef __wasilibc_unmodified_upstream // process file descriptors
46 // Translates ENOTCAPABLE to EINVAL if not a process.
47 static inline __wasi_errno_t errno_fixup_process(__wasi_fd_t fd,
48 __wasi_errno_t error) {
49 if (error == __WASI_ENOTCAPABLE) {
50 __wasi_fdstat_t fds;
51 if (__wasi_fd_stat_get(fd, &fds) == 0 &&
52 fds.fs_filetype != __WASI_FILETYPE_PROCESS)
53 return __WASI_EINVAL;
54 }
55 return error;
56 }
57 #endif
58
59 #ifdef __wasilibc_unmodified_upstream
60 // Translates ENOTCAPABLE to ENOTSOCK if not a socket.
61 static inline __wasi_errno_t errno_fixup_socket(__wasi_fd_t fd,
62 __wasi_errno_t error) {
63 if (error == __WASI_ENOTCAPABLE) {
64 __wasi_fdstat_t fds;
65 if (__wasi_fd_stat_get(fd, &fds) == 0 &&
66 #ifdef __wasilibc_unmodified_upstream // don't hard-code magic numbers
67 (fds.fs_filetype & 0xf0) != 0x80)
68 #else
69 (fds.fs_filetype != __WASI_FILETYPE_SOCKET_STREAM &&
70 fds.fs_filetype != __WASI_FILETYPE_SOCKET_DGRAM))
71 #endif
72 return __WASI_ENOTSOCK;
73 }
74 return error;
75 }
76 #else
77 // WASI syscalls should just return ENOTSOCK if that's what the problem is.
78 static inline __wasi_errno_t errno_fixup_socket(__wasi_fd_t fd,
79 __wasi_errno_t error) {
80 return error;
81 }
82 #endif
83
84 #endif