mirror of
https://github.com/topjohnwu/Magisk.git
synced 2024-12-26 13:07:39 +00:00
Improve Shell and logging
This commit is contained in:
parent
9ebe372a9a
commit
ae05dce958
@ -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));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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) {
|
||||||
|
try {
|
||||||
StreamGobbler out = new StreamGobbler(STDOUT, output);
|
StreamGobbler out = new StreamGobbler(STDOUT, output);
|
||||||
out.start();
|
out.start();
|
||||||
run_raw(true, commands);
|
run_raw(true, commands);
|
||||||
run_raw(true, "echo \'-shell-done-\'");
|
STDIN.write("echo \'-shell-done-\'\n".getBytes("UTF-8"));
|
||||||
try { out.join(); } catch (InterruptedException ignored) {}
|
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,6 +122,7 @@ public class Shell {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void loadInputStream(InputStream in) {
|
public void loadInputStream(InputStream in) {
|
||||||
|
synchronized (process) {
|
||||||
try {
|
try {
|
||||||
int read;
|
int read;
|
||||||
byte[] bytes = new byte[4096];
|
byte[] bytes = new byte[4096];
|
||||||
@ -124,6 +134,7 @@ public class Shell {
|
|||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static List<String> sh(String... commands) {
|
public static List<String> sh(String... commands) {
|
||||||
List<String> res = new ArrayList<>();
|
List<String> res = new ArrayList<>();
|
||||||
|
@ -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
|
||||||
|
Loading…
x
Reference in New Issue
Block a user