Improve Temporary File Improving

This commit is contained in:
TheBrokenRail 2022-03-24 23:03:59 -04:00
parent b5974f3f46
commit 9a4b70b5ca
2 changed files with 27 additions and 26 deletions

View File

@ -50,11 +50,6 @@ static void duplicate_mcpi_executable() {
fclose(original_file); 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 // Close New File
fclose(new_file); fclose(new_file);
close(new_file_fd); 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 into /tmp so it can be modified.
duplicate_mcpi_executable(); duplicate_mcpi_executable();
// Get Path
char *exe = getenv("MCPI_EXECUTABLE_PATH");
// Run patchelf // Run patchelf
const char *const command[] = { const char *const command[] = {
"patchelf", "patchelf",
@ -73,7 +71,7 @@ void patch_mcpi_elf_dependencies(const char *linker) {
"--remove-needed", "libX11.so.6", "--remove-needed", "libX11.so.6",
"--remove-needed", "libEGL.so", "--remove-needed", "libEGL.so",
"--replace-needed", "libGLESv2.so", "libGLESv1_CM.so.1", "--replace-needed", "libGLESv2.so", "libGLESv1_CM.so.1",
getenv("MCPI_EXECUTABLE_PATH"), exe,
NULL NULL
}; };
int return_code = 0; int return_code = 0;
@ -84,4 +82,9 @@ void patch_mcpi_elf_dependencies(const char *linker) {
if (return_code != 0) { if (return_code != 0) {
ERR("patchelf Failed: Exit Code: %i", return_code); 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));
}
} }

View File

@ -1,14 +1,16 @@
#include <unistd.h> #include <unistd.h>
#include <signal.h> #include <signal.h>
#include <errno.h>
#include "compat.h" #include "compat.h"
#include "../init/init.h" #include "../init/init.h"
#include <libreborn/libreborn.h>
#ifndef MCPI_SERVER_MODE #ifndef MCPI_SERVER_MODE
#include <SDL/SDL.h> #include <SDL/SDL.h>
#include <media-layer/core.h> #include <media-layer/core.h>
#include <libreborn/libreborn.h>
#include "../input/input.h" #include "../input/input.h"
#include "../sign/sign.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); 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 // Intercept SDL Events
HOOK(SDL_PollEvent, int, (SDL_Event *event)) { HOOK(SDL_PollEvent, int, (SDL_Event *event)) {
// In Server Mode, Exit Requests Are Handled In src/server/server.cpp // In Server Mode, Exit Requests Are Handled In src/server/server.cpp
@ -138,6 +121,21 @@ void init_compat() {
signal(SIGTERM, exit_handler); 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 // Store Exit Requests
static int exit_requested = 0; static int exit_requested = 0;
int compat_check_exit_requested() { int compat_check_exit_requested() {