Commit a31c1e80 authored by topjohnwu's avatar topjohnwu

post-fs-data mode done

parent 21891230
This diff is collapsed.
......@@ -8,7 +8,6 @@
#include <stdio.h>
#include <limits.h>
#include <pthread.h>
#include <unistd.h>
#include "magisk.h"
#include "utils.h"
......@@ -19,8 +18,8 @@ static void *logger_thread(void *args) {
err_handler = exit_thread;
char buffer[PATH_MAX];
rename("/cache/magisk.log", "/cache/last_magisk.log");
FILE *logfile = xfopen("/cache/magisk.log", "w");
rename(LOGFILE, LASTLOG);
FILE *logfile = xfopen(LOGFILE, "w");
// Disable buffering
setbuf(logfile, NULL);
// Start logcat
......@@ -30,7 +29,6 @@ static void *logger_thread(void *args) {
while (fdgets(buffer, sizeof(buffer), fd)) {
fprintf(logfile, "%s", buffer);
}
close(fd);
return NULL;
}
......
......@@ -24,6 +24,18 @@
#define ARG_MAX 4096
#endif
#define LOGFILE "/cache/magisk.log"
#define LASTLOG "/cache/last_magisk.log"
#define UNBLOCKFILE "/dev/.magisk.unblock"
#define DISABLEFILE "/cache/.disable_magisk"
#define UNINSTALLER "/cache/magisk_uninstaller.sh"
#define MOUNTPOINT "/magisk"
#define MAINIMG "/data/magisk.img"
#define DATABIN "/data/magisk"
#define MAGISKTMP "/dev/magisk"
#define MIRRDIR MAGISKTMP "/mirror"
#define DUMMDIR MAGISKTMP "/dummy"
#define SELINUX_PATH "/sys/fs/selinux/"
#define SELINUX_ENFORCE SELINUX_PATH "enforce"
#define SELINUX_POLICY SELINUX_PATH "policy"
......
......@@ -14,6 +14,8 @@
#include <sys/ioctl.h>
#include <sys/mount.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <selinux/selinux.h>
#include "magisk.h"
#include "utils.h"
......@@ -204,11 +206,6 @@ void unlock_blocks() {
closedir(dir);
}
void unblock_boot_process() {
int fd = open("/dev/.magisk.unblock", O_RDONLY | O_CREAT);
close(fd);
}
void setup_sighandlers(void (*handler)(int)) {
struct sigaction act;
memset(&act, 0, sizeof(act));
......@@ -276,3 +273,110 @@ int bind_mount(const char *from, const char *to) {
int open_new(const char *filename) {
return xopen(filename, O_WRONLY | O_CREAT | O_TRUNC, 0644);
}
// file/link -> file/link only!!
int cp_afc(const char *source, const char *target) {
struct stat buf;
xlstat(source, &buf);
unlink(target);
char *con;
if (S_ISREG(buf.st_mode)) {
int sfd, tfd;
sfd = xopen(source, O_RDONLY);
tfd = xopen(target, O_WRONLY | O_CREAT | O_TRUNC);
xsendfile(tfd, sfd, NULL, buf.st_size);
fchmod(tfd, buf.st_mode & 0777);
fchown(tfd, buf.st_uid, buf.st_gid);
fgetfilecon(sfd, &con);
fsetfilecon(tfd, con);
free(con);
close(sfd);
close(tfd);
} else if (S_ISLNK(buf.st_mode)) {
char buffer[PATH_MAX];
xreadlink(source, buffer, sizeof(buffer));
xsymlink(buffer, target);
lgetfilecon(source, &con);
lsetfilecon(target, con);
free(con);
} else {
return 1;
}
return 0;
}
int clone_dir(const char *source, const char *target) {
DIR *dir;
struct dirent *entry;
char *s_path, *t_path, *con;
if (!(dir = xopendir(source)))
return 1;
s_path = xmalloc(PATH_MAX);
t_path = xmalloc(PATH_MAX);
struct stat buf;
xstat(source, &buf);
mkdir_p(target, buf.st_mode & 0777);
xchmod(target, buf.st_mode & 0777);
lgetfilecon(source, &con);
lsetfilecon(target, con);
free(con);
while ((entry = xreaddir(dir))) {
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
continue;
snprintf(s_path, PATH_MAX, "%s/%s", source, entry->d_name);
snprintf(t_path, PATH_MAX, "%s/%s", target, entry->d_name);
switch (entry->d_type) {
case DT_DIR:
clone_dir(s_path, t_path);
break;
case DT_REG:
case DT_LNK:
cp_afc(s_path, t_path);
break;
}
}
free(s_path);
free(t_path);
closedir(dir);
return 0;
}
int rm_rf(const char *target) {
struct stat buf;
xlstat(target, &buf);
char *next;
if (S_ISDIR(buf.st_mode)) {
DIR *dir;
struct dirent *entry;
if (!(dir = xopendir(target)))
return 1;
next = xmalloc(PATH_MAX);
while ((entry = xreaddir(dir))) {
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
continue;
snprintf(next, PATH_MAX, "%s/%s", target, entry->d_name);
switch (entry->d_type) {
case DT_DIR:
rm_rf(next);
break;
case DT_REG:
case DT_LNK:
unlink(next);
break;
}
}
free(next);
closedir(dir);
rmdir(target);
} else if (S_ISREG(buf.st_mode) || S_ISLNK(buf.st_mode)) {
unlink(target);
} else {
return 1;
}
return 0;
}
......@@ -49,12 +49,15 @@ int xpthread_create(pthread_t *thread, const pthread_attr_t *attr,
void *(*start_routine) (void *), void *arg);
int xsocketpair(int domain, int type, int protocol, int sv[2]);
int xstat(const char *pathname, struct stat *buf);
int xlstat(const char *pathname, struct stat *buf);
int xdup2(int oldfd, int newfd);
ssize_t xreadlink(const char *pathname, char *buf, size_t bufsiz);
int xsymlink(const char *target, const char *linkpath);
int xmount(const char *source, const char *target,
const char *filesystemtype, unsigned long mountflags,
const void *data);
int xumount(const char *target);
int xumount2(const char *target, int flags);
int xchmod(const char *pathname, mode_t mode);
int xrename(const char *oldpath, const char *newpath);
int xmkdir(const char *pathname, mode_t mode);
......@@ -77,11 +80,13 @@ void ps(void (*func)(int));
void ps_filter_proc_name(const char *filter, void (*func)(int));
int create_links(const char *bin, const char *path);
void unlock_blocks();
void unblock_boot_process();
void setup_sighandlers(void (*handler)(int));
int run_command(int *fd, const char *path, char *const argv[]);
int mkdir_p(const char *pathname, mode_t mode);
int bind_mount(const char *from, const char *to);
int open_new(const char *filename);
int cp_afc(const char *source, const char *target);
int clone_dir(const char *source, const char *target);
int rm_rf(const char *target);
#endif
......@@ -220,6 +220,14 @@ int xstat(const char *pathname, struct stat *buf) {
return ret;
}
int xlstat(const char *pathname, struct stat *buf) {
int ret = lstat(pathname, buf);
if (ret == -1) {
PLOGE("lstat %s", pathname);
}
return ret;
}
int xdup2(int oldfd, int newfd) {
int ret = dup2(oldfd, newfd);
if (ret == -1) {
......@@ -257,6 +265,22 @@ int xmount(const char *source, const char *target,
return ret;
}
int xumount(const char *target) {
int ret = umount(target);
if (ret == -1) {
PLOGE("umount %s", target);
}
return ret;
}
int xumount2(const char *target, int flags) {
int ret = umount2(target, flags);
if (ret == -1) {
PLOGE("umount2 %s", target);
}
return ret;
}
int xchmod(const char *pathname, mode_t mode) {
int ret = chmod(pathname, mode);
if (ret == -1) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment