mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-06-28 07:18:30 +00:00
Improve wait statuses
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.
This commit is contained in:
parent
d9d5f45e2d
commit
c8aa33e0e2
22 changed files with 259 additions and 75 deletions
|
@ -27,6 +27,7 @@
|
|||
#include "libc/calls/struct/sigset.h"
|
||||
#include "libc/intrin/strace.internal.h"
|
||||
#include "libc/macros.internal.h"
|
||||
#include "libc/nt/runtime.h"
|
||||
#include "libc/runtime/internal.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/str/str.h"
|
||||
|
@ -172,13 +173,14 @@ static textwindows bool __sig_is_fatal(int sig) {
|
|||
* Handles signal.
|
||||
* @return true if signal was delivered
|
||||
*/
|
||||
bool __sig_handle(int sigops, int sig, int si_code, ucontext_t *ctx) {
|
||||
textwindows bool __sig_handle(int sigops, int sig, int si_code,
|
||||
ucontext_t *ctx) {
|
||||
bool delivered;
|
||||
switch (__sighandrvas[sig]) {
|
||||
case (intptr_t)SIG_DFL:
|
||||
if (__sig_is_fatal(sig)) {
|
||||
STRACE("terminating on %G", sig);
|
||||
_Exitr(128 + sig);
|
||||
ExitProcess(sig);
|
||||
}
|
||||
// fallthrough
|
||||
case (intptr_t)SIG_IGN:
|
||||
|
|
|
@ -44,15 +44,15 @@
|
|||
|
||||
#define MAP_FAILED ((void *)-1)
|
||||
|
||||
#define WCOREDUMP(s) (128 & (s))
|
||||
#define WEXITSTATUS(s) ((0xff00 & (s)) >> 8)
|
||||
#define WIFCONTINUED(s) ((s) == 0xffff)
|
||||
#define WIFEXITED(s) (!WTERMSIG(s))
|
||||
#define WIFSIGNALED(s) (((signed char)((127 & (s)) + 1) >> 1) > 0)
|
||||
#define WIFSTOPPED(s) ((255 & (s)) == 127)
|
||||
#define WSTOPSIG(s) WEXITSTATUS(s)
|
||||
#define WTERMSIG(s) (127 & (s))
|
||||
#define W_STOPCODE(s) ((s) << 8 | 0177)
|
||||
#define WTERMSIG(x) (127 & (x))
|
||||
#define WCOREDUMP(x) (128 & (x))
|
||||
#define WIFEXITED(x) (!WTERMSIG(x))
|
||||
#define WEXITSTATUS(x) ((x) >> 8)
|
||||
#define WSTOPSIG(x) ((0xff00 & (x)) >> 8)
|
||||
#define WIFSTOPPED(x) __wifstopped(x)
|
||||
#define WIFSIGNALED(x) __wifsignaled(x)
|
||||
#define WIFCONTINUED(x) __wifcontinued(x)
|
||||
#define W_STOPCODE(x) ((x) << 8 | 0177)
|
||||
|
||||
#if !(__ASSEMBLER__ + __LINKER__ + 0)
|
||||
COSMOPOLITAN_C_START_
|
||||
|
@ -234,6 +234,10 @@ ssize_t readansi(int, char *, size_t);
|
|||
ssize_t tinyprint(int, const char *, ...) nullterminated();
|
||||
#endif
|
||||
|
||||
int __wifstopped(int) pureconst;
|
||||
int __wifcontinued(int) pureconst;
|
||||
int __wifsignaled(int) pureconst;
|
||||
|
||||
COSMOPOLITAN_C_END_
|
||||
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
|
||||
#endif /* COSMOPOLITAN_LIBC_CALLS_SYSCALLS_H_ */
|
||||
|
|
|
@ -119,7 +119,7 @@ textwindows int sys_execve_nt(const char *program, char *const argv[],
|
|||
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(6543);
|
||||
__imp_ExitProcess(11);
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
|
|
@ -95,14 +95,14 @@ textwindows int sys_kill_nt(int pid, int sig) {
|
|||
do {
|
||||
if (pe.th32ParentProcessID == ntpid) {
|
||||
if ((h = OpenProcess(kNtProcessTerminate, false, pe.th32ProcessID))) {
|
||||
TerminateProcess(h, 128 + sig);
|
||||
TerminateProcess(h, sig);
|
||||
CloseHandle(h);
|
||||
}
|
||||
}
|
||||
} while (Process32Next(hSnap, &pe));
|
||||
}
|
||||
CloseHandle(hSnap);
|
||||
ok = TerminateProcess(g_fds.p[pid].handle, 128 + sig);
|
||||
ok = TerminateProcess(g_fds.p[pid].handle, sig);
|
||||
if (!ok && GetLastError() == kNtErrorAccessDenied) ok = true;
|
||||
return 0;
|
||||
}
|
||||
|
@ -110,7 +110,7 @@ textwindows int sys_kill_nt(int pid, int sig) {
|
|||
// XXX: Is this a raw new technology pid? Because that's messy.
|
||||
if ((h = OpenProcess(kNtProcessTerminate, false, pid))) {
|
||||
if (sig) {
|
||||
ok = TerminateProcess(h, 128 + sig);
|
||||
ok = TerminateProcess(h, sig);
|
||||
if (!ok && GetLastError() == kNtErrorAccessDenied) {
|
||||
ok = true; // cargo culting other codebases here
|
||||
}
|
||||
|
|
|
@ -50,10 +50,6 @@ static dontubsan void RaiseSigFpe(void) {
|
|||
* helps us support Windows. So if the raised signal has a signal
|
||||
* handler, then the reported `si_code` might not be `SI_TKILL`.
|
||||
*
|
||||
* On Windows, if a signal results in the termination of the process
|
||||
* then we use the convention `_Exit(128 + sig)` to notify the parent of
|
||||
* the signal number.
|
||||
*
|
||||
* @param sig can be SIGALRM, SIGINT, SIGTERM, SIGKILL, etc.
|
||||
* @return 0 if signal was delivered and returned, or -1 w/ errno
|
||||
* @asyncsignalsafe
|
||||
|
|
|
@ -134,8 +134,11 @@ static textwindows int sys_wait4_nt_impl(int *pid, int *opt_out_wstatus,
|
|||
if (dwExitCode == kNtStillActive) {
|
||||
return -2;
|
||||
}
|
||||
if (opt_out_wstatus) { // @see WEXITSTATUS()
|
||||
*opt_out_wstatus = (dwExitCode & 0xff) << 8;
|
||||
if (dwExitCode == 0xc9af3d51u) {
|
||||
dwExitCode = kNtStillActive;
|
||||
}
|
||||
if (opt_out_wstatus) {
|
||||
*opt_out_wstatus = dwExitCode;
|
||||
}
|
||||
if (opt_out_rusage) {
|
||||
bzero(opt_out_rusage, sizeof(*opt_out_rusage));
|
||||
|
|
36
libc/calls/wifcontinued.c
Normal file
36
libc/calls/wifcontinued.c
Normal file
|
@ -0,0 +1,36 @@
|
|||
/*-*- 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 2023 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. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/dce.h"
|
||||
|
||||
int __wifcontinued(int x) {
|
||||
if (IsLinux() || IsMetal() || IsWindows()) {
|
||||
return x == 0xffff;
|
||||
} else if (IsXnu()) {
|
||||
return (x & 0177) == 0177 && WSTOPSIG(x) == 0x13; // SIGCONT
|
||||
} else if (IsFreebsd()) {
|
||||
return x == 0x13; // SIGCONT
|
||||
} else if (IsOpenbsd()) {
|
||||
return (x & 0177777) == 0177777;
|
||||
} else if (IsNetbsd()) {
|
||||
return x == 0177777;
|
||||
} else {
|
||||
__builtin_unreachable();
|
||||
}
|
||||
}
|
35
libc/calls/wifsignaled.c
Normal file
35
libc/calls/wifsignaled.c
Normal file
|
@ -0,0 +1,35 @@
|
|||
/*-*- 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 2023 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. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/dce.h"
|
||||
|
||||
int __wifsignaled(int x) {
|
||||
if (IsLinux() || IsMetal() || IsWindows()) {
|
||||
return (x & 0xffff) - 1U < 0xffu;
|
||||
} else if (IsXnu() || IsOpenbsd()) {
|
||||
return (x & 0177) != 0177 && (x & 0177) != 0;
|
||||
} else if (IsFreebsd()) {
|
||||
return (x & 0177) != 0177 && (x & 0177) != 0 && x != 0x13; // SIGCONT
|
||||
} else if (IsNetbsd()) {
|
||||
return !((x & 0177) == 0177 && !(x == 0177777)) && !(x == 0177777) &&
|
||||
!!(127 & x);
|
||||
} else {
|
||||
__builtin_unreachable();
|
||||
}
|
||||
}
|
34
libc/calls/wifstopped.c
Normal file
34
libc/calls/wifstopped.c
Normal file
|
@ -0,0 +1,34 @@
|
|||
/*-*- 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 2023 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. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/dce.h"
|
||||
|
||||
int __wifstopped(int x) {
|
||||
if (IsLinux() || IsMetal() || IsWindows()) {
|
||||
return (short)(((x & 0xffff) * 0x10001U) >> 8) > 0x7f00;
|
||||
} else if (IsXnu()) {
|
||||
return (x & 0177) == 0177 && WSTOPSIG(x) != 0x13; // SIGCONT
|
||||
} else if (IsOpenbsd() || IsFreebsd()) {
|
||||
return (x & 0xff) == 0177;
|
||||
} else if (IsNetbsd()) {
|
||||
return (x & 0177) == 0177 && !(x == 0177777);
|
||||
} else {
|
||||
__builtin_unreachable();
|
||||
}
|
||||
}
|
|
@ -51,13 +51,13 @@ unsigned __wincrash(struct NtExceptionPointers *ep) {
|
|||
if (~tib->tib_flags & TIB_FLAG_WINCRASHING) {
|
||||
tib->tib_flags |= TIB_FLAG_WINCRASHING;
|
||||
} else {
|
||||
ExitProcess(89);
|
||||
ExitProcess(SIGSEGV);
|
||||
}
|
||||
} else {
|
||||
if (!noreentry) {
|
||||
noreentry = true;
|
||||
} else {
|
||||
ExitProcess(89);
|
||||
ExitProcess(SIGSEGV);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -78,7 +78,7 @@ static textwindows ssize_t sys_write_nt_impl(int fd, void *data, size_t size,
|
|||
return epipe();
|
||||
} else {
|
||||
STRACE("broken pipe");
|
||||
_Exitr(128 + EPIPE);
|
||||
ExitProcess(EPIPE);
|
||||
}
|
||||
case kNtErrorAccessDenied: // write doesn't return EACCESS
|
||||
return ebadf();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue