Allow iptables -w to output busy spin message

This commit is contained in:
Mygod
2018-12-14 02:00:31 +08:00
parent b2346dca9a
commit f26716051c
2 changed files with 11 additions and 7 deletions

View File

@@ -53,9 +53,9 @@ class Routing(val downstream: String, ownerAddress: InterfaceAddress? = null) {
} }
fun RootSession.Transaction.iptablesAdd(content: String, table: String = "filter") = fun RootSession.Transaction.iptablesAdd(content: String, table: String = "filter") =
exec("$IPTABLES -t $table -A $content", "$IPTABLES -t $table -D $content") exec("$IPTABLES -t $table -A $content", "$IPTABLES -t $table -D $content", true)
fun RootSession.Transaction.iptablesInsert(content: String, table: String = "filter") = fun RootSession.Transaction.iptablesInsert(content: String, table: String = "filter") =
exec("$IPTABLES -t $table -I $content", "$IPTABLES -t $table -D $content") exec("$IPTABLES -t $table -I $content", "$IPTABLES -t $table -D $content", true)
} }
class InterfaceNotFoundException : SocketException() { class InterfaceNotFoundException : SocketException() {

View File

@@ -58,12 +58,11 @@ class RootSession : AutoCloseable {
class UnexpectedOutputException(msg: String) : RuntimeException(msg) class UnexpectedOutputException(msg: String) : RuntimeException(msg)
private fun checkOutput(command: String, result: Shell.Result, out: Boolean = result.out.isNotEmpty(), private fun checkOutput(command: String, result: Shell.Result, out: Boolean = result.out.isNotEmpty(),
err: Boolean = result.err.isNotEmpty()) { err: Boolean = result.err.isNotEmpty()): String {
if (result.isSuccess && !out && !err) return
val msg = StringBuilder("$command exited with ${result.code}") val msg = StringBuilder("$command exited with ${result.code}")
if (out) result.out.forEach { msg.append("\n$it") } if (out) result.out.forEach { msg.append("\n$it") }
if (err) result.err.forEach { msg.append("\nE $it") } if (err) result.err.forEach { msg.append("\nE $it") }
throw UnexpectedOutputException(msg.toString()) if (!result.isSuccess || out || err) throw UnexpectedOutputException(msg.toString()) else return msg.toString()
} }
private val shell = Shell.newInstance("su") private val shell = Shell.newInstance("su")
@@ -102,6 +101,11 @@ class RootSession : AutoCloseable {
}).exec() }).exec()
} }
fun exec(command: String) = checkOutput(command, execQuiet(command)) fun exec(command: String) = checkOutput(command, execQuiet(command))
fun execWithWait(command: String) {
val result = execQuiet(command)
val message = checkOutput(command, result, err = false)
if (result.err.isNotEmpty()) Timber.i(message)
}
fun execOutUnjoined(command: String): List<String> { fun execOutUnjoined(command: String): List<String> {
val result = execQuiet(command) val result = execQuiet(command)
checkOutput(command, result, false) checkOutput(command, result, false)
@@ -115,9 +119,9 @@ class RootSession : AutoCloseable {
inner class Transaction { inner class Transaction {
private val revertCommands = LinkedList<String>() private val revertCommands = LinkedList<String>()
fun exec(command: String, revert: String? = null) { fun exec(command: String, revert: String? = null, wait: Boolean = false) {
if (revert != null) revertCommands.addFirst(revert) // add first just in case exec fails if (revert != null) revertCommands.addFirst(revert) // add first just in case exec fails
this@RootSession.exec(command) if (wait) this@RootSession.execWithWait(command) else this@RootSession.exec(command)
} }
fun execQuiet(command: String) = this@RootSession.execQuiet(command) fun execQuiet(command: String) = this@RootSession.execQuiet(command)