From bdcb813ee66a0b96c38bd04dbb2ad6a9e99c5fc9 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Sat, 15 Apr 2017 18:33:16 +0800 Subject: [PATCH] Add block rw support --- jni/daemon/daemon.c | 3 +++ jni/utils/misc.c | 32 ++++++++++++++++++++++++++++++-- jni/utils/utils.h | 1 + 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/jni/daemon/daemon.c b/jni/daemon/daemon.c index 7a8241ff3..8868b3a37 100644 --- a/jni/daemon/daemon.c +++ b/jni/daemon/daemon.c @@ -104,6 +104,9 @@ void start_daemon() { // Start log monitor monitor_logs(); + // Unlock all blocks for rw + unlock_blocks(); + // Loop forever to listen to requests while(1) { request_handler(xaccept(fd, NULL, NULL)); diff --git a/jni/utils/misc.c b/jni/utils/misc.c index 292c9b385..a986c0074 100644 --- a/jni/utils/misc.c +++ b/jni/utils/misc.c @@ -9,6 +9,8 @@ #include #include #include +#include +#include #include "magisk.h" #include "utils.h" @@ -139,14 +141,40 @@ void ps_filter_proc_name(const char *pattern, void (*func)(int)) { int create_links(const char *bin, const char *path) { char self[PATH_MAX], linkpath[PATH_MAX]; if (bin == NULL) { - xreadlink("/proc/self/exe", self, PATH_MAX); + xreadlink("/proc/self/exe", self, sizeof(self)); bin = self; } int ret = 0; for (int i = 0; applet[i]; ++i) { - snprintf(linkpath, PATH_MAX, "%s/%s", path, applet[i]); + snprintf(linkpath, sizeof(linkpath), "%s/%s", path, applet[i]); unlink(linkpath); ret |= symlink(bin, linkpath); } return ret; } + +#define DEV_BLOCK "/dev/block" + +void unlock_blocks() { + char path[PATH_MAX]; + DIR *dir; + struct dirent *entry; + int fd, OFF = 0; + + if (!(dir = xopendir(DEV_BLOCK))) + return; + + while((entry = readdir(dir))) { + if (entry->d_type == DT_BLK && strstr(entry->d_name, "mmc") != NULL) { + snprintf(path, sizeof(path), "%s/%s", DEV_BLOCK, entry->d_name); + if ((fd = xopen(path, O_RDONLY)) < 0) + continue; + + if (ioctl(fd, BLKROSET, &OFF) == -1) + PLOGE("ioctl"); + close(fd); + } + } + + closedir(dir); +} diff --git a/jni/utils/utils.h b/jni/utils/utils.h index fdf721e41..e7eb8af0b 100644 --- a/jni/utils/utils.h +++ b/jni/utils/utils.h @@ -61,5 +61,6 @@ ssize_t fdreadline(int fd, char *buf, size_t size); 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(); #endif