public String sendCommand(String commandString) {
    String result = "";

    if (checkRoot()) {
      CommandCapture command = new CommandCapture(0, commandString);
      try {
        RootTools.getShell(true).add(command).waitForFinish();
        result = command.toString();
        LAST_COMMAND_OUTPUT = result;
        LAST_EXIT_CODE = command.exitCode();
      } catch (InterruptedException e) {
        // TODO: Do something useful with the exceptions
        // e.printStackTrace();
      } catch (IOException e) {
        // e.printStackTrace();
      } catch (TimeoutException e) {
        // e.printStackTrace();
      } catch (RootDeniedException e) {
        // e.printStackTrace();
      }
    }
    return result;
  }
  /**
   * This will take a path, which can contain the file name as well, and attempt to remount the
   * underlying partition.
   *
   * <p>For example, passing in the following string:
   * "/system/bin/some/directory/that/really/would/never/exist" will result in /system ultimately
   * being remounted. However, keep in mind that the longer the path you supply, the more work this
   * has to do, and the slower it will run.
   *
   * @param file file path
   * @param mountType mount type: pass in RO (Read only) or RW (Read Write)
   * @return a <code>boolean</code> which indicates whether or not the partition has been remounted
   *     as specified.
   */
  protected boolean remount(String file, String mountType) {

    // if the path has a trailing slash get rid of it.
    if (file.endsWith("/") && !file.equals("/")) {
      file = file.substring(0, file.lastIndexOf("/"));
    }
    // Make sure that what we are trying to remount is in the mount list.
    boolean foundMount = false;
    while (!foundMount) {
      try {
        for (Mount mount : RootTools.getMounts()) {
          RootTools.log(mount.getMountPoint().toString());

          if (file.equals(mount.getMountPoint().toString())) {
            foundMount = true;
            break;
          }
        }
      } catch (Exception e) {
        if (RootTools.debugMode) {
          e.printStackTrace();
        }
        return false;
      }
      if (!foundMount) {
        try {
          file = (new File(file).getParent()).toString();
        } catch (Exception e) {
          e.printStackTrace();
          return false;
        }
      }
    }
    Mount mountPoint = findMountPointRecursive(file);

    RootTools.log(
        InternalVariables.TAG,
        "Remounting "
            + mountPoint.getMountPoint().getAbsolutePath()
            + " as "
            + mountType.toLowerCase());
    final boolean isMountMode = mountPoint.getFlags().contains(mountType.toLowerCase());

    if (!isMountMode) {
      // grab an instance of the internal class
      try {
        CommandCapture command =
            new CommandCapture(
                0,
                "busybox mount -o remount,"
                    + mountType.toLowerCase()
                    + " "
                    + mountPoint.getDevice().getAbsolutePath()
                    + " "
                    + mountPoint.getMountPoint().getAbsolutePath(),
                "toolbox mount -o remount,"
                    + mountType.toLowerCase()
                    + " "
                    + mountPoint.getDevice().getAbsolutePath()
                    + " "
                    + mountPoint.getMountPoint().getAbsolutePath(),
                "mount -o remount,"
                    + mountType.toLowerCase()
                    + " "
                    + mountPoint.getDevice().getAbsolutePath()
                    + " "
                    + mountPoint.getMountPoint().getAbsolutePath(),
                "/system/bin/toolbox mount -o remount,"
                    + mountType.toLowerCase()
                    + " "
                    + mountPoint.getDevice().getAbsolutePath()
                    + " "
                    + mountPoint.getMountPoint().getAbsolutePath());

        Shell.startRootShell().add(command);
        command.waitForFinish();

      } catch (Exception e) {
      }

      mountPoint = findMountPointRecursive(file);
    }

    Log.i(InternalVariables.TAG, mountPoint.getFlags() + " AND " + mountType.toLowerCase());
    if (mountPoint.getFlags().contains(mountType.toLowerCase())) {
      RootTools.log(mountPoint.getFlags().toString());
      return true;
    } else {
      RootTools.log(mountPoint.getFlags().toString());
      return false;
    }
  }
Esempio n. 3
0
  /**
   * This method can be used to unpack a binary from the raw resources folder and store it in
   * /data/data/app.package/files/ This is typically useful if you provide your own C- or C++-based
   * binary. This binary can then be executed using sendShell() and its full path.
   *
   * @param sourceId resource id; typically <code>R.raw.id</code>
   * @param destName destination file name; appended to /data/data/app.package/files/
   * @param mode chmod value for this file
   * @return a <code>boolean</code> which indicates whether or not we were able to create the new
   *     file.
   */
  protected boolean installBinary(int sourceId, String destName, String mode) {
    File mf = new File(filesPath + File.separator + destName);
    if (!mf.exists()) {
      // First, does our files/ directory even exist?
      // We cannot wait for android to lazily create it as we will soon
      // need it.
      try {
        FileInputStream fis = context.openFileInput(BOGUS_FILE_NAME);
        fis.close();
      } catch (FileNotFoundException e) {
        FileOutputStream fos = null;
        try {
          fos = context.openFileOutput("bogus", Context.MODE_PRIVATE);
          fos.write("justcreatedfilesdirectory".getBytes());
        } catch (Exception ex) {
          if (RootTools.debugMode) {
            Log.e(LOG_TAG, ex.toString());
          }
          return false;
        } finally {
          if (null != fos) {
            try {
              fos.close();
              context.deleteFile(BOGUS_FILE_NAME);
            } catch (IOException e1) {
            }
          }
        }
      } catch (IOException ex) {
        if (RootTools.debugMode) {
          Log.e(LOG_TAG, ex.toString());
        }
        return false;
      }

      // Only now can we start creating our actual file
      InputStream iss = context.getResources().openRawResource(sourceId);
      FileOutputStream oss = null;
      try {
        oss = new FileOutputStream(mf);
        byte[] buffer = new byte[4096];
        int len;
        try {
          while (-1 != (len = iss.read(buffer))) {
            oss.write(buffer, 0, len);
          }
        } catch (IOException ex) {
          if (RootTools.debugMode) {
            Log.e(LOG_TAG, ex.toString());
          }
          return false;
        }
      } catch (FileNotFoundException ex) {
        if (RootTools.debugMode) {
          Log.e(LOG_TAG, ex.toString());
        }
        return false;
      } finally {
        if (oss != null) {
          try {
            oss.close();
          } catch (IOException e) {
          }
        }
      }
      try {
        iss.close();
      } catch (IOException ex) {
        if (RootTools.debugMode) {
          Log.e(LOG_TAG, ex.toString());
        }
        return false;
      }

      try {
        CommandCapture command =
            new CommandCapture(0, "chmod " + mode + " " + filesPath + File.separator + destName);
        Shell.startRootShell().add(command);
        command.waitForFinish();
      } catch (Exception e) {
      }
    }
    return true;
  }