aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/org/pacien/tincapp/commands/Executor.kt
diff options
context:
space:
mode:
authorpacien2018-02-24 01:37:36 +0100
committerpacien2018-02-24 01:37:36 +0100
commit3c1a29e2b8717a20948773bbc21abdc723ce5dee (patch)
treed802e31724a27ac8d9a1c9e0d2ab730a225d5031 /app/src/main/java/org/pacien/tincapp/commands/Executor.kt
parentc29970d29bd3c4831fc2f80077d1d8548ed0fc15 (diff)
downloadtincapp-3c1a29e2b8717a20948773bbc21abdc723ce5dee.tar.gz
Handle daemon startup failures
Diffstat (limited to 'app/src/main/java/org/pacien/tincapp/commands/Executor.kt')
-rw-r--r--app/src/main/java/org/pacien/tincapp/commands/Executor.kt17
1 files changed, 12 insertions, 5 deletions
diff --git a/app/src/main/java/org/pacien/tincapp/commands/Executor.kt b/app/src/main/java/org/pacien/tincapp/commands/Executor.kt
index eb04f6d..fedd0d2 100644
--- a/app/src/main/java/org/pacien/tincapp/commands/Executor.kt
+++ b/app/src/main/java/org/pacien/tincapp/commands/Executor.kt
@@ -13,6 +13,7 @@ import java.io.InputStreamReader
13 */ 13 */
14internal object Executor { 14internal object Executor {
15 private const val FAILED = -1 15 private const val FAILED = -1
16 private const val SUCCESS = 0
16 17
17 class CommandExecutionException(msg: String) : Exception(msg) 18 class CommandExecutionException(msg: String) : Exception(msg)
18 19
@@ -26,17 +27,23 @@ internal object Executor {
26 private external fun forkExec(argcv: Array<String>): Int 27 private external fun forkExec(argcv: Array<String>): Int
27 28
28 /** 29 /**
29 * @return FAILED (-1) on error, 0 on no-op, the supplied PID otherwise 30 * @return FAILED (-1) on error, the exit status of the process otherwise
30 */ 31 */
31 private external fun wait(pid: Int): Int 32 private external fun wait(pid: Int): Int
32 33
33 private fun read(stream: InputStream) = BufferedReader(InputStreamReader(stream)).readLines() 34 private fun read(stream: InputStream) = BufferedReader(InputStreamReader(stream)).readLines()
34 35
35 fun forkExec(cmd: Command): CompletableFuture<Void> { 36 fun forkExec(cmd: Command): CompletableFuture<Void> {
36 val pid = forkExec(cmd.asArray()) 37 val pid = forkExec(cmd.asArray()).also {
37 return when (pid) { 38 if (it == FAILED) throw CommandExecutionException("Could not fork child process.")
38 FAILED -> CompletableFuture.failedFuture(CommandExecutionException("Could not fork child process.")) 39 }
39 else -> CompletableFuture.runAsync { wait(pid) } 40
41 return CompletableFuture.runAsync {
42 when (wait(pid)) {
43 SUCCESS -> Unit
44 FAILED -> throw CommandExecutionException("Process terminated abnormally.")
45 else -> throw CommandExecutionException("Non-zero exit status code.")
46 }
40 } 47 }
41 } 48 }
42 49