From d065a7ca22e1d0ffd8c121f35aa3f3466db1521d Mon Sep 17 00:00:00 2001 From: tiann Date: Tue, 11 Apr 2023 12:56:07 +0800 Subject: [PATCH] manager: don't create root shell everytime. this should close #361. MeiZu kernel seems cache something in execve syscall, which will cause double free in kernel. --- .../java/me/weishu/kernelsu/ui/util/KsuCli.kt | 20 +++++++++++++------ .../me/weishu/kernelsu/ui/util/LogEvent.kt | 2 +- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/manager/app/src/main/java/me/weishu/kernelsu/ui/util/KsuCli.kt b/manager/app/src/main/java/me/weishu/kernelsu/ui/util/KsuCli.kt index 6b803c74..bb17a037 100644 --- a/manager/app/src/main/java/me/weishu/kernelsu/ui/util/KsuCli.kt +++ b/manager/app/src/main/java/me/weishu/kernelsu/ui/util/KsuCli.kt @@ -21,6 +21,14 @@ private fun getKsuDaemonPath(): String { return ksuApp.applicationInfo.nativeLibraryDir + File.separator + "libksud.so" } +object KsuCli { + val SHELL: Shell = createRootShell() +} + +fun getRootShell(): Shell { + return KsuCli.SHELL +} + fun createRootShell(): Shell { Shell.enableVerboseLogging = BuildConfig.DEBUG val builder = Shell.Builder.create() @@ -33,7 +41,7 @@ fun createRootShell(): Shell { } fun execKsud(args: String): Boolean { - val shell = createRootShell() + val shell = getRootShell() return ShellUtils.fastCmdResult(shell, "${getKsuDaemonPath()} $args") } @@ -44,7 +52,7 @@ fun install() { } fun listModules(): String { - val shell = createRootShell() + val shell = getRootShell() val out = shell.newJob().add("${getKsuDaemonPath()} module list").to(ArrayList(), null).exec().out return out.joinToString("\n").ifBlank { "[]" } @@ -77,7 +85,7 @@ fun installModule(uri: Uri, onFinish: (Boolean)->Unit, onOutput: (String) -> Uni } val cmd = "module install ${file.absolutePath}" - val shell = createRootShell() + val shell = getRootShell() val callbackList: CallbackList = object : CallbackList() { override fun onAddElement(s: String?) { @@ -96,7 +104,7 @@ fun installModule(uri: Uri, onFinish: (Boolean)->Unit, onOutput: (String) -> Uni } fun reboot(reason: String = "") { - val shell = createRootShell() + val shell = getRootShell() if (reason == "recovery") { // KEYCODE_POWER = 26, hide incorrect "Factory data reset" message ShellUtils.fastCmd(shell, "/system/bin/input keyevent 26") @@ -105,13 +113,13 @@ fun reboot(reason: String = "") { } fun overlayFsAvailable(): Boolean { - val shell = createRootShell() + val shell = getRootShell() // check /proc/filesystems return ShellUtils.fastCmdResult(shell, "cat /proc/filesystems | grep overlay") } fun hasMagisk(): Boolean { - val shell = createRootShell() + val shell = getRootShell() val result = shell.newJob().add("nsenter --mount=/proc/1/ns/mnt which magisk").exec() Log.i(TAG, "has magisk: ${result.isSuccess}") return result.isSuccess diff --git a/manager/app/src/main/java/me/weishu/kernelsu/ui/util/LogEvent.kt b/manager/app/src/main/java/me/weishu/kernelsu/ui/util/LogEvent.kt index c8276b6d..28e75245 100644 --- a/manager/app/src/main/java/me/weishu/kernelsu/ui/util/LogEvent.kt +++ b/manager/app/src/main/java/me/weishu/kernelsu/ui/util/LogEvent.kt @@ -28,7 +28,7 @@ fun getBugreportFile(context: Context): File { val appListFile = File(bugreportDir, "app_list.txt") val propFile = File(bugreportDir, "props.txt") - val shell = createRootShell() + val shell = getRootShell() shell.newJob().add("dmesg > ${dmesgFile.absolutePath}").exec() shell.newJob().add("logcat -d > ${logcatFile.absolutePath}").exec()