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; return data;
} }
extern int launch_magiskhide();
static void *start_magisk_hide(void *args) { static void *start_magisk_hide(void *args) {
launch_magiskhide(-1); launch_magiskhide();
return NULL; return NULL;
} }

View File

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

View File

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

View File

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

View File

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

View File

@ -2,10 +2,21 @@
#define MAGISK_HIDE_H #define MAGISK_HIDE_H
#include <pthread.h> #include <pthread.h>
#include "daemon.h"
#include "array.h" #include "array.h"
#define TERM_THREAD SIGUSR1 #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 // Process monitor
void proc_monitor(); void proc_monitor();
@ -16,7 +27,6 @@ void clean_magisk_props();
// List managements // List managements
int add_list(char *proc); int add_list(char *proc);
int rm_list(char *proc);
int init_list(); int init_list();
int destroy_list(); int destroy_list();
@ -24,4 +34,20 @@ extern int hide_enabled;
extern pthread_mutex_t list_lock; extern pthread_mutex_t list_lock;
extern Array<char *> hide_list; 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 #endif