From c7a8cd21e90ab84be4caf5bd865ec90f17bd7017 Mon Sep 17 00:00:00 2001 From: Justine Tunney Date: Mon, 19 Sep 2022 15:01:48 -0700 Subject: [PATCH] Improve system call wrappers This change improves copy_file_range(), sendfile(), splice(), openpty(), closefrom(), close_range(), fadvise() and posix_fadvise() in addition to writing tests that confirm things like errno and seeking behavior across platforms. We now less aggressively polyfill behavior with some of these functions when the platform support isn't available. Please see: https://justine.lol/cosmopolitan/functions.html --- Makefile | 2 +- examples/script.c | 1 - libc/calls/_ptsname.c | 93 ++++++++++ libc/calls/calls.mk | 1 + libc/calls/close_range.c | 36 ++-- libc/calls/closefrom.c | 26 +-- libc/calls/copy_file_range.c | 115 +++++++++++++ libc/calls/fadvise-nt.c | 6 + libc/calls/fadvise.c | 37 +++- libc/calls/getgroups.c | 4 +- libc/calls/grantpt.c | 4 +- libc/calls/ioctl_tcsets.c | 19 +-- libc/calls/openpty.c | 40 +++-- libc/calls/poll.c | 6 +- libc/calls/posix_fadvise.c | 48 +++++- libc/calls/posix_openpt.c | 5 +- libc/calls/ppoll.c | 6 +- libc/calls/ptsname.c | 9 +- libc/calls/ptsname_r.c | 52 +----- libc/calls/rmdir.c | 2 +- libc/calls/setgroups.c | 4 +- libc/calls/splice.c | 98 ++++++----- libc/calls/syscall-sysv.internal.h | 11 +- libc/calls/syscall_support-sysv.internal.h | 6 +- libc/calls/tcgetattr.c | 2 +- libc/calls/termios.internal.h | 2 + libc/calls/termios2host.c | 32 ++++ libc/calls/tgkill.c | 6 +- libc/calls/unlockpt.c | 8 +- libc/intrin/describeflags.internal.h | 2 + .../copyfd.c => intrin/describeinoutint64.c} | 61 ++----- libc/intrin/pthread_mutex_trylock.c | 2 +- libc/intrin/pthread_once.c | 2 +- libc/intrin/restoretty.c | 4 +- libc/mem/copyfd.c | 11 -- libc/{calls => mem}/copyfd.internal.h | 0 libc/sock/sendfile.c | 140 ++++++++++----- libc/stdio/fputws.c | 2 +- libc/stdio/fputws_unlocked.c | 2 +- libc/sysv/calls/sys_fadvise.s | 2 +- libc/sysv/consts.sh | 5 +- libc/sysv/consts/TIOCPTSNAME.s | 2 + libc/sysv/consts/fio.h | 10 +- libc/sysv/consts/posix.h | 34 ++-- libc/sysv/consts/termios.h | 8 - libc/sysv/syscalls.sh | 4 +- libc/thread/pthread_attr_getguardsize.c | 3 +- libc/thread/pthread_attr_getstack.c | 6 +- libc/thread/pthread_attr_getstacksize.c | 4 +- libc/thread/pthread_attr_init.c | 4 +- libc/thread/pthread_attr_setguardsize.c | 2 +- libc/thread/pthread_attr_setinheritsched.c | 2 +- libc/thread/pthread_attr_setstack.c | 4 +- libc/thread/pthread_attr_setstacksize.c | 2 +- libc/thread/pthread_barrier_wait.c | 2 +- libc/thread/pthread_cond_broadcast.c | 2 +- libc/thread/pthread_cond_signal.c | 2 +- libc/thread/pthread_cond_timedwait.c | 4 +- libc/thread/pthread_cond_wait.c | 2 +- libc/thread/pthread_create.c | 4 +- libc/thread/pthread_getaffinity_np.c | 4 +- libc/thread/pthread_getname_np.c | 2 +- libc/thread/pthread_rwlock_rdlock.c | 2 +- libc/thread/pthread_rwlock_unlock.c | 2 +- libc/thread/pthread_rwlock_wrlock.c | 2 +- libc/thread/pthread_setaffinity_np.c | 4 +- libc/thread/pthread_setname_np.c | 2 +- libc/thread/pthread_spin_destroy.c | 2 +- libc/thread/pthread_spin_init.c | 2 +- libc/thread/pthread_spin_lock.c | 2 +- libc/thread/pthread_spin_trylock.c | 2 +- libc/thread/pthread_spin_unlock.c | 2 +- test/libc/calls/closefrom_test.c | 34 ++-- test/libc/calls/copy_file_range_test.c | 161 ++++++++++++++++++ test/libc/calls/pledge_test.c | 2 +- test/libc/calls/posix_fadvise_test.c | 106 ++++++++++++ test/libc/calls/splice_test.c | 81 +++++++++ test/libc/calls/unveil_test.c | 2 +- test/libc/intrin/asan_test.c | 3 +- test/libc/log/backtrace_test.c | 2 +- test/libc/sock/sendfile_test.c | 104 ++++++++--- test/tool/plinko/plinko_test.c | 2 +- third_party/zlib/insert_string.internal.h | 4 +- tool/build/ar.c | 2 +- tool/build/compile.c | 8 +- tool/build/cp.c | 2 +- tool/build/ocat.c | 2 +- tool/build/pledge.c | 2 +- tool/viz/printpeb.c | 5 +- 89 files changed, 1151 insertions(+), 414 deletions(-) create mode 100644 libc/calls/_ptsname.c create mode 100644 libc/calls/copy_file_range.c create mode 100644 libc/calls/termios2host.c rename libc/{calls/copyfd.c => intrin/describeinoutint64.c} (56%) rename libc/{calls => mem}/copyfd.internal.h (100%) create mode 100644 libc/sysv/consts/TIOCPTSNAME.s create mode 100644 test/libc/calls/copy_file_range_test.c create mode 100644 test/libc/calls/posix_fadvise_test.c create mode 100644 test/libc/calls/splice_test.c diff --git a/Makefile b/Makefile index 1b334601d..cd4191820 100644 --- a/Makefile +++ b/Makefile @@ -94,7 +94,7 @@ o/$(MODE): \ libc/disclaimer.inc \ rx:build/bootstrap \ rx:o/third_party/gcc \ - /proc/self/status \ + /proc/stat \ rw:/dev/null \ w:o/stack.log \ /etc/hosts \ diff --git a/examples/script.c b/examples/script.c index e738ffdee..e81138030 100644 --- a/examples/script.c +++ b/examples/script.c @@ -40,7 +40,6 @@ #include "libc/errno.h" #include "libc/fmt/conv.h" #include "libc/intrin/bswap.h" -#include "libc/intrin/kprintf.h" #include "libc/log/bsd.h" #include "libc/macros.internal.h" #include "libc/mem/mem.h" diff --git a/libc/calls/_ptsname.c b/libc/calls/_ptsname.c new file mode 100644 index 000000000..5a6809899 --- /dev/null +++ b/libc/calls/_ptsname.c @@ -0,0 +1,93 @@ +/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ +│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ +╞══════════════════════════════════════════════════════════════════════════════╡ +│ Copyright 2022 Justine Alexandra Roberts Tunney │ +│ │ +│ Permission to use, copy, modify, and/or distribute this software for │ +│ any purpose with or without fee is hereby granted, provided that the │ +│ above copyright notice and this permission notice appear in all copies. │ +│ │ +│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ +│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ +│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ +│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ +│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ +│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ +│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ +│ PERFORMANCE OF THIS SOFTWARE. │ +╚─────────────────────────────────────────────────────────────────────────────*/ +#include "libc/calls/syscall-sysv.internal.h" +#include "libc/calls/syscall_support-sysv.internal.h" +#include "libc/dce.h" +#include "libc/errno.h" +#include "libc/fmt/itoa.h" +#include "libc/str/str.h" +#include "libc/sysv/consts/termios.h" +#include "libc/sysv/errfuns.h" + +extern const unsigned FIODGNAME; +extern const unsigned TIOCPTSNAME; +extern const unsigned TIOCPTYGNAME; + +struct fiodgname_arg { + int len; + void *buf; +}; + +struct ptmget { + int cfd; + int sfd; + char cn[1024]; + char sn[1024]; +}; + +int _ptsname(int fd, char *buf, size_t size) { + int pty; + size_t n; + struct ptmget t; + + if (_isptmaster(fd)) { + return -1; + } + + t.sn[0] = '/'; + t.sn[1] = 'd'; + t.sn[2] = 'e'; + t.sn[3] = 'v'; + t.sn[4] = '/'; + t.sn[5] = 0; + + if (IsLinux()) { + if (sys_ioctl(fd, TIOCGPTN, &pty)) return -1; + t.sn[5] = 'p'; + t.sn[6] = 't'; + t.sn[7] = 's'; + t.sn[8] = '/'; + FormatInt32(t.sn + 9, pty); + } else if (IsXnu()) { + if (sys_ioctl(fd, TIOCPTYGNAME, t.sn)) { + return -1; + } + } else if (IsFreebsd()) { + struct fiodgname_arg fgn = {sizeof(t.sn) - 5, t.sn + 5}; + if (sys_ioctl(fd, FIODGNAME, &fgn) == -1) { + if (errno == EINVAL) { + errno = ERANGE; + } + return -1; + } + } else if (IsNetbsd()) { + if (sys_ioctl(fd, TIOCPTSNAME, &t)) { + return -1; + } + } else { + return enosys(); + } + + if ((n = strlen(t.sn)) < size) { + memcpy(buf, t.sn, n + 1); + return 0; + } else { + return erange(); + } +} diff --git a/libc/calls/calls.mk b/libc/calls/calls.mk index 10177d083..1cf0c1cc8 100644 --- a/libc/calls/calls.mk +++ b/libc/calls/calls.mk @@ -97,6 +97,7 @@ o/$(MODE)/libc/calls/ntcontext2linux.o: private \ # we always want -O3 because: # it makes the code size smaller too +o/$(MODE)/libc/calls/termios2host.o \ o/$(MODE)/libc/calls/sigenter-freebsd.o \ o/$(MODE)/libc/calls/sigenter-netbsd.o \ o/$(MODE)/libc/calls/sigenter-openbsd.o \ diff --git a/libc/calls/close_range.c b/libc/calls/close_range.c index 9d19965d2..308ee971b 100644 --- a/libc/calls/close_range.c +++ b/libc/calls/close_range.c @@ -17,10 +17,10 @@ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/calls/calls.h" -#include "libc/intrin/strace.internal.h" #include "libc/calls/syscall-sysv.internal.h" -#include "libc/errno.h" -#include "libc/limits.h" +#include "libc/dce.h" +#include "libc/intrin/strace.internal.h" +#include "libc/sysv/errfuns.h" /** * Closes inclusive range of file descriptors, e.g. @@ -32,33 +32,27 @@ * } * } * - * This is supported on Linux 5.9+, FreeBSD, and OpenBSD. On FreeBSD, - * `flags` must be zero. On OpenBSD, we call closefrom(int) so `last` - * should be `-1` in order to get OpenBSD support, otherwise `ENOSYS` - * will be returned. We also polyfill closefrom on FreeBSD since it's - * available on older kernels. + * The following flags are available: * - * On Linux, the following flags are supported: + * - `CLOSE_RANGE_UNSHARE` (Linux-only) + * - `CLOSE_RANGE_CLOEXEC` (Linux-only) * - * - CLOSE_RANGE_UNSHARE - * - CLOSE_RANGE_CLOEXEC + * This is only supported on Linux 5.9+ and FreeBSD 13+. Consider using + * closefrom() which will work on OpenBSD too. * * @return 0 on success, or -1 w/ errno - * @error ENOSYS if not Linux 5.9+ / FreeBSD / OpenBSD - * @error EBADF on OpenBSD if `first` is greater than highest fd * @error EINVAL if flags are bad or first is greater than last * @error EMFILE if a weird race condition happens on Linux - * @error EINTR possibly on OpenBSD + * @error ENOSYS if not Linux 5.9+ or FreeBSD 13+ * @error ENOMEM on Linux maybe + * @see closefrom() */ int close_range(unsigned int first, unsigned int last, unsigned int flags) { - int rc, err; - err = errno; - if ((rc = sys_close_range(first, last, flags)) == -1) { - if (errno == ENOSYS && first <= INT_MAX && last == UINT_MAX && !flags) { - errno = err; - rc = sys_closefrom(first); - } + int rc; + if (IsLinux() || IsFreebsd()) { + rc = sys_close_range(first, last, flags); + } else { + rc = enosys(); } STRACE("close_range(%d, %d, %#x) → %d% m", first, last, flags, rc); return rc; diff --git a/libc/calls/closefrom.c b/libc/calls/closefrom.c index ec02716f4..fd468f583 100644 --- a/libc/calls/closefrom.c +++ b/libc/calls/closefrom.c @@ -17,9 +17,10 @@ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/calls/calls.h" -#include "libc/intrin/strace.internal.h" #include "libc/calls/syscall-sysv.internal.h" +#include "libc/dce.h" #include "libc/errno.h" +#include "libc/intrin/strace.internal.h" #include "libc/limits.h" #include "libc/sysv/errfuns.h" @@ -34,26 +35,27 @@ * } * * @return 0 on success, or -1 w/ errno - * @error ENOSYS if not Linux 5.9+ / FreeBSD / OpenBSD + * @error EBADF if `first` is negative * @error EBADF on OpenBSD if `first` is greater than highest fd * @error EINVAL if flags are bad or first is greater than last * @error EMFILE if a weird race condition happens on Linux + * @error ENOSYS if not Linux 5.9+, FreeBSD 8+, or OpenBSD * @error EINTR possibly on OpenBSD * @error ENOMEM on Linux maybe - * @note supported on Linux 5.9+, FreeBSD 8+, and OpenBSD */ int closefrom(int first) { int rc, err; - if (first >= 0) { - err = errno; - if ((rc = sys_close_range(first, -1, 0)) == -1) { - if (errno == ENOSYS) { - errno = err; - rc = sys_closefrom(first); - } - } - } else { + if (IsNetbsd() || IsWindows() || IsMetal()) { + rc = enosys(); + } else if (first < 0) { + // consistent with openbsd + // freebsd allows this but it's dangerous + // necessary on linux due to type signature rc = ebadf(); + } else if (IsLinux()) { + rc = sys_close_range(first, 0xffffffffu, 0); + } else { + rc = sys_closefrom(first); } STRACE("closefrom(%d) → %d% m", first, rc); return rc; diff --git a/libc/calls/copy_file_range.c b/libc/calls/copy_file_range.c new file mode 100644 index 000000000..708df3977 --- /dev/null +++ b/libc/calls/copy_file_range.c @@ -0,0 +1,115 @@ +/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ +│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ +╞══════════════════════════════════════════════════════════════════════════════╡ +│ Copyright 2022 Justine Alexandra Roberts Tunney │ +│ │ +│ Permission to use, copy, modify, and/or distribute this software for │ +│ any purpose with or without fee is hereby granted, provided that the │ +│ above copyright notice and this permission notice appear in all copies. │ +│ │ +│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ +│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ +│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ +│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ +│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ +│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ +│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ +│ PERFORMANCE OF THIS SOFTWARE. │ +╚─────────────────────────────────────────────────────────────────────────────*/ +#include "libc/calls/calls.h" +#include "libc/calls/struct/sigset.h" +#include "libc/calls/struct/sigset.internal.h" +#include "libc/calls/syscall-sysv.internal.h" +#include "libc/dce.h" +#include "libc/errno.h" +#include "libc/intrin/asan.internal.h" +#include "libc/intrin/describeflags.internal.h" +#include "libc/intrin/strace.internal.h" +#include "libc/sysv/consts/sig.h" +#include "libc/sysv/errfuns.h" +#include "libc/thread/thread.h" + +static struct CopyFileRange { + pthread_once_t once; + bool ok; +} g_copy_file_range; + +static bool HasCopyFileRange(void) { + bool ok; + int e, rc; + e = errno; + if (IsLinux()) { + // We modernize our detection by a few years for simplicity. + // This system call is chosen since it's listed by pledge(). + // https://www.cygwin.com/bugzilla/show_bug.cgi?id=26338 + ok = sys_close_range(-1, -2, 0) == -1 && errno == EINVAL; + } else if (IsFreebsd()) { + ok = sys_copy_file_range(-1, 0, -1, 0, 0, 0) == -1 && errno == EBADF; + } else { + ok = false; + } + errno = e; + return ok; +} + +static void copy_file_range_init(void) { + g_copy_file_range.ok = HasCopyFileRange(); +} + +/** + * Transfers data between files. + * + * If this system call is available (Linux c. 2018 or FreeBSD c. 2021) + * and the file system supports it (e.g. ext4) and the source and dest + * files are on the same file system, then this system call shall make + * copies go about 2x faster. + * + * This implementation requires Linux 5.9+ even though the system call + * was introduced in Linux 4.5. That's to ensure ENOSYS works reliably + * due to a faulty backport, that happened in RHEL7. FreeBSD detection + * on the other hand will work fine. + * + * @param infd is source file, which should be on same file system + * @param opt_in_out_inoffset may be specified for pread() behavior + * @param outfd should be a writable file, but not `O_APPEND` + * @param opt_in_out_outoffset may be specified for pwrite() behavior + * @param uptobytes is maximum number of bytes to transfer + * @param flags is reserved for future use and must be zero + * @return number of bytes transferred, or -1 w/ errno + * @raise EXDEV if source and destination are on different filesystems + * @raise EBADF if `infd` or `outfd` aren't open files or append-only + * @raise EPERM if `fdout` refers to an immutable file on Linux + * @raise EINVAL if ranges overlap or `flags` is non-zero + * @raise EFBIG if `setrlimit(RLIMIT_FSIZE)` is exceeded + * @raise EFAULT if one of the pointers memory is bad + * @raise ERANGE if overflow happens computing ranges + * @raise ENOSPC if file system has run out of space + * @raise ETXTBSY if source or dest is a swap file + * @raise EINTR if a signal was delivered instead + * @raise EISDIR if source or dest is a directory + * @raise ENOSYS if not Linux 5.9+ or FreeBSD 13+ + * @raise EIO if a low-level i/o error happens + * @see sendfile() for seekable → socket + * @see splice() for fd ↔ pipe + */ +ssize_t copy_file_range(int infd, int64_t *opt_in_out_inoffset, int outfd, + int64_t *opt_in_out_outoffset, size_t uptobytes, + uint32_t flags) { + ssize_t rc; + pthread_once(&g_copy_file_range.once, copy_file_range_init); + if (!g_copy_file_range.ok) { + rc = enosys(); + } else if (IsAsan() && ((opt_in_out_inoffset && + !__asan_is_valid(opt_in_out_inoffset, 8)) || + (opt_in_out_outoffset && + !__asan_is_valid(opt_in_out_outoffset, 8)))) { + rc = efault(); + } else { + rc = sys_copy_file_range(infd, opt_in_out_inoffset, outfd, + opt_in_out_outoffset, uptobytes, flags); + } + STRACE("copy_file_range(%d, %s, %d, %s, %'zu, %#x) → %'ld% m", infd, + DescribeInOutInt64(rc, opt_in_out_inoffset), outfd, + DescribeInOutInt64(rc, opt_in_out_outoffset), uptobytes, flags); + return rc; +} diff --git a/libc/calls/fadvise-nt.c b/libc/calls/fadvise-nt.c index 5fe478ad7..2e1c9c7ca 100644 --- a/libc/calls/fadvise-nt.c +++ b/libc/calls/fadvise-nt.c @@ -21,6 +21,7 @@ #include "libc/calls/syscall_support-nt.internal.h" #include "libc/nt/createfile.h" #include "libc/nt/enum/fileflagandattributes.h" +#include "libc/nt/enum/filetype.h" #include "libc/nt/files.h" #include "libc/nt/runtime.h" #include "libc/sysv/consts/madv.h" @@ -33,6 +34,7 @@ textwindows int sys_fadvise_nt(int fd, uint64_t offset, uint64_t len, int rc, flags, mode; uint32_t perm, share, attr; + if ((int64_t)len < 0) return einval(); if (!__isfdkind(fd, kFdFile)) return ebadf(); h1 = g_fds.p[fd].handle; mode = g_fds.p[fd].mode; @@ -57,6 +59,10 @@ textwindows int sys_fadvise_nt(int fd, uint64_t offset, uint64_t len, return -1; } + if (GetFileType(h1) == kNtFileTypePipe) { + return espipe(); + } + // MSDN says only these are allowed, otherwise it returns EINVAL. attr &= kNtFileFlagBackupSemantics | kNtFileFlagDeleteOnClose | kNtFileFlagNoBuffering | kNtFileFlagOpenNoRecall | diff --git a/libc/calls/fadvise.c b/libc/calls/fadvise.c index acf3f847d..265fa103c 100644 --- a/libc/calls/fadvise.c +++ b/libc/calls/fadvise.c @@ -16,11 +16,17 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ +#include "libc/assert.h" #include "libc/calls/calls.h" -#include "libc/intrin/strace.internal.h" #include "libc/calls/syscall-nt.internal.h" #include "libc/calls/syscall-sysv.internal.h" #include "libc/dce.h" +#include "libc/errno.h" +#include "libc/intrin/strace.internal.h" +#include "libc/str/str.h" +#include "libc/sysv/errfuns.h" + +int sys_fadvise_netbsd(int, int, int64_t, int64_t, int) asm("sys_fadvise"); /** * Drops hints to O/S about intended I/O behavior. @@ -28,16 +34,33 @@ * It makes a huge difference. For example, when copying a large file, * it can stop the system from persisting GBs of useless memory content. * - * @param len 0 means ‘til end of file + * @param len 0 means until end of file * @param advice can be MADV_SEQUENTIAL, MADV_RANDOM, etc. - * @return -1 on error + * @return 0 on success, or -1 w/ errno + * @raise EBADF if `fd` isn't a valid file descriptor + * @raise ESPIPE if `fd` refers to a pipe + * @raise EINVAL if `advice` was invalid + * @raise ENOSYS on XNU and OpenBSD */ int fadvise(int fd, uint64_t offset, uint64_t len, int advice) { - int rc; - if (!IsWindows()) { - rc = sys_fadvise(fd, offset, len, advice); /* linux & freebsd */ - } else { + int rc, e = errno; + if (IsLinux()) { + rc = sys_fadvise(fd, offset, len, advice); + } else if (IsFreebsd() || IsNetbsd()) { + if (IsFreebsd()) { + rc = sys_fadvise(fd, offset, len, advice); + } else { + rc = sys_fadvise_netbsd(fd, offset, offset, len, advice); + } + _npassert(rc >= 0); + if (rc) { + errno = rc; + rc = -1; + } + } else if (IsWindows()) { rc = sys_fadvise_nt(fd, offset, len, advice); + } else { + rc = enosys(); } STRACE("fadvise(%d, %'lu, %'lu, %d) → %d% m", fd, offset, len, advice, rc); return rc; diff --git a/libc/calls/getgroups.c b/libc/calls/getgroups.c index e69f5f642..04d5e24f2 100644 --- a/libc/calls/getgroups.c +++ b/libc/calls/getgroups.c @@ -33,7 +33,9 @@ */ int getgroups(int size, uint32_t list[]) { int rc; - if (IsAsan() && size && !__asan_is_valid(list, size * sizeof(list[0]))) { + size_t n; + if (IsAsan() && (__builtin_mul_overflow(size, sizeof(list[0]), &n) || + !__asan_is_valid(list, n))) { rc = efault(); } else if (IsLinux() || IsNetbsd() || IsOpenbsd() || IsFreebsd() || IsXnu()) { rc = sys_getgroups(size, list); diff --git a/libc/calls/grantpt.c b/libc/calls/grantpt.c index 697d22886..9e221efdd 100644 --- a/libc/calls/grantpt.c +++ b/libc/calls/grantpt.c @@ -17,10 +17,12 @@ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/calls/syscall-sysv.internal.h" +#include "libc/calls/syscall_support-sysv.internal.h" #include "libc/calls/termios.h" #include "libc/dce.h" #include "libc/intrin/strace.internal.h" -#include "libc/sysv/consts/termios.h" + +extern const unsigned TIOCPTYGRANT; /** * Grants access to subordinate pseudoteletypewriter. diff --git a/libc/calls/ioctl_tcsets.c b/libc/calls/ioctl_tcsets.c index ca2886bba..94099bd14 100644 --- a/libc/calls/ioctl_tcsets.c +++ b/libc/calls/ioctl_tcsets.c @@ -29,7 +29,7 @@ #include "libc/sysv/consts/termios.h" #include "libc/sysv/errfuns.h" -void __on_ioctl_tcsets(void); +void __on_ioctl_tcsets(int); int ioctl_tcsets_nt(int, uint64_t, const struct termios *); static int ioctl_tcsets_metal(int fd, uint64_t request, @@ -37,19 +37,6 @@ static int ioctl_tcsets_metal(int fd, uint64_t request, return 0; } -static inline void *__termios2host(union metatermios *mt, - const struct termios *lt) { - if (!IsXnu() && !IsFreebsd() && !IsOpenbsd() && !IsNetbsd()) { - return (/*unconst*/ void *)lt; - } else if (IsXnu()) { - COPY_TERMIOS(&mt->xnu, lt); - return &mt->xnu; - } else { - COPY_TERMIOS(&mt->bsd, lt); - return &mt->bsd; - } -} - static int ioctl_tcsets_sysv(int fd, uint64_t request, const struct termios *tio) { union metatermios mt; @@ -72,9 +59,9 @@ int ioctl_tcsets(int fd, uint64_t request, ...) { va_start(va, request); tio = va_arg(va, const struct termios *); va_end(va); - if (_weaken(__on_ioctl_tcsets)) { + if (0 <= fd && fd <= 2 && _weaken(__on_ioctl_tcsets)) { if (!once) { - _weaken(__on_ioctl_tcsets)(); + _weaken(__on_ioctl_tcsets)(fd); once = true; } } diff --git a/libc/calls/openpty.c b/libc/calls/openpty.c index e48548b82..779512cf3 100644 --- a/libc/calls/openpty.c +++ b/libc/calls/openpty.c @@ -16,18 +16,25 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ +#include "libc/assert.h" #include "libc/calls/calls.h" #include "libc/calls/ioctl.h" +#include "libc/calls/struct/metatermios.internal.h" #include "libc/calls/struct/termios.h" #include "libc/calls/struct/winsize.h" +#include "libc/calls/syscall-sysv.internal.h" +#include "libc/calls/syscall_support-sysv.internal.h" #include "libc/calls/termios.h" +#include "libc/calls/termios.internal.h" #include "libc/dce.h" +#include "libc/intrin/asan.internal.h" #include "libc/intrin/kprintf.h" #include "libc/log/rop.h" #include "libc/str/str.h" #include "libc/sysv/consts/o.h" #include "libc/sysv/consts/pty.h" #include "libc/sysv/consts/termios.h" +#include "libc/sysv/errfuns.h" struct IoctlPtmGet { int m; @@ -49,28 +56,37 @@ struct IoctlPtmGet { int openpty(int *mfd, int *sfd, char *name, const struct termios *tio, const struct winsize *wsz) { int m, s, p; - const char *t; - struct IoctlPtmGet ptm; + union metatermios mt; + struct IoctlPtmGet t; + if (IsWindows() || IsMetal()) { + return enosys(); + } + if (IsAsan() && (!__asan_is_valid(mfd, sizeof(int)) || + !__asan_is_valid(sfd, sizeof(int)) || + (name && !__asan_is_valid(name, 16)) || + (tio && !__asan_is_valid(tio, sizeof(*tio))) || + (wsz && !__asan_is_valid(wsz, sizeof(*wsz))))) { + return efault(); + } RETURN_ON_ERROR((m = posix_openpt(O_RDWR | O_NOCTTY))); if (!IsOpenbsd()) { RETURN_ON_ERROR(grantpt(m)); RETURN_ON_ERROR(unlockpt(m)); - if (!(t = ptsname(m))) goto OnError; - RETURN_ON_ERROR((s = open(t, O_RDWR))); + RETURN_ON_ERROR(_ptsname(m, t.sname, sizeof(t.sname))); + RETURN_ON_ERROR((s = sys_open(t.sname, O_RDWR, 0))); } else { - RETURN_ON_ERROR(ioctl(m, PTMGET, &ptm)); + RETURN_ON_ERROR(sys_ioctl(m, PTMGET, &t)); close(m); - m = ptm.m; - s = ptm.s; - t = ptm.sname; + m = t.m; + s = t.s; } *mfd = m; *sfd = s; - if (name) strcpy(name, t); - if (tio) ioctl(s, TCSETSF, tio); - if (wsz) ioctl(s, TIOCSWINSZ, wsz); + if (name) strcpy(name, t.sname); + if (tio) _npassert(!sys_ioctl(s, TCSETSF, __termios2host(&mt, tio))); + if (wsz) _npassert(!sys_ioctl(s, TIOCGWINSZ, wsz)); return 0; OnError: - if (m != -1) close(m); + if (m != -1) sys_close(m); return -1; } diff --git a/libc/calls/poll.c b/libc/calls/poll.c index 52799046b..18fff3b5c 100644 --- a/libc/calls/poll.c +++ b/libc/calls/poll.c @@ -17,13 +17,13 @@ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/calls/calls.h" -#include "libc/intrin/strace.internal.h" #include "libc/dce.h" #include "libc/errno.h" #include "libc/intrin/asan.internal.h" #include "libc/intrin/describeflags.internal.h" #include "libc/intrin/kprintf.h" #include "libc/intrin/likely.h" +#include "libc/intrin/strace.internal.h" #include "libc/macros.internal.h" #include "libc/sock/internal.h" #include "libc/sock/sock.h" @@ -68,10 +68,12 @@ * @norestart */ int poll(struct pollfd *fds, size_t nfds, int timeout_ms) { + size_t n; int i, rc; uint64_t millis; - if (IsAsan() && !__asan_is_valid(fds, nfds * sizeof(struct pollfd))) { + if (IsAsan() && (__builtin_mul_overflow(nfds, sizeof(struct pollfd), &n) || + !__asan_is_valid(fds, n))) { rc = efault(); } else if (!IsWindows()) { if (!IsMetal()) { diff --git a/libc/calls/posix_fadvise.c b/libc/calls/posix_fadvise.c index f9fceaf51..c37861695 100644 --- a/libc/calls/posix_fadvise.c +++ b/libc/calls/posix_fadvise.c @@ -16,8 +16,52 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ +#include "libc/assert.h" #include "libc/calls/calls.h" +#include "libc/calls/syscall-nt.internal.h" +#include "libc/calls/syscall-sysv.internal.h" +#include "libc/dce.h" +#include "libc/errno.h" +#include "libc/intrin/strace.internal.h" +#include "libc/str/str.h" +#include "libc/sysv/errfuns.h" -int(posix_fadvise)(int fd, uint64_t offset, uint64_t len, int advice) { - return fadvise(fd, offset, len, advice); +int sys_fadvise_netbsd(int, int, int64_t, int64_t, int) asm("sys_fadvise"); + +/** + * Drops hints to O/S about intended I/O behavior. + * + * It makes a huge difference. For example, when copying a large file, + * it can stop the system from persisting GBs of useless memory content. + * + * @param len 0 means until end of file + * @param advice can be POSIX_FADV_SEQUENTIAL, POSIX_FADV_RANDOM, etc. + * @return 0 on success, or errno on error + * @raise EBADF if `fd` isn't a valid file descriptor + * @raise EINVAL if `advice` is invalid or `len` is huge + * @raise ESPIPE if `fd` refers to a pipe + * @raise ENOSYS on XNU and OpenBSD + */ +errno_t posix_fadvise(int fd, uint64_t offset, uint64_t len, int advice) { + int rc, e = errno; + if (IsLinux()) { + rc = sys_fadvise(fd, offset, len, advice); + } else if (IsFreebsd()) { + rc = sys_fadvise(fd, offset, len, advice); + _unassert(rc >= 0); + } else if (IsNetbsd()) { + rc = sys_fadvise_netbsd(fd, offset, offset, len, advice); + _unassert(rc >= 0); + } else if (IsWindows()) { + rc = sys_fadvise_nt(fd, offset, len, advice); + } else { + rc = enosys(); + } + if (rc == -1) { + rc = errno; + errno = e; + } + STRACE("posix_fadvise(%d, %'lu, %'lu, %d) → %s", fd, offset, len, advice, + !rc ? "0" : _strerrno(rc)); + return rc; } diff --git a/libc/calls/posix_openpt.c b/libc/calls/posix_openpt.c index 1759287bd..3b5333ef1 100644 --- a/libc/calls/posix_openpt.c +++ b/libc/calls/posix_openpt.c @@ -20,6 +20,7 @@ #include "libc/calls/termios.h" #include "libc/dce.h" #include "libc/errno.h" +#include "libc/intrin/describeflags.internal.h" #include "libc/intrin/strace.internal.h" #include "libc/sysv/consts/o.h" #include "libc/sysv/errfuns.h" @@ -35,7 +36,7 @@ int posix_openpt(int flags) { int rc; if ((flags & O_ACCMODE) != O_RDWR) { rc = einval(); - } else if (IsLinux() || IsXnu()) { + } else if (IsLinux() || IsXnu() || IsNetbsd()) { rc = sys_open("/dev/ptmx", flags, 0); } else if (IsOpenbsd()) { rc = sys_open("/dev/ptm", flags, 0); @@ -45,6 +46,6 @@ int posix_openpt(int flags) { } else { rc = enosys(); } - STRACE("posix_openpt(%#o) → %d% m", flags, rc); + STRACE("posix_openpt(%s) → %d% m", DescribeOpenFlags(flags), rc); return rc; } diff --git a/libc/calls/ppoll.c b/libc/calls/ppoll.c index 8065c364c..969ff63f4 100644 --- a/libc/calls/ppoll.c +++ b/libc/calls/ppoll.c @@ -17,13 +17,13 @@ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/calls/calls.h" -#include "libc/intrin/strace.internal.h" #include "libc/calls/struct/sigset.internal.h" #include "libc/calls/struct/timespec.h" #include "libc/dce.h" #include "libc/errno.h" #include "libc/intrin/asan.internal.h" #include "libc/intrin/kprintf.h" +#include "libc/intrin/strace.internal.h" #include "libc/macros.internal.h" #include "libc/sock/struct/pollfd.h" #include "libc/sock/struct/pollfd.internal.h" @@ -57,12 +57,14 @@ */ int ppoll(struct pollfd *fds, size_t nfds, const struct timespec *timeout, const sigset_t *sigmask) { + size_t n; int e, i, rc; uint64_t millis; sigset_t oldmask; struct timespec ts, *tsp; - if (IsAsan() && (!__asan_is_valid(fds, nfds * sizeof(struct pollfd)) || + if (IsAsan() && (__builtin_mul_overflow(nfds, sizeof(struct pollfd), &n) || + !__asan_is_valid(fds, n) || (timeout && !__asan_is_valid(timeout, sizeof(timeout))) || (sigmask && !__asan_is_valid(sigmask, sizeof(sigmask))))) { rc = efault(); diff --git a/libc/calls/ptsname.c b/libc/calls/ptsname.c index 7dfccca41..5102b6f4a 100644 --- a/libc/calls/ptsname.c +++ b/libc/calls/ptsname.c @@ -16,11 +16,12 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ +#include "libc/calls/syscall_support-sysv.internal.h" #include "libc/calls/termios.h" #include "libc/errno.h" #include "libc/intrin/strace.internal.h" -static char g_ptsname[256]; +static char g_ptsname[16]; /** * Gets name subordinate pseudoteletypewriter. @@ -29,13 +30,11 @@ static char g_ptsname[256]; */ char *ptsname(int fd) { char *res; - errno_t e; - if (!(e = ptsname_r(fd, g_ptsname, sizeof(g_ptsname)))) { + if (!_ptsname(fd, g_ptsname, sizeof(g_ptsname))) { res = g_ptsname; } else { - errno = e; res = 0; } - STRACE("ptsname(%d) → %s% m", fd, res); + STRACE("ptsname(%d) → %#s% m", fd, res); return res; } diff --git a/libc/calls/ptsname_r.c b/libc/calls/ptsname_r.c index be373d9d6..9c0ab77b5 100644 --- a/libc/calls/ptsname_r.c +++ b/libc/calls/ptsname_r.c @@ -16,57 +16,9 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/calls/calls.h" -#include "libc/calls/syscall-sysv.internal.h" +#include "libc/calls/syscall_support-sysv.internal.h" #include "libc/calls/termios.h" -#include "libc/dce.h" #include "libc/errno.h" -#include "libc/fmt/itoa.h" -#include "libc/paths.h" -#include "libc/str/str.h" -#include "libc/sysv/consts/termios.h" -#include "libc/sysv/errfuns.h" - -struct fiodgname_arg { - int len; - void *buf; -}; - -static int PtsName(int fd, char *buf, size_t size) { - if (size < 9 + 12) return erange(); - if (_isptmaster(fd)) return -1; - - if (IsLinux()) { - int pty; - if (sys_ioctl(fd, TIOCGPTN, &pty)) return -1; - buf[0] = '/', buf[1] = 'd', buf[2] = 'e', buf[3] = 'v'; - buf[4] = '/', buf[5] = 'p', buf[6] = 't', buf[7] = 's'; - buf[8] = '/', FormatInt32(buf + 9, pty); - return 0; - } - - if (IsFreebsd()) { - struct fiodgname_arg fgn = {size - 5, buf + 5}; - buf[0] = '/', buf[1] = 'd'; - buf[2] = 'e', buf[3] = 'v'; - buf[4] = '/', buf[5] = 0; - if (sys_ioctl(fd, FIODGNAME, &fgn) == -1) { - if (errno == EINVAL) errno = ERANGE; - return -1; - } - return 0; - } - - if (IsXnu()) { - char b2[128]; - if (sys_ioctl(fd, TIOCPTYGNAME, b2)) return -1; - if (strlen(b2) + 1 > size) return erange(); - strcpy(buf, b2); - return 0; - } - - return enosys(); -} /** * Gets name subordinate pseudoteletypewriter. @@ -75,7 +27,7 @@ static int PtsName(int fd, char *buf, size_t size) { */ errno_t ptsname_r(int fd, char *buf, size_t size) { int rc, e = errno; - if (!PtsName(fd, buf, size)) { + if (!_ptsname(fd, buf, size)) { rc = 0; } else { rc = errno; diff --git a/libc/calls/rmdir.c b/libc/calls/rmdir.c index c2f935187..1695d6660 100644 --- a/libc/calls/rmdir.c +++ b/libc/calls/rmdir.c @@ -22,7 +22,7 @@ /** * Deletes empty directory. * - * @return 0 on success or -1 w/ errno on error + * @return 0 on success, or -1 w/ errno */ int rmdir(const char *path) { return unlinkat(AT_FDCWD, path, AT_REMOVEDIR); diff --git a/libc/calls/setgroups.c b/libc/calls/setgroups.c index ace1f08d1..80d267c15 100644 --- a/libc/calls/setgroups.c +++ b/libc/calls/setgroups.c @@ -39,7 +39,9 @@ */ int setgroups(size_t size, const uint32_t list[]) { int rc; - if (IsAsan() && size && !__asan_is_valid(list, size * sizeof(list[0]))) { + size_t n; + if (IsAsan() && (__builtin_mul_overflow(size, sizeof(list[0]), &n) || + !__asan_is_valid(list, n))) { rc = efault(); } else if (IsLinux() || IsNetbsd() || IsOpenbsd() || IsFreebsd() || IsXnu()) { rc = sys_setgroups(size, list); diff --git a/libc/calls/splice.c b/libc/calls/splice.c index 35f8a0710..4f7d6a39a 100644 --- a/libc/calls/splice.c +++ b/libc/calls/splice.c @@ -17,58 +17,80 @@ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/calls/calls.h" -#include "libc/calls/internal.h" #include "libc/calls/syscall-sysv.internal.h" #include "libc/dce.h" #include "libc/errno.h" +#include "libc/fmt/itoa.h" +#include "libc/intrin/asan.internal.h" +#include "libc/intrin/describeflags.internal.h" +#include "libc/intrin/strace.internal.h" +#include "libc/mem/alloca.h" +#include "libc/str/str.h" #include "libc/sysv/errfuns.h" +#include "libc/thread/thread.h" -static ssize_t splicer(int infd, int64_t *inoffset, int outfd, - int64_t *outoffset, size_t uptobytes, uint32_t flags, - int64_t impl(int infd, int64_t *inoffset, int outfd, - int64_t *outoffset, size_t uptobytes, - uint32_t flags)) { - int olderr; - ssize_t transferred; - if (!uptobytes || flags == -1) return einval(); - if (IsModeDbg() && uptobytes > 1) uptobytes >>= 1; - olderr = errno; - if (__isfdkind(infd, kFdZip) || __isfdkind(outfd, kFdZip) || - (transferred = - impl(infd, inoffset, outfd, outoffset, uptobytes, flags)) == -1 && - errno == ENOSYS) { - errno = olderr; - transferred = copyfd(infd, inoffset, outfd, outoffset, uptobytes, flags); +static struct Splice { + pthread_once_t once; + bool ok; +} g_splice; + +static bool HasSplice(void) { + bool ok; + int e, rc; + e = errno; + if (IsLinux()) { + // Our testing indicates splice() doesn't work as documneted on + // RHEL5 and RHEL7 so let's require a modern kernel to be safe. + // We choose close_range() for this since it's listed by pledge + ok = sys_close_range(-1, -2, 0) == -1 && errno == EINVAL; + } else { + ok = false; } - return transferred; + errno = e; + return ok; +} + +static void splice_init(void) { + g_splice.ok = HasSplice(); } /** * Transfers data to/from pipe. * * @param flags can have SPLICE_F_{MOVE,NONBLOCK,MORE,GIFT} + * @param opt_in_out_inoffset may be specified if `infd` isn't a pipe and is + * used as both an input and output parameter for pread() behavior + * @param opt_in_out_outoffset may be specified if `outfd` isn't a pipe and is + * used as both an input and output parameter for pwrite() behavior * @return number of bytes transferred, 0 on input end, or -1 w/ errno + * @raise EBADF if `infd` or `outfd` aren't open files or append-only + * @raise ESPIPE if an offset arg was specified for a pipe fd + * @raise EINVAL if offset was given for non-seekable device + * @raise EINVAL if file system doesn't support splice() + * @raise EFAULT if one of the pointers memory is bad + * @raise EINVAL if `flags` is invalid + * @raise ENOSYS if not Linux 5.9+ * @see copy_file_range() for file ↔ file * @see sendfile() for seekable → socket */ -ssize_t splice(int infd, int64_t *inopt_out_inoffset, int outfd, - int64_t *inopt_out_outoffset, size_t uptobytes, uint32_t flags) { - return splicer(infd, inopt_out_inoffset, outfd, inopt_out_outoffset, - uptobytes, flags, sys_splice); -} - -/** - * Transfers data between files. - * - * @param outfd should be a writable file, but not O_APPEND - * @param flags is reserved for future use - * @return number of bytes actually copied, or -1 w/ errno - * @see sendfile() for seekable → socket - * @see splice() for fd ↔ pipe - */ -ssize_t copy_file_range(int infd, int64_t *inopt_out_inoffset, int outfd, - int64_t *inopt_out_outoffset, size_t uptobytes, - uint32_t flags) { - return splicer(infd, inopt_out_inoffset, outfd, inopt_out_outoffset, - uptobytes, flags, sys_copy_file_range); +ssize_t splice(int infd, int64_t *opt_in_out_inoffset, int outfd, + int64_t *opt_in_out_outoffset, size_t uptobytes, + uint32_t flags) { + ssize_t rc; + pthread_once(&g_splice.once, splice_init); + if (!g_splice.ok) { + rc = enosys(); + } else if (IsAsan() && ((opt_in_out_inoffset && + !__asan_is_valid(opt_in_out_inoffset, 8)) || + (opt_in_out_outoffset && + !__asan_is_valid(opt_in_out_outoffset, 8)))) { + rc = efault(); + } else { + rc = sys_splice(infd, opt_in_out_inoffset, outfd, opt_in_out_outoffset, + uptobytes, flags); + } + STRACE("splice(%d, %s, %d, %s, %'zu, %#x) → %'ld% m", infd, + DescribeInOutInt64(rc, opt_in_out_inoffset), outfd, + DescribeInOutInt64(rc, opt_in_out_outoffset), uptobytes, flags); + return rc; } diff --git a/libc/calls/syscall-sysv.internal.h b/libc/calls/syscall-sysv.internal.h index 11f0bbdc8..7264cd0f0 100644 --- a/libc/calls/syscall-sysv.internal.h +++ b/libc/calls/syscall-sysv.internal.h @@ -53,9 +53,9 @@ i32 sys_getpgid(i32) hidden; i32 sys_getpgrp(void) hidden; i32 sys_getppid(void) hidden; i32 sys_getpriority(i32, u32) hidden; -i32 sys_getresgid(u32 *, u32 *, u32 *); -i32 sys_getresuid(u32 *, u32 *, u32 *); -i32 sys_getsid(int) hidden; +i32 sys_getresgid(u32 *, u32 *, u32 *) hidden; +i32 sys_getresuid(u32 *, u32 *, u32 *) hidden; +i32 sys_getsid(i32) hidden; i32 sys_gettid(void) hidden; i32 sys_ioctl(i32, u64, ...) hidden; i32 sys_issetugid(void) hidden; @@ -112,11 +112,10 @@ i64 sys_pread(i32, void *, u64, i64, i64) hidden; i64 sys_pwrite(i32, const void *, u64, i64, i64) hidden; i64 sys_read(i32, void *, u64) hidden; i64 sys_readlink(const char *, char *, u64) hidden; -i64 sys_readlinkat(int, const char *, char *, u64) hidden; +i64 sys_readlinkat(i32, const char *, char *, u64) hidden; i64 sys_sendfile(i32, i32, i64 *, u64) hidden; i64 sys_splice(i32, i64 *, i32, i64 *, u64, u32) hidden; i64 sys_write(i32, const void *, u64) hidden; -int _isptmaster(int); u32 sys_getegid(void) hidden; u32 sys_geteuid(void) hidden; u32 sys_getgid(void) hidden; @@ -124,7 +123,7 @@ u32 sys_getuid(void) hidden; u32 sys_umask(u32) hidden; void *__sys_mmap(void *, u64, u32, u32, i64, i64, i64) hidden; void *sys_mremap(void *, u64, u64, i32, void *) hidden; -void sys_exit(int) hidden; +void sys_exit(i32) hidden; #undef i32 #undef i64 diff --git a/libc/calls/syscall_support-sysv.internal.h b/libc/calls/syscall_support-sysv.internal.h index 8b2b41f68..b75389757 100644 --- a/libc/calls/syscall_support-sysv.internal.h +++ b/libc/calls/syscall_support-sysv.internal.h @@ -20,8 +20,10 @@ void *__vdsosym(const char *, const char *) hidden; void __onfork(void) hidden; void __restore_rt() hidden; void __restore_rt_netbsd(void) hidden; -void cosmo2flock(uintptr_t); -void flock2cosmo(uintptr_t); +void cosmo2flock(uintptr_t) hidden; +void flock2cosmo(uintptr_t) hidden; +int _ptsname(int, char *, size_t) hidden; +int _isptmaster(int) hidden; COSMOPOLITAN_C_END_ #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ diff --git a/libc/calls/tcgetattr.c b/libc/calls/tcgetattr.c index e83c2d6d5..28f3eddd1 100644 --- a/libc/calls/tcgetattr.c +++ b/libc/calls/tcgetattr.c @@ -51,7 +51,7 @@ * * @param fd open file descriptor that isatty() * @param tio is where result is stored - * @return -1 w/ errno on error + * @return 0 on success, or -1 w/ errno * @asyncsignalsafe */ int(tcgetattr)(int fd, struct termios *tio) { diff --git a/libc/calls/termios.internal.h b/libc/calls/termios.internal.h index 1e4b46ac5..21efaf2bf 100644 --- a/libc/calls/termios.internal.h +++ b/libc/calls/termios.internal.h @@ -27,4 +27,6 @@ (TO)->c_ospeed = c_ospeed; \ } while (0) +void *__termios2host(union metatermios *, const struct termios *); + #endif /* COSMOPOLITAN_LIBC_CALLS_TERMIOS_INTERNAL_H_ */ diff --git a/libc/calls/termios2host.c b/libc/calls/termios2host.c new file mode 100644 index 000000000..68bf333b0 --- /dev/null +++ b/libc/calls/termios2host.c @@ -0,0 +1,32 @@ +/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ +│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ +╞══════════════════════════════════════════════════════════════════════════════╡ +│ Copyright 2022 Justine Alexandra Roberts Tunney │ +│ │ +│ Permission to use, copy, modify, and/or distribute this software for │ +│ any purpose with or without fee is hereby granted, provided that the │ +│ above copyright notice and this permission notice appear in all copies. │ +│ │ +│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ +│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ +│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ +│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ +│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ +│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ +│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ +│ PERFORMANCE OF THIS SOFTWARE. │ +╚─────────────────────────────────────────────────────────────────────────────*/ +#include "libc/calls/termios.internal.h" +#include "libc/dce.h" + +void *__termios2host(union metatermios *mt, const struct termios *lt) { + if (!IsXnu() && !IsFreebsd() && !IsOpenbsd() && !IsNetbsd()) { + return (/*unconst*/ void *)lt; + } else if (IsXnu()) { + COPY_TERMIOS(&mt->xnu, lt); + return &mt->xnu; + } else { + COPY_TERMIOS(&mt->bsd, lt); + return &mt->bsd; + } +} diff --git a/libc/calls/tgkill.c b/libc/calls/tgkill.c index ee8abc337..5dc8b808a 100644 --- a/libc/calls/tgkill.c +++ b/libc/calls/tgkill.c @@ -17,12 +17,14 @@ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/calls/calls.h" -#include "libc/intrin/strace.internal.h" #include "libc/calls/syscall-sysv.internal.h" +#include "libc/intrin/strace.internal.h" /** - * Kills thread group. + * Kills thread, the Linux way. * + * @param tgid is thread group id, which on Linux means process id + * @param tid is thread id * @raises ENOSYS on non-Linux * @see tkill() */ diff --git a/libc/calls/unlockpt.c b/libc/calls/unlockpt.c index 59ba673a2..e33d35335 100644 --- a/libc/calls/unlockpt.c +++ b/libc/calls/unlockpt.c @@ -18,12 +18,15 @@ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/calls/calls.h" #include "libc/calls/syscall-sysv.internal.h" +#include "libc/calls/syscall_support-sysv.internal.h" #include "libc/calls/termios.h" #include "libc/dce.h" #include "libc/intrin/strace.internal.h" #include "libc/sysv/consts/pty.h" #include "libc/sysv/errfuns.h" +extern const uint32_t TIOCPTYUNLK; + /** * Unlocks pseudoteletypewriter pair. * @@ -32,13 +35,12 @@ * @raise EINVAL if fd is valid but not associated with pty */ int unlockpt(int fd) { - int rc; - if (IsFreebsd() || IsOpenbsd()) { + int rc, unlock = 0; + if (IsFreebsd() || IsOpenbsd() || IsNetbsd()) { rc = _isptmaster(fd); } else if (IsXnu()) { rc = sys_ioctl(fd, TIOCPTYUNLK); } else if (IsLinux()) { - int unlock = 0; rc = sys_ioctl(fd, TIOCSPTLCK, &unlock); } else { rc = enosys(); diff --git a/libc/intrin/describeflags.internal.h b/libc/intrin/describeflags.internal.h index 8ec3b0aa5..73a9b4822 100644 --- a/libc/intrin/describeflags.internal.h +++ b/libc/intrin/describeflags.internal.h @@ -20,6 +20,7 @@ const char *DescribeFrame(char[32], int); const char *DescribeFutexOp(char[64], int); const char *DescribeFutexResult(char[12], int); const char *DescribeHow(char[12], int); +const char *DescribeInOutInt64(char[23], ssize_t, int64_t *); const char *DescribeMapFlags(char[64], int); const char *DescribeMapping(char[8], int, int); const char *DescribeNtConsoleInFlags(char[256], uint32_t); @@ -64,6 +65,7 @@ const char *DescribeWhence(char[12], int); #define DescribeFutexOp(x) DescribeFutexOp(alloca(64), x) #define DescribeFutexResult(x) DescribeFutexResult(alloca(12), x) #define DescribeHow(x) DescribeHow(alloca(12), x) +#define DescribeInOutInt64(rc, x) DescribeInOutInt64(alloca(23), rc, x) #define DescribeMapFlags(x) DescribeMapFlags(alloca(64), x) #define DescribeMapping(x, y) DescribeMapping(alloca(8), x, y) #define DescribeNtConsoleInFlags(x) DescribeNtConsoleInFlags(alloca(256), x) diff --git a/libc/calls/copyfd.c b/libc/intrin/describeinoutint64.c similarity index 56% rename from libc/calls/copyfd.c rename to libc/intrin/describeinoutint64.c index 7ef001d2b..cead51abd 100644 --- a/libc/calls/copyfd.c +++ b/libc/intrin/describeinoutint64.c @@ -1,7 +1,7 @@ /*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ │vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ ╞══════════════════════════════════════════════════════════════════════════════╡ -│ Copyright 2020 Justine Alexandra Roberts Tunney │ +│ Copyright 2022 Justine Alexandra Roberts Tunney │ │ │ │ Permission to use, copy, modify, and/or distribute this software for │ │ any purpose with or without fee is hereby granted, provided that the │ @@ -16,51 +16,22 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/calls/calls.h" #include "libc/errno.h" -#include "libc/macros.internal.h" +#include "libc/fmt/itoa.h" +#include "libc/intrin/describeflags.internal.h" -/** - * Copies data between file descriptors the slow way. - * - * @return -1 on error/interrupt, 0 on eof, or [1..size] on success - * @see copy_file_range() for file ↔ file - * @see sendfile() for seekable → socket - * @see splice() for fd ↔ pipe - */ -ssize_t copyfd(int infd, int64_t *inoutopt_inoffset, int outfd, - int64_t *inoutopt_outoffset, size_t size, uint32_t flags) { - ssize_t rc; - char buf[2048]; - size_t i, j, n; - for (i = 0; i < size; i += j) { - n = MIN(size - i, sizeof(buf)); - if (inoutopt_inoffset) { - rc = pread(infd, buf, n, *inoutopt_inoffset); - } else { - rc = read(infd, buf, n); - } - if (!rc) return i; - if (rc == -1) { - if (i) return i; - return -1; - } - n = rc; - for (j = 0; j < n; j += rc) { - TryAgain: - if (inoutopt_outoffset) { - rc = pwrite(outfd, buf + j, n - j, *inoutopt_outoffset); - } else { - rc = write(outfd, buf + j, n - j); - } - if (rc == -1) { - if (errno == EINTR) goto TryAgain; - if (errno == EWOULDBLOCK) goto TryAgain; /* suboptimal */ - return -1; - } - if (inoutopt_inoffset) *inoutopt_inoffset += rc; - if (inoutopt_outoffset) *inoutopt_outoffset += rc; - } +const char *(DescribeInOutInt64)(char buf[23], ssize_t rc, int64_t *x) { + if (!x) return "NULL"; + char *p = buf; + if (rc != -1) *p++ = '['; + if (rc == -1 && errno == EFAULT) { + *p++ = '!'; + *p++ = '!'; + *p++ = '!'; + } else { + p = FormatInt64(p, *x); } - return i; + if (rc != -1) *p++ = ']'; + *p = 0; + return buf; } diff --git a/libc/intrin/pthread_mutex_trylock.c b/libc/intrin/pthread_mutex_trylock.c index d79c55163..3ecff00d2 100644 --- a/libc/intrin/pthread_mutex_trylock.c +++ b/libc/intrin/pthread_mutex_trylock.c @@ -34,7 +34,7 @@ * @raise EBUSY if lock is already held * @raise ENOTRECOVERABLE if `mutex` is corrupted */ -int pthread_mutex_trylock(pthread_mutex_t *mutex) { +errno_t pthread_mutex_trylock(pthread_mutex_t *mutex) { int c, d, t; if (__tls_enabled && // diff --git a/libc/intrin/pthread_once.c b/libc/intrin/pthread_once.c index eb13d4e80..6805a0ce2 100644 --- a/libc/intrin/pthread_once.c +++ b/libc/intrin/pthread_once.c @@ -44,7 +44,7 @@ * * @return 0 on success, or errno on error */ -int pthread_once(pthread_once_t *once, void init(void)) { +errno_t pthread_once(pthread_once_t *once, void init(void)) { uint32_t old; if (_weaken(nsync_run_once)) { _weaken(nsync_run_once)((nsync_once *)once, init); diff --git a/libc/intrin/restoretty.c b/libc/intrin/restoretty.c index 527bc2bb1..4838b0706 100644 --- a/libc/intrin/restoretty.c +++ b/libc/intrin/restoretty.c @@ -40,10 +40,10 @@ static bool __isrestorable; static union metatermios __oldtermios; // called weakly by libc/calls/ioctl_tcsets.c to avoid pledge("tty") -void __on_ioctl_tcsets(void) { +void __on_ioctl_tcsets(int fd) { int e; e = errno; - if (sys_ioctl(0, TCGETS, &__oldtermios) != -1) { + if (sys_ioctl(fd, TCGETS, &__oldtermios) != -1) { __isrestorable = true; } errno = e; diff --git a/libc/mem/copyfd.c b/libc/mem/copyfd.c index c058505e5..2ac7f7fd2 100644 --- a/libc/mem/copyfd.c +++ b/libc/mem/copyfd.c @@ -25,17 +25,6 @@ /** * Copies data between fds the old fashioned way. * - * Even though Cosmopolitan polyfills copy_file_range() to fallback to - * doing this, it's useful to call this function in cases where you know - * it'd be more helpful to get the larger buffer size with read/write. - * - * Reads are allowed to be interrupted. Writes are uninterruptible. If - * we get an interrupt when partial data was written, we return the - * partial amount. Therefore the file descriptors are guaranteed to - * remain in a consistent state, provided EINTR is the only error. - * - * If n is 0 then 0 is returned and no i/o operations are performed. - * * @return bytes successfully exchanged */ ssize_t _copyfd(int infd, int outfd, size_t n) { diff --git a/libc/calls/copyfd.internal.h b/libc/mem/copyfd.internal.h similarity index 100% rename from libc/calls/copyfd.internal.h rename to libc/mem/copyfd.internal.h diff --git a/libc/sock/sendfile.c b/libc/sock/sendfile.c index aa766d339..2b6fd919f 100644 --- a/libc/sock/sendfile.c +++ b/libc/sock/sendfile.c @@ -16,18 +16,24 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ +#include "libc/assert.h" #include "libc/calls/calls.h" #include "libc/calls/internal.h" #include "libc/calls/sig.internal.h" #include "libc/calls/syscall-sysv.internal.h" #include "libc/calls/syscall_support-nt.internal.h" #include "libc/dce.h" +#include "libc/errno.h" #include "libc/intrin/asan.internal.h" +#include "libc/intrin/describeflags.internal.h" #include "libc/intrin/safemacros.internal.h" #include "libc/intrin/strace.internal.h" +#include "libc/macros.internal.h" +#include "libc/nt/enum/filetype.h" #include "libc/nt/enum/wait.h" #include "libc/nt/errors.h" #include "libc/nt/files.h" +#include "libc/nt/struct/byhandlefileinformation.h" #include "libc/nt/winsock.h" #include "libc/sock/internal.h" #include "libc/sock/sendfile.internal.h" @@ -65,54 +71,76 @@ static textwindows int SendfileBlock(int64_t handle, return got; } -static textwindows ssize_t sendfile_linux2nt(int outfd, int infd, - int64_t *inout_opt_inoffset, - size_t uptobytes) { +static dontinline textwindows ssize_t sys_sendfile_nt( + int outfd, int infd, int64_t *opt_in_out_inoffset, uint32_t uptobytes) { ssize_t rc; - int64_t offset; - struct NtOverlapped overlapped; - if (!__isfdkind(outfd, kFdSocket)) return ebadf(); + int64_t ih, oh, pos, eof, offset; + struct NtByHandleFileInformation wst; if (!__isfdkind(infd, kFdFile)) return ebadf(); - if (inout_opt_inoffset) { - offset = *inout_opt_inoffset; - } else if (!SetFilePointerEx(g_fds.p[infd].handle, 0, &offset, SEEK_CUR)) { + if (!__isfdkind(outfd, kFdSocket)) return ebadf(); + ih = g_fds.p[infd].handle; + oh = g_fds.p[outfd].handle; + if (!SetFilePointerEx(ih, 0, &pos, SEEK_CUR)) { return __winerr(); } - bzero(&overlapped, sizeof(overlapped)); - overlapped.Pointer = (void *)(intptr_t)offset; - overlapped.hEvent = WSACreateEvent(); - if (TransmitFile(g_fds.p[outfd].handle, g_fds.p[infd].handle, uptobytes, 0, - &overlapped, 0, 0)) { + if (opt_in_out_inoffset) { + offset = *opt_in_out_inoffset; + } else { + offset = pos; + } + if (GetFileInformationByHandle(ih, &wst)) { + // TransmitFile() returns EINVAL if `uptobytes` goes past EOF. + eof = (uint64_t)wst.nFileSizeHigh << 32 | wst.nFileSizeLow; + if (offset + uptobytes > eof) { + uptobytes = eof - offset; + } + } else { + return ebadf(); + } + struct NtOverlapped ov = { + .Pointer = (void *)(intptr_t)offset, + .hEvent = WSACreateEvent(), + }; + if (TransmitFile(oh, ih, uptobytes, 0, &ov, 0, 0)) { rc = uptobytes; } else { - rc = SendfileBlock(g_fds.p[outfd].handle, &overlapped); + rc = SendfileBlock(oh, &ov); } - if (rc != -1 && inout_opt_inoffset) { - *inout_opt_inoffset = offset + rc; + if (rc != -1) { + if (opt_in_out_inoffset) { + *opt_in_out_inoffset = offset + rc; + _npassert(SetFilePointerEx(ih, pos, 0, SEEK_SET)); + } else { + _npassert(SetFilePointerEx(ih, offset + rc, 0, SEEK_SET)); + } } - WSACloseEvent(overlapped.hEvent); + WSACloseEvent(ov.hEvent); return rc; } -static ssize_t sendfile_linux2bsd(int outfd, int infd, - int64_t *inout_opt_inoffset, - size_t uptobytes) { - int rc; +static ssize_t sys_sendfile_bsd(int outfd, int infd, + int64_t *opt_in_out_inoffset, + size_t uptobytes) { + ssize_t rc; int64_t offset, sbytes; - if (inout_opt_inoffset) { - offset = *inout_opt_inoffset; + if (opt_in_out_inoffset) { + offset = *opt_in_out_inoffset; } else if ((offset = lseek(infd, 0, SEEK_CUR)) == -1) { return -1; } if (IsFreebsd()) { rc = sys_sendfile_freebsd(infd, outfd, offset, uptobytes, 0, &sbytes, 0); + if (rc == -1 && errno == ENOBUFS) errno = ENOMEM; } else { sbytes = uptobytes; rc = sys_sendfile_xnu(infd, outfd, offset, &sbytes, 0, 0); } + if (rc == -1 && errno == ENOTSOCK) errno = EBADF; if (rc != -1) { - if (inout_opt_inoffset) { - *inout_opt_inoffset += sbytes; + if (opt_in_out_inoffset) { + *opt_in_out_inoffset += sbytes; + } else { + _npassert(lseek(infd, offset + sbytes, SEEK_SET) == offset + sbytes); } return sbytes; } else { @@ -125,35 +153,55 @@ static ssize_t sendfile_linux2bsd(int outfd, int infd, * * @param outfd needs to be a socket * @param infd needs to be a file - * @param inout_opt_inoffset may be specified for pread()-like behavior - * @param uptobytes is usually the number of bytes remaining in file; it - * can't exceed INT_MAX-1; some platforms block until everything's - * sent, whereas others won't; zero isn't allowed - * @return number of bytes transmitted which may be fewer than requested + * @param opt_in_out_inoffset may be specified for pread()-like behavior + * in which case the file position won't be changed; otherwise, this + * shall read from the file pointer which is advanced accordingly + * @param uptobytes is the maximum number of bytes to send; some platforms + * block until everything's sent, whereas others won't; the behavior of + * zero is undefined; this value may overlap the end of file in which + * case what remains is sent; this is silently reduced to `0x7ffff000` + * @return number of bytes transmitted which may be fewer than requested in + * which case caller must be prepared to call sendfile() again + * @raise ESPIPE on Linux RHEL7+ if offset is used but `infd` isn't seekable, + * otherwise this could be EINVAL + * @raise EPIPE on most systems if socket has been shutdown for reading or + * the remote end closed the connection, otherwise this could be EINVAL + * @raise EBADF if `outfd` isn't a valid writeable stream sock descriptor + * @raise EAGAIN if `O_NONBLOCK` is in play and it would have blocked + * @raise EBADF if `infd` isn't a valid readable file descriptor + * @raise EFAULT if `opt_in_out_inoffset` is a bad pointer + * @raise EINVAL if `*opt_in_out_inoffset` is negative + * @raise EOVERFLOW is documented as possible on Linux + * @raise EIO if `infd` had a low-level i/o error + * @raise ENOMEM if we require more vespene gas + * @raise ENOTCONN if `outfd` isn't connected + * @raise ENOSYS on NetBSD and OpenBSD * @see copy_file_range() for file ↔ file * @see splice() for fd ↔ pipe */ -ssize_t sendfile(int outfd, int infd, int64_t *inout_opt_inoffset, +ssize_t sendfile(int outfd, int infd, int64_t *opt_in_out_inoffset, size_t uptobytes) { - int rc; - if (!uptobytes) { - rc = einval(); - } else if (IsAsan() && inout_opt_inoffset && - !__asan_is_valid(inout_opt_inoffset, - sizeof(*inout_opt_inoffset))) { + ssize_t rc; + + // We must reduce this due to the uint32_t type conversion on Windows + // which has a maximum of 0x7ffffffe. It also makes sendfile(..., -1) + // less error prone, since Linux may EINVAL if greater than INT64_MAX + uptobytes = MIN(uptobytes, 0x7ffff000); + + if (IsAsan() && opt_in_out_inoffset && + !__asan_is_valid(opt_in_out_inoffset, 8)) { rc = efault(); - } else if (uptobytes > 0x7ffffffe /* Microsoft's off-by-one */) { - rc = eoverflow(); } else if (IsLinux()) { - rc = sys_sendfile(outfd, infd, inout_opt_inoffset, uptobytes); + rc = sys_sendfile(outfd, infd, opt_in_out_inoffset, uptobytes); } else if (IsFreebsd() || IsXnu()) { - rc = sendfile_linux2bsd(outfd, infd, inout_opt_inoffset, uptobytes); + rc = sys_sendfile_bsd(outfd, infd, opt_in_out_inoffset, uptobytes); } else if (IsWindows()) { - rc = sendfile_linux2nt(outfd, infd, inout_opt_inoffset, uptobytes); + rc = sys_sendfile_nt(outfd, infd, opt_in_out_inoffset, uptobytes); } else { - rc = copyfd(infd, inout_opt_inoffset, outfd, NULL, uptobytes, 0); + rc = enosys(); } - STRACE("sendfile(%d, %d, %p, %'zu) → %ld% m", outfd, infd, inout_opt_inoffset, - uptobytes, rc); + + STRACE("sendfile(%d, %d, %p, %'zu) → %ld% m", outfd, infd, + DescribeInOutInt64(rc, opt_in_out_inoffset), uptobytes, rc); return rc; } diff --git a/libc/stdio/fputws.c b/libc/stdio/fputws.c index c828e0878..f2d7c14a3 100644 --- a/libc/stdio/fputws.c +++ b/libc/stdio/fputws.c @@ -28,7 +28,7 @@ * * @param s is a NUL-terminated string that's non-NULL * @param f is an open stream - * @return strlen(s) or -1 w/ errno on error + * @return strlen(s), or -1 w/ errno * @threadsafe */ int fputws(const wchar_t *s, FILE *f) { diff --git a/libc/stdio/fputws_unlocked.c b/libc/stdio/fputws_unlocked.c index e5fca98fa..90e0d40c2 100644 --- a/libc/stdio/fputws_unlocked.c +++ b/libc/stdio/fputws_unlocked.c @@ -28,7 +28,7 @@ * * @param s is a NUL-terminated string that's non-NULL * @param f is an open stream - * @return strlen(s) or -1 w/ errno on error + * @return strlen(s), or -1 w/ errno */ int fputws_unlocked(const wchar_t *s, FILE *f) { int res = 0; diff --git a/libc/sysv/calls/sys_fadvise.s b/libc/sysv/calls/sys_fadvise.s index 4e6b9effd..b25185c30 100644 --- a/libc/sysv/calls/sys_fadvise.s +++ b/libc/sysv/calls/sys_fadvise.s @@ -1,2 +1,2 @@ .include "o/libc/sysv/macros.internal.inc" -.scall sys_fadvise,0xffffff213ffff0dd,globl,hidden +.scall sys_fadvise,0x1a0fff213ffff0dd,globl,hidden diff --git a/libc/sysv/consts.sh b/libc/sysv/consts.sh index f1823bab8..3fc5ebfe0 100755 --- a/libc/sysv/consts.sh +++ b/libc/sysv/consts.sh @@ -1410,8 +1410,9 @@ syscon termios TIOCSTSTAMP 0 0 0 0x8008745a 0x8008745a 0 # boop syscon termios ENDRUNDISC 0 0 0 0x9 0x9 0 # boop syscon termios TIOCPTMASTER 0 0 0x2000741c 0 0 0 # boop syscon termios TIOCPTYGRANT 0 0x20007454 0 0 0 0 # xnu grantpt() -syscon termios TIOCPTYUNLK 0 0x20007452 0 0 0 0 # xnu grantpt() -syscon termios TIOCPTYGNAME 0 0x40807453 0 0 0 0 # xnu grantpt() +syscon termios TIOCPTYUNLK 0 0x20007452 0 0 0 0 # xnu unlockpt() +syscon termios TIOCPTYGNAME 0 0x40807453 0 0 0 0 # xnu ptyname() +syscon termios TIOCPTSNAME 0 0 0 0 0x48087448 0 # netbsd ptyname() syscon termios FIODGNAME 0 0 0x80106678 0 0 0 # freebsd ptsname_r() syscon termios NETGRAPHDISC 0 0 0x6 0 0 0 # boop syscon termios H4DISC 0 0 0x7 0 0 0 # boop diff --git a/libc/sysv/consts/TIOCPTSNAME.s b/libc/sysv/consts/TIOCPTSNAME.s new file mode 100644 index 000000000..3830ba98f --- /dev/null +++ b/libc/sysv/consts/TIOCPTSNAME.s @@ -0,0 +1,2 @@ +.include "o/libc/sysv/consts/syscon.internal.inc" +.syscon termios,TIOCPTSNAME,0,0,0,0,0x48087448,0 diff --git a/libc/sysv/consts/fio.h b/libc/sysv/consts/fio.h index a4c8812db..4f6f8ef47 100644 --- a/libc/sysv/consts/fio.h +++ b/libc/sysv/consts/fio.h @@ -4,11 +4,11 @@ #if !(__ASSEMBLER__ + __LINKER__ + 0) COSMOPOLITAN_C_START_ -extern const uint64_t FIOASYNC; -extern const uint64_t FIOCLEX; -extern const uint64_t FIONBIO; -extern const uint64_t FIONCLEX; -extern const uint64_t FIONREAD; +extern const uint32_t FIOASYNC; +extern const uint32_t FIOCLEX; +extern const uint32_t FIONBIO; +extern const uint32_t FIONCLEX; +extern const uint32_t FIONREAD; COSMOPOLITAN_C_END_ #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ diff --git a/libc/sysv/consts/posix.h b/libc/sysv/consts/posix.h index 9e28d762c..cf4a157db 100644 --- a/libc/sysv/consts/posix.h +++ b/libc/sysv/consts/posix.h @@ -6,29 +6,25 @@ COSMOPOLITAN_C_START_ extern const int POSIX_FADV_DONTNEED; extern const int POSIX_FADV_NOREUSE; -extern const int POSIX_FADV_NORMAL; -extern const int POSIX_FADV_RANDOM; -extern const int POSIX_FADV_SEQUENTIAL; -extern const int POSIX_FADV_WILLNEED; extern const int POSIX_MADV_DONTNEED; -extern const int POSIX_MADV_NORMAL; -extern const int POSIX_MADV_RANDOM; -extern const int POSIX_MADV_SEQUENTIAL; -extern const int POSIX_MADV_WILLNEED; +extern const int POSIX_MADV_DONTNEED; COSMOPOLITAN_C_END_ #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ -#define POSIX_FADV_DONTNEED SYMBOLIC(POSIX_FADV_DONTNEED) -#define POSIX_FADV_NOREUSE SYMBOLIC(POSIX_FADV_NOREUSE) -#define POSIX_FADV_NORMAL SYMBOLIC(POSIX_FADV_NORMAL) -#define POSIX_FADV_RANDOM SYMBOLIC(POSIX_FADV_RANDOM) -#define POSIX_FADV_SEQUENTIAL SYMBOLIC(POSIX_FADV_SEQUENTIAL) -#define POSIX_FADV_WILLNEED SYMBOLIC(POSIX_FADV_WILLNEED) -#define POSIX_MADV_DONTNEED SYMBOLIC(POSIX_MADV_DONTNEED) -#define POSIX_MADV_NORMAL SYMBOLIC(POSIX_MADV_NORMAL) -#define POSIX_MADV_RANDOM SYMBOLIC(POSIX_MADV_RANDOM) -#define POSIX_MADV_SEQUENTIAL SYMBOLIC(POSIX_MADV_SEQUENTIAL) -#define POSIX_MADV_WILLNEED SYMBOLIC(POSIX_MADV_WILLNEED) +#define POSIX_FADV_NORMAL LITERALLY(0) +#define POSIX_FADV_RANDOM LITERALLY(1) +#define POSIX_FADV_SEQUENTIAL LITERALLY(2) +#define POSIX_FADV_WILLNEED LITERALLY(3) + +#define POSIX_FADV_DONTNEED SYMBOLIC(POSIX_FADV_DONTNEED) +#define POSIX_FADV_NOREUSE SYMBOLIC(POSIX_FADV_NOREUSE) + +#define POSIX_MADV_NORMAL LITERALLY(0) +#define POSIX_MADV_RANDOM LITERALLY(1) +#define POSIX_MADV_SEQUENTIAL LITERALLY(2) +#define POSIX_MADV_WILLNEED LITERALLY(3) + +#define POSIX_MADV_DONTNEED SYMBOLIC(POSIX_MADV_DONTNEED) #endif /* COSMOPOLITAN_LIBC_SYSV_CONSTS_POSIX_H_ */ diff --git a/libc/sysv/consts/termios.h b/libc/sysv/consts/termios.h index 0059408cc..7d3fe9d5d 100644 --- a/libc/sysv/consts/termios.h +++ b/libc/sysv/consts/termios.h @@ -178,10 +178,6 @@ extern const uint8_t VTIME; extern const uint8_t VWERASE; extern const uint32_t XCASE; extern const uint32_t XTABS; -extern const uint32_t FIODGNAME; -extern const uint32_t TIOCPTYGRANT; -extern const uint32_t TIOCPTYUNLK; -extern const uint32_t TIOCPTYGNAME; COSMOPOLITAN_C_END_ #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ @@ -254,7 +250,6 @@ COSMOPOLITAN_C_END_ #define EXTA SYMBOLIC(EXTA) #define EXTB SYMBOLIC(EXTB) #define EXTPROC SYMBOLIC(EXTPROC) -#define FIODGNAME SYMBOLIC(FIODGNAME) #define FLUSHO SYMBOLIC(FLUSHO) #define H4DISC SYMBOLIC(H4DISC) #define HUPCL SYMBOLIC(HUPCL) @@ -324,9 +319,6 @@ COSMOPOLITAN_C_END_ #define TIOCNXCL SYMBOLIC(TIOCNXCL) #define TIOCOUTQ SYMBOLIC(TIOCOUTQ) #define TIOCPTMASTER SYMBOLIC(TIOCPTMASTER) -#define TIOCPTYGNAME SYMBOLIC(TIOCPTYGNAME) -#define TIOCPTYGRANT SYMBOLIC(TIOCPTYGRANT) -#define TIOCPTYUNLK SYMBOLIC(TIOCPTYUNLK) #define TIOCREMOTE SYMBOLIC(TIOCREMOTE) #define TIOCSBRK SYMBOLIC(TIOCSBRK) #define TIOCSCTTY SYMBOLIC(TIOCSCTTY) diff --git a/libc/sysv/syscalls.sh b/libc/sysv/syscalls.sh index f692d7b01..17a4febab 100755 --- a/libc/sysv/syscalls.sh +++ b/libc/sysv/syscalls.sh @@ -249,7 +249,7 @@ scall sys_getdents 0x18606311020c40d9 globl hidden # use opendir/readdir; four scall sys_set_tid_address 0xfffffffffffff0da globl # no wrapper scall sys_restart_syscall 0xfffffffffffff0db globl # no wrapper scall sys_semtimedop 0xfffffffffffff0dc globl # no wrapper -scall sys_fadvise 0xffffff213ffff0dd globl hidden +scall sys_fadvise 0x1a0fff213ffff0dd globl hidden scall sys_timer_create 0x0ebffffffffff0de globl # no wrapper scall sys_timer_settime 0x1beffffffffff0df globl # no wrapper scall sys_timer_gettime 0x1bfffffffffff0e0 globl # no wrapper @@ -383,7 +383,7 @@ scall sys_fsmount 0xfffffffffffff1b0 globl # no wrapper scall sys_fspick 0xfffffffffffff1b1 globl # no wrapper scall sys_pidfd_open 0xfffffffffffff1b2 globl # no wrapper scall sys_clone3 0xfffffffffffff1b3 globl # no wrapper -scall sys_close_range 0xffffff23fffff1b4 globl hidden # linux 5.9 +scall sys_close_range 0xffffff23fffff1b4 globl hidden # Linux 5.9 scall sys_openat2 0xfffffffffffff1b5 globl hidden # Linux 5.6 scall sys_pidfd_getfd 0xfffffffffffff1b6 globl # no wrapper scall sys_faccessat2 0xfffffffffffff1b7 globl hidden diff --git a/libc/thread/pthread_attr_getguardsize.c b/libc/thread/pthread_attr_getguardsize.c index 6b82d638f..4d50f5648 100644 --- a/libc/thread/pthread_attr_getguardsize.c +++ b/libc/thread/pthread_attr_getguardsize.c @@ -24,7 +24,8 @@ * @param guardsize will be set to guard size in bytes * @return 0 on success, or errno on error */ -int pthread_attr_getguardsize(const pthread_attr_t *attr, size_t *guardsize) { +errno_t pthread_attr_getguardsize(const pthread_attr_t *attr, + size_t *guardsize) { *guardsize = attr->guardsize; return 0; } diff --git a/libc/thread/pthread_attr_getstack.c b/libc/thread/pthread_attr_getstack.c index c569c7d65..b71808fe6 100644 --- a/libc/thread/pthread_attr_getstack.c +++ b/libc/thread/pthread_attr_getstack.c @@ -16,8 +16,8 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/thread/thread.h" #include "libc/runtime/stack.h" +#include "libc/thread/thread.h" /** * Returns configuration for thread stack. @@ -30,8 +30,8 @@ * @return 0 on success, or errno on error * @see pthread_attr_setstacksize() */ -int pthread_attr_getstack(const pthread_attr_t *attr, void **stackaddr, - size_t *stacksize) { +errno_t pthread_attr_getstack(const pthread_attr_t *attr, void **stackaddr, + size_t *stacksize) { *stackaddr = attr->stackaddr; *stacksize = attr->stacksize; return 0; diff --git a/libc/thread/pthread_attr_getstacksize.c b/libc/thread/pthread_attr_getstacksize.c index 746ed4f7a..8f1d7552b 100644 --- a/libc/thread/pthread_attr_getstacksize.c +++ b/libc/thread/pthread_attr_getstacksize.c @@ -16,8 +16,8 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/thread/thread.h" #include "libc/runtime/stack.h" +#include "libc/thread/thread.h" /** * Returns size of thread stack. @@ -28,7 +28,7 @@ * @return 0 on success, or errno on error * @see pthread_attr_setstacksize() */ -int pthread_attr_getstacksize(const pthread_attr_t *a, size_t *x) { +errno_t pthread_attr_getstacksize(const pthread_attr_t *a, size_t *x) { if (a->stacksize) { *x = a->stacksize; } else { diff --git a/libc/thread/pthread_attr_init.c b/libc/thread/pthread_attr_init.c index b12dfea81..14fe569aa 100644 --- a/libc/thread/pthread_attr_init.c +++ b/libc/thread/pthread_attr_init.c @@ -16,15 +16,15 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/thread/thread.h" #include "libc/runtime/stack.h" +#include "libc/thread/thread.h" /** * Initializes pthread attributes. * * @return 0 on success, or errno on error */ -int pthread_attr_init(pthread_attr_t *attr) { +errno_t pthread_attr_init(pthread_attr_t *attr) { *attr = (pthread_attr_t){ .stacksize = GetStackSize(), .guardsize = PAGESIZE, diff --git a/libc/thread/pthread_attr_setguardsize.c b/libc/thread/pthread_attr_setguardsize.c index 40b6a77b8..f93cab449 100644 --- a/libc/thread/pthread_attr_setguardsize.c +++ b/libc/thread/pthread_attr_setguardsize.c @@ -24,7 +24,7 @@ * @param guardsize contains guard size in bytes * @return 0 on success, or errno on error */ -int pthread_attr_setguardsize(pthread_attr_t *attr, size_t guardsize) { +errno_t pthread_attr_setguardsize(pthread_attr_t *attr, size_t guardsize) { attr->guardsize = guardsize; return 0; } diff --git a/libc/thread/pthread_attr_setinheritsched.c b/libc/thread/pthread_attr_setinheritsched.c index eb2737720..128a30158 100644 --- a/libc/thread/pthread_attr_setinheritsched.c +++ b/libc/thread/pthread_attr_setinheritsched.c @@ -40,7 +40,7 @@ * @return 0 on success, or errno on error * @raise EINVAL on bad value */ -int pthread_attr_setinheritsched(pthread_attr_t *attr, int inheritsched) { +errno_t pthread_attr_setinheritsched(pthread_attr_t *attr, int inheritsched) { switch (inheritsched) { case PTHREAD_INHERIT_SCHED: case PTHREAD_EXPLICIT_SCHED: diff --git a/libc/thread/pthread_attr_setstack.c b/libc/thread/pthread_attr_setstack.c index 93aec84a0..acb7d1e5f 100644 --- a/libc/thread/pthread_attr_setstack.c +++ b/libc/thread/pthread_attr_setstack.c @@ -62,8 +62,8 @@ * @raise EINVAL if parameters were unacceptable * @see pthread_attr_setstacksize() */ -int pthread_attr_setstack(pthread_attr_t *attr, void *stackaddr, - size_t stacksize) { +errno_t pthread_attr_setstack(pthread_attr_t *attr, void *stackaddr, + size_t stacksize) { if (!stackaddr) { attr->stackaddr = 0; attr->stacksize = 0; diff --git a/libc/thread/pthread_attr_setstacksize.c b/libc/thread/pthread_attr_setstacksize.c index 353d708ab..327e56f77 100644 --- a/libc/thread/pthread_attr_setstacksize.c +++ b/libc/thread/pthread_attr_setstacksize.c @@ -26,7 +26,7 @@ * @return 0 on success, or errno on error * @raise EINVAL if `stacksize` is less than `PTHREAD_STACK_MIN` */ -int pthread_attr_setstacksize(pthread_attr_t *a, size_t stacksize) { +errno_t pthread_attr_setstacksize(pthread_attr_t *a, size_t stacksize) { if (stacksize < PTHREAD_STACK_MIN) return EINVAL; a->stacksize = stacksize; return 0; diff --git a/libc/thread/pthread_barrier_wait.c b/libc/thread/pthread_barrier_wait.c index f73de2d1f..bf389453a 100644 --- a/libc/thread/pthread_barrier_wait.c +++ b/libc/thread/pthread_barrier_wait.c @@ -30,7 +30,7 @@ * @return 0 on success, `PTHREAD_BARRIER_SERIAL_THREAD` to one lucky * thread which was the last arrival, or an errno on error */ -int pthread_barrier_wait(pthread_barrier_t *barrier) { +errno_t pthread_barrier_wait(pthread_barrier_t *barrier) { if (nsync_counter_add(barrier->_nsync, -1)) { nsync_counter_wait(barrier->_nsync, nsync_time_no_deadline); return 0; diff --git a/libc/thread/pthread_cond_broadcast.c b/libc/thread/pthread_cond_broadcast.c index 8518a4b45..1d374cb64 100644 --- a/libc/thread/pthread_cond_broadcast.c +++ b/libc/thread/pthread_cond_broadcast.c @@ -36,7 +36,7 @@ * @see pthread_cond_signal * @see pthread_cond_wait */ -int pthread_cond_broadcast(pthread_cond_t *cond) { +errno_t pthread_cond_broadcast(pthread_cond_t *cond) { nsync_cv_broadcast((nsync_cv *)cond); return 0; } diff --git a/libc/thread/pthread_cond_signal.c b/libc/thread/pthread_cond_signal.c index 11dfd11c4..a04b30246 100644 --- a/libc/thread/pthread_cond_signal.c +++ b/libc/thread/pthread_cond_signal.c @@ -36,7 +36,7 @@ * @see pthread_cond_broadcast * @see pthread_cond_wait */ -int pthread_cond_signal(pthread_cond_t *cond) { +errno_t pthread_cond_signal(pthread_cond_t *cond) { nsync_cv_signal((nsync_cv *)cond); return 0; } diff --git a/libc/thread/pthread_cond_timedwait.c b/libc/thread/pthread_cond_timedwait.c index 31ecf88ba..672685c3a 100644 --- a/libc/thread/pthread_cond_timedwait.c +++ b/libc/thread/pthread_cond_timedwait.c @@ -44,8 +44,8 @@ * @see pthread_cond_broadcast * @see pthread_cond_signal */ -int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, - const struct timespec *abstime) { +errno_t pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, + const struct timespec *abstime) { if (abstime && !(0 <= abstime->tv_nsec && abstime->tv_nsec < 1000000000)) { return EINVAL; } diff --git a/libc/thread/pthread_cond_wait.c b/libc/thread/pthread_cond_wait.c index 30920d280..260a4ce20 100644 --- a/libc/thread/pthread_cond_wait.c +++ b/libc/thread/pthread_cond_wait.c @@ -37,6 +37,6 @@ * @see pthread_cond_broadcast * @see pthread_cond_signal */ -int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex) { +errno_t pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex) { return pthread_cond_timedwait(cond, mutex, 0); } diff --git a/libc/thread/pthread_create.c b/libc/thread/pthread_create.c index d64924206..bff730685 100644 --- a/libc/thread/pthread_create.c +++ b/libc/thread/pthread_create.c @@ -184,8 +184,8 @@ static int FixupCustomStackOnOpenbsd(pthread_attr_t *attr) { * isn't authorized to use it * @threadsafe */ -int pthread_create(pthread_t *thread, const pthread_attr_t *attr, - void *(*start_routine)(void *), void *arg) { +errno_t pthread_create(pthread_t *thread, const pthread_attr_t *attr, + void *(*start_routine)(void *), void *arg) { int rc, e = errno; struct PosixThread *pt; __require_tls(); diff --git a/libc/thread/pthread_getaffinity_np.c b/libc/thread/pthread_getaffinity_np.c index 0133fe20c..d905f4225 100644 --- a/libc/thread/pthread_getaffinity_np.c +++ b/libc/thread/pthread_getaffinity_np.c @@ -31,8 +31,8 @@ * @return 0 on success, or errno on error * @raise ENOSYS if not Linux or Windows */ -int pthread_getaffinity_np(pthread_t thread, size_t bitsetsize, - cpu_set_t *bitset) { +errno_t pthread_getaffinity_np(pthread_t thread, size_t bitsetsize, + cpu_set_t *bitset) { int rc, e = errno; struct PosixThread *pt = (struct PosixThread *)thread; if (!sched_getaffinity(pt->tid, bitsetsize, bitset)) { diff --git a/libc/thread/pthread_getname_np.c b/libc/thread/pthread_getname_np.c index 413cb4c0a..935fff801 100644 --- a/libc/thread/pthread_getname_np.c +++ b/libc/thread/pthread_getname_np.c @@ -44,7 +44,7 @@ * result will still be returned truncated if possible * @raise ENOSYS on MacOS, Windows, FreeBSD, and OpenBSD */ -int pthread_getname_np(pthread_t thread, char *name, size_t size) { +errno_t pthread_getname_np(pthread_t thread, char *name, size_t size) { int e, fd, rc, tid, len; if (!size) return 0; diff --git a/libc/thread/pthread_rwlock_rdlock.c b/libc/thread/pthread_rwlock_rdlock.c index 187b60453..a6a562c07 100644 --- a/libc/thread/pthread_rwlock_rdlock.c +++ b/libc/thread/pthread_rwlock_rdlock.c @@ -24,7 +24,7 @@ * * @return 0 on success, or errno on error */ -int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock) { +errno_t pthread_rwlock_rdlock(pthread_rwlock_t *rwlock) { nsync_mu_rlock((nsync_mu *)rwlock); return 0; } diff --git a/libc/thread/pthread_rwlock_unlock.c b/libc/thread/pthread_rwlock_unlock.c index 617746146..80b6fbbe3 100644 --- a/libc/thread/pthread_rwlock_unlock.c +++ b/libc/thread/pthread_rwlock_unlock.c @@ -25,7 +25,7 @@ * @return 0 on success, or errno on error * @raise EINVAL if lock is in a bad state */ -int pthread_rwlock_unlock(pthread_rwlock_t *rwlock) { +errno_t pthread_rwlock_unlock(pthread_rwlock_t *rwlock) { if (rwlock->_iswrite) { rwlock->_iswrite = 0; nsync_mu_unlock((nsync_mu *)rwlock); diff --git a/libc/thread/pthread_rwlock_wrlock.c b/libc/thread/pthread_rwlock_wrlock.c index f97c3ae95..e4ea7cde0 100644 --- a/libc/thread/pthread_rwlock_wrlock.c +++ b/libc/thread/pthread_rwlock_wrlock.c @@ -24,7 +24,7 @@ * * @return 0 on success, or errno on error */ -int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock) { +errno_t pthread_rwlock_wrlock(pthread_rwlock_t *rwlock) { nsync_mu_lock((nsync_mu *)rwlock); rwlock->_iswrite = 1; return 0; diff --git a/libc/thread/pthread_setaffinity_np.c b/libc/thread/pthread_setaffinity_np.c index 9605d056e..15583c534 100644 --- a/libc/thread/pthread_setaffinity_np.c +++ b/libc/thread/pthread_setaffinity_np.c @@ -27,8 +27,8 @@ * @return 0 on success, or errno on error * @raise ENOSYS if not Linux or Windows */ -int pthread_setaffinity_np(pthread_t thread, size_t bitsetsize, - const cpu_set_t *bitset) { +errno_t pthread_setaffinity_np(pthread_t thread, size_t bitsetsize, + const cpu_set_t *bitset) { int rc, e = errno; struct PosixThread *pt = (struct PosixThread *)thread; if (!sched_setaffinity(pt->tid, bitsetsize, bitset)) { diff --git a/libc/thread/pthread_setname_np.c b/libc/thread/pthread_setname_np.c index 5452299ae..fa6337fab 100644 --- a/libc/thread/pthread_setname_np.c +++ b/libc/thread/pthread_setname_np.c @@ -51,7 +51,7 @@ * @raise ENOSYS on MacOS, Windows, and OpenBSD * @see pthread_getname_np() */ -int pthread_setname_np(pthread_t thread, const char *name) { +errno_t pthread_setname_np(pthread_t thread, const char *name) { char path[128], *p; int e, fd, rc, tid, len; diff --git a/libc/thread/pthread_spin_destroy.c b/libc/thread/pthread_spin_destroy.c index 18fa1bfeb..5ff8cbed9 100644 --- a/libc/thread/pthread_spin_destroy.c +++ b/libc/thread/pthread_spin_destroy.c @@ -23,6 +23,6 @@ * * @return 0 on success, or errno on error */ -int(pthread_spin_destroy)(pthread_spinlock_t *spin) { +errno_t(pthread_spin_destroy)(pthread_spinlock_t *spin) { return pthread_spin_destroy(spin); } diff --git a/libc/thread/pthread_spin_init.c b/libc/thread/pthread_spin_init.c index d8ca27cc1..aca0ddf7f 100644 --- a/libc/thread/pthread_spin_init.c +++ b/libc/thread/pthread_spin_init.c @@ -27,6 +27,6 @@ * @see pthread_spin_destroy * @see pthread_spin_lock */ -int(pthread_spin_init)(pthread_spinlock_t *spin, int pshared) { +errno_t(pthread_spin_init)(pthread_spinlock_t *spin, int pshared) { return pthread_spin_init(spin, pshared); } diff --git a/libc/thread/pthread_spin_lock.c b/libc/thread/pthread_spin_lock.c index 36ced2007..9227cf050 100644 --- a/libc/thread/pthread_spin_lock.c +++ b/libc/thread/pthread_spin_lock.c @@ -50,6 +50,6 @@ * @see pthread_spin_unlock * @see pthread_spin_init */ -int(pthread_spin_lock)(pthread_spinlock_t *spin) { +errno_t(pthread_spin_lock)(pthread_spinlock_t *spin) { return pthread_spin_lock(spin); } diff --git a/libc/thread/pthread_spin_trylock.c b/libc/thread/pthread_spin_trylock.c index 36bcb55c9..a0b763462 100644 --- a/libc/thread/pthread_spin_trylock.c +++ b/libc/thread/pthread_spin_trylock.c @@ -27,6 +27,6 @@ * @return 0 on success, or errno on error * @raise EBUSY if lock is already held */ -int(pthread_spin_trylock)(pthread_spinlock_t *spin) { +errno_t(pthread_spin_trylock)(pthread_spinlock_t *spin) { return pthread_spin_trylock(spin); } diff --git a/libc/thread/pthread_spin_unlock.c b/libc/thread/pthread_spin_unlock.c index 54fafbd18..9db8faa01 100644 --- a/libc/thread/pthread_spin_unlock.c +++ b/libc/thread/pthread_spin_unlock.c @@ -24,6 +24,6 @@ * @return 0 on success, or errno on error * @see pthread_spin_lock */ -int(pthread_spin_unlock)(pthread_spinlock_t *spin) { +errno_t(pthread_spin_unlock)(pthread_spinlock_t *spin) { return pthread_spin_unlock(spin); } diff --git a/test/libc/calls/closefrom_test.c b/test/libc/calls/closefrom_test.c index 203516215..918e521d2 100644 --- a/test/libc/calls/closefrom_test.c +++ b/test/libc/calls/closefrom_test.c @@ -21,6 +21,7 @@ #include "libc/errno.h" #include "libc/runtime/runtime.h" #include "libc/sysv/consts/f.h" +#include "libc/testlib/subprocess.h" #include "libc/testlib/testlib.h" void SetUp(void) { @@ -34,6 +35,10 @@ void SetUp(void) { } } +TEST(closefrom, ebadf) { + ASSERT_SYS(EBADF, -1, closefrom(-2)); +} + TEST(closefrom, test) { ASSERT_SYS(0, 3, dup(2)); ASSERT_SYS(0, 4, dup(2)); @@ -48,14 +53,23 @@ TEST(closefrom, test) { } TEST(close_range, test) { - ASSERT_SYS(0, 3, dup(2)); - ASSERT_SYS(0, 4, dup(2)); - ASSERT_SYS(0, 5, dup(2)); - ASSERT_SYS(0, 6, dup(2)); - EXPECT_SYS(0, 0, close_range(3, -1, 0)); - ASSERT_SYS(0, 0, fcntl(2, F_GETFD)); - ASSERT_SYS(EBADF, -1, fcntl(3, F_GETFD)); - ASSERT_SYS(EBADF, -1, fcntl(4, F_GETFD)); - ASSERT_SYS(EBADF, -1, fcntl(5, F_GETFD)); - ASSERT_SYS(EBADF, -1, fcntl(6, F_GETFD)); + if (IsLinux() || IsFreebsd()) { + ASSERT_SYS(0, 3, dup(2)); + ASSERT_SYS(0, 4, dup(2)); + ASSERT_SYS(0, 5, dup(2)); + ASSERT_SYS(0, 6, dup(2)); + EXPECT_SYS(0, 0, close_range(3, -1, 0)); + ASSERT_SYS(0, 0, fcntl(2, F_GETFD)); + ASSERT_SYS(EBADF, -1, fcntl(3, F_GETFD)); + ASSERT_SYS(EBADF, -1, fcntl(4, F_GETFD)); + ASSERT_SYS(EBADF, -1, fcntl(5, F_GETFD)); + ASSERT_SYS(EBADF, -1, fcntl(6, F_GETFD)); + } else { + EXPECT_SYS(ENOSYS, -1, close_range(3, -1, 0)); + } +} + +TEST(close_range, ignoresNonexistantRanges) { + if (!IsLinux() && !IsFreebsd()) return; + EXPECT_SYS(0, 0, close_range(-2, -1, 0)); } diff --git a/test/libc/calls/copy_file_range_test.c b/test/libc/calls/copy_file_range_test.c new file mode 100644 index 000000000..f0c15d14b --- /dev/null +++ b/test/libc/calls/copy_file_range_test.c @@ -0,0 +1,161 @@ +/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ +│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ +╞══════════════════════════════════════════════════════════════════════════════╡ +│ Copyright 2022 Justine Alexandra Roberts Tunney │ +│ │ +│ Permission to use, copy, modify, and/or distribute this software for │ +│ any purpose with or without fee is hereby granted, provided that the │ +│ above copyright notice and this permission notice appear in all copies. │ +│ │ +│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ +│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ +│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ +│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ +│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ +│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ +│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ +│ PERFORMANCE OF THIS SOFTWARE. │ +╚─────────────────────────────────────────────────────────────────────────────*/ +#include "libc/calls/calls.h" +#include "libc/calls/struct/stat.h" +#include "libc/calls/syscall-sysv.internal.h" +#include "libc/dce.h" +#include "libc/errno.h" +#include "libc/mem/gc.h" +#include "libc/mem/mem.h" +#include "libc/runtime/runtime.h" +#include "libc/stdio/rand.h" +#include "libc/str/str.h" +#include "libc/sysv/consts/o.h" +#include "libc/sysv/consts/posix.h" +#include "libc/testlib/testlib.h" +#include "libc/x/x.h" +#include "libc/x/xasprintf.h" + +char testlib_enable_tmp_setup_teardown; + +void Make(const char *path, int mode) { + int fd, n = lemur64() & 0xfffff; + char *data = _gc(malloc(n)); + rngset(data, n, lemur64, -1); + ASSERT_NE(-1, (fd = creat(path, mode))); + ASSERT_SYS(0, n, write(fd, data, n)); + ASSERT_SYS(0, 0, close(fd)); +} + +void Copy(const char *from, const char *to) { + ssize_t rc; + char buf[512]; + struct stat st; + int fdin, fdout, e = errno; + ASSERT_NE(-1, (fdin = open(from, O_RDONLY))); + ASSERT_SYS(0, 0, fstat(fdin, &st)); + ASSERT_NE(-1, (fdout = creat(to, st.st_mode | 0400))); + if (!(IsXnu() || IsOpenbsd())) { + ASSERT_SYS(0, 0, posix_fadvise(fdin, 0, st.st_size, POSIX_FADV_SEQUENTIAL)); + } + ASSERT_SYS(0, 0, ftruncate(fdout, st.st_size)); + while ((rc = copy_file_range(fdin, 0, fdout, 0, -1u, 0))) { + if (rc == -1) { + ASSERT_EQ(ENOSYS, errno); + errno = e; + while ((rc = read(fdin, buf, sizeof(buf)))) { + ASSERT_NE(-1, rc); + ASSERT_EQ(rc, write(fdout, buf, rc)); + } + break; + } + } + ASSERT_SYS(0, 0, close(fdin)); + ASSERT_SYS(0, 0, close(fdout)); +} + +TEST(copy_file_range, test) { + char *p, *q; + size_t n, m; + Make("foo", 0644); + Copy("foo", "bar"); + p = _gc(xslurp("foo", &n)); + q = _gc(xslurp("bar", &m)); + ASSERT_EQ(n, m); + ASSERT_EQ(0, memcmp(p, q, n)); +} + +bool HasCopyFileRange(void) { + bool ok; + int rc, e; + e = errno; + rc = copy_file_range(-1, 0, -1, 0, 0, 0); + ok = rc == -1 && errno == EBADF; + errno = e; + return ok; +} + +TEST(copy_file_range, badFd) { + if (!HasCopyFileRange()) return; + ASSERT_SYS(EBADF, -1, copy_file_range(-1, 0, -1, 0, -1u, 0)); +} + +TEST(copy_file_range, badFlags) { + if (!HasCopyFileRange()) return; + ASSERT_SYS(EINVAL, -1, copy_file_range(0, 0, 1, 0, -1u, -1)); +} + +TEST(copy_file_range, differentFileSystems) { + if (!IsLinux()) return; + if (!HasCopyFileRange()) return; + ASSERT_SYS(0, 3, open("/proc/stat", 0)); + ASSERT_SYS(0, 4, creat("foo", 0644)); + ASSERT_SYS(EXDEV, -1, copy_file_range(3, 0, 4, 0, -1u, 0)); + ASSERT_SYS(0, 0, close(4)); + ASSERT_SYS(0, 0, close(3)); +} + +TEST(copy_file_range, twoDifferentFiles) { + char buf[16] = {0}; + int64_t i = 1, o = 0; + if (!HasCopyFileRange()) return; + ASSERT_SYS(0, 3, open("foo", O_RDWR | O_CREAT | O_TRUNC, 0644)); + ASSERT_SYS(0, 4, open("bar", O_RDWR | O_CREAT | O_TRUNC, 0644)); + ASSERT_SYS(0, 5, pwrite(3, "hello", 5, 0)); + ASSERT_SYS(0, 4, copy_file_range(3, &i, 4, &o, 4, 0)); + ASSERT_SYS(0, 0, copy_file_range(3, &i, 4, &o, 0, 0)); + ASSERT_EQ(5, i); + ASSERT_EQ(4, o); + ASSERT_SYS(0, 4, read(4, buf, 16)); + ASSERT_STREQ("ello", buf); + ASSERT_SYS(0, 0, close(4)); + ASSERT_SYS(0, 0, close(3)); +} + +TEST(copy_file_range, sameFile_doesntChangeFilePointer) { + char buf[16] = {0}; + int64_t i = 1, o = 5; + if (!HasCopyFileRange()) return; + ASSERT_SYS(0, 3, open("foo", O_RDWR | O_CREAT | O_TRUNC, 0644)); + ASSERT_SYS(0, 5, pwrite(3, "hello", 5, 0)); + ASSERT_SYS(0, 4, copy_file_range(3, &i, 3, &o, 4, 0)); + ASSERT_EQ(5, i); + ASSERT_EQ(9, o); + ASSERT_SYS(0, 9, read(3, buf, 16)); + ASSERT_STREQ("helloello", buf); + ASSERT_SYS(0, 0, close(3)); +} + +TEST(copy_file_range, overlappingRange) { + int rc; + int64_t i = 1, o = 2; + if (!HasCopyFileRange()) return; + ASSERT_SYS(0, 3, open("foo", O_RDWR | O_CREAT | O_TRUNC, 0644)); + ASSERT_SYS(0, 5, pwrite(3, "hello", 5, 0)); + rc = copy_file_range(3, &i, 3, &o, 4, 0); + ASSERT_EQ(-1, rc); + if (IsLinux()) { + // [wut] rhel7 returns enosys here + ASSERT_TRUE(errno == EINVAL || errno == ENOSYS); + } else { + ASSERT_TRUE(errno == EINVAL); + } + errno = 0; + ASSERT_SYS(0, 0, close(3)); +} diff --git a/test/libc/calls/pledge_test.c b/test/libc/calls/pledge_test.c index 60081b4dc..ecc6c566d 100644 --- a/test/libc/calls/pledge_test.c +++ b/test/libc/calls/pledge_test.c @@ -17,7 +17,7 @@ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/calls/calls.h" -#include "libc/calls/copyfd.internal.h" +#include "libc/mem/copyfd.internal.h" #include "libc/calls/internal.h" #include "libc/calls/ioctl.h" #include "libc/calls/pledge.internal.h" diff --git a/test/libc/calls/posix_fadvise_test.c b/test/libc/calls/posix_fadvise_test.c new file mode 100644 index 000000000..152a47856 --- /dev/null +++ b/test/libc/calls/posix_fadvise_test.c @@ -0,0 +1,106 @@ +/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ +│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ +╞══════════════════════════════════════════════════════════════════════════════╡ +│ Copyright 2022 Justine Alexandra Roberts Tunney │ +│ │ +│ Permission to use, copy, modify, and/or distribute this software for │ +│ any purpose with or without fee is hereby granted, provided that the │ +│ above copyright notice and this permission notice appear in all copies. │ +│ │ +│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ +│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ +│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ +│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ +│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ +│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ +│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ +│ PERFORMANCE OF THIS SOFTWARE. │ +╚─────────────────────────────────────────────────────────────────────────────*/ +#include "libc/calls/calls.h" +#include "libc/dce.h" +#include "libc/errno.h" +#include "libc/limits.h" +#include "libc/runtime/runtime.h" +#include "libc/sysv/consts/madv.h" +#include "libc/sysv/consts/posix.h" +#include "libc/testlib/testlib.h" +#include "libc/x/x.h" + +char testlib_enable_tmp_setup_teardown; + +void SetUp(void) { + if (IsOpenbsd() || IsXnu()) exit(0); +} + +TEST(fadvise, ebadf) { + ASSERT_SYS(EBADF, -1, fadvise(-1, 0, 0, MADV_SEQUENTIAL)); +} + +TEST(posix_fadvise, ebadf) { + ASSERT_SYS(0, EBADF, posix_fadvise(-1, 0, 0, POSIX_FADV_SEQUENTIAL)); +} + +TEST(fadvise, test) { + ASSERT_SYS(0, 0, xbarf("foo", "hello", -1)); + ASSERT_SYS(0, 3, open("foo", 0)); + ASSERT_SYS(0, 0, fadvise(3, 0, 0, MADV_SEQUENTIAL)); + ASSERT_SYS(0, 0, close(3)); +} + +TEST(posix_fadvise, test) { + ASSERT_SYS(0, 0, xbarf("foo", "hello", -1)); + ASSERT_SYS(0, 3, open("foo", 0)); + ASSERT_SYS(0, 0, posix_fadvise(3, 0, 0, POSIX_FADV_SEQUENTIAL)); + ASSERT_SYS(0, 0, close(3)); +} + +TEST(fadvise, testBadAdvice) { + ASSERT_SYS(0, 0, xbarf("foo", "hello", -1)); + ASSERT_SYS(0, 3, open("foo", 0)); + ASSERT_SYS(EINVAL, -1, fadvise(3, 0, 0, 127)); + ASSERT_SYS(0, 0, close(3)); +} + +TEST(posix_fadvise, testBadAdvice) { + ASSERT_SYS(0, 0, xbarf("foo", "hello", -1)); + ASSERT_SYS(0, 3, open("foo", 0)); + ASSERT_SYS(0, EINVAL, posix_fadvise(3, 0, 0, 127)); + ASSERT_SYS(0, 0, close(3)); +} + +TEST(fadvise, testPastEof_isFine) { + ASSERT_SYS(0, 0, xbarf("foo", "hello", -1)); + ASSERT_SYS(0, 3, open("foo", 0)); + ASSERT_SYS(0, 0, fadvise(3, 100, 100, MADV_SEQUENTIAL)); + ASSERT_SYS(0, 0, close(3)); +} + +TEST(fadvise, testNegativeLen_isInvalid) { + ASSERT_SYS(0, 0, xbarf("foo", "hello", -1)); + ASSERT_SYS(0, 3, open("foo", 0)); + ASSERT_SYS(EINVAL, -1, fadvise(3, 0, INT64_MIN, MADV_SEQUENTIAL)); + ASSERT_SYS(0, 0, close(3)); +} + +TEST(posix_fadvise, testNegativeLen_isInvalid) { + ASSERT_SYS(0, 0, xbarf("foo", "hello", -1)); + ASSERT_SYS(0, 3, open("foo", 0)); + ASSERT_SYS(0, EINVAL, posix_fadvise(3, 0, INT64_MIN, POSIX_FADV_SEQUENTIAL)); + ASSERT_SYS(0, 0, close(3)); +} + +TEST(fadvise, espipe) { + int fds[2]; + ASSERT_SYS(0, 0, pipe(fds)); + ASSERT_SYS(ESPIPE, -1, fadvise(3, 0, 0, MADV_SEQUENTIAL)); + ASSERT_SYS(0, 0, close(4)); + ASSERT_SYS(0, 0, close(3)); +} + +TEST(posix_fadvise, espipe) { + int fds[2]; + ASSERT_SYS(0, 0, pipe(fds)); + ASSERT_SYS(0, ESPIPE, posix_fadvise(3, 0, 0, POSIX_FADV_SEQUENTIAL)); + ASSERT_SYS(0, 0, close(4)); + ASSERT_SYS(0, 0, close(3)); +} diff --git a/test/libc/calls/splice_test.c b/test/libc/calls/splice_test.c new file mode 100644 index 000000000..c09d8aa05 --- /dev/null +++ b/test/libc/calls/splice_test.c @@ -0,0 +1,81 @@ +/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ +│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ +╞══════════════════════════════════════════════════════════════════════════════╡ +│ Copyright 2022 Justine Alexandra Roberts Tunney │ +│ │ +│ Permission to use, copy, modify, and/or distribute this software for │ +│ any purpose with or without fee is hereby granted, provided that the │ +│ above copyright notice and this permission notice appear in all copies. │ +│ │ +│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ +│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ +│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ +│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ +│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ +│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ +│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ +│ PERFORMANCE OF THIS SOFTWARE. │ +╚─────────────────────────────────────────────────────────────────────────────*/ +#include "libc/calls/calls.h" +#include "libc/dce.h" +#include "libc/errno.h" +#include "libc/runtime/runtime.h" +#include "libc/sysv/consts/o.h" +#include "libc/testlib/testlib.h" + +char testlib_enable_tmp_setup_teardown; + +void SetUp(void) { + int e = errno; + if (splice(-1, 0, -1, 0, 0, 0) == -1 && errno == ENOSYS) { + exit(0); + } + errno = e; +} + +TEST(splice, einval) { + ASSERT_SYS(0, 0, splice(0, 0, 1, 0, 0, -1)); +} + +TEST(splice, espipe) { + int64_t x; + int fds[2]; + ASSERT_SYS(0, 0, pipe(fds)); + ASSERT_SYS(0, 0, splice(0, 0, 4, &x, 0, 0)); + ASSERT_SYS(0, 0, close(4)); + ASSERT_SYS(0, 0, close(3)); +} + +TEST(splice, noOffsets_usesFilePointer) { + // this test fails on rhel5 and rhel7 + int fds[2]; + char buf[16] = {0}; + ASSERT_SYS(0, 0, pipe(fds)); + ASSERT_SYS(0, 5, open("foo", O_RDWR | O_CREAT | O_TRUNC, 0644)); + ASSERT_SYS(0, 5, pwrite(5, "hello", 5, 0)); + ASSERT_SYS(0, 5, splice(5, 0, 4, 0, 5, 0)); + ASSERT_SYS(0, 5, splice(3, 0, 5, 0, 5, 0)); + ASSERT_SYS(0, 10, pread(5, buf, sizeof(buf), 0)); + ASSERT_STREQ("hellohello", buf); + ASSERT_SYS(0, 0, close(5)); + ASSERT_SYS(0, 0, close(4)); + ASSERT_SYS(0, 0, close(3)); +} + +TEST(splice, offsets_doesntChangePointerAndIsReadOnly) { + int fds[2]; + int64_t x = 0; + char buf[16] = {0}; + ASSERT_SYS(0, 0, pipe(fds)); + ASSERT_SYS(0, 5, open("foo", O_RDWR | O_CREAT | O_TRUNC, 0644)); + ASSERT_SYS(0, 5, pwrite(5, "hello", 5, 0)); + ASSERT_SYS(0, 5, splice(5, &x, 4, 0, 5, 0)); + ASSERT_EQ(5, x); + ASSERT_SYS(0, 5, splice(3, 0, 5, &x, 5, 0)); + ASSERT_EQ(10, x); + ASSERT_SYS(0, 10, read(5, buf, sizeof(buf))); + ASSERT_STREQ("hellohello", buf); + ASSERT_SYS(0, 0, close(5)); + ASSERT_SYS(0, 0, close(4)); + ASSERT_SYS(0, 0, close(3)); +} diff --git a/test/libc/calls/unveil_test.c b/test/libc/calls/unveil_test.c index 9f4b71318..8c507ab58 100644 --- a/test/libc/calls/unveil_test.c +++ b/test/libc/calls/unveil_test.c @@ -17,6 +17,7 @@ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/calls/calls.h" +#include "libc/mem/copyfd.internal.h" #include "libc/calls/landlock.h" #include "libc/calls/struct/dirent.h" #include "libc/calls/struct/stat.h" @@ -25,7 +26,6 @@ #include "libc/errno.h" #include "libc/intrin/kprintf.h" #include "libc/mem/gc.h" -#include "libc/calls/copyfd.internal.h" #include "libc/runtime/internal.h" #include "libc/runtime/runtime.h" #include "libc/sock/sock.h" diff --git a/test/libc/intrin/asan_test.c b/test/libc/intrin/asan_test.c index 9a234b089..23618da5f 100644 --- a/test/libc/intrin/asan_test.c +++ b/test/libc/intrin/asan_test.c @@ -21,8 +21,8 @@ #include "libc/intrin/asan.internal.h" #include "libc/log/libfatal.internal.h" #include "libc/log/log.h" -#include "libc/mem/mem.h" #include "libc/mem/gc.internal.h" +#include "libc/mem/mem.h" #include "libc/runtime/runtime.h" #include "libc/stdio/stdio.h" #include "libc/str/str.h" @@ -33,6 +33,7 @@ TEST(asan, test) { char *p; if (!IsAsan()) return; p = gc(malloc(3)); + EXPECT_TRUE(__asan_is_valid(0, 0)); EXPECT_TRUE(__asan_is_valid(p, 3)); EXPECT_FALSE(__asan_is_valid(p, 4)); EXPECT_TRUE(__asan_is_valid(p + 1, 2)); diff --git a/test/libc/log/backtrace_test.c b/test/libc/log/backtrace_test.c index 2cf91549f..821c4db0f 100644 --- a/test/libc/log/backtrace_test.c +++ b/test/libc/log/backtrace_test.c @@ -18,6 +18,7 @@ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/assert.h" #include "libc/calls/calls.h" +#include "libc/mem/copyfd.internal.h" #include "libc/dce.h" #include "libc/errno.h" #include "libc/fmt/conv.h" @@ -26,7 +27,6 @@ #include "libc/log/libfatal.internal.h" #include "libc/log/log.h" #include "libc/mem/gc.h" -#include "libc/calls/copyfd.internal.h" #include "libc/mem/mem.h" #include "libc/runtime/internal.h" #include "libc/runtime/runtime.h" diff --git a/test/libc/sock/sendfile_test.c b/test/libc/sock/sendfile_test.c index 1b464bfa8..77c7025a5 100644 --- a/test/libc/sock/sendfile_test.c +++ b/test/libc/sock/sendfile_test.c @@ -17,8 +17,13 @@ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/calls/calls.h" -#include "libc/mem/mem.h" +#include "libc/calls/struct/sigaction.h" +#include "libc/dce.h" +#include "libc/errno.h" +#include "libc/intrin/kprintf.h" +#include "libc/limits.h" #include "libc/mem/gc.internal.h" +#include "libc/mem/mem.h" #include "libc/runtime/runtime.h" #include "libc/sock/sock.h" #include "libc/sock/struct/sockaddr.h" @@ -26,20 +31,31 @@ #include "libc/sysv/consts/af.h" #include "libc/sysv/consts/ipproto.h" #include "libc/sysv/consts/o.h" +#include "libc/sysv/consts/shut.h" +#include "libc/sysv/consts/sig.h" #include "libc/sysv/consts/sock.h" #include "libc/testlib/hyperion.h" #include "libc/testlib/testlib.h" +#include "libc/x/x.h" char testlib_enable_tmp_setup_teardown; void SetUpOnce(void) { + if (IsNetbsd()) exit(0); + if (IsOpenbsd()) exit(0); ASSERT_SYS(0, 0, pledge("stdio rpath wpath cpath proc inet", 0)); } -TEST(sendfile, test) { - int ws; - char *buf; - int64_t inoffset; +int64_t GetFileOffset(int fd) { + int64_t pos; + ASSERT_NE(-1, (pos = lseek(fd, 0, SEEK_CUR))); + return pos; +} + +TEST(sendfile, testSeeking) { + char buf[1024]; + int rc, ws, fds[2]; + int64_t inoffset = 0; uint32_t addrsize = sizeof(struct sockaddr_in); struct sockaddr_in addr = { .sin_family = AF_INET, @@ -53,24 +69,74 @@ TEST(sendfile, test) { ASSERT_SYS(0, 0, getsockname(3, &addr, &addrsize)); ASSERT_SYS(0, 0, listen(3, 1)); if (!fork()) { - inoffset = 0; ASSERT_SYS(0, 4, accept(3, &addr, &addrsize)); ASSERT_SYS(0, 5, open("hyperion.txt", O_RDONLY)); - ASSERT_SYS(0, 512, sendfile(4, 5, &inoffset, 512)); - EXPECT_EQ(512, inoffset); - ASSERT_SYS(0, 0, close(5)); - ASSERT_SYS(0, 0, close(4)); - ASSERT_SYS(0, 0, close(3)); + ASSERT_SYS(0, 12, sendfile(4, 5, &inoffset, 12)); + ASSERT_EQ(0, GetFileOffset(5)); + ASSERT_SYS(0, 8, read(5, buf, 8)); + ASSERT_EQ(0, memcmp(buf, "The fall", 8)); + ASSERT_EQ(8, GetFileOffset(5)); + ASSERT_SYS(EBADF, -1, sendfile(5, 5, &inoffset, -1)); + ASSERT_EQ(-1, sendfile(5, 5, &inoffset, -1)); + ASSERT_TRUE(errno == ESPIPE || errno == EBADF); + errno = 0; + ASSERT_SYS(0, 500, sendfile(4, 5, &inoffset, -1)); + ASSERT_EQ(8, GetFileOffset(5)); + ASSERT_EQ(512, inoffset); + inoffset = -1; + ASSERT_SYS(EINVAL, -1, sendfile(4, 5, &inoffset, -1)); _Exit(0); } - buf = gc(malloc(512)); - EXPECT_SYS(0, 0, close(3)); - EXPECT_SYS(0, 3, socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)); - EXPECT_SYS(0, 0, connect(3, &addr, sizeof(addr))); - EXPECT_SYS(0, 512, read(3, buf, 512)); - EXPECT_EQ(0, memcmp(buf, kHyperion, 512)); - EXPECT_SYS(0, 0, close(3)); - EXPECT_NE(-1, wait(&ws)); + ASSERT_SYS(0, 0, close(3)); + ASSERT_SYS(0, 3, socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)); + ASSERT_SYS(0, 0, connect(3, &addr, sizeof(addr))); + ASSERT_SYS(0, 12, read(3, buf, 12)); // needed due to windows + ASSERT_SYS(0, 500, read(3, buf + 12, 700)); + ASSERT_EQ(0, memcmp(buf, kHyperion, 512)); + ASSERT_SYS(0, 0, close(3)); + ASSERT_NE(-1, wait(&ws)); + ASSERT_TRUE(WIFEXITED(ws)); + ASSERT_EQ(0, WEXITSTATUS(ws)); +} + +TEST(sendfile, testPositioning) { + int ws, fds[2]; + char buf[1024]; + uint32_t addrsize = sizeof(struct sockaddr_in); + struct sockaddr_in addr = { + .sin_family = AF_INET, + .sin_addr.s_addr = htonl(0x7f000001), + }; + ASSERT_SYS(0, 3, creat("hyperion.txt", 0644)); + ASSERT_SYS(0, 512, write(3, kHyperion, 512)); + ASSERT_SYS(0, 0, close(3)); + ASSERT_SYS(0, 3, socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)); + ASSERT_SYS(0, 0, bind(3, &addr, sizeof(addr))); + ASSERT_SYS(0, 0, getsockname(3, &addr, &addrsize)); + ASSERT_SYS(0, 0, listen(3, 1)); + if (!fork()) { + signal(SIGPIPE, SIG_IGN); + ASSERT_SYS(0, 4, accept(3, &addr, &addrsize)); + ASSERT_SYS(0, 5, open("hyperion.txt", O_RDONLY)); + ASSERT_SYS(0, 6, sendfile(4, 5, 0, 6)); + ASSERT_EQ(6, GetFileOffset(5)); + ASSERT_SYS(0, 6, sendfile(4, 5, 0, 6)); + ASSERT_SYS(0, 0, shutdown(4, SHUT_WR)); + ASSERT_EQ(-1, sendfile(4, 5, 0, 6)); + ASSERT_TRUE(errno == EINVAL || errno == EPIPE); + errno = 0; + ASSERT_EQ(12, GetFileOffset(5)); + _Exit(0); + } + ASSERT_SYS(0, 0, close(3)); + ASSERT_SYS(0, 3, socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)); + ASSERT_SYS(0, 0, connect(3, &addr, sizeof(addr))); + ASSERT_SYS(0, 6, read(3, buf, 6)); + ASSERT_SYS(0, 6, read(3, buf + 6, 6)); + ASSERT_SYS(0, 0, read(3, buf, 12)); + ASSERT_EQ(0, memcmp(buf, kHyperion, 12)); + ASSERT_SYS(0, 0, close(3)); + ASSERT_NE(-1, wait(&ws)); ASSERT_TRUE(WIFEXITED(ws)); ASSERT_EQ(0, WEXITSTATUS(ws)); } diff --git a/test/tool/plinko/plinko_test.c b/test/tool/plinko/plinko_test.c index e35bd896e..f49a14e6b 100644 --- a/test/tool/plinko/plinko_test.c +++ b/test/tool/plinko/plinko_test.c @@ -17,7 +17,7 @@ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/calls/calls.h" -#include "libc/calls/copyfd.internal.h" +#include "libc/mem/copyfd.internal.h" #include "libc/calls/struct/sigaction.h" #include "libc/errno.h" #include "libc/intrin/kprintf.h" diff --git a/third_party/zlib/insert_string.internal.h b/third_party/zlib/insert_string.internal.h index e02377ce3..2d2b83a82 100644 --- a/third_party/zlib/insert_string.internal.h +++ b/third_party/zlib/insert_string.internal.h @@ -51,7 +51,9 @@ COSMOPOLITAN_C_START_ #if defined(TARGET_CPU_WITH_CRC) -local INLINE Pos insert_string_simd(deflate_state* const s, const Pos str) { +// TODO(jart): Why does this fail alignment check? +noubsan local INLINE Pos insert_string_simd(deflate_state* const s, + const Pos str) { Pos ret; unsigned *ip, val, h = 0; diff --git a/tool/build/ar.c b/tool/build/ar.c index b94cb9896..42b775be5 100644 --- a/tool/build/ar.c +++ b/tool/build/ar.c @@ -18,7 +18,7 @@ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/assert.h" #include "libc/calls/calls.h" -#include "libc/calls/copyfd.internal.h" +#include "libc/mem/copyfd.internal.h" #include "libc/calls/struct/iovec.h" #include "libc/calls/struct/stat.h" #include "libc/elf/elf.h" diff --git a/tool/build/compile.c b/tool/build/compile.c index 9540b33e3..9a96c7e70 100644 --- a/tool/build/compile.c +++ b/tool/build/compile.c @@ -17,7 +17,7 @@ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/calls/calls.h" -#include "libc/calls/copyfd.internal.h" +#include "libc/mem/copyfd.internal.h" #include "libc/calls/copyfile.h" #include "libc/calls/ioctl.h" #include "libc/calls/struct/itimerval.h" @@ -755,14 +755,14 @@ bool MovePreservingDestinationInode(const char *from, const char *to) { rc = copy_file_range(fdin, 0, fdout, 0, remain, 0); if (rc != -1) { remain -= rc; - } else if (errno == EXDEV) { + } else if (errno == EXDEV || errno == ENOSYS) { if (lseek(fdin, 0, SEEK_SET) == -1) { - kprintf("%s: failed to lseek after exdev\n", from); + kprintf("%s: failed to lseek\n", from); res = false; break; } if (lseek(fdout, 0, SEEK_SET) == -1) { - kprintf("%s: failed to lseek after exdev\n", to); + kprintf("%s: failed to lseek\n", to); res = false; break; } diff --git a/tool/build/cp.c b/tool/build/cp.c index 4e11bb982..2a5ec4248 100644 --- a/tool/build/cp.c +++ b/tool/build/cp.c @@ -17,7 +17,7 @@ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/calls/calls.h" -#include "libc/calls/copyfd.internal.h" +#include "libc/mem/copyfd.internal.h" #include "libc/calls/copyfile.h" #include "libc/calls/struct/stat.h" #include "libc/errno.h" diff --git a/tool/build/ocat.c b/tool/build/ocat.c index c30a929ca..f6ccdbdfd 100644 --- a/tool/build/ocat.c +++ b/tool/build/ocat.c @@ -17,7 +17,7 @@ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/calls/calls.h" -#include "libc/calls/copyfd.internal.h" +#include "libc/mem/copyfd.internal.h" #include "libc/errno.h" #include "libc/fmt/itoa.h" #include "libc/runtime/runtime.h" diff --git a/tool/build/pledge.c b/tool/build/pledge.c index be6c47df7..4a0a33ea6 100644 --- a/tool/build/pledge.c +++ b/tool/build/pledge.c @@ -18,7 +18,7 @@ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/assert.h" #include "libc/calls/calls.h" -#include "libc/calls/copyfd.internal.h" +#include "libc/mem/copyfd.internal.h" #include "libc/calls/landlock.h" #include "libc/calls/pledge.h" #include "libc/calls/pledge.internal.h" diff --git a/tool/viz/printpeb.c b/tool/viz/printpeb.c index a9d81660c..889144ed2 100644 --- a/tool/viz/printpeb.c +++ b/tool/viz/printpeb.c @@ -16,11 +16,12 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/intrin/safemacros.internal.h" #include "libc/calls/calls.h" #include "libc/calls/internal.h" #include "libc/dce.h" +#include "libc/intrin/safemacros.internal.h" #include "libc/log/log.h" +#include "libc/mem/copyfd.internal.h" #include "libc/nt/dll.h" #include "libc/nt/enum/filetype.h" #include "libc/nt/enum/startf.h" @@ -56,7 +57,7 @@ int NextBestThing(void) { int64_t fd = open("/proc/self/maps", O_RDONLY); posix_fadvise(fd, 0, 0, MADV_SEQUENTIAL); ssize_t wrote; - while ((wrote = copyfd(fd, NULL, 1, NULL, 1024 * 64, 0)) != -1) { + while ((wrote = _copyfd(fd, 1, -1)) != -1) { if (wrote == 0) break; } close(fd);