Test logcat instead of checking logd

This commit is contained in:
topjohnwu 2018-04-08 02:12:40 +08:00
parent 7d7686da33
commit 42284c5efb
4 changed files with 24 additions and 23 deletions

View File

@ -15,7 +15,7 @@
#include "resetprop.h" #include "resetprop.h"
extern int is_daemon_init; extern int is_daemon_init;
int logd = 0; int loggable = 1;
static int am_proc_start_filter(const char *log) { static int am_proc_start_filter(const char *log) {
return strstr(log, "am_proc_start") != NULL; return strstr(log, "am_proc_start") != NULL;
@ -49,15 +49,16 @@ struct log_listener log_events[] = {
static int debug_log_pid = -1, debug_log_fd = -1; static int debug_log_pid = -1, debug_log_fd = -1;
#endif #endif
static void check_logd() { static void test_logcat() {
char *prop = getprop("init.svc.logd"); int log_fd = -1, log_pid;
if (prop != NULL) { char buf[1];
free(prop); log_pid = exec_command(0, &log_fd, NULL, "logcat", NULL);
logd = 1; if (read(log_fd, buf, sizeof(buf)) != sizeof(buf)) {
} else { loggable = 0;
LOGD("log_monitor: logd not started, disable logging"); LOGD("log_monitor: cannot read from logcat, disable logging");
logd = 0;
} }
kill(log_pid, SIGTERM);
waitpid(log_pid, NULL, 0);
} }
static void *logger_thread(void *args) { static void *logger_thread(void *args) {
@ -67,7 +68,7 @@ static void *logger_thread(void *args) {
LOGD("log_monitor: logger start"); LOGD("log_monitor: logger start");
while (1) { while (1) {
if (!logd) { if (!loggable) {
// Disable all services // Disable all services
for (int i = 0; i < (sizeof(log_events) / sizeof(struct log_listener)); ++i) { for (int i = 0; i < (sizeof(log_events) / sizeof(struct log_listener)); ++i) {
close(log_events[i].fd); close(log_events[i].fd);
@ -98,7 +99,7 @@ static void *logger_thread(void *args) {
// Clear buffer before restart // Clear buffer before restart
exec_command_sync("logcat", "-b", "events", "-b", "main", "-c", NULL); exec_command_sync("logcat", "-b", "events", "-b", "main", "-c", NULL);
check_logd(); test_logcat();
} }
// Should never be here, but well... // Should never be here, but well...
@ -163,9 +164,9 @@ static void *debug_magisk_log_thread(void *args) {
void monitor_logs() { void monitor_logs() {
pthread_t thread; pthread_t thread;
check_logd(); test_logcat();
if (logd) { if (loggable) {
// Start log file dumper before monitor // Start log file dumper before monitor
xpthread_create(&thread, NULL, magisk_log_thread, NULL); xpthread_create(&thread, NULL, magisk_log_thread, NULL);
pthread_detach(thread); pthread_detach(thread);
@ -178,7 +179,7 @@ void monitor_logs() {
void start_debug_full_log() { void start_debug_full_log() {
#ifdef MAGISK_DEBUG #ifdef MAGISK_DEBUG
if (logd) { if (loggable) {
// Log everything initially // Log everything initially
debug_log_fd = xopen(DEBUG_LOG, O_WRONLY | O_CREAT | O_CLOEXEC | O_TRUNC, 0644); debug_log_fd = xopen(DEBUG_LOG, O_WRONLY | O_CREAT | O_CLOEXEC | O_TRUNC, 0644);
debug_log_pid = exec_command(0, &debug_log_fd, NULL, "logcat", "-v", "threadtime", NULL); debug_log_pid = exec_command(0, &debug_log_fd, NULL, "logcat", "-v", "threadtime", NULL);
@ -201,7 +202,7 @@ void stop_debug_full_log() {
void start_debug_log() { void start_debug_log() {
#ifdef MAGISK_DEBUG #ifdef MAGISK_DEBUG
if (logd) { if (loggable) {
pthread_t thread; pthread_t thread;
// Start debug thread // Start debug thread
xpthread_create(&thread, NULL, debug_magisk_log_thread, NULL); xpthread_create(&thread, NULL, debug_magisk_log_thread, NULL);

View File

@ -31,7 +31,7 @@ enum {
DAEMON_ERROR = -1, DAEMON_ERROR = -1,
DAEMON_SUCCESS = 0, DAEMON_SUCCESS = 0,
ROOT_REQUIRED, ROOT_REQUIRED,
LOGD_DISABLED, LOGCAT_DISABLED,
HIDE_IS_ENABLED, HIDE_IS_ENABLED,
HIDE_NOT_ENABLED, HIDE_NOT_ENABLED,
HIDE_ITEM_EXIST, HIDE_ITEM_EXIST,

View File

@ -58,7 +58,7 @@ struct log_listener {
}; };
extern struct log_listener log_events[]; extern struct log_listener log_events[];
extern int logd; extern int loggable;
void monitor_logs(); void monitor_logs();
void start_debug_full_log(); void start_debug_full_log();

View File

@ -49,9 +49,9 @@ void launch_magiskhide(int client) {
return; return;
} }
if (!logd) { if (!loggable) {
if (client > 0) { if (client > 0) {
write_int(client, LOGD_DISABLED); write_int(client, LOGCAT_DISABLED);
close(client); close(client);
} }
setprop(MAGISKHIDE_PROP, "0"); setprop(MAGISKHIDE_PROP, "0");
@ -147,14 +147,14 @@ int magiskhide_main(int argc, char *argv[]) {
case ROOT_REQUIRED: case ROOT_REQUIRED:
fprintf(stderr, "Root is required for this operation\n"); fprintf(stderr, "Root is required for this operation\n");
return code; return code;
case LOGD_DISABLED: case LOGCAT_DISABLED:
fprintf(stderr, "Logd is not running, cannot run logcat\n"); fprintf(stderr, "Logcat is disabled, cannot start MagiskHide\n");
return (code); return (code);
case HIDE_NOT_ENABLED: case HIDE_NOT_ENABLED:
fprintf(stderr, "Magisk hide is not enabled yet\n"); fprintf(stderr, "MagiskHide is not enabled yet\n");
return code; return code;
case HIDE_IS_ENABLED: case HIDE_IS_ENABLED:
fprintf(stderr, "Magisk hide is already enabled\n"); fprintf(stderr, "MagiskHide is already enabled\n");
return code; return code;
case HIDE_ITEM_EXIST: case HIDE_ITEM_EXIST:
fprintf(stderr, "Process [%s] already exists in hide list\n", argv[2]); fprintf(stderr, "Process [%s] already exists in hide list\n", argv[2]);