Skip to content

Commit

Permalink
[fix]: root shell on some devices
Browse files Browse the repository at this point in the history
  • Loading branch information
F0x1d committed Jul 6, 2024
1 parent 8b03dd8 commit 784d673
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,18 @@ import javax.inject.Singleton
@Singleton
open class DefaultTerminal @Inject constructor(
@IODispatcher protected val ioDispatcher: CoroutineDispatcher,
): Terminal {
) : Terminal {

companion object {
const val INDEX = 0
}

override val title = R.string.terminal_default

protected open val commandPrefix: Array<String>? = null

override suspend fun isSupported() = true

private fun createProcess(commands: Array<out String>) = (commandPrefix ?: emptyArray())
.plus(commands)
.let(Runtime.getRuntime()::exec)

override suspend fun executeNow(vararg command: String) = withContext(ioDispatcher) {
val process = createProcess(command)
val process = Runtime.getRuntime().exec(command)

val output = async {
process.inputStream.readBytes().decodeToString()
Expand All @@ -43,7 +37,7 @@ open class DefaultTerminal @Inject constructor(
TerminalResult(exitCode, output.await(), error.await())
}

override fun execute(vararg command: String) = createProcess(command).run {
override fun execute(vararg command: String) = Runtime.getRuntime().exec(command).run {
TerminalProcess(
output = inputStream,
error = errorStream,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.f0x1d.logfox.terminals

import com.f0x1d.logfox.arch.di.IODispatcher
import com.f0x1d.logfox.model.terminal.TerminalProcess
import com.f0x1d.logfox.model.terminal.TerminalResult
import com.f0x1d.logfox.terminals.base.Terminal
import com.topjohnwu.superuser.Shell
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.withContext
Expand All @@ -10,17 +12,15 @@ import javax.inject.Singleton

@Singleton
class RootTerminal @Inject constructor(
@IODispatcher ioDispatcher: CoroutineDispatcher,
): DefaultTerminal(ioDispatcher) {
@IODispatcher private val ioDispatcher: CoroutineDispatcher,
) : Terminal {

companion object {
const val INDEX = 1
}

override val title = R.string.root

override val commandPrefix = arrayOf("su", "-c")

override suspend fun isSupported(): Boolean = withContext(ioDispatcher) {
Shell.getShell().isRoot
}
Expand All @@ -35,7 +35,30 @@ class RootTerminal @Inject constructor(
}
}

// can't make currently execute method with libsu
// It seems not all devices support "su -c ...".
// I got feedback that LogFox does not read logs, while other apps do.
// After I implemented libsu permission could be got. So i took a look at libsu code
// and tried to do the same here
private fun createProcess(commands: Array<out String>): Process? = runCatching {
val process = Runtime.getRuntime().exec("su")

process.outputStream.run {
write(commands.joinToString(" ").encodeToByteArray())
write('\n'.code)
flush()
}

process
}.getOrNull()

override fun execute(vararg command: String): TerminalProcess? = createProcess(command)?.run {
TerminalProcess(
output = inputStream,
error = errorStream,
input = outputStream,
destroy = this::destroy,
)
}

override suspend fun exit() = withContext(ioDispatcher) {
Shell.getShell().waitAndClose()
Expand Down

0 comments on commit 784d673

Please sign in to comment.