From 2dd4cf040e4679a715f24dedfdbc8165f1490278 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Sat, 14 Oct 2017 21:08:05 +0800 Subject: [PATCH] Prevent multiple process clashes to start daemon --- jni/daemon/daemon.c | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/jni/daemon/daemon.c b/jni/daemon/daemon.c index 437397001..f090e7afe 100644 --- a/jni/daemon/daemon.c +++ b/jni/daemon/daemon.c @@ -137,6 +137,18 @@ void start_daemon() { xdup2(fd, STDERR_FILENO); close(fd); + // Patch selinux with medium patch before we do anything + load_policydb(SELINUX_POLICY); + sepol_med_rules(); + dump_policydb(SELINUX_LOAD); + + struct sockaddr_un sun; + fd = setup_socket(&sun); + + if (xbind(fd, (struct sockaddr*) &sun, sizeof(sun)) == -1) + exit(1); + xlisten(fd, 10); + if ((is_restart = access(UNBLOCKFILE, F_OK) == 0)) { // Restart stuffs if the daemon is restarted exec_command_sync("logcat", "-b", "all", "-c", NULL); @@ -147,21 +159,10 @@ void start_daemon() { // Start the log monitor monitor_logs(); - LOGI("Magisk v" xstr(MAGISK_VERSION) "(" xstr(MAGISK_VER_CODE) ") daemon started\n"); - - // Patch selinux with medium patch before we do anything - load_policydb(SELINUX_POLICY); - sepol_med_rules(); - dump_policydb(SELINUX_LOAD); - // Continue the larger patch in another thread, we will join later xpthread_create(&sepol_patch, NULL, large_sepol_patch, NULL); - struct sockaddr_un sun; - fd = setup_socket(&sun); - - xbind(fd, (struct sockaddr*) &sun, sizeof(sun)); - xlisten(fd, 10); + LOGI("Magisk v" xstr(MAGISK_VERSION) "(" xstr(MAGISK_VER_CODE) ") daemon started\n"); // Change process name strcpy(argv0, "magisk_daemon");