post-fs-data mode done

This commit is contained in:
topjohnwu
2017-05-04 01:13:04 +08:00
parent 21891230f2
commit a31c1e8084
6 changed files with 361 additions and 48 deletions

View File

@ -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;
}

View File

@ -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

View File

@ -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) {