From a2f1a7a8fc6beb7114de53a0ec39328a97a071a8 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Sat, 24 Oct 2026 05:43:45 +0000 Subject: [PATCH] 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 --- .../topjohnwu/magisk/terminal/TerminalProcess.kt | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/app/apk/src/main/java/com/topjohnwu/magisk/terminal/TerminalProcess.kt b/app/apk/src/main/java/com/topjohnwu/magisk/terminal/TerminalProcess.kt index 0ef246a87..061d77ff2 100644 --- a/app/apk/src/main/java/com/topjohnwu/magisk/terminal/TerminalProcess.kt +++ b/app/apk/src/main/java/com/topjohnwu/magisk/terminal/TerminalProcess.kt @@ -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() } }