Make improvements

- Improved async signal safety of read() particularly for longjmp()
- Started adding cancel cleanup handlers for locks / etc on Windows
- Make /dev/tty work better particularly for uses like `foo | less`
- Eagerly read console input into a linked list, so poll can signal
- Fix some libc definitional bugs, which configure scripts detected
This commit is contained in:
Justine Tunney 2023-09-21 07:30:39 -07:00
parent d6c2830850
commit 0c5dd7b342
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
85 changed files with 1062 additions and 671 deletions

View file

@ -16,13 +16,16 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/assert.h"
#include "libc/calls/calls.h"
#include "libc/calls/console.internal.h"
#include "libc/calls/internal.h"
#include "libc/calls/sig.internal.h"
#include "libc/calls/state.internal.h"
#include "libc/calls/struct/sigaction.h"
#include "libc/calls/struct/timespec.h"
#include "libc/dce.h"
#include "libc/errno.h"
#include "libc/intrin/atomic.h"
#include "libc/intrin/bits.h"
#include "libc/intrin/strace.internal.h"
#include "libc/macros.internal.h"
@ -46,38 +49,37 @@
#include "libc/sysv/consts/sig.h"
#include "libc/sysv/errfuns.h"
#include "libc/thread/posixthread.internal.h"
#include "libc/thread/tls.h"
#ifdef __x86_64__
/*
* Polls on the New Technology.
*
* This function is used to implement poll() and select(). You may poll
* on both sockets and files at the same time. We also poll for signals
* while poll is polling.
*/
textwindows int sys_poll_nt(struct pollfd *fds, uint64_t nfds, uint64_t *ms,
// Polls on the New Technology.
//
// This function is used to implement poll() and select(). You may poll
// on both sockets and files at the same time. We also poll for signals
// while poll is polling.
textwindows int sys_poll_nt(struct pollfd *fds, uint64_t nfds, uint32_t *ms,
const sigset_t *sigmask) {
bool ok;
uint64_t m;
bool interrupted;
sigset_t oldmask;
uint32_t avail, cm;
uint32_t cm, avail, waitfor;
struct sys_pollfd_nt pipefds[8];
struct sys_pollfd_nt sockfds[64];
int pipeindices[ARRAYLEN(pipefds)];
int sockindices[ARRAYLEN(sockfds)];
int i, rc, sn, pn, gotinvals, gotpipes, gotsocks, waitfor;
int i, rc, sn, pn, gotinvals, gotpipes, gotsocks;
// check for interrupts early before doing work
if (sigmask) {
__sig_mask(SIG_SETMASK, sigmask, &oldmask);
}
if ((rc = _check_interrupts(0))) {
goto ReturnPath;
}
#if IsModeDbg()
struct timespec noearlier =
timespec_add(timespec_real(), timespec_frommillis(ms ? *ms : -1u));
#endif
// do the planning
// we need to read static variables
// we might need to spawn threads and open pipes
m = atomic_exchange(&__get_tls()->tib_sigmask, -1);
__fds_lock();
for (gotinvals = rc = sn = pn = i = 0; i < nfds; ++i) {
if (fds[i].fd < 0) continue;
@ -125,9 +127,10 @@ textwindows int sys_poll_nt(struct pollfd *fds, uint64_t nfds, uint64_t *ms,
}
}
__fds_unlock();
atomic_store_explicit(&__get_tls()->tib_sigmask, m, memory_order_release);
if (rc) {
// failed to create a polling solution
goto ReturnPath;
goto Finished;
}
// perform the i/o and sleeping and looping
@ -152,17 +155,8 @@ textwindows int sys_poll_nt(struct pollfd *fds, uint64_t nfds, uint64_t *ms,
pipefds[i].revents |= POLLERR;
}
} else if (GetConsoleMode(pipefds[i].handle, &cm)) {
int e = errno;
avail = CountConsoleInputBytes(pipefds[i].handle);
if (avail > 0) {
if (CountConsoleInputBytes(pipefds[i].handle)) {
pipefds[i].revents |= POLLIN;
} else if (avail == -1u) {
if (errno == ENODATA) {
pipefds[i].revents |= POLLIN;
} else {
pipefds[i].revents |= POLLERR;
}
errno = e;
}
} else {
// we have no way of polling if a non-socket is readable yet
@ -176,42 +170,35 @@ textwindows int sys_poll_nt(struct pollfd *fds, uint64_t nfds, uint64_t *ms,
}
// if we haven't found any good results yet then here we
// compute a small time slice we don't mind sleeping for
waitfor = gotinvals || gotpipes ? 0 : MIN(__SIG_POLL_INTERVAL_MS, *ms);
if (sn) {
#if _NTTRACE
POLLTRACE("WSAPoll(%p, %u, %'d) out of %'lu", sockfds, sn, waitfor, *ms);
#endif
if ((gotsocks = WSAPoll(sockfds, sn, 0)) == -1) {
rc = __winsockerr();
goto ReturnPath;
return __winsockerr();
}
} else {
gotsocks = 0;
}
waitfor = MIN(__SIG_POLL_INTERVAL_MS, *ms);
if (!gotinvals && !gotsocks && !gotpipes && waitfor) {
POLLTRACE("sleeping for %'d out of %'lu ms", waitfor, *ms);
POLLTRACE("poll() sleeping for %'d out of %'u ms", waitfor, *ms);
struct PosixThread *pt = _pthread_self();
pt->abort_errno = 0;
pt->pt_flags |= PT_INSEMAPHORE;
WaitForSingleObject(pt->semaphore, waitfor);
pt->pt_flags &= ~PT_INSEMAPHORE;
if (pt->abort_errno) {
errno = pt->abort_errno;
rc = -1;
goto ReturnPath;
if (sigmask) __sig_mask(SIG_SETMASK, sigmask, &oldmask);
interrupted = _check_interrupts(0) || __pause_thread(waitfor);
if (sigmask) __sig_mask(SIG_SETMASK, &oldmask, 0);
if (interrupted) return -1;
if (*ms != -1u) {
if (waitfor < *ms) {
*ms -= waitfor;
} else {
*ms = 0;
}
}
*ms -= waitfor; // todo: make more resilient
}
// 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 || *ms <= 0) {
if (gotinvals || gotpipes || gotsocks || !*ms) {
break;
}
// otherwise loop limitlessly for timeout to elapse while
// checking for signal delivery interrupts, along the way
if ((rc = _check_interrupts(0))) {
goto ReturnPath;
}
}
// the system call is going to succeed
@ -233,10 +220,16 @@ textwindows int sys_poll_nt(struct pollfd *fds, uint64_t nfds, uint64_t *ms,
// and finally return
rc = gotinvals + gotpipes + gotsocks;
ReturnPath:
if (sigmask) {
__sig_mask(SIG_SETMASK, &oldmask, 0);
Finished:
#if IsModeDbg()
struct timespec ended = timespec_real();
if (!rc && timespec_cmp(ended, noearlier) < 0) {
STRACE("poll() ended %'ld ns too soon!",
timespec_tonanos(timespec_sub(noearlier, ended)));
}
#endif
return rc;
}