|
8 | 8 | #include <arpa/inet.h> |
9 | 9 | #include <microhttpd.h> |
10 | 10 | #include <signal.h> |
| 11 | +#include <stdint.h> |
11 | 12 | #include <stdio.h> |
12 | 13 | #include <stdlib.h> |
13 | 14 | #include <string.h> |
14 | 15 | #include <sys/socket.h> |
| 16 | +#include <sys/syscall.h> |
| 17 | +#include <sys/sysctl.h> |
15 | 18 | #include <sys/stat.h> |
16 | 19 | #include <unistd.h> |
17 | 20 |
|
@@ -46,6 +49,46 @@ static volatile sig_atomic_t resume_flag = 0; |
46 | 49 |
|
47 | 50 | void handle_sigcont(int sig) { resume_flag = 1; } |
48 | 51 |
|
| 52 | +static pid_t |
| 53 | +find_pid(const char* name) { |
| 54 | + int mib[4] = {1, 14, 8, 0}; |
| 55 | + pid_t mypid = getpid(); |
| 56 | + pid_t pid = -1; |
| 57 | + size_t buf_size; |
| 58 | + uint8_t *buf; |
| 59 | + |
| 60 | + if(sysctl(mib, 4, 0, &buf_size, 0, 0)) { |
| 61 | + pldmgr_log("[PLDMGR] sysctl failed\n"); |
| 62 | + return -1; |
| 63 | + } |
| 64 | + |
| 65 | + if(!(buf=malloc(buf_size))) { |
| 66 | + pldmgr_log("[PLDMGR] malloc failed\n"); |
| 67 | + return -1; |
| 68 | + } |
| 69 | + |
| 70 | + if(sysctl(mib, 4, buf, &buf_size, 0, 0)) { |
| 71 | + pldmgr_log("[PLDMGR] sysctl failed\n"); |
| 72 | + free(buf); |
| 73 | + return -1; |
| 74 | + } |
| 75 | + |
| 76 | + for(uint8_t *ptr=buf; ptr<(buf+buf_size);) { |
| 77 | + int ki_structsize = *(int*)ptr; |
| 78 | + pid_t ki_pid = *(pid_t*)&ptr[72]; |
| 79 | + char *ki_tdname = (char*)&ptr[447]; |
| 80 | + |
| 81 | + ptr += ki_structsize; |
| 82 | + if(!strcmp(name, ki_tdname) && ki_pid != mypid) { |
| 83 | + pid = ki_pid; |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + free(buf); |
| 88 | + |
| 89 | + return pid; |
| 90 | +} |
| 91 | + |
49 | 92 | void pldmgr_log(const char *fmt, ...) { |
50 | 93 | char line[MAX_LOG_LINE_LEN]; |
51 | 94 | va_list args; |
@@ -1149,6 +1192,18 @@ __attribute__((used)) volatile const char pldmgr_version_sig[] = "PLDMGR_VER:" M |
1149 | 1192 | int main(int argc, char *argv[]) { |
1150 | 1193 | struct MHD_Daemon *daemon; |
1151 | 1194 | unsigned short port = DEFAULT_PORT; |
| 1195 | + pid_t pid; |
| 1196 | + |
| 1197 | + syscall(SYS_thr_set_name, -1, "pldmgr.elf"); |
| 1198 | + |
| 1199 | + while((pid=find_pid("pldmgr.elf")) > 0) { |
| 1200 | + if(kill(pid, SIGKILL)) { |
| 1201 | + pldmgr_log("[PLDMGR] kill failed\n"); |
| 1202 | + return EXIT_FAILURE; |
| 1203 | + } |
| 1204 | + sleep(1); |
| 1205 | + } |
| 1206 | + |
1152 | 1207 | pldmgr_log("[PLDMGR] Starting Payload Manager v%s on port %d...\n", pldmgr_version_sig + 11, |
1153 | 1208 | port); |
1154 | 1209 |
|
|
0 commit comments