mirror of
https://github.com/kvorschk/Magisk.git
synced 2026-12-04 19:10:47 -05:00
app: run module actions in BusyBox ash
Every script running in the context of Magisk is supposed to run in BusyBox ash with standalone mode. Since module actions and installs run in a PTY, they ran in /system/bin/sh instead: busybox script starts $SHELL, which su sets to /system/bin/sh, and ASH_STANDALONE was never set. Module installers recover by re-executing themselves in BusyBox, but action.sh has no such fallback. Run the PTY command in BusyBox ash with ASH_STANDALONE=1, which has to be set before ash starts. Also run the action from the module's directory name, quoted, rather than from the id in module.prop, which can differ from it. Assisted-by: Claude Opus 5.5
This commit is contained in:
@@ -57,8 +57,13 @@ fun runSuCommand(emulator: TerminalEmulator, columns: Int, rows: Int, command: S
|
|||||||
// busybox script always exits with 0, so have the command write its exit code to a file
|
// busybox script always exits with 0, so have the command write its exit code to a file
|
||||||
val exitFile = File.createTempFile("exit", null, AppContext.cacheDir)
|
val exitFile = File.createTempFile("exit", null, AppContext.cacheDir)
|
||||||
return try {
|
return try {
|
||||||
val wrappedCmd = "export TERM=xterm-256color; stty cols $columns rows $rows 2>/dev/null; " +
|
// Run the command in busybox ash with standalone mode, like every other script in the
|
||||||
|
// context of Magisk. busybox script itself starts $SHELL, which su sets to /system/bin/sh.
|
||||||
|
// ASH_STANDALONE has to be set before ash starts: it is only read on startup.
|
||||||
|
val shellCmd = "export TERM=xterm-256color; " +
|
||||||
|
"stty cols $columns rows $rows 2>/dev/null; " +
|
||||||
"($command); echo \$? > ${exitFile.path.shellQuoted()}"
|
"($command); echo \$? > ${exitFile.path.shellQuoted()}"
|
||||||
|
val wrappedCmd = "ASH_STANDALONE=1 $busyboxPath sh -c ${shellCmd.shellQuoted()}"
|
||||||
|
|
||||||
val process = ProcessBuilder(
|
val process = ProcessBuilder(
|
||||||
"su", "-c",
|
"su", "-c",
|
||||||
|
|||||||
@@ -210,8 +210,8 @@ class MainActivity : ComponentActivity(), SplashScreenHost {
|
|||||||
return@entry
|
return@entry
|
||||||
}
|
}
|
||||||
LaunchedEffect(key) {
|
LaunchedEffect(key) {
|
||||||
if (vm.actionId.isEmpty()) {
|
if (vm.actionDir.isEmpty()) {
|
||||||
vm.actionId = key.id
|
vm.actionDir = key.dir
|
||||||
vm.actionName = key.name
|
vm.actionName = key.name
|
||||||
vm.startRunAction()
|
vm.startRunAction()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import com.topjohnwu.magisk.core.utils.MediaStoreUtils
|
|||||||
import com.topjohnwu.magisk.core.utils.MediaStoreUtils.outputStream
|
import com.topjohnwu.magisk.core.utils.MediaStoreUtils.outputStream
|
||||||
import com.topjohnwu.magisk.terminal.TerminalEmulator
|
import com.topjohnwu.magisk.terminal.TerminalEmulator
|
||||||
import com.topjohnwu.magisk.terminal.runSuCommand
|
import com.topjohnwu.magisk.terminal.runSuCommand
|
||||||
|
import com.topjohnwu.magisk.terminal.shellQuoted
|
||||||
import kotlinx.coroutines.CompletableDeferred
|
import kotlinx.coroutines.CompletableDeferred
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.flow.MutableStateFlow
|
import kotlinx.coroutines.flow.MutableStateFlow
|
||||||
@@ -32,7 +33,7 @@ class ActionViewModel(private val savedState: SavedStateHandle) : BaseViewModel(
|
|||||||
private val _actionState = MutableStateFlow(State.RUNNING)
|
private val _actionState = MutableStateFlow(State.RUNNING)
|
||||||
val actionState: StateFlow<State> = _actionState.asStateFlow()
|
val actionState: StateFlow<State> = _actionState.asStateFlow()
|
||||||
|
|
||||||
var actionId: String = ""
|
var actionDir: String = ""
|
||||||
var actionName: String = ""
|
var actionName: String = ""
|
||||||
|
|
||||||
val emulator = TerminalEmulator(
|
val emulator = TerminalEmulator(
|
||||||
@@ -60,7 +61,7 @@ class ActionViewModel(private val savedState: SavedStateHandle) : BaseViewModel(
|
|||||||
val success = withContext(Dispatchers.IO) {
|
val success = withContext(Dispatchers.IO) {
|
||||||
runSuCommand(
|
runSuCommand(
|
||||||
emulator, columns, rows,
|
emulator, columns, rows,
|
||||||
"cd /data/adb/modules/$actionId && sh ./action.sh"
|
"cd ${"/data/adb/modules/$actionDir".shellQuoted()} && sh ./action.sh"
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -379,7 +379,7 @@ private fun ModuleCard(
|
|||||||
// Drop taps while a screen transition runs, so a quick
|
// Drop taps while a screen transition runs, so a quick
|
||||||
// second tap can't open another screen
|
// second tap can't open another screen
|
||||||
onClick = dropUnlessResumed {
|
onClick = dropUnlessResumed {
|
||||||
viewModel.runAction(item.module.id, item.module.name)
|
viewModel.runAction(item.module.base.name, item.module.name)
|
||||||
},
|
},
|
||||||
) {
|
) {
|
||||||
Row(
|
Row(
|
||||||
|
|||||||
@@ -117,8 +117,8 @@ class ModuleViewModel : AsyncLoadViewModel() {
|
|||||||
navigateTo(Route.Flash(Const.Value.FLASH_ZIP, uri.toString()))
|
navigateTo(Route.Flash(Const.Value.FLASH_ZIP, uri.toString()))
|
||||||
}
|
}
|
||||||
|
|
||||||
fun runAction(id: String, name: String) {
|
fun runAction(dir: String, name: String) {
|
||||||
navigateTo(Route.Action(id, name))
|
navigateTo(Route.Action(dir, name))
|
||||||
}
|
}
|
||||||
|
|
||||||
fun toggleEnabled(item: ModuleItem) {
|
fun toggleEnabled(item: ModuleItem) {
|
||||||
|
|||||||
@@ -26,7 +26,8 @@ sealed interface Route : NavKey, Parcelable {
|
|||||||
|
|
||||||
@Parcelize
|
@Parcelize
|
||||||
data class Action(
|
data class Action(
|
||||||
val id: String,
|
// The module's directory name: unlike the id in module.prop, it is unique
|
||||||
|
val dir: String,
|
||||||
val name: String,
|
val name: String,
|
||||||
) : Route
|
) : Route
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user