Fix bugs in poll(), select(), ppoll(), and pselect()

poll() and select() now delegate to ppoll() and pselect() for assurances
that both polyfill implementations are correct and well-tested. Poll now
polyfills XNU and BSD quirks re: the hanndling of POLLNVAL and the other
similar status flags. This change resolves a misunderstanding concerning
how select(exceptfds) is intended to map to POLPRI. We now use E2BIG for
bouncing requests that exceed the 64 handle limit on Windows. With pipes
and consoles on Windows our poll impl will now report POLLHUP correctly.

Issues with Windows path generation have been fixed. For example, it was
problematic on Windows to say: posix_spawn_file_actions_addchdir_np("/")
due to the need to un-UNC paths in some additional places. Calling fstat
on UNC style volume path handles will now work. posix_spawn now supports
simulating the opening of /dev/null and other special paths on Windows.

Cosmopolitan no longer defines epoll(). I think wepoll is a nice project
for using epoll() on Windows socket handles. However we need generalized
file descriptor support to make epoll() for Windows work well enough for
inclusion in a C library. It's also not worth having epoll() if we can't
get it to work on XNU and BSD OSes which provide different abstractions.
Even epoll() on Linux isn't that great of an abstraction since it's full
of footguns. Last time I tried to get it to be useful I had little luck.
Considering how long it took to get poll() and select() to be consistent
across platforms, we really have no business claiming to have epoll too.
While it'd be nice to have fully implemented, the only software that use
epoll() are event i/o libraries used by things like nodejs. Event i/o is
not the best paradigm for handling i/o; threads make so much more sense.
This commit is contained in:
Justine Tunney 2024-09-01 19:29:47 -07:00
parent 39e7f24947
commit 2ec413b5a9
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
27 changed files with 664 additions and 2132 deletions

View file

@ -56,6 +56,19 @@
#define POLL_INTERVAL_MS 10
// <sync libc/sysv/consts.sh>
#define POLLERR_ 0x0001 // implied in events
#define POLLHUP_ 0x0002 // implied in events
#define POLLNVAL_ 0x0004 // implied in events
#define POLLIN_ 0x0300
#define POLLRDNORM_ 0x0100
#define POLLRDBAND_ 0x0200
#define POLLOUT_ 0x0010
#define POLLWRNORM_ 0x0010
#define POLLWRBAND_ 0x0020 // MSDN undocumented
#define POLLPRI_ 0x0400 // MSDN unsupported
// </sync libc/sysv/consts.sh>
// Polls on the New Technology.
//
// This function is used to implement poll() and select(). You may poll
@ -86,16 +99,16 @@ static textwindows int sys_poll_nt_impl(struct pollfd *fds, uint64_t nfds,
if (__isfdopen(fds[i].fd)) {
if (__isfdkind(fds[i].fd, kFdSocket)) {
if (sn < ARRAYLEN(sockfds)) {
// 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
// WSAPoll whines if we pass POLLNVAL, POLLHUP, or POLLERR.
sockindices[sn] = i;
sockfds[sn].handle = g_fds.p[fds[i].fd].handle;
sockfds[sn].events = fds[i].events & (POLLPRI | POLLIN | POLLOUT);
sockfds[sn].events =
fds[i].events & (POLLRDNORM_ | POLLRDBAND_ | POLLWRNORM_);
sockfds[sn].revents = 0;
++sn;
} else {
// too many socket fds
rc = enomem();
rc = e2big();
break;
}
} else if (pn < ARRAYLEN(pipefds)) {
@ -105,13 +118,13 @@ static textwindows int sys_poll_nt_impl(struct pollfd *fds, uint64_t nfds,
pipefds[pn].revents = 0;
switch (g_fds.p[fds[i].fd].flags & O_ACCMODE) {
case O_RDONLY:
pipefds[pn].events = fds[i].events & POLLIN;
pipefds[pn].events = fds[i].events & POLLIN_;
break;
case O_WRONLY:
pipefds[pn].events = fds[i].events & POLLOUT;
pipefds[pn].events = fds[i].events & POLLOUT_;
break;
case O_RDWR:
pipefds[pn].events = fds[i].events & (POLLIN | POLLOUT);
pipefds[pn].events = fds[i].events & (POLLIN_ | POLLOUT_);
break;
default:
break;
@ -119,7 +132,7 @@ static textwindows int sys_poll_nt_impl(struct pollfd *fds, uint64_t nfds,
++pn;
} else {
// too many non-socket fds
rc = enomem();
rc = e2big();
break;
}
} else {
@ -127,52 +140,60 @@ static textwindows int sys_poll_nt_impl(struct pollfd *fds, uint64_t nfds,
}
}
__fds_unlock();
if (rc) {
if (rc)
// failed to create a polling solution
return rc;
}
// perform the i/o and sleeping and looping
for (;;) {
// see if input is available on non-sockets
for (gotpipes = i = 0; i < pn; ++i) {
if (pipefds[i].events & POLLOUT) {
if (pipefds[i].events & POLLWRNORM_)
// 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;
}
} else if (GetConsoleMode(pipefds[i].handle, &cm)) {
if (CountConsoleInputBytes()) {
pipefds[i].revents |= POLLIN; // both >0 and -1 (eof) are pollin
}
pipefds[i].revents |= POLLWRNORM_;
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, %d}",
pipefds[i].handle, avail, ok, GetLastError());
if (ok) {
if (avail)
pipefds[i].revents |= POLLRDNORM_;
} else if (GetLastError() == kNtErrorHandleEof ||
GetLastError() == kNtErrorBrokenPipe) {
pipefds[i].revents &= ~POLLWRNORM_;
pipefds[i].revents |= POLLHUP_;
} else {
// 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;
pipefds[i].revents &= ~POLLWRNORM_;
pipefds[i].revents |= POLLERR_;
}
} else if (GetConsoleMode(pipefds[i].handle, &cm)) {
switch (CountConsoleInputBytes()) {
case 0:
break;
case -1:
pipefds[i].revents &= ~POLLWRNORM_;
pipefds[i].revents |= POLLHUP_;
break;
default:
pipefds[i].revents |= POLLRDNORM_;
break;
}
} else {
// 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 |= POLLRDNORM_;
}
if (pipefds[i].revents) {
if (!(pipefds[i].events & POLLRDNORM_))
pipefds[i].revents &= ~POLLRDNORM_;
if (pipefds[i].revents)
++gotpipes;
}
}
// if we haven't found any good results yet then here we
// compute a small time slice we don't mind sleeping for
if (sn) {
if ((gotsocks = WSAPoll(sockfds, sn, 0)) == -1) {
if ((gotsocks = WSAPoll(sockfds, sn, 0)) == -1)
return __winsockerr();
}
} else {
gotsocks = 0;
}
@ -190,18 +211,16 @@ static textwindows int sys_poll_nt_impl(struct pollfd *fds, uint64_t nfds,
if (waitfor) {
POLLTRACE("poll() sleeping for %'d out of %'lu ms", waitfor,
timespec_tomillis(remain));
if ((rc = _park_norestart(waitfor, sigmask)) == -1) {
if ((rc = _park_norestart(waitfor, sigmask)) == -1)
return -1; // eintr, ecanceled, etc.
}
}
}
}
// 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
if (gotinvals || gotpipes || gotsocks || !waitfor) {
if (gotinvals || gotpipes || gotsocks || !waitfor)
break;
}
}
// the system call is going to succeed
@ -210,15 +229,13 @@ static textwindows int sys_poll_nt_impl(struct pollfd *fds, uint64_t nfds,
if (fds[i].fd < 0 || __isfdopen(fds[i].fd)) {
fds[i].revents = 0;
} else {
fds[i].revents = POLLNVAL;
fds[i].revents = POLLNVAL_;
}
}
for (i = 0; i < pn; ++i) {
for (i = 0; i < pn; ++i)
fds[pipeindices[i]].revents = pipefds[i].revents;
}
for (i = 0; i < sn; ++i) {
for (i = 0; i < sn; ++i)
fds[sockindices[i]].revents = sockfds[i].revents;
}
// and finally return
return gotinvals + gotpipes + gotsocks;