mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-07-08 12:18: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
|
@ -51,6 +51,7 @@
|
|||
#include "libc/nt/synchronization.h"
|
||||
#include "libc/nt/thread.h"
|
||||
#include "libc/nt/thunk/msabi.h"
|
||||
#include "libc/proc/describefds.internal.h"
|
||||
#include "libc/proc/ntspawn.h"
|
||||
#include "libc/proc/proc.internal.h"
|
||||
#include "libc/runtime/internal.h"
|
||||
|
@ -68,9 +69,8 @@
|
|||
|
||||
#ifdef __x86_64__
|
||||
|
||||
extern long __klog_handle;
|
||||
extern int64_t __wincrashearly;
|
||||
bool32 __onntconsoleevent(uint32_t);
|
||||
void __keystroke_wipe(void);
|
||||
|
||||
static textwindows wontreturn void AbortFork(const char *func) {
|
||||
#ifdef SYSDEBUG
|
||||
|
@ -130,7 +130,6 @@ static textwindows dontinline void ReadOrDie(int64_t h, void *buf, size_t n) {
|
|||
if (!ForkIo2(h, buf, n, ReadFile, "ReadFile", true)) {
|
||||
AbortFork("ReadFile1");
|
||||
}
|
||||
if (_weaken(__klog_handle)) *_weaken(__klog_handle) = 0;
|
||||
#ifndef NDEBUG
|
||||
size_t got;
|
||||
if (!ForkIo2(h, &got, sizeof(got), ReadFile, "ReadFile", true)) {
|
||||
|
@ -283,15 +282,6 @@ textwindows void WinMainForked(void) {
|
|||
fds->p[1].handle = GetStdHandle(kNtStdOutputHandle);
|
||||
fds->p[2].handle = GetStdHandle(kNtStdErrorHandle);
|
||||
|
||||
// untrack children of parent since we specify with both
|
||||
// CreateProcess() and CreateThread() as non-inheritable
|
||||
for (i = 0; i < fds->n; ++i) {
|
||||
if (fds->p[i].kind == kFdProcess) {
|
||||
fds->p[i].kind = 0;
|
||||
atomic_store_explicit(&fds->f, MIN(i, fds->f), memory_order_relaxed);
|
||||
}
|
||||
}
|
||||
|
||||
// restore the crash reporting stuff
|
||||
#ifdef SYSDEBUG
|
||||
RemoveVectoredExceptionHandler(oncrash);
|
||||
|
@ -304,24 +294,6 @@ textwindows void WinMainForked(void) {
|
|||
longjmp(jb, 1);
|
||||
}
|
||||
|
||||
static void __hand_inherit(bool32 bInherit) {
|
||||
struct CosmoTib *tib = __get_tls();
|
||||
SetHandleInformation(tib->tib_syshand, kNtHandleFlagInherit, bInherit);
|
||||
SetHandleInformation(tib->tib_syshand, kNtHandleFlagInherit, bInherit);
|
||||
for (int i = 0; i < _mmi.i; ++i) {
|
||||
if ((_mmi.p[i].flags & MAP_TYPE) == MAP_SHARED) {
|
||||
SetHandleInformation(_mmi.p[i].h, kNtHandleFlagInherit, bInherit);
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < g_fds.n; ++i) {
|
||||
if (g_fds.p[i].kind == kFdEmpty) continue;
|
||||
SetHandleInformation(g_fds.p[i].handle, kNtHandleFlagInherit, bInherit);
|
||||
if (g_fds.p[i].kind == kFdConsole) {
|
||||
SetHandleInformation(g_fds.p[i].extra, kNtHandleFlagInherit, bInherit);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
textwindows int sys_fork_nt(uint32_t dwCreationFlags) {
|
||||
char ok;
|
||||
jmp_buf jb;
|
||||
|
@ -333,8 +305,8 @@ textwindows int sys_fork_nt(uint32_t dwCreationFlags) {
|
|||
char16_t pipename[64];
|
||||
int64_t reader, writer;
|
||||
struct NtStartupInfo startinfo;
|
||||
char *p, forkvar[6 + 21 + 1 + 21 + 1];
|
||||
struct NtProcessInformation procinfo;
|
||||
char *p, forkvar[6 + 21 + 1 + 21 + 1];
|
||||
tib = __get_tls();
|
||||
ftrace_enabled(-1);
|
||||
strace_enabled(-1);
|
||||
|
@ -372,12 +344,10 @@ textwindows int sys_fork_nt(uint32_t dwCreationFlags) {
|
|||
args = args2;
|
||||
}
|
||||
#endif
|
||||
__hand_inherit(true);
|
||||
NTTRACE("STARTING SPAWN");
|
||||
int spawnrc =
|
||||
ntspawn(GetProgramExecutableName(), args, environ, forkvar, 0, 0,
|
||||
true, dwCreationFlags, 0, &startinfo, &procinfo);
|
||||
__hand_inherit(false);
|
||||
int spawnrc = ntspawn(GetProgramExecutableName(), args, environ,
|
||||
(char *[]){forkvar, 0}, dwCreationFlags, 0, 0, 0, 0,
|
||||
&startinfo, &procinfo);
|
||||
if (spawnrc != -1) {
|
||||
CloseHandle(procinfo.hThread);
|
||||
ok = WriteAll(writer, jb, sizeof(jb)) &&
|
||||
|
@ -435,9 +405,11 @@ textwindows int sys_fork_nt(uint32_t dwCreationFlags) {
|
|||
if (ftrace_stackdigs) {
|
||||
_weaken(__hook)(_weaken(ftrace_hook), _weaken(GetSymbolTable)());
|
||||
}
|
||||
// reset console
|
||||
__keystroke_wipe();
|
||||
// reset alarms
|
||||
if (_weaken(__itimer_reset)) {
|
||||
_weaken(__itimer_reset)();
|
||||
if (_weaken(__itimer_wipe)) {
|
||||
_weaken(__itimer_wipe)();
|
||||
}
|
||||
}
|
||||
if (rc == -1) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue