From 3c1a29e2b8717a20948773bbc21abdc723ce5dee Mon Sep 17 00:00:00 2001 From: pacien Date: Sat, 24 Feb 2018 01:37:36 +0100 Subject: Handle daemon startup failures --- .../main/java/org/pacien/tincapp/commands/Executor.kt | 17 ++++++++++++----- app/src/main/java/org/pacien/tincapp/context/App.kt | 6 +++++- .../java/org/pacien/tincapp/service/TincVpnService.kt | 14 ++++++++++---- 3 files changed, 27 insertions(+), 10 deletions(-) (limited to 'app/src/main/java') 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 */ internal object Executor { private const val FAILED = -1 + private const val SUCCESS = 0 class CommandExecutionException(msg: String) : Exception(msg) @@ -26,17 +27,23 @@ internal object Executor { private external fun forkExec(argcv: Array): Int /** - * @return FAILED (-1) on error, 0 on no-op, the supplied PID otherwise + * @return FAILED (-1) on error, the exit status of the process otherwise */ private external fun wait(pid: Int): Int private fun read(stream: InputStream) = BufferedReader(InputStreamReader(stream)).readLines() 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) } + val pid = forkExec(cmd.asArray()).also { + if (it == FAILED) throw CommandExecutionException("Could not fork child process.") + } + + return CompletableFuture.runAsync { + when (wait(pid)) { + SUCCESS -> Unit + FAILED -> throw CommandExecutionException("Process terminated abnormally.") + else -> throw CommandExecutionException("Non-zero exit status code.") + } } } diff --git a/app/src/main/java/org/pacien/tincapp/context/App.kt b/app/src/main/java/org/pacien/tincapp/context/App.kt index 9fb910b..4a4a475 100644 --- a/app/src/main/java/org/pacien/tincapp/context/App.kt +++ b/app/src/main/java/org/pacien/tincapp/context/App.kt @@ -4,6 +4,7 @@ import android.app.Application import android.content.Context import android.content.Intent import android.net.Uri +import android.os.Handler import android.support.annotation.StringRes import android.support.v7.app.AlertDialog import android.view.WindowManager @@ -16,20 +17,23 @@ class App : Application() { override fun onCreate() { super.onCreate() appContext = applicationContext + handler = Handler() } companion object { private var appContext: Context? = null + private var handler: Handler? = null fun getContext() = appContext!! fun getResources() = getContext().resources!! - fun alert(@StringRes title: Int, msg: String, manualLink: String? = null) = + fun alert(@StringRes title: Int, msg: String, manualLink: String? = null) = handler!!.post { AlertDialog.Builder(getContext(), R.style.Theme_AppCompat_Dialog) .setTitle(title).setMessage(msg) .apply { if (manualLink != null) setNeutralButton(R.string.action_open_manual) { _, _ -> openURL(manualLink) } } .setPositiveButton(R.string.action_close, { _, _ -> Unit }) .create().apply { window.setType(WindowManager.LayoutParams.TYPE_SYSTEM_ERROR) }.show() + } fun openURL(url: String) = appContext?.startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(url)).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)) diff --git a/app/src/main/java/org/pacien/tincapp/service/TincVpnService.kt b/app/src/main/java/org/pacien/tincapp/service/TincVpnService.kt index dfdbb32..223763d 100644 --- a/app/src/main/java/org/pacien/tincapp/service/TincVpnService.kt +++ b/app/src/main/java/org/pacien/tincapp/service/TincVpnService.kt @@ -94,12 +94,18 @@ class TincVpnService : VpnService() { val daemon = Tincd.start(netName, deviceFd.fd, privateKeys.first?.fd, privateKeys.second?.fd) setState(netName, interfaceCfg, deviceFd, daemon) - waitForDaemonStartup().thenRun { + + waitForDaemonStartup().whenComplete { _, exception -> deviceFd.close() privateKeys.first?.close() privateKeys.second?.close() - Log.i(TAG, "tinc daemon started.") - broadcastEvent(Actions.EVENT_CONNECTED) + + if (exception != null) { + reportError(resources.getString(R.string.message_daemon_exited, exception.cause!!.message!!), exception) + } else { + Log.i(TAG, "tinc daemon started.") + broadcastEvent(Actions.EVENT_CONNECTED) + } } } @@ -132,7 +138,7 @@ class TincVpnService : VpnService() { private fun waitForDaemonStartup() = CompletableFuture .runAsync { Thread.sleep(SETUP_DELAY) } - .thenCompose { netName?.let { Tinc.pid(it) } ?: CompletableFuture.completedFuture(0) } + .thenCompose { if (daemon!!.isDone) daemon!! else CompletableFuture.runAsync { } } companion object { private const val SETUP_DELAY = 500L // ms -- cgit v1.2.3