From 50ff11405fe68cbfbdd2d0ecc5a4afb05f1e5036 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Mon, 18 Feb 2019 03:18:11 -0500 Subject: [PATCH] Swap out inotify fd before adding watch targets --- native/jni/magiskhide/proc_monitor.cpp | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/native/jni/magiskhide/proc_monitor.cpp b/native/jni/magiskhide/proc_monitor.cpp index 25da0ee63..aa320e63a 100644 --- a/native/jni/magiskhide/proc_monitor.cpp +++ b/native/jni/magiskhide/proc_monitor.cpp @@ -193,6 +193,7 @@ static int xinotify_add_watch(int fd, const char* path, uint32_t mask) { static int new_inotify; static const string_view APK_EXT(".apk"); +static vector hide_apks; static bool parse_packages_xml(string_view &s) { if (!str_starts(s, "d_name, APK_EXT)) { strcpy(value + value_view.length(), "/"); strcpy(value + value_view.length() + 1, entry->d_name); - xinotify_add_watch(new_inotify, value, IN_OPEN); + hide_apks.emplace_back(value); break; } } @@ -244,19 +245,22 @@ void update_inotify_mask() { fcntl(new_inotify, F_SETFD, FD_CLOEXEC); LOGD("proc_monitor: Updating inotify list\n"); + hide_apks.clear(); { MutexGuard lock(list_lock); hide_uid.clear(); file_readline("/data/system/packages.xml", parse_packages_xml, true); } - // Add /data/system to monitor /data/system/packages.xml - xinotify_add_watch(new_inotify, "/data/system", IN_CLOSE_WRITE); - + // Swap out and close old inotify_fd int tmp = inotify_fd; inotify_fd = new_inotify; if (tmp >= 0) close(tmp); + + for (auto apk : hide_apks) + xinotify_add_watch(inotify_fd, apk.data(), IN_OPEN); + xinotify_add_watch(inotify_fd, "/data/system", IN_CLOSE_WRITE); } void proc_monitor() {