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-09-15 04:29:50 +00:00
|
|
|
#include "libc/assert.h"
|
|
|
|
#include "libc/calls/calls.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/calls/internal.h"
|
2022-04-15 06:39:48 +00:00
|
|
|
#include "libc/calls/sig.internal.h"
|
2022-08-13 20:11:56 +00:00
|
|
|
#include "libc/calls/struct/fd.internal.h"
|
|
|
|
#include "libc/calls/struct/iovec.h"
|
|
|
|
#include "libc/calls/struct/iovec.internal.h"
|
2022-05-23 22:06:11 +00:00
|
|
|
#include "libc/calls/syscall_support-nt.internal.h"
|
2022-08-13 20:11:56 +00:00
|
|
|
#include "libc/calls/wincrash.internal.h"
|
2023-08-08 11:00:29 +00:00
|
|
|
#include "libc/errno.h"
|
2022-09-15 04:29:50 +00:00
|
|
|
#include "libc/intrin/strace.internal.h"
|
2023-07-23 17:57:18 +00:00
|
|
|
#include "libc/macros.internal.h"
|
2022-04-16 17:40:23 +00:00
|
|
|
#include "libc/nt/enum/filetype.h"
|
2023-08-08 11:00:29 +00:00
|
|
|
#include "libc/nt/enum/wait.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/nt/errors.h"
|
2023-08-08 11:00:29 +00:00
|
|
|
#include "libc/nt/events.h"
|
2022-04-15 06:39:48 +00:00
|
|
|
#include "libc/nt/files.h"
|
|
|
|
#include "libc/nt/ipc.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/nt/runtime.h"
|
|
|
|
#include "libc/nt/struct/overlapped.h"
|
2022-04-15 06:39:48 +00:00
|
|
|
#include "libc/nt/synchronization.h"
|
2023-08-08 11:00:29 +00:00
|
|
|
#include "libc/nt/thread.h"
|
|
|
|
#include "libc/str/str.h"
|
Support non-blocking i/o across platforms
This change introduces new tests for `O_NONBLOCK` and `SOCK_NONBLOCK` to
confirm that non-blocking i/o is now working on all supported platforms,
including Windows. For example, you can now say on Windows, MacOS, etc.:
socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, IPPROTO_TCP);
To create a non-blocking IPv4 TCP socket. Or you can enable non-blocking
i/o on an existing socket / pipe / etc. file descriptor by calling fcntl
fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
This functionality is polyfilled on older Linux kernels too, e.g. RHEL5.
Now that fcntl() support is much better the FIOCLEX / FIONCLEX polyfills
for ioctl() have been removed since they're ugly non-POSIX diameond APIs
This change fixes a weakness in kprintf() that was causing Windows trace
tools to frequently crash.
2023-07-23 09:56:47 +00:00
|
|
|
#include "libc/sysv/consts/o.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/sysv/errfuns.h"
|
|
|
|
|
2023-08-08 11:00:29 +00:00
|
|
|
static textwindows void sys_read_nt_abort(int64_t handle,
|
|
|
|
struct NtOverlapped *overlapped) {
|
|
|
|
unassert(CancelIoEx(handle, overlapped) ||
|
|
|
|
GetLastError() == kNtErrorNotFound);
|
|
|
|
}
|
2022-09-15 04:29:50 +00:00
|
|
|
|
2023-08-08 11:00:29 +00:00
|
|
|
static textwindows void MungeTerminalInput(struct Fd *fd, char *p, size_t n) {
|
|
|
|
if (!(fd->ttymagic & kFdTtyNoCr2Nl)) {
|
|
|
|
size_t i;
|
|
|
|
for (i = 0; i < n; ++i) {
|
|
|
|
if (p[i] == '\r') {
|
|
|
|
p[i] = '\n';
|
2022-04-16 17:40:23 +00:00
|
|
|
}
|
2023-08-08 11:00:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Manual CMD.EXE echoing for when !ICANON && ECHO is the case.
|
|
|
|
static textwindows void EchoTerminalInput(struct Fd *fd, char *p, size_t n) {
|
|
|
|
int64_t hOutput;
|
|
|
|
if (fd->kind == kFdConsole) {
|
|
|
|
hOutput = fd->extra;
|
|
|
|
} else {
|
|
|
|
hOutput = g_fds.p[1].handle;
|
|
|
|
}
|
|
|
|
if (fd->ttymagic & kFdTtyEchoRaw) {
|
|
|
|
WriteFile(hOutput, p, n, 0, 0);
|
|
|
|
} else {
|
|
|
|
size_t i;
|
|
|
|
for (i = 0; i < n; ++i) {
|
|
|
|
if (isascii(p[i]) && iscntrl(p[i]) && p[i] != '\n' && p[i] != '\t') {
|
|
|
|
char ctl[2];
|
|
|
|
ctl[0] = '^';
|
|
|
|
ctl[1] = p[i] ^ 0100;
|
|
|
|
WriteFile(hOutput, ctl, 2, 0, 0);
|
|
|
|
} else {
|
|
|
|
WriteFile(hOutput, p + i, 1, 0, 0);
|
2022-04-15 06:39:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-08-08 11:00:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static textwindows ssize_t sys_read_nt_impl(struct Fd *fd, void *data,
|
|
|
|
size_t size, int64_t offset) {
|
2022-09-15 04:29:50 +00:00
|
|
|
|
2023-07-23 17:57:18 +00:00
|
|
|
// perform the read i/o operation
|
|
|
|
bool32 ok;
|
|
|
|
uint32_t got;
|
2023-08-08 11:00:29 +00:00
|
|
|
int filetype;
|
2023-08-14 05:42:25 +00:00
|
|
|
int64_t handle;
|
2023-08-08 11:00:29 +00:00
|
|
|
int abort_errno = EAGAIN;
|
2023-07-23 17:57:18 +00:00
|
|
|
size = MIN(size, 0x7ffff000);
|
2023-08-14 05:42:25 +00:00
|
|
|
handle = __resolve_stdin_handle(fd->handle);
|
|
|
|
filetype = GetFileType(handle);
|
2023-08-08 11:00:29 +00:00
|
|
|
if (filetype == kNtFileTypeChar || filetype == kNtFileTypePipe) {
|
|
|
|
struct NtOverlapped overlap = {0};
|
|
|
|
if (offset != -1) {
|
|
|
|
// pread() and pwrite() should not be called on a pipe or tty
|
|
|
|
return espipe();
|
|
|
|
}
|
|
|
|
if ((overlap.hEvent = CreateEvent(0, 0, 0, 0))) {
|
|
|
|
// the win32 manual says it's important to *not* put &got here
|
|
|
|
// since for overlapped i/o, we always use GetOverlappedResult
|
2023-08-14 05:42:25 +00:00
|
|
|
ok = ReadFile(handle, data, size, 0, &overlap);
|
2023-08-08 11:00:29 +00:00
|
|
|
if (!ok && GetLastError() == kNtErrorIoPending) {
|
|
|
|
// i/o operation is in flight; blocking is unavoidable
|
|
|
|
// if we're in non-blocking mode, then immediately abort
|
|
|
|
// if an interrupt is pending, then abort before waiting
|
|
|
|
// otherwise wait for i/o periodically checking interrupts
|
|
|
|
if (fd->flags & O_NONBLOCK) {
|
2023-08-14 05:42:25 +00:00
|
|
|
sys_read_nt_abort(handle, &overlap);
|
2023-08-08 11:00:29 +00:00
|
|
|
} else if (_check_interrupts(kSigOpRestartable, g_fds.p)) {
|
|
|
|
Interrupted:
|
|
|
|
abort_errno = errno;
|
2023-08-14 05:42:25 +00:00
|
|
|
sys_read_nt_abort(handle, &overlap);
|
2023-08-08 11:00:29 +00:00
|
|
|
} else {
|
|
|
|
for (;;) {
|
|
|
|
uint32_t i;
|
|
|
|
i = WaitForSingleObject(overlap.hEvent, __SIG_POLLING_INTERVAL_MS);
|
|
|
|
if (i == kNtWaitTimeout) {
|
|
|
|
if (_check_interrupts(kSigOpRestartable, g_fds.p)) {
|
|
|
|
goto Interrupted;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
npassert(!i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ok = true;
|
|
|
|
}
|
|
|
|
if (ok) {
|
|
|
|
// overlapped is allocated on stack, so it's important we wait
|
|
|
|
// for windows to acknowledge that it's done using that memory
|
2023-08-14 05:42:25 +00:00
|
|
|
ok = GetOverlappedResult(handle, &overlap, &got, true);
|
2023-08-08 11:00:29 +00:00
|
|
|
}
|
|
|
|
CloseHandle(overlap.hEvent);
|
|
|
|
} else {
|
|
|
|
ok = false;
|
|
|
|
}
|
|
|
|
} else if (offset == -1) {
|
|
|
|
// perform simple blocking file read
|
2023-08-14 05:42:25 +00:00
|
|
|
ok = ReadFile(handle, data, size, &got, 0);
|
2023-07-23 17:57:18 +00:00
|
|
|
} else {
|
2023-08-08 11:00:29 +00:00
|
|
|
// perform pread()-style file read at particular file offset
|
2023-07-23 17:57:18 +00:00
|
|
|
int64_t position;
|
|
|
|
// save file pointer which windows clobbers, even for overlapped i/o
|
2023-08-14 05:42:25 +00:00
|
|
|
if (!SetFilePointerEx(handle, 0, &position, SEEK_CUR)) {
|
2023-07-23 17:57:18 +00:00
|
|
|
return __winerr(); // fd probably isn't seekable?
|
|
|
|
}
|
|
|
|
struct NtOverlapped overlap = {0};
|
|
|
|
overlap.Pointer = (void *)(uintptr_t)offset;
|
2023-08-14 05:42:25 +00:00
|
|
|
ok = ReadFile(handle, data, size, 0, &overlap);
|
2023-07-23 17:57:18 +00:00
|
|
|
if (!ok && GetLastError() == kNtErrorIoPending) ok = true;
|
2023-08-14 05:42:25 +00:00
|
|
|
if (ok) ok = GetOverlappedResult(handle, &overlap, &got, true);
|
2023-07-23 17:57:18 +00:00
|
|
|
// restore file pointer which windows clobbers, even on error
|
2023-08-14 05:42:25 +00:00
|
|
|
unassert(SetFilePointerEx(handle, position, 0, SEEK_SET));
|
2022-10-03 05:14:33 +00:00
|
|
|
}
|
|
|
|
if (ok) {
|
2023-08-08 11:00:29 +00:00
|
|
|
if (fd->ttymagic & kFdTtyMunging) {
|
|
|
|
MungeTerminalInput(fd, data, got);
|
|
|
|
}
|
|
|
|
if (fd->ttymagic & kFdTtyEchoing) {
|
|
|
|
EchoTerminalInput(fd, data, got);
|
|
|
|
}
|
2021-03-09 19:21:08 +00:00
|
|
|
return got;
|
|
|
|
}
|
2022-09-15 04:29:50 +00:00
|
|
|
|
2022-04-20 16:56:53 +00:00
|
|
|
switch (GetLastError()) {
|
|
|
|
case kNtErrorBrokenPipe: // broken pipe
|
|
|
|
case kNtErrorNoData: // closing named pipe
|
|
|
|
case kNtErrorHandleEof: // pread read past EOF
|
|
|
|
return 0; //
|
|
|
|
case kNtErrorAccessDenied: // read doesn't return EACCESS
|
|
|
|
return ebadf(); //
|
2023-08-08 11:00:29 +00:00
|
|
|
case kNtErrorOperationAborted:
|
|
|
|
errno = abort_errno;
|
|
|
|
return -1;
|
2022-04-20 16:56:53 +00:00
|
|
|
default:
|
|
|
|
return __winerr();
|
|
|
|
}
|
2021-03-09 19:21:08 +00:00
|
|
|
}
|
|
|
|
|
2021-02-04 03:35:29 +00:00
|
|
|
textwindows ssize_t sys_read_nt(struct Fd *fd, const struct iovec *iov,
|
2023-07-23 17:57:18 +00:00
|
|
|
size_t iovlen, int64_t opt_offset) {
|
2021-03-09 19:21:08 +00:00
|
|
|
ssize_t rc;
|
|
|
|
uint32_t size;
|
2021-02-07 14:11:44 +00:00
|
|
|
size_t i, total;
|
2022-09-15 04:29:50 +00:00
|
|
|
if (opt_offset < -1) return einval();
|
2023-07-30 15:55:01 +00:00
|
|
|
if (_check_interrupts(kSigOpRestartable, fd)) return -1;
|
2020-06-15 14:18:57 +00:00
|
|
|
while (iovlen && !iov[0].iov_len) iov++, iovlen--;
|
2021-02-07 14:11:44 +00:00
|
|
|
if (iovlen) {
|
|
|
|
for (total = i = 0; i < iovlen; ++i) {
|
2021-03-09 19:21:08 +00:00
|
|
|
if (!iov[i].iov_len) continue;
|
|
|
|
rc = sys_read_nt_impl(fd, iov[i].iov_base, iov[i].iov_len, opt_offset);
|
|
|
|
if (rc == -1) return -1;
|
|
|
|
total += rc;
|
|
|
|
if (opt_offset != -1) opt_offset += rc;
|
|
|
|
if (rc < iov[i].iov_len) break;
|
2021-02-07 14:11:44 +00:00
|
|
|
}
|
|
|
|
return total;
|
2020-06-15 14:18:57 +00:00
|
|
|
} else {
|
2021-03-09 19:21:08 +00:00
|
|
|
return sys_read_nt_impl(fd, NULL, 0, opt_offset);
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
|
|
|
}
|