From ab7a3b4ba7326433bdce634d053bf2212a37872e Mon Sep 17 00:00:00 2001 From: Mygod Date: Sun, 17 Feb 2019 12:14:04 +0800 Subject: [PATCH] Handle interrupted dump within poll --- .../be/mygod/vpnhotspot/net/monitor/IpMonitor.kt | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/net/monitor/IpMonitor.kt b/mobile/src/main/java/be/mygod/vpnhotspot/net/monitor/IpMonitor.kt index 5ba6aadb..80a99cff 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/net/monitor/IpMonitor.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/net/monitor/IpMonitor.kt @@ -18,6 +18,8 @@ import kotlin.concurrent.thread abstract class IpMonitor : Runnable { companion object { const val KEY = "service.ipMonitor" + // https://android.googlesource.com/platform/external/iproute2/+/7f7a711/lib/libnetlink.c#493 + private const val MAGIC_SUFFIX = "Dump was interrupted and may be inconsistent." private val currentMode get() = Mode.valueOf(app.pref.getString(KEY, Mode.Poll.toString()) ?: "") } @@ -52,8 +54,7 @@ abstract class IpMonitor : Runnable { } try { process.inputStream.bufferedReader().forEachLine { - // https://android.googlesource.com/platform/external/iproute2/+/7f7a711/lib/libnetlink.c#493 - if (it.endsWith("Dump was interrupted and may be inconsistent.")) { + if (it.endsWith(MAGIC_SUFFIX)) { Timber.w(it) process.destroy() // move on to next mode } else processLine(it) @@ -100,7 +101,12 @@ abstract class IpMonitor : Runnable { SmartSnackbar.make(R.string.noisy_su_failure).show() } } - process.inputStream.bufferedReader().useLines(this::processLines) + process.inputStream.bufferedReader().useLines { + processLines(it.map { line -> + if (line.endsWith(MAGIC_SUFFIX)) throw IOException(line) + line + }) + } } override fun run() { @@ -115,6 +121,7 @@ abstract class IpMonitor : Runnable { RootSession.use { val result = it.execQuiet(command) RootSession.checkOutput(command, result, false) + if (result.out.any { it.endsWith(MAGIC_SUFFIX) }) throw IOException(result.out.joinToString("\n")) processLines(result.out.asSequence()) } } catch (e: RuntimeException) {