Better Exit Code Messages

This commit is contained in:
TheBrokenRail 2022-05-15 13:51:28 -04:00
parent cb4560a602
commit cf1faf4835
8 changed files with 39 additions and 27 deletions

View File

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

View File

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

View File

@ -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

View File

@ -6,7 +6,7 @@
#include <string.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <sys/types.h>
#include <signal.h>
#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);

View File

@ -1,5 +1,4 @@
#include <pthread.h>
#include <signal.h>
#include <libreborn/exec.h>
@ -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

View File

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

View File

@ -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') {

View File

@ -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