mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-07-11 21:49:12 +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,105 +16,46 @@
|
|||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/assert.h"
|
||||
#include "libc/calls/internal.h"
|
||||
#include "libc/calls/struct/fd.internal.h"
|
||||
#include "libc/calls/struct/sigaction.internal.h"
|
||||
#include "libc/fmt/itoa.h"
|
||||
#include "libc/intrin/dll.h"
|
||||
#include "libc/intrin/handlock.internal.h"
|
||||
#include "libc/intrin/weaken.h"
|
||||
#include "libc/nt/accounting.h"
|
||||
#include "libc/nt/console.h"
|
||||
#include "libc/calls/struct/sigset.internal.h"
|
||||
#include "libc/calls/syscall-nt.internal.h"
|
||||
#include "libc/errno.h"
|
||||
#include "libc/intrin/kprintf.h"
|
||||
#include "libc/mem/mem.h"
|
||||
#include "libc/nt/enum/processaccess.h"
|
||||
#include "libc/nt/enum/startf.h"
|
||||
#include "libc/nt/enum/status.h"
|
||||
#include "libc/nt/enum/wait.h"
|
||||
#include "libc/nt/errors.h"
|
||||
#include "libc/nt/files.h"
|
||||
#include "libc/nt/memory.h"
|
||||
#include "libc/nt/process.h"
|
||||
#include "libc/nt/runtime.h"
|
||||
#include "libc/nt/struct/msg.h"
|
||||
#include "libc/nt/struct/processinformation.h"
|
||||
#include "libc/nt/struct/startupinfo.h"
|
||||
#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/memtrack.internal.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/sock/sock.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "libc/sysv/consts/o.h"
|
||||
#include "libc/sysv/consts/sig.h"
|
||||
#include "libc/sysv/errfuns.h"
|
||||
#include "libc/thread/itimer.internal.h"
|
||||
#include "libc/thread/posixthread.internal.h"
|
||||
#include "libc/thread/tls.h"
|
||||
#include "libc/thread/thread.h"
|
||||
#ifdef __x86_64__
|
||||
|
||||
#define keywords textwindows dontinstrument
|
||||
textwindows int sys_execve_nt(const char *program, char *const argv[],
|
||||
char *const envp[]) {
|
||||
|
||||
// clang-format off
|
||||
__msabi extern typeof(CloseHandle) *const __imp_CloseHandle;
|
||||
__msabi extern typeof(DuplicateHandle) *const __imp_DuplicateHandle;
|
||||
__msabi extern typeof(GenerateConsoleCtrlEvent) *const __imp_GenerateConsoleCtrlEvent;
|
||||
__msabi extern typeof(GetCurrentThreadId) *const __imp_GetCurrentThreadId;
|
||||
__msabi extern typeof(GetExitCodeProcess) *const __imp_GetExitCodeProcess;
|
||||
__msabi extern typeof(GetLastError) *const __imp_GetLastError;
|
||||
__msabi extern typeof(OpenThread) *const __imp_OpenThread;
|
||||
__msabi extern typeof(SetConsoleCtrlHandler) *const __imp_SetConsoleCtrlHandler;
|
||||
__msabi extern typeof(SetHandleInformation) *const __imp_SetHandleInformation;
|
||||
__msabi extern typeof(TerminateThread) *const __imp_TerminateThread;
|
||||
__msabi extern typeof(UnmapViewOfFile) *const __imp_UnmapViewOfFile;
|
||||
__msabi extern typeof(WaitForSingleObject) *const __imp_WaitForSingleObject;
|
||||
// clang-format on
|
||||
// execve() needs to be @asyncsignalsafe
|
||||
sigset_t m = __sig_block();
|
||||
_pthread_lock();
|
||||
|
||||
extern long __klog_handle;
|
||||
static void sys_execve_nt_relay(intptr_t, long, long, long);
|
||||
void __stack_call(intptr_t, long, long, long,
|
||||
void (*)(intptr_t, intptr_t, long, long),
|
||||
intptr_t) wontreturn;
|
||||
|
||||
static keywords void PurgeHandle(intptr_t h) {
|
||||
if (!h) return;
|
||||
if (h == -1) return;
|
||||
__imp_CloseHandle(h);
|
||||
}
|
||||
|
||||
static keywords void PurgeThread(intptr_t h) {
|
||||
if (h && h != -1) {
|
||||
__imp_TerminateThread(h, SIGKILL);
|
||||
__imp_CloseHandle(h);
|
||||
}
|
||||
}
|
||||
|
||||
static keywords void sys_execve_inherit(int64_t hands[3], bool32 bInherit) {
|
||||
for (int i = 0; i < 3; ++i) {
|
||||
if (hands[i] != -1) {
|
||||
__imp_SetHandleInformation(hands[i], kNtHandleFlagInherit, bInherit);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
keywords int sys_execve_nt(const char *program, char *const argv[],
|
||||
char *const envp[]) {
|
||||
size_t i;
|
||||
|
||||
__hand_lock();
|
||||
pthread_spin_lock(&_pthread_lock);
|
||||
|
||||
// pass bitmask telling child which fds are sockets
|
||||
int bits;
|
||||
char buf[32], *v = 0;
|
||||
if (_weaken(socket)) {
|
||||
for (bits = i = 0; i < 3; ++i) {
|
||||
if (g_fds.p[i].kind == kFdSocket) {
|
||||
bits |= 1 << i;
|
||||
}
|
||||
}
|
||||
FormatInt32(stpcpy(buf, "__STDIO_SOCKETS="), bits);
|
||||
v = buf;
|
||||
// new process should be a child of our parent
|
||||
int64_t hParentProcess;
|
||||
int ppid = sys_getppid_nt();
|
||||
if (!(hParentProcess = OpenProcess(
|
||||
kNtProcessDupHandle | kNtProcessCreateProcess, false, ppid))) {
|
||||
_pthread_unlock();
|
||||
__sig_unblock(m);
|
||||
return -1;
|
||||
}
|
||||
|
||||
// define stdio handles for the spawned subprocess
|
||||
|
@ -122,96 +63,55 @@ keywords int sys_execve_nt(const char *program, char *const argv[],
|
|||
.cb = sizeof(struct NtStartupInfo),
|
||||
.dwFlags = kNtStartfUsestdhandles,
|
||||
};
|
||||
for (i = 0; i <= 2; ++i) {
|
||||
if (g_fds.p[i].kind != kFdEmpty && //
|
||||
!(g_fds.p[i].flags & O_CLOEXEC)) {
|
||||
si.stdiofds[i] = g_fds.p[i].handle;
|
||||
for (int fd = 0; fd < 3; ++fd) {
|
||||
if (!__is_cloexec(g_fds.p + fd)) {
|
||||
si.stdiofds[fd] = g_fds.p[fd].handle;
|
||||
} else {
|
||||
si.stdiofds[i] = -1;
|
||||
si.stdiofds[fd] = -1;
|
||||
}
|
||||
}
|
||||
|
||||
// pass serialized file descriptor table in environment
|
||||
char *fdspec;
|
||||
int64_t *lpExplicitHandles;
|
||||
uint32_t dwExplicitHandleCount;
|
||||
if (!(fdspec = __describe_fds(g_fds.p, g_fds.n, &si, hParentProcess,
|
||||
&lpExplicitHandles, &dwExplicitHandleCount))) {
|
||||
CloseHandle(hParentProcess);
|
||||
_pthread_unlock();
|
||||
__sig_unblock(m);
|
||||
return -1;
|
||||
}
|
||||
|
||||
// launch the process
|
||||
struct NtProcessInformation pi;
|
||||
sys_execve_inherit(si.stdiofds, true);
|
||||
int rc = ntspawn(program, argv, envp, v, 0, 0, true, 0, 0, &si, &pi);
|
||||
int rc =
|
||||
ntspawn(program, argv, envp, (char *[]){fdspec, 0}, 0, 0, hParentProcess,
|
||||
lpExplicitHandles, dwExplicitHandleCount, &si, &pi);
|
||||
__undescribe_fds(hParentProcess, lpExplicitHandles, dwExplicitHandleCount);
|
||||
if (rc == -1) {
|
||||
sys_execve_inherit(si.stdiofds, false);
|
||||
__hand_unlock();
|
||||
if (__imp_GetLastError() == kNtErrorSharingViolation) {
|
||||
free(fdspec);
|
||||
CloseHandle(hParentProcess);
|
||||
__undescribe_fds(hParentProcess, lpExplicitHandles, dwExplicitHandleCount);
|
||||
_pthread_unlock();
|
||||
__sig_unblock(m);
|
||||
if (GetLastError() == kNtErrorSharingViolation) {
|
||||
return etxtbsy();
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
PurgeHandle(pi.hThread);
|
||||
|
||||
// kill siblings
|
||||
struct Dll *e;
|
||||
for (e = dll_first(_pthread_list); e; e = dll_next(_pthread_list, e)) {
|
||||
struct PosixThread *pt = POSIXTHREAD_CONTAINER(e);
|
||||
if ((pthread_t)pt == __get_tls()->tib_pthread) continue;
|
||||
PurgeThread(pt->tib->tib_syshand);
|
||||
PurgeHandle(pt->semaphore);
|
||||
// give child to libc/proc/proc.c worker thread in parent
|
||||
int64_t handle;
|
||||
if (!DuplicateHandle(GetCurrentProcess(), pi.hProcess, hParentProcess,
|
||||
&handle, 0, false, kNtDuplicateSameAccess)) {
|
||||
kprintf("failed to duplicate handle from %P into %d due to %s\n", ppid,
|
||||
strerror(GetLastError()));
|
||||
_Exit(1);
|
||||
}
|
||||
if (_weaken(__itimer)) {
|
||||
PurgeThread(_weaken(__itimer)->thread);
|
||||
}
|
||||
if (_weaken(__proc)) {
|
||||
PurgeThread(_weaken(__proc)->thread);
|
||||
PurgeHandle(_weaken(__proc)->onstart);
|
||||
}
|
||||
|
||||
// retreat to original win32-provided stack memory
|
||||
__ftrace = 0;
|
||||
__stack_call(pi.hProcess, 0, 0, 0, sys_execve_nt_relay, __oldstack);
|
||||
}
|
||||
|
||||
// child is in same process group so wait for it to get killed by this
|
||||
__msabi static keywords bool32 sys_execve_nt_event(uint32_t dwCtrlType) {
|
||||
return true; // tell win32 we handled signal
|
||||
}
|
||||
|
||||
// this function runs on the original tiny stack that windows gave us
|
||||
// we need to keep the original process alive simply to pass an int32
|
||||
// so we unmap all memory to avoid getting a double whammy after fork
|
||||
static keywords void sys_execve_nt_relay(intptr_t h, long b, long c, long d) {
|
||||
uint32_t i, dwExitCode;
|
||||
|
||||
// close more handles
|
||||
__imp_SetConsoleCtrlHandler((void *)sys_execve_nt_event, 1);
|
||||
for (i = 0; i < g_fds.n; ++i) {
|
||||
if (g_fds.p[i].kind != kFdEmpty) {
|
||||
PurgeHandle(g_fds.p[i].handle);
|
||||
if (g_fds.p[i].kind == kFdConsole) {
|
||||
PurgeHandle(g_fds.p[i].extra);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// free all the memory mmap created
|
||||
for (i = 0; i < _mmi.i; ++i) {
|
||||
__imp_UnmapViewOfFile((void *)((uintptr_t)_mmi.p[i].x << 16));
|
||||
PurgeHandle(_mmi.p[i].h);
|
||||
}
|
||||
|
||||
// wait for process to terminate
|
||||
//
|
||||
// WaitForSingleObject can return kNtWaitAbandoned which MSDN
|
||||
// describes as a "sort of" successful status which indicates
|
||||
// someone else didn't free a mutex and you should check that
|
||||
// persistent resources haven't been left corrupted. not sure
|
||||
// what those resources would be for process objects, however
|
||||
// this status has actually been observed when waiting on 'em
|
||||
do {
|
||||
dwExitCode = 255;
|
||||
if (__imp_WaitForSingleObject(h, -1) != kNtWaitFailed) {
|
||||
__imp_GetExitCodeProcess(h, &dwExitCode);
|
||||
}
|
||||
} while (dwExitCode == kNtStillActive);
|
||||
|
||||
// propagate child exit status to parent
|
||||
TerminateThisProcess(dwExitCode);
|
||||
unassert(!(handle & 0xFFFFFFFFFF000000));
|
||||
TerminateThisProcess(0x23000000u | handle);
|
||||
}
|
||||
|
||||
#endif /* __x86_64__ */
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue