2017-04-04 21:44:13 +02:00
|
|
|
/* log_monitor.c - New thread to monitor logcat
|
|
|
|
*
|
2017-10-08 23:05:52 +02: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 21:44:13 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <pthread.h>
|
2017-05-07 21:11:14 +02:00
|
|
|
#include <unistd.h>
|
2017-06-02 22:31:01 +02:00
|
|
|
#include <sys/wait.h>
|
2018-07-01 12:18:12 +02:00
|
|
|
#include <fcntl.h>
|
2017-04-04 21:44:13 +02:00
|
|
|
|
2017-04-06 00:12:29 +02:00
|
|
|
#include "magisk.h"
|
2017-04-04 21:44:13 +02:00
|
|
|
#include "utils.h"
|
2018-02-11 19:48:15 +01:00
|
|
|
#include "resetprop.h"
|
2017-10-08 23:05:52 +02:00
|
|
|
|
2018-04-07 20:12:40 +02:00
|
|
|
int loggable = 1;
|
2017-10-08 23:05:52 +02:00
|
|
|
|
2017-12-18 11:17:37 +01: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) {
|
2018-07-01 12:18:12 +02:00
|
|
|
return strstr(log, "am_proc_start") == NULL;
|
2017-12-18 11:17:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
};
|
2018-07-01 12:18:12 +02:00
|
|
|
#define EVENT_NUM (sizeof(log_events) / sizeof(struct log_listener))
|
2017-04-04 21:44:13 +02:00
|
|
|
|
2018-04-07 20:12:40 +02:00
|
|
|
static void test_logcat() {
|
|
|
|
int log_fd = -1, log_pid;
|
|
|
|
char buf[1];
|
|
|
|
log_pid = exec_command(0, &log_fd, NULL, "logcat", NULL);
|
|
|
|
if (read(log_fd, buf, sizeof(buf)) != sizeof(buf)) {
|
|
|
|
loggable = 0;
|
|
|
|
LOGD("log_monitor: cannot read from logcat, disable logging");
|
2018-02-11 19:48:15 +01:00
|
|
|
}
|
2018-04-07 20:12:40 +02:00
|
|
|
kill(log_pid, SIGTERM);
|
|
|
|
waitpid(log_pid, NULL, 0);
|
2018-02-11 19:48:15 +01:00
|
|
|
}
|
|
|
|
|
2017-04-04 21:44:13 +02:00
|
|
|
static void *logger_thread(void *args) {
|
2017-10-08 23:05:52 +02:00
|
|
|
int log_fd = -1, log_pid;
|
2018-07-01 12:18:12 +02:00
|
|
|
char line[PIPE_BUF];
|
2017-06-02 22:31:01 +02:00
|
|
|
|
2017-10-14 15:09:32 +02:00
|
|
|
LOGD("log_monitor: logger start");
|
|
|
|
|
2017-06-02 22:31:01 +02:00
|
|
|
while (1) {
|
2018-04-07 20:12:40 +02:00
|
|
|
if (!loggable) {
|
2018-02-11 19:48:15 +01:00
|
|
|
// Disable all services
|
2018-07-01 12:18:12 +02:00
|
|
|
for (int i = 0; i < EVENT_NUM; ++i) {
|
2018-02-11 19:48:15 +01:00
|
|
|
close(log_events[i].fd);
|
|
|
|
log_events[i].fd = -1;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-06-02 22:31:01 +02:00
|
|
|
// Start logcat
|
2018-07-01 12:18:12 +02:00
|
|
|
log_pid = exec_command(0, &log_fd, NULL,
|
|
|
|
"/system/bin/logcat",
|
|
|
|
"-b", "events", "-b", "main", "-b", "crash",
|
|
|
|
"-v", "threadtime",
|
|
|
|
"-s", "am_proc_start", "Magisk", "*:F",
|
|
|
|
NULL);
|
|
|
|
FILE *logs = fdopen(log_fd, "r");
|
|
|
|
while (fgets(line, sizeof(line), logs)) {
|
|
|
|
if (line[0] == '-')
|
|
|
|
continue;
|
|
|
|
size_t len = strlen(line);
|
|
|
|
for (int i = 0; i < EVENT_NUM; ++i) {
|
|
|
|
if (log_events[i].fd > 0 && log_events[i].filter(line))
|
|
|
|
xwrite(log_events[i].fd, line, len);
|
2017-10-08 23:05:52 +02:00
|
|
|
}
|
|
|
|
if (kill(log_pid, 0))
|
|
|
|
break;
|
|
|
|
}
|
2017-12-17 19:51:27 +01:00
|
|
|
|
|
|
|
// Cleanup
|
2018-07-01 12:18:12 +02:00
|
|
|
fclose(logs);
|
2017-12-17 19:51:27 +01:00
|
|
|
log_fd = -1;
|
|
|
|
kill(log_pid, SIGTERM);
|
|
|
|
waitpid(log_pid, NULL, 0);
|
2018-04-07 20:12:40 +02:00
|
|
|
test_logcat();
|
2017-04-04 21:44:13 +02:00
|
|
|
}
|
2017-06-02 22:31:01 +02:00
|
|
|
|
2017-05-07 21:11:14 +02:00
|
|
|
// Should never be here, but well...
|
2017-04-04 21:44:13 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-10-08 23:05:52 +02:00
|
|
|
/* Start new threads to monitor logcat and dump to logfile */
|
2017-04-04 21:44:13 +02:00
|
|
|
void monitor_logs() {
|
2017-06-07 21:20:49 +02:00
|
|
|
pthread_t thread;
|
2017-10-08 23:05:52 +02:00
|
|
|
|
2018-04-07 20:12:40 +02:00
|
|
|
test_logcat();
|
2017-10-08 23:05:52 +02:00
|
|
|
|
2018-04-07 20:12:40 +02:00
|
|
|
if (loggable) {
|
2018-07-01 12:18:12 +02:00
|
|
|
rename(LOGFILE, LOGFILE ".bak");
|
|
|
|
log_events[LOG_EVENT].fd = creat(LOGFILE, 0644);
|
2017-10-08 23:05:52 +02:00
|
|
|
#ifdef MAGISK_DEBUG
|
2018-07-01 12:18:12 +02:00
|
|
|
log_events[DEBUG_EVENT].fd = creat(DEBUG_LOG, 0644);
|
2017-10-10 13:49:15 +02:00
|
|
|
#endif
|
|
|
|
|
2018-07-01 12:18:12 +02:00
|
|
|
// Start logcat monitor
|
|
|
|
xpthread_create(&thread, NULL, logger_thread, NULL);
|
2018-02-11 19:48:15 +01:00
|
|
|
pthread_detach(thread);
|
|
|
|
}
|
2017-04-17 10:36:49 +02:00
|
|
|
}
|