From d3947d2cfa9c41b9bc1d5f8db46962e8121f4098 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Thu, 8 Nov 2018 06:43:11 -0500 Subject: [PATCH] Adjust logging in magiskpolicy --- native/jni/magiskpolicy/magiskpolicy.cpp | 1 + native/jni/magiskpolicy/sepolicy.c | 116 +++++++++-------------- native/jni/misc/init.cpp | 4 - 3 files changed, 47 insertions(+), 74 deletions(-) diff --git a/native/jni/magiskpolicy/magiskpolicy.cpp b/native/jni/magiskpolicy/magiskpolicy.cpp index 43f02140a..dc32d0ff4 100644 --- a/native/jni/magiskpolicy/magiskpolicy.cpp +++ b/native/jni/magiskpolicy/magiskpolicy.cpp @@ -376,6 +376,7 @@ static void parse_statement(char *statement) { } int magiskpolicy_main(int argc, char *argv[]) { + cmdline_logging(); const char *outfile = nullptr; bool magisk = false, live = false; diff --git a/native/jni/magiskpolicy/sepolicy.c b/native/jni/magiskpolicy/sepolicy.c index 57c67d800..850d0b8fd 100644 --- a/native/jni/magiskpolicy/sepolicy.c +++ b/native/jni/magiskpolicy/sepolicy.c @@ -22,6 +22,7 @@ #include "utils.h" #include "magiskpolicy.h" #include "sepolicy.h" +#include "logging.h" policydb_t *policydb = NULL; extern int policydb_index_decls(sepol_handle_t * handle, policydb_t * p); @@ -29,7 +30,7 @@ extern int policydb_index_decls(sepol_handle_t * handle, policydb_t * p); static void *cmalloc(size_t s) { void *t = calloc(s, 1); if (t == NULL) { - fprintf(stderr, "Out of memory\n"); + LOGE("Out of memory\n"); exit(1); } return t; @@ -103,7 +104,7 @@ static int __add_rule(int s, int t, int c, int p, int effect, int not) { if (new_rule) { if (avtab_insert(&policydb->te_avtab, &key, av)) { - fprintf(stderr, "Error inserting into avtab\n"); + LOGW("Error inserting into avtab\n"); return 1; } free(av); @@ -182,7 +183,7 @@ static int __add_xperm_rule(int s, int t, int c, uint16_t low, uint16_t high, in if (new_rule) { if (avtab_insert(&policydb->te_avtab, &key, av)) { - fprintf(stderr, "Error inserting into avtab\n"); + LOGW("Error inserting into avtab\n"); return 1; } free(av); @@ -218,10 +219,9 @@ static int add_xperm_rule_auto(type_datum_t *src, type_datum_t *tgt, class_datum } int load_policydb(const char *filename) { - int fd; - struct stat sb; struct policy_file pf; void *map; + size_t size; int ret; if (policydb) @@ -229,41 +229,23 @@ int load_policydb(const char *filename) { policydb = cmalloc(sizeof(*policydb)); - fd = open(filename, O_RDONLY); - if (fd < 0) { - fprintf(stderr, "Can't open '%s': %s\n", - filename, strerror(errno)); - return 1; - } - if (fstat(fd, &sb) < 0) { - fprintf(stderr, "Can't stat '%s': %s\n", - filename, strerror(errno)); - return 1; - } - map = mmap(NULL, sb.st_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, - fd, 0); - if (map == MAP_FAILED) { - fprintf(stderr, "Can't mmap '%s': %s\n", - filename, strerror(errno)); - return 1; - } + mmap_ro(filename, &map, &size); policy_file_init(&pf); pf.type = PF_USE_MEMORY; pf.data = map; - pf.len = sb.st_size; + pf.len = size; if (policydb_init(policydb)) { - fprintf(stderr, "policydb_init: Out of memory!\n"); + LOGE("policydb_init: Out of memory!\n"); return 1; } ret = policydb_read(policydb, &pf, 0); if (ret) { - fprintf(stderr, "error(s) encountered while parsing configuration\n"); + LOGE("error(s) encountered while parsing configuration\n"); return 1; } - munmap(map, sb.st_size); - close(fd); + munmap(map, size); return 0; } @@ -290,7 +272,7 @@ int compile_split_cil() { mmap_ro(SPLIT_PLAT_CIL, &addr, &size); if (cil_add_file(db, SPLIT_PLAT_CIL, addr, size)) return 1; - fprintf(stderr, "cil_add[%s]\n", SPLIT_PLAT_CIL); + LOGD("cil_add[%s]\n", SPLIT_PLAT_CIL); munmap(addr, size); // mapping @@ -301,7 +283,7 @@ int compile_split_cil() { mmap_ro(path, &addr, &size); if (cil_add_file(db, path, addr, size)) return 1; - fprintf(stderr, "cil_add[%s]\n", path); + LOGD("cil_add[%s]\n", path); munmap(addr, size); close(fd); @@ -315,7 +297,7 @@ int compile_split_cil() { mmap_ro(path, &addr, &size); if (cil_add_file(db, path, addr, size)) return 1; - fprintf(stderr, "cil_add[%s]\n", path); + LOGD("cil_add[%s]\n", path); munmap(addr, size); } } @@ -337,23 +319,19 @@ int dump_policydb(const char *filename) { size_t len; policydb_to_image(NULL, policydb, &data, &len); if (data == NULL) { - fprintf(stderr, "Fail to dump policy image!"); + LOGE("Fail to dump policy image!"); return 1; } fd = creat(filename, 0644); if (fd < 0) { - fprintf(stderr, "Can't open '%s': %s\n", - filename, strerror(errno)); + LOGE("Can't open '%s': %s\n", filename, strerror(errno)); return 1; } - ret = write(fd, data, len); + ret = xwrite(fd, data, len); close(fd); - if (ret < 0) { - fprintf(stderr, "Could not write policy to %s\n", - filename); + if (ret < 0) return 1; - } return 0; } @@ -366,7 +344,7 @@ void destroy_policydb() { int create_domain(const char *d) { symtab_datum_t *src = hashtab_search(policydb->p_types.table, d); if(src) { - fprintf(stderr, "Domain %s already exists\n", d); + LOGW("Domain %s already exists\n", d); return 0; } @@ -420,18 +398,18 @@ int set_domain_state(const char *s, int state) { hashtab_for_each(policydb->p_types.table, &cur) { type = cur->datum; if (ebitmap_set_bit(&policydb->permissive_map, type->s.value, state)) { - fprintf(stderr, "Could not set bit in permissive map\n"); + LOGW("Could not set bit in permissive map\n"); return 1; } } } else { type = hashtab_search(policydb->p_types.table, s); if (type == NULL) { - fprintf(stderr, "type %s does not exist\n", s); - return 1; + LOGW("type %s does not exist\n", s); + return 1; } if (ebitmap_set_bit(&policydb->permissive_map, type->s.value, state)) { - fprintf(stderr, "Could not set bit in permissive map\n"); + LOGW("Could not set bit in permissive map\n"); return 1; } } @@ -449,22 +427,22 @@ int add_transition(const char *s, const char *t, const char *c, const char *d) { src = hashtab_search(policydb->p_types.table, s); if (src == NULL) { - fprintf(stderr, "source type %s does not exist\n", s); + LOGW("source type %s does not exist\n", s); return 1; } tgt = hashtab_search(policydb->p_types.table, t); if (tgt == NULL) { - fprintf(stderr, "target type %s does not exist\n", t); + LOGW("target type %s does not exist\n", t); return 1; } cls = hashtab_search(policydb->p_classes.table, c); if (cls == NULL) { - fprintf(stderr, "class %s does not exist\n", c); + LOGW("class %s does not exist\n", c); return 1; } def = hashtab_search(policydb->p_types.table, d); if (def == NULL) { - fprintf(stderr, "default type %s does not exist\n", d); + LOGW("default type %s does not exist\n", d); return 1; } @@ -482,7 +460,7 @@ int add_transition(const char *s, const char *t, const char *c, const char *d) { if (new_rule) { if (avtab_insert(&policydb->te_avtab, &key, av)) { - fprintf(stderr, "Error inserting into avtab\n"); + LOGW("Error inserting into avtab\n"); return 1; } free(av); @@ -497,22 +475,22 @@ int add_file_transition(const char *s, const char *t, const char *c, const char src = hashtab_search(policydb->p_types.table, s); if (src == NULL) { - fprintf(stderr, "source type %s does not exist\n", s); + LOGW("source type %s does not exist\n", s); return 1; } tgt = hashtab_search(policydb->p_types.table, t); if (tgt == NULL) { - fprintf(stderr, "target type %s does not exist\n", t); + LOGW("target type %s does not exist\n", t); return 1; } cls = hashtab_search(policydb->p_classes.table, c); if (cls == NULL) { - fprintf(stderr, "class %s does not exist\n", c); + LOGW("class %s does not exist\n", c); return 1; } def = hashtab_search(policydb->p_types.table, d); if (def == NULL) { - fprintf(stderr, "default type %s does not exist\n", d); + LOGW("default type %s does not exist\n", d); return 1; } @@ -540,7 +518,7 @@ int add_typeattribute(const char *domainS, const char *attr) { domain = hashtab_search(policydb->p_types.table, domainS); if (domain == NULL) { - fprintf(stderr, "source type %s does not exist\n", domainS); + LOGW("source type %s does not exist\n", domainS); return 1; } @@ -572,7 +550,7 @@ int add_rule(const char *s, const char *t, const char *c, const char *p, int eff if (s) { src = hashtab_search(policydb->p_types.table, s); if (src == NULL) { - fprintf(stderr, "source type %s does not exist\n", s); + LOGW("source type %s does not exist\n", s); return 1; } } @@ -580,7 +558,7 @@ int add_rule(const char *s, const char *t, const char *c, const char *p, int eff if (t) { tgt = hashtab_search(policydb->p_types.table, t); if (tgt == NULL) { - fprintf(stderr, "target type %s does not exist\n", t); + LOGW("target type %s does not exist\n", t); return 1; } } @@ -588,26 +566,24 @@ int add_rule(const char *s, const char *t, const char *c, const char *p, int eff if (c) { cls = hashtab_search(policydb->p_classes.table, c); if (cls == NULL) { - fprintf(stderr, "class %s does not exist\n", c); + LOGW("class %s does not exist\n", c); return 1; } } if (p) { if (c == NULL) { - fprintf(stderr, "No class is specified, cannot add perm [%s] \n", p); + LOGW("No class is specified, cannot add perm [%s] \n", p); return 1; } - if (cls != NULL) { - perm = hashtab_search(cls->permissions.table, p); - if (perm == NULL && cls->comdatum != NULL) { - perm = hashtab_search(cls->comdatum->permissions.table, p); - } - if (perm == NULL) { - fprintf(stderr, "perm %s does not exist in class %s\n", p, c); - return 1; - } + perm = hashtab_search(cls->permissions.table, p); + if (perm == NULL && cls->comdatum != NULL) { + perm = hashtab_search(cls->comdatum->permissions.table, p); + } + if (perm == NULL) { + LOGW("perm %s does not exist in class %s\n", p, c); + return 1; } } return add_rule_auto(src, tgt, cls, perm, effect, n); @@ -621,7 +597,7 @@ int add_xperm_rule(const char *s, const char *t, const char *c, const char *rang if (s) { src = hashtab_search(policydb->p_types.table, s); if (src == NULL) { - fprintf(stderr, "source type %s does not exist\n", s); + LOGW("source type %s does not exist\n", s); return 1; } } @@ -629,7 +605,7 @@ int add_xperm_rule(const char *s, const char *t, const char *c, const char *rang if (t) { tgt = hashtab_search(policydb->p_types.table, t); if (tgt == NULL) { - fprintf(stderr, "target type %s does not exist\n", t); + LOGW("target type %s does not exist\n", t); return 1; } } @@ -637,7 +613,7 @@ int add_xperm_rule(const char *s, const char *t, const char *c, const char *rang if (c) { cls = hashtab_search(policydb->p_classes.table, c); if (cls == NULL) { - fprintf(stderr, "class %s does not exist\n", c); + LOGW("class %s does not exist\n", c); return 1; } } diff --git a/native/jni/misc/init.cpp b/native/jni/misc/init.cpp index 59f1509a2..85f5d8ff4 100644 --- a/native/jni/misc/init.cpp +++ b/native/jni/misc/init.cpp @@ -333,10 +333,6 @@ int main(int argc, char *argv[]) { return dump_magiskrc(argv[3], 0755); } -#ifdef MAGISK_DEBUG - log_cb.d = vprintf; -#endif - // Prevent file descriptor confusion mknod("/null", S_IFCHR | 0666, makedev(1, 3)); int null = open("/null", O_RDWR | O_CLOEXEC);