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
This commit is contained in:
Justine Tunney 2022-09-19 15:01:48 -07:00
parent 224c12f54d
commit c7a8cd21e9
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
89 changed files with 1151 additions and 414 deletions

93
libc/calls/_ptsname.c Normal file
View file

@ -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();
}
}

View file

@ -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 \

View file

@ -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;

View file

@ -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;

View file

@ -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;
}

View file

@ -1,10 +0,0 @@
#ifndef COSMOPOLITAN_LIBC_MEM_IO_H_
#define COSMOPOLITAN_LIBC_MEM_IO_H_
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
ssize_t _copyfd(int, int, size_t);
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_MEM_IO_H_ */

View file

@ -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 |

View file

@ -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;

View file

@ -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);

View file

@ -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.

View file

@ -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;
}
}

View file

@ -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;
}

View file

@ -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()) {

View file

@ -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;
}

View file

@ -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;
}

View file

@ -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();

View file

@ -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;
}

View file

@ -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;

View file

@ -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);

View file

@ -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);

View file

@ -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;
}

View file

@ -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

View file

@ -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) */

View file

@ -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) {

View file

@ -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_ */

View file

@ -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,17 @@
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/calls/termios.internal.h"
#include "libc/dce.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;
}
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;
}
return i;
}

View file

@ -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()
*/

View file

@ -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();