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:
Justine Tunney 2023-07-30 14:26:21 -07:00
parent d9d5f45e2d
commit c8aa33e0e2
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
22 changed files with 259 additions and 75 deletions

View file

@ -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);
}