Skip to content

Commit

Permalink
improve: CLI and code (#3)
Browse files Browse the repository at this point in the history
This commit improves the CLI of ReZygisk, allowing the use of important information like PID of the daemons. Also improves the code of the loaders ptracer.
  • Loading branch information
ThePedroo authored Jun 21, 2024
1 parent 39788a9 commit 678d886
Show file tree
Hide file tree
Showing 13 changed files with 1,109 additions and 725 deletions.
33 changes: 30 additions & 3 deletions loader/src/common/daemon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ namespace zygiskd {

int Connect(uint8_t retry) {
int fd = socket(PF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
struct sockaddr_un addr{
.sun_family = AF_UNIX,
.sun_path={0},
struct sockaddr_un addr = {
.sun_family = AF_UNIX,
.sun_path = { 0 },
};

auto socket_path = TMP_PATH + kCPSocketName;
strcpy(addr.sun_path, socket_path.c_str());
socklen_t socklen = sizeof(addr);
Expand Down Expand Up @@ -67,6 +68,7 @@ namespace zygiskd {
}
socket_utils::write_u8(fd, (uint8_t) SocketAction::GetProcessFlags);
socket_utils::write_u32(fd, uid);

return socket_utils::read_u32(fd);
}

Expand Down Expand Up @@ -139,4 +141,29 @@ namespace zygiskd {
}
}
}

void GetInfo(struct zygote_info *info) {
/* TODO: Optimize and avoid re-connect twice here */
int fd = Connect(1);

if (fd != -1) {
info->running = true;

socket_utils::write_u8(fd, (uint8_t) SocketAction::GetInfo);

int flags = socket_utils::read_u32(fd);

if (flags & (1 << 29)) {
info->root_impl = ZYGOTE_ROOT_IMPL_KERNELSU;
} else if (flags & (1 << 30)) {
info->root_impl = ZYGOTE_ROOT_IMPL_MAGISK;
} else {
info->root_impl = ZYGOTE_ROOT_IMPL_NONE;
}

info->pid = socket_utils::read_u32(fd);

close(fd);
} else info->running = false;
}
}
15 changes: 15 additions & 0 deletions loader/src/include/daemon.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,18 @@ class UniqueFd {
Fd fd_ = -1;
};

enum zygote_root_impl {
ZYGOTE_ROOT_IMPL_NONE,
ZYGOTE_ROOT_IMPL_KERNELSU,
ZYGOTE_ROOT_IMPL_MAGISK
};

struct zygote_info {
enum zygote_root_impl root_impl;
pid_t pid;
bool running;
};

namespace zygiskd {

struct Module {
Expand All @@ -55,6 +67,7 @@ namespace zygiskd {
PingHeartBeat,
RequestLogcatFd,
GetProcessFlags,
GetInfo,
ReadModules,
RequestCompanionSocket,
GetModuleDir,
Expand All @@ -81,4 +94,6 @@ namespace zygiskd {
void ZygoteRestart();

void SystemServerStarted();

void GetInfo(struct zygote_info *info);
}
122 changes: 105 additions & 17 deletions loader/src/ptracer/main.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,60 @@
#include "main.h"
#include <stdio.h>

#include "monitor.h"
#include "utils.hpp"
#include "daemon.h"

#define CUSTOM_TMP_PATH 0
#define SBIN_AS_TMP_PATH 1
#define DEBUG_RAMDISK_AS_TMP_PATH 2

int main(int argc, char **argv) {
zygiskd::Init(getenv("TMP_PATH"));
int tmp_path_type = CUSTOM_TMP_PATH;

if (getenv("TMP_PATH") == NULL) {
tmp_path_type = SBIN_AS_TMP_PATH;

FILE *fp = fopen("/sbin", "r");
if (fp == NULL) {
tmp_path_type = DEBUG_RAMDISK_AS_TMP_PATH;

fp = fopen("/debug_ramdisk", "r");

if (fp == NULL) {
printf("Cannot find TMP_PATH. You should make an issue about that.\n");

return 1;
} else fclose(fp);
} else fclose(fp);
} else {
tmp_path_type = CUSTOM_TMP_PATH;
}

switch (tmp_path_type) {
case CUSTOM_TMP_PATH: {
zygiskd::Init(getenv("TMP_PATH"));

break;
}
case SBIN_AS_TMP_PATH: {
zygiskd::Init("/sbin");

break;
}
case DEBUG_RAMDISK_AS_TMP_PATH: {
zygiskd::Init("/debug_ramdisk");

break;
}
}

printf("The ReZygisk Tracer %s\n\n", ZKSU_VERSION);

if (argc >= 2 && strcmp(argv[1], "monitor") == 0) {
init_monitor();

printf("[ReZygisk]: Started monitoring...\n");

return 0;
} else if (argc >= 3 && strcmp(argv[1], "trace") == 0) {
if (argc >= 4 && strcmp(argv[3], "--restart") == 0) zygiskd::ZygoteRestart();
Expand All @@ -18,35 +66,75 @@ int main(int argc, char **argv) {
return 1;
}

printf("[ReZygisk]: Tracing %ld...\n", pid);

return 0;
} else if (argc >= 2 && strcmp(argv[1], "ctl") == 0) {
if (argc == 3) {
if (strcmp(argv[2], "start") == 0) {
send_control_command(START);
enum Command command;

return 0;
} else if (strcmp(argv[2], "stop") == 0) {
send_control_command(STOP);
if (strcmp(argv[2], "start") == 0) command = START;
else if (strcmp(argv[2], "stop") == 0) command = STOP;
else if (strcmp(argv[2], "exit") == 0) command = EXIT;
else {
printf("[ReZygisk]: Usage: %s ctl <start|stop|exit>\n", argv[0]);

return 0;
} else if (strcmp(argv[2], "exit") == 0) {
send_control_command(EXIT);
return 1;
}

return 0;
}
if (send_control_command(command) == -1) {
printf("[ReZygisk]: Failed to send the command, is the daemon running?\n");

return 1;
}

printf("ReZygisk Tracer %s\n", ZKSU_VERSION);
printf("Usage: %s ctl start|stop|exit\n", argv[0]);
printf("[ReZygisk]: command sent\n");

return 1;
return 0;
} else if (argc >= 2 && strcmp(argv[1], "version") == 0) {
printf("ReZygisk Tracer %s\n", ZKSU_VERSION);

return 0;
} else if (argc >= 2 && strcmp(argv[1], "info") == 0) {
printf("ReZygisk Tracer %s\n", ZKSU_VERSION);

struct zygote_info info;
zygiskd::GetInfo(&info);

printf("Daemon process PID: %d\n", info.pid);

switch (info.root_impl) {
case ZYGOTE_ROOT_IMPL_NONE: {
printf("Root implementation: none\n");

break;
}
case ZYGOTE_ROOT_IMPL_KERNELSU: {
printf("Root implementation: KernelSU\n");

break;
}
case ZYGOTE_ROOT_IMPL_MAGISK: {
printf("Root implementation: Magisk\n");

break;
}
}

printf("Is the daemon running: %s\n", info.running ? "yes" : "no");

return 0;
} else {
printf("ReZygisk Tracer %s\n", ZKSU_VERSION);
printf("usage: %s monitor | trace <pid> | ctl <start|stop|exit> | version\n", argv[0]);
printf(
"Available commands:\n"
" - monitor\n"
" - trace <pid> [--restart]\n"
" - ctl <start|stop|exit>\n"
" - version: Shows the version of ReZygisk.\n"
" - info: Shows information about the created daemon/injection.\n"
"\n"
"<...>: Obligatory\n"
"[...]: Optional\n");

return 1;
}
Expand Down
Loading

0 comments on commit 678d886

Please sign in to comment.