RootShell: stop if we can't start

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
Jason A. Donenfeld 2018-01-09 15:23:26 +01:00 committed by Samuel Holland
parent 23c09eb655
commit 643b698f30

View File

@ -112,38 +112,43 @@ public class RootShell {
} }
public synchronized void start() throws IOException, NoRootException { public synchronized void start() throws IOException, NoRootException {
if (isRunning()) try {
return; if (isRunning())
if (!localBinaryDir.isDirectory() && !localBinaryDir.mkdirs()) return;
throw new FileNotFoundException("Could not create local binary directory"); if (!localBinaryDir.isDirectory() && !localBinaryDir.mkdirs())
if (!localTemporaryDir.isDirectory() && !localTemporaryDir.mkdirs()) throw new FileNotFoundException("Could not create local binary directory");
throw new FileNotFoundException("Could not create local temporary directory"); if (!localTemporaryDir.isDirectory() && !localTemporaryDir.mkdirs())
if (!isExecutableInPath(SU)) throw new FileNotFoundException("Could not create local temporary directory");
throw new NoRootException(deviceNotRootedMessage); if (!isExecutableInPath(SU))
final ProcessBuilder builder = new ProcessBuilder().command(SU); throw new NoRootException(deviceNotRootedMessage);
builder.environment().put("LC_ALL", "C"); final ProcessBuilder builder = new ProcessBuilder().command(SU);
process = builder.start(); builder.environment().put("LC_ALL", "C");
stdin = new OutputStreamWriter(process.getOutputStream(), StandardCharsets.UTF_8); process = builder.start();
stdout = new BufferedReader(new InputStreamReader(process.getInputStream(), stdin = new OutputStreamWriter(process.getOutputStream(), StandardCharsets.UTF_8);
StandardCharsets.UTF_8)); stdout = new BufferedReader(new InputStreamReader(process.getInputStream(),
stderr = new BufferedReader(new InputStreamReader(process.getErrorStream(), StandardCharsets.UTF_8));
StandardCharsets.UTF_8)); stderr = new BufferedReader(new InputStreamReader(process.getErrorStream(),
stdin.write(preamble); StandardCharsets.UTF_8));
stdin.flush(); stdin.write(preamble);
// Check that the shell started successfully. stdin.flush();
final String uid = stdout.readLine(); // Check that the shell started successfully.
if (!"0".equals(uid)) { final String uid = stdout.readLine();
Log.w(TAG, "Root check did not return correct UID: " + uid); if (!"0".equals(uid)) {
throw new NoRootException(deviceNotRootedMessage); 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()); 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());
}
} catch (final IOException | NoRootException e) {
stop();
throw e;
} }
} }