From e7155837d79177c1ac77288924073b78b9c7b16a Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Thu, 31 Oct 2019 01:57:47 -0400 Subject: [PATCH] Make sure magisk daemon won't get killed by init According to this comment in #1880: https://github.com/topjohnwu/Magisk/issues/1880#issuecomment-546657588 If Linux recycled our PPID, and coincidentally the process that reused the PPID is root, AND init wants to kill the whole process group, magiskd will get killed as a result. There is no real way to block a SIGKILL signal, so we simply make sure our daemon PID is the process group leader by renaming the directory. Close #1880 --- native/jni/core/daemon.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/native/jni/core/daemon.cpp b/native/jni/core/daemon.cpp index f13f05d3e..0964b23d6 100644 --- a/native/jni/core/daemon.cpp +++ b/native/jni/core/daemon.cpp @@ -210,9 +210,17 @@ int connect_daemon(bool create) { exit(1); } + int ppid = getpid(); LOGD("client: launching new main daemon process\n"); if (fork_dont_care() == 0) { close(fd); + + // Make sure ppid is not in acct + char src[64], dest[64]; + sprintf(src, "/acct/uid_0/pid_%d", ppid); + sprintf(dest, "/acct/uid_0/pid_%d", getpid()); + rename(src, dest); + main_daemon(); }