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

@ -22,6 +22,7 @@
#include "libc/calls/struct/sigaction.h"
#include "libc/dce.h"
#include "libc/errno.h"
#include "libc/intrin/describeflags.h"
#include "libc/log/libfatal.internal.h"
#include "libc/mem/gc.h"
#include "libc/nexgen32e/rdtsc.h"
@ -55,12 +56,6 @@ void OnSig(int sig) {
gotsig = true;
}
__wur char *FormatPollFd(struct pollfd p[2]) {
return xasprintf("fd:%d revents:%s\n"
"fd:%d revents:%s\n",
p[0].fd, "<TODO:kPollNames>", p[1].fd, "<TODO:kPollNames>");
}
TEST(poll, allZero_doesNothingPrettyMuch) {
EXPECT_SYS(0, 0, poll(0, 0, 0));
}
@ -94,14 +89,52 @@ TEST(poll, testNegativeOneFd_isIgnored) {
struct sockaddr_in addr = {AF_INET, 0, {htonl(INADDR_LOOPBACK)}};
ASSERT_SYS(0, 0, bind(3, (struct sockaddr *)&addr, sizeof(addr)));
ASSERT_SYS(0, 0, listen(3, 10));
struct pollfd fds[] = {{-1}, {3}};
struct pollfd fds[] = {{-1, 0, -1}, {3, 0, -1}};
EXPECT_SYS(0, 0, poll(fds, ARRAYLEN(fds), 1));
EXPECT_STREQ("fd:-1 revents:<TODO:kPollNames>\n"
"fd:3 revents:<TODO:kPollNames>\n",
gc(FormatPollFd(&fds[0])));
EXPECT_EQ(-1, fds[0].fd);
EXPECT_EQ(0, fds[0].revents);
EXPECT_EQ(3, fds[1].fd);
EXPECT_EQ(0, fds[1].revents);
ASSERT_SYS(0, 0, close(3));
}
TEST(poll, testInvalidFd_POLLIN_isChecked) {
struct pollfd fds[] = {{77, POLLIN, -1}};
EXPECT_SYS(0, 1, poll(fds, ARRAYLEN(fds), 1));
EXPECT_EQ(77, fds[0].fd);
EXPECT_EQ(POLLNVAL, fds[0].revents);
}
TEST(poll, testInvalidFd_POLLOUT_isChecked) {
struct pollfd fds[] = {{77, POLLOUT, -1}};
EXPECT_SYS(0, 1, poll(fds, ARRAYLEN(fds), 1));
EXPECT_EQ(77, fds[0].fd);
EXPECT_EQ(POLLNVAL, fds[0].revents);
}
TEST(poll, testInvalidFd_POLLPRI_isChecked) {
struct pollfd fds[] = {{77, POLLPRI, -1}};
EXPECT_SYS(0, 1, poll(fds, ARRAYLEN(fds), 1));
EXPECT_EQ(77, fds[0].fd);
EXPECT_EQ(POLLNVAL, fds[0].revents);
}
TEST(poll, testInvalidFd_POLLHUP_isChecked) {
// this behavior has to be polyfilled on xnu
struct pollfd fds[] = {{77, POLLHUP, -1}};
EXPECT_SYS(0, 1, poll(fds, ARRAYLEN(fds), 1));
EXPECT_EQ(77, fds[0].fd);
EXPECT_EQ(POLLNVAL, fds[0].revents);
}
TEST(poll, testInvalidFd_ZERO_isChecked) {
// this behavior has to be polyfilled on xnu
struct pollfd fds[] = {{77, 0, -1}};
EXPECT_SYS(0, 1, poll(fds, ARRAYLEN(fds), 1));
EXPECT_EQ(77, fds[0].fd);
EXPECT_EQ(POLLNVAL, fds[0].revents);
}
TEST(poll, pipe_noInput) {
// we can't test stdin here since
// we can't assume it isn't /dev/null
@ -115,6 +148,17 @@ TEST(poll, pipe_noInput) {
EXPECT_SYS(0, 0, close(pipefds[1]));
}
TEST(poll, pipe_broken) {
int pipefds[2];
EXPECT_SYS(0, 0, pipe(pipefds));
EXPECT_SYS(0, 0, close(pipefds[1]));
struct pollfd fds[] = {{pipefds[0], POLLIN}};
EXPECT_SYS(0, 1, poll(fds, 1, 0));
// BSDs also set POLLIN here too even though that's wrong
EXPECT_TRUE(!!(fds[0].revents & POLLHUP));
EXPECT_SYS(0, 0, close(pipefds[0]));
}
TEST(poll, pipe_hasInputFromSameProcess) {
char buf[2];
int pipefds[2];
@ -122,7 +166,7 @@ TEST(poll, pipe_hasInputFromSameProcess) {
struct pollfd fds[] = {{pipefds[0], POLLIN}};
EXPECT_SYS(0, 2, write(pipefds[1], "hi", 2));
EXPECT_SYS(0, 1, poll(fds, 1, 1000)); // flake nt!
EXPECT_EQ(POLLIN, fds[0].revents);
EXPECT_TRUE(!!(fds[0].revents & POLLIN));
EXPECT_SYS(0, 2, read(pipefds[0], buf, 2));
EXPECT_SYS(0, 0, poll(fds, 1, 0));
EXPECT_SYS(0, 0, close(pipefds[0]));
@ -150,7 +194,7 @@ TEST(poll, pipe_hasInput) {
EXPECT_SYS(0, 2, read(pipefds[0], buf, 2));
struct pollfd fds[] = {{pipefds[0], POLLIN}};
EXPECT_SYS(0, 1, poll(fds, 1, -1));
EXPECT_EQ(POLLIN, fds[0].revents & POLLIN);
EXPECT_TRUE(!!(fds[0].revents & POLLIN));
EXPECT_SYS(0, 2, read(pipefds[0], buf, 2));
EXPECT_SYS(0, 0, close(pipefds[0]));
ASSERT_NE(-1, wait(&ws));

View file

@ -40,6 +40,7 @@ TEST_LIBC_STDIO_DIRECTDEPS = \
LIBC_THREAD \
LIBC_LOG \
LIBC_X \
THIRD_PARTY_COMPILER_RT \
THIRD_PARTY_GDTOA \
THIRD_PARTY_MBEDTLS \
THIRD_PARTY_MUSL \