aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/org/pacien/tincapp/commands
diff options
context:
space:
mode:
authorpacien2018-02-14 01:25:48 +0100
committerpacien2018-02-14 01:25:48 +0100
commit680fe07b6ea000ee29ac28e2f48665433e7011df (patch)
tree76b9f914a5092cbd1f96f5d749336fbd3e7a7a60 /app/src/main/java/org/pacien/tincapp/commands
parent1eb53743996ffdabb6372a59f10d608f5830314e (diff)
downloadtincapp-680fe07b6ea000ee29ac28e2f48665433e7011df.tar.gz
Properly get daemon state
Diffstat (limited to 'app/src/main/java/org/pacien/tincapp/commands')
-rw-r--r--app/src/main/java/org/pacien/tincapp/commands/Executor.kt17
-rw-r--r--app/src/main/java/org/pacien/tincapp/commands/Tincd.kt3
2 files changed, 15 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 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
13 */ 13 */
14internal object Executor { 14internal object Executor {
15 15
16 private const val FAILED = -1
17
16 class CommandExecutionException(msg: String) : Exception(msg) 18 class CommandExecutionException(msg: String) : Exception(msg)
17 19
18 init { 20 init {
@@ -20,14 +22,23 @@ internal object Executor {
20 } 22 }
21 23
22 /** 24 /**
23 * @return -1 on error, forked child PID otherwise 25 * @return FAILED (-1) on error, forked child PID otherwise
24 */ 26 */
25 private external fun forkExec(argcv: Array<String>): Int 27 private external fun forkExec(argcv: Array<String>): Int
26 28
29 /**
30 * @return FAILED (-1) on error, 0 on no-op, the supplied PID otherwise
31 */
32 private external fun wait(pid: Int): Int
33
27 private fun read(stream: InputStream) = BufferedReader(InputStreamReader(stream)).readLines() 34 private fun read(stream: InputStream) = BufferedReader(InputStreamReader(stream)).readLines()
28 35
29 fun forkExec(cmd: Command) { 36 fun forkExec(cmd: Command): CompletableFuture<Void> {
30 if (forkExec(cmd.asArray()) == -1) throw CommandExecutionException("Could not fork child process.") 37 val pid = forkExec(cmd.asArray())
38 return when (pid) {
39 FAILED -> CompletableFuture.failedFuture(CommandExecutionException("Could not fork child process."))
40 else -> CompletableFuture.runAsync { wait(pid) }
41 }
31 } 42 }
32 43
33 fun call(cmd: Command): CompletableFuture<List<String>> { 44 fun call(cmd: Command): CompletableFuture<List<String>> {
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
7 */ 7 */
8object Tincd { 8object Tincd {
9 9
10 fun start(netName: String, deviceFd: Int, ed25519PrivateKeyFd: Int? = null, rsaPrivateKeyFd: Int? = null) { 10 fun start(netName: String, deviceFd: Int, ed25519PrivateKeyFd: Int? = null, rsaPrivateKeyFd: Int? = null) =
11 Executor.forkExec(Command(AppPaths.tincd().absolutePath) 11 Executor.forkExec(Command(AppPaths.tincd().absolutePath)
12 .withOption("no-detach") 12 .withOption("no-detach")
13 .withOption("config", AppPaths.confDir(netName).absolutePath) 13 .withOption("config", AppPaths.confDir(netName).absolutePath)
@@ -17,6 +17,5 @@ object Tincd {
17 .withOption("option", "Device=" + deviceFd) 17 .withOption("option", "Device=" + deviceFd)
18 .apply { if (ed25519PrivateKeyFd != null) withOption("option", "Ed25519PrivateKeyFile=/proc/self/fd/$ed25519PrivateKeyFd") } 18 .apply { if (ed25519PrivateKeyFd != null) withOption("option", "Ed25519PrivateKeyFile=/proc/self/fd/$ed25519PrivateKeyFd") }
19 .apply { if (rsaPrivateKeyFd != null) withOption("option", "PrivateKeyFile=/proc/self/fd/$rsaPrivateKeyFd") }) 19 .apply { if (rsaPrivateKeyFd != null) withOption("option", "PrivateKeyFile=/proc/self/fd/$rsaPrivateKeyFd") })
20 }
21 20
22} 21}