Magisk/jni/daemon/post_fs_data.c

85 lines
1.8 KiB
C
Raw Normal View History

/* post_fs_data.c - post-fs-data actions
*/
2017-04-17 08:36:49 +00:00
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
2017-04-17 08:36:49 +00:00
#include <fcntl.h>
#include <string.h>
#include <linux/loop.h>
#include <sys/ioctl.h>
#include <sys/mount.h>
2017-04-17 08:36:49 +00:00
#include "magisk.h"
#include "utils.h"
#include "daemon.h"
2017-04-17 08:36:49 +00:00
#include "resetprop.h"
static char *loopsetup(const char *img) {
char device[20];
struct loop_info64 info;
int i, lfd, ffd;
2017-04-21 22:33:40 +00:00
memset(&info, 0, sizeof(info));
2017-04-17 08:36:49 +00:00
// 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;
2017-04-21 22:33:40 +00:00
strcpy((char *) info.lo_file_name, img);
ioctl(lfd, LOOP_SET_STATUS64, &info);
2017-04-17 08:36:49 +00:00
return strdup(device);
}
2017-04-21 22:33:40 +00:00
static void *start_magisk_hide(void *args) {
// Setup default error handler for thread
err_handler = exit_thread;
launch_magiskhide(-1);
return NULL;
}
2017-04-17 08:36:49 +00:00
char *mount_image(const char *img, const char *target) {
char *device = loopsetup(img);
if (device)
2017-04-18 11:32:50 +00:00
xmount(device, target, "ext4", 0, NULL);
2017-04-17 08:36:49 +00:00
return device;
}
void post_fs_data(int client) {
// ack
write_int(client, 0);
close(client);
2017-04-17 08:36:49 +00:00
if (!check_data())
goto unblock;
LOGI("** post-fs-data mode running\n");
LOGI("* Mounting magisk.img\n");
// Mounting magisk image
char *magiskimg = mount_image("/data/magisk.img", "/magisk");
free(magiskimg);
2017-04-21 22:33:40 +00:00
// TODO: Magic Mounts, modules etc.
// Run common scripts
exec_common_script("post-fs-data");
2017-04-17 08:36:49 +00:00
// Start magiskhide if enabled
char *hide_prop = getprop("persist.magisk.hide");
if (hide_prop) {
2017-04-21 22:33:40 +00:00
if (strcmp(hide_prop, "1") == 0) {
pthread_t thread;
xpthread_create(&thread, NULL, start_magisk_hide, NULL);
}
2017-04-17 08:36:49 +00:00
free(hide_prop);
}
unblock:
unblock_boot_process();
}