Separate magiskhide logic from main daemon

This commit is contained in:
topjohnwu 2018-11-01 14:08:33 -04:00
parent 27c688252d
commit b8a3cc8b60
6 changed files with 97 additions and 101 deletions

View File

@ -533,8 +533,10 @@ static int check_data() {
return data;
}
extern int launch_magiskhide();
static void *start_magisk_hide(void *args) {
launch_magiskhide(-1);
launch_magiskhide();
return NULL;
}

View File

@ -38,11 +38,7 @@ static void *request_handler(void *args) {
get_client_cred(client, &credential);
switch (req) {
case LAUNCH_MAGISKHIDE:
case STOP_MAGISKHIDE:
case ADD_HIDELIST:
case RM_HIDELIST:
case LS_HIDELIST:
case MAGISKHIDE:
case POST_FS_DATA:
case LATE_START:
case BOOT_COMPLETE:
@ -56,20 +52,8 @@ static void *request_handler(void *args) {
}
switch (req) {
case LAUNCH_MAGISKHIDE:
launch_magiskhide(client);
break;
case STOP_MAGISKHIDE:
stop_magiskhide(client);
break;
case ADD_HIDELIST:
add_hide_list(client);
break;
case RM_HIDELIST:
rm_hide_list(client);
break;
case LS_HIDELIST:
ls_hide_list(client);
case MAGISKHIDE:
magiskhide_handler(client);
break;
case SUPERUSER:
su_daemon_handler(client, &credential);

View File

@ -24,11 +24,7 @@ enum {
POST_FS_DATA,
LATE_START,
BOOT_COMPLETE,
LAUNCH_MAGISKHIDE,
STOP_MAGISKHIDE,
ADD_HIDELIST,
RM_HIDELIST,
LS_HIDELIST,
MAGISKHIDE,
HIDE_CONNECT,
HANDSHAKE
};
@ -38,11 +34,7 @@ enum {
DAEMON_ERROR = -1,
DAEMON_SUCCESS = 0,
ROOT_REQUIRED,
LOGCAT_DISABLED,
HIDE_IS_ENABLED,
HIDE_NOT_ENABLED,
HIDE_ITEM_EXIST,
HIDE_ITEM_NOT_EXIST,
DAEMON_LAST
};
// daemon.c
@ -87,11 +79,7 @@ void boot_complete(int client);
* MagiskHide *
**************/
void launch_magiskhide(int client);
void stop_magiskhide(int client);
void add_hide_list(int client);
void rm_hide_list(int client);
void ls_hide_list(int client);
void magiskhide_handler(int client);
/*************
* Superuser *

View File

@ -166,11 +166,6 @@ static int add_list(sqlite3 *db, char *proc) {
}
int add_list(char *proc) {
if (!hide_enabled) {
free(proc);
return HIDE_NOT_ENABLED;
}
sqlite3 *db = get_magiskdb();
if (db) {
int ret = add_list(db, proc);
@ -180,12 +175,11 @@ int add_list(char *proc) {
return DAEMON_ERROR;
}
int rm_list(char *proc) {
if (!hide_enabled) {
free(proc);
return HIDE_NOT_ENABLED;
}
int add_list(int client) {
return add_list(read_string(client));
}
static int rm_list(char *proc) {
int ret = DAEMON_ERROR;
// Update list in critical region
@ -216,11 +210,15 @@ int rm_list(char *proc) {
ret = HIDE_ITEM_NOT_EXIST;
}
error:
error:
free(proc);
return ret;
}
int rm_list(int client) {
return rm_list(read_string(client));
}
#define LEGACY_LIST MOUNTPOINT "/.core/hidelist"
int init_list() {
@ -258,25 +256,7 @@ int destroy_list() {
return 0;
}
void add_hide_list(int client) {
char *proc = read_string(client);
// ack
write_int(client, add_list(proc));
close(client);
}
void rm_hide_list(int client) {
char *proc = read_string(client);
// ack
write_int(client, rm_list(proc));
close(client);
}
void ls_hide_list(int client) {
if (!hide_enabled) {
write_int(client, HIDE_NOT_ENABLED);
return;
}
void ls_list(int client) {
write_int(client, DAEMON_SUCCESS);
write_int(client, hide_list.size());
for (auto &s : hide_list)

View File

@ -18,7 +18,7 @@ int hide_enabled = 0;
static pthread_t proc_monitor_thread;
pthread_mutex_t list_lock;
static void usage(char *arg0) {
[[noreturn]] static void usage(char *arg0) {
fprintf(stderr,
"MagiskHide v" xstr(MAGISK_VERSION) "(" xstr(MAGISK_VER_CODE) ") (by topjohnwu) - Hide Magisk!\n\n"
"Usage: %s [--options [arguments...] ]\n\n"
@ -32,24 +32,15 @@ static void usage(char *arg0) {
exit(1);
}
void launch_magiskhide(int client) {
if (hide_enabled) {
if (client > 0) {
write_int(client, HIDE_IS_ENABLED);
close(client);
}
return;
}
int launch_magiskhide() {
if (hide_enabled)
return HIDE_IS_ENABLED;
if (!log_daemon_started) {
if (client > 0) {
write_int(client, LOGCAT_DISABLED);
close(client);
}
setprop(MAGISKHIDE_PROP, "0");
// Remove without actually removing persist props
deleteprop2(MAGISKHIDE_PROP, 0);
return;
return LOGCAT_DISABLED;
}
hide_enabled = 1;
@ -69,33 +60,18 @@ void launch_magiskhide(int client) {
// Add SafetyNet by default
add_list(strdup("com.google.android.gms.unstable"));
if (client > 0) {
write_int(client, DAEMON_SUCCESS);
close(client);
}
// Get thread reference
proc_monitor_thread = pthread_self();
// Start monitoring
proc_monitor();
return;
return DAEMON_SUCCESS;
error:
hide_enabled = 0;
if (client > 0) {
write_int(client, DAEMON_ERROR);
close(client);
}
return;
return DAEMON_ERROR;
}
void stop_magiskhide(int client) {
if (!hide_enabled) {
write_int(client, HIDE_NOT_ENABLED);
close(client);
return;
}
int stop_magiskhide() {
LOGI("* Stopping MagiskHide\n");
hide_enabled = 0;
@ -104,7 +80,45 @@ void stop_magiskhide(int client) {
deleteprop2(MAGISKHIDE_PROP, 0);
pthread_kill(proc_monitor_thread, TERM_THREAD);
write_int(client, DAEMON_SUCCESS);
return DAEMON_SUCCESS;
}
void magiskhide_handler(int client) {
int req = read_int(client);
int res = DAEMON_ERROR;
switch (req) {
case STOP_MAGISKHIDE:
case ADD_HIDELIST:
case RM_HIDELIST:
case LS_HIDELIST:
if (!hide_enabled) {
write_int(client, HIDE_NOT_ENABLED);
close(client);
return;
}
}
switch (req) {
case LAUNCH_MAGISKHIDE:
res = launch_magiskhide();
break;
case STOP_MAGISKHIDE:
res = stop_magiskhide();
break;
case ADD_HIDELIST:
res = add_list(client);
break;
case RM_HIDELIST:
res = rm_list(client);
break;
case LS_HIDELIST:
ls_list(client);
client = -1;
break;
}
write_int(client, res);
close(client);
}
@ -112,7 +126,7 @@ int magiskhide_main(int argc, char *argv[]) {
if (argc < 2) {
usage(argv[0]);
}
int req = DO_NOTHING;
int req;
if (strcmp(argv[1], "--enable") == 0) {
req = LAUNCH_MAGISKHIDE;
} else if (strcmp(argv[1], "--disable") == 0) {
@ -126,7 +140,9 @@ int magiskhide_main(int argc, char *argv[]) {
} else {
usage(argv[0]);
}
int fd = connect_daemon();
write_int(fd, MAGISKHIDE);
write_int(fd, req);
if (req == ADD_HIDELIST || req == RM_HIDELIST) {
write_string(fd, argv[2]);
@ -140,7 +156,7 @@ int magiskhide_main(int argc, char *argv[]) {
return code;
case LOGCAT_DISABLED:
fprintf(stderr, "Logcat is disabled, cannot start MagiskHide\n");
return (code);
return code;
case HIDE_NOT_ENABLED:
fprintf(stderr, "MagiskHide is not enabled yet\n");
return code;

View File

@ -2,10 +2,21 @@
#define MAGISK_HIDE_H
#include <pthread.h>
#include "daemon.h"
#include "array.h"
#define TERM_THREAD SIGUSR1
// Daemon entries
extern "C" {
int launch_magiskhide();
}
int stop_magiskhide();
int add_list(int client);
int rm_list(int client);
void ls_list(int client);
// Process monitor
void proc_monitor();
@ -16,7 +27,6 @@ void clean_magisk_props();
// List managements
int add_list(char *proc);
int rm_list(char *proc);
int init_list();
int destroy_list();
@ -24,4 +34,20 @@ extern int hide_enabled;
extern pthread_mutex_t list_lock;
extern Array<char *> hide_list;
enum {
LAUNCH_MAGISKHIDE,
STOP_MAGISKHIDE,
ADD_HIDELIST,
RM_HIDELIST,
LS_HIDELIST
};
enum {
LOGCAT_DISABLED = DAEMON_LAST,
HIDE_IS_ENABLED,
HIDE_NOT_ENABLED,
HIDE_ITEM_EXIST,
HIDE_ITEM_NOT_EXIST
};
#endif