mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-06-27 06:48:31 +00:00
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:
parent
39e7f24947
commit
2ec413b5a9
27 changed files with 664 additions and 2132 deletions
|
@ -16,46 +16,50 @@
|
|||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/calls/cp.internal.h"
|
||||
#include "libc/dce.h"
|
||||
#include "libc/intrin/strace.h"
|
||||
#include "libc/calls/struct/timespec.h"
|
||||
#include "libc/sock/struct/pollfd.h"
|
||||
#include "libc/sock/struct/pollfd.internal.h"
|
||||
#include "libc/stdckdint.h"
|
||||
#include "libc/sysv/errfuns.h"
|
||||
|
||||
/**
|
||||
* Waits for something to happen on multiple file descriptors at once.
|
||||
* Checks status on multiple file descriptors at once.
|
||||
*
|
||||
* Warning: XNU has an inconsistency with other platforms. If you have
|
||||
* pollfds with fd≥0 and none of the meaningful events flags are added
|
||||
* e.g. POLLIN then XNU won't check for POLLNVAL. This matters because
|
||||
* one of the use-cases for poll() is quickly checking for open files.
|
||||
* Servers that need to handle an unbounded number of client connections
|
||||
* should just create a separate thread for each client. poll() isn't a
|
||||
* scalable i/o solution on any platform.
|
||||
*
|
||||
* Note: Polling works best on Windows for sockets. We're able to poll
|
||||
* input on named pipes. But for anything that isn't a socket, or pipe
|
||||
* with POLLIN, (e.g. regular file) then POLLIN/POLLOUT are always set
|
||||
* into revents if they're requested, provided they were opened with a
|
||||
* mode that permits reading and/or writing.
|
||||
* On Windows it's only possible to poll 64 file descriptors at a time.
|
||||
* This is a limitation imposed by WSAPoll(). Cosmopolitan Libc's poll()
|
||||
* polyfill can go higher in some cases. For example, you can actually
|
||||
* poll 64 sockets and 64 pipes/terminals at the same time. Furthermore,
|
||||
* elements whose fd field is set to a negative number are ignored and
|
||||
* will not count against this limit.
|
||||
*
|
||||
* Note: Windows has a limit of 64 file descriptors and ENOMEM with -1
|
||||
* is returned if that limit is exceeded. In practice the limit is not
|
||||
* this low. For example, pollfds with fd<0 don't count. So the caller
|
||||
* could flip the sign bit with a short timeout, to poll a larger set.
|
||||
* One of the use cases for poll() is to quickly check if a number of
|
||||
* file descriptors are valid. The canonical way to do this is to set
|
||||
* events to 0 which prevents blocking and causes only the invalid,
|
||||
* hangup, and error statuses to be checked.
|
||||
*
|
||||
* On XNU, the POLLHUP and POLLERR statuses aren't checked unless either
|
||||
* POLLIN, POLLOUT, or POLLPRI are specified in the events field. Cosmo
|
||||
* will however polyfill the checking of POLLNVAL on XNU with the events
|
||||
* doesn't specify any of the above i/o events.
|
||||
*
|
||||
* When XNU and BSD OSes report POLLHUP, they will always set POLLIN too
|
||||
* when POLLIN is requested, even in cases when there isn't unread data.
|
||||
*
|
||||
* @param fds[𝑖].fd should be a socket, input pipe, or conosle input
|
||||
* and if it's a negative number then the entry is ignored
|
||||
* and if it's a negative number then the entry is ignored, plus
|
||||
* revents will be set to zero
|
||||
* @param fds[𝑖].events flags can have POLLIN, POLLOUT, POLLPRI,
|
||||
* POLLRDNORM, POLLWRNORM, POLLRDBAND, POLLWRBAND as well as
|
||||
* POLLERR, POLLHUP, and POLLNVAL although the latter are
|
||||
* always implied (assuming fd≥0) so they're ignored here
|
||||
* @param timeout_ms if 0 means don't wait and -1 means wait forever
|
||||
* @return number of items fds whose revents field has been set to
|
||||
* nonzero to describe its events, or 0 if the timeout elapsed,
|
||||
* or -1 w/ errno
|
||||
* @param timeout_ms if 0 means don't wait and negative waits forever
|
||||
* @return number of `fds` whose revents field has been set to a nonzero
|
||||
* number, 0 if the timeout elapsed without events, or -1 w/ errno
|
||||
* @return fds[𝑖].revents is always zero initializaed and then will
|
||||
* be populated with POLL{IN,OUT,PRI,HUP,ERR,NVAL} if something
|
||||
* was determined about the file descriptor
|
||||
* @raise E2BIG if we exceeded the 64 socket limit on Windows
|
||||
* @raise ECANCELED if thread was cancelled in masked mode
|
||||
* @raise EINTR if signal was delivered
|
||||
* @cancelationpoint
|
||||
|
@ -63,22 +67,14 @@
|
|||
* @norestart
|
||||
*/
|
||||
int poll(struct pollfd *fds, size_t nfds, int timeout_ms) {
|
||||
int rc;
|
||||
BEGIN_CANCELATION_POINT;
|
||||
|
||||
if (!IsWindows()) {
|
||||
if (!IsMetal()) {
|
||||
rc = sys_poll(fds, nfds, timeout_ms);
|
||||
} else {
|
||||
rc = sys_poll_metal(fds, nfds, timeout_ms);
|
||||
}
|
||||
struct timespec ts;
|
||||
struct timespec *tsp;
|
||||
if (timeout_ms >= 0) {
|
||||
ts.tv_sec = timeout_ms / 1000;
|
||||
ts.tv_nsec = timeout_ms % 1000 * 1000000;
|
||||
tsp = &ts;
|
||||
} else {
|
||||
uint32_t ms = timeout_ms >= 0 ? timeout_ms : -1u;
|
||||
rc = sys_poll_nt(fds, nfds, &ms, 0);
|
||||
tsp = 0;
|
||||
}
|
||||
|
||||
END_CANCELATION_POINT;
|
||||
STRACE("poll(%s, %'zu, %'d) → %d% lm", DescribePollFds(rc, fds, nfds), nfds,
|
||||
timeout_ms, rc);
|
||||
return rc;
|
||||
return ppoll(fds, nfds, tsp, 0);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue