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.ktx.getProperty
import com.topjohnwu.magisk.core.model.UpdateInfo import com.topjohnwu.magisk.core.model.UpdateInfo
import com.topjohnwu.magisk.core.repository.NetworkService import com.topjohnwu.magisk.core.repository.NetworkService
import com.topjohnwu.superuser.CallbackList
import com.topjohnwu.superuser.Shell import com.topjohnwu.superuser.Shell
import com.topjohnwu.superuser.ShellUtils.fastCmd import com.topjohnwu.superuser.ShellUtils.fastCmd
import com.topjohnwu.superuser.ShellUtils.fastCmdResult
import kotlinx.coroutines.Runnable
val isRunningAsStub get() = Info.stub != null val isRunningAsStub get() = Info.stub != null
@ -24,19 +27,25 @@ object Info {
} else remote } else remote
} }
// Device state var isRooted = false
var noDataExec = false
var patchBootVbmeta = false
@JvmStatic var env = Env() @JvmStatic var env = Env()
private set private set
@JvmField var isSAR = false @JvmStatic var isSAR = false
private set
var legacySAR = false var legacySAR = false
private set
var isAB = false var isAB = false
private set
var slot = ""
private set
@JvmField val isZygiskEnabled = System.getenv("ZYGISK_ENABLED") == "1" @JvmField val isZygiskEnabled = System.getenv("ZYGISK_ENABLED") == "1"
@JvmStatic val isFDE get() = crypto == "block" @JvmStatic val isFDE get() = crypto == "block"
@JvmField var ramdisk = false @JvmStatic var ramdisk = false
var patchBootVbmeta = false private set
var crypto = "" private var crypto = ""
var noDataExec = false
var isRooted = false
var hasGMS = true var hasGMS = true
val isEmulator = val isEmulator =
@ -69,12 +78,39 @@ object Info {
fun init(shell: Shell) { fun init(shell: Shell) {
if (shell.isRoot) { if (shell.isRoot) {
isRooted = true val v = fastCmd(shell, "magisk -v").split(":")
val v = fastCmd(shell, "magisk -v").split(":".toRegex())
env = Env( env = Env(
v[0], v.size >= 3 && v[2] == "D", v[0], v.size >= 3 && v[2] == "D",
runCatching { fastCmd("magisk -V").toInt() }.getOrDefault(-1) 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 { private fun findImage(slot: String): Boolean {
val bootPath = "RECOVERYMODE=${Config.recovery} find_boot_image; echo \"\$BOOTIMAGE\"".fsh() val bootPath = (
"(RECOVERYMODE=${Config.recovery} " +
"SLOT=$slot find_boot_image; " +
"echo \$BOOTIMAGE)").fsh()
if (bootPath.isEmpty()) { if (bootPath.isEmpty()) {
console.add("! Unable to detect target image") console.add("! Unable to detect target image")
return false return false
@ -93,22 +96,14 @@ abstract class MagiskInstallImpl protected constructor(
return true return true
} }
private fun findSecondary(): Boolean { private fun findImage(): Boolean {
val slot = "echo \$SLOT".fsh() return findImage(Info.slot)
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") private fun findSecondary(): Boolean {
return true val slot = if (Info.slot == "_a") "_b" else "_a"
console.add("- Target slot: $slot")
return findImage(slot)
} }
private suspend fun extractFiles(): Boolean { private suspend fun extractFiles(): Boolean {

View File

@ -20,8 +20,8 @@ import java.util.jar.JarFile
class ShellInit : Shell.Initializer() { class ShellInit : Shell.Initializer() {
override fun onInit(context: Context, shell: Shell): Boolean { override fun onInit(context: Context, shell: Shell): Boolean {
Info.init(shell)
if (shell.isRoot) { if (shell.isRoot) {
Info.isRooted = true
RootUtils.bindTask?.let { shell.execTask(it) } RootUtils.bindTask?.let { shell.execTask(it) }
RootUtils.bindTask = null RootUtils.bindTask = null
} }
@ -47,7 +47,8 @@ class ShellInit : Shell.Initializer() {
if (shell.isRoot) { if (shell.isRoot) {
add("export MAGISKTMP=\$(magisk --path)") add("export MAGISKTMP=\$(magisk --path)")
// Test if we can properly execute stuff in /data // 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) { if (Info.noDataExec) {
@ -70,26 +71,9 @@ class ShellInit : Shell.Initializer() {
if (shell.isRoot) { if (shell.isRoot) {
add(context.assets.open("util_functions.sh")) add(context.assets.open("util_functions.sh"))
} }
add("app_init")
}.exec() }.exec()
fun fastCmd(cmd: String) = ShellUtils.fastCmd(shell, cmd) Info.init(shell)
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
return true return true
} }
} }

View File

@ -186,6 +186,10 @@ check_encryption() {
fi fi
} }
printvar() {
eval echo $1=\$$1
}
########################## ##########################
# Non-root util_functions # Non-root util_functions
########################## ##########################
@ -224,13 +228,25 @@ grep_prop() { return; }
############# #############
app_init() { app_init() {
mount_partitions mount_partitions >/dev/null
RAMDISKEXIST=false RAMDISKEXIST=false
check_boot_ramdisk && RAMDISKEXIST=true check_boot_ramdisk && RAMDISKEXIST=true
get_flags get_flags >/dev/null
run_migrations run_migrations
SHA1=$(grep_prop SHA1 $MAGISKTMP/.magisk/config) SHA1=$(grep_prop SHA1 $MAGISKTMP/.magisk/config)
check_encryption 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 export BOOTMODE=true