Don't use system and collect logs

This commit is contained in:
topjohnwu 2017-06-11 20:20:24 +08:00
parent aced0632ec
commit a5aa1b3917
2 changed files with 42 additions and 28 deletions

View File

@ -91,6 +91,8 @@ static void umount_image(const char *target, const char *device) {
}
#define round_size(a) ((((a) / 32) + 2) * 32)
#define SOURCE_TMP "/dev/source"
#define TARGET_TMP "/dev/target"
static int merge_img(const char *source, const char *target) {
if (access(source, F_OK) == -1)
@ -105,20 +107,20 @@ static int merge_img(const char *source, const char *target) {
if (get_img_size(source, &s_used, &s_total)) return 1;
if (get_img_size(target, &t_used, &t_total)) return 1;
n_total = round_size(s_used + t_used);
if (n_total != t_total && resize_img(target, n_total))
return 1;
if (n_total != t_total)
resize_img(target, n_total);
mkdir("/cache/source", 0755);
mkdir("/cache/target", 0755);
mkdir(SOURCE_TMP, 0755);
mkdir(TARGET_TMP, 0755);
char *s_loop, *t_loop;
s_loop = mount_image(source, "/cache/source");
s_loop = mount_image(source, SOURCE_TMP);
if (s_loop == NULL) return 1;
t_loop = mount_image(target, "/cache/target");
t_loop = mount_image(target, TARGET_TMP);
if (t_loop == NULL) return 1;
DIR *dir;
struct dirent *entry;
if (!(dir = opendir("/cache/source")))
if (!(dir = opendir(SOURCE_TMP)))
return 1;
while ((entry = xreaddir(dir))) {
if (entry->d_type == DT_DIR) {
@ -128,7 +130,7 @@ static int merge_img(const char *source, const char *target) {
strcmp(entry->d_name, "lost+found") == 0)
continue;
// Cleanup old module
snprintf(buf, PATH_MAX, "/cache/target/%s", entry->d_name);
snprintf(buf, PATH_MAX, "/dev/target/%s", entry->d_name);
if (access(buf, F_OK) == 0) {
LOGI("Upgrade module: %s\n", entry->d_name);
rm_rf(buf);
@ -138,13 +140,13 @@ static int merge_img(const char *source, const char *target) {
}
}
closedir(dir);
clone_dir("/cache/source", "/cache/target");
clone_dir(SOURCE_TMP, TARGET_TMP);
// Unmount all loop devices
umount_image("/cache/source", s_loop);
umount_image("/cache/target", t_loop);
rmdir("/cache/source");
rmdir("/cache/target");
umount_image(SOURCE_TMP, s_loop);
umount_image(TARGET_TMP, t_loop);
rmdir(SOURCE_TMP);
rmdir(TARGET_TMP);
free(s_loop);
free(t_loop);
unlink(source);
@ -551,9 +553,6 @@ void post_fs_data(int client) {
new_img = 1;
}
// Initialize resetprop for the daemon
init_resetprop();
LOGI("* Mounting " MAINIMG "\n");
// Mounting magisk image
char *magiskloop = mount_image(MAINIMG, MOUNTPOINT);

View File

@ -419,12 +419,17 @@ int create_img(const char *img, int size) {
xwrite(fd, file_contexts, sizeof(file_contexts));
close(fd);
char command[PATH_MAX];
snprintf(command, sizeof(command),
"make_ext4fs -l %dM -a /magisk -S /dev/file_contexts_image %s", size, img);
int ret = system(command);
char buffer[PATH_MAX];
snprintf(buffer, sizeof(buffer),
"make_ext4fs -l %dM -a /magisk -S /dev/file_contexts_image %s; e2fsck -yf %s;", size, img, img);
char *const command[] = { "sh", "-c", buffer, NULL };
int pid, status;
pid = run_command(0, NULL, "/system/bin/sh", command);
if (pid == -1)
return 1;
waitpid(pid, &status, 0);
unlink("/dev/file_contexts_image");
return ret;
return WEXITSTATUS(status);
}
int get_img_size(const char *img, int *used, int *total) {
@ -433,22 +438,23 @@ int get_img_size(const char *img, int *used, int *total) {
char buffer[PATH_MAX];
snprintf(buffer, sizeof(buffer), "e2fsck -yf %s", img);
char *const command[] = { "sh", "-c", buffer, NULL };
int pid, fd = 0, ret = 1;
int pid, fd = 0, status = 1;
pid = run_command(1, &fd, "/system/bin/sh", command);
if (pid == -1)
return 1;
while (fdgets(buffer, sizeof(buffer), fd)) {
LOGD("magisk_img: %s", buffer);
if (strstr(buffer, img)) {
char *tok;
tok = strtok(buffer, ",");
while(tok != NULL) {
if (strstr(tok, "blocks")) {
ret = 0;
status = 0;
break;
}
tok = strtok(NULL, ",");
}
if (ret) continue;
if (status) continue;
sscanf(tok, "%d/%d", used, total);
*used = *used / 256 + 1;
*total /= 256;
@ -456,15 +462,24 @@ int get_img_size(const char *img, int *used, int *total) {
}
}
close(fd);
waitpid(pid, NULL, 0);
return ret;
waitpid(pid, &status, 0);
return WEXITSTATUS(status);
}
int resize_img(const char *img, int size) {
LOGI("Resize %s to %dM\n", img, size);
char buffer[PATH_MAX];
snprintf(buffer, PATH_MAX, "resize2fs %s %dM;", img, size);
return system(buffer);
snprintf(buffer, sizeof(buffer), "resize2fs %s %dM; e2fsck -yf %s;", img, size, img);
char *const command[] = { "sh", "-c", buffer, NULL };
int pid, status, fd = 0;
pid = run_command(1, &fd, "/system/bin/sh", command);
if (pid == -1)
return 1;
while (fdgets(buffer, sizeof(buffer), fd))
LOGD("magisk_img: %s", buffer);
close(fd);
waitpid(pid, &status, 0);
return WEXITSTATUS(status);
}
int switch_mnt_ns(int pid) {