From 4d80361805d2093999e08a84cd5d7960ff7b2fb4 Mon Sep 17 00:00:00 2001 From: vvb2060 Date: Sun, 2 Feb 2025 20:26:01 +0800 Subject: [PATCH] core: search for first available dir in PATH --- native/src/core/module.cpp | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/native/src/core/module.cpp b/native/src/core/module.cpp index b058bccf0..d1fe8ee82 100644 --- a/native/src/core/module.cpp +++ b/native/src/core/module.cpp @@ -233,10 +233,21 @@ private: }; static void inject_magisk_bins(root_node *system) { - auto bin = system->get_child("bin"); + dir_node* bin = system->get_child("bin"); if (!bin) { - bin = new inter_node("bin"); - system->insert(bin); + struct stat st{}; + bin = system; + for (auto &item: split(getenv("PATH"), ":")) { + item.erase(0, item.starts_with("/system/") ? 8 : 1); + auto system_path = "/system/" + item; + if (stat(system_path.data(), &st) == 0 && st.st_mode & S_IXOTH) { + for (const auto &dir: split(item, "/")) { + auto node = bin->get_child(dir); + bin = node ? node : bin->emplace(dir, dir.data()); + } + break; + } + } } // Insert binaries