From 45fa1fce7009a43637d621411b8b95891bbcdfdc Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Fri, 28 Jun 2024 15:44:42 -0700 Subject: [PATCH] Update libsu --- app/build.gradle.kts | 2 +- .../java/com/topjohnwu/magisk/core/App.kt | 5 +- .../topjohnwu/magisk/core/su/TestHandler.kt | 52 ++++++++++++------- .../magisk/core/tasks/MagiskInstaller.kt | 8 +-- .../topjohnwu/magisk/core/utils/DummyList.kt | 16 ++++++ 5 files changed, 56 insertions(+), 27 deletions(-) create mode 100644 app/src/main/java/com/topjohnwu/magisk/core/utils/DummyList.kt diff --git a/app/build.gradle.kts b/app/build.gradle.kts index dc8065e36..4ad7327ea 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -80,7 +80,7 @@ dependencies { implementation("dev.rikka.rikkax.recyclerview:recyclerview-ktx:1.3.2") implementation("io.noties.markwon:core:4.6.2") - val vLibsu = "5.3.0" + val vLibsu = "6.0.0" implementation("com.github.topjohnwu.libsu:core:${vLibsu}") implementation("com.github.topjohnwu.libsu:service:${vLibsu}") implementation("com.github.topjohnwu.libsu:nio:${vLibsu}") diff --git a/app/src/main/java/com/topjohnwu/magisk/core/App.kt b/app/src/main/java/com/topjohnwu/magisk/core/App.kt index a92b2bb98..faf0bfdaa 100644 --- a/app/src/main/java/com/topjohnwu/magisk/core/App.kt +++ b/app/src/main/java/com/topjohnwu/magisk/core/App.kt @@ -68,11 +68,12 @@ open class App() : Application() { ServiceLocator.context = base app.registerActivityLifecycleCallbacks(ActivityTracker) - Shell.setDefaultBuilder(Shell.Builder.create() + val shellBuilder = Shell.Builder.create() .setFlags(Shell.FLAG_MOUNT_MASTER) .setInitializers(ShellInit::class.java) .setContext(base) - .setTimeout(2)) + .setTimeout(2) + Shell.setDefaultBuilder(shellBuilder) Shell.EXECUTOR = Dispatchers.IO.asExecutor() RootUtils.bindTask = RootService.bindOrTask( intent(), diff --git a/app/src/main/java/com/topjohnwu/magisk/core/su/TestHandler.kt b/app/src/main/java/com/topjohnwu/magisk/core/su/TestHandler.kt index 7ce724000..bdb070fc3 100644 --- a/app/src/main/java/com/topjohnwu/magisk/core/su/TestHandler.kt +++ b/app/src/main/java/com/topjohnwu/magisk/core/su/TestHandler.kt @@ -6,39 +6,49 @@ import com.topjohnwu.magisk.core.Info import com.topjohnwu.magisk.core.di.ServiceLocator import com.topjohnwu.magisk.core.tasks.MagiskInstaller import com.topjohnwu.magisk.core.utils.RootUtils +import com.topjohnwu.superuser.CallbackList import com.topjohnwu.superuser.Shell -import com.topjohnwu.superuser.internal.NOPList +import kotlinx.coroutines.Runnable import kotlinx.coroutines.runBlocking +import timber.log.Timber object TestHandler { + object LogList : CallbackList(Runnable::run) { + override fun onAddElement(e: String) { + Timber.i(e) + } + } + fun run(method: String): Bundle { - val r = Bundle() + var reason: String? = null + + fun prerequisite(): Boolean { + // Make sure the Magisk app can get root + val shell = Shell.getShell() + if (!shell.isRoot) { + reason = "shell not root" + return false + } + + // Make sure the root service is running + RootUtils.Connection.await() + return true + } fun setup(): Boolean { - val nop = NOPList.getInstance() return runBlocking { - MagiskInstaller.Emulator(nop, nop).exec() + MagiskInstaller.Emulator(LogList, LogList).exec() } } fun test(): Boolean { // Make sure Zygisk works correctly if (!Info.isZygiskEnabled) { - r.putString("reason", "zygisk not enabled") + reason = "zygisk not enabled" return false } - // Make sure the Magisk app can get root - val shell = Shell.getShell() - if (!shell.isRoot) { - r.putString("reason", "shell not root") - return false - } - - // Make sure the root service is running - RootUtils.Connection.await() - // Clear existing grant for ADB shell runBlocking { ServiceLocator.policyDB.delete(2000) @@ -48,21 +58,23 @@ object TestHandler { return true } - val b = runCatching { + val result = prerequisite() && runCatching { when (method) { "setup" -> setup() "test" -> test() else -> { - r.putString("reason", "unknown method") + reason = "unknown method" false } } }.getOrElse { - r.putString("reason", it.stackTraceToString()) + reason = it.stackTraceToString() false } - r.putBoolean("result", b) - return r + return Bundle().apply { + putBoolean("result", result) + if (reason != null) putString("reason", reason) + } } } diff --git a/app/src/main/java/com/topjohnwu/magisk/core/tasks/MagiskInstaller.kt b/app/src/main/java/com/topjohnwu/magisk/core/tasks/MagiskInstaller.kt index a13763301..37bd40b6a 100644 --- a/app/src/main/java/com/topjohnwu/magisk/core/tasks/MagiskInstaller.kt +++ b/app/src/main/java/com/topjohnwu/magisk/core/tasks/MagiskInstaller.kt @@ -23,13 +23,13 @@ import com.topjohnwu.magisk.core.ktx.copyAndClose import com.topjohnwu.magisk.core.ktx.reboot import com.topjohnwu.magisk.core.ktx.toast import com.topjohnwu.magisk.core.ktx.writeTo +import com.topjohnwu.magisk.core.utils.DummyList import com.topjohnwu.magisk.core.utils.MediaStoreUtils import com.topjohnwu.magisk.core.utils.MediaStoreUtils.inputStream import com.topjohnwu.magisk.core.utils.MediaStoreUtils.outputStream import com.topjohnwu.magisk.core.utils.RootUtils import com.topjohnwu.superuser.Shell import com.topjohnwu.superuser.ShellUtils -import com.topjohnwu.superuser.internal.NOPList import com.topjohnwu.superuser.internal.UiThreadHandler import com.topjohnwu.superuser.nio.ExtendedFile import com.topjohnwu.superuser.nio.FileSystemManager @@ -51,8 +51,8 @@ import java.util.zip.ZipFile import java.util.zip.ZipInputStream abstract class MagiskInstallImpl protected constructor( - protected val console: MutableList = NOPList.getInstance(), - private val logs: MutableList = NOPList.getInstance() + protected val console: MutableList, + private val logs: MutableList ) { protected lateinit var installDir: ExtendedFile @@ -625,7 +625,7 @@ abstract class MagiskInstaller( } } - class FixEnv(private val callback: () -> Unit) : MagiskInstallImpl() { + class FixEnv(private val callback: () -> Unit) : MagiskInstallImpl(DummyList, DummyList) { override suspend fun operations() = fixEnv() override suspend fun exec(): Boolean { diff --git a/app/src/main/java/com/topjohnwu/magisk/core/utils/DummyList.kt b/app/src/main/java/com/topjohnwu/magisk/core/utils/DummyList.kt new file mode 100644 index 000000000..4b23a8bf3 --- /dev/null +++ b/app/src/main/java/com/topjohnwu/magisk/core/utils/DummyList.kt @@ -0,0 +1,16 @@ +package com.topjohnwu.magisk.core.utils + +object DummyList : java.util.AbstractList() { + + override val size: Int get() = 0 + + override fun get(index: Int): String { + throw IndexOutOfBoundsException() + } + + override fun add(element: String): Boolean = false + + override fun add(index: Int, element: String) {} + + override fun clear() {} +}