Put more thought into i/o polyfills

wait4() is now solid enough to run `make -j100` on Windows. You can now
use MSG_DONTWAIT on Windows. There was a handle leak in accept() that's
been fixed. Our WIN32 overlapped i/o code has been simplified. Priority
class now inherits into subprocesses, so the verynice command will work
and the signal mask will now be inherited by execve() and posix_spawn()
This commit is contained in:
Justine Tunney 2023-11-06 16:38:44 -08:00
parent 736fdb757a
commit e961385e55
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
52 changed files with 679 additions and 487 deletions

View file

@ -109,7 +109,7 @@ textwindows int sys_accept_nt(struct Fd *f, struct sockaddr_storage *addr,
if ((resources.handle = WSASocket(f->family, f->type, f->protocol, 0, 0,
kNtWsaFlagOverlapped)) == -1) {
client = __winsockerr();
goto WeFailed;
goto Finish;
}
// accept network connection
@ -118,7 +118,10 @@ textwindows int sys_accept_nt(struct Fd *f, struct sockaddr_storage *addr,
ssize_t bytes_received = __winsock_block(
resources.handle, 0, !!(f->flags & O_NONBLOCK), f->rcvtimeo, m,
sys_accept_nt_start, &(struct AcceptArgs){f->handle, &buffer});
if (bytes_received == -1) goto WeFailed;
if (bytes_received == -1) {
__imp_closesocket(resources.handle);
goto Finish;
}
// create file descriptor for new socket
// don't inherit the file open mode bits
@ -138,7 +141,7 @@ textwindows int sys_accept_nt(struct Fd *f, struct sockaddr_storage *addr,
memcpy(addr, &buffer.remote.addr, sizeof(*addr));
g_fds.p[client].kind = kFdSocket;
WeFailed:
Finish:
pthread_cleanup_pop(false);
__sig_unblock(m);
if (client == -1 && errno == ECONNRESET) {

View file

@ -25,8 +25,14 @@
#include "libc/sock/internal.h"
#include "libc/sock/syscall_fd.internal.h"
#include "libc/sysv/consts/o.h"
#include "libc/sysv/errfuns.h"
#ifdef __x86_64__
#define _MSG_OOB 1
#define _MSG_PEEK 2
#define _MSG_WAITALL 8
#define _MSG_DONTWAIT 64
struct RecvArgs {
const struct iovec *iov;
size_t iovlen;
@ -44,11 +50,17 @@ static textwindows int sys_recv_nt_start(int64_t handle,
textwindows ssize_t sys_recv_nt(int fd, const struct iovec *iov, size_t iovlen,
uint32_t flags) {
if (flags & ~(_MSG_DONTWAIT | _MSG_OOB | _MSG_PEEK | _MSG_WAITALL)) {
return einval();
}
ssize_t rc;
struct Fd *f = g_fds.p + fd;
sigset_t m = __sig_block();
rc = __winsock_block(f->handle, flags, !!(f->flags & O_NONBLOCK), f->rcvtimeo,
m, sys_recv_nt_start, &(struct RecvArgs){iov, iovlen});
bool nonblock = !(flags & _MSG_WAITALL) &&
((f->flags & O_NONBLOCK) || (flags & _MSG_DONTWAIT));
flags &= ~_MSG_DONTWAIT;
rc = __winsock_block(f->handle, flags, nonblock, f->rcvtimeo, m,
sys_recv_nt_start, &(struct RecvArgs){iov, iovlen});
__sig_unblock(m);
return rc;
}

View file

@ -33,7 +33,7 @@
* @param fd is the file descriptor returned by socket()
* @param buf is where received network data gets copied
* @param size is the byte capacity of buf
* @param flags can have MSG_{WAITALL,PEEK,OOB}, etc.
* @param flags can have `MSG_OOB`, `MSG_PEEK`, `MSG_DONTWAIT`, `MSG_WAITALL`
* @return number of bytes received, 0 on remote close, or -1 w/ errno
* @error EINTR, EHOSTUNREACH, ECONNRESET (UDP ICMP Port Unreachable),
* EPIPE (if MSG_NOSIGNAL), EMSGSIZE, ENOTSOCK, EFAULT, etc.

View file

@ -24,9 +24,15 @@
#include "libc/nt/winsock.h"
#include "libc/sock/internal.h"
#include "libc/sock/syscall_fd.internal.h"
#include "libc/sysv/consts/msg.h"
#include "libc/sysv/consts/o.h"
#include "libc/sysv/errfuns.h"
#ifdef __x86_64__
#define _MSG_OOB 1
#define _MSG_PEEK 2
#define _MSG_DONTWAIT 64
struct RecvFromArgs {
const struct iovec *iov;
size_t iovlen;
@ -48,11 +54,14 @@ textwindows ssize_t sys_recvfrom_nt(int fd, const struct iovec *iov,
size_t iovlen, uint32_t flags,
void *opt_out_srcaddr,
uint32_t *opt_inout_srcaddrsize) {
if (flags & ~(_MSG_DONTWAIT | _MSG_OOB | _MSG_PEEK)) return einval();
ssize_t rc;
struct Fd *f = g_fds.p + fd;
sigset_t m = __sig_block();
rc = __winsock_block(f->handle, flags, !!(f->flags & O_NONBLOCK), f->rcvtimeo,
m, sys_recvfrom_nt_start,
bool nonblock = (f->flags & O_NONBLOCK) || (flags & _MSG_DONTWAIT);
flags &= ~_MSG_DONTWAIT;
rc = __winsock_block(f->handle, flags, nonblock, f->rcvtimeo, m,
sys_recvfrom_nt_start,
&(struct RecvFromArgs){iov, iovlen, opt_out_srcaddr,
opt_inout_srcaddrsize});
__sig_unblock(m);

View file

@ -34,15 +34,15 @@
/**
* Receives data from network.
*
* This function blocks unless MSG_DONTWAIT is passed. In that case, the
* non-error EWOULDBLOCK might be returned. It basically means we didn't
* wait around to learn an amount of bytes were written that we know in
* advance are guaranteed to be atomic.
*
* @param fd is the file descriptor returned by socket()
* @param buf is where received network data gets copied
* @param size is the byte capacity of buf
* @param flags is a bitmask which may contain any of the following:
* - `MSG_DONTWAIT` to force `O_NONBLOCK` behavior for this call
* - `MSG_OOB` is broadly supported (untested by cosmo)
* - `MSG_PEEK` is broadly supported (untested by cosmo)
* - `MSG_WAITALL` is broadly supported (untested by cosmo)
* - `MSG_DONTROUTE` is broadly supported (untested by cosmo)
* @param flags can have `MSG_OOB`, `MSG_PEEK`, and `MSG_DONTWAIT`
* @param opt_out_srcaddr receives the binary ip:port of the data's origin
* @param opt_inout_srcaddrsize is srcaddr capacity which gets updated
* @return number of bytes received, 0 on remote close, or -1 w/ errno

View file

@ -25,8 +25,13 @@
#include "libc/sock/internal.h"
#include "libc/sock/syscall_fd.internal.h"
#include "libc/sysv/consts/o.h"
#include "libc/sysv/errfuns.h"
#ifdef __x86_64__
#define _MSG_OOB 1
#define _MSG_DONTROUTE 4
#define _MSG_DONTWAIT 64
struct SendArgs {
const struct iovec *iov;
size_t iovlen;
@ -44,11 +49,14 @@ static textwindows int sys_send_nt_start(int64_t handle,
textwindows ssize_t sys_send_nt(int fd, const struct iovec *iov, size_t iovlen,
uint32_t flags) {
if (flags & ~(_MSG_DONTWAIT | _MSG_OOB | _MSG_DONTROUTE)) return einval();
ssize_t rc;
struct Fd *f = g_fds.p + fd;
sigset_t m = __sig_block();
rc = __winsock_block(f->handle, flags, !!(f->flags & O_NONBLOCK), f->sndtimeo,
m, sys_send_nt_start, &(struct SendArgs){iov, iovlen});
bool nonblock = (f->flags & O_NONBLOCK) || (flags & _MSG_DONTWAIT);
flags &= ~_MSG_DONTWAIT;
rc = __winsock_block(f->handle, flags, nonblock, f->sndtimeo, m,
sys_send_nt_start, &(struct SendArgs){iov, iovlen});
__sig_unblock(m);
return rc;
}

View file

@ -34,7 +34,7 @@
* @param fd is the file descriptor returned by socket()
* @param buf is the data to send, which we'll copy if necessary
* @param size is the byte-length of buf
* @param flags MSG_OOB, MSG_DONTROUTE, MSG_PARTIAL, MSG_NOSIGNAL, etc.
* @param flags can have `MSG_OOB`, `MSG_DONTROUTE`, and `MSG_DONTWAIT`
* @return number of bytes transmitted, or -1 w/ errno
* @error EINTR, EHOSTUNREACH, ECONNRESET (UDP ICMP Port Unreachable),
* EPIPE (if MSG_NOSIGNAL), EMSGSIZE, ENOTSOCK, EFAULT, etc.

View file

@ -25,8 +25,13 @@
#include "libc/sock/internal.h"
#include "libc/sock/syscall_fd.internal.h"
#include "libc/sysv/consts/o.h"
#include "libc/sysv/errfuns.h"
#ifdef __x86_64__
#define _MSG_OOB 1
#define _MSG_DONTROUTE 4
#define _MSG_DONTWAIT 64
struct SendToArgs {
const struct iovec *iov;
size_t iovlen;
@ -47,12 +52,14 @@ static textwindows int sys_sendto_nt_start(int64_t handle,
textwindows ssize_t sys_sendto_nt(int fd, const struct iovec *iov,
size_t iovlen, uint32_t flags,
void *opt_in_addr, uint32_t in_addrsize) {
if (flags & ~(_MSG_DONTWAIT | _MSG_OOB | _MSG_DONTROUTE)) return einval();
ssize_t rc;
struct Fd *f = g_fds.p + fd;
sigset_t m = __sig_block();
bool nonblock = (f->flags & O_NONBLOCK) || (flags & _MSG_DONTWAIT);
flags &= ~_MSG_DONTWAIT;
rc = __winsock_block(
f->handle, flags, !!(f->flags & O_NONBLOCK), f->sndtimeo, m,
sys_sendto_nt_start,
f->handle, flags, nonblock, f->sndtimeo, m, sys_sendto_nt_start,
&(struct SendToArgs){iov, iovlen, opt_in_addr, in_addrsize});
__sig_unblock(m);
return rc;

View file

@ -43,7 +43,7 @@
* @param fd is the file descriptor returned by socket()
* @param buf is the data to send, which we'll copy if necessary
* @param size is the byte-length of buf
* @param flags MSG_OOB, MSG_DONTROUTE, MSG_PARTIAL, MSG_NOSIGNAL, etc.
* @param flags can have `MSG_OOB`, `MSG_DONTROUTE`, and `MSG_DONTWAIT`
* @param opt_addr is a binary ip:port destination override, which is
* mandatory for UDP if connect() wasn't called
* @param addrsize is the byte-length of addr's true polymorphic form

View file

@ -16,50 +16,21 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/assert.h"
#include "libc/calls/internal.h"
#include "libc/calls/sig.internal.h"
#include "libc/calls/struct/fd.internal.h"
#include "libc/calls/struct/sigset.internal.h"
#include "libc/calls/struct/sigset.h"
#include "libc/errno.h"
#include "libc/intrin/atomic.h"
#include "libc/intrin/kprintf.h"
#include "libc/intrin/strace.internal.h"
#include "libc/nt/enum/wait.h"
#include "libc/nt/errors.h"
#include "libc/nt/runtime.h"
#include "libc/nt/struct/iovec.h"
#include "libc/nt/struct/overlapped.h"
#include "libc/nt/thread.h"
#include "libc/nt/winsock.h"
#include "libc/runtime/runtime.h"
#include "libc/sock/internal.h"
#include "libc/sock/syscall_fd.internal.h"
#include "libc/sysv/consts/o.h"
#include "libc/sysv/consts/sicode.h"
#include "libc/sysv/errfuns.h"
#include "libc/thread/posixthread.internal.h"
#ifdef __x86_64__
struct WinsockBlockResources {
int64_t handle;
struct NtOverlapped *overlap;
};
static void UnwindWinsockBlock(void *arg) {
struct WinsockBlockResources *wbr = arg;
uint32_t got, flags;
CancelIoEx(wbr->handle, wbr->overlap);
WSAGetOverlappedResult(wbr->handle, wbr->overlap, &got, true, &flags);
WSACloseEvent(wbr->overlap->hEvent);
}
static void CancelWinsockBlock(int64_t handle, struct NtOverlapped *overlap) {
if (!CancelIoEx(handle, overlap)) {
unassert(WSAGetLastError() == kNtErrorNotFound);
}
}
textwindows ssize_t
__winsock_block(int64_t handle, uint32_t flags, bool nonblock,
uint32_t srwtimeout, sigset_t waitmask,
@ -67,31 +38,20 @@ __winsock_block(int64_t handle, uint32_t flags, bool nonblock,
uint32_t *flags, void *arg),
void *arg) {
int rc;
int sig = 0;
uint32_t status;
uint32_t exchanged;
int olderror = errno;
bool eagained = false;
bool canceled = false;
int handler_was_called;
struct PosixThread *pt;
RestartOperation:
int rc, sig, reason = 0;
uint32_t status, exchanged;
if (_check_cancel() == -1) return -1; // ECANCELED
if ((sig = __sig_get(waitmask))) goto HandleInterrupt;
struct NtOverlapped overlap = {.hEvent = WSACreateEvent()};
struct WinsockBlockResources wbr = {handle, &overlap};
pthread_cleanup_push(UnwindWinsockBlock, &wbr);
rc = StartSocketOp(handle, &overlap, &flags, arg);
if (rc && WSAGetLastError() == kNtErrorIoPending) {
if (nonblock) {
CancelWinsockBlock(handle, &overlap);
eagained = true;
} else if (_check_cancel()) {
CancelWinsockBlock(handle, &overlap);
canceled = true;
} else if ((sig = __sig_get(waitmask))) {
CancelWinsockBlock(handle, &overlap);
CancelIoEx(handle, &overlap);
reason = EAGAIN;
} else {
struct PosixThread *pt;
pt = _pthread_self();
pt->pt_blkmask = waitmask;
pt->pt_iohandle = handle;
@ -101,10 +61,13 @@ RestartOperation:
status = WSAWaitForMultipleEvents(1, &overlap.hEvent, 0,
srwtimeout ? srwtimeout : -1u, 0);
atomic_store_explicit(&pt->pt_blocker, 0, memory_order_release);
if (status == kNtWaitTimeout) {
// SO_RCVTIMEO or SO_SNDTIMEO elapsed
CancelWinsockBlock(handle, &overlap);
eagained = true;
if (status) {
if (status == kNtWaitTimeout) {
reason = EAGAIN; // SO_RCVTIMEO or SO_SNDTIMEO elapsed
} else {
reason = WSAGetLastError(); // ENETDOWN or ENOBUFS
}
CancelIoEx(handle, &overlap);
}
}
rc = 0;
@ -114,30 +77,21 @@ RestartOperation:
? 0
: -1;
}
pthread_cleanup_pop(false);
WSACloseEvent(overlap.hEvent);
if (canceled) {
return ecanceled();
}
if (sig) {
handler_was_called = __sig_relay(sig, SI_KERNEL, waitmask);
if (_check_cancel() == -1) return -1;
} else {
handler_was_called = 0;
}
if (!rc) {
errno = olderror;
return exchanged;
}
if (WSAGetLastError() == kNtErrorOperationAborted) {
if (eagained) return eagain();
if (!handler_was_called && (sig = __sig_get(waitmask))) {
handler_was_called = __sig_relay(sig, SI_KERNEL, waitmask);
if (_check_cancel() == -1) return -1;
if (reason) {
errno = reason;
return -1;
}
if (handler_was_called != 1) {
goto RestartOperation;
if ((sig = __sig_get(waitmask))) {
HandleInterrupt:
int handler_was_called = __sig_relay(sig, SI_KERNEL, waitmask);
if (_check_cancel() == -1) return -1;
if (handler_was_called != 1) goto RestartOperation;
}
return eintr();
}