From 558f95cf7e646480f9c13c6b8ce719bc5a3e70d3 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Sat, 13 Jan 2018 05:49:03 +0800 Subject: [PATCH] Improve APK installation and add logging --- core/jni/core/bootstages.c | 12 +++++++++--- core/jni/utils/misc.c | 7 ++++++- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/core/jni/core/bootstages.c b/core/jni/core/bootstages.c index 9ff676107..8216c68a2 100644 --- a/core/jni/core/bootstages.c +++ b/core/jni/core/bootstages.c @@ -648,6 +648,7 @@ core_only: // Install Magisk Manager if exists if (access(MANAGERAPK, F_OK) == 0) { rename(MANAGERAPK, "/data/magisk.apk"); + setfilecon("/data/magisk.apk", "u:object_r:su_file:s0"); while (1) { sleep(5); int apk_res = -1, pid; @@ -656,12 +657,17 @@ core_only: "/system/bin", "com.android.commands.pm.Pm", "install", "-r", "/data/magisk.apk", NULL); if (pid != -1) { + int err = 0; + while (fdgets(buf, PATH_MAX, apk_res) > 0) { + LOGD("apk_install: %s", buf); + err |= strstr(buf, "Error:") != NULL; + } waitpid(pid, NULL, 0); - fdgets(buf, PATH_MAX, apk_res); close(apk_res); // Keep trying until pm is started - if (strstr(buf, "Error:") == NULL) - break; + if (err) + continue; + break; } } unlink("/data/magisk.apk"); diff --git a/core/jni/utils/misc.c b/core/jni/utils/misc.c index 7bca4ea76..8a7112532 100644 --- a/core/jni/utils/misc.c +++ b/core/jni/utils/misc.c @@ -111,7 +111,12 @@ static int is_num(const char *s) { ssize_t fdgets(char *buf, const size_t size, int fd) { ssize_t len = 0; buf[0] = '\0'; - while (read(fd, buf + len, 1) >= 0 && len < size - 1) { + while (len < size - 1) { + int ret = read(fd, buf + len, 1); + if (ret < 0) + return -1; + if (ret == 0) + break; if (buf[len] == '\0' || buf[len++] == '\n') { buf[len] = '\0'; break;