diff --git a/manager/app/src/main/java/com/sukisu/ultra/ui/util/KsuCli.kt b/manager/app/src/main/java/com/sukisu/ultra/ui/util/KsuCli.kt index f12c06dd..1a5d99f4 100644 --- a/manager/app/src/main/java/com/sukisu/ultra/ui/util/KsuCli.kt +++ b/manager/app/src/main/java/com/sukisu/ultra/ui/util/KsuCli.kt @@ -33,6 +33,18 @@ private val ksuDaemonPath by lazy { "${ksuApp.applicationInfo.nativeLibraryDir}${File.separator}libzakozako.so" } + +object KsuCli { + val SHELL: Shell = createRootShell() + val GLOBAL_MNT_SHELL: Shell = createRootShell(true) +} + +fun getRootShell(globalMnt: Boolean = false): Shell { + return if (globalMnt) KsuCli.GLOBAL_MNT_SHELL else { + KsuCli.SHELL + } +} + inline fun withNewRootShell( globalMnt: Boolean = false, block: Shell.() -> T @@ -84,7 +96,7 @@ fun execKsud(args: String, newShell: Boolean = false): Boolean { ShellUtils.fastCmdResult(this, "$ksuDaemonPath $args") } } else { - ShellUtils.fastCmdResult("$ksuDaemonPath $args") + ShellUtils.fastCmdResult(getRootShell(), "$ksuDaemonPath $args") } } @@ -96,8 +108,9 @@ fun install() { } fun listModules(): String { + val shell = getRootShell() val out = - Shell.cmd("$ksuDaemonPath module list").to(ArrayList(), null).exec().out + shell.newJob().add("$ksuDaemonPath module list").to(ArrayList(), null).exec().out return out.joinToString("\n").ifBlank { "[]" } } @@ -311,17 +324,22 @@ fun installBoot( } fun reboot(reason: String = "") { + val shell = getRootShell() if (reason == "recovery") { // KEYCODE_POWER = 26, hide incorrect "Factory data reset" message - ShellUtils.fastCmdResult("/system/bin/input keyevent 26") + ShellUtils.fastCmd(shell, "/system/bin/input keyevent 26") } - ShellUtils.fastCmdResult("/system/bin/svc power reboot $reason || /system/bin/reboot $reason") + ShellUtils.fastCmd(shell, "/system/bin/svc power reboot $reason || /system/bin/reboot $reason") } -fun rootAvailable() = Shell.isAppGrantedRoot() == true +fun rootAvailable(): Boolean { + val shell = getRootShell() + return shell.isRoot +} fun isAbDevice(): Boolean { - return ShellUtils.fastCmd("getprop ro.build.ab_update").trim().toBoolean() + val shell = getRootShell() + return ShellUtils.fastCmd(shell, "getprop ro.build.ab_update").trim().toBoolean() } fun isInitBoot(): Boolean { @@ -329,77 +347,91 @@ fun isInitBoot(): Boolean { } suspend fun getCurrentKmi(): String = withContext(Dispatchers.IO) { + val shell = getRootShell() val cmd = "boot-info current-kmi" - ShellUtils.fastCmd("$ksuDaemonPath $cmd") + ShellUtils.fastCmd(shell, "$ksuDaemonPath $cmd") } suspend fun getSupportedKmis(): List = withContext(Dispatchers.IO) { + val shell = getRootShell() val cmd = "boot-info supported-kmi" - val out = Shell.cmd("$ksuDaemonPath $cmd").to(ArrayList(), null).exec().out + val out = shell.newJob().add("$ksuDaemonPath $cmd").to(ArrayList(), null).exec().out out.filter { it.isNotBlank() }.map { it.trim() } } fun hasMagisk(): Boolean { - val result = ShellUtils.fastCmdResult("which magisk") - Log.i(TAG, "has magisk: $result") - return result + val shell = getRootShell(true) + val result = shell.newJob().add("which magisk").exec() + Log.i(TAG, "has magisk: ${result.isSuccess}") + return result.isSuccess } fun isSepolicyValid(rules: String?): Boolean { if (rules == null) { return true } + val shell = getRootShell() val result = - Shell.cmd("$ksuDaemonPath sepolicy check '$rules'").to(ArrayList(), null) + shell.newJob().add("$ksuDaemonPath sepolicy check '$rules'").to(ArrayList(), null) .exec() return result.isSuccess } fun getSepolicy(pkg: String): String { + val shell = getRootShell() val result = - Shell.cmd("$ksuDaemonPath profile get-sepolicy $pkg").to(ArrayList(), null) + shell.newJob().add("$ksuDaemonPath profile get-sepolicy $pkg").to(ArrayList(), null) .exec() Log.i(TAG, "code: ${result.code}, out: ${result.out}, err: ${result.err}") return result.out.joinToString("\n") } fun setSepolicy(pkg: String, rules: String): Boolean { - val result = Shell.cmd("$ksuDaemonPath profile set-sepolicy $pkg '$rules'") + val shell = getRootShell() + val result = shell.newJob().add("$ksuDaemonPath profile set-sepolicy $pkg '$rules'") .to(ArrayList(), null).exec() Log.i(TAG, "set sepolicy result: ${result.code}") return result.isSuccess } fun listAppProfileTemplates(): List { - return Shell.cmd("$ksuDaemonPath profile list-templates").to(ArrayList(), null) + val shell = getRootShell() + return shell.newJob().add("$ksuDaemonPath profile list-templates").to(ArrayList(), null) .exec().out } fun getAppProfileTemplate(id: String): String { - return Shell.cmd("$ksuDaemonPath profile get-template '${id}'") + val shell = getRootShell() + return shell.newJob().add("$ksuDaemonPath profile get-template '${id}'") .to(ArrayList(), null).exec().out.joinToString("\n") } fun setAppProfileTemplate(id: String, template: String): Boolean { + val shell = getRootShell() val escapedTemplate = template.replace("\"", "\\\"") val cmd = """$ksuDaemonPath profile set-template "$id" "$escapedTemplate'"""" - return Shell.cmd(cmd) + return shell.newJob().add(cmd) .to(ArrayList(), null).exec().isSuccess } fun deleteAppProfileTemplate(id: String): Boolean { - return Shell.cmd("$ksuDaemonPath profile delete-template '${id}'") + val shell = getRootShell() + return shell.newJob().add("$ksuDaemonPath profile delete-template '${id}'") .to(ArrayList(), null).exec().isSuccess } fun forceStopApp(packageName: String) { - val result = Shell.cmd("am force-stop $packageName").exec() + val shell = getRootShell() + val result = shell.newJob().add("am force-stop $packageName").exec() Log.i(TAG, "force stop $packageName result: $result") } fun launchApp(packageName: String) { + + val shell = getRootShell() val result = - Shell.cmd("cmd package resolve-activity --brief $packageName | tail -n 1 | xargs cmd activity start-activity -n") + shell.newJob() + .add("cmd package resolve-activity --brief $packageName | tail -n 1 | xargs cmd activity start-activity -n") .exec() Log.i(TAG, "launch $packageName result: $result") } @@ -414,31 +446,45 @@ val suSFSDaemonPath by lazy { } fun getSuSFS(): String { - return ShellUtils.fastCmd("$suSFSDaemonPath support") + val shell = getRootShell() + val result = ShellUtils.fastCmd(shell, "$suSFSDaemonPath support") + return result } fun getSuSFSVersion(): String { - return ShellUtils.fastCmd("$suSFSDaemonPath version") + val shell = getRootShell() + val result = ShellUtils.fastCmd(shell, "$suSFSDaemonPath version") + return result } fun getSuSFSVariant(): String { - return ShellUtils.fastCmd("$suSFSDaemonPath variant") + val shell = getRootShell() + val result = ShellUtils.fastCmd(shell, "$suSFSDaemonPath variant") + return result } fun getSuSFSFeatures(): String { - return ShellUtils.fastCmd("$suSFSDaemonPath features") + val shell = getRootShell() + val result = ShellUtils.fastCmd(shell, "$suSFSDaemonPath features") + return result } fun susfsSUS_SU_0(): String { - return ShellUtils.fastCmd("$suSFSDaemonPath sus_su 0") + val shell = getRootShell() + val result = ShellUtils.fastCmd(shell, "$suSFSDaemonPath sus_su 0") + return result } fun susfsSUS_SU_2(): String { - return ShellUtils.fastCmd("$suSFSDaemonPath sus_su 2") + val shell = getRootShell() + val result = ShellUtils.fastCmd(shell, "$suSFSDaemonPath sus_su 2") + return result } fun susfsSUS_SU_Mode(): String { - return ShellUtils.fastCmd("$suSFSDaemonPath sus_su mode") + val shell = getRootShell() + val result = ShellUtils.fastCmd(shell, "$suSFSDaemonPath sus_su mode") + return result } val kpmmgrPath by lazy {