This commit is contained in:
TheBrokenRail 2024-06-08 16:28:47 -04:00
parent 5751a5a0c1
commit e6839074fd
6 changed files with 27 additions and 29 deletions

View File

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

View File

@ -2,11 +2,10 @@
#include <cerrno>
#include <cstring>
#include <string>
#include <sys/wait.h>
#include <trampoline/types.h>
#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");
}
}
}

5
native/src/loop.h Normal file
View File

@ -0,0 +1,5 @@
#pragma once
void create_pipes();
void pipe_read_loop();
void init_pipe_guest();

View File

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

View File

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

View File

@ -1,7 +0,0 @@
#pragma once
#include <sys/types.h>
void init_pipe_common();
void init_pipe_host(pid_t guest_pid);
void init_pipe_guest();