From 9a4b70b5ca543964558491681e2747b1053016ed Mon Sep 17 00:00:00 2001 From: TheBrokenRail Date: Thu, 24 Mar 2022 23:03:59 -0400 Subject: [PATCH] Improve Temporary File Improving --- launcher/src/patchelf.c | 15 +++++++++------ mods/src/compat/compat.c | 38 ++++++++++++++++++-------------------- 2 files changed, 27 insertions(+), 26 deletions(-) diff --git a/launcher/src/patchelf.c b/launcher/src/patchelf.c index 8b64b328b..54dd1d167 100644 --- a/launcher/src/patchelf.c +++ b/launcher/src/patchelf.c @@ -50,11 +50,6 @@ static void duplicate_mcpi_executable() { fclose(original_file); } - // Fix Permissions - if (fchmod(new_file_fd, S_IRUSR | S_IWUSR | S_IXUSR) != 0) { - ERR("Unable To Set File Permissions: %s: %s", new_path, strerror(errno)); - } - // Close New File fclose(new_file); close(new_file_fd); @@ -65,6 +60,9 @@ void patch_mcpi_elf_dependencies(const char *linker) { // Duplicate MCPI executable into /tmp so it can be modified. duplicate_mcpi_executable(); + // Get Path + char *exe = getenv("MCPI_EXECUTABLE_PATH"); + // Run patchelf const char *const command[] = { "patchelf", @@ -73,7 +71,7 @@ void patch_mcpi_elf_dependencies(const char *linker) { "--remove-needed", "libX11.so.6", "--remove-needed", "libEGL.so", "--replace-needed", "libGLESv2.so", "libGLESv1_CM.so.1", - getenv("MCPI_EXECUTABLE_PATH"), + exe, NULL }; int return_code = 0; @@ -84,4 +82,9 @@ void patch_mcpi_elf_dependencies(const char *linker) { if (return_code != 0) { ERR("patchelf Failed: Exit Code: %i", return_code); } + + // Fix Permissions + if (chmod(exe, S_IRUSR | S_IXUSR) != 0) { + ERR("Unable To Set File Permissions: %s: %s", exe, strerror(errno)); + } } diff --git a/mods/src/compat/compat.c b/mods/src/compat/compat.c index 31ec8e37e..1f75fffd9 100644 --- a/mods/src/compat/compat.c +++ b/mods/src/compat/compat.c @@ -1,14 +1,16 @@ #include #include +#include #include "compat.h" #include "../init/init.h" +#include + #ifndef MCPI_SERVER_MODE #include #include -#include #include "../input/input.h" #include "../sign/sign.h" @@ -28,25 +30,6 @@ HOOK(SDL_ShowCursor, int, (int toggle)) { return (*real_SDL_ShowCursor)(toggle == SDL_QUERY ? SDL_QUERY : SDL_DISABLE); } -// Hook SDL_Quit -HOOK(SDL_Quit, __attribute__((noreturn)) void, ()) { - // Cleanup Executable - { - const char *exe = getenv("MCPI_EXECUTABLE_PATH"); - // Check If Executable Is Temporary - if (exe != NULL && starts_with(exe, "/tmp")) { - // Cleanup Temporary File - if (unlink(exe) != 0) { - ERR("Unable To Cleanup Temporary File: %s", strerror(errno)); - } - } - } - - // Call Original Method - ensure_SDL_Quit(); - (*real_SDL_Quit)(); -} - // Intercept SDL Events HOOK(SDL_PollEvent, int, (SDL_Event *event)) { // In Server Mode, Exit Requests Are Handled In src/server/server.cpp @@ -138,6 +121,21 @@ void init_compat() { signal(SIGTERM, exit_handler); } +// Cleanup Temporary Files +__attribute__((destructor)) static void cleanup_temporary() { + // Cleanup Executable + { + const char *exe = getenv("MCPI_EXECUTABLE_PATH"); + // Check If Executable Is Temporary + if (exe != NULL && starts_with(exe, "/tmp")) { + // Cleanup Temporary File + if (unlink(exe) != 0) { + ERR("Unable To Cleanup Temporary File: %s", strerror(errno)); + } + } + } +} + // Store Exit Requests static int exit_requested = 0; int compat_check_exit_requested() {