2017-07-10 16:48:14 +02:00
|
|
|
/* bootstages.c - Core bootstage operations
|
|
|
|
*
|
|
|
|
* All bootstage operations, including simple mount in post-fs,
|
|
|
|
* magisk mount in post-fs-data, various image handling, script
|
|
|
|
* execution, load modules, install Magisk Manager etc.
|
2017-04-30 19:58:52 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <sys/mount.h>
|
|
|
|
#include <sys/wait.h>
|
2019-01-20 05:59:37 +01:00
|
|
|
#include <vector>
|
|
|
|
#include <string>
|
2017-04-30 19:58:52 +02:00
|
|
|
|
2019-02-10 07:05:19 +01:00
|
|
|
#include <magisk.h>
|
|
|
|
#include <db.h>
|
|
|
|
#include <utils.h>
|
|
|
|
#include <daemon.h>
|
|
|
|
#include <resetprop.h>
|
|
|
|
#include <selinux.h>
|
2019-02-12 08:14:57 +01:00
|
|
|
#include <flags.h>
|
2017-04-30 19:58:52 +02:00
|
|
|
|
2019-01-20 05:59:37 +01:00
|
|
|
using namespace std;
|
|
|
|
|
2018-08-03 15:30:44 +02:00
|
|
|
static char buf[PATH_MAX], buf2[PATH_MAX];
|
2019-01-20 05:59:37 +01:00
|
|
|
static vector<string> module_list;
|
2018-11-16 04:55:28 +01:00
|
|
|
static bool seperate_vendor;
|
2017-04-30 19:58:52 +02:00
|
|
|
|
2019-02-12 08:14:57 +01:00
|
|
|
char *system_block, *vendor_block, *data_block;
|
2018-11-13 07:53:48 +01:00
|
|
|
|
2018-11-04 09:38:06 +01:00
|
|
|
static int bind_mount(const char *from, const char *to);
|
2018-11-16 07:15:34 +01:00
|
|
|
extern void auto_start_magiskhide();
|
2017-08-30 20:20:06 +02:00
|
|
|
|
2018-11-04 09:38:06 +01:00
|
|
|
/***************
|
|
|
|
* Magic Mount *
|
|
|
|
***************/
|
2017-04-30 19:58:52 +02:00
|
|
|
|
2019-01-26 08:41:25 +01:00
|
|
|
#ifdef MAGISK_DEBUG
|
|
|
|
#define VLOGI(tag, from, to) LOGI("%s: %s <- %s\n", tag, to, from)
|
|
|
|
#else
|
|
|
|
#define VLOGI(tag, from, to) LOGI("%s: %s\n", tag, to)
|
|
|
|
#endif
|
|
|
|
|
2017-06-11 21:29:01 +02:00
|
|
|
// Precedence: MODULE > SKEL > INTER > DUMMY
|
|
|
|
#define IS_DUMMY 0x01 /* mount from mirror */
|
|
|
|
#define IS_INTER 0x02 /* intermediate node */
|
|
|
|
#define IS_SKEL 0x04 /* mount from skeleton */
|
|
|
|
#define IS_MODULE 0x08 /* mount from module */
|
|
|
|
|
2019-01-20 05:59:37 +01:00
|
|
|
#define IS_DIR(n) ((n)->type == DT_DIR)
|
|
|
|
#define IS_LNK(n) ((n)->type == DT_LNK)
|
|
|
|
#define IS_REG(n) ((n)->type == DT_REG)
|
2017-08-11 05:04:02 +02:00
|
|
|
|
2018-11-04 09:38:06 +01:00
|
|
|
class node_entry {
|
|
|
|
public:
|
2019-01-20 23:52:19 +01:00
|
|
|
explicit node_entry(const char *, uint8_t status = 0, uint8_t type = 0);
|
2018-11-07 08:10:38 +01:00
|
|
|
~node_entry();
|
|
|
|
void create_module_tree(const char *module);
|
|
|
|
void magic_mount();
|
|
|
|
node_entry *extract(const char *name);
|
|
|
|
|
|
|
|
private:
|
2017-05-08 19:09:32 +02:00
|
|
|
const char *module; /* Only used when status & IS_MODULE */
|
2019-01-20 05:59:37 +01:00
|
|
|
const string name;
|
2017-04-30 19:58:52 +02:00
|
|
|
uint8_t type;
|
|
|
|
uint8_t status;
|
2018-11-04 09:38:06 +01:00
|
|
|
node_entry *parent;
|
2019-01-20 05:59:37 +01:00
|
|
|
vector<node_entry *> children;
|
2018-11-04 09:38:06 +01:00
|
|
|
|
2018-11-07 08:10:38 +01:00
|
|
|
node_entry(const char *, const char *, uint8_t type);
|
|
|
|
bool is_root();
|
2019-01-20 05:59:37 +01:00
|
|
|
string get_path();
|
2018-11-04 09:38:06 +01:00
|
|
|
node_entry *insert(node_entry *);
|
|
|
|
void clone_skeleton();
|
|
|
|
int get_path(char *path);
|
2017-04-30 19:58:52 +02:00
|
|
|
};
|
|
|
|
|
2019-02-16 02:45:05 +01:00
|
|
|
//char node_entry::buf[] = {};
|
|
|
|
//char node_entry::buf2[] = {};
|
|
|
|
|
2018-11-07 08:10:38 +01:00
|
|
|
node_entry::node_entry(const char *name, uint8_t status, uint8_t type)
|
|
|
|
: name(name), type(type), status(status), parent(nullptr) {}
|
2017-08-11 05:04:02 +02:00
|
|
|
|
2018-11-07 08:10:38 +01:00
|
|
|
node_entry::node_entry(const char *module, const char *name, uint8_t type)
|
|
|
|
: node_entry(name, (uint8_t) 0, type) {
|
|
|
|
this->module = module;
|
2017-08-11 05:04:02 +02:00
|
|
|
}
|
|
|
|
|
2018-11-04 09:38:06 +01:00
|
|
|
node_entry::~node_entry() {
|
|
|
|
for (auto &node : children)
|
|
|
|
delete node;
|
2017-08-11 05:04:02 +02:00
|
|
|
}
|
|
|
|
|
2018-11-07 08:10:38 +01:00
|
|
|
bool node_entry::is_root() {
|
|
|
|
return parent == nullptr;
|
|
|
|
}
|
|
|
|
|
2019-01-20 05:59:37 +01:00
|
|
|
string node_entry::get_path() {
|
2018-11-04 09:38:06 +01:00
|
|
|
get_path(buf);
|
2018-11-07 08:10:38 +01:00
|
|
|
return buf;
|
2017-08-30 20:20:06 +02:00
|
|
|
}
|
|
|
|
|
2018-11-04 09:38:06 +01:00
|
|
|
int node_entry::get_path(char *path) {
|
|
|
|
int len = 0;
|
|
|
|
if (parent)
|
|
|
|
len = parent->get_path(path);
|
2018-11-07 08:10:38 +01:00
|
|
|
len += sprintf(path + len, "/%s", name.c_str());
|
2018-11-04 09:38:06 +01:00
|
|
|
return len;
|
2018-08-03 11:09:24 +02:00
|
|
|
}
|
|
|
|
|
2018-11-04 09:38:06 +01:00
|
|
|
node_entry *node_entry::insert(node_entry *node) {
|
|
|
|
node->parent = this;
|
|
|
|
for (auto &child : children) {
|
2018-11-07 08:10:38 +01:00
|
|
|
if (child->name == node->name) {
|
2018-11-04 09:38:06 +01:00
|
|
|
if (node->status > child->status) {
|
|
|
|
// The new node has higher precedence
|
|
|
|
delete child;
|
|
|
|
child = node;
|
|
|
|
return node;
|
|
|
|
} else {
|
|
|
|
delete node;
|
|
|
|
return child;
|
|
|
|
}
|
2017-04-30 19:58:52 +02:00
|
|
|
}
|
|
|
|
}
|
2018-11-04 09:38:06 +01:00
|
|
|
children.push_back(node);
|
|
|
|
return node;
|
2017-04-30 19:58:52 +02:00
|
|
|
}
|
|
|
|
|
2018-11-04 09:38:06 +01:00
|
|
|
void node_entry::create_module_tree(const char *module) {
|
2017-04-30 19:58:52 +02:00
|
|
|
DIR *dir;
|
|
|
|
struct dirent *entry;
|
|
|
|
|
2019-01-20 05:59:37 +01:00
|
|
|
auto full_path = get_path();
|
2019-02-12 08:14:57 +01:00
|
|
|
snprintf(buf, PATH_MAX, "%s/%s%s", MODULEROOT, module, full_path.c_str());
|
2017-04-30 19:58:52 +02:00
|
|
|
|
2017-10-13 18:08:12 +02:00
|
|
|
if (!(dir = xopendir(buf)))
|
2018-11-07 08:10:38 +01:00
|
|
|
return;
|
2017-04-30 19:58:52 +02:00
|
|
|
|
|
|
|
while ((entry = xreaddir(dir))) {
|
|
|
|
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
|
|
|
|
continue;
|
|
|
|
// Create new node
|
2019-01-20 05:59:37 +01:00
|
|
|
auto node = new node_entry(module, entry->d_name, entry->d_type);
|
2018-11-07 08:10:38 +01:00
|
|
|
snprintf(buf, PATH_MAX, "%s/%s", full_path.c_str(), entry->d_name);
|
2017-06-11 21:29:01 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Clone the parent in the following condition:
|
|
|
|
* 1. File in module is a symlink
|
|
|
|
* 2. Target file do not exist
|
2018-11-04 09:38:06 +01:00
|
|
|
* 3. Target file is a symlink (exclude /system/vendor)
|
2017-07-30 14:14:12 +02:00
|
|
|
*/
|
2018-11-04 09:38:06 +01:00
|
|
|
bool clone = false;
|
2017-05-08 19:09:32 +02:00
|
|
|
if (IS_LNK(node) || access(buf, F_OK) == -1) {
|
2018-11-04 09:38:06 +01:00
|
|
|
clone = true;
|
2018-11-07 08:10:38 +01:00
|
|
|
} else if (!is_root() || node->name != "vendor") {
|
2017-06-11 21:29:01 +02:00
|
|
|
struct stat s;
|
|
|
|
xstat(buf, &s);
|
|
|
|
if (S_ISLNK(s.st_mode))
|
2018-11-04 09:38:06 +01:00
|
|
|
clone = true;
|
2017-06-11 21:29:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (clone) {
|
2018-11-04 09:38:06 +01:00
|
|
|
// Mark self as a skeleton
|
|
|
|
status |= IS_SKEL; /* This will not overwrite if parent is module */
|
2017-06-11 21:29:01 +02:00
|
|
|
node->status = IS_MODULE;
|
2017-05-08 19:09:32 +02:00
|
|
|
} else if (IS_DIR(node)) {
|
|
|
|
// Check if marked as replace
|
2019-02-12 08:14:57 +01:00
|
|
|
snprintf(buf2, PATH_MAX, "%s/%s%s/.replace", MODULEROOT, module, buf);
|
2017-05-08 19:09:32 +02:00
|
|
|
if (access(buf2, F_OK) == 0) {
|
|
|
|
// Replace everything, mark as leaf
|
2017-06-11 21:29:01 +02:00
|
|
|
node->status = IS_MODULE;
|
|
|
|
} else {
|
|
|
|
// This will be an intermediate node
|
|
|
|
node->status = IS_INTER;
|
2017-04-30 19:58:52 +02:00
|
|
|
}
|
2017-05-08 19:09:32 +02:00
|
|
|
} else if (IS_REG(node)) {
|
2018-11-04 09:38:06 +01:00
|
|
|
// This is a file, mark as leaf
|
2017-06-11 21:29:01 +02:00
|
|
|
node->status = IS_MODULE;
|
2017-05-08 19:09:32 +02:00
|
|
|
}
|
2018-11-04 09:38:06 +01:00
|
|
|
node = insert(node);
|
2017-06-13 18:55:41 +02:00
|
|
|
if (node->status & (IS_SKEL | IS_INTER)) {
|
|
|
|
// Intermediate folder, travel deeper
|
2018-11-04 09:38:06 +01:00
|
|
|
node->create_module_tree(module);
|
2017-04-30 19:58:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir(dir);
|
|
|
|
}
|
|
|
|
|
2018-11-04 09:38:06 +01:00
|
|
|
void node_entry::clone_skeleton() {
|
2017-04-30 19:58:52 +02:00
|
|
|
DIR *dir;
|
|
|
|
struct dirent *entry;
|
2018-11-04 09:38:06 +01:00
|
|
|
struct node_entry *dummy;
|
2017-04-30 19:58:52 +02:00
|
|
|
|
|
|
|
// Clone the structure
|
2019-01-20 05:59:37 +01:00
|
|
|
auto full_path = get_path();
|
2018-11-07 08:10:38 +01:00
|
|
|
snprintf(buf, PATH_MAX, "%s%s", MIRRDIR, full_path.c_str());
|
2017-10-13 18:08:12 +02:00
|
|
|
if (!(dir = xopendir(buf)))
|
2018-11-07 08:10:38 +01:00
|
|
|
return;
|
2017-04-30 19:58:52 +02:00
|
|
|
while ((entry = xreaddir(dir))) {
|
|
|
|
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
|
|
|
|
continue;
|
2017-05-08 19:09:32 +02:00
|
|
|
// Create dummy node
|
2018-11-07 08:10:38 +01:00
|
|
|
dummy = new node_entry(entry->d_name, IS_DUMMY, entry->d_type);
|
2018-11-04 09:38:06 +01:00
|
|
|
insert(dummy);
|
2017-04-30 19:58:52 +02:00
|
|
|
}
|
2017-05-01 22:55:55 +02:00
|
|
|
closedir(dir);
|
2017-04-30 19:58:52 +02:00
|
|
|
|
2018-11-04 09:38:06 +01:00
|
|
|
if (status & IS_SKEL) {
|
2018-11-07 08:10:38 +01:00
|
|
|
file_attr attr;
|
2019-01-20 05:59:37 +01:00
|
|
|
getattr(full_path.c_str(), &attr);
|
2018-11-07 08:10:38 +01:00
|
|
|
LOGI("mnt_tmpfs : %s\n", full_path.c_str());
|
2019-01-20 05:59:37 +01:00
|
|
|
xmount("tmpfs", full_path.c_str(), "tmpfs", 0, nullptr);
|
|
|
|
setattr(full_path.c_str(), &attr);
|
2017-09-26 22:35:16 +02:00
|
|
|
}
|
2017-04-30 19:58:52 +02:00
|
|
|
|
2018-11-04 09:38:06 +01:00
|
|
|
for (auto &child : children) {
|
2018-11-07 08:10:38 +01:00
|
|
|
snprintf(buf, PATH_MAX, "%s/%s", full_path.c_str(), child->name.c_str());
|
2017-06-11 21:29:01 +02:00
|
|
|
|
2017-05-08 19:09:32 +02:00
|
|
|
// Create the dummy file/directory
|
|
|
|
if (IS_DIR(child))
|
2017-06-30 17:22:51 +02:00
|
|
|
xmkdir(buf, 0755);
|
2017-05-08 19:09:32 +02:00
|
|
|
else if (IS_REG(child))
|
2017-11-09 18:51:41 +01:00
|
|
|
close(creat(buf, 0644));
|
2017-06-11 21:29:01 +02:00
|
|
|
// Links will be handled later
|
2017-05-08 19:09:32 +02:00
|
|
|
|
2018-11-07 08:10:38 +01:00
|
|
|
if (is_root() && child->name == "vendor") {
|
|
|
|
if (seperate_vendor) {
|
2017-06-11 21:29:01 +02:00
|
|
|
cp_afc(MIRRDIR "/system/vendor", "/system/vendor");
|
2019-01-26 08:41:25 +01:00
|
|
|
VLOGI("copy_link ", "/system/vendor", MIRRDIR "/system/vendor");
|
2017-06-11 21:29:01 +02:00
|
|
|
}
|
|
|
|
// Skip
|
|
|
|
continue;
|
|
|
|
} else if (child->status & IS_MODULE) {
|
2017-04-30 19:58:52 +02:00
|
|
|
// Mount from module file to dummy file
|
2019-02-12 08:14:57 +01:00
|
|
|
snprintf(buf2, PATH_MAX, "%s/%s%s/%s", MODULEROOT,
|
2018-11-07 08:10:38 +01:00
|
|
|
child->module, full_path.c_str(), child->name.c_str());
|
2017-06-11 21:29:01 +02:00
|
|
|
} else if (child->status & (IS_SKEL | IS_INTER)) {
|
2017-09-26 22:35:16 +02:00
|
|
|
// It's an intermediate folder, recursive clone
|
2018-11-04 09:38:06 +01:00
|
|
|
child->clone_skeleton();
|
2017-04-30 19:58:52 +02:00
|
|
|
continue;
|
2017-05-08 19:09:32 +02:00
|
|
|
} else if (child->status & IS_DUMMY) {
|
|
|
|
// Mount from mirror to dummy file
|
2018-11-07 08:10:38 +01:00
|
|
|
snprintf(buf2, PATH_MAX, "%s%s/%s", MIRRDIR, full_path.c_str(), child->name.c_str());
|
2017-04-30 19:58:52 +02:00
|
|
|
}
|
2017-05-08 19:09:32 +02:00
|
|
|
|
|
|
|
if (IS_LNK(child)) {
|
2017-06-11 21:29:01 +02:00
|
|
|
// Copy symlinks directly
|
|
|
|
cp_afc(buf2, buf);
|
2019-01-26 08:41:25 +01:00
|
|
|
VLOGI("copy_link ", buf2, buf);
|
2017-04-30 19:58:52 +02:00
|
|
|
} else {
|
2018-11-07 08:10:38 +01:00
|
|
|
snprintf(buf, PATH_MAX, "%s/%s", full_path.c_str(), child->name.c_str());
|
2017-04-30 19:58:52 +02:00
|
|
|
bind_mount(buf2, buf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-04 09:38:06 +01:00
|
|
|
void node_entry::magic_mount() {
|
|
|
|
if (status & IS_MODULE) {
|
|
|
|
// Mount module item
|
2019-01-20 05:59:37 +01:00
|
|
|
auto real_path = get_path();
|
2019-02-12 08:14:57 +01:00
|
|
|
snprintf(buf, PATH_MAX, "%s/%s%s", MODULEROOT, module, real_path.c_str());
|
2019-01-20 05:59:37 +01:00
|
|
|
bind_mount(buf, real_path.c_str());
|
2018-11-04 09:38:06 +01:00
|
|
|
} else if (status & IS_SKEL) {
|
2017-06-11 21:29:01 +02:00
|
|
|
// The node is labeled to be cloned with skeleton, lets do it
|
2018-11-04 09:38:06 +01:00
|
|
|
clone_skeleton();
|
|
|
|
} else if (status & IS_INTER) {
|
2017-05-08 19:09:32 +02:00
|
|
|
// It's an intermediate node, travel deeper
|
2018-11-04 09:38:06 +01:00
|
|
|
for (auto &child : children)
|
|
|
|
child->magic_mount();
|
2017-04-30 19:58:52 +02:00
|
|
|
}
|
2017-06-11 21:29:01 +02:00
|
|
|
// The only thing goes here should be vendor placeholder
|
|
|
|
// There should be no dummies, so don't need to handle it here
|
2017-04-30 19:58:52 +02:00
|
|
|
}
|
|
|
|
|
2018-11-07 08:10:38 +01:00
|
|
|
node_entry *node_entry::extract(const char *name) {
|
|
|
|
node_entry *node = nullptr;
|
|
|
|
// Extract the vendor node out of system tree and swap with placeholder
|
|
|
|
for (auto &child : children) {
|
|
|
|
if (child->name == name) {
|
|
|
|
node = child;
|
|
|
|
child = new node_entry(name);
|
|
|
|
child->parent = node->parent;
|
|
|
|
node->parent = nullptr;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2017-05-03 20:39:53 +02:00
|
|
|
/****************
|
|
|
|
* Simple Mount *
|
|
|
|
****************/
|
|
|
|
|
|
|
|
static void simple_mount(const char *path) {
|
|
|
|
DIR *dir;
|
|
|
|
struct dirent *entry;
|
|
|
|
|
2018-04-21 20:16:56 +02:00
|
|
|
snprintf(buf, PATH_MAX, "%s%s", SIMPLEMOUNT, path);
|
2017-05-03 20:39:53 +02:00
|
|
|
if (!(dir = opendir(buf)))
|
|
|
|
return;
|
|
|
|
|
|
|
|
while ((entry = xreaddir(dir))) {
|
|
|
|
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
|
|
|
|
continue;
|
|
|
|
// Target file path
|
|
|
|
snprintf(buf2, PATH_MAX, "%s/%s", path, entry->d_name);
|
|
|
|
// Only mount existing file
|
|
|
|
if (access(buf2, F_OK) == -1)
|
|
|
|
continue;
|
|
|
|
if (entry->d_type == DT_DIR) {
|
2019-01-20 05:59:37 +01:00
|
|
|
simple_mount(string(buf2).c_str());
|
2017-05-03 20:39:53 +02:00
|
|
|
} else if (entry->d_type == DT_REG) {
|
|
|
|
// Actual file path
|
2018-04-21 20:16:56 +02:00
|
|
|
snprintf(buf, PATH_MAX, "%s%s", SIMPLEMOUNT, buf2);
|
2017-05-03 20:39:53 +02:00
|
|
|
// Clone all attributes
|
2017-05-04 22:16:00 +02:00
|
|
|
clone_attr(buf2, buf);
|
2017-05-03 20:39:53 +02:00
|
|
|
// Finally, mount the file
|
|
|
|
bind_mount(buf, buf2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
closedir(dir);
|
|
|
|
}
|
|
|
|
|
2017-08-11 05:43:45 +02:00
|
|
|
/*****************
|
|
|
|
* Miscellaneous *
|
|
|
|
*****************/
|
|
|
|
|
2018-11-04 09:38:06 +01:00
|
|
|
static int bind_mount(const char *from, const char *to) {
|
|
|
|
int ret = xmount(from, to, nullptr, MS_BIND, nullptr);
|
2019-01-26 08:41:25 +01:00
|
|
|
VLOGI("bind_mount", from, to);
|
2018-11-04 09:38:06 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-11-13 07:53:48 +01:00
|
|
|
static bool magisk_env() {
|
|
|
|
LOGI("* Initializing Magisk environment\n");
|
2017-12-25 20:23:58 +01:00
|
|
|
|
2018-11-13 07:53:48 +01:00
|
|
|
// Alternative binaries paths
|
2019-02-11 23:14:07 +01:00
|
|
|
const char *alt_bin[] = { "/cache/data_adb/magisk", "/data/magisk",
|
2018-11-13 07:53:48 +01:00
|
|
|
"/data/data/com.topjohnwu.magisk/install",
|
|
|
|
"/data/user_de/0/com.topjohnwu.magisk/install" };
|
|
|
|
for (auto &alt : alt_bin) {
|
|
|
|
struct stat st;
|
|
|
|
if (lstat(alt, &st) != -1) {
|
|
|
|
if (S_ISLNK(st.st_mode)) {
|
|
|
|
unlink(alt);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
rm_rf(DATABIN);;
|
|
|
|
cp_afc(alt, DATABIN);
|
|
|
|
rm_rf(alt);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove legacy stuffs
|
|
|
|
unlink("/data/magisk.img");
|
|
|
|
unlink("/data/magisk_debug.log");
|
|
|
|
|
2019-02-12 08:14:57 +01:00
|
|
|
// Legacy support
|
2018-11-15 07:36:03 +01:00
|
|
|
symlink(MAGISKTMP, "/sbin/.core");
|
2019-02-12 08:14:57 +01:00
|
|
|
xmkdir(MAGISKTMP "/img", 0755);
|
2018-11-15 07:36:03 +01:00
|
|
|
|
2018-11-13 07:53:48 +01:00
|
|
|
// Create directories in tmpfs overlay
|
|
|
|
xmkdirs(MIRRDIR "/system", 0755);
|
|
|
|
xmkdir(MIRRDIR "/bin", 0755);
|
|
|
|
xmkdir(BBPATH, 0755);
|
|
|
|
xmkdir(BLOCKDIR, 0755);
|
|
|
|
|
2019-02-12 08:14:57 +01:00
|
|
|
// /data/adb directories
|
|
|
|
xmkdir(MODULEROOT, 0755);
|
2018-11-16 04:55:28 +01:00
|
|
|
xmkdir(SECURE_DIR "/post-fs-data.d", 0755);
|
|
|
|
xmkdir(SECURE_DIR "/service.d", 0755);
|
|
|
|
|
2018-11-13 07:53:48 +01:00
|
|
|
LOGI("* Mounting mirrors");
|
|
|
|
bool system_as_root = false;
|
2019-02-18 04:30:23 +01:00
|
|
|
file_readline("/proc/mounts", [&](string_view &line) -> bool {
|
2019-01-20 21:20:34 +01:00
|
|
|
if (str_contains(line, " /system_root ")) {
|
2018-11-13 07:53:48 +01:00
|
|
|
bind_mount("/system_root/system", MIRRDIR "/system");
|
2019-02-18 04:30:23 +01:00
|
|
|
sscanf(line.data(), "%s", buf);
|
2019-01-26 19:00:19 +01:00
|
|
|
system_block = strdup(buf);
|
2018-11-13 07:53:48 +01:00
|
|
|
system_as_root = true;
|
2019-01-20 21:20:34 +01:00
|
|
|
} else if (!system_as_root && str_contains(line, " /system ")) {
|
2019-02-18 04:30:23 +01:00
|
|
|
sscanf(line.data(), "%s %*s %s", buf, buf2);
|
2019-01-26 19:00:19 +01:00
|
|
|
system_block = strdup(buf);
|
2018-11-13 07:53:48 +01:00
|
|
|
xmount(system_block, MIRRDIR "/system", buf2, MS_RDONLY, nullptr);
|
2019-01-26 08:41:25 +01:00
|
|
|
VLOGI("mount", system_block, MIRRDIR "/system");
|
2019-01-20 21:20:34 +01:00
|
|
|
} else if (str_contains(line, " /vendor ")) {
|
2018-11-16 04:55:28 +01:00
|
|
|
seperate_vendor = true;
|
2019-02-18 04:30:23 +01:00
|
|
|
sscanf(line.data(), "%s %*s %s", buf, buf2);
|
2019-01-26 19:00:19 +01:00
|
|
|
vendor_block = strdup(buf);
|
2018-11-13 07:53:48 +01:00
|
|
|
xmkdir(MIRRDIR "/vendor", 0755);
|
2019-01-26 08:41:25 +01:00
|
|
|
xmount(vendor_block, MIRRDIR "/vendor", buf2, MS_RDONLY, nullptr);
|
2019-02-03 08:45:49 +01:00
|
|
|
VLOGI("mount", vendor_block, MIRRDIR "/vendor");
|
2019-02-12 08:14:57 +01:00
|
|
|
} else if (str_contains(line, " /data ")) {
|
2019-02-18 04:30:23 +01:00
|
|
|
sscanf(line.data(), "%s", buf);
|
2019-02-12 08:14:57 +01:00
|
|
|
data_block = strdup(buf);
|
2019-01-20 21:20:34 +01:00
|
|
|
} else if (SDK_INT >= 24 &&
|
|
|
|
str_contains(line, " /proc ") && !str_contains(line, "hidepid=2")) {
|
2018-11-28 07:27:32 +01:00
|
|
|
// Enforce hidepid
|
|
|
|
xmount(nullptr, "/proc", nullptr, MS_REMOUNT, "hidepid=2,gid=3009");
|
2018-11-13 07:53:48 +01:00
|
|
|
}
|
2019-02-18 04:30:23 +01:00
|
|
|
return true;
|
|
|
|
});
|
2018-11-13 07:53:48 +01:00
|
|
|
if (!seperate_vendor) {
|
|
|
|
xsymlink(MIRRDIR "/system/vendor", MIRRDIR "/vendor");
|
2019-01-26 08:41:25 +01:00
|
|
|
VLOGI("link", MIRRDIR "/system/vendor", MIRRDIR "/vendor");
|
2018-11-13 07:53:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
xmkdirs(DATABIN, 0755);
|
|
|
|
bind_mount(DATABIN, MIRRDIR "/bin");
|
|
|
|
if (access(MIRRDIR "/bin/busybox", X_OK) == -1)
|
|
|
|
return false;
|
|
|
|
LOGI("* Setting up internal busybox");
|
2019-01-26 19:39:24 +01:00
|
|
|
exec_command_sync(MIRRDIR "/bin/busybox", "--install", "-s", BBPATH);
|
2018-11-13 07:53:48 +01:00
|
|
|
xsymlink(MIRRDIR "/bin/busybox", BBPATH "/busybox");
|
2019-01-20 23:52:19 +01:00
|
|
|
|
|
|
|
// Disable/remove magiskhide, resetprop, and modules
|
|
|
|
if (SDK_INT < 19) {
|
|
|
|
close(xopen(DISABLEFILE, O_RDONLY | O_CREAT, 0));
|
|
|
|
unlink("/sbin/resetprop");
|
|
|
|
unlink("/sbin/magiskhide");
|
|
|
|
}
|
2018-11-16 04:55:28 +01:00
|
|
|
return true;
|
|
|
|
}
|
2018-11-13 07:53:48 +01:00
|
|
|
|
2019-02-12 08:14:57 +01:00
|
|
|
static void upgrade_modules() {
|
2019-02-12 22:13:31 +01:00
|
|
|
const char *legacy_imgs[] = {SECURE_DIR "/magisk.img", SECURE_DIR "/magisk_merge.img"};
|
|
|
|
for (auto img : legacy_imgs) {
|
2019-02-16 02:45:05 +01:00
|
|
|
if (access(img, F_OK) == 0)
|
|
|
|
migrate_img(img);
|
2019-02-12 22:13:31 +01:00
|
|
|
}
|
2019-02-12 08:14:57 +01:00
|
|
|
DIR *dir;
|
|
|
|
struct dirent *entry;
|
|
|
|
if ((dir = opendir(MODULEUPGRADE))) {
|
|
|
|
while ((entry = xreaddir(dir))) {
|
|
|
|
if (entry->d_type == DT_DIR) {
|
|
|
|
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
|
|
|
|
continue;
|
|
|
|
// Cleanup old module if exists
|
|
|
|
snprintf(buf, sizeof(buf), "%s/%s", MODULEROOT, entry->d_name);
|
|
|
|
if (access(buf, F_OK) == 0)
|
|
|
|
rm_rf(buf);
|
|
|
|
LOGI("Upgrade / New module: %s\n", entry->d_name);
|
|
|
|
snprintf(buf2, sizeof(buf2), "%s/%s", MODULEUPGRADE, entry->d_name);
|
|
|
|
rename(buf2, buf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir(dir);
|
|
|
|
rm_rf(MODULEUPGRADE);
|
|
|
|
}
|
|
|
|
// Legacy support
|
|
|
|
bind_mount(MODULEROOT, MAGISKTMP "/img");
|
|
|
|
xmkdir(LEGACYCORE, 0755);
|
|
|
|
symlink(SECURE_DIR "/post-fs-data.d", LEGACYCORE "/post-fs-data.d");
|
|
|
|
symlink(SECURE_DIR "/service.d", LEGACYCORE "/service.d");
|
|
|
|
}
|
|
|
|
|
2018-12-05 18:47:29 +01:00
|
|
|
static void collect_modules() {
|
2019-02-12 08:14:57 +01:00
|
|
|
chdir(MODULEROOT);
|
|
|
|
rm_rf("lost+found");
|
2018-12-05 18:47:29 +01:00
|
|
|
DIR *dir = xopendir(".");
|
|
|
|
struct dirent *entry;
|
|
|
|
while ((entry = xreaddir(dir))) {
|
|
|
|
if (entry->d_type == DT_DIR) {
|
|
|
|
if (strcmp(entry->d_name, ".") == 0 ||
|
|
|
|
strcmp(entry->d_name, "..") == 0 ||
|
2019-02-12 08:14:57 +01:00
|
|
|
strcmp(entry->d_name, ".core") == 0)
|
2018-12-05 18:47:29 +01:00
|
|
|
continue;
|
|
|
|
chdir(entry->d_name);
|
|
|
|
if (access("remove", F_OK) == 0) {
|
|
|
|
chdir("..");
|
|
|
|
LOGI("%s: remove\n", entry->d_name);
|
|
|
|
rm_rf(entry->d_name);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
unlink("update");
|
|
|
|
if (access("disable", F_OK))
|
2019-01-20 05:59:37 +01:00
|
|
|
module_list.emplace_back(entry->d_name);
|
2018-12-05 18:47:29 +01:00
|
|
|
chdir("..");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir(dir);
|
|
|
|
chdir("/");
|
|
|
|
}
|
|
|
|
|
2018-11-04 09:38:06 +01:00
|
|
|
static bool check_data() {
|
|
|
|
bool mnt = false;
|
|
|
|
bool data = false;
|
2019-02-18 04:30:23 +01:00
|
|
|
file_readline("/proc/mounts", [&](string_view &s) -> bool {
|
|
|
|
if (str_contains(s, " /data ") && !str_contains(s, "tmpfs"))
|
2018-11-04 09:38:06 +01:00
|
|
|
mnt = true;
|
2019-02-18 04:30:23 +01:00
|
|
|
return true;
|
|
|
|
});
|
2018-10-13 03:46:09 +02:00
|
|
|
if (mnt) {
|
2019-01-20 05:59:37 +01:00
|
|
|
auto crypto = getprop("ro.crypto.state");
|
2018-11-07 08:10:38 +01:00
|
|
|
if (!crypto.empty()) {
|
|
|
|
if (crypto == "unencrypted") {
|
2018-10-13 03:46:09 +02:00
|
|
|
// Unencrypted, we can directly access data
|
2018-11-04 09:38:06 +01:00
|
|
|
data = true;
|
2018-10-13 03:46:09 +02:00
|
|
|
} else {
|
|
|
|
// Encrypted, check whether vold is started
|
2018-11-07 08:10:38 +01:00
|
|
|
data = !getprop("init.svc.vold").empty();
|
2018-10-13 03:46:09 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// ro.crypto.state is not set, assume it's unencrypted
|
2018-11-04 09:38:06 +01:00
|
|
|
data = true;
|
2018-10-13 03:46:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
void unlock_blocks() {
|
|
|
|
DIR *dir;
|
|
|
|
struct dirent *entry;
|
|
|
|
int fd, dev, OFF = 0;
|
|
|
|
|
|
|
|
if ((dev = xopen("/dev/block", O_RDONLY | O_CLOEXEC)) < 0)
|
|
|
|
return;
|
|
|
|
dir = xfdopendir(dev);
|
|
|
|
|
|
|
|
while((entry = readdir(dir))) {
|
|
|
|
if (entry->d_type == DT_BLK) {
|
|
|
|
if ((fd = openat(dev, entry->d_name, O_RDONLY)) < 0)
|
|
|
|
continue;
|
|
|
|
if (ioctl(fd, BLKROSET, &OFF) == -1)
|
|
|
|
PLOGE("unlock %s", entry->d_name);
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close(dev);
|
|
|
|
}
|
|
|
|
|
2019-02-14 10:27:30 +01:00
|
|
|
static bool log_dump = false;
|
|
|
|
static void dump_logs() {
|
|
|
|
if (log_dump)
|
|
|
|
return;
|
|
|
|
int test = exec_command_sync("/system/bin/logcat", "-d", "-f", "/dev/null");
|
|
|
|
chmod("/dev/null", 0666);
|
|
|
|
if (test != 0)
|
|
|
|
return;
|
|
|
|
rename(LOGFILE, LOGFILE ".bak");
|
|
|
|
log_dump = true;
|
2019-02-14 23:36:18 +01:00
|
|
|
// Start a daemon thread and wait indefinitely
|
|
|
|
new_daemon_thread([](auto) -> void* {
|
|
|
|
int fd = xopen(LOGFILE, O_WRONLY | O_APPEND | O_CREAT | O_CLOEXEC, 0644);
|
|
|
|
exec_t exec {
|
|
|
|
.fd = fd,
|
|
|
|
.fork = fork_no_zombie
|
|
|
|
};
|
|
|
|
int pid = exec_command(exec, "/system/bin/logcat", "-s", "Magisk");
|
|
|
|
close(fd);
|
|
|
|
if (pid < 0) {
|
|
|
|
log_dump = false;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
waitpid(pid, nullptr, 0);
|
|
|
|
return nullptr;
|
|
|
|
});
|
2019-02-14 10:27:30 +01:00
|
|
|
}
|
|
|
|
|
2017-04-30 19:58:52 +02:00
|
|
|
/****************
|
|
|
|
* Entry points *
|
|
|
|
****************/
|
|
|
|
|
2018-11-16 04:55:28 +01:00
|
|
|
[[noreturn]] static void unblock_boot_process() {
|
2017-11-05 22:41:03 +01:00
|
|
|
close(xopen(UNBLOCKFILE, O_RDONLY | O_CREAT, 0));
|
2018-11-04 09:38:06 +01:00
|
|
|
pthread_exit(nullptr);
|
2017-05-03 19:13:04 +02:00
|
|
|
}
|
|
|
|
|
2019-02-09 07:51:46 +01:00
|
|
|
[[noreturn]] static inline void core_only() {
|
|
|
|
auto_start_magiskhide();
|
|
|
|
unblock_boot_process();
|
|
|
|
}
|
|
|
|
|
|
|
|
void post_fs_data(int client) {
|
|
|
|
// ack
|
|
|
|
write_int(client, 0);
|
|
|
|
close(client);
|
|
|
|
|
2018-04-21 20:16:56 +02:00
|
|
|
if (!check_data())
|
2018-06-10 20:26:18 +02:00
|
|
|
unblock_boot_process();
|
|
|
|
|
2019-02-14 10:27:30 +01:00
|
|
|
dump_logs();
|
|
|
|
|
2019-02-09 07:51:46 +01:00
|
|
|
LOGI("** post-fs-data mode running\n");
|
|
|
|
|
|
|
|
// Unlock all blocks for rw
|
|
|
|
unlock_blocks();
|
|
|
|
|
2018-06-10 20:26:18 +02:00
|
|
|
if (access(SECURE_DIR, F_OK) != 0) {
|
|
|
|
/* If the folder is not automatically created by the system,
|
|
|
|
* do NOT proceed further. Manual creation of the folder
|
|
|
|
* will cause bootloops on FBE devices. */
|
2018-11-16 04:55:28 +01:00
|
|
|
LOGE(SECURE_DIR " is not present, abort...");
|
2018-06-10 20:26:18 +02:00
|
|
|
unblock_boot_process();
|
|
|
|
}
|
2017-04-30 19:58:52 +02:00
|
|
|
|
2018-08-31 09:23:59 +02:00
|
|
|
#if 0
|
2018-08-06 22:41:48 +02:00
|
|
|
// Increment boot count
|
|
|
|
int boot_count = 0;
|
2018-08-09 09:10:00 +02:00
|
|
|
FILE *cf = fopen(BOOTCOUNT, "r");
|
2018-08-06 22:41:48 +02:00
|
|
|
if (cf) {
|
|
|
|
fscanf(cf, "%d", &boot_count);
|
|
|
|
fclose(cf);
|
|
|
|
}
|
|
|
|
boot_count++;
|
|
|
|
if (boot_count > 2)
|
|
|
|
creat(DISABLEFILE, 0644);
|
|
|
|
cf = xfopen(BOOTCOUNT, "w");
|
|
|
|
fprintf(cf, "%d", boot_count);
|
|
|
|
fclose(cf);
|
2018-08-31 09:23:59 +02:00
|
|
|
#endif
|
2018-08-06 22:41:48 +02:00
|
|
|
|
2018-06-10 20:26:18 +02:00
|
|
|
// No uninstaller or core-only mode
|
2018-06-27 00:04:16 +02:00
|
|
|
if (access(DISABLEFILE, F_OK) != 0) {
|
2018-06-10 20:26:18 +02:00
|
|
|
simple_mount("/system");
|
|
|
|
simple_mount("/vendor");
|
|
|
|
}
|
2017-05-03 20:39:53 +02:00
|
|
|
|
2018-11-13 07:53:48 +01:00
|
|
|
if (!magisk_env()) {
|
2018-11-16 04:55:28 +01:00
|
|
|
LOGE("* Magisk environment setup incomplete, abort\n");
|
|
|
|
unblock_boot_process();
|
2018-11-04 09:38:06 +01:00
|
|
|
}
|
2017-04-30 19:58:52 +02:00
|
|
|
|
2019-01-20 23:01:59 +01:00
|
|
|
LOGI("* Running post-fs-data.d scripts\n");
|
|
|
|
exec_common_script("post-fs-data");
|
|
|
|
|
2019-02-12 08:14:57 +01:00
|
|
|
upgrade_modules();
|
|
|
|
|
2019-01-20 23:01:59 +01:00
|
|
|
// Core only mode
|
|
|
|
if (access(DISABLEFILE, F_OK) == 0)
|
|
|
|
core_only();
|
|
|
|
|
2018-06-03 08:43:03 +02:00
|
|
|
restorecon();
|
2018-06-10 20:26:18 +02:00
|
|
|
chmod(SECURE_DIR, 0700);
|
2018-06-03 08:43:03 +02:00
|
|
|
|
2019-02-12 08:14:57 +01:00
|
|
|
collect_modules();
|
|
|
|
|
2017-08-12 19:15:18 +02:00
|
|
|
// Execute module scripts
|
|
|
|
LOGI("* Running module post-fs-data scripts\n");
|
2019-02-16 02:45:05 +01:00
|
|
|
exec_module_script("post-fs-data", module_list);
|
2017-08-12 19:15:18 +02:00
|
|
|
|
2018-12-05 18:47:29 +01:00
|
|
|
// Recollect modules
|
2019-01-20 05:59:37 +01:00
|
|
|
module_list.clear();
|
2018-12-05 18:47:29 +01:00
|
|
|
collect_modules();
|
|
|
|
|
2017-04-30 19:58:52 +02:00
|
|
|
// Create the system root entry
|
2019-01-20 05:59:37 +01:00
|
|
|
auto sys_root = new node_entry("system", IS_INTER);
|
2017-04-30 19:58:52 +02:00
|
|
|
|
2018-11-04 09:38:06 +01:00
|
|
|
// Vendor root entry
|
|
|
|
node_entry *ven_root = nullptr;
|
|
|
|
|
|
|
|
bool has_modules = false;
|
2017-04-30 19:58:52 +02:00
|
|
|
|
|
|
|
LOGI("* Loading modules\n");
|
2019-01-20 05:59:37 +01:00
|
|
|
for (const auto &m : module_list) {
|
|
|
|
const auto module = m.c_str();
|
2017-08-12 19:15:18 +02:00
|
|
|
// Read props
|
2019-02-12 08:14:57 +01:00
|
|
|
snprintf(buf, PATH_MAX, "%s/%s/system.prop", MODULEROOT, module);
|
2017-08-12 19:15:18 +02:00
|
|
|
if (access(buf, F_OK) == 0) {
|
|
|
|
LOGI("%s: loading [system.prop]\n", module);
|
2019-01-20 21:20:34 +01:00
|
|
|
load_prop_file(buf, false);
|
2017-08-12 19:15:18 +02:00
|
|
|
}
|
|
|
|
// Check whether enable auto_mount
|
2019-02-12 08:14:57 +01:00
|
|
|
snprintf(buf, PATH_MAX, "%s/%s/auto_mount", MODULEROOT, module);
|
2017-08-12 19:15:18 +02:00
|
|
|
if (access(buf, F_OK) == -1)
|
|
|
|
continue;
|
|
|
|
// Double check whether the system folder exists
|
2019-02-12 08:14:57 +01:00
|
|
|
snprintf(buf, PATH_MAX, "%s/%s/system", MODULEROOT, module);
|
2017-08-12 19:15:18 +02:00
|
|
|
if (access(buf, F_OK) == -1)
|
|
|
|
continue;
|
2017-05-03 19:13:04 +02:00
|
|
|
|
2017-08-12 19:15:18 +02:00
|
|
|
// Construct structure
|
2018-11-04 09:38:06 +01:00
|
|
|
has_modules = true;
|
2017-08-12 19:15:18 +02:00
|
|
|
LOGI("%s: constructing magic mount structure\n", module);
|
|
|
|
// If /system/vendor exists in module, create a link outside
|
2019-02-12 08:14:57 +01:00
|
|
|
snprintf(buf, PATH_MAX, "%s/%s/system/vendor", MODULEROOT, module);
|
2017-08-12 19:15:18 +02:00
|
|
|
if (access(buf, F_OK) == 0) {
|
2019-02-12 08:14:57 +01:00
|
|
|
snprintf(buf2, PATH_MAX, "%s/%s/vendor", MODULEROOT, module);
|
2017-08-12 19:15:18 +02:00
|
|
|
unlink(buf2);
|
2017-10-13 18:08:12 +02:00
|
|
|
xsymlink(buf, buf2);
|
2017-04-30 19:58:52 +02:00
|
|
|
}
|
2018-11-04 09:38:06 +01:00
|
|
|
sys_root->create_module_tree(module);
|
2017-04-30 19:58:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (has_modules) {
|
2018-11-07 08:10:38 +01:00
|
|
|
// Pull out /system/vendor node if exist
|
|
|
|
ven_root = sys_root->extract("vendor");
|
2017-06-11 21:29:01 +02:00
|
|
|
|
|
|
|
// Magic!!
|
2018-11-04 09:38:06 +01:00
|
|
|
sys_root->magic_mount();
|
|
|
|
if (ven_root) ven_root->magic_mount();
|
2017-04-30 19:58:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Cleanup memory
|
2018-11-04 09:38:06 +01:00
|
|
|
delete sys_root;
|
2018-11-07 08:10:38 +01:00
|
|
|
delete ven_root;
|
2017-05-03 21:05:37 +02:00
|
|
|
|
2018-11-04 09:38:06 +01:00
|
|
|
core_only();
|
2017-04-30 19:58:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void late_start(int client) {
|
|
|
|
LOGI("** late_start service mode running\n");
|
|
|
|
// ack
|
|
|
|
write_int(client, 0);
|
|
|
|
close(client);
|
|
|
|
|
2019-02-14 10:27:30 +01:00
|
|
|
dump_logs();
|
|
|
|
|
2018-06-10 20:26:18 +02:00
|
|
|
if (access(SECURE_DIR, F_OK) != 0) {
|
|
|
|
// It's safe to create the folder at this point if the system didn't create it
|
|
|
|
xmkdir(SECURE_DIR, 0700);
|
2018-11-16 04:55:28 +01:00
|
|
|
// And reboot to make proper setup possible
|
2019-01-26 19:39:24 +01:00
|
|
|
exec_command_sync("/system/bin/reboot");
|
2018-06-10 20:26:18 +02:00
|
|
|
}
|
|
|
|
|
2018-08-02 19:58:56 +02:00
|
|
|
auto_start_magiskhide();
|
|
|
|
|
2017-08-12 19:15:18 +02:00
|
|
|
// Run scripts after full patch, most reliable way to run scripts
|
|
|
|
LOGI("* Running service.d scripts\n");
|
|
|
|
exec_common_script("service");
|
|
|
|
|
2017-06-11 10:51:44 +02:00
|
|
|
// Core only mode
|
2017-07-31 17:31:40 +02:00
|
|
|
if (access(DISABLEFILE, F_OK) == 0)
|
|
|
|
goto core_only;
|
2017-06-11 10:51:44 +02:00
|
|
|
|
2017-04-30 19:58:52 +02:00
|
|
|
LOGI("* Running module service scripts\n");
|
2019-02-16 02:45:05 +01:00
|
|
|
exec_module_script("service", module_list);
|
2017-04-30 19:58:52 +02:00
|
|
|
|
2017-07-31 17:31:40 +02:00
|
|
|
core_only:
|
2017-05-08 19:16:58 +02:00
|
|
|
if (access(MANAGERAPK, F_OK) == 0) {
|
2018-06-13 23:09:54 +02:00
|
|
|
// Install Magisk Manager if exists
|
2018-01-11 13:57:05 +01:00
|
|
|
rename(MANAGERAPK, "/data/magisk.apk");
|
2018-06-13 23:09:54 +02:00
|
|
|
install_apk("/data/magisk.apk");
|
|
|
|
} else {
|
|
|
|
// Check whether we have a valid manager installed
|
2018-11-16 09:20:30 +01:00
|
|
|
db_strings str;
|
|
|
|
get_db_strings(&str, SU_MANAGER);
|
|
|
|
if (validate_manager(str[SU_MANAGER], 0, nullptr)) {
|
|
|
|
// There is no manager installed, install the stub
|
2019-01-26 19:39:24 +01:00
|
|
|
exec_command_sync("/sbin/magiskinit", "-x", "manager", "/data/magisk.apk");
|
2018-11-16 09:20:30 +01:00
|
|
|
install_apk("/data/magisk.apk");
|
2017-05-08 19:16:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-03 15:30:44 +02:00
|
|
|
// All boot stage done, cleanup
|
2019-01-20 05:59:37 +01:00
|
|
|
module_list.clear();
|
2018-08-09 08:52:44 +02:00
|
|
|
}
|
2018-08-06 22:41:48 +02:00
|
|
|
|
2018-08-09 08:52:44 +02:00
|
|
|
void boot_complete(int client) {
|
|
|
|
LOGI("** boot_complete triggered\n");
|
|
|
|
// ack
|
|
|
|
write_int(client, 0);
|
|
|
|
close(client);
|
|
|
|
|
|
|
|
unlink(BOOTCOUNT);
|
2017-04-30 19:58:52 +02:00
|
|
|
}
|