Root shell with no outputs

This commit is contained in:
topjohnwu
2017-07-16 03:32:29 +08:00
parent 87ea2a2bef
commit f4097a372b
9 changed files with 47 additions and 37 deletions
@@ -18,12 +18,12 @@ public class Shell {
// -1 = problematic/unknown issue; 0 = not rooted; 1 = properly rooted
public static int rootStatus;
private static boolean isInit = false;
private final Process rootShell;
private final DataOutputStream rootSTDIN;
private final DataInputStream rootSTDOUT;
private boolean isValid;
private Shell() {
Process process;
try {
@@ -34,15 +34,17 @@ public class Shell {
rootShell = null;
rootSTDIN = null;
rootSTDOUT = null;
isValid = false;
return;
}
rootStatus = 1;
isValid = true;
rootShell = process;
rootSTDIN = new DataOutputStream(rootShell.getOutputStream());
rootSTDOUT = new DataInputStream(rootShell.getInputStream());
su("umask 022");
su_raw("umask 022");
List<String> ret = su("echo -BOC-", "id");
if (ret.isEmpty()) {
@@ -118,33 +120,43 @@ public class Shell {
}
public List<String> su(String... commands) {
if (!isValid) return null;
List<String> res = new ArrayList<>();
su(res, commands);
return res;
}
public void su(List<String> res, String... commands) {
try {
rootShell.exitValue();
return; // The process is dead, return
} catch (IllegalThreadStateException ignored) {
// This should be the expected result
}
public void su_raw(String... commands) {
if (!isValid) return;
synchronized (rootShell) {
StreamGobbler STDOUT = new StreamGobbler(rootSTDOUT, Collections.synchronizedList(res), true);
STDOUT.start();
try {
for (String command : commands) {
rootSTDIN.write((command + "\n").getBytes("UTF-8"));
rootSTDIN.flush();
}
rootSTDIN.write(("echo \'-root-done-\'\n").getBytes("UTF-8"));
rootSTDIN.flush();
STDOUT.join();
} catch (InterruptedException | IOException e) {
} catch (IOException e) {
e.printStackTrace();
rootShell.destroy();
isValid = false;
}
}
}
public void su(List<String> output, String... commands) {
if (!isValid) return;
try {
rootShell.exitValue();
isValid = false;
return; // The process is dead, return
} catch (IllegalThreadStateException ignored) {
// This should be the expected result
}
synchronized (rootShell) {
StreamGobbler STDOUT = new StreamGobbler(rootSTDOUT, output, true);
STDOUT.start();
su_raw(commands);
su_raw("echo \'-root-done-\'");
try { STDOUT.join(); } catch (InterruptedException ignored) {}
}
}
}
@@ -29,13 +29,17 @@ public class StreamGobbler extends Thread {
* @param outputList {@literal List<String>} to write to, or null
*/
public StreamGobbler(InputStream inputStream, List<String> outputList) {
try {
while (inputStream.available() != 0) {
inputStream.skip(inputStream.available());
}
} catch (IOException ignored) {}
reader = new BufferedReader(new InputStreamReader(inputStream));
writer = outputList;
}
public StreamGobbler(InputStream inputStream, List<String> outputList, boolean root) {
reader = new BufferedReader(new InputStreamReader(inputStream));
writer = outputList;
this(inputStream, outputList);
isRoot = root;
}
@@ -48,7 +52,7 @@ public class StreamGobbler extends Thread {
if (TextUtils.equals(line, "-root-done-"))
return;
writer.add(line);
Logger.shell(isRoot, "OUT: " + line);
Logger.shell(isRoot, line);
}
} catch (IOException e) {
// reader probably closed, expected exit condition
@@ -52,12 +52,12 @@ public class Utils {
public static void createFile(Shell shell, String path) {
String folder = path.substring(0, path.lastIndexOf('/'));
String command = "mkdir -p " + folder + " 2>/dev/null; touch " + path + " 2>/dev/null; if [ -f \"" + path + "\" ]; then echo true; else echo false; fi";
shell.su(command);
shell.su_raw(command);
}
public static void removeItem(Shell shell, String path) {
String command = "rm -rf " + path + " 2>/dev/null; if [ -e " + path + " ]; then echo false; else echo true; fi";
shell.su(command);
shell.su_raw(command);
}
public static List<String> getModList(Shell shell, String path) {
@@ -66,14 +66,8 @@ public class Utils {
}
public static List<String> readFile(Shell shell, String path) {
List<String> ret;
String command = "cat " + path;
if (Shell.rootAccess()) {
ret = shell.su(command);
} else {
ret = Shell.sh(command);
}
return ret;
return shell.su(command);
}
public static void dlAndReceive(Context context, DownloadReceiver receiver, String link, String filename) {