diff options
author | Samuel Holland <samuel@sholland.org> | 2018-01-08 04:15:15 -0600 |
---|---|---|
committer | Samuel Holland <samuel@sholland.org> | 2018-01-08 04:25:15 -0600 |
commit | 668d90f0633401a4b0e11e94b697162420d5726c (patch) | |
tree | 232e50adc67ad6c9f7d6036c20d0df84d8720717 | |
parent | ec81014c4e6e305b4720ecdf108f67a83990e14a (diff) |
WgQuickBackend: Improve error handling
Signed-off-by: Samuel Holland <samuel@sholland.org>
-rw-r--r-- | app/src/main/java/com/wireguard/android/backend/WgQuickBackend.java | 21 |
1 files changed, 15 insertions, 6 deletions
diff --git a/app/src/main/java/com/wireguard/android/backend/WgQuickBackend.java b/app/src/main/java/com/wireguard/android/backend/WgQuickBackend.java index c2734fa1..5eaf437d 100644 --- a/app/src/main/java/com/wireguard/android/backend/WgQuickBackend.java +++ b/app/src/main/java/com/wireguard/android/backend/WgQuickBackend.java @@ -1,6 +1,8 @@ package com.wireguard.android.backend; import android.content.Context; +import android.system.ErrnoException; +import android.system.OsConstants; import android.util.Log; import com.wireguard.android.model.Tunnel; @@ -10,7 +12,6 @@ import com.wireguard.android.util.RootShell; import com.wireguard.config.Config; import java.io.File; -import java.io.IOException; import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -69,18 +70,26 @@ public final class WgQuickBackend implements Backend { } @Override - public State setState(final Tunnel tunnel, final State state) throws IOException { + public State setState(final Tunnel tunnel, final State state) throws Exception { Log.v(TAG, "Requested state change to " + state + " for tunnel " + tunnel.getName()); final State originalState = getState(tunnel); final State resolvedState = resolveState(originalState, state); + if (resolvedState == originalState) + return originalState; + final int result; if (resolvedState == State.UP) { + if (!new File("/sys/module/wireguard").exists()) + throw new ErrnoException("WireGuard module not loaded", OsConstants.ENODEV); // FIXME: Assumes file layout from FileConfigStore. Use a temporary file. final File file = new File(context.getFilesDir(), tunnel.getName() + ".conf"); - if (rootShell.run(null, String.format("wg-quick up '%s'", file.getAbsolutePath())) != 0) - throw new IOException("wg-quick failed"); + result = rootShell.run(null, String.format("wg-quick up '%s'", file.getAbsolutePath())); } else { - if (rootShell.run(null, String.format("wg-quick down '%s'", tunnel.getName())) != 0) - throw new IOException("wg-quick failed"); + result = rootShell.run(null, String.format("wg-quick down '%s'", tunnel.getName())); + } + if (result != 0) { + final String message = result == OsConstants.EACCES ? + "Root access unavailable" : "wg-quick failed"; + throw new Exception(message); } return getState(tunnel); } |