From 680021a27b9383172661aacd1409991711120ea2 Mon Sep 17 00:00:00 2001 From: Mygod Date: Tue, 28 Jul 2020 03:16:01 +0800 Subject: [PATCH] Prevent Samsung KNOX hijacking stdout --- .../src/main/java/be/mygod/librootkotlinx/RootServer.kt | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/mobile/src/main/java/be/mygod/librootkotlinx/RootServer.kt b/mobile/src/main/java/be/mygod/librootkotlinx/RootServer.kt index 859caac9..96f399bd 100644 --- a/mobile/src/main/java/be/mygod/librootkotlinx/RootServer.kt +++ b/mobile/src/main/java/be/mygod/librootkotlinx/RootServer.kt @@ -5,6 +5,8 @@ import android.os.Build import android.os.Looper import android.os.Parcelable import android.os.RemoteException +import android.system.Os +import android.system.OsConstants import android.util.Log import androidx.collection.LongSparseArray import androidx.collection.set @@ -440,12 +442,14 @@ class RootServer @JvmOverloads constructor(private val warnLogger: (String) -> U val cancellables = LongSparseArray<() -> Unit>() // thread safety: usage of output should be guarded by callbackWorker - val output = DataOutputStream(System.out.buffered().apply { + val output = DataOutputStream(FileOutputStream(Os.dup(FileDescriptor.out)).buffered().apply { + // prevent future write attempts to System.out, possibly from Samsung changes (again) + Os.dup2(FileDescriptor.err, OsConstants.STDOUT_FILENO) + System.setOut(System.err) val writer = writer() writer.appendln(args[0]) // echo ready signal writer.flush() }) - System.setOut(System.err) // prevent future write attempts to System.out // thread safety: usage of input should be in main thread val input = DataInputStream(System.`in`.buffered()) var counter = 0L