Improve Shell and logging

This commit is contained in:
topjohnwu 2017-10-19 20:37:58 +08:00
parent 9ebe372a9a
commit ae05dce958
3 changed files with 32 additions and 23 deletions

View File

@ -2,8 +2,6 @@ package com.topjohnwu.magisk.utils;
import android.util.Log; import android.util.Log;
import com.topjohnwu.magisk.MagiskManager;
import java.util.Locale; import java.util.Locale;
public class Logger { public class Logger {
@ -27,13 +25,13 @@ public class Logger {
error(String.format(Locale.US, fmt, args)); error(String.format(Locale.US, fmt, args));
} }
public static void shell(String line) { public static void shell(boolean in, String line) {
if (SHELL_LOGGING) { 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) { public static void shell(boolean in, String fmt, Object... args) {
shell(String.format(Locale.US, fmt, args)); shell(in, String.format(Locale.US, fmt, args));
} }
} }

View File

@ -19,8 +19,8 @@ import java.util.List;
public class Shell { public class Shell {
// -1 = no shell; 0 = non root shell; 1 = root shell // -2 = not initialized; -1 = no shell; 0 = non root shell; 1 = root shell
public static int status; public static int status = -2;
private final Process process; private final Process process;
private final OutputStream STDIN; private final OutputStream STDIN;
@ -84,16 +84,25 @@ public class Shell {
} }
public static boolean rootAccess() { public static boolean rootAccess() {
if (status == -2) getShell();
return status > 0; return status > 0;
} }
public void run(Collection<String> output, String... commands) { public void run(Collection<String> output, String... commands) {
synchronized (process) { synchronized (process) {
StreamGobbler out = new StreamGobbler(STDOUT, output); try {
out.start(); StreamGobbler out = new StreamGobbler(STDOUT, output);
run_raw(true, commands); out.start();
run_raw(true, "echo \'-shell-done-\'"); run_raw(true, commands);
try { out.join(); } catch (InterruptedException ignored) {} 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) { synchronized (process) {
try { try {
for (String command : commands) { for (String command : commands) {
Logger.shell(command); Logger.shell(true, command);
STDIN.write((command + (stdout ? "\n" : " >/dev/null\n")).getBytes("UTF-8")); STDIN.write((command + (stdout ? "\n" : " >/dev/null\n")).getBytes("UTF-8"));
STDIN.flush(); STDIN.flush();
} }
@ -113,15 +122,17 @@ public class Shell {
} }
public void loadInputStream(InputStream in) { public void loadInputStream(InputStream in) {
try { synchronized (process) {
int read; try {
byte[] bytes = new byte[4096]; int read;
while ((read = in.read(bytes)) != -1) { byte[] bytes = new byte[4096];
STDIN.write(bytes, 0, read); 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();
} }
} }

View File

@ -46,7 +46,7 @@ public class StreamGobbler extends Thread {
if (TextUtils.equals(line, "-shell-done-")) if (TextUtils.equals(line, "-shell-done-"))
return; return;
writer.add(line); writer.add(line);
Logger.shell(line); Logger.shell(false, line);
} }
} catch (IOException e) { } catch (IOException e) {
// reader probably closed, expected exit condition // reader probably closed, expected exit condition