From e6839074fd2eec91c5bb2e08705f2f235a568f74 Mon Sep 17 00:00:00 2001 From: TheBrokenRail Date: Sat, 8 Jun 2024 16:28:47 -0400 Subject: [PATCH] Tweaks --- native/CMakeLists.txt | 2 +- native/src/{pipe.cpp => loop.cpp} | 20 ++++---------------- native/src/loop.h | 5 +++++ native/src/main.cpp | 20 ++++++++++++++++---- native/src/memory.cpp | 2 +- native/src/pipe.h | 7 ------- 6 files changed, 27 insertions(+), 29 deletions(-) rename native/src/{pipe.cpp => loop.cpp} (81%) create mode 100644 native/src/loop.h delete mode 100644 native/src/pipe.h diff --git a/native/CMakeLists.txt b/native/CMakeLists.txt index 12c0e96..06e0748 100644 --- a/native/CMakeLists.txt +++ b/native/CMakeLists.txt @@ -5,7 +5,7 @@ add_executable(runtime src/memory.cpp src/main.cpp src/trampoline.cpp - src/pipe.cpp + src/loop.cpp src/signals.cpp ) diff --git a/native/src/pipe.cpp b/native/src/loop.cpp similarity index 81% rename from native/src/pipe.cpp rename to native/src/loop.cpp index 055867f..8a63e4f 100644 --- a/native/src/pipe.cpp +++ b/native/src/loop.cpp @@ -2,11 +2,10 @@ #include #include #include -#include #include -#include "pipe.h" +#include "loop.h" #include "log.h" #include "trampoline.h" @@ -23,7 +22,7 @@ static void safe_pipe(int *out) { ERR("Unable To Create Pipe: %s", strerror(errno)); } } -void init_pipe_common() { +void create_pipes() { safe_pipe(arguments_pipe); safe_pipe(return_value_pipe); } @@ -39,7 +38,7 @@ void init_pipe_guest() { } // Host -void init_pipe_host(pid_t guest_pid) { +void pipe_read_loop() { // Close Unneeded Pipes close(arguments_pipe[PIPE_WRITE]); close(return_value_pipe[PIPE_READ]); @@ -62,15 +61,4 @@ void init_pipe_host(pid_t guest_pid) { write(return_value_pipe[PIPE_WRITE], &ret, sizeof(uint32_t)); } } - // Reap Child - int status; - if (waitpid(guest_pid, &status, 0) == -1) { - ERR("Unable To Reap Child: %s", strerror(errno)); - } - // Exit - if (WIFEXITED(status)) { - exit(WEXITSTATUS(status)); - } else { - ERR("Unable To Determine Exit Code"); - } -} \ No newline at end of file +} diff --git a/native/src/loop.h b/native/src/loop.h new file mode 100644 index 0000000..c312928 --- /dev/null +++ b/native/src/loop.h @@ -0,0 +1,5 @@ +#pragma once + +void create_pipes(); +void pipe_read_loop(); +void init_pipe_guest(); diff --git a/native/src/main.cpp b/native/src/main.cpp index 6b4f331..3946f32 100644 --- a/native/src/main.cpp +++ b/native/src/main.cpp @@ -6,7 +6,7 @@ #include "log.h" #include "memory.h" -#include "pipe.h" +#include "loop.h" #include "signals.h" // Main @@ -16,7 +16,7 @@ int main(__attribute__((unused)) int argc, char *argv[]) { ERR("Invalid Arguments"); } // Setup - init_pipe_common(); + create_pipes(); // Fork pid_t pid = fork(); if (pid == -1) { @@ -37,7 +37,19 @@ int main(__attribute__((unused)) int argc, char *argv[]) { init_signals(pid); init_memory(pid); - // Start Pipes - init_pipe_host(pid); + // Read From Pipes + pipe_read_loop(); + + // Reap Child + int status; + if (waitpid(pid, &status, 0) == -1) { + ERR("Unable To Reap Child: %s", strerror(errno)); + } + // Exit + if (WIFEXITED(status)) { + exit(WEXITSTATUS(status)); + } else { + ERR("Unusual Exit"); + } } } \ No newline at end of file diff --git a/native/src/memory.cpp b/native/src/memory.cpp index 0b1805c..e991f04 100644 --- a/native/src/memory.cpp +++ b/native/src/memory.cpp @@ -19,6 +19,6 @@ void memory_writer(uint32_t guest_addr, const void *data, uint32_t size) { remote[0].iov_len = size; const ssize_t ret = process_vm_writev(guest_pid, local, 1, remote, 1, 0); if (ret != size) { - ERR("Unable To Write Data: %s", strerror(errno)); + ERR("Unable To Write Data: %#10x: %s", guest_addr, strerror(errno)); } } \ No newline at end of file diff --git a/native/src/pipe.h b/native/src/pipe.h deleted file mode 100644 index 6bb962e..0000000 --- a/native/src/pipe.h +++ /dev/null @@ -1,7 +0,0 @@ -#pragma once - -#include - -void init_pipe_common(); -void init_pipe_host(pid_t guest_pid); -void init_pipe_guest();