2020-06-15 14:18:57 +00:00
|
|
|
/*-*- 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 │
|
|
|
|
│ │
|
2020-12-28 01:18:44 +00:00
|
|
|
│ 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. │
|
2020-06-15 14:18:57 +00:00
|
|
|
│ │
|
2020-12-28 01:18:44 +00:00
|
|
|
│ 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. │
|
2020-06-15 14:18:57 +00:00
|
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
2022-08-11 19:13:18 +00:00
|
|
|
#include "libc/intrin/likely.h"
|
|
|
|
#include "libc/intrin/weaken.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/calls/calls.h"
|
|
|
|
#include "libc/calls/internal.h"
|
2022-03-23 13:31:55 +00:00
|
|
|
#include "libc/calls/strace.internal.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/calls/struct/iovec.h"
|
2022-05-23 22:06:11 +00:00
|
|
|
#include "libc/calls/syscall-sysv.internal.h"
|
2021-05-14 12:36:58 +00:00
|
|
|
#include "libc/intrin/asan.internal.h"
|
2022-05-12 13:43:59 +00:00
|
|
|
#include "libc/intrin/describeflags.internal.h"
|
2022-04-18 07:01:26 +00:00
|
|
|
#include "libc/intrin/kprintf.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/sock/internal.h"
|
|
|
|
#include "libc/sysv/errfuns.h"
|
2020-11-28 20:01:51 +00:00
|
|
|
#include "libc/zipos/zipos.internal.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Reads data to multiple buffers.
|
|
|
|
*
|
2022-04-16 17:40:23 +00:00
|
|
|
* This is the same thing as read() except it has multiple buffers.
|
|
|
|
* This yields a performance boost in situations where it'd be expensive
|
|
|
|
* to stitch data together using memcpy() or issuing multiple syscalls.
|
|
|
|
* This wrapper is implemented so that readv() calls where iovlen<2 may
|
|
|
|
* be passed to the kernel as read() instead. This yields a 100 cycle
|
|
|
|
* performance boost in the case of a single small iovec.
|
|
|
|
*
|
2020-06-15 14:18:57 +00:00
|
|
|
* @return number of bytes actually read, or -1 w/ errno
|
2022-03-25 14:11:44 +00:00
|
|
|
* @restartable
|
2020-06-15 14:18:57 +00:00
|
|
|
*/
|
|
|
|
ssize_t readv(int fd, const struct iovec *iov, int iovlen) {
|
2022-04-18 07:01:26 +00:00
|
|
|
int i;
|
2022-04-18 15:54:42 +00:00
|
|
|
ssize_t rc;
|
2021-05-14 12:36:58 +00:00
|
|
|
if (fd >= 0 && iovlen >= 0) {
|
2022-03-23 13:31:55 +00:00
|
|
|
if (IsAsan() && !__asan_is_valid_iov(iov, iovlen)) {
|
|
|
|
rc = efault();
|
|
|
|
} else if (fd < g_fds.n && g_fds.p[fd].kind == kFdZip) {
|
|
|
|
rc = weaken(__zipos_read)(
|
2021-05-14 12:36:58 +00:00
|
|
|
(struct ZiposHandle *)(intptr_t)g_fds.p[fd].handle, iov, iovlen, -1);
|
|
|
|
} else if (!IsWindows() && !IsMetal()) {
|
2022-04-16 17:40:23 +00:00
|
|
|
if (iovlen == 1) {
|
|
|
|
rc = sys_read(fd, iov[0].iov_base, iov[0].iov_len);
|
|
|
|
} else {
|
|
|
|
rc = sys_readv(fd, iov, iovlen);
|
|
|
|
}
|
2021-05-14 12:36:58 +00:00
|
|
|
} else if (fd >= g_fds.n) {
|
2022-03-23 13:31:55 +00:00
|
|
|
rc = ebadf();
|
2021-05-14 12:36:58 +00:00
|
|
|
} else if (IsMetal()) {
|
2022-03-23 13:31:55 +00:00
|
|
|
rc = sys_readv_metal(g_fds.p + fd, iov, iovlen);
|
2021-05-14 12:36:58 +00:00
|
|
|
} else {
|
2022-03-23 13:31:55 +00:00
|
|
|
rc = sys_readv_nt(g_fds.p + fd, iov, iovlen);
|
2021-05-14 12:36:58 +00:00
|
|
|
}
|
2021-02-26 02:30:17 +00:00
|
|
|
} else {
|
2022-03-23 13:31:55 +00:00
|
|
|
rc = einval();
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
2022-04-18 07:01:26 +00:00
|
|
|
#if defined(SYSDEBUG) && _DATATRACE
|
2022-05-29 15:14:55 +00:00
|
|
|
if (UNLIKELY(__strace > 0)) {
|
2022-04-18 07:01:26 +00:00
|
|
|
if (rc == -1 && errno == EFAULT) {
|
|
|
|
STRACE("readv(%d, %p, %d) → %'zd% m", fd, iov, iovlen, rc);
|
|
|
|
} else {
|
2022-04-18 15:54:42 +00:00
|
|
|
kprintf(STRACE_PROLOGUE "readv(%d, [", fd);
|
2022-05-12 13:43:59 +00:00
|
|
|
DescribeIov(iov, iovlen, rc != -1 ? rc : 0);
|
2022-04-24 16:59:22 +00:00
|
|
|
kprintf("], %d) → %'ld% m\n", iovlen, rc);
|
2022-04-18 07:01:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2022-03-23 13:31:55 +00:00
|
|
|
return rc;
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|