2017-04-30 17:58:52 +00:00
|
|
|
/* post_fs_data.c - post-fs-data actions
|
|
|
|
*/
|
|
|
|
|
|
|
|
#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 18:39:53 +00:00
|
|
|
#include <selinux/selinux.h>
|
2017-04-30 17:58:52 +00:00
|
|
|
|
|
|
|
#include "magisk.h"
|
|
|
|
#include "utils.h"
|
|
|
|
#include "daemon.h"
|
|
|
|
#include "resetprop.h"
|
|
|
|
|
|
|
|
static char *buf, *buf2;
|
|
|
|
static struct vector module_list;
|
|
|
|
|
2017-06-08 11:15:51 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
static int debug_log_pid, debug_log_fd;
|
|
|
|
#endif
|
|
|
|
|
2017-04-30 17:58:52 +00:00
|
|
|
/******************
|
|
|
|
* Node structure *
|
|
|
|
******************/
|
|
|
|
|
2017-05-08 17:09:32 +00:00
|
|
|
// Precedence: MODULE > SKEL > DUMMY
|
2017-04-30 17:58:52 +00:00
|
|
|
#define DO_NOTHING 0x0 /* intermediate node */
|
2017-05-08 17:09:32 +00:00
|
|
|
#define IS_DUMMY 0x1 /* mount from mirror */
|
|
|
|
#define IS_SKEL 0x2 /* mount from skeleton */
|
|
|
|
#define IS_MODULE 0x4 /* mount from module */
|
2017-04-30 17:58:52 +00:00
|
|
|
|
|
|
|
struct node_entry {
|
2017-05-08 17:09:32 +00:00
|
|
|
const char *module; /* Only used when status & IS_MODULE */
|
2017-04-30 17:58:52 +00:00
|
|
|
char *name;
|
|
|
|
uint8_t type;
|
|
|
|
uint8_t status;
|
|
|
|
struct node_entry *parent;
|
|
|
|
struct vector *children;
|
|
|
|
};
|
|
|
|
|
2017-05-08 17:09:32 +00: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 17:58:52 +00:00
|
|
|
/******************
|
|
|
|
* Image handling *
|
|
|
|
******************/
|
|
|
|
|
|
|
|
static char *loopsetup(const char *img) {
|
|
|
|
char device[20];
|
|
|
|
struct loop_info64 info;
|
|
|
|
int i, lfd, ffd;
|
|
|
|
memset(&info, 0, sizeof(info));
|
|
|
|
// First get an empty loop device
|
|
|
|
for (i = 0; i <= 7; ++i) {
|
|
|
|
sprintf(device, "/dev/block/loop%d", i);
|
|
|
|
lfd = xopen(device, O_RDWR);
|
|
|
|
if (ioctl(lfd, LOOP_GET_STATUS64, &info) == -1)
|
|
|
|
break;
|
|
|
|
close(lfd);
|
|
|
|
}
|
|
|
|
if (i == 8) return NULL;
|
|
|
|
ffd = xopen(img, O_RDWR);
|
|
|
|
if (ioctl(lfd, LOOP_SET_FD, ffd) == -1)
|
|
|
|
return NULL;
|
|
|
|
strcpy((char *) info.lo_file_name, img);
|
|
|
|
ioctl(lfd, LOOP_SET_STATUS64, &info);
|
2017-05-01 20:55:55 +00:00
|
|
|
close(lfd);
|
|
|
|
close(ffd);
|
2017-04-30 17:58:52 +00:00
|
|
|
return strdup(device);
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *mount_image(const char *img, const char *target) {
|
|
|
|
char *device = loopsetup(img);
|
|
|
|
if (device)
|
|
|
|
xmount(device, target, "ext4", 0, NULL);
|
|
|
|
return device;
|
|
|
|
}
|
|
|
|
|
2017-05-03 17:13:04 +00:00
|
|
|
static void umount_image(const char *target, const char *device) {
|
|
|
|
xumount(target);
|
|
|
|
int fd = xopen(device, O_RDWR);
|
|
|
|
ioctl(fd, LOOP_CLR_FD);
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
2017-05-26 14:05:04 +00:00
|
|
|
#define round_size(a) ((((a) / 32) + 2) * 32)
|
2017-06-11 12:20:24 +00:00
|
|
|
#define SOURCE_TMP "/dev/source"
|
|
|
|
#define TARGET_TMP "/dev/target"
|
2017-05-03 17:13:04 +00:00
|
|
|
|
|
|
|
static int merge_img(const char *source, const char *target) {
|
|
|
|
if (access(source, F_OK) == -1)
|
|
|
|
return 0;
|
|
|
|
if (access(target, F_OK) == -1) {
|
|
|
|
rename(source, target);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// resize target to worst case
|
|
|
|
int s_used, s_total, t_used, t_total, n_total;
|
2017-06-11 08:51:44 +00:00
|
|
|
if (get_img_size(source, &s_used, &s_total)) return 1;
|
|
|
|
if (get_img_size(target, &t_used, &t_total)) return 1;
|
2017-05-03 17:13:04 +00:00
|
|
|
n_total = round_size(s_used + t_used);
|
2017-06-11 12:20:24 +00:00
|
|
|
if (n_total != t_total)
|
|
|
|
resize_img(target, n_total);
|
2017-05-03 17:13:04 +00:00
|
|
|
|
2017-06-11 12:20:24 +00:00
|
|
|
mkdir(SOURCE_TMP, 0755);
|
|
|
|
mkdir(TARGET_TMP, 0755);
|
2017-05-03 17:13:04 +00:00
|
|
|
char *s_loop, *t_loop;
|
2017-06-11 12:20:24 +00:00
|
|
|
s_loop = mount_image(source, SOURCE_TMP);
|
2017-05-03 17:13:04 +00:00
|
|
|
if (s_loop == NULL) return 1;
|
2017-06-11 12:20:24 +00:00
|
|
|
t_loop = mount_image(target, TARGET_TMP);
|
2017-05-03 17:13:04 +00:00
|
|
|
if (t_loop == NULL) return 1;
|
|
|
|
|
|
|
|
DIR *dir;
|
|
|
|
struct dirent *entry;
|
2017-06-11 12:20:24 +00:00
|
|
|
if (!(dir = opendir(SOURCE_TMP)))
|
2017-05-03 17:13:04 +00:00
|
|
|
return 1;
|
|
|
|
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;
|
|
|
|
// Cleanup old module
|
2017-06-11 12:20:24 +00:00
|
|
|
snprintf(buf, PATH_MAX, "/dev/target/%s", entry->d_name);
|
2017-05-03 17:13:04 +00:00
|
|
|
if (access(buf, F_OK) == 0) {
|
2017-06-11 08:51:44 +00:00
|
|
|
LOGI("Upgrade module: %s\n", entry->d_name);
|
2017-05-03 17:13:04 +00:00
|
|
|
rm_rf(buf);
|
2017-06-11 08:51:44 +00:00
|
|
|
} else {
|
|
|
|
LOGI("New module: %s\n", entry->d_name);
|
2017-05-03 17:13:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir(dir);
|
2017-06-11 12:20:24 +00:00
|
|
|
clone_dir(SOURCE_TMP, TARGET_TMP);
|
2017-05-03 17:13:04 +00:00
|
|
|
|
|
|
|
// Unmount all loop devices
|
2017-06-11 12:20:24 +00:00
|
|
|
umount_image(SOURCE_TMP, s_loop);
|
|
|
|
umount_image(TARGET_TMP, t_loop);
|
|
|
|
rmdir(SOURCE_TMP);
|
|
|
|
rmdir(TARGET_TMP);
|
2017-05-03 17:13:04 +00:00
|
|
|
free(s_loop);
|
|
|
|
free(t_loop);
|
|
|
|
unlink(source);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void trim_img(const char *img) {
|
|
|
|
int used, total, new_size;
|
|
|
|
get_img_size(img, &used, &total);
|
|
|
|
new_size = round_size(used);
|
2017-05-26 14:05:04 +00:00
|
|
|
if (new_size != total)
|
2017-05-03 17:13:04 +00:00
|
|
|
resize_img(img, new_size);
|
|
|
|
}
|
|
|
|
|
2017-04-30 17:58:52 +00:00
|
|
|
/***********
|
|
|
|
* Scripts *
|
|
|
|
***********/
|
|
|
|
|
|
|
|
void exec_common_script(const char* stage) {
|
|
|
|
DIR *dir;
|
|
|
|
struct dirent *entry;
|
2017-05-03 19:05:37 +00:00
|
|
|
snprintf(buf, PATH_MAX, "%s/%s.d", COREDIR, stage);
|
2017-04-30 17:58:52 +00:00
|
|
|
|
|
|
|
if (!(dir = opendir(buf)))
|
|
|
|
return;
|
|
|
|
|
|
|
|
while ((entry = xreaddir(dir))) {
|
|
|
|
if (entry->d_type == DT_REG) {
|
2017-05-04 20:16:00 +00:00
|
|
|
snprintf(buf2, PATH_MAX, "%s/%s", buf, entry->d_name);
|
|
|
|
if (access(buf2, X_OK) == -1)
|
2017-04-30 17:58:52 +00:00
|
|
|
continue;
|
|
|
|
LOGI("%s.d: exec [%s]\n", stage, entry->d_name);
|
2017-05-04 20:16:00 +00:00
|
|
|
char *const command[] = { "sh", buf2, NULL };
|
2017-06-07 19:20:49 +00:00
|
|
|
int pid = run_command(0, NULL, "/system/bin/sh", command);
|
2017-04-30 17:58:52 +00:00
|
|
|
if (pid != -1)
|
|
|
|
waitpid(pid, NULL, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
closedir(dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
void exec_module_script(const char* stage) {
|
|
|
|
char *module;
|
|
|
|
vec_for_each(&module_list, module) {
|
2017-05-03 17:13:04 +00:00
|
|
|
snprintf(buf, PATH_MAX, "%s/%s/%s.sh", MOUNTPOINT, module, stage);
|
2017-04-30 17:58:52 +00:00
|
|
|
if (access(buf, F_OK) == -1)
|
|
|
|
continue;
|
|
|
|
LOGI("%s: exec [%s.sh]\n", module, stage);
|
|
|
|
char *const command[] = { "sh", buf, NULL };
|
2017-06-07 19:20:49 +00:00
|
|
|
int pid = run_command(0, NULL, "/system/bin/sh", command);
|
2017-04-30 17:58:52 +00:00
|
|
|
if (pid != -1)
|
|
|
|
waitpid(pid, NULL, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/***************
|
|
|
|
* Magic Mount *
|
|
|
|
***************/
|
|
|
|
|
|
|
|
static char *get_full_path(struct node_entry *node) {
|
|
|
|
char buffer[PATH_MAX], temp[PATH_MAX];
|
|
|
|
// Concat the paths
|
|
|
|
struct node_entry *cur = node;
|
|
|
|
strcpy(buffer, node->name);
|
|
|
|
while (cur->parent) {
|
|
|
|
strcpy(temp, buffer);
|
|
|
|
snprintf(buffer, sizeof(buffer), "%s/%s", cur->parent->name, temp);
|
|
|
|
cur = cur->parent;
|
|
|
|
}
|
|
|
|
return strdup(buffer);
|
|
|
|
}
|
|
|
|
|
2017-05-08 17:09:32 +00:00
|
|
|
// Free the node
|
|
|
|
static void destroy_node(struct node_entry *node) {
|
|
|
|
free(node->name);
|
|
|
|
vec_destroy(node->children);
|
|
|
|
free(node->children);
|
|
|
|
free(node);
|
|
|
|
}
|
|
|
|
|
2017-05-04 20:16:00 +00:00
|
|
|
// Free the node and all children recursively
|
2017-04-30 17:58:52 +00:00
|
|
|
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);
|
|
|
|
}
|
2017-05-08 17:09:32 +00:00
|
|
|
destroy_node(node);
|
2017-04-30 17:58:52 +00:00
|
|
|
}
|
|
|
|
|
2017-05-08 17:09:32 +00:00
|
|
|
// Return the child
|
|
|
|
static struct node_entry *insert_child(struct node_entry *p, struct node_entry *c) {
|
2017-04-30 17:58:52 +00:00
|
|
|
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) {
|
2017-05-08 17:09:32 +00:00
|
|
|
// Exist duplicate
|
|
|
|
if (c->status > e->status) {
|
|
|
|
// Precedence is higher, replace with new node
|
|
|
|
c->children = e->children; // Preserve all children
|
|
|
|
free(e->name);
|
|
|
|
free(e);
|
|
|
|
vec_entry(p->children)[_] = c;
|
|
|
|
return c;
|
|
|
|
} else {
|
|
|
|
// Free the new entry, return old
|
|
|
|
destroy_node(c);
|
|
|
|
return e;
|
|
|
|
}
|
2017-04-30 17:58:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// New entry, push back
|
|
|
|
vec_push_back(p->children, c);
|
2017-05-08 17:09:32 +00:00
|
|
|
return c;
|
2017-04-30 17:58:52 +00:00
|
|
|
}
|
|
|
|
|
2017-05-08 17:09:32 +00:00
|
|
|
static void construct_tree(const char *module, struct node_entry *parent) {
|
2017-04-30 17:58:52 +00:00
|
|
|
DIR *dir;
|
|
|
|
struct dirent *entry;
|
|
|
|
struct node_entry *node;
|
|
|
|
|
2017-05-08 17:09:32 +00:00
|
|
|
char *parent_path = get_full_path(parent);
|
|
|
|
snprintf(buf, PATH_MAX, "%s/%s%s", MOUNTPOINT, module, parent_path);
|
2017-04-30 17:58:52 +00:00
|
|
|
|
2017-05-03 18:39:53 +00:00
|
|
|
if (!(dir = opendir(buf)))
|
2017-05-08 17:09:32 +00:00
|
|
|
goto cleanup;
|
2017-04-30 17:58:52 +00: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 17:09:32 +00:00
|
|
|
snprintf(buf, PATH_MAX, "%s/%s", parent_path, node->name);
|
2017-04-30 17:58:52 +00:00
|
|
|
// Check if the entry has a correspond target
|
2017-05-08 17:09:32 +00:00
|
|
|
if (IS_LNK(node) || access(buf, F_OK) == -1) {
|
2017-04-30 17:58:52 +00:00
|
|
|
// Mark the parent folder as a skeleton
|
2017-05-08 17:09:32 +00:00
|
|
|
parent->status |= IS_SKEL;
|
|
|
|
node->status |= IS_MODULE;
|
|
|
|
} 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
|
|
|
|
node->status |= IS_MODULE;
|
2017-04-30 17:58:52 +00:00
|
|
|
}
|
2017-05-08 17:09:32 +00:00
|
|
|
} else if (IS_REG(node)) {
|
|
|
|
// This is a leaf, mark as target
|
|
|
|
node->status |= IS_MODULE;
|
|
|
|
}
|
|
|
|
node = insert_child(parent, node);
|
2017-05-26 14:58:55 +00:00
|
|
|
if (IS_DIR(node) && !(node->status & IS_MODULE)) {
|
2017-05-08 17:09:32 +00:00
|
|
|
// Intermediate node, travel deeper
|
|
|
|
construct_tree(module, node);
|
2017-04-30 17:58:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
closedir(dir);
|
2017-05-08 17:09:32 +00:00
|
|
|
|
|
|
|
cleanup:
|
|
|
|
free(parent_path);
|
2017-04-30 17:58:52 +00:00
|
|
|
}
|
|
|
|
|
2017-05-08 17:09:32 +00:00
|
|
|
static void clone_skeleton(struct node_entry *node) {
|
2017-04-30 17:58:52 +00:00
|
|
|
DIR *dir;
|
|
|
|
struct dirent *entry;
|
|
|
|
struct node_entry *dummy, *child;
|
|
|
|
|
|
|
|
// Clone the structure
|
2017-05-08 17:09:32 +00:00
|
|
|
char *full_path = get_full_path(node);
|
|
|
|
if (!(dir = opendir(full_path)))
|
|
|
|
goto cleanup;
|
2017-04-30 17:58:52 +00:00
|
|
|
while ((entry = xreaddir(dir))) {
|
|
|
|
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
|
|
|
|
continue;
|
2017-05-08 17:09:32 +00:00
|
|
|
// Create dummy node
|
|
|
|
dummy = xcalloc(sizeof(*dummy), 1);
|
|
|
|
dummy->name = strdup(entry->d_name);
|
|
|
|
dummy->type = entry->d_type;
|
|
|
|
dummy->status |= IS_DUMMY;
|
|
|
|
insert_child(node, dummy);
|
2017-04-30 17:58:52 +00:00
|
|
|
}
|
2017-05-01 20:55:55 +00:00
|
|
|
closedir(dir);
|
2017-04-30 17:58:52 +00:00
|
|
|
|
2017-05-08 17:09:32 +00:00
|
|
|
snprintf(buf, PATH_MAX, "%s%s", DUMMDIR, full_path);
|
2017-05-04 20:16:00 +00:00
|
|
|
mkdir_p(buf, 0755);
|
2017-05-08 17:09:32 +00:00
|
|
|
clone_attr(full_path, buf);
|
|
|
|
bind_mount(buf, full_path);
|
2017-04-30 17:58:52 +00:00
|
|
|
|
|
|
|
vec_for_each(node->children, child) {
|
2017-05-08 17:09:32 +00:00
|
|
|
snprintf(buf, PATH_MAX, "%s%s/%s", DUMMDIR, full_path, child->name);
|
|
|
|
// Create the dummy file/directory
|
|
|
|
if (IS_DIR(child))
|
2017-04-30 17:58:52 +00:00
|
|
|
xmkdir(buf, 0755);
|
2017-05-08 17:09:32 +00:00
|
|
|
else if (IS_REG(child))
|
2017-04-30 17:58:52 +00:00
|
|
|
close(open_new(buf));
|
2017-05-08 17:09:32 +00:00
|
|
|
|
|
|
|
if (child->status & IS_MODULE) {
|
2017-04-30 17:58:52 +00:00
|
|
|
// Mount from module file to dummy file
|
2017-05-08 17:09:32 +00:00
|
|
|
snprintf(buf2, PATH_MAX, "%s/%s%s/%s", MOUNTPOINT, child->module, full_path, child->name);
|
|
|
|
} else if (child->status & IS_SKEL) {
|
2017-04-30 17:58:52 +00:00
|
|
|
// It's another skeleton, recursive call and end
|
2017-05-08 17:09:32 +00:00
|
|
|
clone_skeleton(child);
|
2017-04-30 17:58:52 +00:00
|
|
|
continue;
|
2017-05-08 17:09:32 +00: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 17:58:52 +00:00
|
|
|
}
|
2017-05-08 17:09:32 +00:00
|
|
|
|
|
|
|
if (IS_LNK(child)) {
|
2017-04-30 17:58:52 +00:00
|
|
|
// Symlink special treatments
|
|
|
|
char *temp = xmalloc(PATH_MAX);
|
|
|
|
xreadlink(buf2, temp, PATH_MAX);
|
|
|
|
symlink(temp, buf);
|
|
|
|
free(temp);
|
2017-05-03 18:39:53 +00:00
|
|
|
LOGI("cplink: %s -> %s\n", buf2, buf);
|
2017-04-30 17:58:52 +00:00
|
|
|
} else {
|
2017-05-08 17:09:32 +00:00
|
|
|
snprintf(buf, PATH_MAX, "%s/%s", full_path, child->name);
|
2017-04-30 17:58:52 +00:00
|
|
|
bind_mount(buf2, buf);
|
|
|
|
}
|
|
|
|
}
|
2017-05-08 17:09:32 +00:00
|
|
|
|
|
|
|
cleanup:
|
|
|
|
free(full_path);
|
2017-04-30 17:58:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void magic_mount(struct node_entry *node) {
|
|
|
|
char *real_path;
|
|
|
|
struct node_entry *child;
|
|
|
|
|
2017-05-03 17:13:04 +00:00
|
|
|
if (strcmp(node->name, "vendor") == 0 && strcmp(node->parent->name, "/system") == 0) {
|
|
|
|
snprintf(buf, PATH_MAX, "%s/%s/system/vendor", MOUNTPOINT, node->module);
|
|
|
|
snprintf(buf2, PATH_MAX, "%s/%s/vendor", MOUNTPOINT, node->module);
|
|
|
|
unlink(buf2);
|
|
|
|
symlink(buf, buf2);
|
2017-04-30 17:58:52 +00:00
|
|
|
return;
|
2017-05-03 17:13:04 +00:00
|
|
|
}
|
2017-04-30 17:58:52 +00:00
|
|
|
|
2017-05-08 17:09:32 +00:00
|
|
|
if (node->status) {
|
|
|
|
if (node->status & IS_MODULE) {
|
|
|
|
// The real deal, mount module item
|
|
|
|
real_path = get_full_path(node);
|
2017-05-03 17:13:04 +00:00
|
|
|
snprintf(buf, PATH_MAX, "%s/%s%s", MOUNTPOINT, node->module, real_path);
|
2017-04-30 17:58:52 +00:00
|
|
|
bind_mount(buf, real_path);
|
2017-05-08 17:09:32 +00:00
|
|
|
free(real_path);
|
|
|
|
} else if (node->status & IS_SKEL) {
|
|
|
|
// The node is labeled to be cloned with skeleton, lets do it
|
|
|
|
clone_skeleton(node);
|
2017-04-30 17:58:52 +00:00
|
|
|
}
|
2017-05-08 17:09:32 +00:00
|
|
|
// We should not see dummies, so no need to handle it here
|
|
|
|
} else {
|
|
|
|
// It's an intermediate node, travel deeper
|
|
|
|
vec_for_each(node->children, child)
|
|
|
|
magic_mount(child);
|
2017-04-30 17:58:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-03 18:39:53 +00: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
|
|
|
|
snprintf(buf, PATH_MAX, "%s/%s", buf, entry->d_name);
|
|
|
|
// Clone all attributes
|
2017-05-04 20:16:00 +00:00
|
|
|
clone_attr(buf2, buf);
|
2017-05-03 18:39:53 +00:00
|
|
|
// Finally, mount the file
|
|
|
|
bind_mount(buf, buf2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
closedir(dir);
|
|
|
|
}
|
|
|
|
|
2017-04-30 17:58:52 +00:00
|
|
|
/****************
|
|
|
|
* Entry points *
|
|
|
|
****************/
|
|
|
|
|
|
|
|
static void *start_magisk_hide(void *args) {
|
|
|
|
launch_magiskhide(-1);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-05-03 17:13:04 +00:00
|
|
|
static void unblock_boot_process() {
|
|
|
|
close(open(UNBLOCKFILE, O_RDONLY | O_CREAT));
|
|
|
|
pthread_exit(NULL);
|
|
|
|
}
|
|
|
|
|
2017-04-30 17:58:52 +00:00
|
|
|
void post_fs(int client) {
|
2017-05-03 17:13:04 +00:00
|
|
|
// Error handler
|
|
|
|
err_handler = unblock_boot_process;
|
2017-06-07 19:20:49 +00:00
|
|
|
|
|
|
|
// Start log monitor
|
|
|
|
monitor_logs();
|
|
|
|
|
2017-04-30 17:58:52 +00:00
|
|
|
LOGI("** post-fs mode running\n");
|
|
|
|
// ack
|
|
|
|
write_int(client, 0);
|
|
|
|
close(client);
|
|
|
|
|
2017-05-03 17:13:04 +00:00
|
|
|
// Uninstall or core only mode
|
|
|
|
if (access(UNINSTALLER, F_OK) == 0 || access(DISABLEFILE, F_OK) == 0)
|
|
|
|
goto unblock;
|
|
|
|
|
2017-04-30 17:58:52 +00:00
|
|
|
// Allocate buffer
|
|
|
|
buf = xmalloc(PATH_MAX);
|
|
|
|
buf2 = xmalloc(PATH_MAX);
|
|
|
|
|
2017-05-03 18:39:53 +00:00
|
|
|
simple_mount("/system");
|
|
|
|
simple_mount("/vendor");
|
|
|
|
|
2017-04-30 17:58:52 +00:00
|
|
|
unblock:
|
|
|
|
unblock_boot_process();
|
|
|
|
}
|
|
|
|
|
|
|
|
void post_fs_data(int client) {
|
2017-05-03 17:13:04 +00:00
|
|
|
// Error handler
|
|
|
|
err_handler = unblock_boot_process;
|
2017-06-08 11:15:51 +00:00
|
|
|
|
2017-04-30 17:58:52 +00:00
|
|
|
// ack
|
|
|
|
write_int(client, 0);
|
|
|
|
close(client);
|
|
|
|
if (!check_data())
|
|
|
|
goto unblock;
|
|
|
|
|
2017-06-08 11:15:51 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
// Start debug logs in new process
|
|
|
|
debug_log_fd = xopen(DEBUG_LOG, O_WRONLY | O_CREAT | O_CLOEXEC | O_TRUNC, 0644);
|
|
|
|
char *const command[] = { "logcat", "-v", "brief", NULL };
|
|
|
|
debug_log_pid = run_command(0, &debug_log_fd, "/system/bin/logcat", command);
|
|
|
|
#endif
|
|
|
|
|
2017-05-03 17:13:04 +00:00
|
|
|
LOGI("** post-fs-data mode running\n");
|
|
|
|
|
|
|
|
// uninstaller
|
|
|
|
if (access(UNINSTALLER, F_OK) == 0) {
|
|
|
|
close(open(UNBLOCKFILE, O_RDONLY | O_CREAT));
|
2017-06-03 16:35:45 +00:00
|
|
|
system("(BOOTMODE=true sh " UNINSTALLER ") &");
|
2017-05-03 17:13:04 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-04-30 17:58:52 +00:00
|
|
|
// Allocate buffer
|
|
|
|
if (buf == NULL) buf = xmalloc(PATH_MAX);
|
|
|
|
if (buf2 == NULL) buf2 = xmalloc(PATH_MAX);
|
|
|
|
|
2017-05-03 17:13:04 +00:00
|
|
|
// Cache support
|
|
|
|
if (access("/cache/data_bin", F_OK) == 0) {
|
|
|
|
rm_rf(DATABIN);
|
|
|
|
rename("/cache/data_bin", DATABIN);
|
|
|
|
}
|
|
|
|
|
2017-06-02 21:52:49 +00:00
|
|
|
// Magisk Manual Injector support
|
2017-06-11 08:51:44 +00:00
|
|
|
if (access("/data/local/tmp/magisk_inject", F_OK) == 0) {
|
2017-06-02 21:52:49 +00:00
|
|
|
rm_rf(DATABIN);
|
2017-06-11 08:51:44 +00:00
|
|
|
rename("/data/local/tmp/magisk_inject", DATABIN);
|
2017-06-02 21:52:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Lazy.... use shell blob
|
|
|
|
system("mv /data/magisk/stock_boot* /data;");
|
|
|
|
|
2017-05-03 17:13:04 +00:00
|
|
|
// Merge images
|
2017-06-11 08:51:44 +00:00
|
|
|
if (merge_img("/cache/magisk.img", MAINIMG)) {
|
|
|
|
LOGE("Image merge %s -> %s failed!\n", "/cache/magisk.img", MAINIMG);
|
2017-05-03 17:13:04 +00:00
|
|
|
goto unblock;
|
2017-06-11 08:51:44 +00:00
|
|
|
}
|
|
|
|
if (merge_img("/data/magisk_merge.img", MAINIMG)) {
|
|
|
|
LOGE("Image merge %s -> %s failed!\n", "/data/magisk_merge.img", MAINIMG);
|
2017-05-03 17:13:04 +00:00
|
|
|
goto unblock;
|
2017-06-11 08:51:44 +00:00
|
|
|
}
|
2017-05-03 17:13:04 +00:00
|
|
|
|
2017-06-02 21:52:49 +00:00
|
|
|
int new_img = 0;
|
|
|
|
|
|
|
|
if (access(MAINIMG, F_OK) == -1) {
|
|
|
|
if (create_img(MAINIMG, 64))
|
|
|
|
goto unblock;
|
|
|
|
new_img = 1;
|
|
|
|
}
|
2017-06-02 19:58:26 +00:00
|
|
|
|
2017-05-03 17:13:04 +00:00
|
|
|
LOGI("* Mounting " MAINIMG "\n");
|
2017-04-30 17:58:52 +00:00
|
|
|
// Mounting magisk image
|
2017-05-03 17:13:04 +00:00
|
|
|
char *magiskloop = mount_image(MAINIMG, MOUNTPOINT);
|
|
|
|
if (magiskloop == NULL)
|
|
|
|
goto unblock;
|
2017-04-30 17:58:52 +00:00
|
|
|
|
2017-06-02 21:52:49 +00:00
|
|
|
if (new_img) {
|
|
|
|
mkdir(COREDIR, 0755);
|
|
|
|
mkdir(COREDIR "/post-fs-data.d", 0755);
|
|
|
|
mkdir(COREDIR "/service.d", 0755);
|
|
|
|
mkdir(COREDIR "/props", 0755);
|
|
|
|
}
|
|
|
|
|
2017-04-30 17:58:52 +00:00
|
|
|
// Run common scripts
|
|
|
|
LOGI("* Running post-fs-data.d scripts\n");
|
|
|
|
exec_common_script("post-fs-data");
|
|
|
|
|
2017-05-03 17:13:04 +00:00
|
|
|
// Core only mode
|
|
|
|
if (access(DISABLEFILE, F_OK) == 0)
|
|
|
|
goto unblock;
|
|
|
|
|
2017-04-30 17:58:52 +00:00
|
|
|
DIR *dir;
|
|
|
|
struct dirent *entry;
|
|
|
|
char *module;
|
|
|
|
struct node_entry *sys_root, *ven_root = NULL, *child;
|
|
|
|
|
2017-05-03 18:39:53 +00:00
|
|
|
dir = xopendir(MOUNTPOINT);
|
2017-04-30 17:58:52 +00:00
|
|
|
|
|
|
|
// Create the system root entry
|
|
|
|
sys_root = xcalloc(sizeof(*sys_root), 1);
|
|
|
|
sys_root->name = strdup("/system");
|
|
|
|
|
|
|
|
int has_modules = 0;
|
|
|
|
|
|
|
|
// Travel through each modules
|
|
|
|
vec_init(&module_list);
|
|
|
|
LOGI("* Loading modules\n");
|
|
|
|
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;
|
2017-05-03 17:13:04 +00:00
|
|
|
snprintf(buf, PATH_MAX, "%s/%s", MOUNTPOINT, entry->d_name);
|
|
|
|
// Check whether remove
|
|
|
|
snprintf(buf2, PATH_MAX, "%s/remove", buf);
|
|
|
|
if (access(buf2, F_OK) == 0) {
|
|
|
|
rm_rf(buf);
|
|
|
|
continue;
|
|
|
|
}
|
2017-04-30 17:58:52 +00:00
|
|
|
// Check whether disable
|
2017-05-03 17:13:04 +00:00
|
|
|
snprintf(buf2, PATH_MAX, "%s/disable", buf);
|
|
|
|
if (access(buf2, F_OK) == 0)
|
2017-04-30 17:58:52 +00:00
|
|
|
continue;
|
|
|
|
// Add the module to list
|
|
|
|
module = strdup(entry->d_name);
|
|
|
|
vec_push_back(&module_list, module);
|
|
|
|
// Read props
|
2017-05-03 17:13:04 +00:00
|
|
|
snprintf(buf2, PATH_MAX, "%s/system.prop", buf);
|
|
|
|
if (access(buf2, F_OK) == 0) {
|
2017-04-30 17:58:52 +00:00
|
|
|
LOGI("%s: loading [system.prop]\n", module);
|
2017-05-03 17:13:04 +00:00
|
|
|
read_prop_file(buf2, 0);
|
2017-04-30 17:58:52 +00:00
|
|
|
}
|
|
|
|
// Check whether enable auto_mount
|
2017-05-03 17:13:04 +00:00
|
|
|
snprintf(buf2, PATH_MAX, "%s/auto_mount", buf);
|
|
|
|
if (access(buf2, F_OK) == -1)
|
2017-04-30 17:58:52 +00:00
|
|
|
continue;
|
|
|
|
// Double check whether the system folder exists
|
2017-05-03 17:13:04 +00:00
|
|
|
snprintf(buf2, PATH_MAX, "%s/system", buf);
|
|
|
|
if (access(buf2, F_OK) == -1)
|
2017-04-30 17:58:52 +00:00
|
|
|
continue;
|
2017-05-03 17:13:04 +00:00
|
|
|
|
2017-04-30 17:58:52 +00:00
|
|
|
// Construct structure
|
|
|
|
has_modules = 1;
|
|
|
|
LOGI("%s: constructing magic mount structure\n", module);
|
2017-05-08 17:09:32 +00:00
|
|
|
construct_tree(module, sys_root);
|
2017-04-30 17:58:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
closedir(dir);
|
|
|
|
|
2017-05-03 17:13:04 +00:00
|
|
|
// Trim image
|
|
|
|
umount_image(MOUNTPOINT, magiskloop);
|
|
|
|
free(magiskloop);
|
|
|
|
trim_img(MAINIMG);
|
|
|
|
|
|
|
|
// Remount them back :)
|
|
|
|
magiskloop = mount_image(MAINIMG, MOUNTPOINT);
|
|
|
|
free(magiskloop);
|
|
|
|
|
2017-04-30 17:58:52 +00:00
|
|
|
if (has_modules) {
|
|
|
|
// Mount mirrors
|
|
|
|
LOGI("* Mounting system/vendor mirrors");
|
|
|
|
int seperate_vendor = 0;
|
|
|
|
struct vector mounts;
|
|
|
|
vec_init(&mounts);
|
|
|
|
file_to_vector("/proc/mounts", &mounts);
|
|
|
|
char *line;
|
|
|
|
vec_for_each(&mounts, line) {
|
|
|
|
if (strstr(line, " /system ")) {
|
2017-05-03 17:13:04 +00:00
|
|
|
sscanf(line, "%s", buf);
|
|
|
|
snprintf(buf2, PATH_MAX, "%s/system", MIRRDIR);
|
|
|
|
xmkdir_p(buf2, 0755);
|
|
|
|
xmount(buf, buf2, "ext4", MS_RDONLY, NULL);
|
2017-05-03 18:39:53 +00:00
|
|
|
LOGI("mount: %s -> %s\n", buf, buf2);
|
2017-04-30 17:58:52 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (strstr(line, " /vendor ")) {
|
|
|
|
seperate_vendor = 1;
|
2017-05-03 17:13:04 +00:00
|
|
|
sscanf(line, "%s", buf);
|
|
|
|
snprintf(buf2, PATH_MAX, "%s/vendor", MIRRDIR);
|
|
|
|
xmkdir_p(buf2, 0755);
|
|
|
|
xmount(buf, buf2, "ext4", MS_RDONLY, NULL);
|
2017-05-03 18:39:53 +00:00
|
|
|
LOGI("mount: %s -> %s\n", buf, buf2);
|
2017-04-30 17:58:52 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
vec_deep_destroy(&mounts);
|
|
|
|
if (!seperate_vendor) {
|
2017-05-03 17:13:04 +00:00
|
|
|
snprintf(buf, PATH_MAX, "%s/system/vendor", MIRRDIR);
|
|
|
|
snprintf(buf2, PATH_MAX, "%s/vendor", MIRRDIR);
|
|
|
|
symlink(buf, buf2);
|
2017-05-03 18:39:53 +00:00
|
|
|
LOGI("link: %s -> %s\n", buf, buf2);
|
2017-04-30 17:58:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Magic!!
|
|
|
|
|
|
|
|
magic_mount(sys_root);
|
|
|
|
// Get the vendor node if exists
|
|
|
|
vec_for_each(sys_root->children, child) {
|
|
|
|
if (strcmp(child->name, "vendor") == 0) {
|
|
|
|
ven_root = child;
|
|
|
|
free(ven_root->name);
|
|
|
|
ven_root->name = strdup("/vendor");
|
|
|
|
ven_root->parent = NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (ven_root)
|
|
|
|
magic_mount(ven_root);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cleanup memory
|
|
|
|
destroy_subtree(sys_root);
|
|
|
|
|
|
|
|
// Execute module scripts
|
|
|
|
LOGI("* Running module post-fs-data scripts\n");
|
|
|
|
exec_module_script("post-fs-data");
|
|
|
|
|
2017-05-03 19:05:37 +00:00
|
|
|
// Systemless hosts
|
|
|
|
if (access(HOSTSFILE, F_OK) == 0) {
|
|
|
|
LOGI("* Enabling systemless hosts file support");
|
|
|
|
bind_mount(HOSTSFILE, "/system/etc/hosts");
|
|
|
|
}
|
|
|
|
|
2017-04-30 17:58:52 +00:00
|
|
|
// Start magiskhide if enabled
|
2017-05-26 14:05:04 +00:00
|
|
|
char *hide_prop = getprop(MAGISKHIDE_PROP);
|
2017-04-30 17:58:52 +00:00
|
|
|
if (hide_prop) {
|
|
|
|
if (strcmp(hide_prop, "1") == 0) {
|
|
|
|
pthread_t thread;
|
|
|
|
xpthread_create(&thread, NULL, start_magisk_hide, NULL);
|
2017-05-04 20:16:00 +00:00
|
|
|
pthread_detach(thread);
|
2017-04-30 17:58:52 +00:00
|
|
|
}
|
|
|
|
free(hide_prop);
|
|
|
|
}
|
|
|
|
|
|
|
|
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 20:16:00 +00:00
|
|
|
// Allocate buffer
|
|
|
|
if (buf == NULL) buf = xmalloc(PATH_MAX);
|
|
|
|
if (buf2 == NULL) buf2 = xmalloc(PATH_MAX);
|
|
|
|
|
2017-04-30 17:58:52 +00:00
|
|
|
// Wait till the full patch is done
|
|
|
|
pthread_join(sepol_patch, NULL);
|
|
|
|
|
|
|
|
// Run scripts after full patch, most reliable way to run scripts
|
|
|
|
LOGI("* Running service.d scripts\n");
|
|
|
|
exec_common_script("service");
|
2017-06-11 08:51:44 +00:00
|
|
|
|
|
|
|
// Core only mode
|
|
|
|
if (access(DISABLEFILE, F_OK) == 0) {
|
|
|
|
setprop("ro.magisk.disable", "1");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-04-30 17:58:52 +00:00
|
|
|
LOGI("* Running module service scripts\n");
|
|
|
|
exec_module_script("service");
|
|
|
|
|
2017-05-08 17:16:58 +00:00
|
|
|
// Install Magisk Manager if exists
|
|
|
|
if (access(MANAGERAPK, F_OK) == 0) {
|
2017-06-11 08:51:44 +00:00
|
|
|
while (1) {
|
2017-05-08 17:16:58 +00:00
|
|
|
sleep(5);
|
|
|
|
char *const command[] = { "sh", "-c",
|
|
|
|
"CLASSPATH=/system/framework/pm.jar "
|
|
|
|
"/system/bin/app_process /system/bin "
|
|
|
|
"com.android.commands.pm.Pm install -r " MANAGERAPK, NULL };
|
2017-06-07 19:20:49 +00:00
|
|
|
int apk_res = 0, pid;
|
|
|
|
pid = run_command(1, &apk_res, "/system/bin/sh", command);
|
2017-05-08 17:16:58 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
unlink(MANAGERAPK);
|
|
|
|
}
|
|
|
|
|
2017-04-30 17:58:52 +00:00
|
|
|
// All boot stage done, cleanup everything
|
|
|
|
free(buf);
|
|
|
|
free(buf2);
|
|
|
|
vec_deep_destroy(&module_list);
|
2017-06-07 19:20:49 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
// Stop recording the boot logcat after every boot task is done
|
|
|
|
kill(debug_log_pid, SIGTERM);
|
|
|
|
waitpid(debug_log_pid, NULL, 0);
|
|
|
|
close(debug_log_fd);
|
|
|
|
#endif
|
2017-04-30 17:58:52 +00:00
|
|
|
}
|