// Standalone Echoion host. It loads the hot-reloadable Core through CoreLoader // and adapts Echoion's host contract to VoidTeamspeak's typed client API. #include "echoion/host/CoreLoader.hpp" #include "echoion/standalone/ReleaseManifest.hpp" #include "echoion/standalone/StandaloneClientInterface.hpp" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace { std::atomic g_stop{false}; std::atomic g_updateRequested{false}; std::condition_variable g_cv; std::mutex g_mu; void onSignal(int) { g_stop = true; g_cv.notify_all(); } void requestUpdate() { g_updateRequested = true; g_cv.notify_all(); } std::optional argumentValue(int argc, char** argv, std::string_view name) { for (int index = 1; index + 1 < argc; ++index) { if (std::string_view{argv[index]} == name) { return argv[index + 1]; } } return std::nullopt; } std::filesystem::path configDir() { if (const char* x = std::getenv("XDG_CONFIG_HOME")) { return std::filesystem::path(x) / "echoion"; } if (const char* h = std::getenv("HOME")) { return std::filesystem::path(h) / ".config" / "echoion"; } return std::filesystem::path(".") / "echoion"; } void recordInterruptedRelease(const std::filesystem::path& directory) { const auto transaction = directory / "release-transaction"; if (!std::filesystem::exists(transaction) || std::getenv("ECHOION_REEXEC_VERSION")) { return; } auto version = Void::FileSystem::read_file(transaction); if (version && !version->empty()) { std::ignore = Void::FileSystem::atomic_write( directory / "failed-release", *version); Void::Log::error("main", "Release {} failed its startup health check; " "stable service restored", *version); } std::ignore = Void::FileSystem::remove_path(transaction); } std::string envOr(const char* key, const std::string& fallback) { const char* v = std::getenv(key); return v ? std::string(v) : fallback; } int envInt(const char* key, int fallback) { const char* v = std::getenv(key); if (!v) { return fallback; } try { return std::stoi(v); } catch (...) { return fallback; } } // Locate the Core shared library ("Echoion.ts"). std::filesystem::path corePath() { if (const char* p = std::getenv("ECHOION_CORE_PATH")) { return p; } for (const char* c : {"./Echoion.ts", "./bin/Echoion.ts", "./plugins/Echoion.ts"}) { if (std::filesystem::exists(c)) { return c; } } return "./Echoion.ts"; } int importIdentity(int argc, char** argv, const std::filesystem::path& defaultIdentityPath) { std::optional source; auto destination = defaultIdentityPath; bool force = false; bool importRequested = false; for (int i = 1; i < argc; ++i) { const std::string_view argument{argv[i]}; if (argument == "--help") { std::cout << "Usage: echoiond [--config PATH]\n" " echoiond --import-identity EXPORT.ini " "[--identity-path PATH] [--force]\n"; return 0; } if (argument == "--import-identity") { importRequested = true; if (i + 1 < argc && !std::string_view{argv[i + 1]}.starts_with("--")) { source = argv[++i]; } } else if (argument == "--identity-path" && i + 1 < argc) { destination = argv[++i]; } else if (argument == "--force") { force = true; } } if (!importRequested) { return -1; } if (!source) { std::cerr << "--import-identity requires an exported INI path.\n"; return 2; } try { if (std::filesystem::exists(destination) && !force) { std::cerr << "Identity already exists at " << destination << "; pass --force to replace it.\n"; return 2; } const auto identity = Void::TeamSpeak::Identity::importTeamSpeakIni(source->string()); if (const auto parent = destination.parent_path(); !parent.empty()) { std::filesystem::create_directories(parent); } identity.save(destination.string()); std::cout << "Imported TeamSpeak identity " << identity.uid() << " to " << destination << '\n'; return 0; } catch (const std::exception& error) { std::cerr << "Identity import failed: " << error.what() << '\n'; return 2; } } } // namespace int main(int argc, char** argv) { std::set_terminate([] { const auto frames = Void::Stacktrace::capture_stacktrace(); std::cerr << Void::Stacktrace::format_stacktrace(frames) << '\n'; std::abort(); }); if (const auto validationPath = argumentValue(argc, argv, "--validate-release")) { auto manifest = Echoion::Standalone::ReleaseManifest::load(*validationPath); if (!manifest) { std::cerr << manifest.error().message << '\n'; return 2; } if (auto result = manifest->validate(); !result) { std::cerr << result.error().message << '\n'; return 2; } return 0; } std::filesystem::path cfgDir = configDir(); recordInterruptedRelease(cfgDir); std::filesystem::path cfgPath = cfgDir / "config.json"; const auto identityPath = std::filesystem::path( envOr("ECHOION_IDENTITY_PATH", (cfgDir / "identity.txt").string())); if (const auto importResult = importIdentity(argc, argv, identityPath); importResult >= 0) { return importResult; } // Optional CLI: --config for (int i = 1; i + 1 < argc; ++i) { if (std::string(argv[i]) == "--config") { cfgPath = argv[i + 1]; } } Echoion::Standalone::StandaloneSettings settings; settings.identityPath = identityPath.string(); settings.avatarCachePath = (cfgDir / "avatars").string(); settings.securityLevel = envInt("ECHOION_SECURITY_LEVEL", 8); settings.opusBitrate = envInt("ECHOION_OPUS_BITRATE", 96000); auto interface = std::make_shared( settings); if (auto r = interface->initialize(); !r) { Void::Log::error("main", "interface init failed: {}", r.error().message); return 1; } Echoion::Host::CoreLoader loader; loader.setConfigPath(cfgPath); auto corePathResolved = corePath(); Void::Log::info("main", "Loading Core from {}", corePathResolved.string()); if (auto r = loader.load(corePathResolved); !r) { Void::Log::error("main", "failed to load Core: {}", r.error().message); return 1; } if (const auto restorePath = argumentValue(argc, argv, "--restore-state")) { if (auto result = loader.restoreCheckpoint(*restorePath); !result) { Void::Log::error("main", "failed to restore restart checkpoint: {}", result.error().message); return 1; } } auto core = loader.getCore(); if (!core) { Void::Log::error("main", "Core not available after load"); return 1; } core->setPluginInterface(interface); interface->registerEventCallback( [core](const Echoion::Api::PluginEvent& ev) { std::ignore = core->onPluginEvent(ev); }); loader.setHotReloadPrepareCallback( [interface] { interface->quiesceEventCallbacks(); }); // Re-register the Core callback after each hot reload (new Core instance). loader.setHotReloadCallback([&loader, interface] { if (auto c = loader.getCore()) { c->setPluginInterface(interface); interface->registerEventCallback( [c](const Echoion::Api::PluginEvent& ev) { std::ignore = c->onPluginEvent(ev); }); c->triggerAutoConnect(); } }); core->triggerAutoConnect(); std::unique_ptr releaseWatcher; std::optional releaseManifestPath; if (const char* manifest = std::getenv("ECHOION_RELEASE_MANIFEST")) { releaseManifestPath = manifest; releaseWatcher = std::make_unique(); releaseWatcher->watch( *releaseManifestPath, [](const Void::FileSystem::FileWatcher::Event& event) { if (event.exists) { requestUpdate(); } }); Void::Log::info("main", "Watching release manifest {}", releaseManifestPath->string()); } else { // Compatibility mode for development. Production release bundles use // transactional daemon re-exec instead of replacing Core in isolation. loader.enableAutoReload(); } std::signal(SIGINT, onSignal); std::signal(SIGTERM, onSignal); Void::Log::info("main", "Echoion standalone running. Ctrl-C to stop."); if (const char* version = std::getenv("ECHOION_REEXEC_VERSION")) { std::ignore = Void::FileSystem::atomic_write(cfgDir / "release-health", version); std::ignore = Void::FileSystem::remove_path(cfgDir / "release-transaction"); std::ignore = Void::FileSystem::remove_path(cfgDir / "failed-release"); ::unsetenv("ECHOION_REEXEC_VERSION"); Void::Log::info("main", "Release {} passed startup health check", version); } std::optional pendingRelease; std::filesystem::path checkpointPath; while (!g_stop.load() && !pendingRelease) { { std::unique_lock lock(g_mu); g_cv.wait(lock, [] { return g_stop.load() || g_updateRequested.load(); }); } if (g_stop.load() || !releaseManifestPath) { break; } g_updateRequested = false; auto manifest = Echoion::Standalone::ReleaseManifest::load(*releaseManifestPath); if (!manifest) { Void::Log::error("main", "release rejected: {}", manifest.error().message); continue; } if (auto failed = Void::FileSystem::read_file(cfgDir / "failed-release"); failed && *failed == manifest->version) { Void::Log::error("main", "Release {} was previously rolled back; publish a " "new version to retry", manifest->version); continue; } auto executable = std::filesystem::canonical("/proc/self/exe"); auto validation = Void::Process::run( executable, {"--validate-release", releaseManifestPath->string()}, {}, std::chrono::seconds{30}); if (!validation || validation->exit_code != 0) { Void::Log::error("main", "release validation failed: {}", validation ? validation->err : validation.error().message()); continue; } checkpointPath = configDir() / "restart-state.bin"; if (auto checkpoint = loader.checkpoint(checkpointPath); !checkpoint) { Void::Log::error("main", "release checkpoint failed: {}", checkpoint.error().message); continue; } pendingRelease = std::move(*manifest); } releaseWatcher.reset(); Void::Log::info("main", "{}", pendingRelease ? "Applying validated release..." : "Shutting down..."); loader.disableAutoReload(); (void)interface->disconnectFromServer(); interface->quiesceEventCallbacks(); (void)loader.unload(); (void)interface->shutdown(); if (pendingRelease) { if (auto transaction = Void::FileSystem::atomic_write( cfgDir / "release-transaction", pendingRelease->version); !transaction) { Void::Log::error("main", "cannot record release transaction: {}", transaction.error().message()); return 1; } ::setenv("ECHOION_CORE_PATH", pendingRelease->core.c_str(), 1); ::setenv("ECHOION_REEXEC_VERSION", pendingRelease->version.c_str(), 1); std::vector arguments; arguments.reserve(static_cast(argc) + 2); for (int index = 1; index < argc; ++index) { const std::string_view argument{argv[index]}; if (argument == "--restore-state") { ++index; continue; } arguments.emplace_back(argument); } arguments.emplace_back("--restore-state"); arguments.push_back(checkpointPath.string()); if (auto result = Void::Process::reexec(pendingRelease->executable, arguments); !result) { Void::Log::error("main", "release re-exec failed: {}", result.error().message()); return 1; } } return 0; }