57 lines
1.3 KiB
C++
57 lines
1.3 KiB
C++
#include <unistd.h>
|
|
#include <cstring>
|
|
#include <cerrno>
|
|
#include <sys/wait.h>
|
|
|
|
#include "log.h"
|
|
#include "memory.h"
|
|
#include "trampoline.h"
|
|
#include "ptrace/ptrace.h"
|
|
#include "pipe/pipe.h"
|
|
#include "signals.h"
|
|
|
|
// Main
|
|
int main(__attribute__((unused)) int argc, char *argv[]) {
|
|
// Check Arguments
|
|
if (argc < 2) {
|
|
ERR("Invalid Arguments");
|
|
}
|
|
bool use_ptrace = getenv(TRAMPOLINE_USE_PIPES_ENV) == nullptr;
|
|
// Setup
|
|
init_shared_memory_common();
|
|
if (!use_ptrace) {
|
|
init_pipe_common();
|
|
}
|
|
// Fork
|
|
pid_t pid = fork();
|
|
if (pid == -1) {
|
|
ERR("Unable To Fork Process: %s", strerror(errno));
|
|
} else if (pid == 0) {
|
|
// Setup
|
|
setpgid(0, 0);
|
|
init_shared_memory_guest();
|
|
if (use_ptrace) {
|
|
init_ptrace_guest();
|
|
} else {
|
|
init_pipe_guest();
|
|
}
|
|
// Execute Program
|
|
execvp(argv[1], (char *const *) &argv[1]);
|
|
ERR("Unable To Execute Program: %s: %s", argv[1], strerror(errno));
|
|
} else {
|
|
// Parent
|
|
|
|
// Setup Trampoline
|
|
init_shared_memory_host();
|
|
init_signals(pid);
|
|
init_memory(pid);
|
|
init_trampoline();
|
|
|
|
// Start PTrace
|
|
if (use_ptrace) {
|
|
init_ptrace_host(pid);
|
|
} else {
|
|
init_pipe_host(pid);
|
|
}
|
|
}
|
|
} |