First mac commit
This commit is contained in:
66
libraries/RootCommands/.gitignore
vendored
66
libraries/RootCommands/.gitignore
vendored
@@ -1,33 +1,33 @@
|
||||
#Android specific
|
||||
bin
|
||||
gen
|
||||
obj
|
||||
libs/armeabi
|
||||
lint.xml
|
||||
local.properties
|
||||
release.properties
|
||||
ant.properties
|
||||
*.class
|
||||
*.apk
|
||||
|
||||
#Gradle
|
||||
.gradle
|
||||
build
|
||||
gradle.properties
|
||||
gradlew
|
||||
gradlew.bat
|
||||
gradle
|
||||
|
||||
#Maven
|
||||
target
|
||||
pom.xml.*
|
||||
|
||||
#Eclipse
|
||||
.project
|
||||
.classpath
|
||||
.settings
|
||||
.metadata
|
||||
|
||||
#IntelliJ IDEA
|
||||
.idea
|
||||
*.iml
|
||||
#Android specific
|
||||
bin
|
||||
gen
|
||||
obj
|
||||
libs/armeabi
|
||||
lint.xml
|
||||
local.properties
|
||||
release.properties
|
||||
ant.properties
|
||||
*.class
|
||||
*.apk
|
||||
|
||||
#Gradle
|
||||
.gradle
|
||||
build
|
||||
gradle.properties
|
||||
gradlew
|
||||
gradlew.bat
|
||||
gradle
|
||||
|
||||
#Maven
|
||||
target
|
||||
pom.xml.*
|
||||
|
||||
#Eclipse
|
||||
.project
|
||||
.classpath
|
||||
.settings
|
||||
.metadata
|
||||
|
||||
#IntelliJ IDEA
|
||||
.idea
|
||||
*.iml
|
||||
|
@@ -1,30 +1,30 @@
|
||||
|
||||
|
||||
buildscript {
|
||||
repositories {
|
||||
mavenCentral()
|
||||
}
|
||||
dependencies {
|
||||
classpath 'com.android.tools.build:gradle:0.12.+'
|
||||
}
|
||||
}
|
||||
|
||||
apply plugin: 'com.android.library'
|
||||
|
||||
dependencies {
|
||||
}
|
||||
|
||||
android {
|
||||
compileSdkVersion 21
|
||||
buildToolsVersion '21.0.2'
|
||||
|
||||
defaultConfig {
|
||||
minSdkVersion 7
|
||||
targetSdkVersion 21
|
||||
}
|
||||
compileOptions {
|
||||
sourceCompatibility JavaVersion.VERSION_1_7
|
||||
targetCompatibility JavaVersion.VERSION_1_7
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
buildscript {
|
||||
repositories {
|
||||
mavenCentral()
|
||||
}
|
||||
dependencies {
|
||||
classpath 'com.android.tools.build:gradle:0.12.+'
|
||||
}
|
||||
}
|
||||
|
||||
apply plugin: 'com.android.library'
|
||||
|
||||
dependencies {
|
||||
}
|
||||
|
||||
android {
|
||||
compileSdkVersion 21
|
||||
buildToolsVersion '21.0.2'
|
||||
|
||||
defaultConfig {
|
||||
minSdkVersion 7
|
||||
targetSdkVersion 21
|
||||
}
|
||||
compileOptions {
|
||||
sourceCompatibility JavaVersion.VERSION_1_7
|
||||
targetCompatibility JavaVersion.VERSION_1_7
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -1,15 +1,15 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
|
||||
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
package="org.sufficientlysecure.rootcommands"
|
||||
android:versionCode="3"
|
||||
android:versionName="1.2" >
|
||||
|
||||
<uses-sdk
|
||||
android:minSdkVersion="7"
|
||||
android:targetSdkVersion="17" />
|
||||
|
||||
<application />
|
||||
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
|
||||
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
package="org.sufficientlysecure.rootcommands"
|
||||
android:versionCode="3"
|
||||
android:versionName="1.2" >
|
||||
|
||||
<uses-sdk
|
||||
android:minSdkVersion="7"
|
||||
android:targetSdkVersion="17" />
|
||||
|
||||
<application />
|
||||
|
||||
</manifest>
|
@@ -1,43 +1,43 @@
|
||||
|
||||
|
||||
package org.sufficientlysecure.rootcommands;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
public class Mount {
|
||||
protected final File mDevice;
|
||||
protected final File mMountPoint;
|
||||
protected final String mType;
|
||||
protected final Set<String> mFlags;
|
||||
|
||||
Mount(File device, File path, String type, String flagsStr) {
|
||||
mDevice = device;
|
||||
mMountPoint = path;
|
||||
mType = type;
|
||||
mFlags = new HashSet<String>(Arrays.asList(flagsStr.split(",")));
|
||||
}
|
||||
|
||||
public File getDevice() {
|
||||
return mDevice;
|
||||
}
|
||||
|
||||
public File getMountPoint() {
|
||||
return mMountPoint;
|
||||
}
|
||||
|
||||
public String getType() {
|
||||
return mType;
|
||||
}
|
||||
|
||||
public Set<String> getFlags() {
|
||||
return mFlags;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("%s on %s type %s %s", mDevice, mMountPoint, mType, mFlags);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
package org.sufficientlysecure.rootcommands;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
public class Mount {
|
||||
protected final File mDevice;
|
||||
protected final File mMountPoint;
|
||||
protected final String mType;
|
||||
protected final Set<String> mFlags;
|
||||
|
||||
Mount(File device, File path, String type, String flagsStr) {
|
||||
mDevice = device;
|
||||
mMountPoint = path;
|
||||
mType = type;
|
||||
mFlags = new HashSet<String>(Arrays.asList(flagsStr.split(",")));
|
||||
}
|
||||
|
||||
public File getDevice() {
|
||||
return mDevice;
|
||||
}
|
||||
|
||||
public File getMountPoint() {
|
||||
return mMountPoint;
|
||||
}
|
||||
|
||||
public String getType() {
|
||||
return mType;
|
||||
}
|
||||
|
||||
public Set<String> getFlags() {
|
||||
return mFlags;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("%s on %s type %s %s", mDevice, mMountPoint, mType, mFlags);
|
||||
}
|
||||
}
|
||||
|
@@ -1,176 +1,176 @@
|
||||
|
||||
|
||||
package org.sufficientlysecure.rootcommands;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.io.LineNumberReader;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Locale;
|
||||
|
||||
import org.sufficientlysecure.rootcommands.command.SimpleCommand;
|
||||
import org.sufficientlysecure.rootcommands.util.Log;
|
||||
|
||||
//no modifier, this means it is package-private. Only our internal classes can use this.
|
||||
class Remounter {
|
||||
|
||||
private Shell shell;
|
||||
|
||||
public Remounter(Shell shell) {
|
||||
super();
|
||||
this.shell = shell;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 : getMounts()) {
|
||||
Log.d(RootCommands.TAG, mount.getMountPoint().toString());
|
||||
|
||||
if (file.equals(mount.getMountPoint().toString())) {
|
||||
foundMount = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
Log.e(RootCommands.TAG, "Exception", e);
|
||||
return false;
|
||||
}
|
||||
if (!foundMount) {
|
||||
try {
|
||||
file = (new File(file).getParent()).toString();
|
||||
} catch (Exception e) {
|
||||
Log.e(RootCommands.TAG, "Exception", e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
Mount mountPoint = findMountPointRecursive(file);
|
||||
|
||||
Log.d(RootCommands.TAG, "Remounting " + mountPoint.getMountPoint().getAbsolutePath()
|
||||
+ " as " + mountType.toLowerCase(Locale.US));
|
||||
final boolean isMountMode = mountPoint.getFlags().contains(mountType.toLowerCase(Locale.US));
|
||||
|
||||
if (!isMountMode) {
|
||||
// grab an instance of the internal class
|
||||
try {
|
||||
SimpleCommand command = new SimpleCommand("busybox mount -o remount,"
|
||||
+ mountType.toLowerCase(Locale.US) + " " + mountPoint.getDevice().getAbsolutePath()
|
||||
+ " " + mountPoint.getMountPoint().getAbsolutePath(),
|
||||
"toolbox mount -o remount," + mountType.toLowerCase(Locale.US) + " "
|
||||
+ mountPoint.getDevice().getAbsolutePath() + " "
|
||||
+ mountPoint.getMountPoint().getAbsolutePath(), "mount -o remount,"
|
||||
+ mountType.toLowerCase(Locale.US) + " "
|
||||
+ mountPoint.getDevice().getAbsolutePath() + " "
|
||||
+ mountPoint.getMountPoint().getAbsolutePath(),
|
||||
"/system/bin/toolbox mount -o remount," + mountType.toLowerCase(Locale.US) + " "
|
||||
+ mountPoint.getDevice().getAbsolutePath() + " "
|
||||
+ mountPoint.getMountPoint().getAbsolutePath());
|
||||
|
||||
// execute on shell
|
||||
shell.add(command).waitForFinish();
|
||||
|
||||
} catch (Exception e) {
|
||||
}
|
||||
|
||||
mountPoint = findMountPointRecursive(file);
|
||||
}
|
||||
|
||||
if (mountPoint != null) {
|
||||
Log.d(RootCommands.TAG, mountPoint.getFlags() + " AND " + mountType.toLowerCase(Locale.US));
|
||||
if (mountPoint.getFlags().contains(mountType.toLowerCase(Locale.US))) {
|
||||
Log.d(RootCommands.TAG, mountPoint.getFlags().toString());
|
||||
return true;
|
||||
} else {
|
||||
Log.d(RootCommands.TAG, mountPoint.getFlags().toString());
|
||||
}
|
||||
} else {
|
||||
Log.d(RootCommands.TAG, "mountPoint is null");
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private Mount findMountPointRecursive(String file) {
|
||||
try {
|
||||
ArrayList<Mount> mounts = getMounts();
|
||||
for (File path = new File(file); path != null;) {
|
||||
for (Mount mount : mounts) {
|
||||
if (mount.getMountPoint().equals(path)) {
|
||||
return mount;
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
} catch (Exception e) {
|
||||
Log.e(RootCommands.TAG, "Exception", e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* This will return an ArrayList of the class Mount. The class mount contains the following
|
||||
* property's: device mountPoint type flags
|
||||
* <p/>
|
||||
* These will provide you with any information you need to work with the mount points.
|
||||
*
|
||||
* @return <code>ArrayList<Mount></code> an ArrayList of the class Mount.
|
||||
* @throws Exception
|
||||
* if we cannot return the mount points.
|
||||
*/
|
||||
protected static ArrayList<Mount> getMounts() throws Exception {
|
||||
|
||||
final String tempFile = "/data/local/RootToolsMounts";
|
||||
|
||||
// copy /proc/mounts to tempfile. Directly reading it does not work on 4.3
|
||||
Shell shell = Shell.startRootShell();
|
||||
Toolbox tb = new Toolbox(shell);
|
||||
tb.copyFile("/proc/mounts", tempFile, false, false);
|
||||
tb.setFilePermissions(tempFile, "777");
|
||||
shell.close();
|
||||
|
||||
LineNumberReader lnr = null;
|
||||
lnr = new LineNumberReader(new FileReader(tempFile));
|
||||
String line;
|
||||
ArrayList<Mount> mounts = new ArrayList<Mount>();
|
||||
while ((line = lnr.readLine()) != null) {
|
||||
|
||||
Log.d(RootCommands.TAG, line);
|
||||
|
||||
String[] fields = line.split(" ");
|
||||
mounts.add(new Mount(new File(fields[0]), // device
|
||||
new File(fields[1]), // mountPoint
|
||||
fields[2], // fstype
|
||||
fields[3] // flags
|
||||
));
|
||||
}
|
||||
lnr.close();
|
||||
|
||||
return mounts;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
package org.sufficientlysecure.rootcommands;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.io.LineNumberReader;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Locale;
|
||||
|
||||
import org.sufficientlysecure.rootcommands.command.SimpleCommand;
|
||||
import org.sufficientlysecure.rootcommands.util.Log;
|
||||
|
||||
//no modifier, this means it is package-private. Only our internal classes can use this.
|
||||
class Remounter {
|
||||
|
||||
private Shell shell;
|
||||
|
||||
public Remounter(Shell shell) {
|
||||
super();
|
||||
this.shell = shell;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 : getMounts()) {
|
||||
Log.d(RootCommands.TAG, mount.getMountPoint().toString());
|
||||
|
||||
if (file.equals(mount.getMountPoint().toString())) {
|
||||
foundMount = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
Log.e(RootCommands.TAG, "Exception", e);
|
||||
return false;
|
||||
}
|
||||
if (!foundMount) {
|
||||
try {
|
||||
file = (new File(file).getParent()).toString();
|
||||
} catch (Exception e) {
|
||||
Log.e(RootCommands.TAG, "Exception", e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
Mount mountPoint = findMountPointRecursive(file);
|
||||
|
||||
Log.d(RootCommands.TAG, "Remounting " + mountPoint.getMountPoint().getAbsolutePath()
|
||||
+ " as " + mountType.toLowerCase(Locale.US));
|
||||
final boolean isMountMode = mountPoint.getFlags().contains(mountType.toLowerCase(Locale.US));
|
||||
|
||||
if (!isMountMode) {
|
||||
// grab an instance of the internal class
|
||||
try {
|
||||
SimpleCommand command = new SimpleCommand("busybox mount -o remount,"
|
||||
+ mountType.toLowerCase(Locale.US) + " " + mountPoint.getDevice().getAbsolutePath()
|
||||
+ " " + mountPoint.getMountPoint().getAbsolutePath(),
|
||||
"toolbox mount -o remount," + mountType.toLowerCase(Locale.US) + " "
|
||||
+ mountPoint.getDevice().getAbsolutePath() + " "
|
||||
+ mountPoint.getMountPoint().getAbsolutePath(), "mount -o remount,"
|
||||
+ mountType.toLowerCase(Locale.US) + " "
|
||||
+ mountPoint.getDevice().getAbsolutePath() + " "
|
||||
+ mountPoint.getMountPoint().getAbsolutePath(),
|
||||
"/system/bin/toolbox mount -o remount," + mountType.toLowerCase(Locale.US) + " "
|
||||
+ mountPoint.getDevice().getAbsolutePath() + " "
|
||||
+ mountPoint.getMountPoint().getAbsolutePath());
|
||||
|
||||
// execute on shell
|
||||
shell.add(command).waitForFinish();
|
||||
|
||||
} catch (Exception e) {
|
||||
}
|
||||
|
||||
mountPoint = findMountPointRecursive(file);
|
||||
}
|
||||
|
||||
if (mountPoint != null) {
|
||||
Log.d(RootCommands.TAG, mountPoint.getFlags() + " AND " + mountType.toLowerCase(Locale.US));
|
||||
if (mountPoint.getFlags().contains(mountType.toLowerCase(Locale.US))) {
|
||||
Log.d(RootCommands.TAG, mountPoint.getFlags().toString());
|
||||
return true;
|
||||
} else {
|
||||
Log.d(RootCommands.TAG, mountPoint.getFlags().toString());
|
||||
}
|
||||
} else {
|
||||
Log.d(RootCommands.TAG, "mountPoint is null");
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private Mount findMountPointRecursive(String file) {
|
||||
try {
|
||||
ArrayList<Mount> mounts = getMounts();
|
||||
for (File path = new File(file); path != null;) {
|
||||
for (Mount mount : mounts) {
|
||||
if (mount.getMountPoint().equals(path)) {
|
||||
return mount;
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
} catch (Exception e) {
|
||||
Log.e(RootCommands.TAG, "Exception", e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* This will return an ArrayList of the class Mount. The class mount contains the following
|
||||
* property's: device mountPoint type flags
|
||||
* <p/>
|
||||
* These will provide you with any information you need to work with the mount points.
|
||||
*
|
||||
* @return <code>ArrayList<Mount></code> an ArrayList of the class Mount.
|
||||
* @throws Exception
|
||||
* if we cannot return the mount points.
|
||||
*/
|
||||
protected static ArrayList<Mount> getMounts() throws Exception {
|
||||
|
||||
final String tempFile = "/data/local/RootToolsMounts";
|
||||
|
||||
// copy /proc/mounts to tempfile. Directly reading it does not work on 4.3
|
||||
Shell shell = Shell.startRootShell();
|
||||
Toolbox tb = new Toolbox(shell);
|
||||
tb.copyFile("/proc/mounts", tempFile, false, false);
|
||||
tb.setFilePermissions(tempFile, "777");
|
||||
shell.close();
|
||||
|
||||
LineNumberReader lnr = null;
|
||||
lnr = new LineNumberReader(new FileReader(tempFile));
|
||||
String line;
|
||||
ArrayList<Mount> mounts = new ArrayList<Mount>();
|
||||
while ((line = lnr.readLine()) != null) {
|
||||
|
||||
Log.d(RootCommands.TAG, line);
|
||||
|
||||
String[] fields = line.split(" ");
|
||||
mounts.add(new Mount(new File(fields[0]), // device
|
||||
new File(fields[1]), // mountPoint
|
||||
fields[2], // fstype
|
||||
fields[3] // flags
|
||||
));
|
||||
}
|
||||
lnr.close();
|
||||
|
||||
return mounts;
|
||||
}
|
||||
}
|
||||
|
@@ -1,36 +1,36 @@
|
||||
|
||||
|
||||
package org.sufficientlysecure.rootcommands;
|
||||
|
||||
import org.sufficientlysecure.rootcommands.util.Log;
|
||||
|
||||
public class RootCommands {
|
||||
public static boolean DEBUG = false;
|
||||
public static int DEFAULT_TIMEOUT = 10000;
|
||||
|
||||
public static final String TAG = "RootCommands";
|
||||
|
||||
/**
|
||||
* General method to check if user has su binary and accepts root access for this program!
|
||||
*
|
||||
* @return true if everything worked
|
||||
*/
|
||||
public static boolean rootAccessGiven() {
|
||||
boolean rootAccess = false;
|
||||
|
||||
try {
|
||||
Shell rootShell = Shell.startRootShell();
|
||||
|
||||
Toolbox tb = new Toolbox(rootShell);
|
||||
if (tb.isRootAccessGiven()) {
|
||||
rootAccess = true;
|
||||
}
|
||||
|
||||
rootShell.close();
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "Problem while checking for root access!", e);
|
||||
}
|
||||
|
||||
return rootAccess;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
package org.sufficientlysecure.rootcommands;
|
||||
|
||||
import org.sufficientlysecure.rootcommands.util.Log;
|
||||
|
||||
public class RootCommands {
|
||||
public static boolean DEBUG = false;
|
||||
public static int DEFAULT_TIMEOUT = 10000;
|
||||
|
||||
public static final String TAG = "RootCommands";
|
||||
|
||||
/**
|
||||
* General method to check if user has su binary and accepts root access for this program!
|
||||
*
|
||||
* @return true if everything worked
|
||||
*/
|
||||
public static boolean rootAccessGiven() {
|
||||
boolean rootAccess = false;
|
||||
|
||||
try {
|
||||
Shell rootShell = Shell.startRootShell();
|
||||
|
||||
Toolbox tb = new Toolbox(rootShell);
|
||||
if (tb.isRootAccessGiven()) {
|
||||
rootAccess = true;
|
||||
}
|
||||
|
||||
rootShell.close();
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "Problem while checking for root access!", e);
|
||||
}
|
||||
|
||||
return rootAccess;
|
||||
}
|
||||
}
|
||||
|
@@ -1,331 +1,331 @@
|
||||
|
||||
|
||||
package org.sufficientlysecure.rootcommands;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.Closeable;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.sufficientlysecure.rootcommands.command.Command;
|
||||
import org.sufficientlysecure.rootcommands.util.Log;
|
||||
import org.sufficientlysecure.rootcommands.util.RootAccessDeniedException;
|
||||
import org.sufficientlysecure.rootcommands.util.Utils;
|
||||
|
||||
public class Shell implements Closeable {
|
||||
private final Process shellProcess;
|
||||
private final BufferedReader stdOutErr;
|
||||
private final DataOutputStream outputStream;
|
||||
private final List<Command> commands = new ArrayList<Command>();
|
||||
private boolean close = false;
|
||||
|
||||
private static final String LD_LIBRARY_PATH = System.getenv("LD_LIBRARY_PATH");
|
||||
private static final String token = "F*D^W@#FGF";
|
||||
|
||||
/**
|
||||
* Start root shell
|
||||
*
|
||||
* @param customEnv
|
||||
* @param baseDirectory
|
||||
* @return
|
||||
* @throws java.io.IOException
|
||||
*/
|
||||
public static Shell startRootShell(ArrayList<String> customEnv, String baseDirectory)
|
||||
throws IOException, RootAccessDeniedException {
|
||||
Log.d(RootCommands.TAG, "Starting Root Shell!");
|
||||
|
||||
// On some versions of Android (ICS) LD_LIBRARY_PATH is unset when using su
|
||||
// We need to pass LD_LIBRARY_PATH over su for some commands to work correctly.
|
||||
if (customEnv == null) {
|
||||
customEnv = new ArrayList<String>();
|
||||
}
|
||||
customEnv.add("LD_LIBRARY_PATH=" + LD_LIBRARY_PATH);
|
||||
|
||||
Shell shell = new Shell(Utils.getSuPath(), customEnv, baseDirectory);
|
||||
|
||||
return shell;
|
||||
}
|
||||
|
||||
/**
|
||||
* Start root shell without custom environment and base directory
|
||||
*
|
||||
* @return
|
||||
* @throws java.io.IOException
|
||||
*/
|
||||
public static Shell startRootShell() throws IOException, RootAccessDeniedException {
|
||||
return startRootShell(null, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Start default sh shell
|
||||
*
|
||||
* @param customEnv
|
||||
* @param baseDirectory
|
||||
* @return
|
||||
* @throws java.io.IOException
|
||||
*/
|
||||
public static Shell startShell(ArrayList<String> customEnv, String baseDirectory)
|
||||
throws IOException {
|
||||
Log.d(RootCommands.TAG, "Starting Shell!");
|
||||
Shell shell = new Shell("sh", customEnv, baseDirectory);
|
||||
return shell;
|
||||
}
|
||||
|
||||
/**
|
||||
* Start default sh shell without custom environment and base directory
|
||||
*
|
||||
* @return
|
||||
* @throws java.io.IOException
|
||||
*/
|
||||
public static Shell startShell() throws IOException {
|
||||
return startShell(null, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Start custom shell defined by shellPath
|
||||
*
|
||||
* @param shellPath
|
||||
* @param customEnv
|
||||
* @param baseDirectory
|
||||
* @return
|
||||
* @throws java.io.IOException
|
||||
*/
|
||||
public static Shell startCustomShell(String shellPath, ArrayList<String> customEnv,
|
||||
String baseDirectory) throws IOException {
|
||||
Log.d(RootCommands.TAG, "Starting Custom Shell!");
|
||||
Shell shell = new Shell(shellPath, customEnv, baseDirectory);
|
||||
|
||||
return shell;
|
||||
}
|
||||
|
||||
/**
|
||||
* Start custom shell without custom environment and base directory
|
||||
*
|
||||
* @param shellPath
|
||||
* @return
|
||||
* @throws java.io.IOException
|
||||
*/
|
||||
public static Shell startCustomShell(String shellPath) throws IOException {
|
||||
return startCustomShell(shellPath, null, null);
|
||||
}
|
||||
|
||||
private Shell(String shell, ArrayList<String> customEnv, String baseDirectory)
|
||||
throws IOException, RootAccessDeniedException {
|
||||
Log.d(RootCommands.TAG, "Starting shell: " + shell);
|
||||
|
||||
// start shell process!
|
||||
shellProcess = Utils.runWithEnv(shell, customEnv, baseDirectory);
|
||||
|
||||
// StdErr is redirected to StdOut, defined in Command.getCommand()
|
||||
stdOutErr = new BufferedReader(new InputStreamReader(shellProcess.getInputStream()));
|
||||
outputStream = new DataOutputStream(shellProcess.getOutputStream());
|
||||
|
||||
outputStream.write("echo Started\n".getBytes());
|
||||
outputStream.flush();
|
||||
|
||||
while (true) {
|
||||
String line = stdOutErr.readLine();
|
||||
if (line == null)
|
||||
throw new RootAccessDeniedException(
|
||||
"stdout line is null! Access was denied or this executeable is not a shell!");
|
||||
if ("".equals(line))
|
||||
continue;
|
||||
if ("Started".equals(line))
|
||||
break;
|
||||
|
||||
destroyShellProcess();
|
||||
throw new IOException("Unable to start shell, unexpected output \"" + line + "\"");
|
||||
}
|
||||
|
||||
new Thread(inputRunnable, "Shell Input").start();
|
||||
new Thread(outputRunnable, "Shell Output").start();
|
||||
}
|
||||
|
||||
private Runnable inputRunnable = new Runnable() {
|
||||
public void run() {
|
||||
try {
|
||||
writeCommands();
|
||||
} catch (IOException e) {
|
||||
Log.e(RootCommands.TAG, "IO Exception", e);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
private Runnable outputRunnable = new Runnable() {
|
||||
public void run() {
|
||||
try {
|
||||
readOutput();
|
||||
} catch (IOException e) {
|
||||
Log.e(RootCommands.TAG, "IOException", e);
|
||||
} catch (InterruptedException e) {
|
||||
Log.e(RootCommands.TAG, "InterruptedException", e);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Destroy shell process considering that the process could already be terminated
|
||||
*/
|
||||
private void destroyShellProcess() {
|
||||
try {
|
||||
// Yes, this really is the way to check if the process is
|
||||
// still running.
|
||||
shellProcess.exitValue();
|
||||
} catch (IllegalThreadStateException e) {
|
||||
// Only call destroy() if the process is still running;
|
||||
// Calling it for a terminated process will not crash, but
|
||||
// (starting with at least ICS/4.0) spam the log with INFO
|
||||
// messages ala "Failed to destroy process" and "kill
|
||||
// failed: ESRCH (No such process)".
|
||||
shellProcess.destroy();
|
||||
}
|
||||
|
||||
Log.d(RootCommands.TAG, "Shell destroyed");
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes queued commands one after another into the opened shell. After an execution a token is
|
||||
* written to seperate command output on read
|
||||
*
|
||||
* @throws java.io.IOException
|
||||
*/
|
||||
private void writeCommands() throws IOException {
|
||||
try {
|
||||
int commandIndex = 0;
|
||||
while (true) {
|
||||
DataOutputStream out;
|
||||
synchronized (commands) {
|
||||
while (!close && commandIndex >= commands.size()) {
|
||||
commands.wait();
|
||||
}
|
||||
out = this.outputStream;
|
||||
}
|
||||
if (commandIndex < commands.size()) {
|
||||
Command next = commands.get(commandIndex);
|
||||
next.writeCommand(out);
|
||||
String line = "\necho " + token + " " + commandIndex + " $?\n";
|
||||
out.write(line.getBytes());
|
||||
out.flush();
|
||||
commandIndex++;
|
||||
} else if (close) {
|
||||
out.write("\nexit 0\n".getBytes());
|
||||
out.flush();
|
||||
out.close();
|
||||
Log.d(RootCommands.TAG, "Closing shell");
|
||||
return;
|
||||
}
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
Log.e(RootCommands.TAG, "interrupted while writing command", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads output line by line, seperated by token written after every command
|
||||
*
|
||||
* @throws java.io.IOException
|
||||
* @throws InterruptedException
|
||||
*/
|
||||
private void readOutput() throws IOException, InterruptedException {
|
||||
Command command = null;
|
||||
|
||||
// index of current command
|
||||
int commandIndex = 0;
|
||||
while (true) {
|
||||
String lineStdOut = stdOutErr.readLine();
|
||||
|
||||
// terminate on EOF
|
||||
if (lineStdOut == null)
|
||||
break;
|
||||
|
||||
if (command == null) {
|
||||
|
||||
// break on close after last command
|
||||
if (commandIndex >= commands.size()) {
|
||||
if (close)
|
||||
break;
|
||||
continue;
|
||||
}
|
||||
|
||||
// get current command
|
||||
command = commands.get(commandIndex);
|
||||
}
|
||||
|
||||
int pos = lineStdOut.indexOf(token);
|
||||
if (pos > 0) {
|
||||
command.processOutput(lineStdOut.substring(0, pos));
|
||||
}
|
||||
if (pos >= 0) {
|
||||
lineStdOut = lineStdOut.substring(pos);
|
||||
String fields[] = lineStdOut.split(" ");
|
||||
int id = Integer.parseInt(fields[1]);
|
||||
if (id == commandIndex) {
|
||||
command.setExitCode(Integer.parseInt(fields[2]));
|
||||
|
||||
// go to next command
|
||||
commandIndex++;
|
||||
command = null;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
command.processOutput(lineStdOut);
|
||||
}
|
||||
Log.d(RootCommands.TAG, "Read all output");
|
||||
shellProcess.waitFor();
|
||||
destroyShellProcess();
|
||||
|
||||
while (commandIndex < commands.size()) {
|
||||
if (command == null) {
|
||||
command = commands.get(commandIndex);
|
||||
}
|
||||
command.terminated("Unexpected Termination!");
|
||||
commandIndex++;
|
||||
command = null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add command to shell queue
|
||||
*
|
||||
* @param command
|
||||
* @return
|
||||
* @throws java.io.IOException
|
||||
*/
|
||||
public Command add(Command command) throws IOException {
|
||||
if (close)
|
||||
throw new IOException("Unable to add commands to a closed shell");
|
||||
synchronized (commands) {
|
||||
commands.add(command);
|
||||
// set shell on the command object, to know where the command is running on
|
||||
command.addedToShell(this, (commands.size() - 1));
|
||||
commands.notifyAll();
|
||||
}
|
||||
|
||||
return command;
|
||||
}
|
||||
|
||||
/**
|
||||
* Close shell
|
||||
*
|
||||
* @throws java.io.IOException
|
||||
*/
|
||||
public void close() throws IOException {
|
||||
synchronized (commands) {
|
||||
this.close = true;
|
||||
commands.notifyAll();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns number of queued commands
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public int getCommandsSize() {
|
||||
return commands.size();
|
||||
}
|
||||
|
||||
|
||||
|
||||
package org.sufficientlysecure.rootcommands;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.Closeable;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.sufficientlysecure.rootcommands.command.Command;
|
||||
import org.sufficientlysecure.rootcommands.util.Log;
|
||||
import org.sufficientlysecure.rootcommands.util.RootAccessDeniedException;
|
||||
import org.sufficientlysecure.rootcommands.util.Utils;
|
||||
|
||||
public class Shell implements Closeable {
|
||||
private final Process shellProcess;
|
||||
private final BufferedReader stdOutErr;
|
||||
private final DataOutputStream outputStream;
|
||||
private final List<Command> commands = new ArrayList<Command>();
|
||||
private boolean close = false;
|
||||
|
||||
private static final String LD_LIBRARY_PATH = System.getenv("LD_LIBRARY_PATH");
|
||||
private static final String token = "F*D^W@#FGF";
|
||||
|
||||
/**
|
||||
* Start root shell
|
||||
*
|
||||
* @param customEnv
|
||||
* @param baseDirectory
|
||||
* @return
|
||||
* @throws java.io.IOException
|
||||
*/
|
||||
public static Shell startRootShell(ArrayList<String> customEnv, String baseDirectory)
|
||||
throws IOException, RootAccessDeniedException {
|
||||
Log.d(RootCommands.TAG, "Starting Root Shell!");
|
||||
|
||||
// On some versions of Android (ICS) LD_LIBRARY_PATH is unset when using su
|
||||
// We need to pass LD_LIBRARY_PATH over su for some commands to work correctly.
|
||||
if (customEnv == null) {
|
||||
customEnv = new ArrayList<String>();
|
||||
}
|
||||
customEnv.add("LD_LIBRARY_PATH=" + LD_LIBRARY_PATH);
|
||||
|
||||
Shell shell = new Shell(Utils.getSuPath(), customEnv, baseDirectory);
|
||||
|
||||
return shell;
|
||||
}
|
||||
|
||||
/**
|
||||
* Start root shell without custom environment and base directory
|
||||
*
|
||||
* @return
|
||||
* @throws java.io.IOException
|
||||
*/
|
||||
public static Shell startRootShell() throws IOException, RootAccessDeniedException {
|
||||
return startRootShell(null, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Start default sh shell
|
||||
*
|
||||
* @param customEnv
|
||||
* @param baseDirectory
|
||||
* @return
|
||||
* @throws java.io.IOException
|
||||
*/
|
||||
public static Shell startShell(ArrayList<String> customEnv, String baseDirectory)
|
||||
throws IOException {
|
||||
Log.d(RootCommands.TAG, "Starting Shell!");
|
||||
Shell shell = new Shell("sh", customEnv, baseDirectory);
|
||||
return shell;
|
||||
}
|
||||
|
||||
/**
|
||||
* Start default sh shell without custom environment and base directory
|
||||
*
|
||||
* @return
|
||||
* @throws java.io.IOException
|
||||
*/
|
||||
public static Shell startShell() throws IOException {
|
||||
return startShell(null, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Start custom shell defined by shellPath
|
||||
*
|
||||
* @param shellPath
|
||||
* @param customEnv
|
||||
* @param baseDirectory
|
||||
* @return
|
||||
* @throws java.io.IOException
|
||||
*/
|
||||
public static Shell startCustomShell(String shellPath, ArrayList<String> customEnv,
|
||||
String baseDirectory) throws IOException {
|
||||
Log.d(RootCommands.TAG, "Starting Custom Shell!");
|
||||
Shell shell = new Shell(shellPath, customEnv, baseDirectory);
|
||||
|
||||
return shell;
|
||||
}
|
||||
|
||||
/**
|
||||
* Start custom shell without custom environment and base directory
|
||||
*
|
||||
* @param shellPath
|
||||
* @return
|
||||
* @throws java.io.IOException
|
||||
*/
|
||||
public static Shell startCustomShell(String shellPath) throws IOException {
|
||||
return startCustomShell(shellPath, null, null);
|
||||
}
|
||||
|
||||
private Shell(String shell, ArrayList<String> customEnv, String baseDirectory)
|
||||
throws IOException, RootAccessDeniedException {
|
||||
Log.d(RootCommands.TAG, "Starting shell: " + shell);
|
||||
|
||||
// start shell process!
|
||||
shellProcess = Utils.runWithEnv(shell, customEnv, baseDirectory);
|
||||
|
||||
// StdErr is redirected to StdOut, defined in Command.getCommand()
|
||||
stdOutErr = new BufferedReader(new InputStreamReader(shellProcess.getInputStream()));
|
||||
outputStream = new DataOutputStream(shellProcess.getOutputStream());
|
||||
|
||||
outputStream.write("echo Started\n".getBytes());
|
||||
outputStream.flush();
|
||||
|
||||
while (true) {
|
||||
String line = stdOutErr.readLine();
|
||||
if (line == null)
|
||||
throw new RootAccessDeniedException(
|
||||
"stdout line is null! Access was denied or this executeable is not a shell!");
|
||||
if ("".equals(line))
|
||||
continue;
|
||||
if ("Started".equals(line))
|
||||
break;
|
||||
|
||||
destroyShellProcess();
|
||||
throw new IOException("Unable to start shell, unexpected output \"" + line + "\"");
|
||||
}
|
||||
|
||||
new Thread(inputRunnable, "Shell Input").start();
|
||||
new Thread(outputRunnable, "Shell Output").start();
|
||||
}
|
||||
|
||||
private Runnable inputRunnable = new Runnable() {
|
||||
public void run() {
|
||||
try {
|
||||
writeCommands();
|
||||
} catch (IOException e) {
|
||||
Log.e(RootCommands.TAG, "IO Exception", e);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
private Runnable outputRunnable = new Runnable() {
|
||||
public void run() {
|
||||
try {
|
||||
readOutput();
|
||||
} catch (IOException e) {
|
||||
Log.e(RootCommands.TAG, "IOException", e);
|
||||
} catch (InterruptedException e) {
|
||||
Log.e(RootCommands.TAG, "InterruptedException", e);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Destroy shell process considering that the process could already be terminated
|
||||
*/
|
||||
private void destroyShellProcess() {
|
||||
try {
|
||||
// Yes, this really is the way to check if the process is
|
||||
// still running.
|
||||
shellProcess.exitValue();
|
||||
} catch (IllegalThreadStateException e) {
|
||||
// Only call destroy() if the process is still running;
|
||||
// Calling it for a terminated process will not crash, but
|
||||
// (starting with at least ICS/4.0) spam the log with INFO
|
||||
// messages ala "Failed to destroy process" and "kill
|
||||
// failed: ESRCH (No such process)".
|
||||
shellProcess.destroy();
|
||||
}
|
||||
|
||||
Log.d(RootCommands.TAG, "Shell destroyed");
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes queued commands one after another into the opened shell. After an execution a token is
|
||||
* written to seperate command output on read
|
||||
*
|
||||
* @throws java.io.IOException
|
||||
*/
|
||||
private void writeCommands() throws IOException {
|
||||
try {
|
||||
int commandIndex = 0;
|
||||
while (true) {
|
||||
DataOutputStream out;
|
||||
synchronized (commands) {
|
||||
while (!close && commandIndex >= commands.size()) {
|
||||
commands.wait();
|
||||
}
|
||||
out = this.outputStream;
|
||||
}
|
||||
if (commandIndex < commands.size()) {
|
||||
Command next = commands.get(commandIndex);
|
||||
next.writeCommand(out);
|
||||
String line = "\necho " + token + " " + commandIndex + " $?\n";
|
||||
out.write(line.getBytes());
|
||||
out.flush();
|
||||
commandIndex++;
|
||||
} else if (close) {
|
||||
out.write("\nexit 0\n".getBytes());
|
||||
out.flush();
|
||||
out.close();
|
||||
Log.d(RootCommands.TAG, "Closing shell");
|
||||
return;
|
||||
}
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
Log.e(RootCommands.TAG, "interrupted while writing command", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads output line by line, seperated by token written after every command
|
||||
*
|
||||
* @throws java.io.IOException
|
||||
* @throws InterruptedException
|
||||
*/
|
||||
private void readOutput() throws IOException, InterruptedException {
|
||||
Command command = null;
|
||||
|
||||
// index of current command
|
||||
int commandIndex = 0;
|
||||
while (true) {
|
||||
String lineStdOut = stdOutErr.readLine();
|
||||
|
||||
// terminate on EOF
|
||||
if (lineStdOut == null)
|
||||
break;
|
||||
|
||||
if (command == null) {
|
||||
|
||||
// break on close after last command
|
||||
if (commandIndex >= commands.size()) {
|
||||
if (close)
|
||||
break;
|
||||
continue;
|
||||
}
|
||||
|
||||
// get current command
|
||||
command = commands.get(commandIndex);
|
||||
}
|
||||
|
||||
int pos = lineStdOut.indexOf(token);
|
||||
if (pos > 0) {
|
||||
command.processOutput(lineStdOut.substring(0, pos));
|
||||
}
|
||||
if (pos >= 0) {
|
||||
lineStdOut = lineStdOut.substring(pos);
|
||||
String fields[] = lineStdOut.split(" ");
|
||||
int id = Integer.parseInt(fields[1]);
|
||||
if (id == commandIndex) {
|
||||
command.setExitCode(Integer.parseInt(fields[2]));
|
||||
|
||||
// go to next command
|
||||
commandIndex++;
|
||||
command = null;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
command.processOutput(lineStdOut);
|
||||
}
|
||||
Log.d(RootCommands.TAG, "Read all output");
|
||||
shellProcess.waitFor();
|
||||
destroyShellProcess();
|
||||
|
||||
while (commandIndex < commands.size()) {
|
||||
if (command == null) {
|
||||
command = commands.get(commandIndex);
|
||||
}
|
||||
command.terminated("Unexpected Termination!");
|
||||
commandIndex++;
|
||||
command = null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add command to shell queue
|
||||
*
|
||||
* @param command
|
||||
* @return
|
||||
* @throws java.io.IOException
|
||||
*/
|
||||
public Command add(Command command) throws IOException {
|
||||
if (close)
|
||||
throw new IOException("Unable to add commands to a closed shell");
|
||||
synchronized (commands) {
|
||||
commands.add(command);
|
||||
// set shell on the command object, to know where the command is running on
|
||||
command.addedToShell(this, (commands.size() - 1));
|
||||
commands.notifyAll();
|
||||
}
|
||||
|
||||
return command;
|
||||
}
|
||||
|
||||
/**
|
||||
* Close shell
|
||||
*
|
||||
* @throws java.io.IOException
|
||||
*/
|
||||
public void close() throws IOException {
|
||||
synchronized (commands) {
|
||||
this.close = true;
|
||||
commands.notifyAll();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns number of queued commands
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public int getCommandsSize() {
|
||||
return commands.size();
|
||||
}
|
||||
|
||||
}
|
@@ -1,108 +1,108 @@
|
||||
|
||||
|
||||
package org.sufficientlysecure.rootcommands;
|
||||
|
||||
import android.annotation.TargetApi;
|
||||
import android.content.ContentResolver;
|
||||
import android.content.Context;
|
||||
import android.location.LocationManager;
|
||||
import android.os.PowerManager;
|
||||
import android.provider.Settings;
|
||||
|
||||
/**
|
||||
* This methods work when the apk is installed as a system app (under /system/app)
|
||||
*/
|
||||
public class SystemCommands {
|
||||
Context context;
|
||||
|
||||
public SystemCommands(Context context) {
|
||||
super();
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get GPS status
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public boolean getGPS() {
|
||||
return ((LocationManager) context.getSystemService(Context.LOCATION_SERVICE))
|
||||
.isProviderEnabled(LocationManager.GPS_PROVIDER);
|
||||
}
|
||||
|
||||
/**
|
||||
* Enable/Disable GPS
|
||||
*
|
||||
* @param value
|
||||
*/
|
||||
@TargetApi(8)
|
||||
public void setGPS(boolean value) {
|
||||
ContentResolver localContentResolver = context.getContentResolver();
|
||||
Settings.Secure.setLocationProviderEnabled(localContentResolver,
|
||||
LocationManager.GPS_PROVIDER, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO: Not ready yet
|
||||
*/
|
||||
@TargetApi(8)
|
||||
public void reboot() {
|
||||
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
|
||||
pm.reboot("recovery");
|
||||
pm.reboot(null);
|
||||
|
||||
// not working:
|
||||
// reboot(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reboot the device immediately, passing 'reason' (may be null) to the underlying __reboot
|
||||
* system call. Should not return.
|
||||
*
|
||||
* Taken from com.android.server.PowerManagerService.reboot
|
||||
*/
|
||||
// public void reboot(String reason) {
|
||||
//
|
||||
// // final String finalReason = reason;
|
||||
// Runnable runnable = new Runnable() {
|
||||
// public void run() {
|
||||
// synchronized (this) {
|
||||
// // ShutdownThread.reboot(mContext, finalReason, false);
|
||||
// try {
|
||||
// Class<?> clazz = Class.forName("com.android.internal.app.ShutdownThread");
|
||||
//
|
||||
// // if (mReboot) {
|
||||
// Method method = clazz.getMethod("reboot", Context.class, String.class,
|
||||
// Boolean.TYPE);
|
||||
// method.invoke(null, context, null, false);
|
||||
//
|
||||
// // if (mReboot) {
|
||||
// // Method method = clazz.getMethod("reboot", Context.class, String.class,
|
||||
// // Boolean.TYPE);
|
||||
// // method.invoke(null, mContext, mReason, mConfirm);
|
||||
// // } else {
|
||||
// // Method method = clazz.getMethod("shutdown", Context.class, Boolean.TYPE);
|
||||
// // method.invoke(null, mContext, mConfirm);
|
||||
// // }
|
||||
// } catch (Exception e) {
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// }
|
||||
// };
|
||||
// // ShutdownThread must run on a looper capable of displaying the UI.
|
||||
// mHandler.post(runnable);
|
||||
//
|
||||
// // PowerManager.reboot() is documented not to return so just wait for the inevitable.
|
||||
// // synchronized (runnable) {
|
||||
// // while (true) {
|
||||
// // try {
|
||||
// // runnable.wait();
|
||||
// // } catch (InterruptedException e) {
|
||||
// // }
|
||||
// // }
|
||||
// // }
|
||||
// }
|
||||
|
||||
}
|
||||
|
||||
|
||||
package org.sufficientlysecure.rootcommands;
|
||||
|
||||
import android.annotation.TargetApi;
|
||||
import android.content.ContentResolver;
|
||||
import android.content.Context;
|
||||
import android.location.LocationManager;
|
||||
import android.os.PowerManager;
|
||||
import android.provider.Settings;
|
||||
|
||||
/**
|
||||
* This methods work when the apk is installed as a system app (under /system/app)
|
||||
*/
|
||||
public class SystemCommands {
|
||||
Context context;
|
||||
|
||||
public SystemCommands(Context context) {
|
||||
super();
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get GPS status
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public boolean getGPS() {
|
||||
return ((LocationManager) context.getSystemService(Context.LOCATION_SERVICE))
|
||||
.isProviderEnabled(LocationManager.GPS_PROVIDER);
|
||||
}
|
||||
|
||||
/**
|
||||
* Enable/Disable GPS
|
||||
*
|
||||
* @param value
|
||||
*/
|
||||
@TargetApi(8)
|
||||
public void setGPS(boolean value) {
|
||||
ContentResolver localContentResolver = context.getContentResolver();
|
||||
Settings.Secure.setLocationProviderEnabled(localContentResolver,
|
||||
LocationManager.GPS_PROVIDER, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO: Not ready yet
|
||||
*/
|
||||
@TargetApi(8)
|
||||
public void reboot() {
|
||||
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
|
||||
pm.reboot("recovery");
|
||||
pm.reboot(null);
|
||||
|
||||
// not working:
|
||||
// reboot(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reboot the device immediately, passing 'reason' (may be null) to the underlying __reboot
|
||||
* system call. Should not return.
|
||||
*
|
||||
* Taken from com.android.server.PowerManagerService.reboot
|
||||
*/
|
||||
// public void reboot(String reason) {
|
||||
//
|
||||
// // final String finalReason = reason;
|
||||
// Runnable runnable = new Runnable() {
|
||||
// public void run() {
|
||||
// synchronized (this) {
|
||||
// // ShutdownThread.reboot(mContext, finalReason, false);
|
||||
// try {
|
||||
// Class<?> clazz = Class.forName("com.android.internal.app.ShutdownThread");
|
||||
//
|
||||
// // if (mReboot) {
|
||||
// Method method = clazz.getMethod("reboot", Context.class, String.class,
|
||||
// Boolean.TYPE);
|
||||
// method.invoke(null, context, null, false);
|
||||
//
|
||||
// // if (mReboot) {
|
||||
// // Method method = clazz.getMethod("reboot", Context.class, String.class,
|
||||
// // Boolean.TYPE);
|
||||
// // method.invoke(null, mContext, mReason, mConfirm);
|
||||
// // } else {
|
||||
// // Method method = clazz.getMethod("shutdown", Context.class, Boolean.TYPE);
|
||||
// // method.invoke(null, mContext, mConfirm);
|
||||
// // }
|
||||
// } catch (Exception e) {
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// }
|
||||
// };
|
||||
// // ShutdownThread must run on a looper capable of displaying the UI.
|
||||
// mHandler.post(runnable);
|
||||
//
|
||||
// // PowerManager.reboot() is documented not to return so just wait for the inevitable.
|
||||
// // synchronized (runnable) {
|
||||
// // while (true) {
|
||||
// // try {
|
||||
// // runnable.wait();
|
||||
// // } catch (InterruptedException e) {
|
||||
// // }
|
||||
// // }
|
||||
// // }
|
||||
// }
|
||||
|
||||
}
|
||||
|
File diff suppressed because it is too large
Load Diff
@@ -1,155 +1,155 @@
|
||||
|
||||
|
||||
package org.sufficientlysecure.rootcommands.command;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
|
||||
import org.sufficientlysecure.rootcommands.RootCommands;
|
||||
import org.sufficientlysecure.rootcommands.Shell;
|
||||
import org.sufficientlysecure.rootcommands.util.BrokenBusyboxException;
|
||||
import org.sufficientlysecure.rootcommands.util.Log;
|
||||
|
||||
public abstract class Command {
|
||||
final String command[];
|
||||
boolean finished = false;
|
||||
boolean brokenBusyboxDetected = false;
|
||||
int exitCode;
|
||||
int id;
|
||||
int timeout = RootCommands.DEFAULT_TIMEOUT;
|
||||
Shell shell = null;
|
||||
|
||||
public Command(String... command) {
|
||||
this.command = command;
|
||||
}
|
||||
|
||||
public Command(int timeout, String... command) {
|
||||
this.command = command;
|
||||
this.timeout = timeout;
|
||||
}
|
||||
|
||||
/**
|
||||
* This is called from Shell after adding it
|
||||
*
|
||||
* @param shell
|
||||
* @param id
|
||||
*/
|
||||
public void addedToShell(Shell shell, int id) {
|
||||
this.shell = shell;
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets command string executed on the shell
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String getCommand() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (int i = 0; i < command.length; i++) {
|
||||
// redirect stderr to stdout
|
||||
sb.append(command[i] + " 2>&1");
|
||||
sb.append('\n');
|
||||
}
|
||||
Log.d(RootCommands.TAG, "Sending command(s): " + sb.toString());
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public void writeCommand(OutputStream out) throws IOException {
|
||||
out.write(getCommand().getBytes());
|
||||
}
|
||||
|
||||
public void processOutput(String line) {
|
||||
Log.d(RootCommands.TAG, "ID: " + id + ", Output: " + line);
|
||||
|
||||
/*
|
||||
* Try to detect broken toolbox/busybox binaries (see
|
||||
* https://code.google.com/p/busybox-android/issues/detail?id=1)
|
||||
*
|
||||
* It is giving "Value too large for defined data type" on certain file operations (e.g. ls
|
||||
* and chown) in certain directories (e.g. /data/data)
|
||||
*/
|
||||
if (line.contains("Value too large for defined data type")) {
|
||||
Log.e(RootCommands.TAG, "Busybox is broken with high probability due to line: " + line);
|
||||
brokenBusyboxDetected = true;
|
||||
}
|
||||
|
||||
// now execute specific output parsing
|
||||
output(id, line);
|
||||
}
|
||||
|
||||
public abstract void output(int id, String line);
|
||||
|
||||
public void processAfterExecution(int exitCode) {
|
||||
Log.d(RootCommands.TAG, "ID: " + id + ", ExitCode: " + exitCode);
|
||||
|
||||
afterExecution(id, exitCode);
|
||||
}
|
||||
|
||||
public abstract void afterExecution(int id, int exitCode);
|
||||
|
||||
public void commandFinished(int id) {
|
||||
Log.d(RootCommands.TAG, "Command " + id + " finished.");
|
||||
}
|
||||
|
||||
public void setExitCode(int code) {
|
||||
synchronized (this) {
|
||||
exitCode = code;
|
||||
finished = true;
|
||||
commandFinished(id);
|
||||
this.notifyAll();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Close the shell
|
||||
*
|
||||
* @param reason
|
||||
*/
|
||||
public void terminate(String reason) {
|
||||
try {
|
||||
shell.close();
|
||||
Log.d(RootCommands.TAG, "Terminating the shell.");
|
||||
terminated(reason);
|
||||
} catch (IOException e) {
|
||||
}
|
||||
}
|
||||
|
||||
public void terminated(String reason) {
|
||||
setExitCode(-1);
|
||||
Log.d(RootCommands.TAG, "Command " + id + " did not finish, because of " + reason);
|
||||
}
|
||||
|
||||
/**
|
||||
* Waits for this command to finish and forwards exitCode into afterExecution method
|
||||
*
|
||||
* @param timeout
|
||||
* @throws java.util.concurrent.TimeoutException
|
||||
* @throws BrokenBusyboxException
|
||||
*/
|
||||
public void waitForFinish() throws TimeoutException, BrokenBusyboxException {
|
||||
synchronized (this) {
|
||||
while (!finished) {
|
||||
try {
|
||||
this.wait(timeout);
|
||||
} catch (InterruptedException e) {
|
||||
Log.e(RootCommands.TAG, "InterruptedException in waitForFinish()", e);
|
||||
}
|
||||
|
||||
if (!finished) {
|
||||
finished = true;
|
||||
terminate("Timeout");
|
||||
throw new TimeoutException("Timeout has occurred.");
|
||||
}
|
||||
}
|
||||
|
||||
if (brokenBusyboxDetected) {
|
||||
throw new BrokenBusyboxException();
|
||||
}
|
||||
|
||||
processAfterExecution(exitCode);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
package org.sufficientlysecure.rootcommands.command;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
|
||||
import org.sufficientlysecure.rootcommands.RootCommands;
|
||||
import org.sufficientlysecure.rootcommands.Shell;
|
||||
import org.sufficientlysecure.rootcommands.util.BrokenBusyboxException;
|
||||
import org.sufficientlysecure.rootcommands.util.Log;
|
||||
|
||||
public abstract class Command {
|
||||
final String command[];
|
||||
boolean finished = false;
|
||||
boolean brokenBusyboxDetected = false;
|
||||
int exitCode;
|
||||
int id;
|
||||
int timeout = RootCommands.DEFAULT_TIMEOUT;
|
||||
Shell shell = null;
|
||||
|
||||
public Command(String... command) {
|
||||
this.command = command;
|
||||
}
|
||||
|
||||
public Command(int timeout, String... command) {
|
||||
this.command = command;
|
||||
this.timeout = timeout;
|
||||
}
|
||||
|
||||
/**
|
||||
* This is called from Shell after adding it
|
||||
*
|
||||
* @param shell
|
||||
* @param id
|
||||
*/
|
||||
public void addedToShell(Shell shell, int id) {
|
||||
this.shell = shell;
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets command string executed on the shell
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String getCommand() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (int i = 0; i < command.length; i++) {
|
||||
// redirect stderr to stdout
|
||||
sb.append(command[i] + " 2>&1");
|
||||
sb.append('\n');
|
||||
}
|
||||
Log.d(RootCommands.TAG, "Sending command(s): " + sb.toString());
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public void writeCommand(OutputStream out) throws IOException {
|
||||
out.write(getCommand().getBytes());
|
||||
}
|
||||
|
||||
public void processOutput(String line) {
|
||||
Log.d(RootCommands.TAG, "ID: " + id + ", Output: " + line);
|
||||
|
||||
/*
|
||||
* Try to detect broken toolbox/busybox binaries (see
|
||||
* https://code.google.com/p/busybox-android/issues/detail?id=1)
|
||||
*
|
||||
* It is giving "Value too large for defined data type" on certain file operations (e.g. ls
|
||||
* and chown) in certain directories (e.g. /data/data)
|
||||
*/
|
||||
if (line.contains("Value too large for defined data type")) {
|
||||
Log.e(RootCommands.TAG, "Busybox is broken with high probability due to line: " + line);
|
||||
brokenBusyboxDetected = true;
|
||||
}
|
||||
|
||||
// now execute specific output parsing
|
||||
output(id, line);
|
||||
}
|
||||
|
||||
public abstract void output(int id, String line);
|
||||
|
||||
public void processAfterExecution(int exitCode) {
|
||||
Log.d(RootCommands.TAG, "ID: " + id + ", ExitCode: " + exitCode);
|
||||
|
||||
afterExecution(id, exitCode);
|
||||
}
|
||||
|
||||
public abstract void afterExecution(int id, int exitCode);
|
||||
|
||||
public void commandFinished(int id) {
|
||||
Log.d(RootCommands.TAG, "Command " + id + " finished.");
|
||||
}
|
||||
|
||||
public void setExitCode(int code) {
|
||||
synchronized (this) {
|
||||
exitCode = code;
|
||||
finished = true;
|
||||
commandFinished(id);
|
||||
this.notifyAll();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Close the shell
|
||||
*
|
||||
* @param reason
|
||||
*/
|
||||
public void terminate(String reason) {
|
||||
try {
|
||||
shell.close();
|
||||
Log.d(RootCommands.TAG, "Terminating the shell.");
|
||||
terminated(reason);
|
||||
} catch (IOException e) {
|
||||
}
|
||||
}
|
||||
|
||||
public void terminated(String reason) {
|
||||
setExitCode(-1);
|
||||
Log.d(RootCommands.TAG, "Command " + id + " did not finish, because of " + reason);
|
||||
}
|
||||
|
||||
/**
|
||||
* Waits for this command to finish and forwards exitCode into afterExecution method
|
||||
*
|
||||
* @param timeout
|
||||
* @throws java.util.concurrent.TimeoutException
|
||||
* @throws BrokenBusyboxException
|
||||
*/
|
||||
public void waitForFinish() throws TimeoutException, BrokenBusyboxException {
|
||||
synchronized (this) {
|
||||
while (!finished) {
|
||||
try {
|
||||
this.wait(timeout);
|
||||
} catch (InterruptedException e) {
|
||||
Log.e(RootCommands.TAG, "InterruptedException in waitForFinish()", e);
|
||||
}
|
||||
|
||||
if (!finished) {
|
||||
finished = true;
|
||||
terminate("Timeout");
|
||||
throw new TimeoutException("Timeout has occurred.");
|
||||
}
|
||||
}
|
||||
|
||||
if (brokenBusyboxDetected) {
|
||||
throw new BrokenBusyboxException();
|
||||
}
|
||||
|
||||
processAfterExecution(exitCode);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@@ -1,51 +1,51 @@
|
||||
|
||||
|
||||
package org.sufficientlysecure.rootcommands.command;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.content.Context;
|
||||
import android.os.Build;
|
||||
|
||||
public abstract class ExecutableCommand extends Command {
|
||||
public static final String EXECUTABLE_PREFIX = "lib";
|
||||
public static final String EXECUTABLE_SUFFIX = "_exec.so";
|
||||
|
||||
/**
|
||||
* This class provides a way to use your own binaries!
|
||||
*
|
||||
* Include your own executables, renamed from * to lib*_exec.so, in your libs folder under the
|
||||
* architecture directories. Now they will be deployed by Android the same way libraries are
|
||||
* deployed!
|
||||
*
|
||||
* See README for more information how to use your own executables!
|
||||
*
|
||||
* @param context
|
||||
* @param executableName
|
||||
* @param parameters
|
||||
*/
|
||||
public ExecutableCommand(Context context, String executableName, String parameters) {
|
||||
super(getLibDirectory(context) + File.separator + EXECUTABLE_PREFIX + executableName
|
||||
+ EXECUTABLE_SUFFIX + " " + parameters);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get full path to lib directory of app
|
||||
*
|
||||
* @return dir as String
|
||||
*/
|
||||
@SuppressLint("NewApi")
|
||||
private static String getLibDirectory(Context context) {
|
||||
if (Build.VERSION.SDK_INT >= 9) {
|
||||
return context.getApplicationInfo().nativeLibraryDir;
|
||||
} else {
|
||||
return context.getApplicationInfo().dataDir + File.separator + "lib";
|
||||
}
|
||||
}
|
||||
|
||||
public abstract void output(int id, String line);
|
||||
|
||||
public abstract void afterExecution(int id, int exitCode);
|
||||
|
||||
|
||||
|
||||
package org.sufficientlysecure.rootcommands.command;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.content.Context;
|
||||
import android.os.Build;
|
||||
|
||||
public abstract class ExecutableCommand extends Command {
|
||||
public static final String EXECUTABLE_PREFIX = "lib";
|
||||
public static final String EXECUTABLE_SUFFIX = "_exec.so";
|
||||
|
||||
/**
|
||||
* This class provides a way to use your own binaries!
|
||||
*
|
||||
* Include your own executables, renamed from * to lib*_exec.so, in your libs folder under the
|
||||
* architecture directories. Now they will be deployed by Android the same way libraries are
|
||||
* deployed!
|
||||
*
|
||||
* See README for more information how to use your own executables!
|
||||
*
|
||||
* @param context
|
||||
* @param executableName
|
||||
* @param parameters
|
||||
*/
|
||||
public ExecutableCommand(Context context, String executableName, String parameters) {
|
||||
super(getLibDirectory(context) + File.separator + EXECUTABLE_PREFIX + executableName
|
||||
+ EXECUTABLE_SUFFIX + " " + parameters);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get full path to lib directory of app
|
||||
*
|
||||
* @return dir as String
|
||||
*/
|
||||
@SuppressLint("NewApi")
|
||||
private static String getLibDirectory(Context context) {
|
||||
if (Build.VERSION.SDK_INT >= 9) {
|
||||
return context.getApplicationInfo().nativeLibraryDir;
|
||||
} else {
|
||||
return context.getApplicationInfo().dataDir + File.separator + "lib";
|
||||
}
|
||||
}
|
||||
|
||||
public abstract void output(int id, String line);
|
||||
|
||||
public abstract void afterExecution(int id, int exitCode);
|
||||
|
||||
}
|
@@ -1,29 +1,29 @@
|
||||
|
||||
|
||||
package org.sufficientlysecure.rootcommands.command;
|
||||
|
||||
public class SimpleCommand extends Command {
|
||||
private StringBuilder sb = new StringBuilder();
|
||||
|
||||
public SimpleCommand(String... command) {
|
||||
super(command);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void output(int id, String line) {
|
||||
sb.append(line).append('\n');
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterExecution(int id, int exitCode) {
|
||||
}
|
||||
|
||||
public String getOutput() {
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public int getExitCode() {
|
||||
return exitCode;
|
||||
}
|
||||
|
||||
|
||||
|
||||
package org.sufficientlysecure.rootcommands.command;
|
||||
|
||||
public class SimpleCommand extends Command {
|
||||
private StringBuilder sb = new StringBuilder();
|
||||
|
||||
public SimpleCommand(String... command) {
|
||||
super(command);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void output(int id, String line) {
|
||||
sb.append(line).append('\n');
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterExecution(int id, int exitCode) {
|
||||
}
|
||||
|
||||
public String getOutput() {
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public int getExitCode() {
|
||||
return exitCode;
|
||||
}
|
||||
|
||||
}
|
@@ -1,31 +1,31 @@
|
||||
|
||||
|
||||
package org.sufficientlysecure.rootcommands.command;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
public class SimpleExecutableCommand extends ExecutableCommand {
|
||||
private StringBuilder sb = new StringBuilder();
|
||||
|
||||
public SimpleExecutableCommand(Context context, String executableName, String parameters) {
|
||||
super(context, executableName, parameters);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void output(int id, String line) {
|
||||
sb.append(line).append('\n');
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterExecution(int id, int exitCode) {
|
||||
}
|
||||
|
||||
public String getOutput() {
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public int getExitCode() {
|
||||
return exitCode;
|
||||
}
|
||||
|
||||
|
||||
|
||||
package org.sufficientlysecure.rootcommands.command;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
public class SimpleExecutableCommand extends ExecutableCommand {
|
||||
private StringBuilder sb = new StringBuilder();
|
||||
|
||||
public SimpleExecutableCommand(Context context, String executableName, String parameters) {
|
||||
super(context, executableName, parameters);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void output(int id, String line) {
|
||||
sb.append(line).append('\n');
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterExecution(int id, int exitCode) {
|
||||
}
|
||||
|
||||
public String getOutput() {
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public int getExitCode() {
|
||||
return exitCode;
|
||||
}
|
||||
|
||||
}
|
@@ -1,18 +1,18 @@
|
||||
|
||||
|
||||
package org.sufficientlysecure.rootcommands.util;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class BrokenBusyboxException extends IOException {
|
||||
private static final long serialVersionUID = 8337358201589488409L;
|
||||
|
||||
public BrokenBusyboxException() {
|
||||
super();
|
||||
}
|
||||
|
||||
public BrokenBusyboxException(String detailMessage) {
|
||||
super(detailMessage);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
package org.sufficientlysecure.rootcommands.util;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class BrokenBusyboxException extends IOException {
|
||||
private static final long serialVersionUID = 8337358201589488409L;
|
||||
|
||||
public BrokenBusyboxException() {
|
||||
super();
|
||||
}
|
||||
|
||||
public BrokenBusyboxException(String detailMessage) {
|
||||
super(detailMessage);
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -1,69 +1,69 @@
|
||||
|
||||
|
||||
package org.sufficientlysecure.rootcommands.util;
|
||||
|
||||
import org.sufficientlysecure.rootcommands.RootCommands;
|
||||
|
||||
/**
|
||||
* Wraps Android Logging to enable or disable debug output using Constants
|
||||
*
|
||||
*/
|
||||
public final class Log {
|
||||
|
||||
public static void v(String tag, String msg) {
|
||||
if (RootCommands.DEBUG) {
|
||||
android.util.Log.v(tag, msg);
|
||||
}
|
||||
}
|
||||
|
||||
public static void v(String tag, String msg, Throwable tr) {
|
||||
if (RootCommands.DEBUG) {
|
||||
android.util.Log.v(tag, msg, tr);
|
||||
}
|
||||
}
|
||||
|
||||
public static void d(String tag, String msg) {
|
||||
if (RootCommands.DEBUG) {
|
||||
android.util.Log.d(tag, msg);
|
||||
}
|
||||
}
|
||||
|
||||
public static void d(String tag, String msg, Throwable tr) {
|
||||
if (RootCommands.DEBUG) {
|
||||
android.util.Log.d(tag, msg, tr);
|
||||
}
|
||||
}
|
||||
|
||||
public static void i(String tag, String msg) {
|
||||
if (RootCommands.DEBUG) {
|
||||
android.util.Log.i(tag, msg);
|
||||
}
|
||||
}
|
||||
|
||||
public static void i(String tag, String msg, Throwable tr) {
|
||||
if (RootCommands.DEBUG) {
|
||||
android.util.Log.i(tag, msg, tr);
|
||||
}
|
||||
}
|
||||
|
||||
public static void w(String tag, String msg) {
|
||||
android.util.Log.w(tag, msg);
|
||||
}
|
||||
|
||||
public static void w(String tag, String msg, Throwable tr) {
|
||||
android.util.Log.w(tag, msg, tr);
|
||||
}
|
||||
|
||||
public static void w(String tag, Throwable tr) {
|
||||
android.util.Log.w(tag, tr);
|
||||
}
|
||||
|
||||
public static void e(String tag, String msg) {
|
||||
android.util.Log.e(tag, msg);
|
||||
}
|
||||
|
||||
public static void e(String tag, String msg, Throwable tr) {
|
||||
android.util.Log.e(tag, msg, tr);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
package org.sufficientlysecure.rootcommands.util;
|
||||
|
||||
import org.sufficientlysecure.rootcommands.RootCommands;
|
||||
|
||||
/**
|
||||
* Wraps Android Logging to enable or disable debug output using Constants
|
||||
*
|
||||
*/
|
||||
public final class Log {
|
||||
|
||||
public static void v(String tag, String msg) {
|
||||
if (RootCommands.DEBUG) {
|
||||
android.util.Log.v(tag, msg);
|
||||
}
|
||||
}
|
||||
|
||||
public static void v(String tag, String msg, Throwable tr) {
|
||||
if (RootCommands.DEBUG) {
|
||||
android.util.Log.v(tag, msg, tr);
|
||||
}
|
||||
}
|
||||
|
||||
public static void d(String tag, String msg) {
|
||||
if (RootCommands.DEBUG) {
|
||||
android.util.Log.d(tag, msg);
|
||||
}
|
||||
}
|
||||
|
||||
public static void d(String tag, String msg, Throwable tr) {
|
||||
if (RootCommands.DEBUG) {
|
||||
android.util.Log.d(tag, msg, tr);
|
||||
}
|
||||
}
|
||||
|
||||
public static void i(String tag, String msg) {
|
||||
if (RootCommands.DEBUG) {
|
||||
android.util.Log.i(tag, msg);
|
||||
}
|
||||
}
|
||||
|
||||
public static void i(String tag, String msg, Throwable tr) {
|
||||
if (RootCommands.DEBUG) {
|
||||
android.util.Log.i(tag, msg, tr);
|
||||
}
|
||||
}
|
||||
|
||||
public static void w(String tag, String msg) {
|
||||
android.util.Log.w(tag, msg);
|
||||
}
|
||||
|
||||
public static void w(String tag, String msg, Throwable tr) {
|
||||
android.util.Log.w(tag, msg, tr);
|
||||
}
|
||||
|
||||
public static void w(String tag, Throwable tr) {
|
||||
android.util.Log.w(tag, tr);
|
||||
}
|
||||
|
||||
public static void e(String tag, String msg) {
|
||||
android.util.Log.e(tag, msg);
|
||||
}
|
||||
|
||||
public static void e(String tag, String msg, Throwable tr) {
|
||||
android.util.Log.e(tag, msg, tr);
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -1,18 +1,18 @@
|
||||
|
||||
|
||||
package org.sufficientlysecure.rootcommands.util;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class RootAccessDeniedException extends IOException {
|
||||
private static final long serialVersionUID = 9088998884166225540L;
|
||||
|
||||
public RootAccessDeniedException() {
|
||||
super();
|
||||
}
|
||||
|
||||
public RootAccessDeniedException(String detailMessage) {
|
||||
super(detailMessage);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
package org.sufficientlysecure.rootcommands.util;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class RootAccessDeniedException extends IOException {
|
||||
private static final long serialVersionUID = 9088998884166225540L;
|
||||
|
||||
public RootAccessDeniedException() {
|
||||
super();
|
||||
}
|
||||
|
||||
public RootAccessDeniedException(String detailMessage) {
|
||||
super(detailMessage);
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -1,16 +1,16 @@
|
||||
|
||||
|
||||
package org.sufficientlysecure.rootcommands.util;
|
||||
|
||||
public class UnsupportedArchitectureException extends Exception {
|
||||
private static final long serialVersionUID = 7826528799780001655L;
|
||||
|
||||
public UnsupportedArchitectureException() {
|
||||
super();
|
||||
}
|
||||
|
||||
public UnsupportedArchitectureException(String detailMessage) {
|
||||
super(detailMessage);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
package org.sufficientlysecure.rootcommands.util;
|
||||
|
||||
public class UnsupportedArchitectureException extends Exception {
|
||||
private static final long serialVersionUID = 7826528799780001655L;
|
||||
|
||||
public UnsupportedArchitectureException() {
|
||||
super();
|
||||
}
|
||||
|
||||
public UnsupportedArchitectureException(String detailMessage) {
|
||||
super(detailMessage);
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -1,89 +1,89 @@
|
||||
|
||||
|
||||
package org.sufficientlysecure.rootcommands.util;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Map;
|
||||
|
||||
import org.sufficientlysecure.rootcommands.RootCommands;
|
||||
|
||||
public class Utils {
|
||||
/*
|
||||
* The emulator and ADP1 device both have a su binary in /system/xbin/su, but it doesn't allow
|
||||
* apps to use it (su app_29 $ su su: uid 10029 not allowed to su).
|
||||
*
|
||||
* Cyanogen used to have su in /system/bin/su, in newer versions it's a symlink to
|
||||
* /system/xbin/su.
|
||||
*
|
||||
* The Archos tablet has it in /data/bin/su, since they don't have write access to /system yet.
|
||||
*/
|
||||
static final String[] BinaryPlaces = { "/data/bin/", "/system/bin/", "/system/xbin/", "/sbin/",
|
||||
"/data/local/xbin/", "/data/local/bin/", "/system/sd/xbin/", "/system/bin/failsafe/",
|
||||
"/data/local/" };
|
||||
|
||||
/**
|
||||
* Determine the path of the su executable.
|
||||
*
|
||||
* Code from https://github.com/miracle2k/android-autostarts, use under Apache License was
|
||||
* agreed by Michael Elsdörfer
|
||||
*/
|
||||
public static String getSuPath() {
|
||||
for (String p : BinaryPlaces) {
|
||||
File su = new File(p + "su");
|
||||
if (su.exists()) {
|
||||
Log.d(RootCommands.TAG, "su found at: " + p);
|
||||
return su.getAbsolutePath();
|
||||
} else {
|
||||
Log.v(RootCommands.TAG, "No su in: " + p);
|
||||
}
|
||||
}
|
||||
Log.d(RootCommands.TAG, "No su found in a well-known location, " + "will just use \"su\".");
|
||||
return "su";
|
||||
}
|
||||
|
||||
/**
|
||||
* This code is adapted from java.lang.ProcessBuilder.start().
|
||||
*
|
||||
* The problem is that Android doesn't allow us to modify the map returned by
|
||||
* ProcessBuilder.environment(), even though the docstring indicates that it should. This is
|
||||
* because it simply returns the SystemEnvironment object that System.getenv() gives us. The
|
||||
* relevant portion in the source code is marked as "// android changed", so presumably it's not
|
||||
* the case in the original version of the Apache Harmony project.
|
||||
*
|
||||
* Note that simply passing the environment variables we want to Process.exec won't be good
|
||||
* enough, since that would override the environment we inherited completely.
|
||||
*
|
||||
* We needed to be able to set a CLASSPATH environment variable for our new process in order to
|
||||
* use the "app_process" command directly. Note: "app_process" takes arguments passed on to the
|
||||
* Dalvik VM as well; this might be an alternative way to set the class path.
|
||||
*
|
||||
* Code from https://github.com/miracle2k/android-autostarts, use under Apache License was
|
||||
* agreed by Michael Elsdörfer
|
||||
*/
|
||||
public static Process runWithEnv(String command, ArrayList<String> customAddedEnv,
|
||||
String baseDirectory) throws IOException {
|
||||
|
||||
Map<String, String> environment = System.getenv();
|
||||
String[] envArray = new String[environment.size()
|
||||
+ (customAddedEnv != null ? customAddedEnv.size() : 0)];
|
||||
int i = 0;
|
||||
for (Map.Entry<String, String> entry : environment.entrySet()) {
|
||||
envArray[i++] = entry.getKey() + "=" + entry.getValue();
|
||||
}
|
||||
if (customAddedEnv != null) {
|
||||
for (String entry : customAddedEnv) {
|
||||
envArray[i++] = entry;
|
||||
}
|
||||
}
|
||||
|
||||
Process process;
|
||||
if (baseDirectory == null) {
|
||||
process = Runtime.getRuntime().exec(command, envArray, null);
|
||||
} else {
|
||||
process = Runtime.getRuntime().exec(command, envArray, new File(baseDirectory));
|
||||
}
|
||||
return process;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
package org.sufficientlysecure.rootcommands.util;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Map;
|
||||
|
||||
import org.sufficientlysecure.rootcommands.RootCommands;
|
||||
|
||||
public class Utils {
|
||||
/*
|
||||
* The emulator and ADP1 device both have a su binary in /system/xbin/su, but it doesn't allow
|
||||
* apps to use it (su app_29 $ su su: uid 10029 not allowed to su).
|
||||
*
|
||||
* Cyanogen used to have su in /system/bin/su, in newer versions it's a symlink to
|
||||
* /system/xbin/su.
|
||||
*
|
||||
* The Archos tablet has it in /data/bin/su, since they don't have write access to /system yet.
|
||||
*/
|
||||
static final String[] BinaryPlaces = { "/data/bin/", "/system/bin/", "/system/xbin/", "/sbin/",
|
||||
"/data/local/xbin/", "/data/local/bin/", "/system/sd/xbin/", "/system/bin/failsafe/",
|
||||
"/data/local/" };
|
||||
|
||||
/**
|
||||
* Determine the path of the su executable.
|
||||
*
|
||||
* Code from https://github.com/miracle2k/android-autostarts, use under Apache License was
|
||||
* agreed by Michael Elsdörfer
|
||||
*/
|
||||
public static String getSuPath() {
|
||||
for (String p : BinaryPlaces) {
|
||||
File su = new File(p + "su");
|
||||
if (su.exists()) {
|
||||
Log.d(RootCommands.TAG, "su found at: " + p);
|
||||
return su.getAbsolutePath();
|
||||
} else {
|
||||
Log.v(RootCommands.TAG, "No su in: " + p);
|
||||
}
|
||||
}
|
||||
Log.d(RootCommands.TAG, "No su found in a well-known location, " + "will just use \"su\".");
|
||||
return "su";
|
||||
}
|
||||
|
||||
/**
|
||||
* This code is adapted from java.lang.ProcessBuilder.start().
|
||||
*
|
||||
* The problem is that Android doesn't allow us to modify the map returned by
|
||||
* ProcessBuilder.environment(), even though the docstring indicates that it should. This is
|
||||
* because it simply returns the SystemEnvironment object that System.getenv() gives us. The
|
||||
* relevant portion in the source code is marked as "// android changed", so presumably it's not
|
||||
* the case in the original version of the Apache Harmony project.
|
||||
*
|
||||
* Note that simply passing the environment variables we want to Process.exec won't be good
|
||||
* enough, since that would override the environment we inherited completely.
|
||||
*
|
||||
* We needed to be able to set a CLASSPATH environment variable for our new process in order to
|
||||
* use the "app_process" command directly. Note: "app_process" takes arguments passed on to the
|
||||
* Dalvik VM as well; this might be an alternative way to set the class path.
|
||||
*
|
||||
* Code from https://github.com/miracle2k/android-autostarts, use under Apache License was
|
||||
* agreed by Michael Elsdörfer
|
||||
*/
|
||||
public static Process runWithEnv(String command, ArrayList<String> customAddedEnv,
|
||||
String baseDirectory) throws IOException {
|
||||
|
||||
Map<String, String> environment = System.getenv();
|
||||
String[] envArray = new String[environment.size()
|
||||
+ (customAddedEnv != null ? customAddedEnv.size() : 0)];
|
||||
int i = 0;
|
||||
for (Map.Entry<String, String> entry : environment.entrySet()) {
|
||||
envArray[i++] = entry.getKey() + "=" + entry.getValue();
|
||||
}
|
||||
if (customAddedEnv != null) {
|
||||
for (String entry : customAddedEnv) {
|
||||
envArray[i++] = entry;
|
||||
}
|
||||
}
|
||||
|
||||
Process process;
|
||||
if (baseDirectory == null) {
|
||||
process = Runtime.getRuntime().exec(command, envArray, null);
|
||||
} else {
|
||||
process = Runtime.getRuntime().exec(command, envArray, new File(baseDirectory));
|
||||
}
|
||||
return process;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user