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() } }