mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-01-31 19:43:32 +00:00
c8aa33e0e2
This change has the insight that dwExitCode isn't an exit code but rather should be used to pass the wait status. This lets us report killing as a termination status, similar to UNIX. This change also fixes the fact that exit(259) on Windows will break the parent due way WIN32 is designed. We now work around that. It turns out that NetBSD and OpenBSD, will let you have exit codes beyond 255. This change will let you use them when it's possible.
141 lines
5.7 KiB
C
141 lines
5.7 KiB
C
/*-*- 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 │
|
|
│ │
|
|
│ 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. │
|
|
│ │
|
|
│ 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. │
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
|
#define ShouldUseMsabiAttribute() 1
|
|
#include "libc/calls/calls.h"
|
|
#include "libc/calls/internal.h"
|
|
#include "libc/calls/ntspawn.h"
|
|
#include "libc/calls/syscall-nt.internal.h"
|
|
#include "libc/fmt/itoa.h"
|
|
#include "libc/intrin/strace.internal.h"
|
|
#include "libc/intrin/weaken.h"
|
|
#include "libc/mem/alloca.h"
|
|
#include "libc/nt/accounting.h"
|
|
#include "libc/nt/console.h"
|
|
#include "libc/nt/enum/startf.h"
|
|
#include "libc/nt/enum/status.h"
|
|
#include "libc/nt/memory.h"
|
|
#include "libc/nt/runtime.h"
|
|
#include "libc/nt/struct/processinformation.h"
|
|
#include "libc/nt/struct/startupinfo.h"
|
|
#include "libc/nt/synchronization.h"
|
|
#include "libc/nt/thunk/msabi.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/at.h"
|
|
#include "libc/sysv/consts/map.h"
|
|
#include "libc/sysv/consts/o.h"
|
|
#include "libc/sysv/consts/ok.h"
|
|
#include "libc/sysv/errfuns.h"
|
|
|
|
__msabi extern typeof(CloseHandle) *const __imp_CloseHandle;
|
|
__msabi extern typeof(WaitForSingleObject) *const __imp_WaitForSingleObject;
|
|
__msabi extern typeof(GetExitCodeProcess) *const __imp_GetExitCodeProcess;
|
|
__msabi extern typeof(UnmapViewOfFile) *const __imp_UnmapViewOfFile;
|
|
|
|
static dontinstrument __msabi bool32
|
|
BlockExecveConsoleEvent(uint32_t dwCtrlType) {
|
|
// block SIGINT and SIGQUIT in execve() parent process
|
|
return true;
|
|
}
|
|
|
|
textwindows int sys_execve_nt(const char *program, char *const argv[],
|
|
char *const envp[]) {
|
|
int rc;
|
|
size_t i;
|
|
uint32_t dwExitCode;
|
|
char progbuf[PATH_MAX];
|
|
struct NtStartupInfo startinfo;
|
|
struct NtProcessInformation procinfo;
|
|
|
|
if (strlen(program) + 4 + 1 > PATH_MAX) {
|
|
return enametoolong();
|
|
}
|
|
|
|
// this is a non-recoverable operation, so do some manual validation
|
|
if (sys_faccessat_nt(AT_FDCWD, program, X_OK, 0) == -1) {
|
|
stpcpy(stpcpy(progbuf, program), ".com");
|
|
if (sys_faccessat_nt(AT_FDCWD, progbuf, X_OK, 0) != -1) {
|
|
program = progbuf;
|
|
} else {
|
|
stpcpy(stpcpy(progbuf, program), ".exe");
|
|
if (sys_faccessat_nt(AT_FDCWD, progbuf, X_OK, 0) != -1) {
|
|
program = progbuf;
|
|
} else {
|
|
return eacces();
|
|
}
|
|
}
|
|
}
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
// execve operation is unrecoverable from this point
|
|
|
|
// close non-stdio and cloexec handles
|
|
for (i = 0; i < g_fds.n; ++i) {
|
|
if (g_fds.p[i].kind == kFdEmpty) {
|
|
g_fds.p[i].handle = -1;
|
|
} else if (i > 2 || (g_fds.p[i].flags & O_CLOEXEC)) {
|
|
__imp_CloseHandle(g_fds.p[i].handle);
|
|
g_fds.p[i].handle = -1;
|
|
}
|
|
}
|
|
|
|
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;
|
|
}
|
|
bzero(&startinfo, sizeof(startinfo));
|
|
startinfo.cb = sizeof(struct NtStartupInfo);
|
|
startinfo.dwFlags = kNtStartfUsestdhandles;
|
|
startinfo.hStdInput = g_fds.p[0].handle;
|
|
startinfo.hStdOutput = g_fds.p[1].handle;
|
|
startinfo.hStdError = g_fds.p[2].handle;
|
|
|
|
// spawn the process
|
|
rc = ntspawn(program, argv, envp, v, 0, 0, true, 0, 0, &startinfo, &procinfo);
|
|
if (rc == -1) {
|
|
STRACE("panic: unrecoverable ntspawn(%#s) error: %m", program);
|
|
__imp_ExitProcess(11);
|
|
}
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
// zombie shell process remains, to wait for child and propagate its exit
|
|
// code
|
|
|
|
__imp_CloseHandle(g_fds.p[0].handle);
|
|
__imp_CloseHandle(g_fds.p[1].handle);
|
|
__imp_CloseHandle(procinfo.hThread);
|
|
__imp_SetConsoleCtrlHandler((void *)BlockExecveConsoleEvent, 1);
|
|
do {
|
|
__imp_WaitForSingleObject(procinfo.hProcess, -1);
|
|
dwExitCode = kNtStillActive;
|
|
__imp_GetExitCodeProcess(procinfo.hProcess, &dwExitCode);
|
|
} while (dwExitCode == kNtStillActive);
|
|
__imp_CloseHandle(procinfo.hProcess);
|
|
__imp_ExitProcess(dwExitCode);
|
|
notpossible;
|
|
}
|