1 /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining a copy
4  * of this software and associated documentation files (the "Software"), to
5  * deal in the Software without restriction, including without limitation the
6  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7  * sell copies of the Software, and to permit persons to whom the Software is
8  * furnished to do so, subject to the following conditions:
9  *
10  * The above copyright notice and this permission notice shall be included in
11  * all copies or substantial portions of the Software.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19  * IN THE SOFTWARE.
20  */
21 
22 #include "linux-syscalls.h"
23 #include <unistd.h>
24 #include <signal.h>
25 #include <sys/syscall.h>
26 #include <sys/types.h>
27 #include <errno.h>
28 
29 #if defined(__arm__)
30 # if defined(__thumb__) || defined(__ARM_EABI__)
31 #  define UV_SYSCALL_BASE 0
32 # else
33 #  define UV_SYSCALL_BASE 0x900000
34 # endif
35 #endif /* __arm__ */
36 
37 #ifndef __NR_recvmmsg
38 # if defined(__x86_64__)
39 #  define __NR_recvmmsg 299
40 # elif defined(__i386__)
41 #  define __NR_recvmmsg 337
42 # elif defined(__arm__)
43 #  define __NR_recvmmsg (UV_SYSCALL_BASE + 365)
44 # endif
45 #endif /* __NR_recvmsg */
46 
47 #ifndef __NR_sendmmsg
48 # if defined(__x86_64__)
49 #  define __NR_sendmmsg 307
50 # elif defined(__i386__)
51 #  define __NR_sendmmsg 345
52 # elif defined(__arm__)
53 #  define __NR_sendmmsg (UV_SYSCALL_BASE + 374)
54 # endif
55 #endif /* __NR_sendmmsg */
56 
57 #ifndef __NR_utimensat
58 # if defined(__x86_64__)
59 #  define __NR_utimensat 280
60 # elif defined(__i386__)
61 #  define __NR_utimensat 320
62 # elif defined(__arm__)
63 #  define __NR_utimensat (UV_SYSCALL_BASE + 348)
64 # endif
65 #endif /* __NR_utimensat */
66 
67 #ifndef __NR_preadv
68 # if defined(__x86_64__)
69 #  define __NR_preadv 295
70 # elif defined(__i386__)
71 #  define __NR_preadv 333
72 # elif defined(__arm__)
73 #  define __NR_preadv (UV_SYSCALL_BASE + 361)
74 # endif
75 #endif /* __NR_preadv */
76 
77 #ifndef __NR_pwritev
78 # if defined(__x86_64__)
79 #  define __NR_pwritev 296
80 # elif defined(__i386__)
81 #  define __NR_pwritev 334
82 # elif defined(__arm__)
83 #  define __NR_pwritev (UV_SYSCALL_BASE + 362)
84 # endif
85 #endif /* __NR_pwritev */
86 
87 #ifndef __NR_dup3
88 # if defined(__x86_64__)
89 #  define __NR_dup3 292
90 # elif defined(__i386__)
91 #  define __NR_dup3 330
92 # elif defined(__arm__)
93 #  define __NR_dup3 (UV_SYSCALL_BASE + 358)
94 # endif
95 #endif /* __NR_pwritev */
96 
97 #ifndef __NR_statx
98 # if defined(__x86_64__)
99 #  define __NR_statx 332
100 # elif defined(__i386__)
101 #  define __NR_statx 383
102 # elif defined(__aarch64__)
103 #  define __NR_statx 397
104 # elif defined(__arm__)
105 #  define __NR_statx (UV_SYSCALL_BASE + 397)
106 # elif defined(__ppc__)
107 #  define __NR_statx 383
108 # elif defined(__s390__)
109 #  define __NR_statx 379
110 # endif
111 #endif /* __NR_statx */
112 
113 #ifndef __NR_getrandom
114 # if defined(__x86_64__)
115 #  define __NR_getrandom 318
116 # elif defined(__i386__)
117 #  define __NR_getrandom 355
118 # elif defined(__aarch64__)
119 #  define __NR_getrandom 384
120 # elif defined(__arm__)
121 #  define __NR_getrandom (UV_SYSCALL_BASE + 384)
122 # elif defined(__ppc__)
123 #  define __NR_getrandom 359
124 # elif defined(__s390__)
125 #  define __NR_getrandom 349
126 # endif
127 #endif /* __NR_getrandom */
128 
129 struct uv__mmsghdr;
130 
uv__sendmmsg(int fd,struct uv__mmsghdr * mmsg,unsigned int vlen,unsigned int flags)131 int uv__sendmmsg(int fd,
132                  struct uv__mmsghdr* mmsg,
133                  unsigned int vlen,
134                  unsigned int flags) {
135 #if defined(__NR_sendmmsg)
136   return syscall(__NR_sendmmsg, fd, mmsg, vlen, flags);
137 #else
138   return errno = ENOSYS, -1;
139 #endif
140 }
141 
142 
uv__recvmmsg(int fd,struct uv__mmsghdr * mmsg,unsigned int vlen,unsigned int flags,struct timespec * timeout)143 int uv__recvmmsg(int fd,
144                  struct uv__mmsghdr* mmsg,
145                  unsigned int vlen,
146                  unsigned int flags,
147                  struct timespec* timeout) {
148 #if defined(__NR_recvmmsg)
149   return syscall(__NR_recvmmsg, fd, mmsg, vlen, flags, timeout);
150 #else
151   return errno = ENOSYS, -1;
152 #endif
153 }
154 
155 
uv__preadv(int fd,const struct iovec * iov,int iovcnt,int64_t offset)156 ssize_t uv__preadv(int fd, const struct iovec *iov, int iovcnt, int64_t offset) {
157 #if defined(__NR_preadv)
158   return syscall(__NR_preadv, fd, iov, iovcnt, (long)offset, (long)(offset >> 32));
159 #else
160   return errno = ENOSYS, -1;
161 #endif
162 }
163 
164 
uv__pwritev(int fd,const struct iovec * iov,int iovcnt,int64_t offset)165 ssize_t uv__pwritev(int fd, const struct iovec *iov, int iovcnt, int64_t offset) {
166 #if defined(__NR_pwritev)
167   return syscall(__NR_pwritev, fd, iov, iovcnt, (long)offset, (long)(offset >> 32));
168 #else
169   return errno = ENOSYS, -1;
170 #endif
171 }
172 
173 
uv__dup3(int oldfd,int newfd,int flags)174 int uv__dup3(int oldfd, int newfd, int flags) {
175 #if defined(__NR_dup3)
176   return syscall(__NR_dup3, oldfd, newfd, flags);
177 #else
178   return errno = ENOSYS, -1;
179 #endif
180 }
181 
182 
uv__statx(int dirfd,const char * path,int flags,unsigned int mask,struct uv__statx * statxbuf)183 int uv__statx(int dirfd,
184               const char* path,
185               int flags,
186               unsigned int mask,
187               struct uv__statx* statxbuf) {
188   /* __NR_statx make Android box killed by SIGSYS.
189    * That looks like a seccomp2 sandbox filter rejecting the system call.
190    */
191 #if defined(__NR_statx) && !defined(__ANDROID__)
192   return syscall(__NR_statx, dirfd, path, flags, mask, statxbuf);
193 #else
194   return errno = ENOSYS, -1;
195 #endif
196 }
197 
198 
uv__getrandom(void * buf,size_t buflen,unsigned flags)199 ssize_t uv__getrandom(void* buf, size_t buflen, unsigned flags) {
200 #if defined(__NR_getrandom)
201   return syscall(__NR_getrandom, buf, buflen, flags);
202 #else
203   return errno = ENOSYS, -1;
204 #endif
205 }
206