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

@ -32,7 +32,7 @@
#include "libc/sysv/errfuns.h"
/**
* Does what poll() does except with bitset API.
* Checks status on multiple file descriptors at once.
*
* This function is the same as saying:
*
@ -41,15 +41,23 @@
* select(nfds, readfds, writefds, exceptfds, timeout);
* sigprocmask(SIG_SETMASK, old, 0);
*
* Except it happens atomically.
*
* The Linux Kernel modifies the timeout parameter. This wrapper gives
* it a local variable due to POSIX requiring that `timeout` be const.
* If you need that information from the Linux Kernel use sys_pselect.
*
* This system call is supported on all platforms. It's like select()
* except that it atomically changes the sigprocmask() during the op.
* Except it happens atomically. Unlike ppoll() Cosmo guarantees this is
* atomic on all supported platforms.
*
* @param nfds is the number of the highest file descriptor set in these
* bitsets by the caller, plus one; this value can't be greater than
* `FD_SETSIZE` which Cosmopolitan currently defines as 1024 because
* `fd_set` has a static size
* @param readfds may be used to be notified when you can call read() on
* a file descriptor without it blocking; this includes when data is
* is available to be read as well as eof and error conditions
* @param writefds may be used to be notified when write() may be called
* on a file descriptor without it blocking
* @param exceptfds may be used to be notified of exceptional conditions
* such as out-of-band data on a socket; it is equivalent to POLLPRI
* in the revents of poll()
* @param timeout if null will block indefinitely
* @param sigmask may be null in which case no mask change happens
* @raise ECANCELED if thread was cancelled in masked mode
* @raise EINTR if signal was delivered
* @cancelationpoint
@ -74,7 +82,7 @@ int pselect(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
fd_set *old_exceptfds_ptr = 0;
BEGIN_CANCELATION_POINT;
if (nfds < 0) {
if (nfds < 0 || nfds > FD_SETSIZE) {
rc = einval();
} else {
if (readfds) {