2020-06-15 14:18:57 +00:00
|
|
|
|
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
|
|
|
|
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
|
|
|
|
|
╞══════════════════════════════════════════════════════════════════════════════╡
|
|
|
|
|
│ Copyright 2020 Justine Alexandra Roberts Tunney │
|
|
|
|
|
│ │
|
2020-12-28 01:18:44 +00:00
|
|
|
|
│ Permission to use, copy, modify, and/or distribute this software for │
|
|
|
|
|
│ any purpose with or without fee is hereby granted, provided that the │
|
|
|
|
|
│ above copyright notice and this permission notice appear in all copies. │
|
2020-06-15 14:18:57 +00:00
|
|
|
|
│ │
|
2020-12-28 01:18:44 +00:00
|
|
|
|
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
|
|
|
|
|
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
|
|
|
|
|
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
|
|
|
|
|
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
|
|
|
|
|
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
|
|
|
|
|
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
|
|
|
|
|
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
|
|
|
|
│ PERFORMANCE OF THIS SOFTWARE. │
|
2020-06-15 14:18:57 +00:00
|
|
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
2022-11-06 02:49:41 +00:00
|
|
|
|
#include "libc/calls/cp.internal.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
|
#include "libc/dce.h"
|
2021-05-16 04:53:26 +00:00
|
|
|
|
#include "libc/intrin/asan.internal.h"
|
2022-09-19 22:01:48 +00:00
|
|
|
|
#include "libc/intrin/strace.internal.h"
|
2022-08-13 20:11:56 +00:00
|
|
|
|
#include "libc/sock/struct/pollfd.h"
|
|
|
|
|
#include "libc/sock/struct/pollfd.internal.h"
|
2023-06-10 16:15:19 +00:00
|
|
|
|
#include "libc/stdckdint.h"
|
2021-05-16 04:53:26 +00:00
|
|
|
|
#include "libc/sysv/errfuns.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Waits for something to happen on multiple file descriptors at once.
|
|
|
|
|
*
|
2022-04-16 17:40:23 +00:00
|
|
|
|
* 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.
|
|
|
|
|
*
|
|
|
|
|
* 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.
|
|
|
|
|
*
|
|
|
|
|
* 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.
|
|
|
|
|
*
|
2022-04-15 06:39:48 +00:00
|
|
|
|
* @param fds[𝑖].fd should be a socket, input pipe, or conosle input
|
2022-04-16 17:40:23 +00:00
|
|
|
|
* and if it's a negative number then the entry is ignored
|
|
|
|
|
* @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
|
2020-06-15 14:18:57 +00:00
|
|
|
|
* @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
|
2022-04-16 17:40:23 +00:00
|
|
|
|
* nonzero to describe its events, or 0 if the timeout elapsed,
|
|
|
|
|
* 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
|
2022-11-06 02:49:41 +00:00
|
|
|
|
* @raise ECANCELED if thread was cancelled in masked mode
|
|
|
|
|
* @raise EINTR if signal was delivered
|
Make improvements
- We now serialize the file descriptor table when spawning / executing
processes on Windows. This means you can now inherit more stuff than
just standard i/o. It's needed by bash, which duplicates the console
to file descriptor #255. We also now do a better job serializing the
environment variables, so you're less likely to encounter E2BIG when
using your bash shell. We also no longer coerce environ to uppercase
- execve() on Windows now remotely controls its parent process to make
them spawn a replacement for itself. Then it'll be able to terminate
immediately once the spawn succeeds, without having to linger around
for the lifetime as a shell process for proxying the exit code. When
process worker thread running in the parent sees the child die, it's
given a handle to the new child, to replace it in the process table.
- execve() and posix_spawn() on Windows will now provide CreateProcess
an explicit handle list. This allows us to remove handle locks which
enables better fork/spawn concurrency, with seriously correct thread
safety. Other codebases like Go use the same technique. On the other
hand fork() still favors the conventional WIN32 inheritence approach
which can be a little bit messy, but is *controlled* by guaranteeing
perfectly clean slates at both the spawning and execution boundaries
- sigset_t is now 64 bits. Having it be 128 bits was a mistake because
there's no reason to use that and it's only supported by FreeBSD. By
using the system word size, signal mask manipulation on Windows goes
very fast. Furthermore @asyncsignalsafe funcs have been rewritten on
Windows to take advantage of signal masking, now that it's much more
pleasant to use.
- All the overlapped i/o code on Windows has been rewritten for pretty
good signal and cancelation safety. We're now able to ensure overlap
data structures are cleaned up so long as you don't longjmp() out of
out of a signal handler that interrupted an i/o operation. Latencies
are also improved thanks to the removal of lots of "busy wait" code.
Waits should be optimal for everything except poll(), which shall be
the last and final demon we slay in the win32 i/o horror show.
- getrusage() on Windows is now able to report RUSAGE_CHILDREN as well
as RUSAGE_SELF, thanks to aggregation in the process manager thread.
2023-10-08 12:36:18 +00:00
|
|
|
|
* @cancelationpoint
|
2020-06-15 14:18:57 +00:00
|
|
|
|
* @asyncsignalsafe
|
2022-03-25 14:11:44 +00:00
|
|
|
|
* @norestart
|
2020-06-15 14:18:57 +00:00
|
|
|
|
*/
|
2022-04-15 06:39:48 +00:00
|
|
|
|
int poll(struct pollfd *fds, size_t nfds, int timeout_ms) {
|
2022-11-02 05:36:03 +00:00
|
|
|
|
int rc;
|
2022-09-19 22:01:48 +00:00
|
|
|
|
size_t n;
|
Make improvements
- We now serialize the file descriptor table when spawning / executing
processes on Windows. This means you can now inherit more stuff than
just standard i/o. It's needed by bash, which duplicates the console
to file descriptor #255. We also now do a better job serializing the
environment variables, so you're less likely to encounter E2BIG when
using your bash shell. We also no longer coerce environ to uppercase
- execve() on Windows now remotely controls its parent process to make
them spawn a replacement for itself. Then it'll be able to terminate
immediately once the spawn succeeds, without having to linger around
for the lifetime as a shell process for proxying the exit code. When
process worker thread running in the parent sees the child die, it's
given a handle to the new child, to replace it in the process table.
- execve() and posix_spawn() on Windows will now provide CreateProcess
an explicit handle list. This allows us to remove handle locks which
enables better fork/spawn concurrency, with seriously correct thread
safety. Other codebases like Go use the same technique. On the other
hand fork() still favors the conventional WIN32 inheritence approach
which can be a little bit messy, but is *controlled* by guaranteeing
perfectly clean slates at both the spawning and execution boundaries
- sigset_t is now 64 bits. Having it be 128 bits was a mistake because
there's no reason to use that and it's only supported by FreeBSD. By
using the system word size, signal mask manipulation on Windows goes
very fast. Furthermore @asyncsignalsafe funcs have been rewritten on
Windows to take advantage of signal masking, now that it's much more
pleasant to use.
- All the overlapped i/o code on Windows has been rewritten for pretty
good signal and cancelation safety. We're now able to ensure overlap
data structures are cleaned up so long as you don't longjmp() out of
out of a signal handler that interrupted an i/o operation. Latencies
are also improved thanks to the removal of lots of "busy wait" code.
Waits should be optimal for everything except poll(), which shall be
the last and final demon we slay in the win32 i/o horror show.
- getrusage() on Windows is now able to report RUSAGE_CHILDREN as well
as RUSAGE_SELF, thanks to aggregation in the process manager thread.
2023-10-08 12:36:18 +00:00
|
|
|
|
BEGIN_CANCELATION_POINT;
|
2022-04-17 03:29:08 +00:00
|
|
|
|
|
2023-06-10 16:15:19 +00:00
|
|
|
|
if (IsAsan() &&
|
|
|
|
|
(ckd_mul(&n, nfds, sizeof(struct pollfd)) || !__asan_is_valid(fds, n))) {
|
Introduce --strace flag for system call tracing
This is similar to the --ftrace (c function call trace) flag, except
it's less noisy since it only logs system calls to stderr. Having this
flag is valuable because (1) system call tracing tells us a lot about
the behavior of complex programs and (2) it's usually very hard to get
system call tracing on various operating systems, e.g. strace, ktrace,
dtruss, truss, nttrace, etc. Especially on Apple platforms where even
with the special boot trick, debuggers still aren't guaranteed to work.
make -j8 o//examples
o//examples/hello.com --strace
This is enabled by default in MODE=, MODE=opt, and MODE=dbg. In MODE=dbg
extra information will be printed.
make -j8 MODE=dbg o/dbg/examples
o/dbg/examples/hello.com --strace |& less
This change also changes:
- Rename IsText() → _istext()
- Rename IsUtf8() → _isutf8()
- Fix madvise() on Windows NT
- Fix empty string case of inet_ntop()
- vfork() wrapper now saves and restores errno
- Update xsigaction() to yoink syscall support
2022-03-19 01:07:28 +00:00
|
|
|
|
rc = efault();
|
|
|
|
|
} else if (!IsWindows()) {
|
2021-09-28 05:58:51 +00:00
|
|
|
|
if (!IsMetal()) {
|
Introduce --strace flag for system call tracing
This is similar to the --ftrace (c function call trace) flag, except
it's less noisy since it only logs system calls to stderr. Having this
flag is valuable because (1) system call tracing tells us a lot about
the behavior of complex programs and (2) it's usually very hard to get
system call tracing on various operating systems, e.g. strace, ktrace,
dtruss, truss, nttrace, etc. Especially on Apple platforms where even
with the special boot trick, debuggers still aren't guaranteed to work.
make -j8 o//examples
o//examples/hello.com --strace
This is enabled by default in MODE=, MODE=opt, and MODE=dbg. In MODE=dbg
extra information will be printed.
make -j8 MODE=dbg o/dbg/examples
o/dbg/examples/hello.com --strace |& less
This change also changes:
- Rename IsText() → _istext()
- Rename IsUtf8() → _isutf8()
- Fix madvise() on Windows NT
- Fix empty string case of inet_ntop()
- vfork() wrapper now saves and restores errno
- Update xsigaction() to yoink syscall support
2022-03-19 01:07:28 +00:00
|
|
|
|
rc = sys_poll(fds, nfds, timeout_ms);
|
2021-09-28 05:58:51 +00:00
|
|
|
|
} else {
|
Introduce --strace flag for system call tracing
This is similar to the --ftrace (c function call trace) flag, except
it's less noisy since it only logs system calls to stderr. Having this
flag is valuable because (1) system call tracing tells us a lot about
the behavior of complex programs and (2) it's usually very hard to get
system call tracing on various operating systems, e.g. strace, ktrace,
dtruss, truss, nttrace, etc. Especially on Apple platforms where even
with the special boot trick, debuggers still aren't guaranteed to work.
make -j8 o//examples
o//examples/hello.com --strace
This is enabled by default in MODE=, MODE=opt, and MODE=dbg. In MODE=dbg
extra information will be printed.
make -j8 MODE=dbg o/dbg/examples
o/dbg/examples/hello.com --strace |& less
This change also changes:
- Rename IsText() → _istext()
- Rename IsUtf8() → _isutf8()
- Fix madvise() on Windows NT
- Fix empty string case of inet_ntop()
- vfork() wrapper now saves and restores errno
- Update xsigaction() to yoink syscall support
2022-03-19 01:07:28 +00:00
|
|
|
|
rc = sys_poll_metal(fds, nfds, timeout_ms);
|
2021-09-28 05:58:51 +00:00
|
|
|
|
}
|
2020-06-15 14:18:57 +00:00
|
|
|
|
} else {
|
2023-09-21 14:30:39 +00:00
|
|
|
|
uint32_t ms = timeout_ms >= 0 ? timeout_ms : -1u;
|
|
|
|
|
rc = sys_poll_nt(fds, nfds, &ms, 0);
|
2020-06-15 14:18:57 +00:00
|
|
|
|
}
|
2022-04-17 03:29:08 +00:00
|
|
|
|
|
Make improvements
- We now serialize the file descriptor table when spawning / executing
processes on Windows. This means you can now inherit more stuff than
just standard i/o. It's needed by bash, which duplicates the console
to file descriptor #255. We also now do a better job serializing the
environment variables, so you're less likely to encounter E2BIG when
using your bash shell. We also no longer coerce environ to uppercase
- execve() on Windows now remotely controls its parent process to make
them spawn a replacement for itself. Then it'll be able to terminate
immediately once the spawn succeeds, without having to linger around
for the lifetime as a shell process for proxying the exit code. When
process worker thread running in the parent sees the child die, it's
given a handle to the new child, to replace it in the process table.
- execve() and posix_spawn() on Windows will now provide CreateProcess
an explicit handle list. This allows us to remove handle locks which
enables better fork/spawn concurrency, with seriously correct thread
safety. Other codebases like Go use the same technique. On the other
hand fork() still favors the conventional WIN32 inheritence approach
which can be a little bit messy, but is *controlled* by guaranteeing
perfectly clean slates at both the spawning and execution boundaries
- sigset_t is now 64 bits. Having it be 128 bits was a mistake because
there's no reason to use that and it's only supported by FreeBSD. By
using the system word size, signal mask manipulation on Windows goes
very fast. Furthermore @asyncsignalsafe funcs have been rewritten on
Windows to take advantage of signal masking, now that it's much more
pleasant to use.
- All the overlapped i/o code on Windows has been rewritten for pretty
good signal and cancelation safety. We're now able to ensure overlap
data structures are cleaned up so long as you don't longjmp() out of
out of a signal handler that interrupted an i/o operation. Latencies
are also improved thanks to the removal of lots of "busy wait" code.
Waits should be optimal for everything except poll(), which shall be
the last and final demon we slay in the win32 i/o horror show.
- getrusage() on Windows is now able to report RUSAGE_CHILDREN as well
as RUSAGE_SELF, thanks to aggregation in the process manager thread.
2023-10-08 12:36:18 +00:00
|
|
|
|
END_CANCELATION_POINT;
|
2022-11-02 16:42:52 +00:00
|
|
|
|
STRACE("poll(%s, %'zu, %'d) → %d% lm", DescribePollFds(rc, fds, nfds), nfds,
|
2022-11-02 05:36:03 +00:00
|
|
|
|
timeout_ms, rc);
|
Introduce --strace flag for system call tracing
This is similar to the --ftrace (c function call trace) flag, except
it's less noisy since it only logs system calls to stderr. Having this
flag is valuable because (1) system call tracing tells us a lot about
the behavior of complex programs and (2) it's usually very hard to get
system call tracing on various operating systems, e.g. strace, ktrace,
dtruss, truss, nttrace, etc. Especially on Apple platforms where even
with the special boot trick, debuggers still aren't guaranteed to work.
make -j8 o//examples
o//examples/hello.com --strace
This is enabled by default in MODE=, MODE=opt, and MODE=dbg. In MODE=dbg
extra information will be printed.
make -j8 MODE=dbg o/dbg/examples
o/dbg/examples/hello.com --strace |& less
This change also changes:
- Rename IsText() → _istext()
- Rename IsUtf8() → _isutf8()
- Fix madvise() on Windows NT
- Fix empty string case of inet_ntop()
- vfork() wrapper now saves and restores errno
- Update xsigaction() to yoink syscall support
2022-03-19 01:07:28 +00:00
|
|
|
|
return rc;
|
2020-06-15 14:18:57 +00:00
|
|
|
|
}
|