Do not readlink /proc/self/exe

Old platforms prevent `readlink /proc/self/exe` from being called from non-main threads on release builds

https://stackoverflow.com/questions/28590831/android-permission-denied-when-reading-proc-self-exe-from-non-main-thread
This commit is contained in:
canyie 2023-01-02 23:07:33 +08:00 committed by John Wu
parent 9cb4862248
commit 9043cd5c46
2 changed files with 23 additions and 1 deletions

View File

@ -32,6 +32,7 @@ import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
@ -180,4 +181,24 @@ public final class Utils {
public static boolean isMainShellRoot() {
return MainShell.get().isRoot();
}
@SuppressLint("DiscouragedPrivateApi")
public static boolean isProcess64Bit() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
return Process.is64Bit();
}
try {
Class<?> classVMRuntime = Class.forName("dalvik.system.VMRuntime");
Method getRuntime = classVMRuntime.getDeclaredMethod("getRuntime");
getRuntime.setAccessible(true);
Object runtime = getRuntime.invoke(null);
Method is64Bit = classVMRuntime.getDeclaredMethod("is64Bit");
is64Bit.setAccessible(true);
// noinspection ConstantConditions
return (boolean) is64Bit.invoke(runtime);
} catch (ReflectiveOperationException e) {
err(e);
return false;
}
}
}

View File

@ -215,7 +215,8 @@ public class RootServiceManager implements Handler.Callback {
break;
}
String app_process = new File("/proc/self/exe").getCanonicalPath();
// We cannot readlink /proc/self/exe on old kernels
String app_process = "/system/bin/app_process" + (Utils.isProcess64Bit() ? "64" : "32");
String cmd = String.format(Locale.ROOT,
"(%s CLASSPATH=%s %s %s /system/bin %s " +
"com.topjohnwu.superuser.internal.RootServerMain '%s' %d %s >/dev/null 2>&1)&",