app: report real exit codes of terminal commands

Module installs and actions run in a PTY through busybox script, which
always exits with 0. The exit code of the command itself was lost, so
a failed module install or action was shown as done, with a reboot
button.

Run the command in a subshell and have it write its exit code to a
temporary file, then read it after the process exits.

Assisted-by: Claude Opus 5.5
This commit is contained in:
topjohnwu
2026-10-24 13:01:45 -07:00
committed by John Wu
parent 1db3c0be3e
commit a2f1a7a8fc
@@ -1,10 +1,12 @@
package com.topjohnwu.magisk.terminal
import com.topjohnwu.magisk.core.AppContext
import com.topjohnwu.superuser.Shell
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import timber.log.Timber
import java.io.File
private val busyboxPath: String by lazy {
Shell.cmd("readlink /proc/self/exe").exec().out.firstOrNull()
@@ -46,11 +48,14 @@ fun TerminalEmulator.appendLineOnMain(line: String) {
* Reads raw bytes from the process and feeds them to the terminal emulator.
* Must be called from a background thread. [columns] and [rows] should be read from the
* emulator on the main thread, after the terminal has been sized to its viewport.
* Returns true if the process exits with code 0.
* Returns true if the command exits with code 0.
*/
fun runSuCommand(emulator: TerminalEmulator, columns: Int, rows: Int, command: String): Boolean {
// 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)
return try {
val wrappedCmd = "export TERM=xterm-256color; stty cols $columns rows $rows 2>/dev/null; $command"
val wrappedCmd = "export TERM=xterm-256color; stty cols $columns rows $rows 2>/dev/null; " +
"($command); echo \$? > '${exitFile.path}'"
val escapedCmd = wrappedCmd.replace("'", "'\\''")
val process = ProcessBuilder(
@@ -69,10 +74,13 @@ fun runSuCommand(emulator: TerminalEmulator, columns: Int, rows: Int, command: S
}
}
process.waitFor() == 0
process.waitFor()
exitFile.readText().trim() == "0"
} catch (e: Exception) {
Timber.e(e, "runSuCommand failed")
emulator.appendLineOnMain("! Error: ${e.message}")
false
} finally {
exitFile.delete()
}
}