[skip ci]: Reapply "manager: Optimized import, optimized all libsu shell calls, and fixed WebUI memory leaks Partial modification (#369)"

This reverts commit 37368fb41c.
This commit is contained in:
ShirkNeko
2025-09-12 21:58:26 +08:00
parent fad6420405
commit 0c578e4518

View File

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