diff --git a/launcher/src/client/launcher.cpp b/launcher/src/client/launcher.cpp index 07841c1..7c54550 100644 --- a/launcher/src/client/launcher.cpp +++ b/launcher/src/client/launcher.cpp @@ -98,7 +98,7 @@ static void run_command_and_set_env(const char *env_name, const char *command[]) free(output); } // Check Return Code - if (return_code != 0) { + if (!is_exit_status_success(return_code)) { INFO("Launch Interrupted"); exit(EXIT_SUCCESS); } diff --git a/launcher/src/crash-report.c b/launcher/src/crash-report.c index 7cc86e2..ecd04e5 100644 --- a/launcher/src/crash-report.c +++ b/launcher/src/crash-report.c @@ -161,19 +161,16 @@ void setup_crash_report() { untrack_child(ret); // Check If Is Crash - int is_crash = WIFEXITED(status) ? WEXITSTATUS(status) != 0 : 1; + int is_crash = !is_exit_status_success(status); // Log Exit Code To log If Crash if (is_crash) { // Create Exit Code Log Line + char *exit_status = NULL; + get_exit_status_string(status, &exit_status); char *exit_code_line = NULL; - if (WIFEXITED(status)) { - safe_asprintf(&exit_code_line, "[CRASH]: Terminated: Exit Code: %i\n", WEXITSTATUS(status)); - } else if (WIFSIGNALED(status)) { - safe_asprintf(&exit_code_line, "[CRASH]: Terminated: Signal: %i%s\n", WTERMSIG(status), WCOREDUMP(status) ? " (Core Dumped)" : ""); - } else { - safe_asprintf(&exit_code_line, "[CRASH]: Terminated\n"); - } + safe_asprintf(&exit_code_line, "[CRASH]: Terminated%s\n", exit_status); + free(exit_status); // Print Exit Code Log Line fprintf(stderr, "%s", exit_code_line); diff --git a/launcher/src/patchelf.c b/launcher/src/patchelf.c index 946c661..b60265c 100644 --- a/launcher/src/patchelf.c +++ b/launcher/src/patchelf.c @@ -103,8 +103,11 @@ void patch_mcpi_elf_dependencies(const char *linker) { } else { return_code = patch_mcpi_elf_dependencies_with_extra_patchelf_args("--set-interpreter", linker); } - if (return_code != 0) { - ERR("patchelf Failed: Exit Code: %i", return_code); + if (!is_exit_status_success(return_code)) { + char *exit_status_line = NULL; + get_exit_status_string(return_code, &exit_status_line); + ERR("patchelf Failed%s", exit_status_line); + free(exit_status_line); } // Fix Permissions diff --git a/libreborn/include/libreborn/exec.h b/libreborn/include/libreborn/exec.h index c43cd4e..d7a4c26 100644 --- a/libreborn/include/libreborn/exec.h +++ b/libreborn/include/libreborn/exec.h @@ -6,7 +6,7 @@ #include #include #include -#include +#include #include "log.h" #include "string.h" @@ -28,7 +28,11 @@ char *get_binary_directory(); __attribute__((noreturn)) void safe_execvpe_relative_to_binary(const char *const argv[], const char *const envp[]); // Run Command And Get Output -char *run_command(const char *const command[], int *return_code); +char *run_command(const char *const command[], int *exit_status); +#define is_exit_status_success(status) (WIFEXITED(status) && WEXITSTATUS(status) == 0) + +// Get Exit Status String +void get_exit_status_string(int status, char **out); // Track Children void track_child(pid_t pid); diff --git a/libreborn/src/util/exec.c b/libreborn/src/util/exec.c index 920d898..ac7e237 100644 --- a/libreborn/src/util/exec.c +++ b/libreborn/src/util/exec.c @@ -1,5 +1,4 @@ #include -#include #include @@ -61,7 +60,7 @@ __attribute__((noreturn)) void safe_execvpe_relative_to_binary(const char *const } // Run Command And Get Output -char *run_command(const char *const command[], int *return_code) { +char *run_command(const char *const command[], int *exit_status) { // Store Output int output_pipe[2]; safe_pipe2(output_pipe, 0); @@ -99,8 +98,8 @@ char *run_command(const char *const command[], int *return_code) { int status; waitpid(ret, &status, 0); untrack_child(ret); - if (return_code != NULL) { - *return_code = WIFEXITED(status) ? WEXITSTATUS(status) : EXIT_FAILURE; + if (exit_status != NULL) { + *exit_status = status; } // Return @@ -108,6 +107,19 @@ char *run_command(const char *const command[], int *return_code) { } } +// Get Exit Status String +void get_exit_status_string(int status, char **out) { + if (out != NULL) { + *out =NULL; + if (WIFEXITED(status)) { + safe_asprintf(out, ": Exit Code: %i", WEXITSTATUS(status)); + } else if (WIFSIGNALED(status)) { + safe_asprintf(out, ": Signal: %i%s", WTERMSIG(status), WCOREDUMP(status) ? " (Core Dumped)" : ""); + } else { + safe_asprintf(out, ": Terminated"); + } + } +} // Track Children #define MAX_CHILDREN 128 diff --git a/media-layer/proxy/src/server/server.cpp b/media-layer/proxy/src/server/server.cpp index c4f2824..3ece1c8 100644 --- a/media-layer/proxy/src/server/server.cpp +++ b/media-layer/proxy/src/server/server.cpp @@ -26,13 +26,9 @@ void _check_proxy_state() { // Check Client State if (!_client_is_alive) { void_write_cache(); // Child Is Dead, No Reason To Send A Dead Process Data - if (WIFEXITED(_client_status)) { - PROXY_ERR("Client Terminated: Exit Code: %i", WEXITSTATUS(_client_status)); - } else if (WIFSIGNALED(_client_status)) { - PROXY_ERR("Client Terminated: Signal: %i%s", WTERMSIG(_client_status), WCOREDUMP(_client_status) ? " (Core Dumped)" : ""); - } else { - PROXY_ERR("Client Terminated"); - } + char *exit_status = NULL; + get_exit_status_string(_client_status, &exit_status); + PROXY_ERR("Client Terminated%s", exit_status); } } diff --git a/mods/src/chat/ui.c b/mods/src/chat/ui.c index 194bdb9..bb83205 100644 --- a/mods/src/chat/ui.c +++ b/mods/src/chat/ui.c @@ -43,7 +43,7 @@ static void *chat_thread(__attribute__((unused)) void *nop) { // Handle Message if (output != NULL) { // Check Return Code - if (return_code == 0) { + if (is_exit_status_success(return_code)) { // Remove Ending Newline int length = strlen(output); if (output[length - 1] == '\n') { diff --git a/mods/src/game-mode/ui.cpp b/mods/src/game-mode/ui.cpp index 5e3b726..da483df 100644 --- a/mods/src/game-mode/ui.cpp +++ b/mods/src/game-mode/ui.cpp @@ -25,7 +25,7 @@ static char *run_command_proper(const char *command[], bool allow_empty) { // Handle Message if (output != NULL) { // Check Return Code - if (return_code == 0) { + if (is_exit_status_success(return_code)) { // Remove Ending Newline int length = strlen(output); if (output[length - 1] == '\n') { @@ -42,7 +42,7 @@ static char *run_command_proper(const char *command[], bool allow_empty) { free(output); } // Return - return return_code != 0 ? NULL : run_command_proper(command, allow_empty); + return !is_exit_status_success(return_code) ? NULL : run_command_proper(command, allow_empty); } // Track Create World State