From 4c230d9e61880c8315a6cbc89b4d4c9affee7a62 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Tue, 5 Sep 2017 04:07:33 +0800 Subject: [PATCH] Root shell workaround --- .../magisk/asyncs/InstallMagisk.java | 20 ++++++------- .../com/topjohnwu/magisk/utils/Shell.java | 28 +++++++++++++++++++ 2 files changed, 37 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/com/topjohnwu/magisk/asyncs/InstallMagisk.java b/app/src/main/java/com/topjohnwu/magisk/asyncs/InstallMagisk.java index 433297cf7..e105c776a 100644 --- a/app/src/main/java/com/topjohnwu/magisk/asyncs/InstallMagisk.java +++ b/app/src/main/java/com/topjohnwu/magisk/asyncs/InstallMagisk.java @@ -154,8 +154,16 @@ public class InstallMagisk extends ParallelTask { mList.add("- Use boot image: " + boot); + Shell shell; + if (mode == PATCH_MODE && Shell.rootAccess()) { + // Force non-root shell + shell = Shell.getShell("sh"); + } else { + shell = getShell(); + } + // Patch boot image - getShell().sh(mList, + shell.sh(mList, "cd " + install, "KEEPFORCEENCRYPT=" + mKeepEnc + " KEEPVERITY=" + mKeepVerity + " sh " + "update-binary indep boot_patch.sh " + boot + " 2>&1" + @@ -176,16 +184,6 @@ public class InstallMagisk extends ParallelTask { getShell().sh_raw("cp -f " + patched_boot + " " + dest); break; case ".img.tar": - // Workaround root shell issues so we can access the file through Java - if (Shell.rootAccess()) { - // Get non-root UID - int uid = mm.getApplicationInfo().uid; - // Get app selabel - String selabel = getShell().su("/system/bin/ls -dZ " + install + " | cut -d' ' -f1").get(0); - getShell().su( - "chcon " + selabel + " " + patched_boot, - "chown " + uid + "." + uid + " " + patched_boot); - } TarOutputStream tar = new TarOutputStream(new BufferedOutputStream(new FileOutputStream(dest))); tar.putNextEntry(new TarEntry(patched_boot, "boot.img")); byte buffer[] = new byte[4096]; diff --git a/app/src/main/java/com/topjohnwu/magisk/utils/Shell.java b/app/src/main/java/com/topjohnwu/magisk/utils/Shell.java index a674e3abf..822b6cbdd 100644 --- a/app/src/main/java/com/topjohnwu/magisk/utils/Shell.java +++ b/app/src/main/java/com/topjohnwu/magisk/utils/Shell.java @@ -87,10 +87,38 @@ public class Shell { sh_raw("umask 022"); } + private Shell(String command) { + Process process; + DataOutputStream in; + DataInputStream out; + + try { + process = Runtime.getRuntime().exec(command); + in = new DataOutputStream(process.getOutputStream()); + out = new DataInputStream(process.getInputStream()); + } catch (IOException e) { + // Nothing works.... + shellProcess = null; + STDIN = null; + STDOUT = null; + isValid = false; + return; + } + + isValid = true; + shellProcess = process; + STDIN = in; + STDOUT = out; + } + public static Shell getShell() { return new Shell(); } + public static Shell getShell(String command) { + return new Shell(command); + } + public static Shell getShell(Context context) { MagiskManager magiskManager = Utils.getMagiskManager(context); if (magiskManager.shell == null || !magiskManager.shell.isValid) {