mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-01-31 03:27:39 +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();
|
||||
|
|
|
@ -20,6 +20,7 @@
|
|||
#include "libc/intrin/promises.internal.h"
|
||||
#include "libc/intrin/strace.internal.h"
|
||||
#include "libc/nexgen32e/vendor.internal.h"
|
||||
#include "libc/nt/enum/status.h"
|
||||
#include "libc/nt/runtime.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/sysv/consts/nr.h"
|
||||
|
@ -30,7 +31,10 @@
|
|||
* When running on bare metal, this function will reboot your computer
|
||||
* by hosing the interrupt descriptors and triple faulting the system.
|
||||
*
|
||||
* @param exitcode is masked with 255 on unix (but not windows)
|
||||
* Exit codes are narrowed to an unsigned char on most platforms. The
|
||||
* exceptions would be Windows, NetBSD, and OpenBSD, which should let
|
||||
* you have larger exit codes.
|
||||
*
|
||||
* @asyncsignalsafe
|
||||
* @threadsafe
|
||||
* @vforksafe
|
||||
|
@ -79,7 +83,23 @@ wontreturn void _Exit(int exitcode) {
|
|||
: "x8", "memory");
|
||||
#endif
|
||||
} else if (IsWindows()) {
|
||||
ExitProcess(exitcode);
|
||||
uint32_t waitstatus;
|
||||
waitstatus = exitcode;
|
||||
waitstatus <<= 8;
|
||||
if (waitstatus == kNtStillActive) {
|
||||
// The GetExitCodeProcess function returns a valid error code
|
||||
// defined by the application only after the thread terminates.
|
||||
// Therefore, an application should not use STILL_ACTIVE (259) as
|
||||
// an error code (STILL_ACTIVE is a macro for STATUS_PENDING
|
||||
// (minwinbase.h)). If a thread returns STILL_ACTIVE (259) as an
|
||||
// error code, then applications that test for that value could
|
||||
// interpret it to mean that the thread is still running, and
|
||||
// continue to test for the completion of the thread after the
|
||||
// thread has terminated, which could put the application into an
|
||||
// infinite loop. -Quoth MSDN (see also libc/calls/wait4-nt.c)
|
||||
waitstatus = 0xc9af3d51u;
|
||||
}
|
||||
ExitProcess(waitstatus);
|
||||
}
|
||||
#ifdef __x86_64__
|
||||
asm("push\t$0\n\t"
|
||||
|
|
|
@ -389,6 +389,9 @@ privileged static size_t kformat(char *b, size_t n, const char *fmt,
|
|||
if (!(tib && (tib->tib_flags & TIB_FLAG_VFORKED))) {
|
||||
if (tib) {
|
||||
x = atomic_load_explicit(&tib->tib_tid, memory_order_relaxed);
|
||||
if (IsNetbsd() && x == 1) {
|
||||
x = __pid;
|
||||
}
|
||||
} else {
|
||||
x = __pid;
|
||||
}
|
||||
|
|
|
@ -28,10 +28,10 @@ __msabi extern typeof(TerminateProcess) *const __imp_TerminateProcess;
|
|||
* Terminates the specified process and all of its threads.
|
||||
* @note this wrapper takes care of ABI, STRACE(), and __winerr()
|
||||
*/
|
||||
textwindows bool32 TerminateProcess(int64_t hProcess, uint32_t uExitCode) {
|
||||
textwindows bool32 TerminateProcess(int64_t hProcess, uint32_t uWaitStatus) {
|
||||
bool32 ok;
|
||||
ok = __imp_TerminateProcess(hProcess, uExitCode);
|
||||
ok = __imp_TerminateProcess(hProcess, uWaitStatus);
|
||||
if (!ok) __winerr();
|
||||
NTTRACE("TerminateProcess(%ld, %u) → %hhhd% m", hProcess, uExitCode, ok);
|
||||
NTTRACE("TerminateProcess(%ld, %u) → %hhhd% m", hProcess, uWaitStatus, ok);
|
||||
return ok;
|
||||
}
|
||||
|
|
|
@ -74,7 +74,7 @@ static textwindows wontreturn void AbortFork(const char *func) {
|
|||
#ifdef SYSDEBUG
|
||||
kprintf("fork() %s() failed with win32 error %d\n", func, GetLastError());
|
||||
#endif
|
||||
ExitProcess(177);
|
||||
ExitProcess(11);
|
||||
}
|
||||
|
||||
static textwindows char16_t *ParseInt(char16_t *p, int64_t *x) {
|
||||
|
@ -166,7 +166,7 @@ static textwindows int OnForkCrash(struct NtExceptionPointers *ep) {
|
|||
"\tRip = %x%n",
|
||||
ep->ExceptionRecord->ExceptionCode,
|
||||
ep->ContextRecord ? ep->ContextRecord->Rip : -1);
|
||||
ExitProcess(73);
|
||||
ExitProcess(11);
|
||||
}
|
||||
|
||||
textwindows void WinMainForked(void) {
|
||||
|
@ -370,7 +370,7 @@ textwindows int sys_fork_nt(uint32_t dwCreationFlags) {
|
|||
untrackpid = -1;
|
||||
rc = pid;
|
||||
} else {
|
||||
TerminateProcess(procinfo.hProcess, 127);
|
||||
TerminateProcess(procinfo.hProcess, 9);
|
||||
CloseHandle(procinfo.hProcess);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -122,7 +122,7 @@ __msabi static textwindows int OnEarlyWinCrash(struct NtExceptionPointers *ep) {
|
|||
*p++ = '\r';
|
||||
*p++ = '\n';
|
||||
WriteFile(GetStdHandle(kNtStdErrorHandle), buf, p - buf, &wrote, 0);
|
||||
ExitProcess(200);
|
||||
ExitProcess(11);
|
||||
}
|
||||
|
||||
__msabi static textwindows void DeduplicateStdioHandles(void) {
|
||||
|
|
|
@ -49,6 +49,6 @@ textwindows dontasan void WinSockInit(void) {
|
|||
NTTRACE("WSAStartup()");
|
||||
if ((rc = WSAStartup(VERSION, &kNtWsaData)) != 0 ||
|
||||
kNtWsaData.wVersion != VERSION) {
|
||||
ExitProcess(123);
|
||||
ExitProcess(1 << 8);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*-*- 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│
|
||||
/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│
|
||||
│vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│
|
||||
╚──────────────────────────────────────────────────────────────────────────────╝
|
||||
│ │
|
||||
│ Musl Libc │
|
||||
|
@ -31,41 +31,38 @@ asm(".ident\t\"\\n\\n\
|
|||
Musl libc (MIT License)\\n\
|
||||
Copyright 2005-2014 Rich Felker, et. al.\"");
|
||||
asm(".include \"libc/disclaimer.inc\"");
|
||||
// clang-format off
|
||||
|
||||
/**
|
||||
* Compares two version strings.
|
||||
*/
|
||||
int strverscmp(const char *l0, const char *r0) {
|
||||
const unsigned char *l = (const void *)l0;
|
||||
const unsigned char *r = (const void *)r0;
|
||||
size_t i, dp, j;
|
||||
int z = 1;
|
||||
/* Find maximal matching prefix and track its maximal digit
|
||||
* suffix and whether those digits are all zeros. */
|
||||
for (dp = i = 0; l[i] == r[i]; i++) {
|
||||
int c = l[i];
|
||||
if (!c) return 0;
|
||||
if (!isdigit(c)) {
|
||||
dp = i + 1, z = 1;
|
||||
} else if (c != '0') {
|
||||
z = 0;
|
||||
}
|
||||
}
|
||||
if (l[dp] != '0' && r[dp] != '0') {
|
||||
/* If we're not looking at a digit sequence that began
|
||||
* with a zero, longest digit string is greater. */
|
||||
for (j = i; isdigit(l[j]); j++) {
|
||||
if (!isdigit(r[j])) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
if (isdigit(r[j])) {
|
||||
return -1;
|
||||
}
|
||||
} else if (z && dp < i && (isdigit(l[i]) || isdigit(r[i]))) {
|
||||
/* Otherwise, if common prefix of digit sequence is
|
||||
* all zeros, digits order less than non-digits. */
|
||||
return (unsigned char)(l[i] - '0') - (unsigned char)(r[i] - '0');
|
||||
}
|
||||
return l[i] - r[i];
|
||||
int strverscmp(const char *l0, const char *r0)
|
||||
{
|
||||
const unsigned char *l = (const void *)l0;
|
||||
const unsigned char *r = (const void *)r0;
|
||||
size_t i, dp, j;
|
||||
int z = 1;
|
||||
|
||||
/* Find maximal matching prefix and track its maximal digit
|
||||
* suffix and whether those digits are all zeros. */
|
||||
for (dp=i=0; l[i]==r[i]; i++) {
|
||||
int c = l[i];
|
||||
if (!c) return 0;
|
||||
if (!isdigit(c)) dp=i+1, z=1;
|
||||
else if (c!='0') z=0;
|
||||
}
|
||||
|
||||
if (l[dp]-'1'<9U && r[dp]-'1'<9U) {
|
||||
/* If we're looking at non-degenerate digit sequences starting
|
||||
* with nonzero digits, longest digit string is greater. */
|
||||
for (j=i; isdigit(l[j]); j++)
|
||||
if (!isdigit(r[j])) return 1;
|
||||
if (isdigit(r[j])) return -1;
|
||||
} else if (z && dp<i && (isdigit(l[i]) || isdigit(r[i]))) {
|
||||
/* Otherwise, if common prefix of digit sequence is
|
||||
* all zeros, digits order less than non-digits. */
|
||||
return (unsigned char)(l[i]-'0') - (unsigned char)(r[i]-'0');
|
||||
}
|
||||
|
||||
return l[i] - r[i];
|
||||
}
|
||||
|
|
|
@ -30,7 +30,7 @@ int wcscmp(const wchar_t *a, const wchar_t *b) {
|
|||
size_t i = 0;
|
||||
if (a == b) return 0;
|
||||
while (a[i] == b[i] && b[i]) ++i;
|
||||
return (int)a[i] < (int)b[i] ? -1 : (int)a[i] > (int)b[i];
|
||||
return a[i] < b[i] ? -1 : a[i] > b[i];
|
||||
}
|
||||
|
||||
__weak_reference(wcscmp, wcscoll);
|
||||
|
|
|
@ -31,8 +31,8 @@ int wmemcmp(const wchar_t *a, const wchar_t *b, size_t n) {
|
|||
size_t i;
|
||||
for (i = 0; i < n; ++i) {
|
||||
if (a[i] != b[i]) {
|
||||
return a[i] - b[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
return i < n ? (a[i] > b[i]) - (a[i] < b[i]) : 0;
|
||||
}
|
||||
|
|
|
@ -16,7 +16,12 @@
|
|||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/atomic.h"
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/calls/struct/sigaction.h"
|
||||
#include "libc/dce.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/sysv/consts/sig.h"
|
||||
#include "libc/testlib/subprocess.h"
|
||||
#include "libc/testlib/testlib.h"
|
||||
|
||||
|
@ -60,3 +65,52 @@ TEST(exit, test) {
|
|||
ASSERT_EQ(3, p[2]);
|
||||
ASSERT_EQ(3, p[3]);
|
||||
}
|
||||
|
||||
TEST(exit, narrowing) {
|
||||
SPAWN(vfork);
|
||||
_Exit(31337);
|
||||
EXITS(IsWindows() || IsNetbsd() || IsOpenbsd() ? 31337 : 31337 & 255);
|
||||
}
|
||||
|
||||
TEST(exit, exitCode259_wontCauseParentProcessToHangForever) {
|
||||
if (!IsWindows()) return;
|
||||
SPAWN(vfork);
|
||||
_Exit(259);
|
||||
EXITS(259);
|
||||
}
|
||||
|
||||
TEST(exit, sigkill) {
|
||||
int ws, pid;
|
||||
atomic_int *ready = _mapshared(4);
|
||||
ASSERT_NE(-1, (pid = fork()));
|
||||
if (!pid) {
|
||||
for (*ready = 1;;) {
|
||||
pause();
|
||||
}
|
||||
}
|
||||
while (!*ready) donothing;
|
||||
ASSERT_EQ(0, kill(pid, SIGKILL));
|
||||
ASSERT_SYS(0, pid, wait(&ws));
|
||||
ASSERT_EQ(SIGKILL, ws);
|
||||
munmap(ready, 4);
|
||||
}
|
||||
|
||||
// NetBSD is the only operating system that ignores SIGPWR by default
|
||||
|
||||
TEST(exit, sigalrm) {
|
||||
int ws, pid;
|
||||
sighandler_t oldint = signal(SIGALRM, SIG_DFL);
|
||||
atomic_int *ready = _mapshared(4);
|
||||
ASSERT_NE(-1, (pid = fork()));
|
||||
if (!pid) {
|
||||
for (*ready = 1;;) {
|
||||
pause();
|
||||
}
|
||||
}
|
||||
while (!*ready) donothing;
|
||||
ASSERT_EQ(0, kill(pid, SIGALRM));
|
||||
ASSERT_SYS(0, pid, wait(&ws));
|
||||
ASSERT_EQ(SIGALRM, ws);
|
||||
munmap(ready, 4);
|
||||
signal(SIGALRM, oldint);
|
||||
}
|
||||
|
|
6
third_party/python/python.mk
vendored
6
third_party/python/python.mk
vendored
|
@ -1847,7 +1847,6 @@ THIRD_PARTY_PYTHON_PYTEST_PYMAINS = \
|
|||
third_party/python/Lib/test/test_exception_variations.py \
|
||||
third_party/python/Lib/test/test_exceptions.py \
|
||||
third_party/python/Lib/test/test_extcall.py \
|
||||
third_party/python/Lib/test/test_faulthandler.py \
|
||||
third_party/python/Lib/test/test_fcntl.py \
|
||||
third_party/python/Lib/test/test_file.py \
|
||||
third_party/python/Lib/test/test_file_eintr.py \
|
||||
|
@ -2900,8 +2899,9 @@ o/$(MODE)/third_party/python/Lib/test/test_docxmlrpc.py.runs: $(PYTHONTESTER)
|
|||
o/$(MODE)/third_party/python/Lib/test/test_extcall.py.runs: $(PYTHONTESTER)
|
||||
@$(COMPILE) -ACHECK -wtT$@ $(PYHARNESSARGS) $(PYTHONTESTER) -m test.test_extcall $(PYTESTARGS)
|
||||
|
||||
o/$(MODE)/third_party/python/Lib/test/test_faulthandler.py.runs: $(PYTHONTESTER)
|
||||
@$(COMPILE) -ACHECK -wtT$@ $(PYHARNESSARGS) $(PYTHONTESTER) -m test.test_faulthandler $(PYTESTARGS)
|
||||
# too slow
|
||||
#o/$(MODE)/third_party/python/Lib/test/test_faulthandler.py.runs: $(PYTHONTESTER)
|
||||
# @$(COMPILE) -ACHECK -wtT$@ $(PYHARNESSARGS) $(PYTHONTESTER) -m test.test_faulthandler $(PYTESTARGS)
|
||||
|
||||
o/$(MODE)/third_party/python/Lib/test/test_fcntl.py.runs: $(PYTHONTESTER)
|
||||
@$(COMPILE) -ACHECK -wtT$@ $(PYHARNESSARGS) $(PYTHONTESTER) -m test.test_fcntl $(PYTESTARGS)
|
||||
|
|
Loading…
Reference in a new issue