diff options
Diffstat (limited to 'app/src/fil/libre/repwifiapp/helpers/RootCommand.java')
-rw-r--r-- | app/src/fil/libre/repwifiapp/helpers/RootCommand.java | 81 |
1 files changed, 64 insertions, 17 deletions
diff --git a/app/src/fil/libre/repwifiapp/helpers/RootCommand.java b/app/src/fil/libre/repwifiapp/helpers/RootCommand.java index bd859c8..61dfa2d 100644 --- a/app/src/fil/libre/repwifiapp/helpers/RootCommand.java +++ b/app/src/fil/libre/repwifiapp/helpers/RootCommand.java @@ -1,5 +1,5 @@ // -// Copyright 2017 Filippo "Fil" Bergamo <fil.bergamo@riseup.net> +// Copyright 2017, 2018 Filippo "Fil" Bergamo <fil.bergamo@riseup.net> // // This file is part of RepWifiApp. // @@ -20,40 +20,61 @@ package fil.libre.repwifiapp.helpers; +import fil.libre.repwifiapp.Commons; +import fil.libre.repwifiapp.Utils; import java.io.DataOutputStream; import java.io.InputStream; +import java.util.concurrent.TimeoutException; public class RootCommand extends ShellCommand { - // protected static final String CMD_WRAPPING = "export TEMPOUT=\"$(%s)\";echo \"$TEMPOUT\";exit $?"; - protected static final String CMD_WRAPPING = "TEMPOUT=\"$(%s)\";ec=$?;echo \"$TEMPOUT\";exit $ec"; + protected static final String CMD_WRAPPING = "TEMPOUT=\"$(%s)\";ec=$?;echo \"$TEMPOUT\";echo $ec > \"%s\";exit $ec"; + //protected static final String CMD_WRAPPING = "TEMPOUT=\"$(%s)\";ec=$?;echo \"$TEMPOUT\";exit $ec"; public RootCommand(String commandText) { super(commandText); this._cmdTxt = commandText; } + + public static boolean executeRootCmd(String cmd){ + // won't return + try { + return executeRootCmd(cmd, -1); + } catch (TimeoutException e){ + // will never throw timeout exception as it won't use asynchronous wait. + return false; + } + } + + public static boolean executeRootCmd(String cmd, long timeoutMillis) throws TimeoutException{ - public static boolean executeRootCmd(String cmd) { - try { RootCommand c = new RootCommand(cmd); - if (c.execute() == 0) { + if (c.execute(timeoutMillis) == 0) { return true; } else { return false; } - } catch (Exception e) { - Utils.logError("Error executing \"" + cmd + "\"", e); + } + catch (TimeoutException te){ + throw te; + } + catch (Exception e) { + Logger.logError("Error executing \"" + cmd + "\"", e); return false; } } @Override public int execute() throws Exception { - + return execute(-1); + } + + public int execute(long timeoutMillis) throws Exception{ + if (this._cmdTxt == null) { return EXITCODE_INVALID_INPUT; } @@ -64,9 +85,9 @@ public class RootCommand extends ShellCommand { InputStream os = su.getInputStream(); InputStream es = su.getErrorStream(); - Utils.logDebug("SU:EXEC: " + this._cmdTxt); + Logger.logDebug("SU:EXEC: " + this._cmdTxt); - String wrappedCmd = String.format(CMD_WRAPPING, this._cmdTxt); + String wrappedCmd = String.format(CMD_WRAPPING, this._cmdTxt, Commons.getExitCodeTempFile()); stdin.writeBytes(wrappedCmd + "\n"); stdin.flush(); @@ -76,7 +97,16 @@ public class RootCommand extends ShellCommand { sb.append(getStringFromStream(es)); sb.append(getStringFromStream(os)); - int res = su.waitFor(); + int res; + + if (timeoutMillis <= MIN_TIMEOUT_MILLIS){ + res = su.waitFor(); + + } else { + Logger.logDebug("Executing command with " + timeoutMillis + "ms timeout."); + ProcessTimeout w = new ProcessTimeout(); + res = w.waitFor(su, timeoutMillis); + } // re-read the output, in case it was empty when first tried sb.append(getStringFromStream(es)); @@ -84,10 +114,16 @@ public class RootCommand extends ShellCommand { this._cmdOut = sb.toString(); - Utils.logDebug("OUT: " + getOutput()); + Logger.logDebug("OUT: " + getOutput()); + if (res == 0){ + // could be su's own exit code hiding the original one: + res = readLastExitCodeFromFile(); + } + + Logger.logDebug("ExitCode: " + res); return res; - + } public int testRootAccess() throws Exception { @@ -96,16 +132,27 @@ public class RootCommand extends ShellCommand { DataOutputStream stdin = new DataOutputStream(su.getOutputStream()); - Utils.logDebug("Testing root access: executing simple \"su\""); + Logger.logDebug("Testing root access: executing simple \"su\""); stdin.writeBytes("exit\n"); stdin.flush(); int res = su.waitFor(); - Utils.logDebug("Simple \"su\" exitcode: " + res); + Logger.logDebug("Simple \"su\" exitcode: " + res); return res; } - + + private int readLastExitCodeFromFile(){ + + String strec = Utils.readFile(Commons.getExitCodeTempFile()).trim(); + try{ + return Integer.parseInt(strec); + }catch(NumberFormatException e){ + Logger.logError("NumberFormatException while parsing contents of ExitCodeTempFile: " + strec); + return EXITCODE_PARSING_ERROR; + } + } + } |