Skip to content

Commit

Permalink
Added support for commands that run until manually finished/terminated
Browse files Browse the repository at this point in the history
  • Loading branch information
Stericson committed Jan 12, 2015
1 parent 2614b95 commit 269e6ed
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 72 deletions.
82 changes: 44 additions & 38 deletions src/com/stericson/RootShell/RootShell.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public class RootShell {

public static boolean debugMode = false;

public static final String version = "RootShell v1.2";
public static final String version = "RootShell v1.3";

/**
* Setting this to false will disable the handler that is used
Expand Down Expand Up @@ -176,29 +176,50 @@ public void commandOutput(int id, String line) {

/**
* @param binaryName String that represent the binary to find.
*
* @return <code>List<String></code> containing the locations the binary was found at.
*/
public static List<String> findBinary(final String binaryName) {
return findBinary(binaryName, null);
}

/**
* @param binaryName <code>String</code> that represent the binary to find.
* @param searchPaths <code>List<String></code> which contains the paths to search for this binary in.
*
* @return <code>List<String></code> containing the locations the binary was found at.
*/
public static List<String> findBinary(final String binaryName, List<String> searchPaths) {

final List<String> foundPaths = new ArrayList<String>();

boolean found = false;

final List<String> list = new ArrayList<String>();
String[] places = {
"/sbin/", "/system/bin/", "/system/xbin/", "/data/local/xbin/",
"/data/local/bin/", "/system/sd/xbin/", "/system/bin/failsafe/", "/data/local/"
};
if(searchPaths == null)
{
searchPaths = RootShell.getPath();
}

RootShell.log("Checking for " + binaryName);

//Try to use stat first
try {
for (final String path : places) {
for (String path : searchPaths) {

if(!path.endsWith("/"))
{
path += "/";
}

final String currentPath = path;

Command cc = new Command(0, false, "stat " + path + binaryName) {
@Override
public void commandOutput(int id, String line) {
if (line.contains("File: ") && line.contains(binaryName)) {
list.add(path);
foundPaths.add(currentPath);

RootShell.log(binaryName + " was found here: " + path);
RootShell.log(binaryName + " was found here: " + currentPath);
}

RootShell.log(line);
Expand All @@ -212,49 +233,33 @@ public void commandOutput(int id, String line) {

}

found = !list.isEmpty();
found = !foundPaths.isEmpty();
} catch (Exception e) {
RootShell.log(binaryName + " was not found, more information MAY be available with Debugging on.");
}

if (!found) {
RootShell.log("Trying second method");

for (String where : places) {
if (RootShell.exists(where + binaryName)) {
RootShell.log(binaryName + " was found here: " + where);
list.add(where);
found = true;
} else {
RootShell.log(binaryName + " was NOT found here: " + where);
for (String path : searchPaths) {

if(!path.endsWith("/"))
{
path += "/";
}
}
}

if (!found) {
RootShell.log("Trying third method");

try {
List<String> paths = RootShell.getPath();

if (paths != null) {
for (String path : paths) {
if (RootShell.exists(path + "/" + binaryName)) {
RootShell.log(binaryName + " was found here: " + path);
list.add(path);
} else {
RootShell.log(binaryName + " was NOT found here: " + path);
}
}
if (RootShell.exists(path + binaryName)) {
RootShell.log(binaryName + " was found here: " + path);
foundPaths.add(path);
} else {
RootShell.log(binaryName + " was NOT found here: " + path);
}
} catch (Exception e) {
RootShell.log(binaryName + " was not found, more information MAY be available with Debugging on.");
}
}

Collections.reverse(list);
Collections.reverse(foundPaths);

return list;
return foundPaths;
}

/**
Expand Down Expand Up @@ -520,6 +525,7 @@ private static void commandWait(Shell shell, Command cmd) throws Exception {
while (!cmd.isFinished()) {

RootShell.log(version, shell.getCommandQueuePositionString(cmd));
RootShell.log(version, "Processed " + cmd.totalOutputProcessed + " of " + cmd.totalOutput + " output from command.");

synchronized (cmd) {
try {
Expand Down
2 changes: 1 addition & 1 deletion src/com/stericson/RootShell/containers/RootClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ static void displayError(Exception e) {
// and immediately re-generate the necessary jar file.
static public class AnnotationsFinder {

private final String AVOIDDIRPATH = "stericson" + File.separator + "RootTools" + File.separator;
private final String AVOIDDIRPATH = "stericson" + File.separator + "RootShell" + File.separator;

private List<File> classFiles;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* This file is part of the RootShell Project: http://code.google.com/p/roottools/
* This file is part of the RootShell Project: https://github.com/Stericson/RootShell
*
* Copyright (c) 2014 Stephen Erickson, Chris Ravenscroft
*
Expand Down
60 changes: 44 additions & 16 deletions src/com/stericson/RootShell/execution/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,6 @@ public void commandCompleted(int id, int exitcode) {
//pass
}

protected final void finishCommand() {
executing = false;
finished = true;
this.notifyAll();
}

protected final void commandFinished() {
if (!terminated) {
synchronized (this) {
Expand Down Expand Up @@ -158,6 +152,19 @@ private void createHandler(boolean handlerEnabled) {
}
}

public final void finish()
{
RootShell.log("Command finished at users request!");
commandFinished();
}

protected final void finishCommand() {
executing = false;
finished = true;
this.notifyAll();
}


public final String getCommand() {
StringBuilder sb = new StringBuilder();

Expand Down Expand Up @@ -201,7 +208,13 @@ protected final void startExecution() {
executing = true;
}

public final void terminate(String reason) {
public final void terminate()
{
RootShell.log("Terminating command at users request!");
terminated("Terminated at users request!");
}

protected final void terminate(String reason) {
try {
Shell.closeAll();
RootShell.log("Terminating all shells.");
Expand Down Expand Up @@ -246,21 +259,36 @@ protected final void output(int id, String line) {
}
}

public final void resetCommand()
{
this.finished = false;
this.totalOutput = 0;
this.totalOutputProcessed = 0;
this.executing = false;
this.terminated = false;
this.exitCode = -1;
}

private class ExecutionMonitor extends Thread {

public void run() {
while (!finished) {

synchronized (Command.this) {
try {
Command.this.wait(timeout);
} catch (InterruptedException e) {
if(timeout > 0)
{
//We need to kill the command after the given timeout
while (!finished) {

synchronized (Command.this) {
try {
Command.this.wait(timeout);
} catch (InterruptedException e) {
}
}
}

if (!finished) {
RootShell.log("Timeout Exception has occurred.");
terminate("Timeout Exception");
if (!finished) {
RootShell.log("Timeout Exception has occurred.");
terminate("Timeout Exception");
}
}
}
}
Expand Down
38 changes: 23 additions & 15 deletions src/com/stericson/RootShell/execution/Shell.java
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,9 @@ public Command add(Command command) throws IOException {
//Don't add commands while cleaning
;
}

command.resetCommand();

this.commands.add(command);

this.notifyThreads();
Expand Down Expand Up @@ -711,33 +714,31 @@ public void run() {


/**
* We will wait a bit for output to be processed...
* wait for output to be processed...
*
* MAX, 10 iterations
*/
int iterations = 0;
while (command.totalOutput > command.totalOutputProcessed) {

final int MAX_ITERATIONS = 10;

if(iterations == 0)
{
iterations++;
RootShell.log("Waiting for output to be processed. " + command.totalOutputProcessed + " Of " + command.totalOutput);
}
else if (iterations > MAX_ITERATIONS) {
RootShell.log(RootShell.version, "All output not processed! Did you forget the super call for commandOutput???", RootShell.LogLevel.WARN, null);
RootShell.log(RootShell.version, command.totalOutputProcessed + " Of " + command.totalOutput + " processed", RootShell.LogLevel.WARN, null);
RootShell.log(RootShell.version, "This doesn't mean there is a problem, just that we couldn't confirm that all output was processed.", RootShell.LogLevel.WARN, null);
break;
}

try {
iterations++;
this.wait(1000);

synchronized (this)
{
this.wait(2000);
}
} catch (Exception e) {
RootShell.log(e.getMessage());
}
}

RootShell.log("Read all output");

command.setExitCode(exitCode);
command.commandFinished();
command = null;
Expand All @@ -750,8 +751,6 @@ else if (iterations > MAX_ITERATIONS) {
}
}

RootShell.log("Read all output");

try {
proc.waitFor();
proc.destroy();
Expand All @@ -763,7 +762,16 @@ else if (iterations > MAX_ITERATIONS) {
command = commands.get(read);
}

command.terminated("Unexpected Termination.");
if(command.totalOutput < command.totalOutputProcessed)
{
command.terminated("All output not processed!");
command.terminated("Did you forget the super.commandOutput call or are you waiting on the command object?");
}
else
{
command.terminated("Unexpected Termination.");
}

command = null;
read++;
}
Expand Down
6 changes: 5 additions & 1 deletion src/com/stericson/RootShellTests/SanityCheckRootShell.java
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,10 @@ public void run()
visualUpdate(TestHandler.ACTION_DISPLAY, "[ Checking Root ]\n");
visualUpdate(TestHandler.ACTION_DISPLAY, result + " k\n\n");

result = RootShell.isBusyboxAvailable();
visualUpdate(TestHandler.ACTION_DISPLAY, "[ Checking Busybox ]\n");
visualUpdate(TestHandler.ACTION_DISPLAY, result + " k\n\n");

visualUpdate(TestHandler.ACTION_PDISPLAY, "Testing file exists");
visualUpdate(TestHandler.ACTION_DISPLAY, "[ Checking Exists() ]\n");
visualUpdate(TestHandler.ACTION_DISPLAY, RootShell.exists("/system/sbin/[") + " k\n\n");
Expand Down Expand Up @@ -223,7 +227,7 @@ public void run()
public void commandOutput(int id, String line)
{
visualUpdate(TestHandler.ACTION_DISPLAY, line + "\n");
super.commandOutput(id, line);
//super.commandOutput(id, line);
}
};
shell.add(cmd);
Expand Down

0 comments on commit 269e6ed

Please sign in to comment.