summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorJason A. Donenfeld <Jason@zx2c4.com>2018-01-09 15:23:26 +0100
committerSamuel Holland <samuel@sholland.org>2018-01-09 08:39:07 -0600
commit643b698f3096ab8e48ef124e76752f1e30be3804 (patch)
treec9fb76bb0ee1847ab9a6c23f4abd139c6bfb62c4
parent23c09eb655ed779b08b8d1e4542a6bd60c83ffba (diff)
RootShell: stop if we can't start
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
-rw-r--r--app/src/main/java/com/wireguard/android/util/RootShell.java67
1 files changed, 36 insertions, 31 deletions
diff --git a/app/src/main/java/com/wireguard/android/util/RootShell.java b/app/src/main/java/com/wireguard/android/util/RootShell.java
index f5eb440f..a565523b 100644
--- a/app/src/main/java/com/wireguard/android/util/RootShell.java
+++ b/app/src/main/java/com/wireguard/android/util/RootShell.java
@@ -112,38 +112,43 @@ public class RootShell {
}
public synchronized void start() throws IOException, NoRootException {
- if (isRunning())
- return;
- if (!localBinaryDir.isDirectory() && !localBinaryDir.mkdirs())
- throw new FileNotFoundException("Could not create local binary directory");
- if (!localTemporaryDir.isDirectory() && !localTemporaryDir.mkdirs())
- throw new FileNotFoundException("Could not create local temporary directory");
- if (!isExecutableInPath(SU))
- throw new NoRootException(deviceNotRootedMessage);
- final ProcessBuilder builder = new ProcessBuilder().command(SU);
- builder.environment().put("LC_ALL", "C");
- process = builder.start();
- stdin = new OutputStreamWriter(process.getOutputStream(), StandardCharsets.UTF_8);
- stdout = new BufferedReader(new InputStreamReader(process.getInputStream(),
- StandardCharsets.UTF_8));
- stderr = new BufferedReader(new InputStreamReader(process.getErrorStream(),
- StandardCharsets.UTF_8));
- stdin.write(preamble);
- stdin.flush();
- // Check that the shell started successfully.
- final String uid = stdout.readLine();
- if (!"0".equals(uid)) {
- Log.w(TAG, "Root check did not return correct UID: " + uid);
- throw new NoRootException(deviceNotRootedMessage);
- }
- if (!isRunning()) {
- String line;
- while ((line = stderr.readLine()) != null) {
- Log.w(TAG, "Root check returned an error: " + line);
- if (line.contains("Permission denied"))
- throw new NoRootException(deviceNotRootedMessage);
+ try {
+ if (isRunning())
+ return;
+ if (!localBinaryDir.isDirectory() && !localBinaryDir.mkdirs())
+ throw new FileNotFoundException("Could not create local binary directory");
+ if (!localTemporaryDir.isDirectory() && !localTemporaryDir.mkdirs())
+ throw new FileNotFoundException("Could not create local temporary directory");
+ if (!isExecutableInPath(SU))
+ throw new NoRootException(deviceNotRootedMessage);
+ final ProcessBuilder builder = new ProcessBuilder().command(SU);
+ builder.environment().put("LC_ALL", "C");
+ process = builder.start();
+ stdin = new OutputStreamWriter(process.getOutputStream(), StandardCharsets.UTF_8);
+ stdout = new BufferedReader(new InputStreamReader(process.getInputStream(),
+ StandardCharsets.UTF_8));
+ stderr = new BufferedReader(new InputStreamReader(process.getErrorStream(),
+ StandardCharsets.UTF_8));
+ stdin.write(preamble);
+ stdin.flush();
+ // Check that the shell started successfully.
+ final String uid = stdout.readLine();
+ if (!"0".equals(uid)) {
+ Log.w(TAG, "Root check did not return correct UID: " + uid);
+ throw new NoRootException(deviceNotRootedMessage);
+ }
+ if (!isRunning()) {
+ String line;
+ while ((line = stderr.readLine()) != null) {
+ Log.w(TAG, "Root check returned an error: " + line);
+ if (line.contains("Permission denied"))
+ throw new NoRootException(deviceNotRootedMessage);
+ }
+ throw new IOException("Shell failed to start: " + process.exitValue());
}
- throw new IOException("Shell failed to start: " + process.exitValue());
+ } catch (final IOException | NoRootException e) {
+ stop();
+ throw e;
}
}