diff options
Diffstat (limited to 'app/src/fil/libre/repwifiapp/helpers/RootCommand.java')
-rw-r--r-- | app/src/fil/libre/repwifiapp/helpers/RootCommand.java | 68 |
1 files changed, 43 insertions, 25 deletions
diff --git a/app/src/fil/libre/repwifiapp/helpers/RootCommand.java b/app/src/fil/libre/repwifiapp/helpers/RootCommand.java index 9d9f7d3..bd859c8 100644 --- a/app/src/fil/libre/repwifiapp/helpers/RootCommand.java +++ b/app/src/fil/libre/repwifiapp/helpers/RootCommand.java @@ -22,71 +22,89 @@ package fil.libre.repwifiapp.helpers; import java.io.DataOutputStream; import java.io.InputStream; -import fil.libre.repwifiapp.Commons; + 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"; + public RootCommand(String commandText) { super(commandText); this._cmdTxt = commandText; } + + public static boolean executeRootCmd(String cmd) { + + try { + RootCommand c = new RootCommand(cmd); + if (c.execute() == 0) { + return true; + } else { + return false; + } + + } catch (Exception e) { + Utils.logError("Error executing \"" + cmd + "\"", e); + return false; + } + } + @Override public int execute() throws Exception { + if (this._cmdTxt == null) { + return EXITCODE_INVALID_INPUT; + } + Process su = Runtime.getRuntime().exec("su"); DataOutputStream stdin = new DataOutputStream(su.getOutputStream()); InputStream os = su.getInputStream(); InputStream es = su.getErrorStream(); - if (this._cmdTxt != null) { - - Utils.logDebug("SU:EXEC: " + this._cmdTxt); + Utils.logDebug("SU:EXEC: " + this._cmdTxt); - this._cmdTxt += " > " + Commons.getTempOutFile(); - - stdin.writeBytes(this._cmdTxt + "\n"); - stdin.flush(); - } + String wrappedCmd = String.format(CMD_WRAPPING, this._cmdTxt); + + stdin.writeBytes(wrappedCmd + "\n"); + stdin.flush(); StringBuilder sb = new StringBuilder(); sb.append(getStringFromStream(es)); sb.append(getStringFromStream(os)); - this._cmdOut = sb.toString(); - - stdin.writeBytes("exit\n"); - stdin.flush(); - int res = su.waitFor(); // re-read the output, in case it was empty when first tried sb.append(getStringFromStream(es)); sb.append(getStringFromStream(os)); + this._cmdOut = sb.toString(); + Utils.logDebug("OUT: " + getOutput()); return res; } + + public int testRootAccess() throws Exception { - @Override - public String getOutput() { + Process su = Runtime.getRuntime().exec("su"); - String[] lastOut = Utils.readFileLines(Commons.getTempOutFile()); - if (lastOut == null) { - return this._cmdOut; - } + DataOutputStream stdin = new DataOutputStream(su.getOutputStream()); - String fout = ""; + Utils.logDebug("Testing root access: executing simple \"su\""); + stdin.writeBytes("exit\n"); + stdin.flush(); - for (String s : lastOut) { - fout += s + "\n"; - } + int res = su.waitFor(); - return fout; + Utils.logDebug("Simple \"su\" exitcode: " + res); + + return res; } |