You've already forked ReZygisk
mirror of
https://github.com/PerformanC/ReZygisk.git
synced 2025-09-06 06:37:01 +00:00
update: some loader/ code to C
This commit changes some code from "loader" folder to use C keywords and not C++ only keywords.
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,6 +1,7 @@
|
||||
.gradle
|
||||
.idea
|
||||
.cxx
|
||||
.vscode
|
||||
build
|
||||
local.properties
|
||||
Cargo.lock
|
||||
|
||||
@@ -1,55 +1,53 @@
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <string_view>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <time.h>
|
||||
#include <sys/system_properties.h>
|
||||
|
||||
#include "main.hpp"
|
||||
#include "main.h"
|
||||
#include "utils.hpp"
|
||||
#include "daemon.h"
|
||||
#include <sys/mount.h>
|
||||
|
||||
using namespace std::string_view_literals;
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
zygiskd::Init(getenv("TMP_PATH"));
|
||||
if (argc >= 2 && argv[1] == "monitor"sv) {
|
||||
init_monitor();
|
||||
return 0;
|
||||
} else if (argc >= 3 && argv[1] == "trace"sv) {
|
||||
if (argc >= 4 && argv[3] == "--restart"sv) {
|
||||
zygiskd::ZygoteRestart();
|
||||
}
|
||||
auto pid = strtol(argv[2], 0, 0);
|
||||
if (!trace_zygote(pid)) {
|
||||
kill(pid, SIGKILL);
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
} else if (argc >= 2 && argv[1] == "ctl"sv) {
|
||||
if (argc == 3) {
|
||||
if (argv[2] == "start"sv) {
|
||||
send_control_command(START);
|
||||
return 0;
|
||||
} else if (argv[2] == "stop"sv) {
|
||||
send_control_command(STOP);
|
||||
return 0;
|
||||
} else if (argv[2] == "exit"sv) {
|
||||
send_control_command(EXIT);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
printf("ReZygisk Tracer %s\n", ZKSU_VERSION);
|
||||
printf("Usage: %s ctl start|stop|exit\n", argv[0]);
|
||||
zygiskd::Init(getenv("TMP_PATH"));
|
||||
if (argc >= 2 && strcmp(argv[1], "monitor") == 0) {
|
||||
init_monitor();
|
||||
|
||||
return 0;
|
||||
} else if (argc >= 3 && strcmp(argv[1], "trace") == 0) {
|
||||
if (argc >= 4 && strcmp(argv[3], "--restart") == 0) zygiskd::ZygoteRestart();
|
||||
|
||||
long pid = strtol(argv[2], 0, 0);
|
||||
if (!trace_zygote(pid)) {
|
||||
kill(pid, SIGKILL);
|
||||
|
||||
return 1;
|
||||
} else if (argc >= 2 && argv[1] == "version"sv) {
|
||||
printf("ReZygisk Tracer %s\n", ZKSU_VERSION);
|
||||
}
|
||||
|
||||
return 0;
|
||||
} else if (argc >= 2 && strcmp(argv[1], "ctl") == 0) {
|
||||
if (argc == 3) {
|
||||
if (strcmp(argv[2], "start") == 0) {
|
||||
send_control_command(START);
|
||||
|
||||
return 0;
|
||||
} else {
|
||||
printf("ReZygisk Tracer %s\n", ZKSU_VERSION);
|
||||
printf("usage: %s monitor | trace <pid> | ctl <start|stop|exit> | version\n", argv[0]);
|
||||
return 1;
|
||||
} else if (strcmp(argv[2], "stop") == 0) {
|
||||
send_control_command(STOP);
|
||||
|
||||
return 0;
|
||||
} else if (strcmp(argv[2], "exit") == 0) {
|
||||
send_control_command(EXIT);
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
printf("ReZygisk Tracer %s\n", ZKSU_VERSION);
|
||||
printf("Usage: %s ctl start|stop|exit\n", argv[0]);
|
||||
|
||||
return 1;
|
||||
} else if (argc >= 2 && strcmp(argv[1], "version") == 0) {
|
||||
printf("ReZygisk Tracer %s\n", ZKSU_VERSION);
|
||||
|
||||
return 0;
|
||||
} else {
|
||||
printf("ReZygisk Tracer %s\n", ZKSU_VERSION);
|
||||
printf("usage: %s monitor | trace <pid> | ctl <start|stop|exit> | version\n", argv[0]);
|
||||
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
26
loader/src/ptracer/main.h
Normal file
26
loader/src/ptracer/main.h
Normal file
@@ -0,0 +1,26 @@
|
||||
#ifndef MAIN_HPP
|
||||
#define MAIN_HPP
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
void init_monitor();
|
||||
bool trace_zygote(int pid);
|
||||
|
||||
enum Command {
|
||||
START = 1,
|
||||
STOP = 2,
|
||||
EXIT = 3,
|
||||
|
||||
/* sent from daemon */
|
||||
ZYGOTE64_INJECTED = 4,
|
||||
ZYGOTE32_INJECTED = 5,
|
||||
DAEMON64_SET_INFO = 6,
|
||||
DAEMON32_SET_INFO = 7,
|
||||
DAEMON64_SET_ERROR_INFO = 8,
|
||||
DAEMON32_SET_ERROR_INFO = 9,
|
||||
SYSTEM_SERVER_STARTED = 10
|
||||
};
|
||||
|
||||
void send_control_command(enum Command cmd);
|
||||
|
||||
#endif /* MAIN_HPP */
|
||||
@@ -1,21 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
|
||||
void init_monitor();
|
||||
bool trace_zygote(int pid);
|
||||
|
||||
enum Command {
|
||||
START = 1,
|
||||
STOP = 2,
|
||||
EXIT = 3,
|
||||
// sent from daemon
|
||||
ZYGOTE64_INJECTED = 4,
|
||||
ZYGOTE32_INJECTED = 5,
|
||||
DAEMON64_SET_INFO = 6,
|
||||
DAEMON32_SET_INFO = 7,
|
||||
DAEMON64_SET_ERROR_INFO = 8,
|
||||
DAEMON32_SET_ERROR_INFO = 9,
|
||||
SYSTEM_SERVER_STARTED = 10
|
||||
};
|
||||
|
||||
void send_control_command(Command cmd);
|
||||
File diff suppressed because it is too large
Load Diff
@@ -161,7 +161,10 @@ bool inject_on_main(int pid, const char *lib_path) {
|
||||
|
||||
return true;
|
||||
} else {
|
||||
LOGE("stopped by other reason: %s", parse_status(status).c_str());
|
||||
char status_str[64];
|
||||
parse_status(status, status_str, sizeof(status_str));
|
||||
|
||||
LOGE("stopped by other reason: %s", status_str);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -205,12 +208,18 @@ bool trace_zygote(int pid) {
|
||||
ptrace(PTRACE_DETACH, pid, 0, SIGCONT);
|
||||
}
|
||||
} else {
|
||||
LOGE("unknown state %s, not SIGTRAP + EVENT_STOP", parse_status(status).c_str());
|
||||
char status_str[64];
|
||||
parse_status(status, status_str, sizeof(status_str));
|
||||
|
||||
LOGE("unknown state %s, not SIGTRAP + EVENT_STOP", status_str);
|
||||
ptrace(PTRACE_DETACH, pid, 0, 0);
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
LOGE("unknown state %s, not SIGSTOP + EVENT_STOP", parse_status(status).c_str());
|
||||
char status_str[64];
|
||||
parse_status(status, status_str, sizeof(status_str));
|
||||
|
||||
LOGE("unknown state %s, not SIGSTOP + EVENT_STOP", status_str);
|
||||
ptrace(PTRACE_DETACH, pid, 0, 0);
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -359,7 +359,10 @@ uintptr_t remote_call(int pid, struct user_regs_struct ®s, uintptr_t func_add
|
||||
}
|
||||
return regs.REG_RET;
|
||||
} else {
|
||||
LOGE("stopped by other reason %s at addr %p", parse_status(status).c_str(), (void*) regs.REG_IP);
|
||||
char status_str[64];
|
||||
parse_status(status, status_str, sizeof(status_str));
|
||||
|
||||
LOGE("stopped by other reason %s at addr %p", status_str, (void*) regs.REG_IP);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -394,42 +397,45 @@ void wait_for_trace(int pid, int* status, int flags) {
|
||||
}
|
||||
}
|
||||
if (!WIFSTOPPED(*status)) {
|
||||
LOGE("process %d not stopped for trace: %s, exit", pid, parse_status(*status).c_str());
|
||||
char status_str[64];
|
||||
parse_status(*status, status_str, sizeof(status_str));
|
||||
|
||||
LOGE("process %d not stopped for trace: %s, exit", pid, status_str);
|
||||
exit(1);
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
std::string parse_status(int status) {
|
||||
std::ostringstream os;
|
||||
os << "0x" << std::hex << status << std::dec << " ";
|
||||
if (WIFEXITED(status)) {
|
||||
os << "exited with " << WEXITSTATUS(status);
|
||||
} else if (WIFSIGNALED(status)) {
|
||||
os << "signaled with " << sigabbrev_np(WTERMSIG(status)) << "(" << WTERMSIG(status) << ")";
|
||||
} else if (WIFSTOPPED(status)) {
|
||||
os << "stopped by ";
|
||||
auto stop_sig = WSTOPSIG(status);
|
||||
os << "signal=" << sigabbrev_np(stop_sig) << "(" << stop_sig << "),";
|
||||
os << "event=" << parse_ptrace_event(status);
|
||||
} else {
|
||||
os << "unknown";
|
||||
}
|
||||
return os.str();
|
||||
void parse_status(int status, char *buf, size_t len) {
|
||||
snprintf(buf, len, "0x%x ", status);
|
||||
if (WIFEXITED(status)) {
|
||||
snprintf(buf + strlen(buf), len - strlen(buf), "exited with %d", WEXITSTATUS(status));
|
||||
} else if (WIFSIGNALED(status)) {
|
||||
snprintf(buf + strlen(buf), len - strlen(buf), "signaled with %s(%d)", sigabbrev_np(WTERMSIG(status)), WTERMSIG(status));
|
||||
} else if (WIFSTOPPED(status)) {
|
||||
snprintf(buf + strlen(buf), len - strlen(buf), "stopped by ");
|
||||
auto stop_sig = WSTOPSIG(status);
|
||||
snprintf(buf + strlen(buf), len - strlen(buf), "signal=%s(%d),", sigabbrev_np(stop_sig), stop_sig);
|
||||
snprintf(buf + strlen(buf), len - strlen(buf), "event=%s", parse_ptrace_event(status));
|
||||
} else {
|
||||
snprintf(buf + strlen(buf), len - strlen(buf), "unknown");
|
||||
}
|
||||
}
|
||||
|
||||
std::string get_program(int pid) {
|
||||
std::string path = "/proc/";
|
||||
path += std::to_string(pid);
|
||||
path += "/exe";
|
||||
constexpr const auto SIZE = 256;
|
||||
char buf[SIZE + 1];
|
||||
auto sz = readlink(path.c_str(), buf, SIZE);
|
||||
if (sz == -1) {
|
||||
PLOGE("readlink /proc/%d/exe", pid);
|
||||
return "";
|
||||
}
|
||||
buf[sz] = 0;
|
||||
return buf;
|
||||
int get_program(int pid, char *buf, size_t size) {
|
||||
char path[64];
|
||||
snprintf(path, sizeof(path), "/proc/%d/exe", pid);
|
||||
|
||||
ssize_t sz = readlink(path, buf, size);
|
||||
|
||||
if (sz == -1) {
|
||||
PLOGE("readlink /proc/%d/exe", pid);
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
buf[sz] = 0;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -89,7 +89,7 @@ int fork_dont_care();
|
||||
|
||||
void wait_for_trace(int pid, int* status, int flags);
|
||||
|
||||
std::string parse_status(int status);
|
||||
void parse_status(int status, char *buf, size_t len);
|
||||
|
||||
#define WPTEVENT(x) (x >> 16)
|
||||
|
||||
@@ -116,7 +116,7 @@ inline const char* sigabbrev_np(int sig) {
|
||||
return "(unknown)";
|
||||
}
|
||||
|
||||
std::string get_program(int pid);
|
||||
int get_program(int pid, char *buf, size_t size);
|
||||
void *find_module_return_addr(std::vector<MapInfo> &info, std::string_view suffix);
|
||||
|
||||
// pid = 0, fd != nullptr -> set to fd
|
||||
|
||||
Reference in New Issue
Block a user