Magisk/app/src/main/java/com/topjohnwu/magisk/utils/Utils.java

57 lines
1.7 KiB
Java
Raw Normal View History

2016-08-22 21:18:28 +00:00
package com.topjohnwu.magisk.utils;
2016-08-22 19:50:46 +00:00
2016-08-22 08:09:36 +00:00
import java.util.List;
import eu.chainfire.libsuperuser.Shell;
2016-08-20 15:26:49 +00:00
public class Utils {
2016-08-22 17:44:34 +00:00
public static final String suPath = sh("getprop magisk.supath");
2016-08-22 19:50:46 +00:00
public static boolean rootAccess = false;
2016-08-22 17:44:34 +00:00
public static String sh(String... commands) {
List<String> result = Shell.SH.run(commands);
StringBuilder builder = new StringBuilder();
for (String s : result) {
builder.append(s);
}
return builder.toString();
}
public static String su(String... commands) {
List<String> result = Shell.run(Utils.suPath + "/su", commands, null, false);
2016-08-20 15:26:49 +00:00
2016-08-22 08:09:36 +00:00
StringBuilder builder = new StringBuilder();
for (String s : result) {
builder.append(s);
2016-08-20 15:26:49 +00:00
}
2016-08-22 08:09:36 +00:00
return builder.toString();
2016-08-20 15:26:49 +00:00
}
2016-08-22 21:18:28 +00:00
public static void checkRoot() {
2016-08-22 17:44:34 +00:00
String [] availableTestCommands = new String[] {"echo -BOC-", "id"};
List<String> ret = Shell.run(Utils.suPath + "/su", availableTestCommands, null, false);
if (ret == null)
2016-08-22 21:18:28 +00:00
return;
2016-08-22 17:44:34 +00:00
// Taken from libsuperuser
// this is only one of many ways this can be done
for (String line : ret) {
if (line.contains("uid=")) {
// id command is working, let's see if we are actually root
2016-08-22 21:18:28 +00:00
rootAccess = line.contains("uid=0");
2016-08-22 17:44:34 +00:00
} else if (line.contains("-BOC-")) {
// if we end up here, at least the su command starts some kind
// of shell, let's hope it has root privileges - no way to know without
// additional native binaries
2016-08-22 21:18:28 +00:00
rootAccess = true;
2016-08-22 17:44:34 +00:00
}
}
}
2016-08-20 15:26:49 +00:00
}