Improve daemon startup

This commit is contained in:
topjohnwu 2018-04-21 20:16:59 +08:00
parent 7db3d84ba2
commit 614c552e55
2 changed files with 33 additions and 29 deletions

View File

@ -137,29 +137,33 @@ void daemon_init() {
DIR *dir; DIR *dir;
struct dirent *entry; struct dirent *entry;
int root, sbin, fd; int root, sbin, fd;
char buf[PATH_MAX], buf2[PATH_MAX]; char buf[PATH_MAX];
void *data; void *magisk, *init;
size_t size; size_t magisk_size, init_size;
// Create hardlink mirror of /sbin to /root // Create hardlink mirror of /sbin to /root
xmount(NULL, "/", NULL, MS_REMOUNT, NULL); xmount(NULL, "/", NULL, MS_REMOUNT, NULL);
full_read("/sbin/magisk", &data, &size); mkdir("/root", 0750);
full_read("/sbin/magisk", &magisk, &magisk_size);
full_read("/sbin/magiskinit", &init, &init_size);
root = xopen("/root", O_RDONLY | O_CLOEXEC); root = xopen("/root", O_RDONLY | O_CLOEXEC);
sbin = xopen("/sbin", O_RDONLY | O_CLOEXEC); sbin = xopen("/sbin", O_RDONLY | O_CLOEXEC);
// unlink("/sbin/magisk");
unlink("/sbin/magiskinit");
link_dir(sbin, root); link_dir(sbin, root);
unlink("/sbin/magisk");
close(sbin); close(sbin);
// Mount the /sbin tmpfs overlay // Mount the /sbin tmpfs overlay
xmount("tmpfs", "/sbin", "tmpfs", 0, NULL); xmount("tmpfs", "/sbin", "tmpfs", 0, NULL);
chmod("/sbin", 0755); chmod("/sbin", 0755);
setfilecon("/sbin", "u:object_r:rootfs:s0"); setfilecon("/sbin", "u:object_r:rootfs:s0");
sbin = xopen("/sbin", O_RDONLY | O_CLOEXEC);
// Setup magisk // Setup magisk
fd = creat("/sbin/magisk", 0755); fd = creat("/sbin/magisk", 0755);
xwrite(fd, data, size); xwrite(fd, magisk, magisk_size);
close(fd); close(fd);
free(data); free(magisk);
setfilecon("/sbin/magisk", "u:object_r:"SEPOL_FILE_DOMAIN":s0"); setfilecon("/sbin/magisk", "u:object_r:"SEPOL_FILE_DOMAIN":s0");
for (int i = 0; applet[i]; ++i) { for (int i = 0; applet[i]; ++i) {
snprintf(buf, PATH_MAX, "/sbin/%s", applet[i]); snprintf(buf, PATH_MAX, "/sbin/%s", applet[i]);
@ -167,12 +171,10 @@ void daemon_init() {
} }
// Setup magiskinit // Setup magiskinit
full_read("/root/magiskinit", &data, &size);
unlink("/root/magiskinit");
fd = creat("/sbin/magiskinit", 0755); fd = creat("/sbin/magiskinit", 0755);
xwrite(fd, data, size); xwrite(fd, init, init_size);
close(fd); close(fd);
free(data); free(init);
setfilecon("/sbin/magiskinit", "u:object_r:"SEPOL_FILE_DOMAIN":s0"); setfilecon("/sbin/magiskinit", "u:object_r:"SEPOL_FILE_DOMAIN":s0");
for (int i = 0; init_applet[i]; ++i) { for (int i = 0; init_applet[i]; ++i) {
snprintf(buf, PATH_MAX, "/sbin/%s", init_applet[i]); snprintf(buf, PATH_MAX, "/sbin/%s", init_applet[i]);
@ -184,10 +186,10 @@ void daemon_init() {
while((entry = xreaddir(dir))) { while((entry = xreaddir(dir))) {
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) continue; if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) continue;
snprintf(buf, PATH_MAX, "/root/%s", entry->d_name); snprintf(buf, PATH_MAX, "/root/%s", entry->d_name);
snprintf(buf2, PATH_MAX, "/sbin/%s", entry->d_name); symlinkat(buf, sbin, entry->d_name);
xsymlink(buf, buf2);
} }
close(sbin);
close(root); close(root);
xmount(NULL, "/", NULL, MS_REMOUNT | MS_RDONLY, NULL); xmount(NULL, "/", NULL, MS_REMOUNT | MS_RDONLY, NULL);

View File

@ -375,14 +375,8 @@ int main(int argc, char *argv[]) {
if (null > STDERR_FILENO) if (null > STDERR_FILENO)
close(null); close(null);
// Extract and link files // Backup self
mkdir("/overlay", 0000); link("/init", "/init.bak");
dump_magiskrc("/overlay/init.magisk.rc", 0750);
mkdir("/overlay/sbin", 0755);
dump_magisk("/overlay/sbin/magisk", 0755);
patch_socket_name("/overlay/sbin/magisk");
mkdir("/overlay/root", 0750);
link("/init", "/overlay/root/magiskinit");
struct cmdline cmd; struct cmdline cmd;
parse_cmdline(&cmd); parse_cmdline(&cmd);
@ -395,7 +389,7 @@ int main(int argc, char *argv[]) {
if (cmd.skip_initramfs) { if (cmd.skip_initramfs) {
// Clear rootfs // Clear rootfs
excl_list = (char *[]) { "overlay", ".backup", NULL }; excl_list = (char *[]) { "overlay", ".backup", "init.bak", NULL };
frm_rf(root); frm_rf(root);
} else if (access("/ramdisk.cpio.xz", R_OK) == 0) { } else if (access("/ramdisk.cpio.xz", R_OK) == 0) {
// High compression mode // High compression mode
@ -464,20 +458,28 @@ int main(int argc, char *argv[]) {
// Only patch rootfs if not intended to run in recovery // Only patch rootfs if not intended to run in recovery
if (access("/etc/recovery.fstab", F_OK) != 0) { if (access("/etc/recovery.fstab", F_OK) != 0) {
int overlay = open("/overlay", O_RDONLY | O_CLOEXEC); int fd;
mv_dir(overlay, root);
close(overlay); // Handle ramdisk overlays
fd = open("/overlay", O_RDONLY | O_CLOEXEC);
if (fd >= 0) {
mv_dir(fd, root);
close(fd);
rmdir("/overlay"); rmdir("/overlay");
}
patch_ramdisk(); patch_ramdisk();
patch_sepolicy(); patch_sepolicy();
// Dump binaries
dump_magiskrc("/init.magisk.rc", 0750);
dump_magisk("/sbin/magisk", 0755);
patch_socket_name("/sbin/magisk");
rename("/init.bak", "/sbin/magiskinit");
} }
// Clean up // Clean up
close(root); close(root);
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
if (!cmd.skip_initramfs) if (!cmd.skip_initramfs)
umount("/system"); umount("/system");
umount("/vendor"); umount("/vendor");