Fix magiskinit invincible mode and logcat monitor

This commit is contained in:
topjohnwu 2017-12-18 02:51:27 +08:00
parent 17f0fea3fc
commit d065040321
4 changed files with 17 additions and 11 deletions

View File

@ -262,9 +262,6 @@ void start_daemon() {
exit(1); exit(1);
xlisten(fd, 10); xlisten(fd, 10);
// Start the log monitor
monitor_logs();
if ((is_daemon_init = (access(MAGISKTMP, F_OK) == 0))) { if ((is_daemon_init = (access(MAGISKTMP, F_OK) == 0))) {
// Restart stuffs if the daemon is restarted // Restart stuffs if the daemon is restarted
exec_command_sync("logcat", "-b", "all", "-c", NULL); exec_command_sync("logcat", "-b", "all", "-c", NULL);
@ -274,6 +271,9 @@ void start_daemon() {
daemon_init(); daemon_init();
} }
// Start the log monitor
monitor_logs();
LOGI("Magisk v" xstr(MAGISK_VERSION) "(" xstr(MAGISK_VER_CODE) ") daemon started\n"); LOGI("Magisk v" xstr(MAGISK_VERSION) "(" xstr(MAGISK_VER_CODE) ") daemon started\n");
// Change process name // Change process name

View File

@ -39,7 +39,14 @@ static void *logger_thread(void *args) {
if (kill(log_pid, 0)) if (kill(log_pid, 0))
break; break;
} }
// Clear buffer if restart required
// Cleanup
close(log_fd);
log_fd = -1;
kill(log_pid, SIGTERM);
waitpid(log_pid, NULL, 0);
// Clear buffer before restart
exec_command_sync("logcat", "-b", "all", "-c", NULL); exec_command_sync("logcat", "-b", "all", "-c", NULL);
} }
@ -54,7 +61,6 @@ static void *magisk_log_thread(void *args) {
struct vector logs; struct vector logs;
vec_init(&logs); vec_init(&logs);
FILE *log;
int pipefd[2]; int pipefd[2];
if (xpipe2(pipefd, O_CLOEXEC) == -1) if (xpipe2(pipefd, O_CLOEXEC) == -1)
return NULL; return NULL;
@ -64,14 +70,14 @@ static void *magisk_log_thread(void *args) {
LOGD("log_monitor: magisk log dumper start"); LOGD("log_monitor: magisk log dumper start");
for (char *line; xxread(pipefd[0], &line, sizeof(line)) > 0; free(line)) { FILE *log;
char *ss; char *ss;
for (char *line; xxread(pipefd[0], &line, sizeof(line)) > 0; free(line)) {
if ((ss = strstr(line, " Magisk")) && (ss[-1] != 'D') && (ss[-1] != 'V')) { if ((ss = strstr(line, " Magisk")) && (ss[-1] != 'D') && (ss[-1] != 'V')) {
if (!have_data) { if (!have_data) {
if ((have_data = check_data())) { if ((have_data = check_data())) {
// Dump buffered logs to file // Dump buffered logs to file
rename(LOGFILE, LASTLOG); log = xfopen(LOGFILE, "w");
log = xfopen(LOGFILE, "a");
setbuf(log, NULL); setbuf(log, NULL);
char *tmp; char *tmp;
vec_for_each(&logs, tmp) { vec_for_each(&logs, tmp) {

View File

@ -380,9 +380,9 @@ static void magisk_init_daemon() {
destroy_policydb(); destroy_policydb();
// Keep Magisk daemon always alive // Keep Magisk daemon always alive
while (1) {
struct sockaddr_un sun; struct sockaddr_un sun;
fd = setup_socket(&sun); fd = setup_socket(&sun);
while (1) {
while (connect(fd, (struct sockaddr*) &sun, sizeof(sun))) while (connect(fd, (struct sockaddr*) &sun, sizeof(sun)))
usleep(10000); /* Wait 10 ms after each try */ usleep(10000); /* Wait 10 ms after each try */
@ -391,6 +391,7 @@ static void magisk_init_daemon() {
/* If things went here, it means the other side of the socket is closed /* If things went here, it means the other side of the socket is closed
* We restart the daemon again */ * We restart the daemon again */
close(fd);
if (fork_dont_care() == 0) { if (fork_dont_care() == 0) {
execv("/sbin/magisk", (char *[]) { "resetprop", "magisk.daemon", "1", NULL } ); execv("/sbin/magisk", (char *[]) { "resetprop", "magisk.daemon", "1", NULL } );
exit(1); exit(1);

View File

@ -14,7 +14,6 @@
#endif #endif
#define LOGFILE "/cache/magisk.log" #define LOGFILE "/cache/magisk.log"
#define LASTLOG "/cache/last_magisk.log"
#define DEBUG_LOG "/data/adb/magisk_debug.log" #define DEBUG_LOG "/data/adb/magisk_debug.log"
#define UNBLOCKFILE "/dev/.magisk.unblock" #define UNBLOCKFILE "/dev/.magisk.unblock"
#define PATCHDONE "/dev/.magisk.patch.done" #define PATCHDONE "/dev/.magisk.patch.done"