Loop for every for logging

This commit is contained in:
topjohnwu 2017-06-03 04:31:01 +08:00
parent b9eab39541
commit c282a8f328
6 changed files with 90 additions and 75 deletions

View File

@ -9,6 +9,7 @@
#include <limits.h> #include <limits.h>
#include <pthread.h> #include <pthread.h>
#include <unistd.h> #include <unistd.h>
#include <sys/wait.h>
#include "magisk.h" #include "magisk.h"
#include "utils.h" #include "utils.h"
@ -23,17 +24,26 @@ static void *logger_thread(void *args) {
FILE *logfile = xfdopen(xopen(LOGFILE, O_WRONLY | O_CREAT | O_CLOEXEC | O_TRUNC, 0644), "w"); FILE *logfile = xfdopen(xopen(LOGFILE, O_WRONLY | O_CREAT | O_CLOEXEC | O_TRUNC, 0644), "w");
// Disable buffering // Disable buffering
setbuf(logfile, NULL); setbuf(logfile, NULL);
// Start logcat int fd, log_pid;
char *const command[] = { "logcat", "-s", "Magisk", "-v", "time", NULL };
int fd; while (1) {
run_command(&fd, "/system/bin/logcat", command); // Start logcat
while (fdgets(buffer, PATH_MAX, fd)) { char *const command[] = { "logcat", "-s", "Magisk", "-v", "time", NULL };
fprintf(logfile, "%s", buffer); log_pid = run_command(&fd, "/system/bin/logcat", command);
while (fdgets(buffer, PATH_MAX, fd)) {
fprintf(logfile, "%s", buffer);
}
// For some reason it went here, clear buffer and restart
close(fd);
kill(log_pid, SIGTERM);
waitpid(log_pid, NULL, 0);
system("logcat -c");
} }
// Should never be here, but well... // Should never be here, but well...
free(buffer);
close(fd);
fclose(logfile); fclose(logfile);
free(buffer);
return NULL; return NULL;
} }

View File

@ -32,6 +32,7 @@
#define MOUNTPOINT "/magisk" #define MOUNTPOINT "/magisk"
#define COREDIR MOUNTPOINT "/.core" #define COREDIR MOUNTPOINT "/.core"
#define HOSTSFILE COREDIR "/hosts" #define HOSTSFILE COREDIR "/hosts"
#define HIDELIST COREDIR "/hidelist"
#define MAINIMG "/data/magisk.img" #define MAINIMG "/data/magisk.img"
#define DATABIN "/data/magisk" #define DATABIN "/data/magisk"
#define MANAGERAPK DATABIN "/magisk.apk" #define MANAGERAPK DATABIN "/magisk.apk"

View File

@ -113,7 +113,7 @@ int hide_daemon() {
// Unmount loop mounts // Unmount loop mounts
vec_for_each_r(&mount_list, line) { vec_for_each_r(&mount_list, line) {
if (strstr(line, "/dev/block/loop") && !strstr(line, DUMMYPATH)) { if (strstr(line, "/dev/block/loop") && !strstr(line, DUMMDIR)) {
sscanf(line, "%*s %512s", buffer); sscanf(line, "%*s %512s", buffer);
lazy_unmount(buffer); lazy_unmount(buffer);
} }

View File

@ -3,9 +3,6 @@
#include <pthread.h> #include <pthread.h>
#define HIDELIST "/magisk/.core/magiskhide/hidelist"
#define DUMMYPATH "/dev/magisk/dummy"
// Kill process // Kill process
void kill_proc(int pid); void kill_proc(int pid);

View File

@ -101,75 +101,82 @@ void proc_monitor() {
break; break;
} }
// Monitor am_proc_start while (1) {
system("logcat -b events -c"); // Clear previous buffer
char *const command[] = { "logcat", "-b", "events", "-v", "raw", "-s", "am_proc_start", NULL }; system("logcat -b events -c");
log_pid = run_command(&log_fd, "/system/bin/logcat", command);
while(fdgets(buffer, PATH_MAX, log_fd)) { // Monitor am_proc_start
int ret, comma = 0; char *const command[] = { "logcat", "-b", "events", "-v", "raw", "-s", "am_proc_start", NULL };
char *pos = buffer, *line, processName[256]; log_pid = run_command(&log_fd, "/system/bin/logcat", command);
while(1) { while(fdgets(buffer, PATH_MAX, log_fd)) {
pos = strchr(pos, ','); int ret, comma = 0;
if(pos == NULL) char *pos = buffer, *line, processName[256];
break;
pos[0] = ' ';
++comma;
}
if (comma == 6) while(1) {
ret = sscanf(buffer, "[%*d %d %*d %*d %256s", &pid, processName); pos = strchr(pos, ',');
else if(pos == NULL)
ret = sscanf(buffer, "[%*d %d %*d %256s", &pid, processName); break;
pos[0] = ' ';
++comma;
}
if(ret != 2) if (comma == 6)
continue; ret = sscanf(buffer, "[%*d %d %*d %*d %256s", &pid, processName);
else
ret = sscanf(buffer, "[%*d %d %*d %256s", &pid, processName);
ret = 0; if(ret != 2)
continue;
// Critical region ret = 0;
pthread_mutex_lock(&hide_lock);
vec_for_each(hide_list, line) { // Critical region
if (strcmp(processName, line) == 0) { pthread_mutex_lock(&hide_lock);
while(1) { vec_for_each(hide_list, line) {
ret = 1; if (strcmp(processName, line) == 0) {
for (int i = 0; i < zygote_num; ++i) { while(1) {
read_namespace(pid, buffer, 32); ret = 1;
if (strcmp(buffer, zygote_ns[i]) == 0) { for (int i = 0; i < zygote_num; ++i) {
usleep(50); read_namespace(pid, buffer, 32);
ret = 0; if (strcmp(buffer, zygote_ns[i]) == 0) {
break; usleep(50);
ret = 0;
break;
}
} }
if (ret) break;
} }
if (ret) break;
ret = 0;
// Send pause signal ASAP
if (kill(pid, SIGSTOP) == -1) continue;
LOGI("proc_monitor: %s (PID=%d ns=%s)\n", processName, pid, buffer);
// Unmount start
xwrite(sv[0], &pid, sizeof(pid));
// Get the hide daemon return code
xxread(sv[0], &ret, sizeof(ret));
LOGD("proc_monitor: hide daemon response code: %d\n", ret);
break;
} }
}
pthread_mutex_unlock(&hide_lock);
ret = 0; if (ret) {
// Wait hide process to kill itself
// Send pause signal ASAP waitpid(hide_pid, NULL, 0);
if (kill(pid, SIGSTOP) == -1) continue; quit_pthread(SIGUSR1);
LOGI("proc_monitor: %s (PID=%d ns=%s)\n", processName, pid, buffer);
// Unmount start
xwrite(sv[0], &pid, sizeof(pid));
// Get the hide daemon return code
xxread(sv[0], &ret, sizeof(ret));
LOGD("proc_monitor: hide daemon response code: %d\n", ret);
break;
} }
} }
pthread_mutex_unlock(&hide_lock);
if (ret) { // For some reason it went here, restart logging
// Wait hide process to kill itself kill(log_pid, SIGTERM);
waitpid(hide_pid, NULL, 0); waitpid(log_pid, NULL, 0);
quit_pthread(SIGUSR1); close(log_fd);
} log_pid = 0;
} }
// Should never be here
quit_pthread(SIGUSR1);
} }

View File

@ -67,7 +67,7 @@ int file_to_vector(const char* filename, struct vector *v) {
size_t len = 0; size_t len = 0;
ssize_t read; ssize_t read;
FILE *fp = xfopen(filename, "r"); FILE *fp = fopen(filename, "r");
if (fp == NULL) if (fp == NULL)
return 1; return 1;
@ -105,16 +105,16 @@ int isNum(const char *s) {
/* Read a whole line from file descriptor */ /* Read a whole line from file descriptor */
ssize_t fdgets(char *buf, const size_t size, int fd) { ssize_t fdgets(char *buf, const size_t size, int fd) {
ssize_t read = 0; ssize_t len = 0;
buf[0] = '\0'; buf[0] = '\0';
while (xread(fd, buf + read, 1) && read < size - 1) { while (read(fd, buf + len, 1) >= 0 && len < size - 1) {
if (buf[read] == '\0' || buf[read++] == '\n') { if (buf[len] == '\0' || buf[len++] == '\n') {
buf[read] = '\0'; buf[len] = '\0';
break; break;
} }
} }
buf[size - 1] = '\0'; buf[size - 1] = '\0';
return read; return len;
} }
/* Call func for each process */ /* Call func for each process */