Add Crash Report Dialog

This commit is contained in:
TheBrokenRail 2022-05-13 22:36:12 -04:00
parent b59c580f6a
commit 77d7b82a14
20 changed files with 360 additions and 57 deletions

@ -1 +1 @@
Subproject commit 3dbcdbb34a0c92297155de48ed491ea3e587b208
Subproject commit 775bea8911f27848d1359ae656cf074dc0a07396

View File

@ -2,7 +2,7 @@ project(launcher)
# Launcher
if(BUILD_NATIVE_COMPONENTS)
add_executable(launcher src/bootstrap.c src/patchelf.c)
add_executable(launcher src/bootstrap.c src/patchelf.c src/crash-report.c)
if(MCPI_SERVER_MODE)
target_sources(launcher PRIVATE src/server/launcher.c)
else()

View File

@ -12,6 +12,7 @@
#include "bootstrap.h"
#include "patchelf.h"
#include "crash-report.h"
// Set Environmental Variable
#define PRESERVE_ENVIRONMENTAL_VARIABLE(name) \
@ -139,6 +140,14 @@ static void load(char **ld_preload, char *folder) {
#define MCPI_BINARY "minecraft-pi"
#define QEMU_BINARY "qemu-arm"
// Exit Handler
static void exit_handler(__attribute__((unused)) int signal_id) {
// Pass Signal To Child
murder_children();
while (wait(NULL) > 0) {}
_exit(EXIT_SUCCESS);
}
// Pre-Bootstrap
void pre_bootstrap() {
// GTK Dark Mode
@ -148,9 +157,11 @@ void pre_bootstrap() {
// AppImage
#ifdef MCPI_IS_APPIMAGE_BUILD
char *owd = getenv("OWD");
if (owd != NULL && chdir(owd) != 0) {
ERR("AppImage: Unable To Fix Current Directory: %s", strerror(errno));
{
char *owd = getenv("OWD");
if (owd != NULL && chdir(owd) != 0) {
ERR("AppImage: Unable To Fix Current Directory: %s", strerror(errno));
}
}
#endif
@ -176,6 +187,21 @@ void pre_bootstrap() {
// Free Binary Directory
free(binary_directory);
// Setup Crash Reports
setup_crash_report();
// Install Signal Handlers
struct sigaction act_sigint;
memset((void *) &act_sigint, 0, sizeof (struct sigaction));
act_sigint.sa_flags = SA_RESTART;
act_sigint.sa_handler = &exit_handler;
sigaction(SIGINT, &act_sigint, NULL);
struct sigaction act_sigterm;
memset((void *) &act_sigterm, 0, sizeof (struct sigaction));
act_sigterm.sa_flags = SA_RESTART;
act_sigterm.sa_handler = &exit_handler;
sigaction(SIGTERM, &act_sigterm, NULL);
}
// Bootstrap

View File

@ -99,7 +99,8 @@ static void run_command_and_set_env(const char *env_name, const char *command[])
}
// Check Return Code
if (return_code != 0) {
ERR("Launch Interrupted");
INFO("Launch Interrupted");
exit(EXIT_SUCCESS);
}
}
}

213
launcher/src/crash-report.c Normal file
View File

@ -0,0 +1,213 @@
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <signal.h>
#include <poll.h>
#include <libreborn/libreborn.h>
#include "crash-report.h"
// Show Crash Report Dialog
#ifndef MCPI_HEADLESS_MODE
#define DIALOG_TITLE "Crash Report"
#define CRASH_REPORT_DIALOG_WIDTH "640"
#define CRASH_REPORT_DIALOG_HEIGHT "480"
static void show_report(const char *log_filename) {
const char *command[] = {
"zenity",
"--title", DIALOG_TITLE,
"--class", GUI_TITLE,
"--width", CRASH_REPORT_DIALOG_WIDTH,
"--height", CRASH_REPORT_DIALOG_HEIGHT,
"--text-info",
"--text", "Minecraft: Pi Edition: Reborn has crashed!",
"--filename", log_filename,
"--no-wrap",
"--font", "Monospace",
NULL
};
free(run_command(command, NULL));
}
#endif
// Exit Handler
static void exit_handler(__attribute__((unused)) int signal) {
// Murder
murder_children();
}
// Setup
void setup_crash_report() {
// Store Output
#ifndef MCPI_HEADLESS_MODE
int output_pipe[2];
safe_pipe2(output_pipe, 0);
int error_pipe[2];
safe_pipe2(error_pipe, 0);
#endif
// Fork
pid_t ret = fork();
if (ret == -1) {
ERR("Unable To Fork: %s", strerror(errno));
} else if (ret == 0) {
// Child Process
// Pipe stdio
#ifndef MCPI_HEADLESS_MODE
dup2(output_pipe[1], STDOUT_FILENO);
close(output_pipe[0]);
close(output_pipe[1]);
dup2(error_pipe[1], STDERR_FILENO);
close(error_pipe[0]);
close(error_pipe[1]);
#endif
// Create New Process Group
setpgid(0, 0);
// Continue Execution
} else {
// Parent Process
track_child(ret);
// Install Signal Handlers
struct sigaction act_sigint;
memset((void *) &act_sigint, 0, sizeof (struct sigaction));
act_sigint.sa_flags = SA_RESTART;
act_sigint.sa_handler = &exit_handler;
sigaction(SIGINT, &act_sigint, NULL);
struct sigaction act_sigterm;
memset((void *) &act_sigterm, 0, sizeof (struct sigaction));
act_sigterm.sa_flags = SA_RESTART;
act_sigterm.sa_handler = &exit_handler;
sigaction(SIGTERM, &act_sigterm, NULL);
// Capture stdio
#ifndef MCPI_HEADLESS_MODE
// Close Unneeded File Descriptors
close(output_pipe[1]);
close(error_pipe[1]);
// Create A Buffer
#define BUFFER_SIZE 1024
char buf[BUFFER_SIZE];
// Create Temporary File
char log_filename[] = "/tmp/.minecraft-pi-log-XXXXXX";
int log_file_fd = mkstemp(log_filename);
if (log_file_fd == -1) {
ERR("Unable To Create Log File: %s", strerror(errno));
}
// Setup Polling
int number_fds = 2;
struct pollfd poll_fds[number_fds];
poll_fds[0].fd = output_pipe[0];
poll_fds[1].fd = error_pipe[0];
for (int i = 0; i < number_fds; i++) {
poll_fds[i].events = POLLIN;
}
// Poll Data
int number_open_fds = number_fds;
while (number_open_fds > 0) {
int poll_ret = poll(poll_fds, number_fds, -1);
if (poll_ret == -1) {
if (errno == EINTR) {
continue;
} else {
ERR("Unable To Poll Data: %s", strerror(errno));
}
}
// Handle Data
for (int i = 0; i < number_fds; i++) {
if (poll_fds[i].revents != 0) {
if (poll_fds[i].revents & POLLIN) {
// Data Available
ssize_t bytes_read = read(poll_fds[i].fd, (void *) buf, BUFFER_SIZE - 1 /* Account For NULL-Terminator */);
if (bytes_read == -1) {
ERR("Unable To Read Log Data: %s", strerror(errno));
}
// Print To Terminal
buf[bytes_read] = '\0';
fprintf(i == 0 ? stdout : stderr, "%s", buf);
// Write To log
if (write(log_file_fd, (void *) buf, bytes_read) == -1) {
ERR("Unable To Write Log Data: %s", strerror(errno));
}
} else {
// File Descriptor No Longer Accessible
if (close(poll_fds[i].fd) == -1) {
ERR("Unable To Close File Descriptor: %s", strerror(errno));
}
number_open_fds--;
}
}
}
}
#endif
// Get Return Code
int status;
waitpid(ret, &status, 0);
untrack_child(ret);
// Check If Is Crash
int is_crash = WIFEXITED(status) ? WEXITSTATUS(status) != 0 : 1;
// Log Exit Code To log If Crash
if (is_crash) {
// Create Exit Code Log Line
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");
}
// Print Exit Code Log Line
fprintf(stderr, "%s", exit_code_line);
// Write Exit Code Log Line
#ifndef MCPI_HEADLESS_MODE
if (write(log_file_fd, (void *) exit_code_line, strlen(exit_code_line)) == -1) {
ERR("Unable To Write Exit Code To Log: %s", strerror(errno));
}
#endif
// Free Exit Code Log Line
free(exit_code_line);
}
// Show Crash Log
#ifndef MCPI_HEADLESS_MODE
// Close Log File FD
if (close(log_file_fd) == -1) {
ERR("Unable To Close Log File Descriptor: %s", strerror(errno));
}
// Show Report
if (is_crash) {
show_report(log_filename);
}
// Delete Log File
if (unlink(log_filename) == -1) {
ERR("Unable To Delete Log File: %s", strerror(errno));
}
#endif
// Exit
exit(WIFEXITED(status) ? WEXITSTATUS(status) : EXIT_FAILURE);
}
}

View File

@ -0,0 +1,11 @@
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
void setup_crash_report();
#ifdef __cplusplus
}
#endif

View File

@ -8,7 +8,7 @@
#include "patchelf.h"
// Duplicate MCPI Executable Into /tmp
#define TMP_DIR "/tmp/.minecraft-pi-tmp"
#define TMP_DIR "/tmp/.minecraft-pi-patched"
static void duplicate_mcpi_executable() {
// Get Original Path
const char *original_path = getenv("MCPI_EXECUTABLE_PATH");

View File

@ -1,11 +1,11 @@
project(libreborn)
add_library(reborn-util STATIC src/util/elf.c src/util/exec.c src/util/string.c src/util/util.c src/util/log.c)
add_library(reborn-util STATIC src/util/elf.c src/util/exec.c src/util/string.c src/util/util.c)
target_include_directories(reborn-util PUBLIC include)
if(BUILD_ARM_COMPONENTS)
add_library(reborn-patch SHARED src/patch/patch.c)
target_link_libraries(reborn-patch dl reborn-util)
target_link_libraries(reborn-patch dl pthread reborn-util)
target_compile_definitions(reborn-patch PUBLIC -DREBORN_HAS_PATCH_CODE)
# Install
install(TARGETS reborn-patch DESTINATION "${MCPI_LIB_DIR}")

View File

@ -6,6 +6,7 @@
#include <string.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <sys/types.h>
#include "log.h"
#include "string.h"
@ -29,6 +30,11 @@ __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);
// Track Children
void track_child(pid_t pid);
void untrack_child(pid_t pid);
void murder_children();
#ifdef __cplusplus
}
#endif

View File

@ -3,23 +3,9 @@
#include <stdio.h>
#include <stdlib.h>
#ifdef __cplusplus
extern "C" {
#endif
// Colors
char *color_reset();
char *color_yellow();
char *color_faint();
char *color_red();
// Logging
#define INFO(format, ...) { fprintf(stderr, "[INFO]: " format "\n", ##__VA_ARGS__); }
#define WARN(format, ...) { fprintf(stderr, "%s[WARN]: " format "%s\n", color_yellow(), ##__VA_ARGS__, color_reset()); }
#define DEBUG(format, ...) { const char *debug = getenv("MCPI_DEBUG"); if (debug != NULL && strlen(debug) > 0) { fprintf(stderr, "%s[DEBUG]: " format "%s\n", color_faint(), ##__VA_ARGS__, color_reset()); } }
#define ERR(format, ...) { fprintf(stderr, "%s[ERR]: (%s:%i): " format "%s\n", color_red(), __FILE__, __LINE__, ##__VA_ARGS__, color_reset()); exit(EXIT_FAILURE); }
#define WARN(format, ...) { fprintf(stderr, "[WARN]: " format "\n", ##__VA_ARGS__); }
#define DEBUG(format, ...) { const char *debug = getenv("MCPI_DEBUG"); if (debug != NULL && strlen(debug) > 0) { fprintf(stderr, "[DEBUG]: " format "\n", ##__VA_ARGS__); } }
#define ERR(format, ...) { fprintf(stderr, "[ERR]: (%s:%i): " format "\n", __FILE__, __LINE__, ##__VA_ARGS__); exit(EXIT_FAILURE); }
#define IMPOSSIBLE() ERR("This Should Never Be Called")
#ifdef __cplusplus
}
#endif

View File

@ -96,7 +96,9 @@ void _overwrite_call(const char *file, int line, void *start, void *target) {
// Add New Target To Code Block
update_code_block(target);
uint32_t new_instruction = generate_bl_instruction(start, code_block, 0);
// Patch
int use_b_instruction = ((unsigned char *) start)[3] == B_INSTRUCTION;
uint32_t new_instruction = generate_bl_instruction(start, code_block, use_b_instruction);
_patch(file, line, start, (unsigned char *) &new_instruction);
// Increment Code Block Position

View File

@ -1,3 +1,6 @@
#include <pthread.h>
#include <signal.h>
#include <libreborn/exec.h>
// Safe execvpe()
@ -86,13 +89,14 @@ char *run_command(const char *const command[], int *return_code) {
safe_execvpe(command, (const char *const *) environ);
} else {
// Parent Process
track_child(ret);
// Read stdout
close(output_pipe[1]);
char *output = NULL;
#define BUFFER_SIZE 1024
char buf[BUFFER_SIZE];
size_t bytes_read = 0;
ssize_t bytes_read = 0;
while ((bytes_read = read(output_pipe[0], (void *) buf, BUFFER_SIZE - 1 /* Account For NULL-Terminator */)) > 0) {
buf[bytes_read] = '\0';
string_append(&output, "%s", buf);
@ -102,6 +106,7 @@ char *run_command(const char *const command[], int *return_code) {
// Get Return Code
int status;
waitpid(ret, &status, 0);
untrack_child(ret);
if (return_code != NULL) {
*return_code = WIFEXITED(status) ? WEXITSTATUS(status) : EXIT_FAILURE;
}
@ -110,3 +115,37 @@ char *run_command(const char *const command[], int *return_code) {
return output;
}
}
// Track Children
#define MAX_CHILDREN 128
static pid_t children[MAX_CHILDREN] = { 0 };
static pthread_mutex_t children_lock = PTHREAD_MUTEX_INITIALIZER;
void track_child(pid_t pid) {
pthread_mutex_lock(&children_lock);
for (int i = 0; i < MAX_CHILDREN; i++) {
if (children[i] == 0) {
children[i] = pid;
break;
}
}
pthread_mutex_unlock(&children_lock);
}
void untrack_child(pid_t pid) {
pthread_mutex_lock(&children_lock);
for (int i = 0; i < MAX_CHILDREN; i++) {
if (children[i] == pid) {
children[i] = 0;
}
}
pthread_mutex_unlock(&children_lock);
}
void murder_children() {
pthread_mutex_lock(&children_lock);
for (int i = 0; i < MAX_CHILDREN; i++) {
if (children[i] != 0) {
kill(children[i], SIGTERM);
}
}
pthread_mutex_unlock(&children_lock);
}

View File

@ -1,16 +0,0 @@
#include <stdio.h>
#include <unistd.h>
#define COLOR(name, value) \
char *color_##name() { \
static char *out = NULL; \
if (out == NULL) { \
out = isatty(fileno(stderr)) ? "\x1b[" value "m" : ""; \
} \
return out; \
}
COLOR(reset, "0")
COLOR(yellow, "93")
COLOR(faint, "2")
COLOR(red, "91")

View File

@ -273,18 +273,20 @@ void SDL_WM_SetCaption(const char *title, __attribute__((unused)) const char *ic
glfwWindowHint(GLFW_AUTO_ICONIFY, GLFW_FALSE);
glfwWindowHint(GLFW_ALPHA_BITS, 0); // Fix Transparent Window On Wayland
// Create Window
glfw_window = glfwCreateWindow(DEFAULT_WIDTH, DEFAULT_HEIGHT, title, NULL, NULL);
if (!glfw_window) {
ERR("Unable To Create GLFW Window");
}
// Don't Process Events In Headless Mode
// Event Handlers
glfwSetKeyCallback(glfw_window, glfw_key);
glfwSetCharCallback(glfw_window, glfw_char);
glfwSetCursorPosCallback(glfw_window, glfw_motion);
glfwSetMouseButtonCallback(glfw_window, glfw_click);
glfwSetScrollCallback(glfw_window, glfw_scroll);
// Make Window Context Current
glfwMakeContextCurrent(glfw_window);
// Init OpenAL

View File

@ -28,14 +28,15 @@ int SDL_GetWMInfo(SDL_SysWMinfo *info) {
}
// Quit
__attribute__ ((noreturn)) void SDL_Quit() {
void SDL_Quit() {
// Cleanup Media Layer
media_cleanup();
// Wait For Children To Stop
signal(SIGCHLD, SIG_IGN);
murder_children();
while (wait(NULL) > 0) {}
// Exit
INFO("Stopped");
exit(EXIT_SUCCESS);
}

View File

@ -32,7 +32,7 @@ int SDL_ShowCursor(int toggle);
void *SDL_SetVideoMode(int width, int height, int bpp, uint32_t flags);
int SDL_GetWMInfo(SDL_SysWMinfo *info);
__attribute__((noreturn)) void SDL_Quit();
void SDL_Quit();
#ifdef __cplusplus
}

View File

@ -4,6 +4,7 @@
#include <cstring>
#include <sys/prctl.h>
#include <csignal>
#include <exception>
#include "../common/common.h"
@ -37,15 +38,29 @@ void _check_proxy_state() {
}
}
// Exit Handler
static volatile int exit_requested = 0;
static void exit_handler(__attribute__((unused)) int signal_id) {
// Request Exit
exit_requested = 1;
}
// Main
int main(int argc, char *argv[]) {
// Ignore SIGINT, Send Signal To Parent
signal(SIGINT, SIG_IGN);
// Install Signal Handlers
struct sigaction act_sigint;
memset((void *) &act_sigint, 0, sizeof (struct sigaction));
act_sigint.sa_handler = &exit_handler;
sigaction(SIGINT, &act_sigint, NULL);
struct sigaction act_sigterm;
memset((void *) &act_sigterm, 0, sizeof (struct sigaction));
act_sigterm.sa_handler = &exit_handler;
sigaction(SIGTERM, &act_sigterm, NULL);
// Send Signal On Parent Death To Interrupt Connection Read/Write And Exit
prctl(PR_SET_PDEATHSIG, SIGUSR1);
struct sigaction sa;
sigemptyset(&sa.sa_mask);
memset((void *) &sa, 0, sizeof (struct sigaction));
sa.sa_flags = SA_NOCLDSTOP;
sa.sa_handler = &sigusr1_handler;
if (sigaction(SIGUSR1, &sa, NULL) == -1) {
@ -67,7 +82,7 @@ int main(int argc, char *argv[]) {
// Loop
int running = is_connection_open();
while (running) {
while (running && !exit_requested) {
unsigned char unique_id = read_byte();
if (get_handlers().size() > unique_id && get_handlers()[unique_id] != NULL) {
// Run Method
@ -84,6 +99,9 @@ int main(int argc, char *argv[]) {
PROXY_ERR("Invalid Method ID: %i", (int) unique_id);
}
}
if (is_connection_open()) {
close_connection();
}
// Exit
PROXY_INFO("Stopped");

View File

@ -214,16 +214,21 @@ CALL(9, media_cleanup, void, ()) {
if (is_connection_open()) {
// Lock Proxy
start_proxy_call();
// Block Until Cleanup Is Complete
flush_write_cache();
read_byte();
// Close The Connection
close_connection();
// Release Proxy
end_proxy_call();
}
#else
// Close The Connection
close_connection();
// Run
media_cleanup();
// Confirm Cleanup
write_byte(0);
// Close The Connection
close_connection();
#endif
}

View File

@ -84,6 +84,7 @@ static void start_media_layer_proxy_client(int read, int write) {
} else {
// Parent Process
_client_pid = ret;
track_child(_client_pid);
}
update_client_state(1, 0);
}

View File

@ -116,9 +116,17 @@ static void exit_handler(__attribute__((unused)) int data) {
compat_request_exit();
}
void init_compat() {
// Install Exit Handler
signal(SIGINT, exit_handler);
signal(SIGTERM, exit_handler);
// Install Signal Handlers
struct sigaction act_sigint;
memset((void *) &act_sigint, 0, sizeof (struct sigaction));
act_sigint.sa_flags = SA_RESTART;
act_sigint.sa_handler = &exit_handler;
sigaction(SIGINT, &act_sigint, NULL);
struct sigaction act_sigterm;
memset((void *) &act_sigterm, 0, sizeof (struct sigaction));
act_sigterm.sa_flags = SA_RESTART;
act_sigterm.sa_handler = &exit_handler;
sigaction(SIGTERM, &act_sigterm, NULL);
}
// Cleanup Temporary Files