From d66c284bed6e9f85c105ea49c5000c865007683c Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Fri, 5 May 2017 04:16:00 +0800 Subject: [PATCH] Fix several small issues --- jni/daemon/bootstages.c | 35 ++++++++++++++++++----------------- jni/magiskhide/magiskhide.c | 3 --- jni/resetprop/resetprop.cpp | 5 ----- jni/utils/misc.c | 22 ++++++++++++++-------- jni/utils/utils.h | 1 + jni/utils/vector.c | 10 ++++++++++ jni/utils/vector.h | 3 +++ scripts/flash_script.sh | 2 +- 8 files changed, 47 insertions(+), 34 deletions(-) diff --git a/jni/daemon/bootstages.c b/jni/daemon/bootstages.c index 432a5a6f1..e7a74ee29 100644 --- a/jni/daemon/bootstages.c +++ b/jni/daemon/bootstages.c @@ -109,10 +109,9 @@ static int get_img_size(const char *img, int *used, int *total) { #define round_size(a) ((((a) / 32) + 1) * 32) static int resize_img(const char *img, int size) { - char buffer[ARG_MAX]; LOGI("resize %s to %dM\n", img, size); - snprintf(buffer, sizeof(buffer), "e2fsck -yf %s && resize2fs %s %dM;", img, img, size); - return system(buffer); + snprintf(buf, PATH_MAX, "e2fsck -yf %s; resize2fs %s %dM;", img, img, size); + return system(buf); } static int merge_img(const char *source, const char *target) { @@ -194,11 +193,11 @@ void exec_common_script(const char* stage) { while ((entry = xreaddir(dir))) { if (entry->d_type == DT_REG) { - snprintf(buf, PATH_MAX, "%s/%s", buf, entry->d_name); - if (access(buf, X_OK) == -1) + snprintf(buf2, PATH_MAX, "%s/%s", buf, entry->d_name); + if (access(buf2, X_OK) == -1) continue; LOGI("%s.d: exec [%s]\n", stage, entry->d_name); - char *const command[] = { "sh", buf, NULL }; + char *const command[] = { "sh", buf2, NULL }; int pid = run_command(NULL, "/system/bin/sh", command); if (pid != -1) waitpid(pid, NULL, 0); @@ -249,13 +248,14 @@ static char *get_full_path(struct node_entry *node) { return strdup(buffer); } +// Free the node and all children recursively static void destroy_subtree(struct node_entry *node) { // Never free parent, since it shall be freed by themselves - free(node->name); struct node_entry *e; vec_for_each(node->children, e) { destroy_subtree(e); } + free(node->name); vec_destroy(node->children); free(node->children); free(node); @@ -271,7 +271,9 @@ static void insert_child(struct node_entry *p, struct node_entry *c) { vec_for_each(p->children, e) { if (strcmp(e->name, c->name) == 0) { // Exist duplicate, replace - destroy_subtree(e); + c->children = e->children; + free(e->name); + free(e); vec_entry(p->children)[_] = c; return; } @@ -353,7 +355,8 @@ static void clone_skeleton(struct node_entry *node, const char *real_path) { closedir(dir); snprintf(buf, PATH_MAX, "%s%s", DUMMDIR, real_path); - xmkdir_p(buf, 0755); + mkdir_p(buf, 0755); + clone_attr(real_path, buf); bind_mount(buf, real_path); vec_for_each(node->children, child) { @@ -445,14 +448,7 @@ static void simple_mount(const char *path) { // Actual file path snprintf(buf, PATH_MAX, "%s/%s", buf, entry->d_name); // Clone all attributes - struct stat s; - xstat(buf2, &s); - chmod(buf, s.st_mode & 0777); - chown(buf, s.st_uid, s.st_gid); - char *con; - getfilecon(buf2, &con); - setfilecon(buf, con); - free(con); + clone_attr(buf2, buf); // Finally, mount the file bind_mount(buf, buf2); } @@ -689,6 +685,7 @@ void post_fs_data(int client) { if (strcmp(hide_prop, "1") == 0) { pthread_t thread; xpthread_create(&thread, NULL, start_magisk_hide, NULL); + pthread_detach(thread); } free(hide_prop); } @@ -703,6 +700,10 @@ void late_start(int client) { write_int(client, 0); close(client); + // Allocate buffer + if (buf == NULL) buf = xmalloc(PATH_MAX); + if (buf2 == NULL) buf2 = xmalloc(PATH_MAX); + // Wait till the full patch is done pthread_join(sepol_patch, NULL); diff --git a/jni/magiskhide/magiskhide.c b/jni/magiskhide/magiskhide.c index 325658593..07a979c4c 100644 --- a/jni/magiskhide/magiskhide.c +++ b/jni/magiskhide/magiskhide.c @@ -57,9 +57,6 @@ void launch_magiskhide(int client) { hideEnabled = 1; - if (init_resetprop()) - goto error; - if (client != -1) { if (setprop("persist.magisk.hide", "1")) goto error; diff --git a/jni/resetprop/resetprop.cpp b/jni/resetprop/resetprop.cpp index 4f1a8f466..85295b665 100644 --- a/jni/resetprop/resetprop.cpp +++ b/jni/resetprop/resetprop.cpp @@ -277,11 +277,6 @@ int resetprop_main(int argc, char *argv[]) { } } - PRINT_D("resetprop by nkk71 & topjohnwu\n"); - - if (init_resetprop()) - return -1; - if (file) { return read_prop_file(filename, trigger); } else if (del) { diff --git a/jni/utils/misc.c b/jni/utils/misc.c index b175cb68a..d2dffce08 100644 --- a/jni/utils/misc.c +++ b/jni/utils/misc.c @@ -308,7 +308,7 @@ int cp_afc(const char *source, const char *target) { int clone_dir(const char *source, const char *target) { DIR *dir; struct dirent *entry; - char *s_path, *t_path, *con; + char *s_path, *t_path; if (!(dir = xopendir(source))) return 1; @@ -316,13 +316,8 @@ int clone_dir(const char *source, const char *target) { s_path = xmalloc(PATH_MAX); t_path = xmalloc(PATH_MAX); - struct stat buf; - xstat(source, &buf); - mkdir_p(target, buf.st_mode & 0777); - xchmod(target, buf.st_mode & 0777); - lgetfilecon(source, &con); - lsetfilecon(target, con); - free(con); + mkdir_p(target, 0755); + clone_attr(source, target); while ((entry = xreaddir(dir))) { if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) @@ -380,3 +375,14 @@ int rm_rf(const char *target) { } return 0; } + +void clone_attr(const char *source, const char *target) { + struct stat buf; + xstat(source, &buf); + chmod(target, buf.st_mode & 0777); + chown(target, buf.st_uid, buf.st_gid); + char *con; + lgetfilecon(source, &con); + lsetfilecon(target, con); + free(con); +} diff --git a/jni/utils/utils.h b/jni/utils/utils.h index e6884dc32..12ce637f4 100644 --- a/jni/utils/utils.h +++ b/jni/utils/utils.h @@ -88,5 +88,6 @@ int open_new(const char *filename); int cp_afc(const char *source, const char *target); int clone_dir(const char *source, const char *target); int rm_rf(const char *target); +void clone_attr(const char *source, const char *target); #endif diff --git a/jni/utils/vector.c b/jni/utils/vector.c index 63e5cafc9..bd2c323e0 100644 --- a/jni/utils/vector.c +++ b/jni/utils/vector.c @@ -2,6 +2,7 @@ */ #include +#include #include "vector.h" @@ -49,3 +50,12 @@ void vec_deep_destroy(struct vector *v) { } vec_destroy(v); } + +struct vector *vec_dup(struct vector *v) { + struct vector *ret = malloc(sizeof(*ret)); + vec_size(ret) = vec_size(v); + vec_cap(ret) = vec_cap(v); + vec_entry(v) = malloc(sizeof(void*) * vec_cap(ret)); + memcpy(vec_entry(ret), vec_entry(v), sizeof(void*) * vec_cap(ret)); + return ret; +} diff --git a/jni/utils/vector.h b/jni/utils/vector.h index 224b2a0a0..ef169f21d 100644 --- a/jni/utils/vector.h +++ b/jni/utils/vector.h @@ -11,11 +11,14 @@ struct vector { size_t cap; void **data; }; + void vec_init(struct vector *v); void vec_push_back(struct vector *v, void *p); void vec_sort(struct vector *v, int (*compar)(const void *, const void *)); void vec_destroy(struct vector *v); void vec_deep_destroy(struct vector *v); +struct vector *vec_dup(struct vector *v); + #define vec_size(v) (v)->size #define vec_cap(v) (v)->cap #define vec_entry(v) (v)->data diff --git a/scripts/flash_script.sh b/scripts/flash_script.sh index c508856d9..69e854110 100644 --- a/scripts/flash_script.sh +++ b/scripts/flash_script.sh @@ -285,7 +285,7 @@ if [ -f $IMG ]; then ui_print "- $IMG detected!" else ui_print "- Creating $IMG" - make_ext4fs -l 64M -a /magisk -S $COMMONDIR/file_contexts_image $IMG + make_ext4fs -l 32M -a /magisk -S $COMMONDIR/file_contexts_image $IMG fi mount_image $IMG /magisk