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 <linux/loop.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/mount.h>
|
|
|
|
#include <sys/wait.h>
|
2017-05-03 20:39:53 +02:00
|
|
|
#include <selinux/selinux.h>
|
2017-04-30 19:58:52 +02:00
|
|
|
|
|
|
|
#include "magisk.h"
|
|
|
|
#include "utils.h"
|
|
|
|
#include "daemon.h"
|
|
|
|
#include "resetprop.h"
|
|
|
|
|
|
|
|
static char *buf, *buf2;
|
|
|
|
static struct vector module_list;
|
2017-08-11 05:43:45 +02:00
|
|
|
static int seperate_vendor = 0;
|
2017-04-30 19:58:52 +02:00
|
|
|
|
2017-08-30 20:20:06 +02:00
|
|
|
extern char **environ;
|
|
|
|
|
2017-04-30 19:58:52 +02:00
|
|
|
/******************
|
|
|
|
* Node structure *
|
|
|
|
******************/
|
|
|
|
|
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 */
|
|
|
|
|
2017-08-11 05:04:02 +02: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-04-30 19:58:52 +02:00
|
|
|
struct node_entry {
|
2017-05-08 19:09:32 +02:00
|
|
|
const char *module; /* Only used when status & IS_MODULE */
|
2017-04-30 19:58:52 +02:00
|
|
|
char *name;
|
|
|
|
uint8_t type;
|
|
|
|
uint8_t status;
|
|
|
|
struct node_entry *parent;
|
|
|
|
struct vector *children;
|
|
|
|
};
|
|
|
|
|
2017-08-11 05:04:02 +02:00
|
|
|
static void concat_path(struct node_entry *node) {
|
|
|
|
if (node->parent)
|
|
|
|
concat_path(node->parent);
|
|
|
|
int len = strlen(buf);
|
|
|
|
buf[len] = '/';
|
|
|
|
strcpy(buf + len + 1, node->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *get_full_path(struct node_entry *node) {
|
|
|
|
buf[0] = '\0';
|
|
|
|
concat_path(node);
|
|
|
|
return strdup(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Free the node
|
|
|
|
static void destroy_node(struct node_entry *node) {
|
|
|
|
free(node->name);
|
|
|
|
vec_destroy(node->children);
|
|
|
|
free(node->children);
|
|
|
|
free(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
struct node_entry *e;
|
|
|
|
vec_for_each(node->children, e) {
|
|
|
|
destroy_subtree(e);
|
|
|
|
}
|
|
|
|
destroy_node(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the child
|
|
|
|
static struct node_entry *insert_child(struct node_entry *p, struct node_entry *c) {
|
|
|
|
c->parent = p;
|
|
|
|
if (p->children == NULL) {
|
|
|
|
p->children = xmalloc(sizeof(struct vector));
|
|
|
|
vec_init(p->children);
|
|
|
|
}
|
|
|
|
struct node_entry *e;
|
|
|
|
vec_for_each(p->children, e) {
|
|
|
|
if (strcmp(e->name, c->name) == 0) {
|
|
|
|
// Exist duplicate
|
|
|
|
if (c->status > e->status) {
|
|
|
|
// Precedence is higher, replace with new node
|
|
|
|
destroy_subtree(e);
|
2017-09-07 13:22:30 +02:00
|
|
|
vec_cur(p->children) = c;
|
2017-08-11 05:04:02 +02:00
|
|
|
return c;
|
|
|
|
} else {
|
|
|
|
// Free the new entry, return old
|
|
|
|
destroy_node(c);
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// New entry, push back
|
|
|
|
vec_push_back(p->children, c);
|
|
|
|
return c;
|
|
|
|
}
|
2017-05-08 19:09:32 +02:00
|
|
|
|
2017-04-30 19:58:52 +02:00
|
|
|
/***********
|
2017-08-30 20:20:06 +02:00
|
|
|
* setenvs *
|
2017-04-30 19:58:52 +02:00
|
|
|
***********/
|
|
|
|
|
2017-08-30 20:20:06 +02:00
|
|
|
static void bb_setenv(struct vector *v) {
|
|
|
|
for (int i = 0; environ[i]; ++i) {
|
|
|
|
if (strncmp(environ[i], "PATH=", 5) == 0) {
|
|
|
|
snprintf(buf, PATH_MAX, "PATH=%s:%s", BBPATH, strchr(environ[i], '=') + 1);
|
|
|
|
vec_push_back(v, strdup(buf));
|
|
|
|
} else {
|
|
|
|
vec_push_back(v, strdup(environ[i]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
vec_push_back(v, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void pm_setenv(struct vector *v) {
|
|
|
|
for (int i = 0; environ[i]; ++i) {
|
|
|
|
if (strncmp(environ[i], "CLASSPATH=", 10) != 0)
|
|
|
|
vec_push_back(v, strdup(environ[i]));
|
|
|
|
}
|
|
|
|
vec_push_back(v, strdup("CLASSPATH=/system/framework/pm.jar"));
|
|
|
|
vec_push_back(v, NULL);
|
2017-08-01 09:34:16 +02:00
|
|
|
}
|
|
|
|
|
2017-08-30 20:20:06 +02:00
|
|
|
/***********
|
|
|
|
* Scripts *
|
|
|
|
***********/
|
|
|
|
|
2017-08-01 09:34:16 +02:00
|
|
|
static void exec_common_script(const char* stage) {
|
2017-04-30 19:58:52 +02:00
|
|
|
DIR *dir;
|
|
|
|
struct dirent *entry;
|
2017-05-03 21:05:37 +02:00
|
|
|
snprintf(buf, PATH_MAX, "%s/%s.d", COREDIR, stage);
|
2017-04-30 19:58:52 +02:00
|
|
|
|
2017-10-13 18:08:12 +02:00
|
|
|
if (!(dir = xopendir(buf)))
|
2017-04-30 19:58:52 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
while ((entry = xreaddir(dir))) {
|
|
|
|
if (entry->d_type == DT_REG) {
|
2017-05-04 22:16:00 +02:00
|
|
|
snprintf(buf2, PATH_MAX, "%s/%s", buf, entry->d_name);
|
|
|
|
if (access(buf2, X_OK) == -1)
|
2017-04-30 19:58:52 +02:00
|
|
|
continue;
|
|
|
|
LOGI("%s.d: exec [%s]\n", stage, entry->d_name);
|
2017-08-20 15:36:32 +02:00
|
|
|
int pid = exec_command(0, NULL, bb_setenv, "sh", buf2, NULL);
|
2017-04-30 19:58:52 +02:00
|
|
|
if (pid != -1)
|
|
|
|
waitpid(pid, NULL, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
closedir(dir);
|
|
|
|
}
|
|
|
|
|
2017-08-01 09:34:16 +02:00
|
|
|
static void exec_module_script(const char* stage) {
|
2017-04-30 19:58:52 +02:00
|
|
|
char *module;
|
|
|
|
vec_for_each(&module_list, module) {
|
2017-08-03 12:07:34 +02:00
|
|
|
snprintf(buf2, PATH_MAX, "%s/%s/%s.sh", MOUNTPOINT, module, stage);
|
|
|
|
snprintf(buf, PATH_MAX, "%s/%s/disable", MOUNTPOINT, module);
|
|
|
|
if (access(buf2, F_OK) == -1 || access(buf, F_OK) == 0)
|
2017-04-30 19:58:52 +02:00
|
|
|
continue;
|
|
|
|
LOGI("%s: exec [%s.sh]\n", module, stage);
|
2017-08-20 15:36:32 +02:00
|
|
|
int pid = exec_command(0, NULL, bb_setenv, "sh", buf2, NULL);
|
2017-04-30 19:58:52 +02:00
|
|
|
if (pid != -1)
|
|
|
|
waitpid(pid, NULL, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/***************
|
|
|
|
* Magic Mount *
|
|
|
|
***************/
|
|
|
|
|
2017-05-08 19:09:32 +02:00
|
|
|
static void construct_tree(const char *module, struct node_entry *parent) {
|
2017-04-30 19:58:52 +02:00
|
|
|
DIR *dir;
|
|
|
|
struct dirent *entry;
|
|
|
|
struct node_entry *node;
|
|
|
|
|
2017-05-08 19:09:32 +02:00
|
|
|
char *parent_path = get_full_path(parent);
|
|
|
|
snprintf(buf, PATH_MAX, "%s/%s%s", MOUNTPOINT, module, parent_path);
|
2017-04-30 19:58:52 +02:00
|
|
|
|
2017-10-13 18:08:12 +02:00
|
|
|
if (!(dir = xopendir(buf)))
|
2017-05-08 19:09:32 +02:00
|
|
|
goto cleanup;
|
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
|
|
|
|
node = xcalloc(sizeof(*node), 1);
|
|
|
|
node->module = module;
|
|
|
|
node->name = strdup(entry->d_name);
|
|
|
|
node->type = entry->d_type;
|
2017-05-08 19:09:32 +02:00
|
|
|
snprintf(buf, PATH_MAX, "%s/%s", parent_path, node->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
|
|
|
|
* 3. Target file is a symlink, but not /system/vendor
|
2017-07-30 14:14:12 +02:00
|
|
|
*/
|
2017-06-11 21:29:01 +02:00
|
|
|
int clone = 0;
|
2017-05-08 19:09:32 +02:00
|
|
|
if (IS_LNK(node) || access(buf, F_OK) == -1) {
|
2017-06-11 21:29:01 +02:00
|
|
|
clone = 1;
|
2017-08-11 05:04:02 +02:00
|
|
|
} else if (parent->parent != NULL || strcmp(node->name, "vendor") != 0) {
|
2017-06-11 21:29:01 +02:00
|
|
|
struct stat s;
|
|
|
|
xstat(buf, &s);
|
|
|
|
if (S_ISLNK(s.st_mode))
|
|
|
|
clone = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (clone) {
|
2017-04-30 19:58:52 +02:00
|
|
|
// Mark the parent folder as a skeleton
|
2017-06-11 21:29:01 +02:00
|
|
|
parent->status |= IS_SKEL; /* This will not overwrite if parent is module */
|
|
|
|
node->status = IS_MODULE;
|
2017-05-08 19:09:32 +02:00
|
|
|
} else if (IS_DIR(node)) {
|
|
|
|
// Check if marked as replace
|
|
|
|
snprintf(buf2, PATH_MAX, "%s/%s%s/.replace", MOUNTPOINT, module, buf);
|
|
|
|
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)) {
|
|
|
|
// This is a leaf, mark as target
|
2017-06-11 21:29:01 +02:00
|
|
|
node->status = IS_MODULE;
|
2017-05-08 19:09:32 +02:00
|
|
|
}
|
|
|
|
node = insert_child(parent, node);
|
2017-06-13 18:55:41 +02:00
|
|
|
if (node->status & (IS_SKEL | IS_INTER)) {
|
|
|
|
// Intermediate folder, travel deeper
|
2017-05-08 19:09:32 +02:00
|
|
|
construct_tree(module, node);
|
2017-04-30 19:58:52 +02:00
|
|
|
}
|
|
|
|
}
|
2017-07-30 14:14:12 +02:00
|
|
|
|
2017-04-30 19:58:52 +02:00
|
|
|
closedir(dir);
|
2017-05-08 19:09:32 +02:00
|
|
|
|
|
|
|
cleanup:
|
|
|
|
free(parent_path);
|
2017-04-30 19:58:52 +02:00
|
|
|
}
|
|
|
|
|
2017-05-08 19:09:32 +02:00
|
|
|
static void clone_skeleton(struct node_entry *node) {
|
2017-04-30 19:58:52 +02:00
|
|
|
DIR *dir;
|
|
|
|
struct dirent *entry;
|
|
|
|
struct node_entry *dummy, *child;
|
|
|
|
|
|
|
|
// Clone the structure
|
2017-05-08 19:09:32 +02:00
|
|
|
char *full_path = get_full_path(node);
|
2017-06-11 21:29:01 +02:00
|
|
|
snprintf(buf, PATH_MAX, "%s%s", MIRRDIR, full_path);
|
2017-10-13 18:08:12 +02:00
|
|
|
if (!(dir = xopendir(buf)))
|
2017-05-08 19:09:32 +02:00
|
|
|
goto cleanup;
|
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
|
|
|
|
dummy = xcalloc(sizeof(*dummy), 1);
|
|
|
|
dummy->name = strdup(entry->d_name);
|
|
|
|
dummy->type = entry->d_type;
|
2017-06-11 21:29:01 +02:00
|
|
|
dummy->status = IS_DUMMY;
|
2017-05-08 19:09:32 +02:00
|
|
|
insert_child(node, 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
|
|
|
|
2017-09-26 22:35:16 +02:00
|
|
|
if (node->status & IS_SKEL) {
|
2017-09-28 19:07:28 +02:00
|
|
|
struct stat s;
|
|
|
|
char *con;
|
|
|
|
xstat(full_path, &s);
|
|
|
|
getfilecon(full_path, &con);
|
2017-09-26 22:35:16 +02:00
|
|
|
LOGI("tmpfs: %s\n", full_path);
|
2017-10-13 18:08:12 +02:00
|
|
|
xmount("tmpfs", full_path, "tmpfs", 0, NULL);
|
2017-09-28 19:07:28 +02:00
|
|
|
chmod(full_path, s.st_mode & 0777);
|
|
|
|
chown(full_path, s.st_uid, s.st_gid);
|
|
|
|
setfilecon(full_path, con);
|
|
|
|
free(con);
|
2017-09-26 22:35:16 +02:00
|
|
|
}
|
2017-04-30 19:58:52 +02:00
|
|
|
|
|
|
|
vec_for_each(node->children, child) {
|
2017-09-26 22:35:16 +02:00
|
|
|
snprintf(buf, PATH_MAX, "%s/%s", full_path, child->name);
|
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
|
|
|
|
2017-08-11 05:29:35 +02:00
|
|
|
if (child->parent->parent == NULL && strcmp(child->name, "vendor") == 0) {
|
2017-06-11 21:29:01 +02:00
|
|
|
if (IS_LNK(child)) {
|
|
|
|
cp_afc(MIRRDIR "/system/vendor", "/system/vendor");
|
|
|
|
LOGI("cplink: %s -> %s\n", MIRRDIR "/system/vendor", "/system/vendor");
|
|
|
|
}
|
|
|
|
// Skip
|
|
|
|
continue;
|
|
|
|
} else if (child->status & IS_MODULE) {
|
2017-04-30 19:58:52 +02:00
|
|
|
// Mount from module file to dummy file
|
2017-05-08 19:09:32 +02:00
|
|
|
snprintf(buf2, PATH_MAX, "%s/%s%s/%s", MOUNTPOINT, child->module, full_path, child->name);
|
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
|
2017-05-08 19:09:32 +02:00
|
|
|
clone_skeleton(child);
|
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
|
|
|
|
snprintf(buf2, PATH_MAX, "%s%s/%s", MIRRDIR, full_path, child->name);
|
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);
|
2017-09-13 09:45:07 +02:00
|
|
|
#ifdef MAGISK_DEBUG
|
2017-09-26 22:06:44 +02:00
|
|
|
LOGI("cplink: %s -> %s\n",buf2, buf);
|
2017-09-13 09:45:07 +02:00
|
|
|
#else
|
|
|
|
LOGI("cplink: %s\n", buf);
|
|
|
|
#endif
|
2017-04-30 19:58:52 +02:00
|
|
|
} else {
|
2017-05-08 19:09:32 +02:00
|
|
|
snprintf(buf, PATH_MAX, "%s/%s", full_path, child->name);
|
2017-04-30 19:58:52 +02:00
|
|
|
bind_mount(buf2, buf);
|
|
|
|
}
|
|
|
|
}
|
2017-05-08 19:09:32 +02:00
|
|
|
|
|
|
|
cleanup:
|
|
|
|
free(full_path);
|
2017-04-30 19:58:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void magic_mount(struct node_entry *node) {
|
|
|
|
char *real_path;
|
|
|
|
struct node_entry *child;
|
|
|
|
|
2017-06-11 21:29:01 +02:00
|
|
|
if (node->status & IS_MODULE) {
|
|
|
|
// The real deal, mount module item
|
|
|
|
real_path = get_full_path(node);
|
|
|
|
snprintf(buf, PATH_MAX, "%s/%s%s", MOUNTPOINT, node->module, real_path);
|
|
|
|
bind_mount(buf, real_path);
|
|
|
|
free(real_path);
|
|
|
|
} else if (node->status & IS_SKEL) {
|
|
|
|
// The node is labeled to be cloned with skeleton, lets do it
|
|
|
|
clone_skeleton(node);
|
|
|
|
} else if (node->status & IS_INTER) {
|
2017-05-08 19:09:32 +02:00
|
|
|
// It's an intermediate node, travel deeper
|
|
|
|
vec_for_each(node->children, child)
|
|
|
|
magic_mount(child);
|
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
|
|
|
}
|
|
|
|
|
2017-05-03 20:39:53 +02:00
|
|
|
/****************
|
|
|
|
* Simple Mount *
|
|
|
|
****************/
|
|
|
|
|
|
|
|
static void simple_mount(const char *path) {
|
|
|
|
DIR *dir;
|
|
|
|
struct dirent *entry;
|
|
|
|
|
|
|
|
snprintf(buf, PATH_MAX, "%s%s", CACHEMOUNT, path);
|
|
|
|
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) {
|
|
|
|
char *new_path = strdup(buf2);
|
|
|
|
simple_mount(new_path);
|
|
|
|
free(new_path);
|
|
|
|
} else if (entry->d_type == DT_REG) {
|
|
|
|
// Actual file path
|
2017-06-30 15:49:35 +02:00
|
|
|
snprintf(buf, PATH_MAX, "%s%s", CACHEMOUNT, 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 *
|
|
|
|
*****************/
|
|
|
|
|
2017-10-08 16:00:22 +02:00
|
|
|
// A one time setup
|
2017-10-11 21:39:39 +02:00
|
|
|
static void daemon_init() {
|
2017-10-08 16:00:22 +02:00
|
|
|
LOGI("* Creating /sbin overlay");
|
|
|
|
DIR *dir;
|
|
|
|
struct dirent *entry;
|
|
|
|
int root, sbin;
|
|
|
|
// Setup links under /sbin
|
|
|
|
xmount(NULL, "/", NULL, MS_REMOUNT, NULL);
|
|
|
|
xmkdir("/root", 0755);
|
2017-10-13 18:08:12 +02:00
|
|
|
chmod("/root", 0755);
|
2017-10-08 16:00:22 +02:00
|
|
|
root = xopen("/root", O_RDONLY | O_CLOEXEC);
|
|
|
|
sbin = xopen("/sbin", O_RDONLY | O_CLOEXEC);
|
2017-10-13 18:08:12 +02:00
|
|
|
dir = xfdopendir(sbin);
|
|
|
|
while((entry = xreaddir(dir))) {
|
2017-10-08 16:00:22 +02:00
|
|
|
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) continue;
|
|
|
|
linkat(sbin, entry->d_name, root, entry->d_name, 0);
|
|
|
|
if (strcmp(entry->d_name, "magisk") == 0)
|
|
|
|
unlinkat(sbin, entry->d_name, 0);
|
|
|
|
}
|
|
|
|
close(sbin);
|
2017-10-28 10:12:01 +02:00
|
|
|
xmount("tmpfs", "/sbin", "tmpfs", 0, NULL);
|
2017-10-08 16:00:22 +02:00
|
|
|
sbin = xopen("/sbin", O_RDONLY | O_CLOEXEC);
|
|
|
|
fchmod(sbin, 0755);
|
|
|
|
fsetfilecon(sbin, "u:object_r:rootfs:s0");
|
2017-10-13 18:08:12 +02:00
|
|
|
dir = xfdopendir(root);
|
|
|
|
while((entry = xreaddir(dir))) {
|
2017-10-08 16:00:22 +02:00
|
|
|
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) continue;
|
2017-10-14 15:09:05 +02:00
|
|
|
snprintf(buf, PATH_MAX, "/root/%s", entry->d_name);
|
|
|
|
snprintf(buf2, PATH_MAX, "/sbin/%s", entry->d_name);
|
|
|
|
xsymlink(buf, buf2);
|
2017-10-08 16:00:22 +02:00
|
|
|
}
|
|
|
|
for (int i = 0; applet[i]; ++i) {
|
2017-10-14 15:09:05 +02:00
|
|
|
snprintf(buf2, PATH_MAX, "/sbin/%s", applet[i]);
|
|
|
|
xsymlink("/root/magisk", buf2);
|
2017-10-08 16:00:22 +02:00
|
|
|
}
|
2017-10-28 10:12:01 +02:00
|
|
|
xsymlink(MOUNTPOINT, FAKEPOINT);
|
|
|
|
close(root);
|
|
|
|
close(sbin);
|
2017-10-08 16:00:22 +02:00
|
|
|
xmount(NULL, "/", NULL, MS_REMOUNT | MS_RDONLY, NULL);
|
|
|
|
|
2017-08-11 05:43:45 +02:00
|
|
|
LOGI("* Mounting mirrors");
|
|
|
|
struct vector mounts;
|
|
|
|
vec_init(&mounts);
|
|
|
|
file_to_vector("/proc/mounts", &mounts);
|
|
|
|
char *line;
|
2017-09-13 09:45:07 +02:00
|
|
|
int skip_initramfs = 0;
|
|
|
|
// Check whether skip_initramfs device
|
2017-08-11 05:43:45 +02:00
|
|
|
vec_for_each(&mounts, line) {
|
2017-09-13 09:45:07 +02:00
|
|
|
if (strstr(line, " /system_root ")) {
|
|
|
|
xmkdir_p(MIRRDIR "/system", 0755);
|
|
|
|
bind_mount("/system_root/system", MIRRDIR "/system");
|
|
|
|
skip_initramfs = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
vec_for_each(&mounts, line) {
|
|
|
|
if (!skip_initramfs && strstr(line, " /system ")) {
|
2017-08-11 05:43:45 +02:00
|
|
|
sscanf(line, "%s", buf);
|
|
|
|
xmkdir_p(MIRRDIR "/system", 0755);
|
|
|
|
xmount(buf, MIRRDIR "/system", "ext4", MS_RDONLY, NULL);
|
2017-09-13 09:45:07 +02:00
|
|
|
#ifdef MAGISK_DEBUG
|
2017-09-26 22:06:44 +02:00
|
|
|
LOGI("mount: %s -> %s\n", buf, MIRRDIR "/system");
|
2017-09-13 09:45:07 +02:00
|
|
|
#else
|
|
|
|
LOGI("mount: %s\n", MIRRDIR "/system");
|
|
|
|
#endif
|
2017-10-14 15:09:05 +02:00
|
|
|
} else if (strstr(line, " /vendor ")) {
|
2017-08-11 05:43:45 +02:00
|
|
|
seperate_vendor = 1;
|
|
|
|
sscanf(line, "%s", buf);
|
|
|
|
xmkdir_p(MIRRDIR "/vendor", 0755);
|
|
|
|
xmount(buf, MIRRDIR "/vendor", "ext4", MS_RDONLY, NULL);
|
2017-09-13 09:45:07 +02:00
|
|
|
#ifdef MAGISK_DEBUG
|
2017-09-26 22:06:44 +02:00
|
|
|
LOGI("mount: %s -> %s\n", buf, MIRRDIR "/vendor");
|
2017-09-13 09:45:07 +02:00
|
|
|
#else
|
|
|
|
LOGI("mount: %s\n", MIRRDIR "/vendor");
|
|
|
|
#endif
|
2017-08-11 05:43:45 +02:00
|
|
|
}
|
2017-10-14 15:09:05 +02:00
|
|
|
free(line);
|
2017-08-11 05:43:45 +02:00
|
|
|
}
|
2017-10-14 15:09:05 +02:00
|
|
|
vec_destroy(&mounts);
|
2017-08-11 05:43:45 +02:00
|
|
|
if (!seperate_vendor) {
|
2017-10-13 18:08:12 +02:00
|
|
|
xsymlink(MIRRDIR "/system/vendor", MIRRDIR "/vendor");
|
2017-09-13 09:45:07 +02:00
|
|
|
#ifdef MAGISK_DEBUG
|
2017-09-26 22:06:44 +02:00
|
|
|
LOGI("link: %s -> %s\n", MIRRDIR "/system/vendor", MIRRDIR "/vendor");
|
2017-09-13 09:45:07 +02:00
|
|
|
#else
|
|
|
|
LOGI("link: %s\n", MIRRDIR "/vendor");
|
|
|
|
#endif
|
2017-08-11 05:43:45 +02:00
|
|
|
}
|
2017-10-13 18:08:12 +02:00
|
|
|
xmkdir_p(MIRRDIR "/bin", 0755);
|
2017-08-11 11:14:55 +02:00
|
|
|
bind_mount(DATABIN, MIRRDIR "/bin");
|
2017-08-11 05:43:45 +02:00
|
|
|
|
2017-10-08 16:00:22 +02:00
|
|
|
LOGI("* Setting up internal busybox");
|
2017-10-13 18:08:12 +02:00
|
|
|
xmkdir_p(BBPATH, 0755);
|
2017-08-20 15:36:32 +02:00
|
|
|
exec_command_sync(MIRRDIR "/bin/busybox", "--install", "-s", BBPATH, NULL);
|
2017-10-13 18:08:12 +02:00
|
|
|
xsymlink(MIRRDIR "/bin/busybox", BBPATH "/busybox");
|
2017-08-11 05:43:45 +02:00
|
|
|
}
|
|
|
|
|
2017-08-12 19:15:18 +02:00
|
|
|
static int prepare_img() {
|
2017-10-11 21:39:39 +02:00
|
|
|
// First merge images
|
|
|
|
if (merge_img("/data/magisk_merge.img", MAINIMG)) {
|
2017-10-14 15:09:05 +02:00
|
|
|
LOGE("Image merge /data/magisk_merge.img -> " MAINIMG " failed!\n");
|
2017-10-11 21:39:39 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-08-12 19:15:18 +02:00
|
|
|
if (access(MAINIMG, F_OK) == -1) {
|
|
|
|
if (create_img(MAINIMG, 64))
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOGI("* Mounting " MAINIMG "\n");
|
|
|
|
// Mounting magisk image
|
|
|
|
char *magiskloop = mount_image(MAINIMG, MOUNTPOINT);
|
|
|
|
if (magiskloop == NULL)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
vec_init(&module_list);
|
|
|
|
|
2017-09-27 23:50:06 +02:00
|
|
|
xmkdir(COREDIR, 0755);
|
|
|
|
xmkdir(COREDIR "/post-fs-data.d", 0755);
|
|
|
|
xmkdir(COREDIR "/service.d", 0755);
|
|
|
|
xmkdir(COREDIR "/props", 0755);
|
|
|
|
|
|
|
|
DIR *dir = xopendir(MOUNTPOINT);
|
|
|
|
struct dirent *entry;
|
|
|
|
while ((entry = xreaddir(dir))) {
|
|
|
|
if (entry->d_type == DT_DIR) {
|
|
|
|
if (strcmp(entry->d_name, ".") == 0 ||
|
|
|
|
strcmp(entry->d_name, "..") == 0 ||
|
|
|
|
strcmp(entry->d_name, ".core") == 0 ||
|
|
|
|
strcmp(entry->d_name, "lost+found") == 0)
|
|
|
|
continue;
|
|
|
|
snprintf(buf, PATH_MAX, "%s/%s/remove", MOUNTPOINT, entry->d_name);
|
|
|
|
if (access(buf, F_OK) == 0) {
|
|
|
|
snprintf(buf, PATH_MAX, "%s/%s", MOUNTPOINT, entry->d_name);
|
2017-10-11 20:57:18 +02:00
|
|
|
rm_rf(buf);
|
2017-09-27 23:50:06 +02:00
|
|
|
continue;
|
2017-08-12 19:15:18 +02:00
|
|
|
}
|
2017-09-27 23:50:06 +02:00
|
|
|
snprintf(buf, PATH_MAX, "%s/%s/disable", MOUNTPOINT, entry->d_name);
|
|
|
|
if (access(buf, F_OK) == 0)
|
|
|
|
continue;
|
|
|
|
vec_push_back(&module_list, strdup(entry->d_name));
|
2017-08-12 19:15:18 +02:00
|
|
|
}
|
2017-09-27 23:50:06 +02:00
|
|
|
}
|
2017-08-12 19:15:18 +02:00
|
|
|
|
2017-09-27 23:50:06 +02:00
|
|
|
closedir(dir);
|
2017-08-12 19:15:18 +02:00
|
|
|
|
2017-09-27 23:50:06 +02:00
|
|
|
// Trim image
|
|
|
|
umount_image(MOUNTPOINT, magiskloop);
|
|
|
|
free(magiskloop);
|
|
|
|
trim_img(MAINIMG);
|
|
|
|
|
|
|
|
// Remount them back :)
|
|
|
|
magiskloop = mount_image(MAINIMG, MOUNTPOINT);
|
|
|
|
free(magiskloop);
|
2017-08-12 19:15:18 +02:00
|
|
|
|
2017-10-14 15:09:05 +02:00
|
|
|
// Fix file selinux contexts
|
2017-10-11 21:39:39 +02:00
|
|
|
fix_filecon();
|
2017-08-12 19:15:18 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-10-11 21:39:39 +02:00
|
|
|
void fix_filecon() {
|
|
|
|
int dirfd = xopen(MOUNTPOINT, O_RDONLY | O_CLOEXEC);
|
|
|
|
restorecon(dirfd, 0);
|
|
|
|
close(dirfd);
|
|
|
|
}
|
|
|
|
|
2017-04-30 19:58:52 +02:00
|
|
|
/****************
|
|
|
|
* Entry points *
|
|
|
|
****************/
|
|
|
|
|
2017-05-03 19:13:04 +02:00
|
|
|
static void unblock_boot_process() {
|
2017-11-05 22:41:03 +01:00
|
|
|
close(xopen(UNBLOCKFILE, O_RDONLY | O_CREAT, 0));
|
2017-05-03 19:13:04 +02:00
|
|
|
pthread_exit(NULL);
|
|
|
|
}
|
|
|
|
|
2017-04-30 19:58:52 +02:00
|
|
|
void post_fs(int client) {
|
|
|
|
LOGI("** post-fs mode running\n");
|
|
|
|
// ack
|
|
|
|
write_int(client, 0);
|
|
|
|
close(client);
|
|
|
|
|
2017-05-03 19:13:04 +02:00
|
|
|
// Uninstall or core only mode
|
|
|
|
if (access(UNINSTALLER, F_OK) == 0 || access(DISABLEFILE, F_OK) == 0)
|
|
|
|
goto unblock;
|
|
|
|
|
2017-04-30 19:58:52 +02:00
|
|
|
// Allocate buffer
|
|
|
|
buf = xmalloc(PATH_MAX);
|
|
|
|
buf2 = xmalloc(PATH_MAX);
|
|
|
|
|
2017-05-03 20:39:53 +02:00
|
|
|
simple_mount("/system");
|
|
|
|
simple_mount("/vendor");
|
|
|
|
|
2017-04-30 19:58:52 +02:00
|
|
|
unblock:
|
|
|
|
unblock_boot_process();
|
|
|
|
}
|
|
|
|
|
|
|
|
void post_fs_data(int client) {
|
|
|
|
// ack
|
|
|
|
write_int(client, 0);
|
|
|
|
close(client);
|
|
|
|
if (!check_data())
|
|
|
|
goto unblock;
|
|
|
|
|
2017-10-08 23:05:52 +02:00
|
|
|
// Start the debug log
|
|
|
|
start_debug_full_log();
|
2017-06-08 13:15:51 +02:00
|
|
|
|
2017-05-03 19:13:04 +02:00
|
|
|
LOGI("** post-fs-data mode running\n");
|
|
|
|
|
2017-04-30 19:58:52 +02:00
|
|
|
// Allocate buffer
|
|
|
|
if (buf == NULL) buf = xmalloc(PATH_MAX);
|
|
|
|
if (buf2 == NULL) buf2 = xmalloc(PATH_MAX);
|
|
|
|
|
2017-08-20 15:36:32 +02:00
|
|
|
// Magisk binaries
|
|
|
|
char *bin_path = NULL;
|
|
|
|
if (access("/cache/data_bin", F_OK) == 0)
|
|
|
|
bin_path = "/cache/data_bin";
|
2017-08-30 21:08:09 +02:00
|
|
|
else if (access("/data/data/com.topjohnwu.magisk/install", F_OK) == 0)
|
|
|
|
bin_path = "/data/data/com.topjohnwu.magisk/install";
|
2017-09-15 12:05:42 +02:00
|
|
|
else if (access("/data/user_de/0/com.topjohnwu.magisk/install", F_OK) == 0)
|
|
|
|
bin_path = "/data/user_de/0/com.topjohnwu.magisk/install";
|
2017-08-20 15:36:32 +02:00
|
|
|
if (bin_path) {
|
2017-10-11 20:57:18 +02:00
|
|
|
rm_rf(DATABIN);
|
|
|
|
cp_afc(bin_path, DATABIN);
|
|
|
|
rm_rf(bin_path);
|
2017-05-03 19:13:04 +02:00
|
|
|
}
|
|
|
|
|
2017-11-14 21:41:30 +01:00
|
|
|
// Lazy.... use shell blob to match files
|
|
|
|
exec_command_sync("sh", "-c", "mv -f /data/magisk/stock_*.img.gz /data", NULL);
|
|
|
|
|
2017-10-08 16:00:22 +02:00
|
|
|
// Initialize
|
|
|
|
daemon_init();
|
2017-06-02 23:52:49 +02:00
|
|
|
|
2017-08-01 20:21:50 +02:00
|
|
|
// uninstaller
|
|
|
|
if (access(UNINSTALLER, F_OK) == 0) {
|
|
|
|
close(open(UNBLOCKFILE, O_RDONLY | O_CREAT));
|
2017-08-20 15:36:32 +02:00
|
|
|
setenv("BOOTMODE", "true", 1);
|
2017-08-30 20:20:06 +02:00
|
|
|
exec_command(0, NULL, bb_setenv, "sh", UNINSTALLER, NULL);
|
2017-08-01 20:21:50 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-10-11 21:39:39 +02:00
|
|
|
// Merge, trim, mount magisk.img, which will also travel through the modules
|
2017-08-12 19:15:18 +02:00
|
|
|
// After this, it will create the module list
|
|
|
|
if (prepare_img())
|
2017-09-05 20:25:40 +02:00
|
|
|
goto core_only; // Mounting fails, we can only do core only stuffs
|
2017-04-30 19:58:52 +02:00
|
|
|
|
2017-08-12 19:15:18 +02:00
|
|
|
// Run common scripts
|
|
|
|
LOGI("* Running post-fs-data.d scripts\n");
|
|
|
|
exec_common_script("post-fs-data");
|
2017-06-02 23:52:49 +02:00
|
|
|
|
2017-05-03 19:13:04 +02:00
|
|
|
// Core only mode
|
|
|
|
if (access(DISABLEFILE, F_OK) == 0)
|
2017-07-13 18:54:43 +02:00
|
|
|
goto core_only;
|
2017-05-03 19:13:04 +02:00
|
|
|
|
2017-08-12 19:15:18 +02:00
|
|
|
// Execute module scripts
|
|
|
|
LOGI("* Running module post-fs-data scripts\n");
|
|
|
|
exec_module_script("post-fs-data");
|
|
|
|
|
2017-04-30 19:58:52 +02:00
|
|
|
char *module;
|
|
|
|
struct node_entry *sys_root, *ven_root = NULL, *child;
|
|
|
|
|
|
|
|
// Create the system root entry
|
|
|
|
sys_root = xcalloc(sizeof(*sys_root), 1);
|
2017-08-11 05:04:02 +02:00
|
|
|
sys_root->name = strdup("system");
|
2017-06-11 21:29:01 +02:00
|
|
|
sys_root->status = IS_INTER;
|
2017-04-30 19:58:52 +02:00
|
|
|
|
|
|
|
int has_modules = 0;
|
|
|
|
|
|
|
|
LOGI("* Loading modules\n");
|
2017-08-12 19:15:18 +02:00
|
|
|
vec_for_each(&module_list, module) {
|
|
|
|
// Read props
|
|
|
|
snprintf(buf, PATH_MAX, "%s/%s/system.prop", MOUNTPOINT, module);
|
|
|
|
if (access(buf, F_OK) == 0) {
|
|
|
|
LOGI("%s: loading [system.prop]\n", module);
|
|
|
|
read_prop_file(buf, 0);
|
|
|
|
}
|
|
|
|
// Check whether enable auto_mount
|
|
|
|
snprintf(buf, PATH_MAX, "%s/%s/auto_mount", MOUNTPOINT, module);
|
|
|
|
if (access(buf, F_OK) == -1)
|
|
|
|
continue;
|
|
|
|
// Double check whether the system folder exists
|
|
|
|
snprintf(buf, PATH_MAX, "%s/%s/system", MOUNTPOINT, module);
|
|
|
|
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
|
|
|
|
has_modules = 1;
|
|
|
|
LOGI("%s: constructing magic mount structure\n", module);
|
|
|
|
// If /system/vendor exists in module, create a link outside
|
|
|
|
snprintf(buf, PATH_MAX, "%s/%s/system/vendor", MOUNTPOINT, module);
|
|
|
|
if (access(buf, F_OK) == 0) {
|
|
|
|
snprintf(buf2, PATH_MAX, "%s/%s/vendor", MOUNTPOINT, module);
|
|
|
|
unlink(buf2);
|
2017-10-13 18:08:12 +02:00
|
|
|
xsymlink(buf, buf2);
|
2017-04-30 19:58:52 +02:00
|
|
|
}
|
2017-08-12 19:15:18 +02:00
|
|
|
construct_tree(module, sys_root);
|
2017-04-30 19:58:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (has_modules) {
|
2017-06-11 21:29:01 +02:00
|
|
|
// Extract the vendor node out of system tree and swap with placeholder
|
2017-04-30 19:58:52 +02:00
|
|
|
vec_for_each(sys_root->children, child) {
|
|
|
|
if (strcmp(child->name, "vendor") == 0) {
|
|
|
|
ven_root = child;
|
2017-06-11 21:29:01 +02:00
|
|
|
child = xcalloc(sizeof(*child), 1);
|
|
|
|
child->type = seperate_vendor ? DT_LNK : DT_DIR;
|
|
|
|
child->parent = ven_root->parent;
|
2017-08-11 05:29:35 +02:00
|
|
|
child->name = strdup("vendor");
|
|
|
|
child->status = 0;
|
|
|
|
// Swap!
|
2017-09-07 13:22:30 +02:00
|
|
|
vec_cur(sys_root->children) = child;
|
2017-04-30 19:58:52 +02:00
|
|
|
ven_root->parent = NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-06-11 21:29:01 +02:00
|
|
|
|
|
|
|
// Magic!!
|
|
|
|
magic_mount(sys_root);
|
|
|
|
if (ven_root) magic_mount(ven_root);
|
2017-04-30 19:58:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Cleanup memory
|
|
|
|
destroy_subtree(sys_root);
|
2017-06-11 21:29:01 +02:00
|
|
|
if (ven_root) destroy_subtree(ven_root);
|
2017-04-30 19:58:52 +02:00
|
|
|
|
2017-07-13 18:54:43 +02:00
|
|
|
core_only:
|
2017-05-03 21:05:37 +02:00
|
|
|
// Systemless hosts
|
|
|
|
if (access(HOSTSFILE, F_OK) == 0) {
|
|
|
|
LOGI("* Enabling systemless hosts file support");
|
|
|
|
bind_mount(HOSTSFILE, "/system/etc/hosts");
|
|
|
|
}
|
|
|
|
|
2017-10-10 13:49:15 +02:00
|
|
|
auto_start_magiskhide();
|
|
|
|
|
2017-04-30 19:58:52 +02:00
|
|
|
unblock:
|
|
|
|
unblock_boot_process();
|
|
|
|
}
|
|
|
|
|
|
|
|
void late_start(int client) {
|
|
|
|
LOGI("** late_start service mode running\n");
|
|
|
|
// ack
|
|
|
|
write_int(client, 0);
|
|
|
|
close(client);
|
|
|
|
|
2017-05-04 22:16:00 +02:00
|
|
|
// Allocate buffer
|
|
|
|
if (buf == NULL) buf = xmalloc(PATH_MAX);
|
|
|
|
if (buf2 == NULL) buf2 = xmalloc(PATH_MAX);
|
|
|
|
|
2017-04-30 19:58:52 +02:00
|
|
|
// Wait till the full patch is done
|
2017-11-05 22:41:03 +01:00
|
|
|
while (access(PATCHDONE, F_OK) == -1)
|
|
|
|
usleep(500); /* Wait 0.5ms */
|
2017-04-30 19:58:52 +02:00
|
|
|
|
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");
|
|
|
|
exec_module_script("service");
|
|
|
|
|
2017-07-31 17:31:40 +02:00
|
|
|
core_only:
|
2017-05-08 19:16:58 +02:00
|
|
|
// Install Magisk Manager if exists
|
|
|
|
if (access(MANAGERAPK, F_OK) == 0) {
|
2017-06-11 10:51:44 +02:00
|
|
|
while (1) {
|
2017-05-08 19:16:58 +02:00
|
|
|
sleep(5);
|
2017-07-10 16:29:53 +02:00
|
|
|
int apk_res = -1, pid;
|
2017-08-20 15:36:32 +02:00
|
|
|
pid = exec_command(1, &apk_res, pm_setenv,
|
|
|
|
"app_process",
|
|
|
|
"/system/bin", "com.android.commands.pm.Pm",
|
|
|
|
"install", "-r", MANAGERAPK, NULL);
|
2017-08-01 09:34:16 +02:00
|
|
|
if (pid != -1) {
|
|
|
|
waitpid(pid, NULL, 0);
|
|
|
|
fdgets(buf, PATH_MAX, apk_res);
|
|
|
|
close(apk_res);
|
|
|
|
// Keep trying until pm is started
|
|
|
|
if (strstr(buf, "Error:") == NULL)
|
|
|
|
break;
|
|
|
|
}
|
2017-05-08 19:16:58 +02:00
|
|
|
}
|
|
|
|
unlink(MANAGERAPK);
|
|
|
|
}
|
|
|
|
|
2017-04-30 19:58:52 +02:00
|
|
|
// All boot stage done, cleanup everything
|
|
|
|
free(buf);
|
|
|
|
free(buf2);
|
2017-07-10 19:53:45 +02:00
|
|
|
buf = buf2 = NULL;
|
2017-04-30 19:58:52 +02:00
|
|
|
vec_deep_destroy(&module_list);
|
2017-06-07 21:20:49 +02:00
|
|
|
|
2017-10-08 23:05:52 +02:00
|
|
|
stop_debug_full_log();
|
2017-04-30 19:58:52 +02:00
|
|
|
}
|