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
|
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
2021-01-28 23:49:15 +00:00
|
|
|
#include "libc/bits/bits.h"
|
2022-03-16 20:33:13 +00:00
|
|
|
#include "libc/bits/weaken.h"
|
2022-04-15 06:39:48 +00:00
|
|
|
#include "libc/calls/calls.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/calls/internal.h"
|
2022-03-25 14:11:44 +00:00
|
|
|
#include "libc/calls/sig.internal.h"
|
2022-04-15 06:39:48 +00:00
|
|
|
#include "libc/calls/sigbits.h"
|
2022-05-23 22:06:11 +00:00
|
|
|
#include "libc/calls/state.internal.h"
|
2022-04-15 06:39:48 +00:00
|
|
|
#include "libc/calls/strace.internal.h"
|
2022-03-16 20:33:13 +00:00
|
|
|
#include "libc/calls/struct/sigaction.h"
|
2022-04-16 17:40:23 +00:00
|
|
|
#include "libc/errno.h"
|
2022-04-15 06:39:48 +00:00
|
|
|
#include "libc/intrin/kprintf.h"
|
|
|
|
#include "libc/intrin/spinlock.h"
|
2021-03-01 07:42:35 +00:00
|
|
|
#include "libc/macros.internal.h"
|
2022-04-15 06:39:48 +00:00
|
|
|
#include "libc/mem/mem.h"
|
2022-04-16 17:40:23 +00:00
|
|
|
#include "libc/nt/enum/filetype.h"
|
2022-04-15 06:39:48 +00:00
|
|
|
#include "libc/nt/errors.h"
|
2022-04-16 17:40:23 +00:00
|
|
|
#include "libc/nt/files.h"
|
2022-04-15 06:39:48 +00:00
|
|
|
#include "libc/nt/ipc.h"
|
|
|
|
#include "libc/nt/runtime.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/nt/struct/pollfd.h"
|
2022-04-15 06:39:48 +00:00
|
|
|
#include "libc/nt/synchronization.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/nt/winsock.h"
|
|
|
|
#include "libc/sock/internal.h"
|
2022-04-16 17:40:23 +00:00
|
|
|
#include "libc/sysv/consts/o.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/sysv/consts/poll.h"
|
2022-04-15 06:39:48 +00:00
|
|
|
#include "libc/sysv/consts/sig.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/sysv/errfuns.h"
|
|
|
|
|
2022-05-17 11:14:28 +00:00
|
|
|
_Alignas(64) static int poll_lock;
|
2022-04-15 06:39:48 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Polls on the New Technology.
|
|
|
|
*
|
|
|
|
* This function is used to implement poll() and select(). You may poll
|
|
|
|
* on both sockets and files at the same time. We also poll for signals
|
|
|
|
* while poll is polling.
|
|
|
|
*/
|
|
|
|
textwindows int sys_poll_nt(struct pollfd *fds, uint64_t nfds, uint64_t *ms) {
|
|
|
|
bool ok;
|
|
|
|
uint32_t avail;
|
|
|
|
struct sys_pollfd_nt pipefds[8];
|
|
|
|
struct sys_pollfd_nt sockfds[64];
|
|
|
|
int pipeindices[ARRAYLEN(pipefds)];
|
|
|
|
int sockindices[ARRAYLEN(sockfds)];
|
2022-04-16 17:40:23 +00:00
|
|
|
int i, sn, pn, failed, gotinvals, gotpipes, gotsocks, waitfor;
|
2022-04-15 06:39:48 +00:00
|
|
|
|
|
|
|
// check for interrupts early before doing work
|
|
|
|
if (_check_interrupts(false, g_fds.p)) return eintr();
|
|
|
|
|
|
|
|
// do the planning
|
|
|
|
// we need to read static variables
|
|
|
|
// we might need to spawn threads and open pipes
|
|
|
|
_spinlock(&poll_lock);
|
2022-04-16 17:40:23 +00:00
|
|
|
_spinlock(&__fds_lock);
|
|
|
|
for (gotinvals = failed = sn = pn = i = 0; i < nfds; ++i) {
|
2022-04-15 06:39:48 +00:00
|
|
|
if (fds[i].fd < 0) continue;
|
|
|
|
if (__isfdopen(fds[i].fd)) {
|
|
|
|
if (__isfdkind(fds[i].fd, kFdSocket)) {
|
|
|
|
if (sn < ARRAYLEN(sockfds)) {
|
2022-04-16 17:40:23 +00:00
|
|
|
// the magnums for POLLIN/OUT/PRI on NT include the other ones too
|
|
|
|
// we need to clear ones like POLLNVAL or else WSAPoll shall whine
|
2022-04-15 06:39:48 +00:00
|
|
|
sockindices[sn] = i;
|
|
|
|
sockfds[sn].handle = g_fds.p[fds[i].fd].handle;
|
|
|
|
sockfds[sn].events = fds[i].events & (POLLPRI | POLLIN | POLLOUT);
|
2022-04-16 17:40:23 +00:00
|
|
|
sockfds[sn].revents = 0;
|
|
|
|
++sn;
|
2022-04-15 06:39:48 +00:00
|
|
|
} else {
|
|
|
|
// too many socket fds
|
|
|
|
failed = enomem();
|
|
|
|
break;
|
|
|
|
}
|
2022-04-16 17:40:23 +00:00
|
|
|
} else if (pn < ARRAYLEN(pipefds)) {
|
|
|
|
pipeindices[pn] = i;
|
|
|
|
pipefds[pn].handle = g_fds.p[fds[i].fd].handle;
|
|
|
|
pipefds[pn].events = 0;
|
|
|
|
pipefds[pn].revents = 0;
|
|
|
|
switch (g_fds.p[fds[i].fd].flags & O_ACCMODE) {
|
|
|
|
case O_RDONLY:
|
|
|
|
pipefds[pn].events = fds[i].events & POLLIN;
|
2022-04-15 06:39:48 +00:00
|
|
|
break;
|
2022-04-16 17:40:23 +00:00
|
|
|
case O_WRONLY:
|
|
|
|
pipefds[pn].events = fds[i].events & POLLOUT;
|
|
|
|
break;
|
|
|
|
case O_RDWR:
|
|
|
|
pipefds[pn].events = fds[i].events & (POLLIN | POLLOUT);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
unreachable;
|
2022-04-15 06:39:48 +00:00
|
|
|
}
|
2022-04-16 17:40:23 +00:00
|
|
|
++pn;
|
2022-04-15 06:39:48 +00:00
|
|
|
} else {
|
2022-04-16 17:40:23 +00:00
|
|
|
// too many non-socket fds
|
|
|
|
failed = enomem();
|
2022-04-15 06:39:48 +00:00
|
|
|
break;
|
|
|
|
}
|
2021-08-26 04:35:58 +00:00
|
|
|
} else {
|
2022-04-16 17:40:23 +00:00
|
|
|
++gotinvals;
|
2021-08-26 04:35:58 +00:00
|
|
|
}
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
2022-04-16 17:40:23 +00:00
|
|
|
_spunlock(&__fds_lock);
|
2022-04-15 06:39:48 +00:00
|
|
|
_spunlock(&poll_lock);
|
|
|
|
if (failed) {
|
|
|
|
// failed to create a polling solution
|
|
|
|
return failed;
|
|
|
|
}
|
|
|
|
|
|
|
|
// perform the i/o and sleeping and looping
|
2021-01-25 21:08:05 +00:00
|
|
|
for (;;) {
|
2022-04-15 06:39:48 +00:00
|
|
|
// see if input is available on non-sockets
|
|
|
|
for (gotpipes = i = 0; i < pn; ++i) {
|
2022-04-16 17:40:23 +00:00
|
|
|
if (pipefds[i].events & POLLOUT) {
|
|
|
|
// we have no way of polling if a non-socket is writeable yet
|
|
|
|
// therefore we assume that if it can happen, it shall happen
|
|
|
|
pipefds[i].revents = POLLOUT;
|
|
|
|
}
|
|
|
|
if (pipefds[i].events & POLLIN) {
|
|
|
|
if (GetFileType(pipefds[i].handle) == kNtFileTypePipe) {
|
|
|
|
ok = PeekNamedPipe(pipefds[i].handle, 0, 0, 0, &avail, 0);
|
|
|
|
POLLTRACE("PeekNamedPipe(%ld, 0, 0, 0, [%'u], 0) → %hhhd% m",
|
|
|
|
pipefds[i].handle, avail, ok);
|
|
|
|
if (ok) {
|
|
|
|
if (avail) {
|
|
|
|
pipefds[i].revents = POLLIN;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
pipefds[i].revents = POLLERR;
|
|
|
|
}
|
2022-04-15 06:39:48 +00:00
|
|
|
} else {
|
2022-04-16 17:40:23 +00:00
|
|
|
// we have no way of polling if a non-socket is readable yet
|
|
|
|
// therefore we assume that if it can happen it shall happen
|
|
|
|
pipefds[i].revents = POLLIN;
|
2022-04-15 06:39:48 +00:00
|
|
|
}
|
2022-04-16 17:40:23 +00:00
|
|
|
}
|
|
|
|
if (pipefds[i].revents) {
|
|
|
|
++gotpipes;
|
2022-04-15 06:39:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// if we haven't found any good results yet then here we
|
|
|
|
// compute a small time slice we don't mind sleeping for
|
2022-04-16 17:40:23 +00:00
|
|
|
waitfor = gotinvals || gotpipes ? 0 : MIN(__SIG_POLLING_INTERVAL_MS, *ms);
|
2022-04-15 06:39:48 +00:00
|
|
|
if (sn) {
|
|
|
|
// we need to poll the socket handles separately because
|
|
|
|
// microsoft certainly loves to challenge us with coding
|
|
|
|
// please note that winsock will fail if we pass zero fd
|
2022-04-22 02:13:19 +00:00
|
|
|
#if _NTTRACE
|
2022-04-16 17:40:23 +00:00
|
|
|
POLLTRACE("WSAPoll(%p, %u, %'d) out of %'lu", sockfds, sn, waitfor, *ms);
|
2022-04-22 02:13:19 +00:00
|
|
|
#endif
|
2022-04-15 06:39:48 +00:00
|
|
|
if ((gotsocks = WSAPoll(sockfds, sn, waitfor)) == -1) {
|
|
|
|
return __winsockerr();
|
2021-01-25 21:08:05 +00:00
|
|
|
}
|
2022-04-15 06:39:48 +00:00
|
|
|
*ms -= waitfor;
|
2021-01-25 21:08:05 +00:00
|
|
|
} else {
|
2022-04-15 06:39:48 +00:00
|
|
|
gotsocks = 0;
|
2022-04-16 17:40:23 +00:00
|
|
|
if (!gotinvals && !gotpipes && waitfor) {
|
2022-04-15 06:39:48 +00:00
|
|
|
// if we've only got pipes and none of them are ready
|
|
|
|
// then we'll just explicitly sleep for the time left
|
2022-04-16 17:40:23 +00:00
|
|
|
POLLTRACE("SleepEx(%'d, false) out of %'lu", waitfor, *ms);
|
|
|
|
if (SleepEx(__SIG_POLLING_INTERVAL_MS, true) == kNtWaitIoCompletion) {
|
|
|
|
POLLTRACE("IOCP EINTR");
|
|
|
|
} else {
|
|
|
|
*ms -= waitfor;
|
2022-04-15 06:39:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// we gave all the sockets and all the named pipes a shot
|
|
|
|
// if we found anything at all then it's time to end work
|
2022-04-16 17:40:23 +00:00
|
|
|
if (gotinvals || gotpipes || gotsocks || *ms <= 0) {
|
2022-04-15 06:39:48 +00:00
|
|
|
break;
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
2022-04-15 06:39:48 +00:00
|
|
|
// otherwise loop limitlessly for timeout to elapse while
|
|
|
|
// checking for signal delivery interrupts, along the way
|
|
|
|
if (_check_interrupts(false, g_fds.p)) {
|
|
|
|
return eintr();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-16 17:40:23 +00:00
|
|
|
// the system call is going to succeed
|
|
|
|
// it's now ok to start setting the output memory
|
|
|
|
for (i = 0; i < nfds; ++i) {
|
|
|
|
if (fds[i].fd < 0 || __isfdopen(fds[i].fd)) {
|
|
|
|
fds[i].revents = 0;
|
|
|
|
} else {
|
|
|
|
fds[i].revents = POLLNVAL;
|
|
|
|
}
|
|
|
|
}
|
2022-04-15 06:39:48 +00:00
|
|
|
for (i = 0; i < pn; ++i) {
|
2022-04-16 17:40:23 +00:00
|
|
|
fds[pipeindices[i]].revents = pipefds[i].revents;
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
2022-04-15 06:39:48 +00:00
|
|
|
for (i = 0; i < sn; ++i) {
|
2022-04-16 17:40:23 +00:00
|
|
|
fds[sockindices[i]].revents = sockfds[i].revents;
|
2022-04-15 06:39:48 +00:00
|
|
|
}
|
|
|
|
|
2022-04-16 17:40:23 +00:00
|
|
|
// and finally return
|
|
|
|
return gotinvals + gotpipes + gotsocks;
|
2022-04-15 06:39:48 +00:00
|
|
|
}
|