From b9b697f2cb9d0e6c9af177a0b55b23e5b636b008 Mon Sep 17 00:00:00 2001 From: Jonas 'Sortie' Termansen Date: Sun, 9 Sep 2012 12:21:39 +0200 Subject: [PATCH] Implement proper W* error codes in sys/wait.h and kernel. Note that signals can't really kill a process at this time. --- sortix/include/sortix/wait.h | 12 ++++++++++++ sortix/process.cpp | 4 ++-- utils/chvideomode.cpp | 3 +-- utils/init.cpp | 2 +- utils/mxsh.cpp | 15 ++++++++++----- 5 files changed, 26 insertions(+), 10 deletions(-) diff --git a/sortix/include/sortix/wait.h b/sortix/include/sortix/wait.h index ca0dd3c3..00711aff 100644 --- a/sortix/include/sortix/wait.h +++ b/sortix/include/sortix/wait.h @@ -31,6 +31,18 @@ __BEGIN_DECLS #define WNOHANG (1<<0) +#define WEXITSTATUS(status) ((status >> 8) & 0xFF) +#define WTERMSIG(status) ((status >> 0) & 0x7F) +#define WSTOPSIG(status) WTERMSIG(status) +#define WIFEXITED(status) (WTERMSIG(status) == 0) +#define WIFSIGNALED(status) (WTERMSIG(status) != 0) +/*#define WIFCONTINUED(status) (WTERMSIG(status) == TODO)*/ +/*#define WIFSTOPPED(status) (WTERMSIG(status) == TODO)*/ +/*#define WIFCONTINUED(status) (WTERMSIG(status) == TODO)*/ + +#define W_EXITCODE(ret, sig) ((ret) << 8 | (sig)) +#define W_STOPCODE(sig) ((sig) << 8 | 0x7f) + __END_DECLS #endif diff --git a/sortix/process.cpp b/sortix/process.cpp index 6c8f68a4..6593096b 100644 --- a/sortix/process.cpp +++ b/sortix/process.cpp @@ -374,7 +374,7 @@ namespace Sortix int exitstatus = zombie->exitstatus; if ( exitstatus < 0 ) - exitstatus = 0; + exitstatus = W_EXITCODE(128 + SIGKILL, SIGKILL); // TODO: Validate that status is a valid user-space int! if ( status ) @@ -396,7 +396,7 @@ namespace Sortix ScopedLock lock(&threadlock); // Status codes can only contain 8 bits according to ISO C and POSIX. if ( exitstatus == -1 ) - exitstatus = status % 256; + exitstatus = W_EXITCODE(status & 0xFF, 0); // Broadcast SIGKILL to all our threads which will begin our long path // of process termination. We simply can't stop the threads as they may diff --git a/utils/chvideomode.cpp b/utils/chvideomode.cpp index c1aa0cd9..dfb20e68 100644 --- a/utils/chvideomode.cpp +++ b/utils/chvideomode.cpp @@ -365,14 +365,13 @@ Try make sure the desired driver is loaded and is configured correctly.\n"); if ( childpid < 0 ) { perror("fork"); exit(1); } if ( childpid ) { - // TODO: Use the right WEXITSTATUS-ish macros here! int status; waitpid(childpid, &status, 0); if ( !SetCurrentMode(prevmode) ) { error(1, errno, "Unable to restore video mode: %s", prevmode); } - exit(status); + exit(WEXITSTATUS(status)); } execvp(argv[1], argv + 1); perror(argv[1]); diff --git a/utils/init.cpp b/utils/init.cpp index 77fed09b..0a199790 100644 --- a/utils/init.cpp +++ b/utils/init.cpp @@ -50,7 +50,7 @@ int runsystem() int status; waitpid(childpid, &status, 0); // TODO: Use the proper macro! - if ( 128 <= status ) + if ( 128 <= WEXITSTATUS(status) || WIFSIGNALED(status) ) { printf("Looks like the system crashed, trying to bring it back up.\n"); return runsystem(); diff --git a/utils/mxsh.cpp b/utils/mxsh.cpp index 4648cd21..0be0d9f6 100644 --- a/utils/mxsh.cpp +++ b/utils/mxsh.cpp @@ -173,21 +173,26 @@ readcmd: } status = internalresult; - if ( !internal && waitpid(childpid, &status, 0) < 0 ) + int exitstatus; + if ( !internal && waitpid(childpid, &exitstatus, 0) < 0 ) { perror("waitpid"); return 127; } + // TODO: HACK: Most signals can't kill processes yet. + if ( WEXITSTATUS(exitstatus) == 128 + SIGINT ) + printf("^C\n"); + if ( WTERMSIG(status) == SIGKILL ) + printf("Killed\n"); + + status = WEXITSTATUS(exitstatus); + if ( strcmp(execmode, ";") == 0 && tokens[cmdnext] && !lastcmd ) { goto readcmd; } - // TODO: Hack, use the right macros! - if ( status == 128 + SIGINT ) - printf("^C\n"); - result = status; goto out; }