Add STDERR support

This commit is contained in:
topjohnwu 2017-11-18 01:16:14 +08:00
parent b570cb5b77
commit 49c672ac4d
4 changed files with 41 additions and 38 deletions

View File

@ -168,19 +168,18 @@ public class InstallMagisk extends ParallelTask<Void, Void, Boolean> {
shell = Shell.getShell(); shell = Shell.getShell();
// Patch boot image // Patch boot image
shell.run(mList, shell.run(mList, null,
"cd " + install, "cd " + install,
"KEEPFORCEENCRYPT=" + mKeepEnc + " KEEPVERITY=" + mKeepVerity + " sh " + "KEEPFORCEENCRYPT=" + mKeepEnc + " KEEPVERITY=" + mKeepVerity + " sh " +
"update-binary indep boot_patch.sh " + boot + " || echo 'Failed!'" "update-binary indep boot_patch.sh " + boot + " || echo 'Failed!'");
);
if (TextUtils.equals(mList.get(mList.size() - 1), "Failed!")) if (TextUtils.equals(mList.get(mList.size() - 1), "Failed!"))
return false; return false;
shell.run(mList, shell.run(null, null,
"mv -f new-boot.img ../ 2>/dev/null", "mv -f new-boot.img ../",
"mv bin/busybox busybox 2>/dev/null", "mv bin/busybox busybox",
"rm -rf bin *.img update-binary 2>/dev/null", "rm -rf bin *.img update-binary",
"cd /"); "cd /");
File patched_boot = new File(install.getParent(), "new-boot.img"); File patched_boot = new File(install.getParent(), "new-boot.img");
@ -197,7 +196,7 @@ public class InstallMagisk extends ParallelTask<Void, Void, Boolean> {
) { ) {
SignBoot.doSignature("/boot", in, out, keyIn, certIn); SignBoot.doSignature("/boot", in, out, keyIn, certIn);
} }
shell.run_raw(false, "mv -f " + signed + " " + patched_boot); shell.run_raw(false, false, "mv -f " + signed + " " + patched_boot);
} }
switch (mode) { switch (mode) {
@ -206,7 +205,7 @@ public class InstallMagisk extends ParallelTask<Void, Void, Boolean> {
dest.getParentFile().mkdirs(); dest.getParentFile().mkdirs();
switch (mm.bootFormat) { switch (mm.bootFormat) {
case ".img": case ".img":
shell.run_raw(false, "cp -f " + patched_boot + " " + dest); shell.run_raw(false, false, "cp -f " + patched_boot + " " + dest);
break; break;
case ".img.tar": case ".img.tar":
try ( try (

View File

@ -24,7 +24,7 @@ public class Const {
public static final String MAGISK_DISABLE_FILE = "/cache/.disable_magisk"; public static final String MAGISK_DISABLE_FILE = "/cache/.disable_magisk";
public static final String TMP_FOLDER_PATH = "/dev/tmp"; public static final String TMP_FOLDER_PATH = "/dev/tmp";
public static final String MAGISK_LOG = "/cache/magisk.log"; public static final String MAGISK_LOG = "/cache/magisk.log";
public static final String BUSYBOXPATH = "/dev/magisk/bin"; public static final String BUSYBOX_PATH = "/dev/magisk/bin";
public static final File EXTERNAL_PATH = new File(Environment.getExternalStorageDirectory(), "MagiskManager"); public static final File EXTERNAL_PATH = new File(Environment.getExternalStorageDirectory(), "MagiskManager");
public static String MAGISK_PATH() { public static String MAGISK_PATH() {

View File

@ -25,6 +25,7 @@ public class Shell {
private final Process process; private final Process process;
private final OutputStream STDIN; private final OutputStream STDIN;
private final InputStream STDOUT; private final InputStream STDOUT;
private final InputStream STDERR;
private static void testRootShell(Shell shell) throws IOException { private static void testRootShell(Shell shell) throws IOException {
shell.STDIN.write(("id\n").getBytes("UTF-8")); shell.STDIN.write(("id\n").getBytes("UTF-8"));
@ -41,6 +42,7 @@ public class Shell {
process = Runtime.getRuntime().exec(command); process = Runtime.getRuntime().exec(command);
STDIN = process.getOutputStream(); STDIN = process.getOutputStream();
STDOUT = process.getInputStream(); STDOUT = process.getInputStream();
STDERR = process.getErrorStream();
} }
public static Shell getShell() { public static Shell getShell() {
@ -87,12 +89,11 @@ public class Shell {
} }
// Root shell initialization // Root shell initialization
mm.shell.run_raw(false, mm.shell.run_raw(false, false,
"export PATH=" + Const.BUSYBOXPATH + ":$PATH", "export PATH=" + Const.BUSYBOX_PATH + ":$PATH",
"mount_partitions", "mount_partitions",
"find_boot_image", "find_boot_image",
"migrate_boot_backup" "migrate_boot_backup");
);
} }
} }
@ -104,16 +105,20 @@ public class Shell {
return status > 0; return status > 0;
} }
public void run(Collection<String> output, String... commands) { public void run(Collection<String> output, Collection<String> error, String... commands) {
StreamGobbler out, err;
synchronized (process) { synchronized (process) {
try { try {
StreamGobbler out = new StreamGobbler(STDOUT, output); out = new StreamGobbler(STDOUT, output);
err = new StreamGobbler(STDERR, error);
out.start(); out.start();
run_raw(true, commands); err.start();
STDIN.write("echo \'-shell-done-\'\n".getBytes("UTF-8")); run_raw(output != null, error != null, commands);
STDIN.write("echo \'-shell-done-\'\necho \'-shell-done-\' >&2\n".getBytes("UTF-8"));
STDIN.flush(); STDIN.flush();
try { try {
out.join(); out.join();
err.join();
} catch (InterruptedException ignored) {} } catch (InterruptedException ignored) {}
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
@ -122,12 +127,15 @@ public class Shell {
} }
} }
public void run_raw(boolean stdout, String... commands) { public void run_raw(boolean stdout, boolean stderr, String... commands) {
String suffix = "\n";
if (!stderr) suffix = " 2>/dev/null" + suffix;
if (!stdout) suffix = " >/dev/null" + suffix;
synchronized (process) { synchronized (process) {
try { try {
for (String command : commands) { for (String command : commands) {
Logger.shell(true, command); Logger.shell(true, command);
STDIN.write((command + (stdout ? "\n" : " >/dev/null\n")).getBytes("UTF-8")); STDIN.write((command + suffix).getBytes("UTF-8"));
STDIN.flush(); STDIN.flush();
} }
} catch (IOException e) { } catch (IOException e) {
@ -140,12 +148,7 @@ public class Shell {
public void loadInputStream(InputStream in) { public void loadInputStream(InputStream in) {
synchronized (process) { synchronized (process) {
try { try {
int read; Utils.inToOut(in, STDIN);
byte[] bytes = new byte[4096];
while ((read = in.read(bytes)) != -1) {
STDIN.write(bytes, 0, read);
}
STDIN.flush();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -162,14 +165,14 @@ public class Shell {
Shell shell = getShell(); Shell shell = getShell();
if (shell == null) if (shell == null)
return; return;
shell.run(output, commands); shell.run(output, null, commands);
} }
public static void sh_raw(String... commands) { public static void sh_raw(String... commands) {
Shell shell = getShell(); Shell shell = getShell();
if (shell == null) if (shell == null)
return; return;
shell.run_raw(false, commands); shell.run_raw(false, false, commands);
} }
public static List<String> su(String... commands) { public static List<String> su(String... commands) {

View File

@ -7,6 +7,7 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.util.Collection; import java.util.Collection;
import java.util.Collections;
/** /**
* Modified by topjohnwu, based on Chainfire's libsuperuser * Modified by topjohnwu, based on Chainfire's libsuperuser
@ -14,8 +15,8 @@ import java.util.Collection;
public class StreamGobbler extends Thread { public class StreamGobbler extends Thread {
private BufferedReader reader = null; private BufferedReader reader;
private Collection<String> writer = null; private Collection<String> writer;
/** /**
* <p>StreamGobbler constructor</p> * <p>StreamGobbler constructor</p>
@ -24,17 +25,17 @@ public class StreamGobbler extends Thread {
* possible to prevent a deadlock from occurring, or Process.waitFor() never * possible to prevent a deadlock from occurring, or Process.waitFor() never
* returning (as the buffer is full, pausing the native process)</p> * returning (as the buffer is full, pausing the native process)</p>
* *
* @param inputStream InputStream to read from * @param in InputStream to read from
* @param outputList {@literal List<String>} to write to, or null * @param out {@literal List<String>} to write to, or null
*/ */
public StreamGobbler(InputStream inputStream, Collection<String> outputList) { public StreamGobbler(InputStream in, Collection<String> out) {
try { try {
while (inputStream.available() != 0) { while (in.available() != 0) {
inputStream.skip(inputStream.available()); in.skip(in.available());
} }
} catch (IOException ignored) {} } catch (IOException ignored) {}
reader = new BufferedReader(new InputStreamReader(inputStream)); reader = new BufferedReader(new InputStreamReader(in));
writer = outputList; writer = out == null ? null : Collections.synchronizedCollection(out);
} }
@Override @Override
@ -45,7 +46,7 @@ public class StreamGobbler extends Thread {
while ((line = reader.readLine()) != null) { while ((line = reader.readLine()) != null) {
if (TextUtils.equals(line, "-shell-done-")) if (TextUtils.equals(line, "-shell-done-"))
return; return;
writer.add(line); if (writer != null) writer.add(line);
Logger.shell(false, line); Logger.shell(false, line);
} }
} catch (IOException e) { } catch (IOException e) {