From 680fe07b6ea000ee29ac28e2f48665433e7011df Mon Sep 17 00:00:00 2001 From: pacien Date: Wed, 14 Feb 2018 01:25:48 +0100 Subject: Properly get daemon state --- .../main/java/org/pacien/tincapp/commands/Executor.kt | 17 ++++++++++++++--- app/src/main/java/org/pacien/tincapp/commands/Tincd.kt | 3 +-- 2 files changed, 15 insertions(+), 5 deletions(-) (limited to 'app/src/main/java/org/pacien/tincapp/commands') 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 eace2e7..a3b5cea 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,8 @@ import java.io.InputStreamReader */ internal object Executor { + private const val FAILED = -1 + class CommandExecutionException(msg: String) : Exception(msg) init { @@ -20,14 +22,23 @@ internal object Executor { } /** - * @return -1 on error, forked child PID otherwise + * @return FAILED (-1) on error, forked child PID otherwise */ private external fun forkExec(argcv: Array): Int + /** + * @return FAILED (-1) on error, 0 on no-op, the supplied PID otherwise + */ + private external fun wait(pid: Int): Int + private fun read(stream: InputStream) = BufferedReader(InputStreamReader(stream)).readLines() - fun forkExec(cmd: Command) { - if (forkExec(cmd.asArray()) == -1) throw CommandExecutionException("Could not fork child process.") + fun forkExec(cmd: Command): CompletableFuture { + val pid = forkExec(cmd.asArray()) + return when (pid) { + FAILED -> CompletableFuture.failedFuture(CommandExecutionException("Could not fork child process.")) + else -> CompletableFuture.runAsync { wait(pid) } + } } fun call(cmd: Command): CompletableFuture> { diff --git a/app/src/main/java/org/pacien/tincapp/commands/Tincd.kt b/app/src/main/java/org/pacien/tincapp/commands/Tincd.kt index d9c665d..44fcef5 100644 --- a/app/src/main/java/org/pacien/tincapp/commands/Tincd.kt +++ b/app/src/main/java/org/pacien/tincapp/commands/Tincd.kt @@ -7,7 +7,7 @@ import org.pacien.tincapp.context.AppPaths */ object Tincd { - fun start(netName: String, deviceFd: Int, ed25519PrivateKeyFd: Int? = null, rsaPrivateKeyFd: Int? = null) { + fun start(netName: String, deviceFd: Int, ed25519PrivateKeyFd: Int? = null, rsaPrivateKeyFd: Int? = null) = Executor.forkExec(Command(AppPaths.tincd().absolutePath) .withOption("no-detach") .withOption("config", AppPaths.confDir(netName).absolutePath) @@ -17,6 +17,5 @@ object Tincd { .withOption("option", "Device=" + deviceFd) .apply { if (ed25519PrivateKeyFd != null) withOption("option", "Ed25519PrivateKeyFile=/proc/self/fd/$ed25519PrivateKeyFd") } .apply { if (rsaPrivateKeyFd != null) withOption("option", "PrivateKeyFile=/proc/self/fd/$rsaPrivateKeyFd") }) - } } -- cgit v1.2.3