Signed-off-by: faumaray <faumaray@gmail.com>
This commit is contained in:
faumaray committed 2026-07-16 19:59:57 +03:00
1 parent 8aa968eb9f
commit 11850b1a8d
11 files changed
+1569 -160

No files matched your search

+214 -49
View File
@@ -3,6 +3,7 @@
#include "echoion/host/CoreLoader.hpp"
#include "echoion/standalone/ReleaseManifest.hpp"
#include "echoion/standalone/RuntimePaths.hpp"
#include "echoion/standalone/StandaloneClientInterface.hpp"
#include <CoreInterface.hpp>
@@ -25,6 +26,8 @@
#include <optional>
#include <string>
#include <string_view>
#include <system_error>
#include <vector>
namespace
{
@@ -56,6 +59,48 @@ namespace
return std::nullopt;
}
bool hasArgument(int argc, char** argv, std::string_view name)
{
for (int index = 1; index < argc; ++index) {
if (std::string_view{argv[index]} == name) {
return true;
}
}
return false;
}
std::optional<std::filesystem::path>
optionalArgumentPath(int argc, char** argv, std::string_view name)
{
for (int index = 1; index < argc; ++index) {
if (std::string_view{argv[index]} != name) {
continue;
}
if (index + 1 < argc &&
!std::string_view{argv[index + 1]}.starts_with("--")) {
return std::filesystem::path{argv[index + 1]};
}
return std::nullopt;
}
return std::nullopt;
}
std::optional<std::filesystem::path> environmentPath(const char* name)
{
const char* value = std::getenv(name);
if (!value || std::string_view{value}.empty()) {
return std::nullopt;
}
return std::filesystem::path{value};
}
std::filesystem::path absolutePath(const std::filesystem::path& path)
{
std::error_code error;
auto absolute = std::filesystem::absolute(path, error);
return (error ? path : absolute).lexically_normal();
}
std::filesystem::path configDir()
{
if (const char* x = std::getenv("XDG_CONFIG_HOME")) {
@@ -67,6 +112,60 @@ namespace
return std::filesystem::path(".") / "echoion";
}
bool prepareStateDirectory(const std::filesystem::path& stateDirectory,
bool explicitlySelected)
{
std::error_code error;
std::filesystem::create_directories(stateDirectory, error);
if (error) {
std::cerr << "Cannot create state directory " << stateDirectory
<< ": " << error.message() << '\n';
return false;
}
const auto setPath = [](const char* name,
const std::filesystem::path& path,
bool overwrite) {
const auto value = path.string();
return ::setenv(name, value.c_str(), overwrite ? 1 : 0) == 0;
};
const auto temporaryDirectory = stateDirectory / "tmp";
std::filesystem::create_directories(temporaryDirectory, error);
if (error) {
std::cerr << "Cannot create temporary directory "
<< temporaryDirectory << ": " << error.message() << '\n';
return false;
}
if (!setPath("ECHOION_STATE_DIR", stateDirectory, true) ||
!setPath("XDG_CACHE_HOME", stateDirectory / "cache",
explicitlySelected) ||
!setPath("XDG_STATE_HOME", stateDirectory / "state",
explicitlySelected) ||
!setPath("XDG_DATA_HOME", stateDirectory / "data",
explicitlySelected) ||
!setPath("TMPDIR", temporaryDirectory, explicitlySelected)) {
std::cerr << "Cannot export runtime state paths\n";
return false;
}
return true;
}
int validateInstall(const std::filesystem::path& installRoot)
{
auto manifest = Echoion::Standalone::ReleaseManifest::load(
installRoot / "manifest.json");
if (!manifest) {
std::cerr << manifest.error().message << '\n';
return 2;
}
if (auto result = manifest->validateInstallLayout(); !result) {
std::cerr << result.error().message << '\n';
return 2;
}
std::cout << "Echoion installation is valid: " << installRoot << '\n';
return 0;
}
void recordInterruptedRelease(const std::filesystem::path& directory)
{
const auto transaction = directory / "release-transaction";
@@ -105,21 +204,6 @@ namespace
}
}
// 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)
{
@@ -130,9 +214,11 @@ namespace
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";
std::cout
<< "Usage: echoiond [--config PATH] [--state-dir PATH]\n"
" echoiond --import-identity EXPORT.ini "
"[--identity-path PATH] [--force]\n"
" echoiond --validate-install [ROOT]\n";
return 0;
}
if (argument == "--import-identity") {
@@ -184,8 +270,14 @@ int main(int argc, char** argv)
std::cerr << Void::Stacktrace::format_stacktrace(frames) << '\n';
std::abort();
});
if (const auto validationPath =
argumentValue(argc, argv, "--validate-release")) {
if (hasArgument(argc, argv, "--validate-release")) {
const auto validationPath =
argumentValue(argc, argv, "--validate-release");
if (!validationPath || validationPath->starts_with("--")) {
std::cerr << "--validate-release requires a manifest path\n";
return 2;
}
auto manifest =
Echoion::Standalone::ReleaseManifest::load(*validationPath);
if (!manifest) {
@@ -199,25 +291,70 @@ int main(int argc, char** argv)
return 0;
}
std::filesystem::path cfgDir = configDir();
recordInterruptedRelease(cfgDir);
const auto runningExecutable = Echoion::Standalone::runningExecutablePath();
if (!runningExecutable) {
std::cerr << runningExecutable.error() << '\n';
return 2;
}
const auto installRoot = Echoion::Standalone::deriveInstallRoot(
*runningExecutable, environmentPath("ECHOION_HOME"));
if (hasArgument(argc, argv, "--validate-install")) {
const auto requestedRoot =
optionalArgumentPath(argc, argv, "--validate-install");
return validateInstall(requestedRoot ? absolutePath(*requestedRoot)
: installRoot);
}
const auto installRootString = installRoot.string();
if (::setenv("ECHOION_HOME", installRootString.c_str(), 1) != 0) {
std::cerr << "Cannot export ECHOION_HOME\n";
return 2;
}
for (const std::string_view requiredValue : {"--config", "--state-dir"}) {
if (hasArgument(argc, argv, requiredValue) &&
!optionalArgumentPath(argc, argv, requiredValue)) {
std::cerr << requiredValue << " requires a path\n";
return 2;
}
}
const auto cfgDir = absolutePath(configDir());
std::filesystem::path cfgPath = cfgDir / "config.json";
const auto identityPath = std::filesystem::path(
envOr("ECHOION_IDENTITY_PATH", (cfgDir / "identity.txt").string()));
if (const auto requestedConfig =
optionalArgumentPath(argc, argv, "--config")) {
cfgPath = absolutePath(*requestedConfig);
}
const auto commandLineState =
optionalArgumentPath(argc, argv, "--state-dir");
const auto environmentState = environmentPath("ECHOION_STATE_DIR");
const auto stateDirectory = Echoion::Standalone::resolveStateDirectory(
commandLineState, environmentState, environmentPath("XDG_STATE_HOME"),
environmentPath("HOME"), cfgDir);
if (!prepareStateDirectory(stateDirectory,
commandLineState.has_value() ||
environmentState.has_value())) {
return 2;
}
recordInterruptedRelease(stateDirectory);
const auto identityPath = absolutePath(std::filesystem::path(envOr(
"ECHOION_IDENTITY_PATH", (stateDirectory / "identity.txt").string())));
if (const auto importResult = importIdentity(argc, argv, identityPath);
importResult >= 0) {
return importResult;
}
// Optional CLI: --config <path>
for (int i = 1; i + 1 < argc; ++i) {
if (std::string(argv[i]) == "--config") {
cfgPath = argv[i + 1];
}
}
const auto requestedRestore =
optionalArgumentPath(argc, argv, "--restore-state");
const std::optional<std::filesystem::path> restorePath =
requestedRestore ? std::optional{absolutePath(*requestedRestore)}
: std::nullopt;
const auto requestedManifest = environmentPath("ECHOION_RELEASE_MANIFEST");
const std::optional<std::filesystem::path> releaseManifestPath =
requestedManifest ? std::optional{absolutePath(*requestedManifest)}
: std::nullopt;
Echoion::Standalone::StandaloneSettings settings;
settings.identityPath = identityPath.string();
settings.avatarCachePath = (cfgDir / "avatars").string();
settings.avatarCachePath = (stateDirectory / "avatars").string();
settings.securityLevel = envInt("ECHOION_SECURITY_LEVEL", 8);
settings.opusBitrate = envInt("ECHOION_OPUS_BITRATE", 96000);
@@ -233,13 +370,27 @@ int main(int argc, char** argv)
Echoion::Host::CoreLoader loader;
loader.setConfigPath(cfgPath);
auto corePathResolved = corePath();
const auto executableDirectory = runningExecutable->parent_path();
const auto corePathResolved = Echoion::Standalone::resolveCorePath(
installRoot, executableDirectory, environmentPath("ECHOION_CORE_PATH"));
if (const auto workingDirectory =
Echoion::Standalone::runtimeWorkingDirectory(installRoot,
executableDirectory)) {
std::error_code error;
std::filesystem::current_path(*workingDirectory, error);
if (error) {
Void::Log::error("main",
"cannot select runtime asset directory {}: {}",
workingDirectory->string(), error.message());
return 1;
}
}
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 (restorePath) {
if (auto result = loader.restoreCheckpoint(*restorePath); !result) {
Void::Log::error("main", "failed to restore restart checkpoint: {}",
result.error().message);
@@ -274,9 +425,7 @@ int main(int argc, char** argv)
core->triggerAutoConnect();
std::unique_ptr<Void::FileSystem::FileWatcher> releaseWatcher;
std::optional<std::filesystem::path> releaseManifestPath;
if (const char* manifest = std::getenv("ECHOION_RELEASE_MANIFEST")) {
releaseManifestPath = manifest;
if (releaseManifestPath) {
releaseWatcher = std::make_unique<Void::FileSystem::FileWatcher>();
releaseWatcher->watch(
*releaseManifestPath,
@@ -287,21 +436,27 @@ int main(int argc, char** argv)
});
Void::Log::info("main", "Watching release manifest {}",
releaseManifestPath->string());
} else {
} else if (!std::filesystem::is_regular_file(installRoot /
"manifest.json")) {
// Compatibility mode for development. Production release bundles use
// transactional daemon re-exec instead of replacing Core in isolation.
loader.enableAutoReload();
} else {
Void::Log::info(
"main", "{}",
"Core auto-reload is disabled for an installed release payload");
}
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(
stateDirectory / "release-health", version);
std::ignore = Void::FileSystem::remove_path(stateDirectory /
"release-transaction");
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");
Void::FileSystem::remove_path(stateDirectory / "failed-release");
::unsetenv("ECHOION_REEXEC_VERSION");
Void::Log::info("main", "Release {} passed startup health check",
version);
@@ -327,7 +482,7 @@ int main(int argc, char** argv)
continue;
}
if (auto failed =
Void::FileSystem::read_file(cfgDir / "failed-release");
Void::FileSystem::read_file(stateDirectory / "failed-release");
failed && *failed == manifest->version) {
Void::Log::error("main",
"Release {} was previously rolled back; publish a "
@@ -335,17 +490,17 @@ int main(int argc, char** argv)
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});
*runningExecutable,
{"--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";
checkpointPath = stateDirectory / "restart-state.bin";
if (auto checkpoint = loader.checkpoint(checkpointPath); !checkpoint) {
Void::Log::error("main", "release checkpoint failed: {}",
checkpoint.error().message);
@@ -365,14 +520,24 @@ int main(int argc, char** argv)
(void)interface->shutdown();
if (pendingRelease) {
if (auto transaction = Void::FileSystem::atomic_write(
cfgDir / "release-transaction", pendingRelease->version);
stateDirectory / "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);
const auto pendingRoot =
Echoion::Standalone::deriveInstallRoot(pendingRelease->executable);
const auto pendingRootString = pendingRoot.string();
const auto pendingCoreString = pendingRelease->core.string();
if (::setenv("ECHOION_HOME", pendingRootString.c_str(), 1) != 0 ||
::setenv("ECHOION_CORE_PATH", pendingCoreString.c_str(), 1) != 0 ||
::setenv("ECHOION_REEXEC_VERSION", pendingRelease->version.c_str(),
1) != 0) {
Void::Log::error("main", "cannot export pending release paths");
return 1;
}
std::vector<std::string> arguments;
arguments.reserve(static_cast<std::size_t>(argc) + 2);
for (int index = 1; index < argc; ++index) {