Install stub APK when needed

This commit is contained in:
topjohnwu 2018-06-14 05:09:54 +08:00
parent 970a2e87b3
commit d7d76f54cc
4 changed files with 48 additions and 26 deletions

2
app

@ -1 +1 @@
Subproject commit 499a157946f0063aea0795b204a1f2858591d8d3
Subproject commit e6c1dd532d2910285d503f271183839446495360

View File

@ -16,6 +16,7 @@
#include <selinux/selinux.h>
#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

View File

@ -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));

View File

@ -2,6 +2,7 @@
#define DB_H
#include <sqlite3.h>
#include <sys/stat.h>
/***************
* DB Settings *