mirror of
https://github.com/topjohnwu/Magisk.git
synced 2025-12-03 07:01:53 +00:00
Modernize code base
This commit is contained in:
@@ -15,7 +15,7 @@
|
||||
#include "daemon.h"
|
||||
#include "db.h"
|
||||
|
||||
auto hide_list = Array<char *>();
|
||||
Array<CharArray> hide_list;
|
||||
|
||||
static const char *prop_key[] =
|
||||
{ "ro.boot.vbmeta.device_state", "ro.boot.verifiedbootstate", "ro.boot.flash.locked",
|
||||
@@ -46,14 +46,10 @@ void hide_sensitive_props() {
|
||||
LOGI("hide_utils: Hiding sensitive props\n");
|
||||
|
||||
// Hide all sensitive props
|
||||
char *value;
|
||||
for (int i = 0; prop_key[i]; ++i) {
|
||||
value = getprop(prop_key[i]);
|
||||
if (value) {
|
||||
if (strcmp(value, prop_value[i]) != 0)
|
||||
setprop(prop_key[i], prop_value[i], false);
|
||||
free(value);
|
||||
}
|
||||
CharArray value = getprop(prop_key[i]);
|
||||
if (!value.empty() && value != prop_value[i])
|
||||
setprop(prop_key[i], prop_value[i], false);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -139,13 +135,11 @@ void clean_magisk_props() {
|
||||
}, nullptr, false);
|
||||
}
|
||||
|
||||
static int add_list(sqlite3 *db, char *proc) {
|
||||
static int add_list(sqlite3 *db, const char *proc) {
|
||||
for (auto &s : hide_list) {
|
||||
// They should be unique
|
||||
if (strcmp(s, proc) == 0) {
|
||||
free(proc);
|
||||
if (s == proc)
|
||||
return HIDE_ITEM_EXIST;
|
||||
}
|
||||
}
|
||||
|
||||
LOGI("hide_list add: [%s]\n", proc);
|
||||
@@ -164,7 +158,7 @@ static int add_list(sqlite3 *db, char *proc) {
|
||||
return DAEMON_SUCCESS;
|
||||
}
|
||||
|
||||
int add_list(char *proc) {
|
||||
int add_list(const char *proc) {
|
||||
sqlite3 *db = get_magiskdb();
|
||||
if (db) {
|
||||
int ret = add_list(db, proc);
|
||||
@@ -175,20 +169,20 @@ int add_list(char *proc) {
|
||||
}
|
||||
|
||||
int add_list(int client) {
|
||||
return add_list(read_string(client));
|
||||
char *proc = read_string(client);
|
||||
int ret = add_list(proc);
|
||||
free(proc);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int rm_list(char *proc) {
|
||||
int ret = DAEMON_ERROR;
|
||||
|
||||
static int rm_list(const char *proc) {
|
||||
// Update list in critical region
|
||||
bool do_rm = false;
|
||||
pthread_mutex_lock(&list_lock);
|
||||
for (auto it = hide_list.begin(); it != hide_list.end(); ++it) {
|
||||
if (strcmp(*it, proc) == 0) {
|
||||
if (*it == proc) {
|
||||
do_rm = true;
|
||||
LOGI("hide_list rm: [%s]\n", proc);
|
||||
free(*it);
|
||||
hide_list.erase(it);
|
||||
break;
|
||||
}
|
||||
@@ -199,45 +193,44 @@ static int rm_list(char *proc) {
|
||||
kill_process(proc);
|
||||
sqlite3 *db = get_magiskdb();
|
||||
if (db == nullptr)
|
||||
goto error;
|
||||
return DAEMON_ERROR;
|
||||
char sql[128];
|
||||
sprintf(sql, "DELETE FROM hidelist WHERE process='%s'", proc);
|
||||
sqlite3_exec(db, sql, nullptr, nullptr, nullptr);
|
||||
sqlite3_close_v2(db);
|
||||
ret = DAEMON_SUCCESS;
|
||||
return DAEMON_SUCCESS;
|
||||
} else {
|
||||
ret = HIDE_ITEM_NOT_EXIST;
|
||||
return HIDE_ITEM_NOT_EXIST;
|
||||
}
|
||||
}
|
||||
|
||||
error:
|
||||
int rm_list(int client) {
|
||||
char *proc = read_string(client);
|
||||
int ret = rm_list(proc);
|
||||
free(proc);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int rm_list(int client) {
|
||||
return rm_list(read_string(client));
|
||||
}
|
||||
|
||||
#define LEGACY_LIST MOUNTPOINT "/.core/hidelist"
|
||||
|
||||
int init_list() {
|
||||
bool init_list() {
|
||||
LOGD("hide_list: initialize\n");
|
||||
|
||||
sqlite3 *db = get_magiskdb();
|
||||
if (db == nullptr)
|
||||
return 1;
|
||||
return false;
|
||||
|
||||
sqlite3_exec(db, "SELECT process FROM hidelist",
|
||||
[] (auto, auto, char **data, auto) -> int
|
||||
{
|
||||
LOGI("hide_list: [%s]\n", data[0]);
|
||||
hide_list.push_back(strdup(data[0]));
|
||||
hide_list.push_back(data[0]);
|
||||
return 0;
|
||||
}, nullptr, nullptr);
|
||||
|
||||
// Migrate old hide list into database
|
||||
if (access(LEGACY_LIST, R_OK) == 0) {
|
||||
auto tmp = Array<char *>();
|
||||
Array<CharArray> tmp;
|
||||
file_to_array(LEGACY_LIST, tmp);
|
||||
for (auto &s : tmp)
|
||||
add_list(db, s);
|
||||
@@ -245,14 +238,7 @@ int init_list() {
|
||||
}
|
||||
|
||||
sqlite3_close_v2(db);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int destroy_list() {
|
||||
for (auto &str : hide_list)
|
||||
free(str);
|
||||
hide_list.clear();
|
||||
return 0;
|
||||
return true;
|
||||
}
|
||||
|
||||
void ls_list(int client) {
|
||||
|
||||
@@ -54,11 +54,11 @@ int launch_magiskhide() {
|
||||
pthread_mutex_init(&list_lock, nullptr);
|
||||
|
||||
// Initialize the hide list
|
||||
if (init_list())
|
||||
if (!init_list())
|
||||
goto error;
|
||||
|
||||
// Add SafetyNet by default
|
||||
add_list(strdup("com.google.android.gms.unstable"));
|
||||
add_list("com.google.android.gms.unstable");
|
||||
|
||||
// Get thread reference
|
||||
proc_monitor_thread = pthread_self();
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
|
||||
#include "daemon.h"
|
||||
#include "array.h"
|
||||
#include "CharArray.h"
|
||||
|
||||
#define TERM_THREAD SIGUSR1
|
||||
|
||||
@@ -24,13 +25,12 @@ void hide_sensitive_props();
|
||||
void clean_magisk_props();
|
||||
|
||||
// List managements
|
||||
int add_list(char *proc);
|
||||
int init_list();
|
||||
int destroy_list();
|
||||
int add_list(const char *proc);
|
||||
bool init_list();
|
||||
|
||||
extern int hide_enabled;
|
||||
extern pthread_mutex_t list_lock;
|
||||
extern Array<char *> hide_list;
|
||||
extern Array<CharArray> hide_list;
|
||||
|
||||
enum {
|
||||
LAUNCH_MAGISKHIDE,
|
||||
|
||||
@@ -27,13 +27,13 @@ static int sockfd = -1;
|
||||
// Workaround for the lack of pthread_cancel
|
||||
static void term_thread(int) {
|
||||
LOGD("proc_monitor: running cleanup\n");
|
||||
destroy_list();
|
||||
hide_list.clear(true);
|
||||
hide_enabled = 0;
|
||||
close(sockfd);
|
||||
sockfd = -1;
|
||||
pthread_mutex_destroy(&list_lock);
|
||||
LOGD("proc_monitor: terminating\n");
|
||||
pthread_exit(NULL);
|
||||
pthread_exit(nullptr);
|
||||
}
|
||||
|
||||
static int read_ns(const int pid, struct stat *st) {
|
||||
@@ -63,7 +63,7 @@ static void hide_daemon(int pid) {
|
||||
LOGD("hide_daemon: start unmount for pid=[%d]\n", pid);
|
||||
|
||||
char buffer[PATH_MAX];
|
||||
auto mounts = Array<char *>();
|
||||
Array<CharArray> mounts;
|
||||
|
||||
manage_selinux();
|
||||
clean_magisk_props();
|
||||
@@ -76,11 +76,10 @@ static void hide_daemon(int pid) {
|
||||
|
||||
// Unmount dummy skeletons and /sbin links
|
||||
for (auto &s : mounts) {
|
||||
if (strstr(s, "tmpfs /system/") || strstr(s, "tmpfs /vendor/") || strstr(s, "tmpfs /sbin")) {
|
||||
if (s.contains("tmpfs /system/") || s.contains("tmpfs /vendor/") || s.contains("tmpfs /sbin")) {
|
||||
sscanf(s, "%*s %4096s", buffer);
|
||||
lazy_unmount(buffer);
|
||||
}
|
||||
free(s);
|
||||
}
|
||||
mounts.clear();
|
||||
|
||||
@@ -90,13 +89,11 @@ static void hide_daemon(int pid) {
|
||||
|
||||
// Unmount everything under /system, /vendor, and loop mounts
|
||||
for (auto &s : mounts) {
|
||||
if (strstr(s, "/dev/block/loop") || strstr(s, " /system/") || strstr(s, " /vendor/")) {
|
||||
if (s.contains("/dev/block/loop") || s.contains(" /system/") || s.contains(" /vendor/")) {
|
||||
sscanf(s, "%*s %4096s", buffer);
|
||||
lazy_unmount(buffer);
|
||||
}
|
||||
free(s);
|
||||
}
|
||||
mounts.clear();
|
||||
|
||||
exit:
|
||||
// Send resume signal
|
||||
@@ -159,7 +156,7 @@ void proc_monitor() {
|
||||
bool hide = false;
|
||||
pthread_mutex_lock(&list_lock);
|
||||
for (auto &s : hide_list) {
|
||||
if (strcmp(proc, s) == 0) {
|
||||
if (s == proc) {
|
||||
hide = true;
|
||||
break;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user