runtime/native/src/main.cpp

57 lines
1.3 KiB
C++
Raw Normal View History

2024-06-04 17:22:15 -04:00
#include <unistd.h>
#include <cstring>
#include <cerrno>
#include <sys/wait.h>
#include "log.h"
#include "memory.h"
#include "trampoline.h"
2024-06-05 21:18:48 -04:00
#include "ptrace/ptrace.h"
#include "pipe/pipe.h"
#include "signals.h"
2024-06-04 17:22:15 -04:00
// Main
int main(__attribute__((unused)) int argc, char *argv[]) {
2024-06-05 21:18:48 -04:00
// Check Arguments
if (argc < 2) {
ERR("Invalid Arguments");
}
2024-06-08 03:09:44 -04:00
bool use_ptrace = getenv(TRAMPOLINE_USE_PIPES_ENV) == nullptr;
2024-06-05 21:18:48 -04:00
// Setup
2024-06-08 03:09:44 -04:00
init_shared_memory_common();
2024-06-05 21:18:48 -04:00
if (!use_ptrace) {
init_pipe_common();
}
2024-06-04 17:22:15 -04:00
// Fork
pid_t pid = fork();
if (pid == -1) {
ERR("Unable To Fork Process: %s", strerror(errno));
} else if (pid == 0) {
2024-06-05 21:18:48 -04:00
// Setup
setpgid(0, 0);
2024-06-08 03:09:44 -04:00
init_shared_memory_guest();
2024-06-05 21:18:48 -04:00
if (use_ptrace) {
init_ptrace_guest();
} else {
init_pipe_guest();
2024-06-05 00:11:54 -04:00
}
// Execute Program
2024-06-04 17:22:15 -04:00
execvp(argv[1], (char *const *) &argv[1]);
ERR("Unable To Execute Program: %s: %s", argv[1], strerror(errno));
} else {
// Parent
// Setup Trampoline
2024-06-08 03:09:44 -04:00
init_shared_memory_host();
2024-06-05 21:18:48 -04:00
init_signals(pid);
2024-06-04 17:22:15 -04:00
init_memory(pid);
init_trampoline();
2024-06-05 21:18:48 -04:00
// Start PTrace
if (use_ptrace) {
init_ptrace_host(pid);
} else {
init_pipe_host(pid);
}
2024-06-04 17:22:15 -04:00
}
}