Prevent polluting global shell env

This commit is contained in:
topjohnwu 2024-07-01 18:20:21 -07:00
parent 966c6314f8
commit 1fbd053a42
4 changed files with 79 additions and 48 deletions

View File

@ -7,8 +7,11 @@ import com.topjohnwu.magisk.core.di.AppContext
import com.topjohnwu.magisk.core.ktx.getProperty
import com.topjohnwu.magisk.core.model.UpdateInfo
import com.topjohnwu.magisk.core.repository.NetworkService
import com.topjohnwu.superuser.CallbackList
import com.topjohnwu.superuser.Shell
import com.topjohnwu.superuser.ShellUtils.fastCmd
import com.topjohnwu.superuser.ShellUtils.fastCmdResult
import kotlinx.coroutines.Runnable
val isRunningAsStub get() = Info.stub != null
@ -24,19 +27,25 @@ object Info {
} else remote
}
// Device state
var isRooted = false
var noDataExec = false
var patchBootVbmeta = false
@JvmStatic var env = Env()
private set
@JvmField var isSAR = false
@JvmStatic var isSAR = false
private set
var legacySAR = false
private set
var isAB = false
private set
var slot = ""
private set
@JvmField val isZygiskEnabled = System.getenv("ZYGISK_ENABLED") == "1"
@JvmStatic val isFDE get() = crypto == "block"
@JvmField var ramdisk = false
var patchBootVbmeta = false
var crypto = ""
var noDataExec = false
var isRooted = false
@JvmStatic var ramdisk = false
private set
private var crypto = ""
var hasGMS = true
val isEmulator =
@ -69,12 +78,39 @@ object Info {
fun init(shell: Shell) {
if (shell.isRoot) {
isRooted = true
val v = fastCmd(shell, "magisk -v").split(":".toRegex())
val v = fastCmd(shell, "magisk -v").split(":")
env = Env(
v[0], v.size >= 3 && v[2] == "D",
runCatching { fastCmd("magisk -V").toInt() }.getOrDefault(-1)
)
Config.denyList = fastCmdResult(shell, "magisk --denylist status")
}
val map = mutableMapOf<String, String>()
val list = object : CallbackList<String>(Runnable::run) {
override fun onAddElement(e: String) {
val split = e.split("=")
if (split.size >= 2) {
map[split[0]] = split[1]
}
}
}
shell.newJob().add("(app_init)").to(list).exec()
fun getVar(name: String) = map[name] ?: ""
fun getBool(name: String) = map[name].toBoolean()
isSAR = getBool("SYSTEM_AS_ROOT")
ramdisk = getBool("RAMDISKEXIST")
isAB = getBool("ISAB")
patchBootVbmeta = getBool("PATCHVBMETAFLAG")
crypto = getVar("CRYPTOTYPE")
slot = getVar("SLOT")
legacySAR = getBool("LEGACYSAR")
// Default presets
Config.recovery = getBool("RECOVERYMODE")
Config.keepVerity = getBool("KEEPVERITY")
Config.keepEnc = getBool("KEEPFORCEENCRYPT")
}
}

View File

@ -82,8 +82,11 @@ abstract class MagiskInstallImpl protected constructor(
}
}
private fun findImage(): Boolean {
val bootPath = "RECOVERYMODE=${Config.recovery} find_boot_image; echo \"\$BOOTIMAGE\"".fsh()
private fun findImage(slot: String): Boolean {
val bootPath = (
"(RECOVERYMODE=${Config.recovery} " +
"SLOT=$slot find_boot_image; " +
"echo \$BOOTIMAGE)").fsh()
if (bootPath.isEmpty()) {
console.add("! Unable to detect target image")
return false
@ -93,22 +96,14 @@ abstract class MagiskInstallImpl protected constructor(
return true
}
private fun findImage(): Boolean {
return findImage(Info.slot)
}
private fun findSecondary(): Boolean {
val slot = "echo \$SLOT".fsh()
val target = if (slot == "_a") "_b" else "_a"
console.add("- Target slot: $target")
val bootPath = arrayOf(
"SLOT=$target",
"find_boot_image",
"SLOT=$slot",
"echo \"\$BOOTIMAGE\"").fsh()
if (bootPath.isEmpty()) {
console.add("! Unable to detect target image")
return false
}
srcBoot = rootFS.getFile(bootPath)
console.add("- Target image: $bootPath")
return true
val slot = if (Info.slot == "_a") "_b" else "_a"
console.add("- Target slot: $slot")
return findImage(slot)
}
private suspend fun extractFiles(): Boolean {

View File

@ -20,8 +20,8 @@ import java.util.jar.JarFile
class ShellInit : Shell.Initializer() {
override fun onInit(context: Context, shell: Shell): Boolean {
Info.init(shell)
if (shell.isRoot) {
Info.isRooted = true
RootUtils.bindTask?.let { shell.execTask(it) }
RootUtils.bindTask = null
}
@ -47,7 +47,8 @@ class ShellInit : Shell.Initializer() {
if (shell.isRoot) {
add("export MAGISKTMP=\$(magisk --path)")
// Test if we can properly execute stuff in /data
Info.noDataExec = !shell.newJob().add("$localBB sh -c \"$localBB true\"").exec().isSuccess
Info.noDataExec = !shell.newJob()
.add("$localBB sh -c '$localBB true'").exec().isSuccess
}
if (Info.noDataExec) {
@ -70,26 +71,9 @@ class ShellInit : Shell.Initializer() {
if (shell.isRoot) {
add(context.assets.open("util_functions.sh"))
}
add("app_init")
}.exec()
fun fastCmd(cmd: String) = ShellUtils.fastCmd(shell, cmd)
fun getVar(name: String) = fastCmd("echo \$$name")
fun getBool(name: String) = getVar(name).toBoolean()
Info.isSAR = getBool("SYSTEM_AS_ROOT")
Info.ramdisk = getBool("RAMDISKEXIST")
Info.isAB = getBool("ISAB")
Info.crypto = getVar("CRYPTOTYPE")
Info.patchBootVbmeta = getBool("PATCHVBMETAFLAG")
Info.legacySAR = getBool("LEGACYSAR")
// Default presets
Config.recovery = getBool("RECOVERYMODE")
Config.keepVerity = getBool("KEEPVERITY")
Config.keepEnc = getBool("KEEPFORCEENCRYPT")
Config.denyList = shell.newJob().add("magisk --denylist status").exec().isSuccess
Info.init(shell)
return true
}
}

View File

@ -186,6 +186,10 @@ check_encryption() {
fi
}
printvar() {
eval echo $1=\$$1
}
##########################
# Non-root util_functions
##########################
@ -224,13 +228,25 @@ grep_prop() { return; }
#############
app_init() {
mount_partitions
mount_partitions >/dev/null
RAMDISKEXIST=false
check_boot_ramdisk && RAMDISKEXIST=true
get_flags
get_flags >/dev/null
run_migrations
SHA1=$(grep_prop SHA1 $MAGISKTMP/.magisk/config)
check_encryption
# Dump variables
printvar SLOT
printvar SYSTEM_AS_ROOT
printvar RAMDISKEXIST
printvar ISAB
printvar CRYPTOTYPE
printvar PATCHVBMETAFLAG
printvar LEGACYSAR
printvar RECOVERYMODE
printvar KEEPVERITY
printvar KEEPFORCEENCRYPT
}
export BOOTMODE=true