Make MagiskHide work without magisk.img

This commit is contained in:
topjohnwu 2017-09-06 02:25:40 +08:00
parent 0bf404f75e
commit eceba26894
6 changed files with 47 additions and 28 deletions

View File

@ -589,7 +589,7 @@ void post_fs_data(int client) {
// Trim, mount magisk.img, which will also travel through the modules
// After this, it will create the module list
if (prepare_img())
goto unblock;
goto core_only; // Mounting fails, we can only do core only stuffs
// Run common scripts
LOGI("* Running post-fs-data.d scripts\n");

View File

@ -40,6 +40,7 @@ static void *request_handler(void *args) {
case STOP_MAGISKHIDE:
case ADD_HIDELIST:
case RM_HIDELIST:
case LS_HIDELIST:
case POST_FS:
case POST_FS_DATA:
case LATE_START:
@ -65,6 +66,9 @@ static void *request_handler(void *args) {
case RM_HIDELIST:
rm_hide_list(client);
break;
case LS_HIDELIST:
ls_hide_list(client);
break;
case SUPERUSER:
su_daemon_receiver(client);
break;

View File

@ -15,6 +15,7 @@ typedef enum {
STOP_MAGISKHIDE,
ADD_HIDELIST,
RM_HIDELIST,
LS_HIDELIST,
SUPERUSER,
CHECK_VERSION,
CHECK_VERSION_CODE,
@ -69,6 +70,7 @@ 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);
/*************
* Superuser *

View File

@ -19,7 +19,7 @@
#include "daemon.h"
static char *prop_key[] =
{ "ro.boot.verifiedbootstate", "ro.boot.flash.locked", "ro.boot.veritymode", "ro.boot.warranty_bit", "ro.warranty_bit",
{ "ro.boot.verifiedbootstate", "ro.boot.flash.locked", "ro.boot.veritymode", "ro.boot.warranty_bit", "ro.warranty_bit",
"ro.debuggable", "ro.secure", "ro.build.type", "ro.build.tags", "ro.build.selinux", NULL };
static char *prop_value[] =
@ -72,7 +72,7 @@ void clean_magisk_props() {
void relink_sbin() {
struct stat st;
if (stat("/sbin_orig", &st) == -1 && errno == ENOENT) {
// Re-link all binaries and bind mount
// Re-link all binaries and bind mount
DIR *dir;
struct dirent *entry;
char from[PATH_MAX], to[PATH_MAX];
@ -98,7 +98,7 @@ void relink_sbin() {
symlink(from, to);
lsetfilecon(to, "u:object_r:rootfs:s0");
}
closedir(dir);
xmount("/dev/sbin_bind", "/sbin", NULL, MS_BIND, NULL);
@ -140,10 +140,7 @@ int add_list(char *proc) {
pthread_mutex_unlock(&hide_lock);
pthread_mutex_lock(&file_lock);
if (vector_to_file(HIDELIST, hide_list)) {
pthread_mutex_unlock(&file_lock);
return DAEMON_ERROR;
}
vector_to_file(HIDELIST, hide_list); // Do not complain if file not found
pthread_mutex_unlock(&file_lock);
return DAEMON_SUCCESS;
}
@ -184,8 +181,7 @@ int rm_list(char *proc) {
ret = DAEMON_SUCCESS;
pthread_mutex_lock(&file_lock);
if (vector_to_file(HIDELIST, hide_list))
ret = DAEMON_ERROR;
vector_to_file(HIDELIST, hide_list); // Do not complain if file not found
pthread_mutex_unlock(&file_lock);
} else {
ret = HIDE_ITEM_NOT_EXIST;
@ -241,3 +237,18 @@ void rm_hide_list(int client) {
write_int(client, rm_list(proc));
close(client);
}
void ls_hide_list(int client) {
err_handler = do_nothing;
if (!hideEnabled) {
write_int(client, HIDE_NOT_ENABLED);
return;
}
write_int(client, DAEMON_SUCCESS);
write_int(client, vec_size(hide_list));
char *s;
vec_for_each(hide_list, s) {
write_string(client, s);
}
close(client);
}

View File

@ -123,15 +123,7 @@ int magiskhide_main(int argc, char *argv[]) {
} else if (strcmp(argv[1], "--rm") == 0 && argc > 2) {
req = RM_HIDELIST;
} else if (strcmp(argv[1], "--ls") == 0) {
FILE *fp = fopen(HIDELIST, "r");
if (fp == NULL)
return 1;
char buffer[512];
while (fgets(buffer, sizeof(buffer), fp)) {
printf("%s", buffer);
}
fclose(fp);
return 0;
req = LS_HIDELIST;
}
int fd = connect_daemon();
write_int(fd, req);
@ -139,28 +131,38 @@ int magiskhide_main(int argc, char *argv[]) {
write_string(fd, argv[2]);
}
daemon_response code = read_int(fd);
close(fd);
switch (code) {
case DAEMON_ERROR:
fprintf(stderr, "Error occured in daemon...\n");
break;
return code;
case DAEMON_SUCCESS:
break;
case ROOT_REQUIRED:
fprintf(stderr, "Root is required for this operation\n");
break;
return code;
case HIDE_NOT_ENABLED:
fprintf(stderr, "Magisk hide is not enabled yet\n");
break;
return code;
case HIDE_IS_ENABLED:
fprintf(stderr, "Magisk hide is already enabled\n");
break;
return code;
case HIDE_ITEM_EXIST:
fprintf(stderr, "Process [%s] already exists in hide list\n", argv[2]);
break;
return code;
case HIDE_ITEM_NOT_EXIST:
fprintf(stderr, "Process [%s] does not exist in hide list\n", argv[2]);
break;
return code;
}
return code;
if (req == LS_HIDELIST) {
int argc = read_int(fd);
for (int i = 0; i < argc; ++i) {
char *s = read_string(fd);
printf("%s\n", s);
free(s);
}
}
close(fd);
return 0;
}

View File

@ -70,7 +70,7 @@ int file_to_vector(const char* filename, struct vector *v) {
size_t len = 0;
ssize_t read;
FILE *fp = fopen(filename, "r");
FILE *fp = xfopen(filename, "r");
if (fp == NULL)
return 1;