diff --git a/Makefile b/Makefile index d0a2561..71b24b3 100644 --- a/Makefile +++ b/Makefile @@ -24,7 +24,7 @@ PS5_PORT ?= 9020 CC := $(PS5_PAYLOAD_SDK)/host/x86_64-ps5-payload-cc LD := $(PS5_PAYLOAD_SDK)/host/x86_64-ps5-payload-ld -CFLAGS := -O2 -Wall -Werror +CFLAGS := -g -Wall -Werror all: bootstrap.elf diff --git a/elfldr.c b/elfldr.c index bcfeeb0..30f2f6a 100644 --- a/elfldr.c +++ b/elfldr.c @@ -456,7 +456,7 @@ elfldr_spawn(uint8_t *elf) { return -1; } - if(pt_continue(pid)) { + if(pt_continue(pid, SIGCONT)) { perror("[elfldr.elf] pt_continue"); pt_detach(pid); return -1; diff --git a/main.c b/main.c index d9ab2d5..9c4af32 100644 --- a/main.c +++ b/main.c @@ -18,6 +18,7 @@ along with this program; see the file COPYING. If not, see #include #include #include +#include #include #include #include @@ -27,10 +28,12 @@ along with this program; see the file COPYING. If not, see #include #include +#include #include #include "elfldr.h" +#include "pt.h" /** @@ -99,6 +102,45 @@ readsock(int fd) { } +/** + * + **/ +static void* +serve_thread(void* args) { + int fd = (int)(long)args; + uint8_t* elf; + int status; + pid_t pid; + + if(!(elf=readsock(fd))) { + close(fd); + return 0; + } + + if(memcmp(elf, "\x7f\x45\x4c\x46", 4)) { + close(fd); + free(elf); + return 0; + } + + if((pid=elfldr_spawn(elf)) < 0) { + close(fd); + free(elf); + return 0; + } + + free(elf); + + waitpid(pid, &status, WTRAPPED | WUNTRACED); + if(WIFSTOPPED(status)) { + printf("PID %d received the fatal POSIX signal %d\n", pid, WSTOPSIG(status)); + pt_continue(pid, SIGKILL); + } + + return 0; +} + + /** * **/ @@ -110,7 +152,7 @@ serve_elfldr(uint16_t port) { struct ifaddrs *ifaddr; int ifaddr_wait = 1; socklen_t socklen; - uint8_t* elf; + pthread_t trd; int connfd; int srvfd; @@ -143,7 +185,7 @@ serve_elfldr(uint16_t port) { } ifaddr_wait = 0; - notify("Serving ELF loader on %s:%d (%s)\n", ip, port, ifa->ifa_name); + notify("Serving ELF loader on %s:%d (%s)", ip, port, ifa->ifa_name); } freeifaddrs(ifaddr); @@ -183,19 +225,7 @@ serve_elfldr(uint16_t port) { perror("[elfldr.elf] accept"); break; } - - if(!(elf=readsock(connfd))) { - close(connfd); - continue; - } - - if(!memcmp(elf, "\x7f\x45\x4c\x46", 4)) { - elfldr_spawn(elf); - } else { - write(connfd, "Not an ELF file\n", 16); - } - free(elf); - close(connfd); + pthread_create(&trd, NULL, serve_thread, (void*)(long)connfd); } return close(srvfd); diff --git a/pt.c b/pt.c index 88c86ed..97564ad 100644 --- a/pt.c +++ b/pt.c @@ -118,8 +118,8 @@ pt_step(int pid) { int -pt_continue(pid_t pid) { - if(sys_ptrace(PT_CONTINUE, pid, (caddr_t)1, SIGCONT) == -1) { +pt_continue(pid_t pid, int sig) { + if(sys_ptrace(PT_CONTINUE, pid, (caddr_t)1, sig) == -1) { return -1; } diff --git a/pt.h b/pt.h index 37df5f0..138c102 100644 --- a/pt.h +++ b/pt.h @@ -24,7 +24,7 @@ along with this program; see the file COPYING. If not, see int pt_attach(pid_t pid); int pt_detach(pid_t pid); int pt_step(pid_t pid); -int pt_continue(pid_t pid); +int pt_continue(pid_t pid, int sig); int pt_getregs(pid_t pid, struct reg *r); int pt_setregs(pid_t pid, const struct reg *r);