Move magiskhide config into database

This commit is contained in:
topjohnwu 2018-11-16 01:15:34 -05:00
parent 3aad9d8166
commit ab74290fe3
5 changed files with 88 additions and 82 deletions

View File

@ -30,6 +30,7 @@ static bool seperate_vendor;
char *system_block, *vendor_block, *magiskloop;
static int bind_mount(const char *from, const char *to);
extern void auto_start_magiskhide();
/***************
* Magic Mount *
@ -599,22 +600,6 @@ static bool check_data() {
return data;
}
extern int launch_magiskhide(int client = -1);
static void auto_start_magiskhide() {
if (!start_log_daemon())
return;
CharArray hide_prop = getprop(MAGISKHIDE_PROP, true);
if (hide_prop != "0") {
pthread_t thread;
xpthread_create(&thread, nullptr, [](void*) -> void* {
launch_magiskhide();
return nullptr;
}, nullptr);
pthread_detach(thread);
}
}
void unlock_blocks() {
DIR *dir;
struct dirent *entry;

View File

@ -42,7 +42,8 @@ int db_strings::getKeyIdx(const char *key) const {
db_settings::db_settings() : data {
ROOT_ACCESS_APPS_AND_ADB,
MULTIUSER_MODE_OWNER_ONLY,
NAMESPACE_MODE_REQUESTER
NAMESPACE_MODE_REQUESTER,
1
} {}
int &db_settings::operator[](const int idx) {
@ -188,7 +189,7 @@ static int settings_cb(void *v, int col_num, char **data, char **col_name) {
return 0;
}
int get_db_settings(sqlite3 *db, struct db_settings *dbs, int key) {
int get_db_settings(sqlite3 *db, db_settings *dbs, int key) {
if (db == nullptr)
return 1;
char *err;
@ -224,7 +225,7 @@ static int strings_cb(void *v, int col_num, char **data, char **col_name) {
return 0;
}
int get_db_strings(sqlite3 *db, struct db_strings *str, int key) {
int get_db_strings(sqlite3 *db, db_strings *str, int key) {
if (db == nullptr)
return 1;
char *err;

View File

@ -12,16 +12,18 @@
((const char *[]) { \
"root_access", \
"multiuser_mode", \
"mnt_ns" \
"mnt_ns", \
"magiskhide", \
})
#define DB_SETTINGS_NUM 3
#define DB_SETTINGS_NUM 4
// Settings keys
enum {
ROOT_ACCESS = 0,
SU_MULTIUSER_MODE,
SU_MNT_NS
SU_MNT_NS,
HIDE_CONFIG
};
// Values for root_access
@ -127,8 +129,8 @@ struct su_access {
********************/
sqlite3 *get_magiskdb();
int get_db_settings(sqlite3 *db, struct db_settings *dbs, int key = -1);
int get_db_strings(sqlite3 *db, struct db_strings *str, int key = -1);
int get_db_settings(sqlite3 *db, db_settings *dbs, int key = -1);
int get_db_strings(sqlite3 *db, db_strings *str, int key = -1);
int get_uid_policy(sqlite3 *db, int uid, struct su_access *su);
int validate_manager(char *alt_pkg, int userid, struct stat *st);
int exec_sql(const char *sql);

View File

@ -16,6 +16,9 @@
#include "db.h"
Vector<CharArray> hide_list;
pthread_mutex_t list_lock;
static pthread_t proc_monitor_thread;
static const char *prop_key[] =
{ "ro.boot.vbmeta.device_state", "ro.boot.verifiedbootstate", "ro.boot.flash.locked",
@ -250,3 +253,76 @@ void ls_list(int client) {
write_string(client, s);
close(client);
}
static void set_hide_config() {
sqlite3 *db = get_magiskdb();
char sql[64];
sprintf(sql, "REPLACE INTO settings (key,value) VALUES('%s',%d)",
DB_SETTING_KEYS[HIDE_CONFIG], hide_enabled);
sqlite3_exec(db, sql, nullptr, nullptr, nullptr);
sqlite3_close_v2(db);
}
int launch_magiskhide(int client) {
if (hide_enabled)
return HIDE_IS_ENABLED;
if (!log_daemon_started)
return LOGCAT_DISABLED;
hide_enabled = true;
set_hide_config();
LOGI("* Starting MagiskHide\n");
hide_sensitive_props();
// Initialize the mutex lock
pthread_mutex_init(&list_lock, nullptr);
// Initialize the hide list
if (!init_list())
goto error;
// Add SafetyNet by default
add_list("com.google.android.gms.unstable");
// Get thread reference
proc_monitor_thread = pthread_self();
if (client >= 0) {
write_int(client, DAEMON_SUCCESS);
close(client);
}
// Start monitoring
proc_monitor();
error:
hide_enabled = false;
return DAEMON_ERROR;
}
int stop_magiskhide() {
LOGI("* Stopping MagiskHide\n");
hide_enabled = false;
set_hide_config();
pthread_kill(proc_monitor_thread, TERM_THREAD);
return DAEMON_SUCCESS;
}
void auto_start_magiskhide() {
if (!start_log_daemon())
return;
sqlite3 *db = get_magiskdb();
db_settings dbs;
get_db_settings(db, &dbs, HIDE_CONFIG);
sqlite3_close_v2(db);
if (dbs[HIDE_CONFIG]) {
pthread_t thread;
xpthread_create(&thread, nullptr, [](void*) -> void* {
launch_magiskhide(-1);
return nullptr;
}, nullptr);
pthread_detach(thread);
}
}

View File

@ -8,15 +8,11 @@
#include <sys/types.h>
#include "magisk.h"
#include "utils.h"
#include "magiskhide.h"
#include "daemon.h"
#include "resetprop.h"
#include "flags.h"
bool hide_enabled = false;
static pthread_t proc_monitor_thread;
pthread_mutex_t list_lock;
[[noreturn]] static void usage(char *arg0) {
fprintf(stderr,
@ -33,60 +29,6 @@ pthread_mutex_t list_lock;
exit(1);
}
int launch_magiskhide(int client) {
if (hide_enabled)
return HIDE_IS_ENABLED;
if (!log_daemon_started) {
setprop(MAGISKHIDE_PROP, "0");
// Remove without actually removing persist props
deleteprop(MAGISKHIDE_PROP);
return LOGCAT_DISABLED;
}
hide_enabled = true;
LOGI("* Starting MagiskHide\n");
deleteprop(MAGISKHIDE_PROP, true);
hide_sensitive_props();
// Initialize the mutex lock
pthread_mutex_init(&list_lock, nullptr);
// Initialize the hide list
if (!init_list())
goto error;
// Add SafetyNet by default
add_list("com.google.android.gms.unstable");
// Get thread reference
proc_monitor_thread = pthread_self();
if (client >= 0) {
write_int(client, DAEMON_SUCCESS);
close(client);
}
// Start monitoring
proc_monitor();
error:
hide_enabled = false;
return DAEMON_ERROR;
}
int stop_magiskhide() {
LOGI("* Stopping MagiskHide\n");
hide_enabled = false;
setprop(MAGISKHIDE_PROP, "0");
// Remove without actually removing persist props
deleteprop(MAGISKHIDE_PROP);
pthread_kill(proc_monitor_thread, TERM_THREAD);
return DAEMON_SUCCESS;
}
void magiskhide_handler(int client) {
int req = read_int(client);
int res = DAEMON_ERROR;