- protected void runAsRoot(String command) throws java.io.IOException,java.lang.InterruptedException {
- Process rootcmd = Runtime.getRuntime().exec(new String[]{"su","-c","sh"});
- DataOutputStream sh = new DataOutputStream(rootcmd.getOutputStream());
- sh.writeBytes(command + "\n");
- sh.writeBytes("exit\n");
- sh.flush();
- sh.close();
+ protected void runAsRoot(String command) throws NotRootedException,ShellException {
+ Process rootcmd;
+
+ Log.i(TAG, "Running as root: " + command);
+ try {
+ rootcmd = Runtime.getRuntime().exec(new String[]{"su","-c","sh"});
+ } catch (java.io.IOException e) {
+ Log.e(TAG, "Got IOException: " + e.getMessage() + " (" + e.getCause() + ")");
+ throw new NotRootedException();
+ }
+
+ try {
+ DataOutputStream sh = new DataOutputStream(rootcmd.getOutputStream());
+ sh.writeBytes(command + "\n");
+ sh.writeBytes("exit\n");
+ sh.flush();
+ sh.close();
+ } catch (java.io.IOException e) {
+ Log.e(TAG, "Got IOException: " + e.getMessage() + " (" + e.getCause() + ")");
+ throw new ShellException();
+ }
+
+ try {
+ int r = rootcmd.waitFor();
+
+ if (r != 0) {
+ Log.e(TAG, "Process returned: " + r);
+ throw new ShellException();
+ }
+ } catch (java.lang.InterruptedException e) {
+ Log.e(TAG, "Got InterruptedException: " + e.getMessage() + " (" + e.getCause() + ")");
+ throw new ShellException();
+ }