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(__has_feature)
30 # if __has_feature(memory_sanitizer)
31 #  define MSAN_ACTIVE 1
32 #  include <sanitizer/msan_interface.h>
33 # endif
34 #endif
35 
36 #if defined(__i386__)
37 # ifndef __NR_socketcall
38 #  define __NR_socketcall 102
39 # endif
40 #endif
41 
42 #if defined(__arm__)
43 # if defined(__thumb__) || defined(__ARM_EABI__)
44 #  define UV_SYSCALL_BASE 0
45 # else
46 #  define UV_SYSCALL_BASE 0x900000
47 # endif
48 #endif /* __arm__ */
49 
50 #ifndef __NR_accept4
51 # if defined(__x86_64__)
52 #  define __NR_accept4 288
53 # elif defined(__i386__)
54    /* Nothing. Handled through socketcall(). */
55 # elif defined(__arm__)
56 #  define __NR_accept4 (UV_SYSCALL_BASE + 366)
57 # endif
58 #endif /* __NR_accept4 */
59 
60 #ifndef __NR_eventfd
61 # if defined(__x86_64__)
62 #  define __NR_eventfd 284
63 # elif defined(__i386__)
64 #  define __NR_eventfd 323
65 # elif defined(__arm__)
66 #  define __NR_eventfd (UV_SYSCALL_BASE + 351)
67 # endif
68 #endif /* __NR_eventfd */
69 
70 #ifndef __NR_eventfd2
71 # if defined(__x86_64__)
72 #  define __NR_eventfd2 290
73 # elif defined(__i386__)
74 #  define __NR_eventfd2 328
75 # elif defined(__arm__)
76 #  define __NR_eventfd2 (UV_SYSCALL_BASE + 356)
77 # endif
78 #endif /* __NR_eventfd2 */
79 
80 #ifndef __NR_inotify_init
81 # if defined(__x86_64__)
82 #  define __NR_inotify_init 253
83 # elif defined(__i386__)
84 #  define __NR_inotify_init 291
85 # elif defined(__arm__)
86 #  define __NR_inotify_init (UV_SYSCALL_BASE + 316)
87 # endif
88 #endif /* __NR_inotify_init */
89 
90 #ifndef __NR_inotify_init1
91 # if defined(__x86_64__)
92 #  define __NR_inotify_init1 294
93 # elif defined(__i386__)
94 #  define __NR_inotify_init1 332
95 # elif defined(__arm__)
96 #  define __NR_inotify_init1 (UV_SYSCALL_BASE + 360)
97 # endif
98 #endif /* __NR_inotify_init1 */
99 
100 #ifndef __NR_inotify_add_watch
101 # if defined(__x86_64__)
102 #  define __NR_inotify_add_watch 254
103 # elif defined(__i386__)
104 #  define __NR_inotify_add_watch 292
105 # elif defined(__arm__)
106 #  define __NR_inotify_add_watch (UV_SYSCALL_BASE + 317)
107 # endif
108 #endif /* __NR_inotify_add_watch */
109 
110 #ifndef __NR_inotify_rm_watch
111 # if defined(__x86_64__)
112 #  define __NR_inotify_rm_watch 255
113 # elif defined(__i386__)
114 #  define __NR_inotify_rm_watch 293
115 # elif defined(__arm__)
116 #  define __NR_inotify_rm_watch (UV_SYSCALL_BASE + 318)
117 # endif
118 #endif /* __NR_inotify_rm_watch */
119 
120 #ifndef __NR_pipe2
121 # if defined(__x86_64__)
122 #  define __NR_pipe2 293
123 # elif defined(__i386__)
124 #  define __NR_pipe2 331
125 # elif defined(__arm__)
126 #  define __NR_pipe2 (UV_SYSCALL_BASE + 359)
127 # endif
128 #endif /* __NR_pipe2 */
129 
130 #ifndef __NR_recvmmsg
131 # if defined(__x86_64__)
132 #  define __NR_recvmmsg 299
133 # elif defined(__i386__)
134 #  define __NR_recvmmsg 337
135 # elif defined(__arm__)
136 #  define __NR_recvmmsg (UV_SYSCALL_BASE + 365)
137 # endif
138 #endif /* __NR_recvmsg */
139 
140 #ifndef __NR_sendmmsg
141 # if defined(__x86_64__)
142 #  define __NR_sendmmsg 307
143 # elif defined(__i386__)
144 #  define __NR_sendmmsg 345
145 # elif defined(__arm__)
146 #  define __NR_sendmmsg (UV_SYSCALL_BASE + 374)
147 # endif
148 #endif /* __NR_sendmmsg */
149 
150 #ifndef __NR_utimensat
151 # if defined(__x86_64__)
152 #  define __NR_utimensat 280
153 # elif defined(__i386__)
154 #  define __NR_utimensat 320
155 # elif defined(__arm__)
156 #  define __NR_utimensat (UV_SYSCALL_BASE + 348)
157 # endif
158 #endif /* __NR_utimensat */
159 
160 #ifndef __NR_preadv
161 # if defined(__x86_64__)
162 #  define __NR_preadv 295
163 # elif defined(__i386__)
164 #  define __NR_preadv 333
165 # elif defined(__arm__)
166 #  define __NR_preadv (UV_SYSCALL_BASE + 361)
167 # endif
168 #endif /* __NR_preadv */
169 
170 #ifndef __NR_pwritev
171 # if defined(__x86_64__)
172 #  define __NR_pwritev 296
173 # elif defined(__i386__)
174 #  define __NR_pwritev 334
175 # elif defined(__arm__)
176 #  define __NR_pwritev (UV_SYSCALL_BASE + 362)
177 # endif
178 #endif /* __NR_pwritev */
179 
180 #ifndef __NR_dup3
181 # if defined(__x86_64__)
182 #  define __NR_dup3 292
183 # elif defined(__i386__)
184 #  define __NR_dup3 330
185 # elif defined(__arm__)
186 #  define __NR_dup3 (UV_SYSCALL_BASE + 358)
187 # endif
188 #endif /* __NR_pwritev */
189 
190 #ifndef __NR_statx
191 # if defined(__x86_64__)
192 #  define __NR_statx 332
193 # elif defined(__i386__)
194 #  define __NR_statx 383
195 # elif defined(__aarch64__)
196 #  define __NR_statx 397
197 # elif defined(__arm__)
198 #  define __NR_statx (UV_SYSCALL_BASE + 397)
199 # elif defined(__ppc__)
200 #  define __NR_statx 383
201 # elif defined(__s390__)
202 #  define __NR_statx 379
203 # endif
204 #endif /* __NR_statx */
205 
206 #ifndef __NR_getrandom
207 # if defined(__x86_64__)
208 #  define __NR_getrandom 318
209 # elif defined(__i386__)
210 #  define __NR_getrandom 355
211 # elif defined(__aarch64__)
212 #  define __NR_getrandom 384
213 # elif defined(__arm__)
214 #  define __NR_getrandom (UV_SYSCALL_BASE + 384)
215 # elif defined(__ppc__)
216 #  define __NR_getrandom 359
217 # elif defined(__s390__)
218 #  define __NR_getrandom 349
219 # endif
220 #endif /* __NR_getrandom */
221 
uv__accept4(int fd,struct sockaddr * addr,socklen_t * addrlen,int flags)222 int uv__accept4(int fd, struct sockaddr* addr, socklen_t* addrlen, int flags) {
223 #if defined(__i386__)
224   unsigned long args[4];
225   int r;
226 
227   args[0] = (unsigned long) fd;
228   args[1] = (unsigned long) addr;
229   args[2] = (unsigned long) addrlen;
230   args[3] = (unsigned long) flags;
231 
232   r = syscall(__NR_socketcall, 18 /* SYS_ACCEPT4 */, args);
233 
234   /* socketcall() raises EINVAL when SYS_ACCEPT4 is not supported but so does
235    * a bad flags argument. Try to distinguish between the two cases.
236    */
237   if (r == -1)
238     if (errno == EINVAL)
239       if ((flags & ~(UV__SOCK_CLOEXEC|UV__SOCK_NONBLOCK)) == 0)
240         errno = ENOSYS;
241 
242   return r;
243 #elif defined(__NR_accept4)
244   return syscall(__NR_accept4, fd, addr, addrlen, flags);
245 #else
246   return errno = ENOSYS, -1;
247 #endif
248 }
249 
250 
uv__eventfd(unsigned int count)251 int uv__eventfd(unsigned int count) {
252 #if defined(__NR_eventfd)
253   return syscall(__NR_eventfd, count);
254 #else
255   return errno = ENOSYS, -1;
256 #endif
257 }
258 
259 
uv__eventfd2(unsigned int count,int flags)260 int uv__eventfd2(unsigned int count, int flags) {
261 #if defined(__NR_eventfd2)
262   return syscall(__NR_eventfd2, count, flags);
263 #else
264   return errno = ENOSYS, -1;
265 #endif
266 }
267 
268 
uv__inotify_init(void)269 int uv__inotify_init(void) {
270 #if defined(__NR_inotify_init)
271   return syscall(__NR_inotify_init);
272 #else
273   return errno = ENOSYS, -1;
274 #endif
275 }
276 
277 
uv__inotify_init1(int flags)278 int uv__inotify_init1(int flags) {
279 #if defined(__NR_inotify_init1)
280   return syscall(__NR_inotify_init1, flags);
281 #else
282   return errno = ENOSYS, -1;
283 #endif
284 }
285 
286 
uv__inotify_add_watch(int fd,const char * path,uint32_t mask)287 int uv__inotify_add_watch(int fd, const char* path, uint32_t mask) {
288 #if defined(__NR_inotify_add_watch)
289   return syscall(__NR_inotify_add_watch, fd, path, mask);
290 #else
291   return errno = ENOSYS, -1;
292 #endif
293 }
294 
295 
uv__inotify_rm_watch(int fd,int32_t wd)296 int uv__inotify_rm_watch(int fd, int32_t wd) {
297 #if defined(__NR_inotify_rm_watch)
298   return syscall(__NR_inotify_rm_watch, fd, wd);
299 #else
300   return errno = ENOSYS, -1;
301 #endif
302 }
303 
304 
uv__pipe2(int pipefd[2],int flags)305 int uv__pipe2(int pipefd[2], int flags) {
306 #if defined(__NR_pipe2)
307   int result;
308   result = syscall(__NR_pipe2, pipefd, flags);
309 #if MSAN_ACTIVE
310   if (!result)
311     __msan_unpoison(pipefd, sizeof(int[2]));
312 #endif
313   return result;
314 #else
315   return errno = ENOSYS, -1;
316 #endif
317 }
318 
319 
uv__sendmmsg(int fd,struct uv__mmsghdr * mmsg,unsigned int vlen,unsigned int flags)320 int uv__sendmmsg(int fd,
321                  struct uv__mmsghdr* mmsg,
322                  unsigned int vlen,
323                  unsigned int flags) {
324 #if defined(__NR_sendmmsg)
325   return syscall(__NR_sendmmsg, fd, mmsg, vlen, flags);
326 #else
327   return errno = ENOSYS, -1;
328 #endif
329 }
330 
331 
uv__recvmmsg(int fd,struct uv__mmsghdr * mmsg,unsigned int vlen,unsigned int flags,struct timespec * timeout)332 int uv__recvmmsg(int fd,
333                  struct uv__mmsghdr* mmsg,
334                  unsigned int vlen,
335                  unsigned int flags,
336                  struct timespec* timeout) {
337 #if defined(__NR_recvmmsg)
338   return syscall(__NR_recvmmsg, fd, mmsg, vlen, flags, timeout);
339 #else
340   return errno = ENOSYS, -1;
341 #endif
342 }
343 
344 
uv__preadv(int fd,const struct iovec * iov,int iovcnt,int64_t offset)345 ssize_t uv__preadv(int fd, const struct iovec *iov, int iovcnt, int64_t offset) {
346 #if defined(__NR_preadv)
347   return syscall(__NR_preadv, fd, iov, iovcnt, (long)offset, (long)(offset >> 32));
348 #else
349   return errno = ENOSYS, -1;
350 #endif
351 }
352 
353 
uv__pwritev(int fd,const struct iovec * iov,int iovcnt,int64_t offset)354 ssize_t uv__pwritev(int fd, const struct iovec *iov, int iovcnt, int64_t offset) {
355 #if defined(__NR_pwritev)
356   return syscall(__NR_pwritev, fd, iov, iovcnt, (long)offset, (long)(offset >> 32));
357 #else
358   return errno = ENOSYS, -1;
359 #endif
360 }
361 
362 
uv__dup3(int oldfd,int newfd,int flags)363 int uv__dup3(int oldfd, int newfd, int flags) {
364 #if defined(__NR_dup3)
365   return syscall(__NR_dup3, oldfd, newfd, flags);
366 #else
367   return errno = ENOSYS, -1;
368 #endif
369 }
370 
371 
uv__statx(int dirfd,const char * path,int flags,unsigned int mask,struct uv__statx * statxbuf)372 int uv__statx(int dirfd,
373               const char* path,
374               int flags,
375               unsigned int mask,
376               struct uv__statx* statxbuf) {
377   /* __NR_statx make Android box killed by SIGSYS.
378    * That looks like a seccomp2 sandbox filter rejecting the system call.
379    */
380 #if defined(__NR_statx) && !defined(__ANDROID__)
381   return syscall(__NR_statx, dirfd, path, flags, mask, statxbuf);
382 #else
383   return errno = ENOSYS, -1;
384 #endif
385 }
386 
387 
uv__getrandom(void * buf,size_t buflen,unsigned flags)388 ssize_t uv__getrandom(void* buf, size_t buflen, unsigned flags) {
389 #if defined(__NR_getrandom)
390   return syscall(__NR_getrandom, buf, buflen, flags);
391 #else
392   return errno = ENOSYS, -1;
393 #endif
394 }
395