Path: blob/master/Utilities/cmlibuv/src/unix/linux-syscalls.c
3156 views
/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.1*2* Permission is hereby granted, free of charge, to any person obtaining a copy3* of this software and associated documentation files (the "Software"), to4* deal in the Software without restriction, including without limitation the5* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or6* sell copies of the Software, and to permit persons to whom the Software is7* furnished to do so, subject to the following conditions:8*9* The above copyright notice and this permission notice shall be included in10* all copies or substantial portions of the Software.11*12* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR13* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,14* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE15* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER16* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING17* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS18* IN THE SOFTWARE.19*/2021#include "linux-syscalls.h"22#include <unistd.h>23#include <signal.h>24#include <sys/syscall.h>25#include <sys/types.h>26#include <errno.h>2728#if defined(__arm__)29# if defined(__thumb__) || defined(__ARM_EABI__)30# define UV_SYSCALL_BASE 031# else32# define UV_SYSCALL_BASE 0x90000033# endif34#endif /* __arm__ */3536#ifndef __NR_recvmmsg37# if defined(__x86_64__)38# define __NR_recvmmsg 29939# elif defined(__arm__)40# define __NR_recvmmsg (UV_SYSCALL_BASE + 365)41# endif42#endif /* __NR_recvmsg */4344#ifndef __NR_sendmmsg45# if defined(__x86_64__)46# define __NR_sendmmsg 30747# elif defined(__arm__)48# define __NR_sendmmsg (UV_SYSCALL_BASE + 374)49# endif50#endif /* __NR_sendmmsg */5152#ifndef __NR_utimensat53# if defined(__x86_64__)54# define __NR_utimensat 28055# elif defined(__i386__)56# define __NR_utimensat 32057# elif defined(__arm__)58# define __NR_utimensat (UV_SYSCALL_BASE + 348)59# endif60#endif /* __NR_utimensat */6162#ifndef __NR_preadv63# if defined(__x86_64__)64# define __NR_preadv 29565# elif defined(__i386__)66# define __NR_preadv 33367# elif defined(__arm__)68# define __NR_preadv (UV_SYSCALL_BASE + 361)69# endif70#endif /* __NR_preadv */7172#ifndef __NR_pwritev73# if defined(__x86_64__)74# define __NR_pwritev 29675# elif defined(__i386__)76# define __NR_pwritev 33477# elif defined(__arm__)78# define __NR_pwritev (UV_SYSCALL_BASE + 362)79# endif80#endif /* __NR_pwritev */8182#ifndef __NR_dup383# if defined(__x86_64__)84# define __NR_dup3 29285# elif defined(__i386__)86# define __NR_dup3 33087# elif defined(__arm__)88# define __NR_dup3 (UV_SYSCALL_BASE + 358)89# endif90#endif /* __NR_pwritev */9192#ifndef __NR_copy_file_range93# if defined(__x86_64__)94# define __NR_copy_file_range 32695# elif defined(__i386__)96# define __NR_copy_file_range 37797# elif defined(__s390__)98# define __NR_copy_file_range 37599# elif defined(__arm__)100# define __NR_copy_file_range (UV_SYSCALL_BASE + 391)101# elif defined(__aarch64__)102# define __NR_copy_file_range 285103# elif defined(__powerpc__)104# define __NR_copy_file_range 379105# elif defined(__arc__)106# define __NR_copy_file_range 285107# endif108#endif /* __NR_copy_file_range */109110#ifndef __NR_statx111# if defined(__x86_64__)112# define __NR_statx 332113# elif defined(__i386__)114# define __NR_statx 383115# elif defined(__aarch64__)116# define __NR_statx 397117# elif defined(__arm__)118# define __NR_statx (UV_SYSCALL_BASE + 397)119# elif defined(__ppc__)120# define __NR_statx 383121# elif defined(__s390__)122# define __NR_statx 379123# endif124#endif /* __NR_statx */125126#ifndef __NR_getrandom127# if defined(__x86_64__)128# define __NR_getrandom 318129# elif defined(__i386__)130# define __NR_getrandom 355131# elif defined(__aarch64__)132# define __NR_getrandom 384133# elif defined(__arm__)134# define __NR_getrandom (UV_SYSCALL_BASE + 384)135# elif defined(__ppc__)136# define __NR_getrandom 359137# elif defined(__s390__)138# define __NR_getrandom 349139# endif140#endif /* __NR_getrandom */141142struct uv__mmsghdr;143144int uv__sendmmsg(int fd, struct uv__mmsghdr* mmsg, unsigned int vlen) {145#if defined(__i386__)146unsigned long args[4];147int rc;148149args[0] = (unsigned long) fd;150args[1] = (unsigned long) mmsg;151args[2] = (unsigned long) vlen;152args[3] = /* flags */ 0;153154/* socketcall() raises EINVAL when SYS_SENDMMSG is not supported. */155rc = syscall(/* __NR_socketcall */ 102, 20 /* SYS_SENDMMSG */, args);156if (rc == -1)157if (errno == EINVAL)158errno = ENOSYS;159160return rc;161#elif defined(__NR_sendmmsg)162return syscall(__NR_sendmmsg, fd, mmsg, vlen, /* flags */ 0);163#else164return errno = ENOSYS, -1;165#endif166}167168169int uv__recvmmsg(int fd, struct uv__mmsghdr* mmsg, unsigned int vlen) {170#if defined(__i386__)171unsigned long args[5];172int rc;173174args[0] = (unsigned long) fd;175args[1] = (unsigned long) mmsg;176args[2] = (unsigned long) vlen;177args[3] = /* flags */ 0;178args[4] = /* timeout */ 0;179180/* socketcall() raises EINVAL when SYS_RECVMMSG is not supported. */181rc = syscall(/* __NR_socketcall */ 102, 19 /* SYS_RECVMMSG */, args);182if (rc == -1)183if (errno == EINVAL)184errno = ENOSYS;185186return rc;187#elif defined(__NR_recvmmsg)188return syscall(__NR_recvmmsg, fd, mmsg, vlen, /* flags */ 0, /* timeout */ 0);189#else190return errno = ENOSYS, -1;191#endif192}193194195ssize_t uv__preadv(int fd, const struct iovec *iov, int iovcnt, int64_t offset) {196#if !defined(__NR_preadv) || defined(__ANDROID_API__) && __ANDROID_API__ < 24197return errno = ENOSYS, -1;198#else199return syscall(__NR_preadv, fd, iov, iovcnt, (long)offset, (long)(offset >> 32));200#endif201}202203204ssize_t uv__pwritev(int fd, const struct iovec *iov, int iovcnt, int64_t offset) {205#if !defined(__NR_pwritev) || defined(__ANDROID_API__) && __ANDROID_API__ < 24206return errno = ENOSYS, -1;207#else208return syscall(__NR_pwritev, fd, iov, iovcnt, (long)offset, (long)(offset >> 32));209#endif210}211212213int uv__dup3(int oldfd, int newfd, int flags) {214#if !defined(__NR_dup3) || defined(__ANDROID_API__) && __ANDROID_API__ < 21215return errno = ENOSYS, -1;216#else217return syscall(__NR_dup3, oldfd, newfd, flags);218#endif219}220221222ssize_t223uv__fs_copy_file_range(int fd_in,224off_t* off_in,225int fd_out,226off_t* off_out,227size_t len,228unsigned int flags)229{230#ifdef __NR_copy_file_range231return syscall(__NR_copy_file_range,232fd_in,233off_in,234fd_out,235off_out,236len,237flags);238#else239return errno = ENOSYS, -1;240#endif241}242243244int uv__statx(int dirfd,245const char* path,246int flags,247unsigned int mask,248struct uv__statx* statxbuf) {249#if !defined(__NR_statx) || defined(__ANDROID_API__) && __ANDROID_API__ < 30250return errno = ENOSYS, -1;251#else252return syscall(__NR_statx, dirfd, path, flags, mask, statxbuf);253#endif254}255256257ssize_t uv__getrandom(void* buf, size_t buflen, unsigned flags) {258#if !defined(__NR_getrandom) || defined(__ANDROID_API__) && __ANDROID_API__ < 28259return errno = ENOSYS, -1;260#else261return syscall(__NR_getrandom, buf, buflen, flags);262#endif263}264265266