2018-07-05 23:57:18 +00:00
|
|
|
/* log_daemon.c - A dedicated daemon to monitor logcat
|
2017-04-04 19:44:13 +00:00
|
|
|
*
|
2017-10-08 21:05:52 +00:00
|
|
|
* A universal logcat monitor for many usages. Add listeners to the list,
|
2018-07-05 23:57:18 +00:00
|
|
|
* and the new log line will be sent through sockets to trigger
|
2017-10-08 21:05:52 +00:00
|
|
|
* 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>
|
2018-07-01 10:18:12 +00:00
|
|
|
#include <fcntl.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-07-02 14:11:28 +00:00
|
|
|
#include "daemon.h"
|
2017-10-08 21:05:52 +00:00
|
|
|
|
2018-08-02 17:58:56 +00:00
|
|
|
static int loggable = 0;
|
2018-07-05 23:32:16 +00:00
|
|
|
static struct vector log_cmd, clear_cmd;
|
2018-07-02 14:11:28 +00:00
|
|
|
static int sockfd;
|
|
|
|
static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
|
|
|
|
|
|
|
|
enum {
|
|
|
|
HIDE_EVENT,
|
2018-07-02 14:48:26 +00:00
|
|
|
LOG_EVENT
|
2018-07-02 14:11:28 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct log_listener {
|
|
|
|
int fd;
|
|
|
|
int (*filter) (const char*);
|
|
|
|
};
|
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) {
|
2018-07-02 14:48:26 +00:00
|
|
|
return !am_proc_start_filter(log);
|
2017-12-18 10:17:37 +00:00
|
|
|
}
|
|
|
|
|
2018-07-03 14:25:39 +00:00
|
|
|
static struct log_listener events[] = {
|
2017-12-18 10:17:37 +00:00
|
|
|
{ /* HIDE_EVENT */
|
|
|
|
.fd = -1,
|
|
|
|
.filter = am_proc_start_filter
|
|
|
|
},
|
|
|
|
{ /* LOG_EVENT */
|
|
|
|
.fd = -1,
|
|
|
|
.filter = magisk_log_filter
|
|
|
|
}
|
|
|
|
};
|
2018-07-03 14:25:39 +00:00
|
|
|
#define EVENT_NUM (sizeof(events) / sizeof(struct log_listener))
|
2017-04-04 19:44:13 +00:00
|
|
|
|
2018-07-02 14:11:28 +00:00
|
|
|
static void sigpipe_handler(int sig) {
|
2018-07-03 14:25:39 +00:00
|
|
|
close(events[HIDE_EVENT].fd);
|
|
|
|
events[HIDE_EVENT].fd = -1;
|
2018-07-02 14:11:28 +00:00
|
|
|
}
|
|
|
|
|
2018-07-02 17:38:19 +00:00
|
|
|
static void *monitor_thread(void *args) {
|
|
|
|
// Block SIGPIPE to prevent interruption
|
|
|
|
sigset_t block_set;
|
|
|
|
sigemptyset(&block_set);
|
|
|
|
sigaddset(&block_set, SIGPIPE);
|
|
|
|
pthread_sigmask(SIG_SETMASK, &block_set, NULL);
|
|
|
|
// Give the main daemon some time before we monitor it
|
|
|
|
sleep(5);
|
|
|
|
int fd;
|
2018-07-05 23:32:16 +00:00
|
|
|
char b;
|
2018-07-05 23:51:17 +00:00
|
|
|
while (1) {
|
2018-07-02 17:38:19 +00:00
|
|
|
fd = connect_daemon();
|
2018-07-05 23:51:17 +00:00
|
|
|
write_int(fd, HANDSHAKE);
|
2018-07-02 17:38:19 +00:00
|
|
|
// This should hold unless the daemon is killed
|
2018-07-05 23:32:16 +00:00
|
|
|
read(fd, &b, sizeof(b));
|
2018-07-02 17:38:19 +00:00
|
|
|
// The main daemon crashed, spawn a new one
|
|
|
|
close(fd);
|
2018-07-05 23:51:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *logcat_thread(void *args) {
|
|
|
|
int log_fd = -1, log_pid;
|
|
|
|
char line[4096];
|
|
|
|
while (1) {
|
|
|
|
// Start logcat
|
|
|
|
log_pid = exec_array(0, &log_fd, NULL, (char **) vec_entry(&log_cmd));
|
|
|
|
FILE *logs = fdopen(log_fd, "r");
|
|
|
|
while (fgets(line, sizeof(line), logs)) {
|
|
|
|
if (line[0] == '-')
|
|
|
|
continue;
|
|
|
|
size_t len = strlen(line);
|
|
|
|
pthread_mutex_lock(&lock);
|
|
|
|
for (int i = 0; i < EVENT_NUM; ++i) {
|
|
|
|
if (events[i].fd > 0 && events[i].filter(line))
|
|
|
|
write(events[i].fd, line, len);
|
|
|
|
}
|
|
|
|
pthread_mutex_unlock(&lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(logs);
|
|
|
|
log_fd = -1;
|
|
|
|
kill(log_pid, SIGTERM);
|
|
|
|
waitpid(log_pid, NULL, 0);
|
|
|
|
|
|
|
|
LOGI("magisklogd: logcat output EOF");
|
|
|
|
// Clear buffer
|
|
|
|
log_pid = exec_array(0, NULL, NULL, (char **) vec_entry(&clear_cmd));
|
|
|
|
waitpid(log_pid, NULL, 0);
|
|
|
|
}
|
2018-07-02 17:38:19 +00:00
|
|
|
}
|
|
|
|
|
2018-08-02 17:58:56 +00:00
|
|
|
int check_and_start_logger() {
|
|
|
|
if (!loggable) {
|
|
|
|
int fd;
|
|
|
|
loggable = exec_command_sync("/system/bin/logcat", "-d", "-f", "/dev/null", NULL) == 0;
|
|
|
|
chmod("/dev/null", 0666);
|
|
|
|
if (loggable) {
|
|
|
|
connect_daemon2(LOG_DAEMON, &fd);
|
|
|
|
write_int(fd, HANDSHAKE);
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return loggable;
|
|
|
|
}
|
|
|
|
|
2018-07-02 14:11:28 +00:00
|
|
|
void log_daemon() {
|
|
|
|
setsid();
|
|
|
|
struct sockaddr_un sun;
|
|
|
|
sockfd = setup_socket(&sun, LOG_DAEMON);
|
2018-09-15 06:49:19 +00:00
|
|
|
if (xbind(sockfd, (struct sockaddr*) &sun, sizeof(sun.sun_family) + strlen(sun.sun_path + 1) + 1))
|
2018-07-02 14:11:28 +00:00
|
|
|
exit(1);
|
2018-07-03 14:25:39 +00:00
|
|
|
xlisten(sockfd, 10);
|
2018-07-02 14:11:28 +00:00
|
|
|
LOGI("Magisk v" xstr(MAGISK_VERSION) "(" xstr(MAGISK_VER_CODE) ") logger started\n");
|
2018-07-03 14:25:39 +00:00
|
|
|
strcpy(argv0, "magisklogd");
|
2018-07-02 14:11:28 +00:00
|
|
|
|
2018-07-02 17:38:19 +00:00
|
|
|
// Set SIGPIPE handler
|
2018-07-02 14:11:28 +00:00
|
|
|
struct sigaction act;
|
|
|
|
memset(&act, 0, sizeof(act));
|
|
|
|
act.sa_handler = sigpipe_handler;
|
|
|
|
sigaction(SIGPIPE, &act, NULL);
|
|
|
|
|
|
|
|
// Setup log dumps
|
|
|
|
rename(LOGFILE, LOGFILE ".bak");
|
2018-07-04 15:46:40 +00:00
|
|
|
events[LOG_EVENT].fd = xopen(LOGFILE, O_CREAT | O_WRONLY | O_TRUNC | O_CLOEXEC | O_APPEND, 0644);
|
2018-07-02 14:11:28 +00:00
|
|
|
|
2018-07-05 23:32:16 +00:00
|
|
|
// Construct cmdline
|
|
|
|
vec_init(&log_cmd);
|
|
|
|
vec_push_back(&log_cmd, "/system/bin/logcat");
|
|
|
|
// Test whether these buffers actually works
|
|
|
|
const char* b[] = { "main", "events", "crash" };
|
|
|
|
for (int i = 0; i < 3; ++i) {
|
|
|
|
if (exec_command_sync("/system/bin/logcat", "-b", b[i], "-d", "-f", "/dev/null", NULL) == 0)
|
|
|
|
vec_push_back_all(&log_cmd, "-b", b[i], NULL);
|
|
|
|
}
|
2018-07-15 22:42:36 +00:00
|
|
|
chmod("/dev/null", 0666);
|
2018-07-05 23:32:16 +00:00
|
|
|
vec_dup(&log_cmd, &clear_cmd);
|
2018-07-17 19:25:36 +00:00
|
|
|
vec_push_back_all(&log_cmd, "-v", "threadtime", "-s", "am_proc_start", "Magisk", NULL);
|
|
|
|
#ifdef MAGISK_DEBUG
|
|
|
|
vec_push_back(&log_cmd, "*:F");
|
|
|
|
#endif
|
2018-07-05 23:32:16 +00:00
|
|
|
vec_push_back(&log_cmd, NULL);
|
|
|
|
vec_push_back(&clear_cmd, "-c");
|
|
|
|
vec_push_back(&clear_cmd, NULL);
|
|
|
|
|
2018-07-05 23:51:17 +00:00
|
|
|
// Start worker threads
|
|
|
|
pthread_t thread;
|
|
|
|
pthread_create(&thread, NULL, monitor_thread, NULL);
|
|
|
|
pthread_detach(thread);
|
|
|
|
xpthread_create(&thread, NULL, logcat_thread, NULL);
|
|
|
|
pthread_detach(thread);
|
2017-06-02 20:31:01 +00:00
|
|
|
|
2018-07-05 23:51:17 +00:00
|
|
|
while(1) {
|
|
|
|
int fd = xaccept4(sockfd, NULL, NULL, SOCK_CLOEXEC);
|
|
|
|
switch(read_int(fd)) {
|
|
|
|
case HIDE_CONNECT:
|
|
|
|
pthread_mutex_lock(&lock);
|
|
|
|
close(events[HIDE_EVENT].fd);
|
|
|
|
events[HIDE_EVENT].fd = fd;
|
|
|
|
pthread_mutex_unlock(&lock);
|
|
|
|
break;
|
|
|
|
case HANDSHAKE:
|
|
|
|
default:
|
|
|
|
close(fd);
|
|
|
|
break;
|
2017-10-08 21:05:52 +00:00
|
|
|
}
|
2018-02-11 18:48:15 +00:00
|
|
|
}
|
2017-04-17 08:36:49 +00:00
|
|
|
}
|