Use scripts to setup sudb

This commit is contained in:
topjohnwu 2018-02-18 12:41:58 +08:00
parent c840a30c30
commit 74379150a1
6 changed files with 38 additions and 38 deletions

2
.gitignore vendored
View File

@ -6,7 +6,7 @@
app/release
*.hprof
.externalNativeBuild/
*.sh
src/main/assets/*.sh
public.certificate.x509.pem
private.key.pk8
*.apk

View File

@ -101,8 +101,10 @@ public class MagiskManager extends Shell.ContainerApp {
Shell.setInitializer(new Shell.Initializer() {
@Override
public void onRootShellInit(@NonNull Shell shell) {
try (InputStream in = MagiskManager.get().getAssets().open(Const.UTIL_FUNCTIONS)) {
shell.loadInputStream(null, null, in);
try (InputStream utils = MagiskManager.get().getAssets().open(Const.UTIL_FUNCTIONS);
InputStream sudb = getResources().openRawResource(R.raw.sudb)) {
shell.loadInputStream(null, null, utils);
shell.loadInputStream(null, null, sudb);
} catch (IOException e) {
e.printStackTrace();
}
@ -205,7 +207,8 @@ public class MagiskManager extends Shell.ContainerApp {
try {
magiskVersionString = Utils.cmd("magisk -v").split(":")[0];
magiskVersionCode = Integer.parseInt(Utils.cmd("magisk -V"));
String s = Utils.cmd((magiskVersionCode > 1435 ? "resetprop -p " : "getprop ") + Const.MAGISKHIDE_PROP);
String s = Utils.cmd((magiskVersionCode > 1435 ? "resetprop -p " : "getprop ")
+ Const.MAGISKHIDE_PROP);
magiskHide = s == null || Integer.parseInt(s) != 0;
} catch (Exception ignored) {}

View File

@ -136,7 +136,6 @@ public class HideManager extends ParallelTask<Void, Void, Boolean> {
repack.delete();
mm.suDB.setStrings(Const.Key.SU_REQUESTER, pkg);
mm.suDB.flush();
Utils.dumpPrefs();
Utils.uninstallPkg(Const.ORIG_PKG_NAME);

View File

@ -42,22 +42,11 @@ public class SuDatabaseHelper {
return new SuDatabaseHelper(mm);
} catch (Exception e) {
// Let's cleanup everything and try again
cleanup("*");
Shell.Sync.su("sudb_clean '*'");
return new SuDatabaseHelper(mm);
}
}
public static void cleanup() {
cleanup(String.valueOf(Const.USER_ID));
}
public static void cleanup(String s) {
Shell.Sync.su(
"umount -l /data/user*/*/*/databases/su.db",
"umount -l /sbin/.core/db-" + s + "/magisk.db",
"rm -rf /sbin/.core/db-" + s);
}
private SuDatabaseHelper(MagiskManager mm) {
pm = mm.getPackageManager();
mDb = openDatabase(mm);
@ -76,7 +65,7 @@ public class SuDatabaseHelper {
DB_FILE = new File(Utils.fmt("/sbin/.core/db-%d/magisk.db", Const.USER_ID));
Context de = Build.VERSION.SDK_INT >= Build.VERSION_CODES.N
? mm.createDeviceProtectedStorageContext() : mm;
if (!DB_FILE.exists()) {
if (!DB_FILE.canWrite()) {
if (!Shell.rootAccess()) {
// We don't want the app to crash, create a db and return
DB_FILE = mm.getDatabasePath("su.db");
@ -84,7 +73,7 @@ public class SuDatabaseHelper {
}
mm.loadMagiskInfo();
// Cleanup
cleanup();
Shell.Sync.su("sudb_clean " + Const.USER_ID);
if (mm.magiskVersionCode < 1410) {
// Super old legacy mode
DB_FILE = mm.getDatabasePath("su.db");
@ -103,19 +92,13 @@ public class SuDatabaseHelper {
// v14.5 global DB location
GLOBAL_DB = new File(de.getFilesDir().getParentFile().getParentFile(),
"magisk.db").getPath();
// We need some additional policies on old versions
Shell.Sync.su("magiskpolicy --live 'create su_file' 'allow * su_file file *'");
}
// Touch global DB and setup db in tmpfs
Shell.Sync.su(Utils.fmt("touch %s; mkdir -p %s; touch %s; touch %s-journal;" +
"mount -o bind %s %s;" +
"chcon u:object_r:su_file:s0 %s/*; chown %d.%d %s;" +
"chmod 666 %s/*; chmod 700 %s;",
GLOBAL_DB, DB_FILE.getParent(), DB_FILE, DB_FILE,
GLOBAL_DB, DB_FILE,
DB_FILE.getParent(), Process.myUid(), Process.myUid(), DB_FILE.getParent(),
DB_FILE.getParent(), DB_FILE.getParent()
));
if (mm.magiskVersionCode < 1550) {
// We need some additional policies on old versions
Shell.Sync.su("magiskpolicy --live " +
"'create su_file' 'allow * su_file file *' 'allow * su_file dir *'");
}
Shell.Sync.su("sudb_setup " + Process.myUid() + " " + GLOBAL_DB);
}
}
// Not using legacy mode, open the mounted global DB
@ -320,9 +303,4 @@ public class SuDatabaseHelper {
}
return value;
}
public void flush() {
mDb.close();
mDb = SQLiteDatabase.openOrCreateDatabase(DB_FILE, null);
}
}

View File

@ -50,8 +50,7 @@ public class Utils {
}
public static void uninstallPkg(String pkg) {
SuDatabaseHelper.cleanup();
Shell.Sync.su("pm uninstall " + pkg);
Shell.Sync.su("sudb_clean " + Const.USER_ID, "pm uninstall " + pkg);
}
public static void dlAndReceive(Context context, DownloadReceiver receiver, String link, String filename) {

21
src/main/res/raw/sudb.sh Normal file
View File

@ -0,0 +1,21 @@
sudb_clean() {
local USERID=$1
local DIR="/sbin/.core/db-${USERID}"
umount -l /data/user*/*/*/databases/su.db $DIR $DIR/*
rm -rf $DIR
}
sudb_setup() {
local USER=$1
local GLOBAL_DB=$2
local USERID=$(($USER / 100000))
local DIR=/sbin/.core/db-${USERID}
touch $GLOBAL_DB
mkdir -p $DIR
touch $DIR/magisk.db
mount -o bind $GLOBAL_DB $DIR/magisk.db
chcon u:object_r:su_file:s0 $DIR $DIR/*
chmod 700 $DIR
chown $USER.$USER $DIR
chmod 666 $DIR/*
}