From 6eb1925ec39a184b15df81bc9c831f859ec48d19 Mon Sep 17 00:00:00 2001 From: xiaozhou233 Date: Sun, 18 Oct 2026 22:31:26 +0800 Subject: [PATCH] Feat: add Linux libloader entry point - libloader: add platform/linux/Entry.cpp that starts the loader from a constructor - libloader: read the config path from JuiceAgent_ConfigPath, falling back to $HOME/.juiceagent - libloader: default JuiceAgentNativeLibraryPath to libagent.so on non-Windows - injector: print a notice that the Linux injector is not open source yet --- src/injector/injector_linux.c | 6 +++ src/libloader/core/Loader.cpp | 9 +++- src/libloader/platform/linux/Entry.cpp | 59 ++++++++++++++++++++++++++ 3 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 src/libloader/platform/linux/Entry.cpp diff --git a/src/injector/injector_linux.c b/src/injector/injector_linux.c index 2ca06ff..0b9be5f 100644 --- a/src/injector/injector_linux.c +++ b/src/injector/injector_linux.c @@ -1,4 +1,10 @@ +#include + int main(int argc, char *argv[]) { // TODO: Implement Linux injector + // Due to certain reasons, the Injector for Linux is not open source. + // Please wait a moment for the next update. + printf("Due to certain reasons, the Injector for Linux is not open source. Please wait a moment for the next update.\n"); + return 0; } \ No newline at end of file diff --git a/src/libloader/core/Loader.cpp b/src/libloader/core/Loader.cpp index 1d5e014..2b0489f 100644 --- a/src/libloader/core/Loader.cpp +++ b/src/libloader/core/Loader.cpp @@ -92,7 +92,14 @@ void JuiceAgent::Loader::initialize(const char* runtime_dir, JNIEnv* env, jvmtiE JuiceAgent::Utils::Serializer ser; ser.add_kv("Version", cfg.get("JuiceAgent.Version", -1)); - ser.add_kv("JuiceAgentNativeLibraryPath", cfg.getPath("JuiceAgent.JuiceAgentNativeLibraryPath", (cfg.getRuntimeDir()/"libagent.dll").string())); + + #ifdef _WIN32 + std::string default_libagent_path = (cfg.getRuntimeDir()/"libagent.dll").string(); + #else + std::string default_libagent_path = (cfg.getRuntimeDir()/"libagent.so").string(); + #endif + + ser.add_kv("JuiceAgentNativeLibraryPath", cfg.getPath("JuiceAgent.JuiceAgentNativeLibraryPath", default_libagent_path)); ser.add_kv("RuntimeDir", cfg.getRuntimeDir().string()); std::string args = ser.serialize(); diff --git a/src/libloader/platform/linux/Entry.cpp b/src/libloader/platform/linux/Entry.cpp new file mode 100644 index 0000000..06d12a5 --- /dev/null +++ b/src/libloader/platform/linux/Entry.cpp @@ -0,0 +1,59 @@ +#include "JuiceAgent/Logger.hpp" +#include "Loader.hpp" + +#include +#include +#include +#include +#include + +namespace fs = std::filesystem; + +static void run_loader() { + const char* env = std::getenv("JuiceAgent_ConfigPath"); + std::string current_path; + + if (env != nullptr && env[0] != '\0') { + // Use the environment variable if it exists and is not empty. + current_path = env; + } else { + spdlog::trace("[libloader] JuiceAgent_ConfigPath is not set"); + + const char* home = std::getenv("HOME"); + if (home == nullptr || home[0] == '\0') { + spdlog::error("[libloader] HOME environment variable is not set"); + return; + } + + // Fall back to $HOME/.juiceagent. + fs::path config_dir = fs::path(home) / ".juiceagent"; + + std::error_code ec; + fs::create_directories(config_dir, ec); + + if (ec) { + spdlog::error( + "[libloader] Failed to create config directory: {}", + ec.message() + ); + return; + } + + current_path = config_dir.string(); + } + + spdlog::info("[libloader] current path: {}", current_path); + + JuiceAgent::Loader::entrypoint(current_path.c_str()); +} + +__attribute__((constructor)) +static void entry() { + Logger::init(); + + spdlog::info("[libloader] loaded!"); + + std::thread([] { + run_loader(); + }).detach(); +} \ No newline at end of file