Commit 632824bd authored by n8fr8's avatar n8fr8
Browse files

remove RootCommands library as we don't need it now

parent d190f3a1
#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
apply plugin: 'com.android.library'
android {
compileSdkVersion 25
buildToolsVersion "25"
sourceSets {
main {
jni.srcDirs = []
}
}
defaultConfig {
minSdkVersion 9
targetSdkVersion 25
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
<?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>
\ No newline at end of file
/*
* Copyright (C) 2012 Dominik Schürmann <dominik@dominikschuermann.de>
* Copyright (c) 2012 Stephen Erickson, Chris Ravenscroft, Adam Shanks (RootTools)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
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);
}
}
/*
* Copyright (C) 2012 Dominik Schürmann <dominik@dominikschuermann.de>
* Copyright (c) 2012 Stephen Erickson, Chris Ravenscroft, Adam Shanks (RootTools)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
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;
}
}
/*
* Copyright (C) 2012 Dominik Schürmann <dominik@dominikschuermann.de>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
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;
}
}
/*
* Copyright (C) 2012 Dominik Schürmann <dominik@dominikschuermann.de>
* Copyright (c) 2012 Stephen Erickson, Chris Ravenscroft, Adam Shanks, Jeremy Lakeman (RootTools)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
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 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 IOException
*/
public static Shell startRootShell() throws IOException, RootAccessDeniedException {
return startRootShell(null, null);
}
/**
* Start default sh shell
*
* @param customEnv
* @param baseDirectory
* @return
* @throws 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 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 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 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");
}
/**