Init RootJava launch string asynchronously

This commit is contained in:
Mygod
2020-06-28 02:13:13 +08:00
parent 89cb8e6973
commit d909cc6903

View File

@@ -120,27 +120,33 @@ class RootServer @JvmOverloads constructor(private val warnLogger: (String) -> U
warnLogger(line) warnLogger(line)
} }
} }
private fun doInit(context: Context, niceName: String) { private suspend fun doInit(context: Context, niceName: String) = coroutineScope {
val writer: DataOutputStream @Suppress("BlockingMethodInNonBlockingContext")
val reader: BufferedReader val init = async {
try { try {
process = ProcessBuilder("su").start() process = ProcessBuilder("su").start()
val token1 = UUID.randomUUID().toString() val token1 = UUID.randomUUID().toString()
writer = DataOutputStream(process.outputStream.buffered()) val writer = DataOutputStream(process.outputStream.buffered())
writer.writeBytes("echo $token1\n") writer.writeBytes("echo $token1\n")
writer.flush() writer.flush()
reader = process.inputStream.bufferedReader() val reader = process.inputStream.bufferedReader()
reader.lookForToken(token1) reader.lookForToken(token1)
} catch (e: Exception) { if (DEBUG) Log.d(TAG, "Root shell initialized")
throw NoShellException(e) reader to writer
} catch (e: Exception) {
throw NoShellException(e)
}
} }
if (DEBUG) Log.d(TAG, "Root shell initialized")
val appProcess = AppProcess.getAppProcess()
val token2 = UUID.randomUUID().toString() val token2 = UUID.randomUUID().toString()
writer.writeBytes(RootJava.getLaunchString(context.packageCodePath + " exec", // hack: plugging in exec val launchString = async(Dispatchers.IO) {
RootServer::class.java.name, appProcess, AppProcess.guessIfAppProcessIs64Bits(appProcess), val appProcess = AppProcess.getAppProcess()
arrayOf("$token2\n"), niceName)) RootJava.getLaunchString(context.packageCodePath + " exec", // hack: plugging in exec
RootServer::class.java.name, appProcess, AppProcess.guessIfAppProcessIs64Bits(appProcess),
arrayOf("$token2\n"), niceName)
}
val list = awaitAll(init, launchString)
val (reader, writer) = list[0] as Pair<BufferedReader, DataOutputStream>
writer.writeBytes(list[1] as String)
writer.flush() writer.flush()
reader.lookForToken(token2) // wait for ready signal reader.lookForToken(token2) // wait for ready signal
output = writer output = writer
@@ -176,7 +182,7 @@ class RootServer @JvmOverloads constructor(private val warnLogger: (String) -> U
val future = CompletableDeferred<Unit>() val future = CompletableDeferred<Unit>()
worker = Thread { worker = Thread {
try { try {
doInit(context, niceName) runBlocking { doInit(context, niceName) }
future.complete(Unit) future.complete(Unit)
} catch (e: Throwable) { } catch (e: Throwable) {
future.completeExceptionally(e) future.completeExceptionally(e)