mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-06-28 07:18:30 +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,6 +16,7 @@
|
|||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/calls/cp.internal.h"
|
||||
#include "libc/calls/struct/sigset.h"
|
||||
#include "libc/calls/struct/sigset.internal.h"
|
||||
|
@ -24,14 +25,18 @@
|
|||
#include "libc/dce.h"
|
||||
#include "libc/errno.h"
|
||||
#include "libc/intrin/strace.h"
|
||||
#include "libc/runtime/stack.h"
|
||||
#include "libc/sock/struct/pollfd.h"
|
||||
#include "libc/sock/struct/pollfd.internal.h"
|
||||
#include "libc/stdckdint.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "libc/sysv/consts/f.h"
|
||||
#include "libc/sysv/consts/poll.h"
|
||||
#include "libc/sysv/consts/sig.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.
|
||||
*
|
||||
* This function is the same as saying:
|
||||
*
|
||||
|
@ -41,16 +46,51 @@
|
|||
* sigprocmask(SIG_SETMASK, old, 0);
|
||||
*
|
||||
* Except it happens atomically when the kernel supports doing that. On
|
||||
* kernel such as XNU and NetBSD which don't, this wrapper will fall
|
||||
* back to using the example above. Consider using pselect() which is
|
||||
* atomic on all supported platforms.
|
||||
* kernels such as XNU and NetBSD which don't, this wrapper will fall
|
||||
* back to using the example above. If you need ironclad assurances of
|
||||
* signal mask atomicity, then consider using pselect() which Cosmo Libc
|
||||
* guarantees to be atomic on all supported platforms.
|
||||
*
|
||||
* 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_ppoll().
|
||||
* Servers that need to handle an unbounded number of client connections
|
||||
* should just create a separate thread for each client. poll(), ppoll()
|
||||
* and select() aren't scalable i/o solutions on any platform.
|
||||
*
|
||||
* On Windows it's only possible to poll 64 file descriptors at a time;
|
||||
* it's a limitation imposed by WSAPoll(). Cosmopolitan Libc's ppoll()
|
||||
* polyfill can go higher in some cases; for example, It's possible to
|
||||
* 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.
|
||||
*
|
||||
* 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, 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 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
|
||||
* @param timeout if null will block indefinitely
|
||||
* @param sigmask may be null in which case no mask change happens
|
||||
* @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
|
||||
|
@ -59,11 +99,32 @@
|
|||
*/
|
||||
int ppoll(struct pollfd *fds, size_t nfds, const struct timespec *timeout,
|
||||
const sigset_t *sigmask) {
|
||||
int e, rc;
|
||||
int e, fdcount;
|
||||
sigset_t oldmask;
|
||||
struct timespec ts, *tsp;
|
||||
BEGIN_CANCELATION_POINT;
|
||||
|
||||
// The OpenBSD poll() man pages claims it'll ignore POLLERR, POLLHUP,
|
||||
// and POLLNVAL in pollfd::events except it doesn't actually do this.
|
||||
size_t bytes = 0;
|
||||
struct pollfd *fds2 = 0;
|
||||
if (IsOpenbsd()) {
|
||||
if (ckd_mul(&bytes, nfds, sizeof(struct pollfd)))
|
||||
return einval();
|
||||
#pragma GCC push_options
|
||||
#pragma GCC diagnostic ignored "-Walloca-larger-than="
|
||||
#pragma GCC diagnostic ignored "-Wanalyzer-out-of-bounds"
|
||||
fds2 = alloca(bytes);
|
||||
#pragma GCC pop_options
|
||||
CheckLargeStackAllocation(fds2, bytes);
|
||||
memcpy(fds2, fds, bytes);
|
||||
for (size_t i = 0; i < nfds; ++i)
|
||||
fds2[i].events &= ~(POLLERR | POLLHUP | POLLNVAL);
|
||||
struct pollfd *swap = fds;
|
||||
fds = fds2;
|
||||
fds2 = swap;
|
||||
}
|
||||
|
||||
if (!IsWindows()) {
|
||||
e = errno;
|
||||
if (timeout) {
|
||||
|
@ -72,8 +133,8 @@ int ppoll(struct pollfd *fds, size_t nfds, const struct timespec *timeout,
|
|||
} else {
|
||||
tsp = 0;
|
||||
}
|
||||
rc = sys_ppoll(fds, nfds, tsp, sigmask, 8);
|
||||
if (rc == -1 && errno == ENOSYS) {
|
||||
fdcount = sys_ppoll(fds, nfds, tsp, sigmask, 8);
|
||||
if (fdcount == -1 && errno == ENOSYS) {
|
||||
int ms;
|
||||
errno = e;
|
||||
if (!timeout || ckd_add(&ms, timeout->tv_sec,
|
||||
|
@ -82,7 +143,7 @@ int ppoll(struct pollfd *fds, size_t nfds, const struct timespec *timeout,
|
|||
}
|
||||
if (sigmask)
|
||||
sys_sigprocmask(SIG_SETMASK, sigmask, &oldmask);
|
||||
rc = poll(fds, nfds, ms);
|
||||
fdcount = sys_poll(fds, nfds, ms);
|
||||
if (sigmask)
|
||||
sys_sigprocmask(SIG_SETMASK, &oldmask, 0);
|
||||
}
|
||||
|
@ -92,11 +153,38 @@ int ppoll(struct pollfd *fds, size_t nfds, const struct timespec *timeout,
|
|||
ckd_add(&ms, timeout->tv_sec, (timeout->tv_nsec + 999999) / 1000000)) {
|
||||
ms = -1u;
|
||||
}
|
||||
rc = sys_poll_nt(fds, nfds, &ms, sigmask);
|
||||
fdcount = sys_poll_nt(fds, nfds, &ms, sigmask);
|
||||
}
|
||||
|
||||
if (IsOpenbsd() && fdcount != -1) {
|
||||
struct pollfd *swap = fds;
|
||||
fds = fds2;
|
||||
fds2 = swap;
|
||||
memcpy(fds, fds2, bytes);
|
||||
}
|
||||
|
||||
// One of the use cases for poll() is checking if a large number of
|
||||
// file descriptors exist. However on XNU if none of the meaningful
|
||||
// event flags are specified (e.g. POLLIN, POLLOUT) then it doesn't
|
||||
// perform the POLLNVAL check that's implied on all other platforms
|
||||
if (IsXnu() && fdcount != -1) {
|
||||
for (size_t i = 0; i < nfds; ++i) {
|
||||
if (fds[i].fd >= 0 && //
|
||||
!fds[i].revents && //
|
||||
!(fds[i].events & (POLLIN | POLLOUT | POLLPRI))) {
|
||||
int err = errno;
|
||||
if (fcntl(fds[i].fd, F_GETFL) == -1) {
|
||||
errno = err;
|
||||
fds[i].revents = POLLNVAL;
|
||||
++fdcount;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
END_CANCELATION_POINT;
|
||||
STRACE("ppoll(%s, %'zu, %s, %s) → %d% lm", DescribePollFds(rc, fds, nfds),
|
||||
nfds, DescribeTimespec(0, timeout), DescribeSigset(0, sigmask), rc);
|
||||
return rc;
|
||||
STRACE("ppoll(%s, %'zu, %s, %s) → %d% lm",
|
||||
DescribePollFds(fdcount, fds, nfds), nfds,
|
||||
DescribeTimespec(0, timeout), DescribeSigset(0, sigmask), fdcount);
|
||||
return fdcount;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue