diff --git a/app b/app index 499a15794..e6c1dd532 160000 --- a/app +++ b/app @@ -1 +1 @@ -Subproject commit 499a157946f0063aea0795b204a1f2858591d8d3 +Subproject commit e6c1dd532d2910285d503f271183839446495360 diff --git a/native/jni/core/bootstages.c b/native/jni/core/bootstages.c index 7b52a5469..986156984 100644 --- a/native/jni/core/bootstages.c +++ b/native/jni/core/bootstages.c @@ -16,6 +16,7 @@ #include #include "magisk.h" +#include "db.h" #include "utils.h" #include "daemon.h" #include "resetprop.h" @@ -454,6 +455,30 @@ static int prepare_img() { return 0; } +void install_apk(const char *apk) { + setfilecon(apk, "u:object_r:"SEPOL_FILE_DOMAIN":s0"); + while (1) { + sleep(5); + LOGD("apk_install: attempting to install APK"); + int apk_res = -1, pid; + pid = exec_command(1, &apk_res, NULL, "/system/bin/pm", "install", "-r", apk, NULL); + if (pid != -1) { + int err = 0; + while (fdgets(buf, PATH_MAX, apk_res) > 0) { + LOGD("apk_install: %s", buf); + err |= strstr(buf, "Error:") != NULL; + } + waitpid(pid, NULL, 0); + close(apk_res); + // Keep trying until pm is started + if (err) + continue; + break; + } + } + unlink(apk); +} + /**************** * Entry points * ****************/ @@ -806,31 +831,21 @@ void late_start(int client) { exec_module_script("service"); core_only: - // Install Magisk Manager if exists if (access(MANAGERAPK, F_OK) == 0) { + // Install Magisk Manager if exists rename(MANAGERAPK, "/data/magisk.apk"); - setfilecon("/data/magisk.apk", "u:object_r:"SEPOL_FILE_DOMAIN":s0"); - while (1) { - sleep(5); - LOGD("apk_install: attempting to install APK"); - int apk_res = -1, pid; - pid = exec_command(1, &apk_res, NULL, - "/system/bin/pm", "install", "-r", "/data/magisk.apk", NULL); - if (pid != -1) { - int err = 0; - while (fdgets(buf, PATH_MAX, apk_res) > 0) { - LOGD("apk_install: %s", buf); - err |= strstr(buf, "Error:") != NULL; - } - waitpid(pid, NULL, 0); - close(apk_res); - // Keep trying until pm is started - if (err) - continue; - break; - } + install_apk("/data/magisk.apk"); + } else { + // Check whether we have a valid manager installed + sqlite3 *db = get_magiskdb(); + struct db_strings str; + INIT_DB_STRINGS(&str); + get_db_strings(db, SU_MANAGER, &str); + if (validate_manager(str.s[SU_MANAGER], 0, NULL)) { + // There is no manager installed, install the stub + exec_command_sync("/sbin/magiskinit", "-x", "manager", "/data/magisk.apk", NULL); + install_apk("/data/magisk.apk"); } - unlink("/data/magisk.apk"); } // All boot stage done, cleanup everything diff --git a/native/jni/core/db.c b/native/jni/core/db.c index d9d69afe8..92ca42009 100644 --- a/native/jni/core/db.c +++ b/native/jni/core/db.c @@ -23,7 +23,7 @@ static int policy_cb(void *v, int col_num, char **data, char **col_name) { else if (strcmp(col_name[i], "notification") == 0) su->notify = atoi(data[i]); } - LOGD("su_db: query policy=[%d] log=[%d] notify=[%d]\n", su->policy, su->log, su->notify); + LOGD("magiskdb: query policy=[%d] log=[%d] notify=[%d]\n", su->policy, su->log, su->notify); return 0; } @@ -42,7 +42,7 @@ static int settings_cb(void *v, int col_num, char **data, char **col_name) { } if (key >= 0) { dbs->v[key] = value; - LOGD("su_db: query %s=[%d]\n", DB_SETTING_KEYS[key], value); + LOGD("magiskdb: query %s=[%d]\n", DB_SETTING_KEYS[key], value); } return 0; } @@ -63,7 +63,7 @@ static int strings_cb(void *v, int col_num, char **data, char **col_name) { } if (key >= 0) { strcpy(dbs->s[key], value); - LOGD("su_db: query %s=[%s]\n", DB_STRING_KEYS[key], value); + LOGD("magiskdb: query %s=[%s]\n", DB_STRING_KEYS[key], value); } return 0; } @@ -83,6 +83,8 @@ sqlite3 *get_magiskdb() { } int get_db_settings(sqlite3 *db, int key, struct db_settings *dbs) { + if (db == NULL) + return 1; char *err; if (key > 0) { char query[128]; @@ -99,6 +101,8 @@ int get_db_settings(sqlite3 *db, int key, struct db_settings *dbs) { } int get_db_strings(sqlite3 *db, int key, struct db_strings *str) { + if (db == NULL) + return 1; char *err; if (key > 0) { char query[128]; @@ -115,6 +119,8 @@ int get_db_strings(sqlite3 *db, int key, struct db_strings *str) { } int get_uid_policy(sqlite3 *db, int uid, struct su_access *su) { + if (db == NULL) + return 1; char query[256], *err; sprintf(query, "SELECT policy, logging, notification FROM policies " "WHERE uid=%d AND (until=0 OR until>%li)", uid, time(NULL)); diff --git a/native/jni/include/db.h b/native/jni/include/db.h index f0c1483a5..0b56c980e 100644 --- a/native/jni/include/db.h +++ b/native/jni/include/db.h @@ -2,6 +2,7 @@ #define DB_H #include +#include /*************** * DB Settings *