Skip to content

Commit

Permalink
fix: description changer (#11)
Browse files Browse the repository at this point in the history
This commit fixes the code that changes the description of the module.

fixes #10
  • Loading branch information
ThePedroo authored Jun 24, 2024
1 parent 7d83bb3 commit 4aa8b2f
Showing 1 changed file with 93 additions and 73 deletions.
166 changes: 93 additions & 73 deletions loader/src/ptracer/monitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ enum TracingState {
EXITING
};

char monitor_stop_reason[32];
static char monitor_stop_reason[32];

constexpr char SOCKET_NAME[] = "init_monitor";

Expand Down Expand Up @@ -122,22 +122,8 @@ struct Status {
char *daemon_error_info;
};

static Status status64 = {
.supported = false,
.zygote_injected = false,
.daemon_running = false,
.daemon_pid = -1,
.daemon_info = NULL,
.daemon_error_info = NULL
};
static Status status32 = {
.supported = false,
.zygote_injected = false,
.daemon_running = false,
.daemon_pid = -1,
.daemon_info = NULL,
.daemon_error_info = NULL
};
static Status status64;
static Status status32;

struct SocketHandler : public EventHandler {
int sock_fd_;
Expand Down Expand Up @@ -182,7 +168,7 @@ struct SocketHandler : public EventHandler {
std::vector<uint8_t> buf;
buf.resize(sizeof(MsgHead), 0);

MsgHead &msg = *reinterpret_cast<MsgHead*>(buf.data());
MsgHead &msg = *((MsgHead *)buf.data());

ssize_t real_size;
ssize_t nread = recv(sock_fd_, &msg, sizeof(msg), MSG_PEEK);
Expand All @@ -192,7 +178,7 @@ struct SocketHandler : public EventHandler {
PLOGE("read socket");
}

if (static_cast<size_t>(nread) < sizeof(Command)) {
if ((size_t)nread < sizeof(Command)) {
LOGE("read %zu < %zu", nread, sizeof(Command));
continue;
}
Expand Down Expand Up @@ -251,7 +237,7 @@ struct SocketHandler : public EventHandler {
LOGI("stop tracing requested");

tracing_state = STOPPING;
memcpy(monitor_stop_reason, "user requested", sizeof("user requested"));
strcpy(monitor_stop_reason, "user requested");

ptrace(PTRACE_INTERRUPT, 1, 0, 0);
updateStatus();
Expand All @@ -263,7 +249,7 @@ struct SocketHandler : public EventHandler {
LOGI("prepare for exit ...");

tracing_state = EXITING;
memcpy(monitor_stop_reason, "user requested", sizeof("user requested"));
strcpy(monitor_stop_reason, "user requested");

updateStatus();
loop.Stop();
Expand All @@ -287,15 +273,32 @@ struct SocketHandler : public EventHandler {
case DAEMON64_SET_INFO: {
LOGD("received daemon64 info %s", msg.data);

status64.daemon_info = msg.data;
/* Will only happen if somehow the daemon restarts */
if (status64.daemon_info != NULL) {
free(status64.daemon_info);
status64.daemon_info = NULL;
}

status64.daemon_info = (char *)malloc(msg.length);
memcpy(status64.daemon_info, msg.data, msg.length - 1);
status64.daemon_info[msg.length - 1] = '\0';

updateStatus();

break;
}
case DAEMON32_SET_INFO: {
LOGD("received daemon32 info %s", msg.data);

status32.daemon_info = msg.data;
if (status32.daemon_info != NULL) {
free(status32.daemon_info);
status32.daemon_info = NULL;
}

status32.daemon_info = (char *)malloc(msg.length);
memcpy(status32.daemon_info, msg.data, msg.length - 1);
status32.daemon_info[msg.length - 1] = '\0';

updateStatus();

break;
Expand All @@ -304,7 +307,16 @@ struct SocketHandler : public EventHandler {
LOGD("received daemon64 error info %s", msg.data);

status64.daemon_running = false;
status64.daemon_error_info = msg.data;

if (status64.daemon_error_info != NULL) {
free(status64.daemon_error_info);
status64.daemon_error_info = NULL;
}

status64.daemon_error_info = (char *)malloc(msg.length);
memcpy(status64.daemon_error_info, msg.data, msg.length - 1);
status64.daemon_error_info[msg.length - 1] = '\0';

updateStatus();

break;
Expand All @@ -313,7 +325,16 @@ struct SocketHandler : public EventHandler {
LOGD("received daemon32 error info %s", msg.data);

status32.daemon_running = false;
status32.daemon_error_info = msg.data;

if (status32.daemon_error_info != NULL) {
free(status32.daemon_error_info);
status32.daemon_error_info = NULL;
}

status32.daemon_error_info = (char *)malloc(msg.length);
memcpy(status32.daemon_error_info, msg.data, msg.length - 1);
status32.daemon_error_info[msg.length - 1] = '\0';

updateStatus();

break;
Expand Down Expand Up @@ -364,16 +385,16 @@ CREATE_ZYGOTE_START_COUNTER(64)
CREATE_ZYGOTE_START_COUNTER(32)

static bool ensure_daemon_created(bool is_64bit) {
Status status = is_64bit ? status64 : status32;
Status *status = is_64bit ? &status64 : &status32;
if (is_64bit) {
LOGD("new zygote started, unmounting prop ...");

umount2("/data/adb/modules/zygisksu/module.prop", MNT_DETACH);
}

status.zygote_injected = false;
status->zygote_injected = false;

if (status.daemon_pid == -1) {
if (status->daemon_pid == -1) {
pid_t pid = fork();
if (pid < 0) {
PLOGE("create daemon%s", is_64bit ? "64" : "32");
Expand All @@ -389,18 +410,14 @@ static bool ensure_daemon_created(bool is_64bit) {

exit(1);
} else {
LOGI("daemon%s started with pid %d", is_64bit ? "64" : "32", pid);

status.supported = true;
status.daemon_pid = pid;
status.daemon_running = true;
status->supported = true;
status->daemon_pid = pid;
status->daemon_running = true;

return true;
}
} else {
LOGI("daemon%s already started with pid %d", is_64bit ? "64" : "32", status.daemon_pid);

return status.daemon_running;
return status->daemon_running;
}
}

Expand Down Expand Up @@ -638,51 +655,46 @@ struct SigChldHandler : public EventHandler {
};

static char pre_section[1024];
static int pre_section_len = 0;
static char post_section[1024];
static int post_section_len = 0;

#define WRITE_STATUS_ABI(suffix) \
if (status ## suffix.supported) { \
strcat(status_text, " zygote" #suffix ":"); \
\
if (tracing_state != TRACING) strcat(status_text, "❓ unknown,"); \
else if (status##suffix.zygote_injected) strcat(status_text, "😋 injected,"); \
else strcat(status_text, "❌ not injected,"); \
\
strcat(status_text, " daemon" #suffix ":"); \
if (status ## suffix.daemon_running) { \
strcat(status_text, "😋 running"); \
\
if (status ## suffix.daemon_info[0] != '\0') { \
strcat(status_text, "("); \
strcat(status_text, status ## suffix.daemon_info); \
strcat(status_text, ")"); \
} \
} else { \
strcat(status_text, "❌ crashed"); \
\
if (status ## suffix.daemon_error_info[0] != '\0') { \
strcat(status_text, "("); \
strcat(status_text, status ## suffix.daemon_error_info); \
strcat(status_text, ")"); \
} \
} \

#define WRITE_STATUS_ABI(suffix) \
if (status ## suffix.supported) { \
strcat(status_text, " zygote" # suffix ":"); \
if (tracing_state != TRACING) strcat(status_text, "❓ unknown, "); \
else if (status ## suffix.zygote_injected) strcat(status_text, "😋 injected, "); \
else strcat(status_text, "❌ not injected, "); \
\
strcat(status_text, " daemon" # suffix ":"); \
if (status ## suffix.daemon_running) { \
strcat(status_text, "😋running"); \
\
if (status ## suffix.daemon_info != NULL) { \
strcat(status_text, "("); \
strcat(status_text, status ## suffix.daemon_info); \
strcat(status_text, ")"); \
} \
} else { \
strcat(status_text, "❌ crashed "); \
\
if (status ## suffix.daemon_error_info != NULL) { \
strcat(status_text, "("); \
strcat(status_text, status ## suffix.daemon_error_info); \
strcat(status_text, ")"); \
} \
} \
}

static void updateStatus() {
FILE *prop = fopen(prop_path, "w");
char status_text[256] = "monitor: ";
char status_text[1024] = "monitor: ";

switch (tracing_state) {
case TRACING: {
strcat(status_text, "😋 tracing");

break;
}
case STOPPING: {
[[fallthrough]];
}
case STOPPING: [[fallthrough]];
case STOPPED: {
strcat(status_text, "❌ stopped");

Expand All @@ -696,11 +708,11 @@ static void updateStatus() {
}

if (tracing_state != TRACING && monitor_stop_reason[0] != '\0') {
size_t status_text_len = strlen(status_text);
snprintf(status_text + status_text_len, sizeof(status_text) - status_text_len, "(%s)", monitor_stop_reason);
strcat(status_text, "(");
strcat(status_text, monitor_stop_reason);
strcat(status_text, ")");
}

strcat(status_text, ", ");
strcat(status_text, ",");

WRITE_STATUS_ABI(64)
WRITE_STATUS_ABI(32)
Expand All @@ -725,6 +737,9 @@ static bool prepare_environment() {

const char field_name[] = "description=";

int pre_section_len = 0;
int post_section_len = 0;

/* TODO: improve this code */
int i = 1;
while (1) {
Expand Down Expand Up @@ -804,6 +819,11 @@ void init_monitor() {
looper.RegisterHandler(ptraceHandler, EPOLLIN | EPOLLET);
looper.Loop();

if (status64.daemon_info != NULL) free(status64.daemon_info);
if (status64.daemon_error_info != NULL) free(status64.daemon_error_info);
if (status32.daemon_info != NULL) free(status32.daemon_info);
if (status32.daemon_error_info != NULL) free(status32.daemon_error_info);

LOGI("exit");
}

Expand Down

0 comments on commit 4aa8b2f

Please sign in to comment.