Magisk/jni/daemon/log_monitor.c

56 lines
1.3 KiB
C
Raw Normal View History

2017-04-04 19:44:13 +00:00
/* log_monitor.c - New thread to monitor logcat
*
* Open a new thread to call logcat and get logs with tag "Magisk"
* Also, write the logs to a log file for debugging purpose
*
*/
#include <stdio.h>
2017-04-14 19:23:09 +00:00
#include <limits.h>
2017-04-04 19:44:13 +00:00
#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"
2017-04-14 19:23:09 +00:00
#include "daemon.h"
2017-04-04 19:44:13 +00:00
static void *logger_thread(void *args) {
2017-04-21 22:33:40 +00:00
// Setup error handler
err_handler = exit_thread;
2017-05-07 19:11:14 +00:00
char *buffer = xmalloc(PATH_MAX);
2017-05-03 17:13:04 +00:00
rename(LOGFILE, LASTLOG);
2017-05-07 19:11:14 +00:00
FILE *logfile = xfdopen(xopen(LOGFILE, O_WRONLY | O_CREAT | O_CLOEXEC | O_TRUNC, 0644), "w");
2017-04-04 19:44:13 +00:00
// Disable buffering
setbuf(logfile, NULL);
2017-06-02 20:31:01 +00:00
int fd, log_pid;
while (1) {
// Start logcat
char *const command[] = { "logcat", "-s", "Magisk", "-v", "time", NULL };
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");
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...
fclose(logfile);
2017-06-02 20:31:01 +00:00
free(buffer);
2017-04-04 19:44:13 +00:00
return NULL;
}
2017-04-05 22:12:29 +00:00
/* Start a new thread to monitor logcat and dump to logfile */
2017-04-04 19:44:13 +00:00
void monitor_logs() {
2017-04-17 08:36:49 +00:00
pthread_t log_monitor_thread;
2017-04-21 16:54:08 +00:00
xpthread_create(&log_monitor_thread, NULL, logger_thread, NULL);
pthread_detach(log_monitor_thread);
2017-04-17 08:36:49 +00:00
}