From 74379150a18eb6faf1a0398cb60b7cdec15d228f Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Sun, 18 Feb 2018 12:41:58 +0800 Subject: [PATCH] Use scripts to setup sudb --- .gitignore | 2 +- .../com/topjohnwu/magisk/MagiskManager.java | 9 +++-- .../topjohnwu/magisk/asyncs/HideManager.java | 1 - .../magisk/database/SuDatabaseHelper.java | 40 +++++-------------- .../com/topjohnwu/magisk/utils/Utils.java | 3 +- src/main/res/raw/sudb.sh | 21 ++++++++++ 6 files changed, 38 insertions(+), 38 deletions(-) create mode 100644 src/main/res/raw/sudb.sh diff --git a/.gitignore b/.gitignore index 13f3c8a7c..3f0eb250d 100644 --- a/.gitignore +++ b/.gitignore @@ -6,7 +6,7 @@ app/release *.hprof .externalNativeBuild/ -*.sh +src/main/assets/*.sh public.certificate.x509.pem private.key.pk8 *.apk diff --git a/src/main/java/com/topjohnwu/magisk/MagiskManager.java b/src/main/java/com/topjohnwu/magisk/MagiskManager.java index e27dafdac..b59818a9e 100644 --- a/src/main/java/com/topjohnwu/magisk/MagiskManager.java +++ b/src/main/java/com/topjohnwu/magisk/MagiskManager.java @@ -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) {} diff --git a/src/main/java/com/topjohnwu/magisk/asyncs/HideManager.java b/src/main/java/com/topjohnwu/magisk/asyncs/HideManager.java index 0eac84702..3de3b5999 100644 --- a/src/main/java/com/topjohnwu/magisk/asyncs/HideManager.java +++ b/src/main/java/com/topjohnwu/magisk/asyncs/HideManager.java @@ -136,7 +136,6 @@ public class HideManager extends ParallelTask { repack.delete(); mm.suDB.setStrings(Const.Key.SU_REQUESTER, pkg); - mm.suDB.flush(); Utils.dumpPrefs(); Utils.uninstallPkg(Const.ORIG_PKG_NAME); diff --git a/src/main/java/com/topjohnwu/magisk/database/SuDatabaseHelper.java b/src/main/java/com/topjohnwu/magisk/database/SuDatabaseHelper.java index e3cccd292..baf314b0e 100644 --- a/src/main/java/com/topjohnwu/magisk/database/SuDatabaseHelper.java +++ b/src/main/java/com/topjohnwu/magisk/database/SuDatabaseHelper.java @@ -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); - } } diff --git a/src/main/java/com/topjohnwu/magisk/utils/Utils.java b/src/main/java/com/topjohnwu/magisk/utils/Utils.java index 939869e81..128322276 100644 --- a/src/main/java/com/topjohnwu/magisk/utils/Utils.java +++ b/src/main/java/com/topjohnwu/magisk/utils/Utils.java @@ -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) { diff --git a/src/main/res/raw/sudb.sh b/src/main/res/raw/sudb.sh new file mode 100644 index 000000000..a2c5e5998 --- /dev/null +++ b/src/main/res/raw/sudb.sh @@ -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/* +}