From ae05dce958a45533f7e375650d467a547eb8afe0 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Thu, 19 Oct 2017 20:37:58 +0800 Subject: [PATCH] Improve Shell and logging --- .../com/topjohnwu/magisk/utils/Logger.java | 10 ++--- .../com/topjohnwu/magisk/utils/Shell.java | 43 ++++++++++++------- .../topjohnwu/magisk/utils/StreamGobbler.java | 2 +- 3 files changed, 32 insertions(+), 23 deletions(-) diff --git a/app/src/main/java/com/topjohnwu/magisk/utils/Logger.java b/app/src/main/java/com/topjohnwu/magisk/utils/Logger.java index f6fd8a5ce..70afb9944 100644 --- a/app/src/main/java/com/topjohnwu/magisk/utils/Logger.java +++ b/app/src/main/java/com/topjohnwu/magisk/utils/Logger.java @@ -2,8 +2,6 @@ package com.topjohnwu.magisk.utils; import android.util.Log; -import com.topjohnwu.magisk.MagiskManager; - import java.util.Locale; public class Logger { @@ -27,13 +25,13 @@ public class Logger { error(String.format(Locale.US, fmt, args)); } - public static void shell(String line) { + public static void shell(boolean in, String line) { if (SHELL_LOGGING) { - Log.d(DEBUG_TAG, "SHELL: " + line); + Log.d(DEBUG_TAG, (in ? "SHELLIN : " : "SHELLOUT: ") + line); } } - public static void shell(String fmt, Object... args) { - shell(String.format(Locale.US, fmt, args)); + public static void shell(boolean in, String fmt, Object... args) { + shell(in, String.format(Locale.US, fmt, args)); } } 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 f98ab61dc..d51a546bb 100644 --- a/app/src/main/java/com/topjohnwu/magisk/utils/Shell.java +++ b/app/src/main/java/com/topjohnwu/magisk/utils/Shell.java @@ -19,8 +19,8 @@ import java.util.List; public class Shell { - // -1 = no shell; 0 = non root shell; 1 = root shell - public static int status; + // -2 = not initialized; -1 = no shell; 0 = non root shell; 1 = root shell + public static int status = -2; private final Process process; private final OutputStream STDIN; @@ -84,16 +84,25 @@ public class Shell { } public static boolean rootAccess() { + if (status == -2) getShell(); return status > 0; } public void run(Collection output, String... commands) { synchronized (process) { - StreamGobbler out = new StreamGobbler(STDOUT, output); - out.start(); - run_raw(true, commands); - run_raw(true, "echo \'-shell-done-\'"); - try { out.join(); } catch (InterruptedException ignored) {} + try { + StreamGobbler out = new StreamGobbler(STDOUT, output); + out.start(); + run_raw(true, commands); + STDIN.write("echo \'-shell-done-\'\n".getBytes("UTF-8")); + STDIN.flush(); + try { + out.join(); + } catch (InterruptedException ignored) {} + } catch (IOException e) { + e.printStackTrace(); + process.destroy(); + } } } @@ -101,7 +110,7 @@ public class Shell { synchronized (process) { try { for (String command : commands) { - Logger.shell(command); + Logger.shell(true, command); STDIN.write((command + (stdout ? "\n" : " >/dev/null\n")).getBytes("UTF-8")); STDIN.flush(); } @@ -113,15 +122,17 @@ public class Shell { } public void loadInputStream(InputStream in) { - try { - int read; - byte[] bytes = new byte[4096]; - while ((read = in.read(bytes)) != -1) { - STDIN.write(bytes, 0, read); + synchronized (process) { + try { + int read; + byte[] bytes = new byte[4096]; + while ((read = in.read(bytes)) != -1) { + STDIN.write(bytes, 0, read); + } + STDIN.flush(); + } catch (IOException e) { + e.printStackTrace(); } - STDIN.flush(); - } catch (IOException e) { - e.printStackTrace(); } } diff --git a/app/src/main/java/com/topjohnwu/magisk/utils/StreamGobbler.java b/app/src/main/java/com/topjohnwu/magisk/utils/StreamGobbler.java index 3544cb422..c7e4b889b 100644 --- a/app/src/main/java/com/topjohnwu/magisk/utils/StreamGobbler.java +++ b/app/src/main/java/com/topjohnwu/magisk/utils/StreamGobbler.java @@ -46,7 +46,7 @@ public class StreamGobbler extends Thread { if (TextUtils.equals(line, "-shell-done-")) return; writer.add(line); - Logger.shell(line); + Logger.shell(false, line); } } catch (IOException e) { // reader probably closed, expected exit condition