cosmopolitan/libc/sock/select-nt.c
Justine Tunney 072e1d2910 Make signal handling work well across platforms
- Fix sigsuspend() on XNU
- Fix strsignal() on non-Linux
- Add unit tests for strsignal()
- Add unit tests for setitimer()
- Add unit tests for sigsuspend()
- Rewrite setitimer() for New Technology
- Rewrite nanosleep() for New Technology
- Polyfill SIGALRM on the New Technology
- select(0,0,0,0) on NT now calls pause()
- Remove some NTDLL calls that aren't needed
- Polyfill SA_NOCLDWAIT on the New Technology
- Polyfill SA_RESETHAND on the New Technology
- Polyfill sigprocmask() on the New Technology
- Polyfill SIGCHLD+SIG_IGN on the New Technology
- Polyfill SA_RESTART masking on the New Technology
- Deliver console signals from main thread on New Technology
- Document SA_RESTART behavior w/ @sarestartable / @norestart
- System call trace in MODE=dbg now prints inherited FDs and signal mask
2022-03-25 07:28:57 -07:00

132 lines
4.9 KiB
C

/*-*- 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 │
│ │
│ 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/bits/popcnt.h"
#include "libc/calls/calls.h"
#include "libc/calls/internal.h"
#include "libc/macros.internal.h"
#include "libc/mem/mem.h"
#include "libc/nt/winsock.h"
#include "libc/sock/internal.h"
#include "libc/sock/yoink.inc"
#include "libc/sysv/errfuns.h"
static int GetFdsPopcnt(int nfds, fd_set *fds) {
int i, n = 0;
if (fds) {
for (i = 0; i < nfds; ++i) {
n += popcnt(fds->fds_bits[i]);
}
}
return n;
}
static int FindFdByHandle(int nfds, int64_t h) {
int i, n;
n = MIN(nfds << 3, g_fds.n);
for (i = 0; i < n; ++i) {
if (h == g_fds.p[i].handle && g_fds.p[i].kind != kFdEmpty) {
return i;
}
}
return -1;
}
static struct NtFdSet *FdSetToNtFdSet(int nfds, fd_set *fds) {
int i, j, k, n, m, fd;
struct NtFdSet *ntfds;
if (fds && (n = GetFdsPopcnt(nfds, fds))) {
m = MIN(n, ARRAYLEN(ntfds->fd_array));
ntfds = malloc(sizeof(struct NtFdSet));
for (k = i = 0; i < nfds; ++i) {
if (fds->fds_bits[i]) {
for (j = 0; j < 64 && k < m; ++j) {
if ((fds->fds_bits[i] & (1ul << j)) && i * 8 + j < g_fds.n) {
ntfds->fd_array[k++] = g_fds.p[i * 8 + j].handle;
}
}
}
}
ntfds->fd_count = m;
return ntfds;
} else {
return NULL;
}
}
static void NtFdSetToFdSet(int nfds, fd_set *fds, struct NtFdSet *ntfds) {
int i, fd;
if (ntfds) {
for (i = 0; i < nfds; ++i) {
fds->fds_bits[i] = 0;
}
for (i = 0; i < ntfds->fd_count; ++i) {
if ((fd = FindFdByHandle(nfds, ntfds->fd_array[i])) != -1) {
fds->fds_bits[fd >> 3] |= 1ul << (fd & 7);
}
}
}
}
static struct NtTimeval *TimevalToNtTimeval(struct timeval *tv,
struct NtTimeval *nttv) {
if (tv) {
nttv->tv_sec = tv->tv_sec;
nttv->tv_usec = tv->tv_usec;
return nttv;
} else {
return NULL;
}
}
int sys_select_nt(int nfds, fd_set *readfds, fd_set *writefds,
fd_set *exceptfds, struct timeval *timeout) {
int n, rc;
struct timespec req, rem;
struct NtTimeval nttimeout, *nttimeoutp;
struct NtFdSet *ntreadfds, *ntwritefds, *ntexceptfds;
if (readfds || writefds || exceptfds) {
nfds = MIN(ARRAYLEN(readfds->fds_bits), ROUNDUP(nfds, 8)) >> 3;
ntreadfds = FdSetToNtFdSet(nfds, readfds);
ntwritefds = FdSetToNtFdSet(nfds, writefds);
ntexceptfds = FdSetToNtFdSet(nfds, exceptfds);
nttimeoutp = TimevalToNtTimeval(timeout, &nttimeout);
if ((rc = __sys_select_nt(0, ntreadfds, ntwritefds, ntexceptfds,
nttimeoutp)) != -1) {
NtFdSetToFdSet(nfds, readfds, ntreadfds);
NtFdSetToFdSet(nfds, writefds, ntwritefds);
NtFdSetToFdSet(nfds, exceptfds, ntexceptfds);
} else {
__winsockerr();
}
free(ntreadfds);
free(ntwritefds);
free(ntexceptfds);
} else if (timeout) {
req.tv_sec = timeout->tv_sec;
req.tv_nsec = timeout->tv_usec * 1000;
rem.tv_sec = 0;
rem.tv_nsec = 0;
rc = sys_nanosleep_nt(&req, &rem);
timeout->tv_sec = rem.tv_sec;
timeout->tv_usec = rem.tv_nsec / 1000;
} else {
rc = pause();
}
return rc;
}