Implement Magic Mount

This commit is contained in:
topjohnwu
2017-05-01 01:58:52 +08:00
parent d3d5703f3f
commit e16d604d0d
19 changed files with 581 additions and 203 deletions
+31 -27
View File
@@ -9,6 +9,7 @@
#include <fcntl.h>
#include <pwd.h>
#include <signal.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/mount.h>
@@ -238,10 +239,9 @@ int run_command(int *fd, const char *path, char *const argv[]) {
xdup2(sv[0], STDOUT_FILENO);
xdup2(sv[0], STDERR_FILENO);
} else {
int null = open("/dev/null", O_RDWR);
xdup2(null, STDIN_FILENO);
xdup2(null, STDOUT_FILENO);
xdup2(null, STDERR_FILENO);
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
}
execv(path, argv);
@@ -249,29 +249,33 @@ int run_command(int *fd, const char *path, char *const argv[]) {
return -1;
}
#define MAGISK_CORE "/magisk/.core/"
void exec_common_script(const char* stage) {
DIR *dir;
struct dirent *entry;
char buf[PATH_MAX];
snprintf(buf, sizeof(buf), MAGISK_CORE "%s.d", stage);
if (!(dir = opendir(buf)))
return;
while ((entry = xreaddir(dir))) {
if (entry->d_type == DT_REG) {
snprintf(buf, sizeof(buf), MAGISK_CORE "%s.d/%s", stage, entry->d_name);
if (access(buf, X_OK) == -1)
continue;
LOGI("%s.d: exec [%s]\n", stage, entry->d_name);
char *const command[] = { "sh", buf, NULL };
int pid = run_command(NULL, "/system/bin/sh", command);
if (pid != -1)
waitpid(pid, NULL, 0);
int mkdir_p(const char *pathname, mode_t mode) {
char *path = strdup(pathname), *p;
errno = 0;
for (p = path + 1; *p; ++p) {
if (*p == '/') {
*p = '\0';
if (mkdir(path, mode) == -1) {
if (errno != EEXIST)
return -1;
}
*p = '/';
}
}
closedir(dir);
if (mkdir(path, mode) == -1) {
if (errno != EEXIST)
return -1;
}
free(path);
return 0;
}
int bind_mount(const char *from, const char *to) {
int ret = xmount(from, to, NULL, MS_BIND, NULL);
LOGD("bind_mount: %s -> %s\n", from, to);
return ret;
}
int open_new(const char *filename) {
return xopen(filename, O_WRONLY | O_CREAT | O_TRUNC, 0644);
}
+4 -1
View File
@@ -61,6 +61,7 @@ int xmkdir(const char *pathname, mode_t mode);
void *xmmap(void *addr, size_t length, int prot, int flags,
int fd, off_t offset);
ssize_t xsendfile(int out_fd, int in_fd, off_t *offset, size_t count);
int xmkdir_p(const char *pathname, mode_t mode);
// misc.c
@@ -79,6 +80,8 @@ void unlock_blocks();
void unblock_boot_process();
void setup_sighandlers(void (*handler)(int));
int run_command(int *fd, const char *path, char *const argv[]);
void exec_common_script(const char* stage);
int mkdir_p(const char *pathname, mode_t mode);
int bind_mount(const char *from, const char *to);
int open_new(const char *filename);
#endif
+4 -4
View File
@@ -21,11 +21,11 @@ void vec_deep_destroy(struct vector *v);
#define vec_entry(v) (v)->data
/* Usage: vec_for_each(vector *v, void *e) */
#define vec_for_each(v, e) \
e = (v)->data[0]; \
for (size_t _ = 0; _ < (v)->size; ++_, e = (v)->data[_])
e = v ? (v)->data[0] : NULL; \
for (size_t _ = 0; v && _ < (v)->size; ++_, e = (v)->data[_])
#define vec_for_each_r(v, e) \
e = (v)->data[(v)->size - 1]; \
for (size_t _ = (v)->size; _ > 0; --_, e = (v)->data[_ - 1])
e = v ? (v)->data[(v)->size - 1] : NULL; \
for (size_t _ = (v)->size; v && _ > 0; --_, e = (v)->data[_ - 1])
#endif
+7 -1
View File
@@ -298,4 +298,10 @@ ssize_t xsendfile(int out_fd, int in_fd, off_t *offset, size_t count) {
return ret;
}
int xmkdir_p(const char *pathname, mode_t mode) {
int ret = mkdir_p(pathname, mode);
if (ret == -1) {
PLOGE("mkdir_p %s", pathname);
}
return ret;
}