From 959ed7f866f7bed4bf5d1b4286217d0ddeacd271 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Fri, 27 Jan 2017 01:02:40 +0800 Subject: [PATCH] Implement logging and bug fixes --- .../java/com/topjohnwu/magisk/Global.java | 4 +- .../topjohnwu/magisk/superuser/Policy.java | 2 +- .../magisk/superuser/SuDatabaseHelper.java | 2 +- .../magisk/superuser/SuLogDatabaseHelper.java | 62 +++++++++++++++++++ .../magisk/superuser/SuLogEntry.java | 42 +++++++++++++ .../magisk/superuser/SuReceiver.java | 11 +++- .../magisk/superuser/SuRequestActivity.java | 5 +- 7 files changed, 120 insertions(+), 8 deletions(-) create mode 100644 app/src/main/java/com/topjohnwu/magisk/superuser/SuLogDatabaseHelper.java create mode 100644 app/src/main/java/com/topjohnwu/magisk/superuser/SuLogEntry.java diff --git a/app/src/main/java/com/topjohnwu/magisk/Global.java b/app/src/main/java/com/topjohnwu/magisk/Global.java index 8bb5488f6..2ee4a46b4 100644 --- a/app/src/main/java/com/topjohnwu/magisk/Global.java +++ b/app/src/main/java/com/topjohnwu/magisk/Global.java @@ -46,6 +46,8 @@ public class Global { public static boolean isDarkTheme; public static boolean shellLogging; public static boolean devLogging; + public static int suRequestTimeout = 10; + public static int suLogTimeout = 14; } @@ -59,7 +61,7 @@ public class Global { static void updateMagiskInfo() { List ret = Shell.sh("getprop magisk.version"); - if (Utils.isValidShellResponse(ret)) { + if (!Utils.isValidShellResponse(ret)) { Info.magiskVersion = -1; } else { try { diff --git a/app/src/main/java/com/topjohnwu/magisk/superuser/Policy.java b/app/src/main/java/com/topjohnwu/magisk/superuser/Policy.java index 3bf39a8d6..a8a6f0766 100644 --- a/app/src/main/java/com/topjohnwu/magisk/superuser/Policy.java +++ b/app/src/main/java/com/topjohnwu/magisk/superuser/Policy.java @@ -45,7 +45,7 @@ public class Policy { values.put("uid", uid); values.put("package_name", packageName); values.put("app_name", appName); - values.put("policy",policy); + values.put("policy", policy); values.put("until", until); values.put("logging", logging ? 1 : 0); values.put("notification", notification ? 1 : 0); diff --git a/app/src/main/java/com/topjohnwu/magisk/superuser/SuDatabaseHelper.java b/app/src/main/java/com/topjohnwu/magisk/superuser/SuDatabaseHelper.java index ab77643af..21cf457c0 100644 --- a/app/src/main/java/com/topjohnwu/magisk/superuser/SuDatabaseHelper.java +++ b/app/src/main/java/com/topjohnwu/magisk/superuser/SuDatabaseHelper.java @@ -62,7 +62,7 @@ public class SuDatabaseHelper extends SQLiteOpenHelper { Policy policy; // Clear outdated policies db.delete(TABLE_NAME, "until > 0 and until < ?", new String[] { String.valueOf(System.currentTimeMillis()) }); - try (Cursor c = db.query(TABLE_NAME, null, null, null, null, null, "app_name")) { + try (Cursor c = db.query(TABLE_NAME, null, null, null, null, null, "app_name ASC")) { while (c.moveToNext()) { policy = new Policy(c); // Package is uninstalled diff --git a/app/src/main/java/com/topjohnwu/magisk/superuser/SuLogDatabaseHelper.java b/app/src/main/java/com/topjohnwu/magisk/superuser/SuLogDatabaseHelper.java new file mode 100644 index 000000000..f01d8ab7e --- /dev/null +++ b/app/src/main/java/com/topjohnwu/magisk/superuser/SuLogDatabaseHelper.java @@ -0,0 +1,62 @@ +package com.topjohnwu.magisk.superuser; + +import android.content.Context; +import android.database.Cursor; +import android.database.sqlite.SQLiteDatabase; +import android.database.sqlite.SQLiteOpenHelper; + +import com.topjohnwu.magisk.Global; + +import java.util.ArrayList; +import java.util.List; + +public class SuLogDatabaseHelper extends SQLiteOpenHelper { + + private static final int DATABASE_VER = 1; + private static final String TABLE_NAME = "logs"; + + public SuLogDatabaseHelper(Context context) { + super(context, "sulog.db", null, DATABASE_VER); + } + + @Override + public void onCreate(SQLiteDatabase db) { + db.execSQL( + "CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " (id INTEGER PRIMARY KEY AUTOINCREMENT, " + + "from_uid INT, package_name TEXT, app_name TEXT, from_pid INT, " + + "to_uid INT, action INT, time INT, command TEXT)"); + } + + @Override + public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { + // Currently new database, no upgrading + } + + public void addLog(SuLogEntry log) { + SQLiteDatabase db = getWritableDatabase(); + db.insert(TABLE_NAME, null, log.getContentValues()); + db.close(); + } + + public List getLogList() { + return getLogList(null); + } + + public List getLogList(int uid) { + return getLogList("uid=" + uid); + } + + public List getLogList(String selection) { + List ret = new ArrayList<>(); + SQLiteDatabase db = getWritableDatabase(); + // Clear outdated logs + db.delete(TABLE_NAME, "time < ?", new String[] { String.valueOf( + System.currentTimeMillis() / 1000 - Global.Configs.suLogTimeout * 86400) }); + try (Cursor c = db.query(TABLE_NAME, null, selection, null, null, null, "time DESC")) { + while (c.moveToNext()) + ret.add(new SuLogEntry(c)); + } + db.close(); + return ret; + } +} diff --git a/app/src/main/java/com/topjohnwu/magisk/superuser/SuLogEntry.java b/app/src/main/java/com/topjohnwu/magisk/superuser/SuLogEntry.java new file mode 100644 index 000000000..fe90fcd51 --- /dev/null +++ b/app/src/main/java/com/topjohnwu/magisk/superuser/SuLogEntry.java @@ -0,0 +1,42 @@ +package com.topjohnwu.magisk.superuser; + +import android.content.ContentValues; +import android.database.Cursor; + +public class SuLogEntry { + + public int fromUid, toUid, fromPid; + public String packageName, appName, command; + public boolean action; + public long time; + + public SuLogEntry(Policy policy) { + fromUid = policy.uid; + packageName = policy.packageName; + appName = policy.appName; + } + + public SuLogEntry(Cursor c) { + fromUid = c.getInt(c.getColumnIndex("from_uid")); + fromPid = c.getInt(c.getColumnIndex("from_pid")); + toUid = c.getInt(c.getColumnIndex("to_uid")); + packageName = c.getString(c.getColumnIndex("package_name")); + appName = c.getString(c.getColumnIndex("app_name")); + command = c.getString(c.getColumnIndex("command")); + action = c.getInt(c.getColumnIndex("action")) != 0; + time = c.getLong(c.getColumnIndex("until")); + } + + public ContentValues getContentValues() { + ContentValues values = new ContentValues(); + values.put("from_uid", fromUid); + values.put("package_name", packageName); + values.put("app_name", appName); + values.put("from_pid", fromPid); + values.put("command", command); + values.put("to_uid", toUid); + values.put("action", action); + values.put("time", time); + return values; + } +} diff --git a/app/src/main/java/com/topjohnwu/magisk/superuser/SuReceiver.java b/app/src/main/java/com/topjohnwu/magisk/superuser/SuReceiver.java index c72de958b..0476599d8 100644 --- a/app/src/main/java/com/topjohnwu/magisk/superuser/SuReceiver.java +++ b/app/src/main/java/com/topjohnwu/magisk/superuser/SuReceiver.java @@ -24,8 +24,8 @@ public class SuReceiver extends BroadcastReceiver { action = intent.getStringExtra("action"); if (action == null) return; - SuDatabaseHelper dbHelper = new SuDatabaseHelper(context); - policy = dbHelper.getPolicy(fromUid); + SuDatabaseHelper suDbHelper = new SuDatabaseHelper(context); + policy = suDbHelper.getPolicy(fromUid); if (policy == null) try { policy = new Policy(fromUid, context.getPackageManager()); } catch (Throwable throwable) { @@ -53,7 +53,12 @@ public class SuReceiver extends BroadcastReceiver { if (pid < 0) return; command = intent.getStringExtra("command"); if (command == null) return; - // TODO: Place info into logs + SuLogEntry log = new SuLogEntry(policy); + log.toUid = toUid; + log.fromPid = pid; + log.command = command; + SuLogDatabaseHelper logDbHelper = new SuLogDatabaseHelper(context); + logDbHelper.addLog(log); } } } diff --git a/app/src/main/java/com/topjohnwu/magisk/superuser/SuRequestActivity.java b/app/src/main/java/com/topjohnwu/magisk/superuser/SuRequestActivity.java index a70f75090..dfd7262bc 100644 --- a/app/src/main/java/com/topjohnwu/magisk/superuser/SuRequestActivity.java +++ b/app/src/main/java/com/topjohnwu/magisk/superuser/SuRequestActivity.java @@ -18,6 +18,7 @@ import android.widget.LinearLayout; import android.widget.Spinner; import android.widget.TextView; +import com.topjohnwu.magisk.Global; import com.topjohnwu.magisk.R; import java.io.DataInputStream; @@ -46,7 +47,7 @@ public class SuRequestActivity extends AppCompatActivity { private PackageManager pm; private PackageInfo info; - private int uid, countdown = 10; + private int uid; private String appName, packageName; private CountDownTimer timer; @@ -73,7 +74,7 @@ public class SuRequestActivity extends AppCompatActivity { appNameView.setText(appName); packageNameView.setText(packageName); - timer = new CountDownTimer(countdown * 1000, 1000) { + timer = new CountDownTimer(Global.Configs.suRequestTimeout * 1000, 1000) { @Override public void onTick(long millisUntilFinished) { deny_btn.setText(getString(R.string.deny, "(" + millisUntilFinished / 1000 + ")"));