mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-07-20 09:30:31 +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.
This commit is contained in:
parent
af7cb3c82f
commit
791f79fcb3
382 changed files with 4008 additions and 4511 deletions
|
@ -16,207 +16,61 @@
|
|||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/calls/internal.h"
|
||||
#include "libc/calls/sig.internal.h"
|
||||
#include "libc/calls/struct/fd.internal.h"
|
||||
#include "libc/calls/struct/iovec.internal.h"
|
||||
#include "libc/calls/struct/iovec.h"
|
||||
#include "libc/calls/struct/sigset.h"
|
||||
#include "libc/calls/struct/sigset.internal.h"
|
||||
#include "libc/calls/syscall-nt.internal.h"
|
||||
#include "libc/calls/syscall_support-nt.internal.h"
|
||||
#include "libc/errno.h"
|
||||
#include "libc/intrin/atomic.h"
|
||||
#include "libc/intrin/nomultics.internal.h"
|
||||
#include "libc/intrin/strace.internal.h"
|
||||
#include "libc/intrin/weaken.h"
|
||||
#include "libc/macros.internal.h"
|
||||
#include "libc/nt/console.h"
|
||||
#include "libc/nt/enum/consolemodeflags.h"
|
||||
#include "libc/nt/enum/filetype.h"
|
||||
#include "libc/nt/enum/wait.h"
|
||||
#include "libc/nt/errors.h"
|
||||
#include "libc/nt/events.h"
|
||||
#include "libc/nt/files.h"
|
||||
#include "libc/nt/runtime.h"
|
||||
#include "libc/nt/struct/overlapped.h"
|
||||
#include "libc/nt/synchronization.h"
|
||||
#include "libc/nt/thread.h"
|
||||
#include "libc/nt/thunk/msabi.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/sysv/consts/o.h"
|
||||
#include "libc/sysv/consts/sicode.h"
|
||||
#include "libc/sysv/consts/sig.h"
|
||||
#include "libc/sysv/errfuns.h"
|
||||
#include "libc/thread/thread.h"
|
||||
#include "libc/thread/tls.h"
|
||||
#ifdef __x86_64__
|
||||
|
||||
static bool IsMouseModeCommand(int x) {
|
||||
return x == 1000 || // SET_VT200_MOUSE
|
||||
x == 1002 || // SET_BTN_EVENT_MOUSE
|
||||
x == 1006 || // SET_SGR_EXT_MODE_MOUSE
|
||||
x == 1015; // SET_URXVT_EXT_MODE_MOUSE
|
||||
static inline void RaiseSignal(int sig) {
|
||||
if (_weaken(__sig_raise)) {
|
||||
_weaken(__sig_raise)(sig, SI_KERNEL);
|
||||
} else {
|
||||
TerminateThisProcess(sig);
|
||||
}
|
||||
}
|
||||
|
||||
static textwindows ssize_t sys_write_nt_impl(int fd, void *data, size_t size,
|
||||
ssize_t offset) {
|
||||
ssize_t offset,
|
||||
uint64_t waitmask) {
|
||||
uint64_t m;
|
||||
struct Fd *f = g_fds.p + fd;
|
||||
bool isconsole = f->kind == kFdConsole;
|
||||
|
||||
// perform the write i/o operation
|
||||
bool32 ok;
|
||||
struct Fd *f;
|
||||
uint32_t sent;
|
||||
int64_t handle;
|
||||
struct PosixThread *pt;
|
||||
|
||||
f = g_fds.p + fd;
|
||||
pt = _pthread_self();
|
||||
size = MIN(size, 0x7ffff000);
|
||||
if (f->kind == kFdConsole) {
|
||||
handle = f->extra; // get write end of console
|
||||
} else {
|
||||
handle = f->handle;
|
||||
// determine win32 handle for writing
|
||||
int64_t handle = f->handle;
|
||||
if (isconsole && _weaken(GetConsoleOutputHandle)) {
|
||||
handle = _weaken(GetConsoleOutputHandle)();
|
||||
}
|
||||
|
||||
bool pwriting = offset != -1;
|
||||
bool seekable = f->kind == kFdFile && GetFileType(handle) == kNtFileTypeDisk;
|
||||
bool nonblock = !!(f->flags & O_NONBLOCK);
|
||||
pt->abort_errno = EAGAIN;
|
||||
|
||||
if (pwriting && !seekable) {
|
||||
return espipe();
|
||||
}
|
||||
if (!pwriting) {
|
||||
offset = 0;
|
||||
// intercept ansi tty configuration sequences
|
||||
if (isconsole && _weaken(InterceptTerminalCommands)) {
|
||||
_weaken(InterceptTerminalCommands)(data, size);
|
||||
}
|
||||
|
||||
// To use the tty mouse events feature:
|
||||
// - write(1, "\e[?1000;1002;1015;1006h") to enable
|
||||
// - write(1, "\e[?1000;1002;1015;1006l") to disable
|
||||
// See o//examples/ttyinfo.com and o//tool/viz/life.com
|
||||
uint32_t cm;
|
||||
if (!seekable && (f->kind == kFdConsole || GetConsoleMode(handle, &cm))) {
|
||||
int64_t hin;
|
||||
if (f->kind == kFdConsole) {
|
||||
hin = f->handle;
|
||||
} else {
|
||||
hin = GetStdHandle(kNtStdInputHandle);
|
||||
}
|
||||
if (GetConsoleMode(hin, &cm)) {
|
||||
int t = 0;
|
||||
unsigned x;
|
||||
bool m = false;
|
||||
char *p = data;
|
||||
uint32_t cm2 = cm;
|
||||
for (int i = 0; i < size; ++i) {
|
||||
switch (t) {
|
||||
case 0:
|
||||
if (p[i] == 033) {
|
||||
t = 1;
|
||||
}
|
||||
break;
|
||||
case 1:
|
||||
if (p[i] == '[') {
|
||||
t = 2;
|
||||
} else {
|
||||
t = 0;
|
||||
}
|
||||
break;
|
||||
case 2:
|
||||
if (p[i] == '?') {
|
||||
t = 3;
|
||||
x = 0;
|
||||
} else {
|
||||
t = 0;
|
||||
}
|
||||
break;
|
||||
case 3:
|
||||
if ('0' <= p[i] && p[i] <= '9') {
|
||||
x *= 10;
|
||||
x += p[i] - '0';
|
||||
} else if (p[i] == ';') {
|
||||
m |= IsMouseModeCommand(x);
|
||||
x = 0;
|
||||
} else {
|
||||
m |= IsMouseModeCommand(x);
|
||||
if (p[i] == 'h') {
|
||||
__ttyconf.magic |= kTtyXtMouse;
|
||||
cm2 |= kNtEnableMouseInput;
|
||||
cm2 &= kNtEnableQuickEditMode; // precludes mouse events
|
||||
} else if (p[i] == 'l') {
|
||||
__ttyconf.magic &= ~kTtyXtMouse;
|
||||
cm2 |= kNtEnableQuickEditMode; // disables mouse too
|
||||
}
|
||||
t = 0;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
__builtin_unreachable();
|
||||
}
|
||||
}
|
||||
if (cm2 != cm) {
|
||||
SetConsoleMode(hin, cm2);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (seekable && !pwriting) {
|
||||
pthread_mutex_lock(&f->lock);
|
||||
offset = f->pointer;
|
||||
}
|
||||
|
||||
struct NtOverlapped overlap = {.hEvent = CreateEvent(0, 0, 0, 0),
|
||||
.Pointer = offset};
|
||||
ok = WriteFile(handle, data, size, 0, &overlap);
|
||||
if (!ok && GetLastError() == kNtErrorIoPending) {
|
||||
BlockingOperation:
|
||||
if (!nonblock) {
|
||||
pt->ioverlap = &overlap;
|
||||
pt->iohandle = handle;
|
||||
}
|
||||
if (nonblock) {
|
||||
CancelIoEx(handle, &overlap);
|
||||
} else if (_check_interrupts(kSigOpRestartable)) {
|
||||
Interrupted:
|
||||
pt->abort_errno = errno;
|
||||
CancelIoEx(handle, &overlap);
|
||||
} else {
|
||||
for (;;) {
|
||||
uint32_t i;
|
||||
i = WaitForSingleObject(overlap.hEvent, __SIG_IO_INTERVAL_MS);
|
||||
if (i == kNtWaitTimeout) {
|
||||
if (_check_interrupts(kSigOpRestartable)) {
|
||||
goto Interrupted;
|
||||
}
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
pt->ioverlap = 0;
|
||||
pt->iohandle = 0;
|
||||
ok = true;
|
||||
}
|
||||
if (ok) {
|
||||
// overlapped is allocated on stack, so it's important we wait
|
||||
// for windows to acknowledge that it's done using that memory
|
||||
ok = GetOverlappedResult(handle, &overlap, &sent, nonblock);
|
||||
if (!ok && GetLastError() == kNtErrorIoIncomplete) {
|
||||
goto BlockingOperation;
|
||||
}
|
||||
}
|
||||
CloseHandle(overlap.hEvent);
|
||||
|
||||
if (seekable && !pwriting) {
|
||||
if (ok) f->pointer = offset + sent;
|
||||
pthread_mutex_unlock(&f->lock);
|
||||
}
|
||||
|
||||
if (ok) {
|
||||
return sent;
|
||||
}
|
||||
|
||||
errno_t err;
|
||||
if (_weaken(pthread_testcancel_np) &&
|
||||
(err = _weaken(pthread_testcancel_np)())) {
|
||||
return ecanceled();
|
||||
}
|
||||
// perform heavy lifting
|
||||
ssize_t rc;
|
||||
rc = sys_readwrite_nt(fd, data, size, offset, handle, waitmask,
|
||||
(void *)WriteFile);
|
||||
if (rc != -2) return rc;
|
||||
|
||||
// mops up win32 errors
|
||||
switch (GetLastError()) {
|
||||
// case kNtErrorInvalidHandle:
|
||||
// return ebadf(); /* handled by consts.sh */
|
||||
|
@ -224,24 +78,20 @@ static textwindows ssize_t sys_write_nt_impl(int fd, void *data, size_t size,
|
|||
// return edquot(); /* handled by consts.sh */
|
||||
case kNtErrorBrokenPipe: // broken pipe
|
||||
case kNtErrorNoData: // closing named pipe
|
||||
if (_weaken(__sig_raise)) {
|
||||
_weaken(__sig_raise)(SIGPIPE, SI_KERNEL);
|
||||
return epipe();
|
||||
} else {
|
||||
TerminateThisProcess(SIGPIPE);
|
||||
}
|
||||
m = __sig_beginwait(waitmask);
|
||||
RaiseSignal(SIGPIPE);
|
||||
__sig_finishwait(m);
|
||||
return epipe();
|
||||
case kNtErrorAccessDenied: // write doesn't return EACCESS
|
||||
return ebadf();
|
||||
case kNtErrorOperationAborted:
|
||||
errno = pt->abort_errno;
|
||||
return -1;
|
||||
default:
|
||||
return __winerr();
|
||||
}
|
||||
}
|
||||
|
||||
textwindows ssize_t sys_write_nt(int fd, const struct iovec *iov, size_t iovlen,
|
||||
ssize_t opt_offset) {
|
||||
static textwindows ssize_t sys_write_nt2(int fd, const struct iovec *iov,
|
||||
size_t iovlen, ssize_t opt_offset,
|
||||
uint64_t waitmask) {
|
||||
ssize_t rc;
|
||||
size_t i, total;
|
||||
if (opt_offset < -1) return einval();
|
||||
|
@ -249,7 +99,8 @@ textwindows ssize_t sys_write_nt(int fd, const struct iovec *iov, size_t iovlen,
|
|||
if (iovlen) {
|
||||
for (total = i = 0; i < iovlen; ++i) {
|
||||
if (!iov[i].iov_len) continue;
|
||||
rc = sys_write_nt_impl(fd, iov[i].iov_base, iov[i].iov_len, opt_offset);
|
||||
rc = sys_write_nt_impl(fd, iov[i].iov_base, iov[i].iov_len, opt_offset,
|
||||
waitmask);
|
||||
if (rc == -1) {
|
||||
if (total && errno != ECANCELED) {
|
||||
return total;
|
||||
|
@ -260,9 +111,21 @@ textwindows ssize_t sys_write_nt(int fd, const struct iovec *iov, size_t iovlen,
|
|||
total += rc;
|
||||
if (opt_offset != -1) opt_offset += rc;
|
||||
if (rc < iov[i].iov_len) break;
|
||||
waitmask = -1; // disable eintr/ecanceled for remaining iovecs
|
||||
}
|
||||
return total;
|
||||
} else {
|
||||
return sys_write_nt_impl(fd, NULL, 0, opt_offset);
|
||||
return sys_write_nt_impl(fd, NULL, 0, opt_offset, waitmask);
|
||||
}
|
||||
}
|
||||
|
||||
textwindows ssize_t sys_write_nt(int fd, const struct iovec *iov, size_t iovlen,
|
||||
ssize_t opt_offset) {
|
||||
ssize_t rc;
|
||||
sigset_t m = __sig_block();
|
||||
rc = sys_write_nt2(fd, iov, iovlen, opt_offset, m);
|
||||
__sig_unblock(m);
|
||||
return rc;
|
||||
}
|
||||
|
||||
#endif /* __x86_64__ */
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue