2017-04-04 19:44:13 +00:00
|
|
|
/* log_monitor.c - New thread to monitor logcat
|
|
|
|
*
|
2017-10-08 21:05:52 +00:00
|
|
|
* A universal logcat monitor for many usages. Add listeners to the list,
|
|
|
|
* and the pointer of the new log line will be sent through pipes to trigger
|
|
|
|
* asynchronous events without polling
|
2017-04-04 19:44:13 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <pthread.h>
|
2017-05-07 19:11:14 +00:00
|
|
|
#include <unistd.h>
|
2017-06-02 20:31:01 +00:00
|
|
|
#include <sys/wait.h>
|
2017-04-04 19:44:13 +00:00
|
|
|
|
2017-04-05 22:12:29 +00:00
|
|
|
#include "magisk.h"
|
2017-04-04 19:44:13 +00:00
|
|
|
#include "utils.h"
|
2018-02-11 18:48:15 +00:00
|
|
|
#include "resetprop.h"
|
2017-10-08 21:05:52 +00:00
|
|
|
|
2017-11-27 20:43:46 +00:00
|
|
|
extern int is_daemon_init;
|
2018-02-11 18:48:15 +00:00
|
|
|
int logd = 0;
|
2017-10-08 21:05:52 +00:00
|
|
|
|
2017-12-18 10:17:37 +00:00
|
|
|
static int am_proc_start_filter(const char *log) {
|
|
|
|
return strstr(log, "am_proc_start") != NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int magisk_log_filter(const char *log) {
|
|
|
|
char *ss;
|
|
|
|
return (ss = strstr(log, " Magisk")) && (ss[-1] != 'D') && (ss[-1] != 'V');
|
|
|
|
}
|
|
|
|
|
|
|
|
static int magisk_debug_log_filter(const char *log) {
|
|
|
|
return strstr(log, "Magisk") != NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct log_listener log_events[] = {
|
|
|
|
{ /* HIDE_EVENT */
|
|
|
|
.fd = -1,
|
|
|
|
.filter = am_proc_start_filter
|
|
|
|
},
|
|
|
|
{ /* LOG_EVENT */
|
|
|
|
.fd = -1,
|
|
|
|
.filter = magisk_log_filter
|
|
|
|
},
|
|
|
|
{ /* DEBUG_EVENT */
|
|
|
|
.fd = -1,
|
|
|
|
.filter = magisk_debug_log_filter
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-10-08 21:05:52 +00:00
|
|
|
#ifdef MAGISK_DEBUG
|
2018-02-11 18:48:15 +00:00
|
|
|
static int debug_log_pid = -1, debug_log_fd = -1;
|
2017-10-08 21:05:52 +00:00
|
|
|
#endif
|
2017-04-04 19:44:13 +00:00
|
|
|
|
2018-02-11 18:48:15 +00:00
|
|
|
static void check_logd() {
|
|
|
|
char *prop = getprop("init.svc.logd");
|
|
|
|
if (prop != NULL) {
|
|
|
|
free(prop);
|
|
|
|
logd = 1;
|
|
|
|
} else {
|
|
|
|
LOGD("log_monitor: logd not started, disable logging");
|
|
|
|
logd = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-04 19:44:13 +00:00
|
|
|
static void *logger_thread(void *args) {
|
2017-10-08 21:05:52 +00:00
|
|
|
int log_fd = -1, log_pid;
|
|
|
|
char line[4096];
|
2017-06-02 20:31:01 +00:00
|
|
|
|
2017-10-14 13:09:32 +00:00
|
|
|
LOGD("log_monitor: logger start");
|
|
|
|
|
2017-06-02 20:31:01 +00:00
|
|
|
while (1) {
|
2018-02-11 18:48:15 +00:00
|
|
|
if (!logd) {
|
|
|
|
// Disable all services
|
|
|
|
for (int i = 0; i < (sizeof(log_events) / sizeof(struct log_listener)); ++i) {
|
|
|
|
close(log_events[i].fd);
|
|
|
|
log_events[i].fd = -1;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-06-02 20:31:01 +00:00
|
|
|
// Start logcat
|
2018-01-01 15:58:13 +00:00
|
|
|
log_pid = exec_command(0, &log_fd, NULL, "logcat", "-b", "events", "-b", "main", "-v", "threadtime", "-s", "am_proc_start", "-s", "Magisk", NULL);
|
2017-10-08 21:05:52 +00:00
|
|
|
while (fdgets(line, sizeof(line), log_fd)) {
|
2017-12-18 10:17:37 +00:00
|
|
|
for (int i = 0; i < (sizeof(log_events) / sizeof(struct log_listener)); ++i) {
|
|
|
|
if (log_events[i].fd > 0 && log_events[i].filter(line)) {
|
2017-10-08 21:05:52 +00:00
|
|
|
char *s = strdup(line);
|
2017-12-18 10:17:37 +00:00
|
|
|
xwrite(log_events[i].fd, &s, sizeof(s));
|
2017-10-08 21:05:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (kill(log_pid, 0))
|
|
|
|
break;
|
|
|
|
}
|
2017-12-17 18:51:27 +00:00
|
|
|
|
|
|
|
// Cleanup
|
|
|
|
close(log_fd);
|
|
|
|
log_fd = -1;
|
|
|
|
kill(log_pid, SIGTERM);
|
|
|
|
waitpid(log_pid, NULL, 0);
|
|
|
|
|
|
|
|
// Clear buffer before restart
|
2018-01-01 15:58:13 +00:00
|
|
|
exec_command_sync("logcat", "-b", "events", "-b", "main", "-c", NULL);
|
2018-02-11 18:48:15 +00:00
|
|
|
|
|
|
|
check_logd();
|
2017-04-04 19:44:13 +00:00
|
|
|
}
|
2017-06-02 20:31:01 +00:00
|
|
|
|
2017-05-07 19:11:14 +00:00
|
|
|
// Should never be here, but well...
|
2017-04-04 19:44:13 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-10-08 21:05:52 +00:00
|
|
|
static void *magisk_log_thread(void *args) {
|
2017-12-07 15:25:43 +00:00
|
|
|
// Buffer logs before we have data access
|
2017-10-08 21:05:52 +00:00
|
|
|
struct vector logs;
|
|
|
|
vec_init(&logs);
|
|
|
|
|
|
|
|
int pipefd[2];
|
|
|
|
if (xpipe2(pipefd, O_CLOEXEC) == -1)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
// Register our listener
|
2017-12-18 10:17:37 +00:00
|
|
|
log_events[LOG_EVENT].fd = pipefd[1];
|
2017-10-08 21:05:52 +00:00
|
|
|
|
2017-10-14 13:09:32 +00:00
|
|
|
LOGD("log_monitor: magisk log dumper start");
|
|
|
|
|
2017-12-22 20:58:51 +00:00
|
|
|
FILE *log = NULL;
|
2017-10-08 21:05:52 +00:00
|
|
|
for (char *line; xxread(pipefd[0], &line, sizeof(line)) > 0; free(line)) {
|
2017-12-22 20:58:51 +00:00
|
|
|
if (!is_daemon_init) {
|
|
|
|
vec_push_back(&logs, strdup(line));
|
|
|
|
} else {
|
|
|
|
if (log == NULL) {
|
2017-12-18 10:17:37 +00:00
|
|
|
// Dump buffered logs to file
|
|
|
|
log = xfopen(LOGFILE, "w");
|
|
|
|
setbuf(log, NULL);
|
|
|
|
char *tmp;
|
|
|
|
vec_for_each(&logs, tmp) {
|
|
|
|
fprintf(log, "%s", tmp);
|
|
|
|
free(tmp);
|
2017-10-08 21:05:52 +00:00
|
|
|
}
|
2017-12-18 10:17:37 +00:00
|
|
|
vec_destroy(&logs);
|
2017-10-08 21:05:52 +00:00
|
|
|
}
|
2017-12-18 10:17:37 +00:00
|
|
|
fprintf(log, "%s", line);
|
2017-12-22 20:58:51 +00:00
|
|
|
}
|
2017-10-08 21:05:52 +00:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *debug_magisk_log_thread(void *args) {
|
|
|
|
FILE *log = xfopen(DEBUG_LOG, "a");
|
|
|
|
setbuf(log, NULL);
|
|
|
|
int pipefd[2];
|
|
|
|
if (xpipe2(pipefd, O_CLOEXEC) == -1)
|
|
|
|
return NULL;
|
|
|
|
|
2017-10-14 13:09:32 +00:00
|
|
|
LOGD("log_monitor: debug log dumper start");
|
|
|
|
|
2017-10-08 21:05:52 +00:00
|
|
|
// Register our listener
|
2017-12-18 10:17:37 +00:00
|
|
|
log_events[DEBUG_EVENT].fd = pipefd[1];
|
|
|
|
|
|
|
|
for (char *line; xxread(pipefd[0], &line, sizeof(line)) > 0; free(line))
|
|
|
|
fprintf(log, "%s", line);
|
2017-10-08 21:05:52 +00:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Start new threads to monitor logcat and dump to logfile */
|
2017-04-04 19:44:13 +00:00
|
|
|
void monitor_logs() {
|
2017-06-07 19:20:49 +00:00
|
|
|
pthread_t thread;
|
2017-10-08 21:05:52 +00:00
|
|
|
|
2018-02-11 18:48:15 +00:00
|
|
|
check_logd();
|
2017-10-08 21:05:52 +00:00
|
|
|
|
2018-02-11 18:48:15 +00:00
|
|
|
if (logd) {
|
|
|
|
// Start log file dumper before monitor
|
|
|
|
xpthread_create(&thread, NULL, magisk_log_thread, NULL);
|
|
|
|
pthread_detach(thread);
|
2017-10-08 21:05:52 +00:00
|
|
|
|
2018-02-11 18:48:15 +00:00
|
|
|
// Start logcat monitor
|
|
|
|
xpthread_create(&thread, NULL, logger_thread, NULL);
|
|
|
|
pthread_detach(thread);
|
|
|
|
}
|
2017-10-08 21:05:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void start_debug_full_log() {
|
|
|
|
#ifdef MAGISK_DEBUG
|
2018-02-11 18:48:15 +00:00
|
|
|
if (logd) {
|
|
|
|
// Log everything initially
|
|
|
|
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);
|
|
|
|
close(debug_log_fd);
|
|
|
|
}
|
2017-10-08 21:05:52 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void stop_debug_full_log() {
|
|
|
|
#ifdef MAGISK_DEBUG
|
|
|
|
// Stop recording the boot logcat after every boot task is done
|
2018-02-11 18:48:15 +00:00
|
|
|
if (debug_log_pid > 0) {
|
|
|
|
kill(debug_log_pid, SIGTERM);
|
|
|
|
waitpid(debug_log_pid, NULL, 0);
|
|
|
|
// Start debug thread
|
|
|
|
start_debug_log();
|
|
|
|
}
|
2017-10-10 11:49:15 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void start_debug_log() {
|
|
|
|
#ifdef MAGISK_DEBUG
|
2018-02-11 18:48:15 +00:00
|
|
|
if (logd) {
|
|
|
|
pthread_t thread;
|
|
|
|
// Start debug thread
|
|
|
|
xpthread_create(&thread, NULL, debug_magisk_log_thread, NULL);
|
|
|
|
pthread_detach(thread);
|
|
|
|
}
|
2017-10-08 21:05:52 +00:00
|
|
|
#endif
|
2017-04-17 08:36:49 +00:00
|
|
|
}
|