Proper module management

This commit is contained in:
topjohnwu 2016-08-23 01:44:34 +08:00
parent 3ad06c406c
commit 7eadc74f6c
5 changed files with 60 additions and 18 deletions

View File

@ -46,23 +46,23 @@ public class Module {
}
public void createDisableFile() {
Utils.executeCommand("touch " + mDisableFile.getPath());
Utils.su("touch " + mDisableFile.getPath());
}
public boolean removeDisableFile() {
return mDisableFile.delete();
public void removeDisableFile() {
Utils.su("rm -f " + mDisableFile.getPath());
}
public boolean isEnabled() {
return mDisableFile.exists();
return ! mDisableFile.exists();
}
public void createRemoveFile() {
Utils.executeCommand("touch " + mRemoveFile.getPath());
Utils.su("touch " + mRemoveFile.getPath());
}
public boolean deleteRemoveFile() {
return mRemoveFile.delete();
public void deleteRemoveFile() {
Utils.su("rm -f " + mRemoveFile.getPath());
}
public boolean willBeRemoved() {

View File

@ -226,8 +226,8 @@ public class LogFragment extends Fragment {
@Override
protected void onPreExecute() {
Utils.executeCommand("chmod 777 /cache");
Utils.executeCommand("chmod 777 /cache/magisk.log");
Utils.su("chmod 755 /cache");
Utils.su("chmod 644 /cache/magisk.log");
txtLog.setText("");
}

View File

@ -11,7 +11,7 @@ import com.topjohnwu.magisk.R;
import java.io.File;
import static com.topjohnwu.magisk.ui.utils.Utils.executeCommand;
import static com.topjohnwu.magisk.ui.utils.Utils.su;
public class MainActivity extends Activity {
@ -32,13 +32,13 @@ public class MainActivity extends Activity {
safetyNet = (TextView) findViewById(R.id.safety_net);
permissive = (TextView) findViewById(R.id.permissive);
suPath = executeCommand("getprop magisk.supath");
suPath = su("getprop magisk.supath");
updateStatus();
rootToggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
executeCommand(b ? "setprop magisk.root 1" : "setprop magisk.root 0");
su(b ? "setprop magisk.root 1" : "setprop magisk.root 0");
updateStatus();
}
});
@ -46,7 +46,7 @@ public class MainActivity extends Activity {
selinuxToggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
executeCommand(b ? "setenforce 1" : "setenforce 0");
su(b ? "setenforce 1" : "setenforce 0");
updateStatus();
}
});
@ -55,9 +55,9 @@ public class MainActivity extends Activity {
}
private void updateStatus() {
String selinux = executeCommand("getenforce");
String selinux = su("getenforce");
magiskVersion.setText(getString(R.string.magisk_version, executeCommand("getprop magisk.version")));
magiskVersion.setText(getString(R.string.magisk_version, su("getprop magisk.version")));
selinuxStatus.setText(selinux);
if (selinux.equals("Enforcing")) {

View File

@ -88,7 +88,7 @@ public class ModulesFragment extends Fragment {
}
});
Utils.executeCommand("chmod 777 /cache");
Utils.su("chmod 755 /cache");
File[] magiskCache = new File(MAGISK_CACHE_PATH).listFiles(new FileFilter() {
@Override

View File

@ -6,8 +6,11 @@ import eu.chainfire.libsuperuser.Shell;
public class Utils {
public static String executeCommand(String... commands) {
List<String> result = Shell.SU.run(commands);
public static final String suPath = sh("getprop magisk.supath");
public static final boolean rootAccess = isRoot();
public static String sh(String... commands) {
List<String> result = Shell.SH.run(commands);
StringBuilder builder = new StringBuilder();
for (String s : result) {
@ -17,4 +20,43 @@ public class Utils {
return builder.toString();
}
public static String su(String... commands) {
List<String> result = Shell.run(Utils.suPath + "/su", commands, null, false);
StringBuilder builder = new StringBuilder();
for (String s : result) {
builder.append(s);
}
Shell.SU.available();
return builder.toString();
}
public static boolean isRoot() {
String [] availableTestCommands = new String[] {"echo -BOC-", "id"};
List<String> ret = Shell.run(Utils.suPath + "/su", availableTestCommands, null, false);
if (ret == null)
return false;
// Taken from libsuperuser
// this is only one of many ways this can be done
boolean echo_seen = false;
for (String line : ret) {
if (line.contains("uid=")) {
// id command is working, let's see if we are actually root
return line.contains("uid=0");
} else if (line.contains("-BOC-")) {
// if we end up here, at least the su command starts some kind
// of shell, let's hope it has root privileges - no way to know without
// additional native binaries
echo_seen = true;
}
}
return echo_seen;
}
}