1 parent
8583a479dd
commit
3851aeb703
42 files changed
+6948
-759
No files matched your search
+247
-224
@@ -1,9 +1,9 @@
|
||||
#include <Void/Log/Log.hpp>
|
||||
#include <Void/Plugin/Loader.hpp>
|
||||
#include <Void/Log/Log.hpp>
|
||||
|
||||
#if defined(__unix__) || defined(__APPLE__)
|
||||
# include <dlfcn.h>
|
||||
# define VOID_HAS_DLOPEN 1
|
||||
#include <dlfcn.h>
|
||||
#define VOID_HAS_DLOPEN 1
|
||||
#endif
|
||||
|
||||
#include <filesystem>
|
||||
@@ -13,258 +13,281 @@
|
||||
namespace Void::Plugin
|
||||
{
|
||||
|
||||
std::string_view to_string(LoadError e) noexcept
|
||||
{
|
||||
switch (e) {
|
||||
case LoadError::FileNotFound: return "file not found";
|
||||
case LoadError::DlopenFailed: return "dlopen failed";
|
||||
case LoadError::SymbolMissing: return "required symbol missing";
|
||||
case LoadError::AbiMismatch: return "ABI version mismatch";
|
||||
case LoadError::CreateFailed: return "plugin create() returned null";
|
||||
case LoadError::NotSupported: return "platform does not support dlopen";
|
||||
case LoadError::StateRejected: return "new instance rejected state";
|
||||
case LoadError::ActivationFailed: return "plugin activation failed";
|
||||
case LoadError::UnknownPlugin: return "no plugin registered under name";
|
||||
}
|
||||
return "unknown";
|
||||
}
|
||||
std::string_view to_string(LoadError e) noexcept
|
||||
{
|
||||
switch (e) {
|
||||
case LoadError::FileNotFound:
|
||||
return "file not found";
|
||||
case LoadError::DlopenFailed:
|
||||
return "dlopen failed";
|
||||
case LoadError::SymbolMissing:
|
||||
return "required symbol missing";
|
||||
case LoadError::AbiMismatch:
|
||||
return "ABI version mismatch";
|
||||
case LoadError::CreateFailed:
|
||||
return "plugin create() returned null";
|
||||
case LoadError::NotSupported:
|
||||
return "platform does not support dlopen";
|
||||
case LoadError::StateRejected:
|
||||
return "new instance rejected state";
|
||||
case LoadError::ActivationFailed:
|
||||
return "plugin activation failed";
|
||||
case LoadError::UnknownPlugin:
|
||||
return "no plugin registered under name";
|
||||
case LoadError::NameMismatch:
|
||||
return "replacement plugin name mismatch";
|
||||
}
|
||||
return "unknown";
|
||||
}
|
||||
|
||||
// ── internal helpers ──────────────────────────────────────────────────────────
|
||||
// ── internal helpers
|
||||
// ──────────────────────────────────────────────────────────
|
||||
|
||||
void PluginLoader::destroy_current() noexcept
|
||||
{
|
||||
void PluginLoader::destroy_current() noexcept
|
||||
{
|
||||
#ifdef VOID_HAS_DLOPEN
|
||||
if (plugin_) {
|
||||
plugin_->on_unload();
|
||||
if (destroy_) destroy_(plugin_);
|
||||
plugin_ = nullptr;
|
||||
}
|
||||
if (handle_) {
|
||||
dlclose(handle_);
|
||||
handle_ = nullptr;
|
||||
}
|
||||
destroy_ = nullptr;
|
||||
transfer_ = nullptr;
|
||||
ready_ = nullptr;
|
||||
state_version_ = nullptr;
|
||||
serialize_ = nullptr;
|
||||
free_string_ = nullptr;
|
||||
deserialize_ = nullptr;
|
||||
if (plugin_) {
|
||||
plugin_->on_unload();
|
||||
if (destroy_) {
|
||||
destroy_(plugin_);
|
||||
}
|
||||
plugin_ = nullptr;
|
||||
}
|
||||
if (handle_) {
|
||||
dlclose(handle_);
|
||||
handle_ = nullptr;
|
||||
}
|
||||
destroy_ = nullptr;
|
||||
transfer_ = nullptr;
|
||||
ready_ = nullptr;
|
||||
state_version_ = nullptr;
|
||||
serialize_ = nullptr;
|
||||
free_string_ = nullptr;
|
||||
deserialize_ = nullptr;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
// Move plugin state old -> new across a reload. Prefers the v2
|
||||
// serialize/deserialize protocol (survives layout changes between
|
||||
// versions); falls back to the raw transfer_state hook; else no-op.
|
||||
// Returns false only when the new instance actively rejects the state.
|
||||
bool transfer_plugin_state(const PluginLoader& from, PluginLoader& to)
|
||||
{
|
||||
if (!from.plugin_ || !to.plugin_) {
|
||||
return true;
|
||||
}
|
||||
if (from.serialize_ && to.deserialize_) {
|
||||
const std::uint32_t from_version =
|
||||
from.state_version_ ? from.state_version_() : 0;
|
||||
const char* json = from.serialize_(from.plugin_);
|
||||
if (json != nullptr) {
|
||||
const int rc = to.deserialize_(to.plugin_, json, from_version);
|
||||
if (from.free_string_) {
|
||||
from.free_string_(json);
|
||||
}
|
||||
if (rc != 0) {
|
||||
Void::Log::warn("Plugin",
|
||||
"deserialize_state rejected state "
|
||||
"(from v{}, rc={})",
|
||||
from_version, rc);
|
||||
return false;
|
||||
}
|
||||
Void::Log::debug("Plugin",
|
||||
"state transferred via v2 protocol (v{})",
|
||||
from_version);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
if (to.transfer_) {
|
||||
to.transfer_(from.plugin_, to.plugin_);
|
||||
Void::Log::debug("Plugin", "state transferred via transfer_state");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
// Move plugin state old -> new across a reload. Prefers the v2
|
||||
// serialize/deserialize protocol (survives layout changes between
|
||||
// versions); falls back to the raw transfer_state hook; else no-op.
|
||||
// Returns false only when the new instance actively rejects the state.
|
||||
bool transfer_plugin_state(const PluginLoader& from, PluginLoader& to)
|
||||
{
|
||||
if (!from.plugin_ || !to.plugin_) {
|
||||
return true;
|
||||
}
|
||||
if (from.serialize_ && to.deserialize_) {
|
||||
const std::uint32_t from_version =
|
||||
from.state_version_ ? from.state_version_() : 0;
|
||||
const char* json = from.serialize_(from.plugin_);
|
||||
if (json != nullptr) {
|
||||
const int rc = to.deserialize_(to.plugin_, json, from_version);
|
||||
if (from.free_string_) {
|
||||
from.free_string_(json);
|
||||
}
|
||||
if (rc != 0) {
|
||||
Void::Log::warn("Plugin",
|
||||
"deserialize_state rejected state "
|
||||
"(from v{}, rc={})",
|
||||
from_version, rc);
|
||||
return false;
|
||||
}
|
||||
Void::Log::debug("Plugin",
|
||||
"state transferred via v2 protocol (v{})",
|
||||
from_version);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
if (to.transfer_) {
|
||||
to.transfer_(from.plugin_, to.plugin_);
|
||||
Void::Log::debug("Plugin", "state transferred via transfer_state");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
std::expected<void, LoadError>
|
||||
PluginLoader::load_from(const std::filesystem::path& path)
|
||||
{
|
||||
std::expected<void, LoadError>
|
||||
PluginLoader::load_from(const std::filesystem::path& path)
|
||||
{
|
||||
#ifdef VOID_HAS_DLOPEN
|
||||
void* h = dlopen(path.c_str(), RTLD_NOW | RTLD_LOCAL);
|
||||
if (!h) return std::unexpected{LoadError::DlopenFailed};
|
||||
void* h = dlopen(path.c_str(), RTLD_NOW | RTLD_LOCAL);
|
||||
if (!h) {
|
||||
return std::unexpected{LoadError::DlopenFailed};
|
||||
}
|
||||
|
||||
const auto sym_info = reinterpret_cast<info_fn> (dlsym(h, kInfoSymbol));
|
||||
const auto sym_create = reinterpret_cast<create_fn> (dlsym(h, kCreateSymbol));
|
||||
const auto sym_destroy = reinterpret_cast<destroy_fn>(dlsym(h, kDestroySymbol));
|
||||
const auto sym_info = reinterpret_cast<info_fn>(dlsym(h, kInfoSymbol));
|
||||
const auto sym_create =
|
||||
reinterpret_cast<create_fn>(dlsym(h, kCreateSymbol));
|
||||
const auto sym_destroy =
|
||||
reinterpret_cast<destroy_fn>(dlsym(h, kDestroySymbol));
|
||||
|
||||
if (!sym_info || !sym_create || !sym_destroy) {
|
||||
dlclose(h);
|
||||
return std::unexpected{LoadError::SymbolMissing};
|
||||
}
|
||||
if (!sym_info || !sym_create || !sym_destroy) {
|
||||
dlclose(h);
|
||||
return std::unexpected{LoadError::SymbolMissing};
|
||||
}
|
||||
|
||||
const PluginInfo info = sym_info();
|
||||
if (info.abi_version != ABI_VERSION) {
|
||||
dlclose(h);
|
||||
return std::unexpected{LoadError::AbiMismatch};
|
||||
}
|
||||
const PluginInfo info = sym_info();
|
||||
if (info.abi_version != ABI_VERSION) {
|
||||
dlclose(h);
|
||||
return std::unexpected{LoadError::AbiMismatch};
|
||||
}
|
||||
|
||||
void* raw = sym_create();
|
||||
if (!raw) {
|
||||
dlclose(h);
|
||||
return std::unexpected{LoadError::CreateFailed};
|
||||
}
|
||||
void* raw = sym_create();
|
||||
if (!raw) {
|
||||
dlclose(h);
|
||||
return std::unexpected{LoadError::CreateFailed};
|
||||
}
|
||||
|
||||
handle_ = h;
|
||||
plugin_ = static_cast<IPlugin*>(raw);
|
||||
destroy_ = sym_destroy;
|
||||
transfer_ = reinterpret_cast<transfer_fn>(dlsym(h, kTransferSymbol));
|
||||
ready_ = reinterpret_cast<ready_fn>(dlsym(h, kReadySymbol));
|
||||
state_version_ = reinterpret_cast<state_version_fn>(
|
||||
dlsym(h, kStateVersionSymbol));
|
||||
serialize_ = reinterpret_cast<serialize_fn>(dlsym(h, kSerializeSymbol));
|
||||
free_string_ = reinterpret_cast<free_string_fn>(
|
||||
dlsym(h, kFreeStringSymbol));
|
||||
deserialize_ = reinterpret_cast<deserialize_fn>(
|
||||
dlsym(h, kDeserializeSymbol));
|
||||
info_ = info;
|
||||
return {};
|
||||
handle_ = h;
|
||||
plugin_ = static_cast<IPlugin*>(raw);
|
||||
destroy_ = sym_destroy;
|
||||
transfer_ = reinterpret_cast<transfer_fn>(dlsym(h, kTransferSymbol));
|
||||
ready_ = reinterpret_cast<ready_fn>(dlsym(h, kReadySymbol));
|
||||
state_version_ =
|
||||
reinterpret_cast<state_version_fn>(dlsym(h, kStateVersionSymbol));
|
||||
serialize_ = reinterpret_cast<serialize_fn>(dlsym(h, kSerializeSymbol));
|
||||
free_string_ =
|
||||
reinterpret_cast<free_string_fn>(dlsym(h, kFreeStringSymbol));
|
||||
deserialize_ =
|
||||
reinterpret_cast<deserialize_fn>(dlsym(h, kDeserializeSymbol));
|
||||
info_ = info;
|
||||
return {};
|
||||
#else
|
||||
(void)path;
|
||||
return std::unexpected{LoadError::NotSupported};
|
||||
(void)path;
|
||||
return std::unexpected{LoadError::NotSupported};
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
// ── public API ────────────────────────────────────────────────────────────────
|
||||
// ── public API
|
||||
// ────────────────────────────────────────────────────────────────
|
||||
|
||||
std::expected<PluginLoader, LoadError>
|
||||
PluginLoader::load(std::filesystem::path path)
|
||||
{
|
||||
if (!std::filesystem::exists(path))
|
||||
return std::unexpected{LoadError::FileNotFound};
|
||||
std::expected<PluginLoader, LoadError>
|
||||
PluginLoader::load(std::filesystem::path path)
|
||||
{
|
||||
if (!std::filesystem::exists(path)) {
|
||||
return std::unexpected{LoadError::FileNotFound};
|
||||
}
|
||||
|
||||
PluginLoader ldr;
|
||||
ldr.path_ = path;
|
||||
PluginLoader ldr;
|
||||
ldr.path_ = path;
|
||||
|
||||
if (auto r = ldr.load_from(path); !r) {
|
||||
Void::Log::warn("Plugin", "load '{}' failed: {}", path.string(),
|
||||
to_string(r.error()));
|
||||
return std::unexpected{r.error()};
|
||||
}
|
||||
if (auto r = ldr.load_from(path); !r) {
|
||||
Void::Log::warn("Plugin", "load '{}' failed: {}", path.string(),
|
||||
to_string(r.error()));
|
||||
return std::unexpected{r.error()};
|
||||
}
|
||||
|
||||
try {
|
||||
ldr.plugin_->on_load();
|
||||
} catch (...) {
|
||||
return std::unexpected{LoadError::ActivationFailed};
|
||||
}
|
||||
if (ldr.ready_ && ldr.ready_(ldr.plugin_) != 0) {
|
||||
return std::unexpected{LoadError::ActivationFailed};
|
||||
}
|
||||
Void::Log::info("Plugin", "loaded '{}' ({} v{})", path.string(),
|
||||
ldr.info_.name, ldr.info_.plugin_version);
|
||||
return ldr;
|
||||
}
|
||||
try {
|
||||
ldr.plugin_->on_load();
|
||||
} catch (...) {
|
||||
return std::unexpected{LoadError::ActivationFailed};
|
||||
}
|
||||
if (ldr.ready_ && ldr.ready_(ldr.plugin_) != 0) {
|
||||
return std::unexpected{LoadError::ActivationFailed};
|
||||
}
|
||||
Void::Log::info("Plugin", "loaded '{}' ({} v{})", path.string(),
|
||||
ldr.info_.name, ldr.info_.plugin_version);
|
||||
return ldr;
|
||||
}
|
||||
|
||||
PluginLoader::~PluginLoader() { destroy_current(); }
|
||||
PluginLoader::~PluginLoader() { destroy_current(); }
|
||||
|
||||
PluginLoader::PluginLoader(PluginLoader&& o) noexcept
|
||||
: handle_ {std::exchange(o.handle_, nullptr)}
|
||||
, plugin_ {std::exchange(o.plugin_, nullptr)}
|
||||
, destroy_ {std::exchange(o.destroy_, nullptr)}
|
||||
, transfer_ {std::exchange(o.transfer_, nullptr)}
|
||||
, ready_ {std::exchange(o.ready_, nullptr)}
|
||||
, state_version_{std::exchange(o.state_version_, nullptr)}
|
||||
, serialize_ {std::exchange(o.serialize_, nullptr)}
|
||||
, free_string_ {std::exchange(o.free_string_, nullptr)}
|
||||
, deserialize_ {std::exchange(o.deserialize_, nullptr)}
|
||||
, info_ {o.info_}
|
||||
, path_ {std::move(o.path_)}
|
||||
{}
|
||||
PluginLoader::PluginLoader(PluginLoader&& o) noexcept
|
||||
: handle_{std::exchange(o.handle_, nullptr)},
|
||||
plugin_{std::exchange(o.plugin_, nullptr)},
|
||||
destroy_{std::exchange(o.destroy_, nullptr)},
|
||||
transfer_{std::exchange(o.transfer_, nullptr)},
|
||||
ready_{std::exchange(o.ready_, nullptr)},
|
||||
state_version_{std::exchange(o.state_version_, nullptr)},
|
||||
serialize_{std::exchange(o.serialize_, nullptr)},
|
||||
free_string_{std::exchange(o.free_string_, nullptr)},
|
||||
deserialize_{std::exchange(o.deserialize_, nullptr)}, info_{o.info_},
|
||||
path_{std::move(o.path_)}
|
||||
{
|
||||
}
|
||||
|
||||
PluginLoader& PluginLoader::operator=(PluginLoader&& o) noexcept
|
||||
{
|
||||
if (this != &o) {
|
||||
destroy_current();
|
||||
handle_ = std::exchange(o.handle_, nullptr);
|
||||
plugin_ = std::exchange(o.plugin_, nullptr);
|
||||
destroy_ = std::exchange(o.destroy_, nullptr);
|
||||
transfer_ = std::exchange(o.transfer_, nullptr);
|
||||
ready_ = std::exchange(o.ready_, nullptr);
|
||||
state_version_ = std::exchange(o.state_version_, nullptr);
|
||||
serialize_ = std::exchange(o.serialize_, nullptr);
|
||||
free_string_ = std::exchange(o.free_string_, nullptr);
|
||||
deserialize_ = std::exchange(o.deserialize_, nullptr);
|
||||
info_ = o.info_;
|
||||
path_ = std::move(o.path_);
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
PluginLoader& PluginLoader::operator=(PluginLoader&& o) noexcept
|
||||
{
|
||||
if (this != &o) {
|
||||
destroy_current();
|
||||
handle_ = std::exchange(o.handle_, nullptr);
|
||||
plugin_ = std::exchange(o.plugin_, nullptr);
|
||||
destroy_ = std::exchange(o.destroy_, nullptr);
|
||||
transfer_ = std::exchange(o.transfer_, nullptr);
|
||||
ready_ = std::exchange(o.ready_, nullptr);
|
||||
state_version_ = std::exchange(o.state_version_, nullptr);
|
||||
serialize_ = std::exchange(o.serialize_, nullptr);
|
||||
free_string_ = std::exchange(o.free_string_, nullptr);
|
||||
deserialize_ = std::exchange(o.deserialize_, nullptr);
|
||||
info_ = o.info_;
|
||||
path_ = std::move(o.path_);
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
std::expected<void, LoadError> PluginLoader::reload()
|
||||
{
|
||||
std::expected<void, LoadError> PluginLoader::reload()
|
||||
{
|
||||
#ifdef VOID_HAS_DLOPEN
|
||||
// Copy to a unique temp path so dlopen gets a fresh inode (bypasses the
|
||||
// path-based cache) even if the file was replaced in-place.
|
||||
const auto tmp = path_.parent_path()
|
||||
/ (path_.filename().string() + ".void_hotswap");
|
||||
// Copy to a unique temp path so dlopen gets a fresh inode (bypasses the
|
||||
// path-based cache) even if the file was replaced in-place.
|
||||
const auto tmp =
|
||||
path_.parent_path() / (path_.filename().string() + ".void_hotswap");
|
||||
|
||||
{
|
||||
std::error_code ec;
|
||||
std::filesystem::copy_file(path_, tmp,
|
||||
std::filesystem::copy_options::overwrite_existing, ec);
|
||||
if (ec) return std::unexpected{LoadError::FileNotFound};
|
||||
}
|
||||
{
|
||||
std::error_code ec;
|
||||
std::filesystem::copy_file(
|
||||
path_, tmp, std::filesystem::copy_options::overwrite_existing,
|
||||
ec);
|
||||
if (ec) {
|
||||
return std::unexpected{LoadError::FileNotFound};
|
||||
}
|
||||
}
|
||||
|
||||
// Load new instance while old is still alive (for state transfer).
|
||||
PluginLoader next;
|
||||
next.path_ = path_;
|
||||
if (auto r = next.load_from(tmp); !r) {
|
||||
std::error_code ec;
|
||||
std::filesystem::remove(tmp, ec);
|
||||
Void::Log::warn("Plugin", "reload '{}' failed: {}; keeping old",
|
||||
path_.string(), to_string(r.error()));
|
||||
return r;
|
||||
}
|
||||
// Load new instance while old is still alive (for state transfer).
|
||||
PluginLoader next;
|
||||
next.path_ = path_;
|
||||
if (auto r = next.load_from(tmp); !r) {
|
||||
std::error_code ec;
|
||||
std::filesystem::remove(tmp, ec);
|
||||
Void::Log::warn("Plugin", "reload '{}' failed: {}; keeping old",
|
||||
path_.string(), to_string(r.error()));
|
||||
return r;
|
||||
}
|
||||
|
||||
// Transfer state with both instances alive. A rejection aborts the
|
||||
// reload and keeps the old instance.
|
||||
if (!transfer_plugin_state(*this, next)) {
|
||||
std::error_code ec;
|
||||
std::filesystem::remove(tmp, ec);
|
||||
Void::Log::warn("Plugin", "reload '{}' aborted: state rejected",
|
||||
path_.string());
|
||||
return std::unexpected{LoadError::StateRejected};
|
||||
}
|
||||
// Transfer state with both instances alive. A rejection aborts the
|
||||
// reload and keeps the old instance.
|
||||
if (!transfer_plugin_state(*this, next)) {
|
||||
std::error_code ec;
|
||||
std::filesystem::remove(tmp, ec);
|
||||
Void::Log::warn("Plugin", "reload '{}' aborted: state rejected",
|
||||
path_.string());
|
||||
return std::unexpected{LoadError::StateRejected};
|
||||
}
|
||||
|
||||
try {
|
||||
next.plugin_->on_load();
|
||||
} catch (...) {
|
||||
std::error_code ec;
|
||||
std::filesystem::remove(tmp, ec);
|
||||
return std::unexpected{LoadError::ActivationFailed};
|
||||
}
|
||||
if (next.ready_ && next.ready_(next.plugin_) != 0) {
|
||||
std::error_code ec;
|
||||
std::filesystem::remove(tmp, ec);
|
||||
return std::unexpected{LoadError::ActivationFailed};
|
||||
}
|
||||
try {
|
||||
next.plugin_->on_load();
|
||||
} catch (...) {
|
||||
std::error_code ec;
|
||||
std::filesystem::remove(tmp, ec);
|
||||
return std::unexpected{LoadError::ActivationFailed};
|
||||
}
|
||||
if (next.ready_ && next.ready_(next.plugin_) != 0) {
|
||||
std::error_code ec;
|
||||
std::filesystem::remove(tmp, ec);
|
||||
return std::unexpected{LoadError::ActivationFailed};
|
||||
}
|
||||
|
||||
// Activation succeeded; only now destroy old and install new.
|
||||
destroy_current();
|
||||
*this = std::move(next);
|
||||
Void::Log::info("Plugin", "reloaded '{}' ({} v{})", path_.string(),
|
||||
info_.name, info_.plugin_version);
|
||||
// Activation succeeded; only now destroy old and install new.
|
||||
destroy_current();
|
||||
*this = std::move(next);
|
||||
Void::Log::info("Plugin", "reloaded '{}' ({} v{})", path_.string(),
|
||||
info_.name, info_.plugin_version);
|
||||
|
||||
std::error_code ec;
|
||||
std::filesystem::remove(tmp, ec);
|
||||
return {};
|
||||
std::error_code ec;
|
||||
std::filesystem::remove(tmp, ec);
|
||||
return {};
|
||||
#else
|
||||
return std::unexpected{LoadError::NotSupported};
|
||||
return std::unexpected{LoadError::NotSupported};
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace Void::Plugin
|
||||
+76
-33
@@ -122,8 +122,7 @@ namespace Void::Plugin
|
||||
reinterpret_cast<transfer_fn>(dlsym(h, kTransferSymbol));
|
||||
inst->configure =
|
||||
reinterpret_cast<configure_fn>(dlsym(h, kConfigureSymbol));
|
||||
inst->ready =
|
||||
reinterpret_cast<ready_fn>(dlsym(h, kReadySymbol));
|
||||
inst->ready = reinterpret_cast<ready_fn>(dlsym(h, kReadySymbol));
|
||||
inst->state_version = reinterpret_cast<state_version_fn>(
|
||||
dlsym(h, kStateVersionSymbol));
|
||||
inst->serialize =
|
||||
@@ -183,10 +182,10 @@ namespace Void::Plugin
|
||||
hotswap_copy(const std::filesystem::path& path)
|
||||
{
|
||||
static std::atomic<std::uint64_t> counter{0};
|
||||
const auto tmp =
|
||||
path.parent_path() /
|
||||
(path.filename().string() + ".void_hotswap." +
|
||||
std::to_string(counter.fetch_add(1, std::memory_order_relaxed)));
|
||||
const auto tmp = path.parent_path() /
|
||||
(path.filename().string() + ".void_hotswap." +
|
||||
std::to_string(counter.fetch_add(
|
||||
1, std::memory_order_relaxed)));
|
||||
std::error_code ec;
|
||||
std::filesystem::copy_file(
|
||||
path, tmp, std::filesystem::copy_options::overwrite_existing,
|
||||
@@ -251,8 +250,7 @@ namespace Void::Plugin
|
||||
} catch (...) {
|
||||
return std::unexpected{LoadError::ActivationFailed};
|
||||
}
|
||||
if ((*inst)->ready != nullptr &&
|
||||
(*inst)->ready((*inst)->plugin) != 0) {
|
||||
if ((*inst)->ready != nullptr && (*inst)->ready((*inst)->plugin) != 0) {
|
||||
return std::unexpected{LoadError::ActivationFailed};
|
||||
}
|
||||
if (m_options.instance_validator &&
|
||||
@@ -338,7 +336,7 @@ namespace Void::Plugin
|
||||
if (entry == nullptr) {
|
||||
return std::unexpected{LoadError::UnknownPlugin};
|
||||
}
|
||||
auto result = reload_locked(*entry, name);
|
||||
auto result = reload_locked(*entry, name, entry->path);
|
||||
if (!result.has_value()) {
|
||||
return result;
|
||||
}
|
||||
@@ -351,14 +349,51 @@ namespace Void::Plugin
|
||||
}
|
||||
|
||||
std::expected<void, LoadError>
|
||||
PluginManager::reload_locked(Entry& entry, std::string_view name)
|
||||
PluginManager::replace(std::string_view name,
|
||||
const std::filesystem::path& newPath)
|
||||
{
|
||||
std::vector<std::function<void(std::string_view)>> callbacks;
|
||||
bool restartWatcher = false;
|
||||
{
|
||||
std::lock_guard lock(m_mutex);
|
||||
auto* entry = find(name);
|
||||
if (entry == nullptr) {
|
||||
return std::unexpected{LoadError::UnknownPlugin};
|
||||
}
|
||||
std::error_code error;
|
||||
if (!std::filesystem::is_regular_file(newPath, error)) {
|
||||
return std::unexpected{LoadError::FileNotFound};
|
||||
}
|
||||
auto result = reload_locked(*entry, name, newPath);
|
||||
if (!result) {
|
||||
return result;
|
||||
}
|
||||
restartWatcher = entry->watched;
|
||||
if (restartWatcher) {
|
||||
m_watcher.unwatch(entry->path);
|
||||
}
|
||||
entry->path = newPath;
|
||||
callbacks = reload_callbacks_locked();
|
||||
}
|
||||
if (restartWatcher) {
|
||||
watch(name);
|
||||
}
|
||||
for (const auto& callback : callbacks) {
|
||||
callback(name);
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
std::expected<void, LoadError>
|
||||
PluginManager::reload_locked(Entry& entry, std::string_view name,
|
||||
const std::filesystem::path& sourcePath)
|
||||
{
|
||||
// Copy to a fresh inode so dlopen loads the new bytes instead of
|
||||
// returning the still-open handle for this path. The caller must
|
||||
// have installed the new file via atomic rename (a fresh inode of
|
||||
// its own) — overwriting the mapped file in place corrupts the
|
||||
// running instance's mapping (SIGBUS).
|
||||
auto tmp = hotswap_copy(entry.path);
|
||||
auto tmp = hotswap_copy(sourcePath);
|
||||
if (!tmp.has_value()) {
|
||||
return std::unexpected{tmp.error()};
|
||||
}
|
||||
@@ -371,6 +406,12 @@ namespace Void::Plugin
|
||||
to_string(next.error()));
|
||||
return std::unexpected{next.error()};
|
||||
}
|
||||
if ((*next)->plugin->name() != name) {
|
||||
Void::Log::warn("Plugin.Manager",
|
||||
"replace '{}' rejected: candidate reports '{}'",
|
||||
name, (*next)->plugin->name());
|
||||
return std::unexpected{LoadError::NameMismatch};
|
||||
}
|
||||
|
||||
if (m_options.config_provider && (*next)->configure != nullptr) {
|
||||
const std::string config = m_options.config_provider(name);
|
||||
@@ -392,16 +433,16 @@ namespace Void::Plugin
|
||||
} catch (...) {
|
||||
return std::unexpected{LoadError::ActivationFailed};
|
||||
}
|
||||
if ((*next)->ready != nullptr &&
|
||||
(*next)->ready((*next)->plugin) != 0) {
|
||||
if ((*next)->ready != nullptr && (*next)->ready((*next)->plugin) != 0) {
|
||||
Void::Log::warn("Plugin.Manager",
|
||||
"reload '{}' aborted: activation failed", name);
|
||||
"reload '{}' aborted: activation failed", name);
|
||||
return std::unexpected{LoadError::ActivationFailed};
|
||||
}
|
||||
if (m_options.instance_validator &&
|
||||
!m_options.instance_validator(*(*next)->plugin, (*next)->info)) {
|
||||
Void::Log::warn("Plugin.Manager",
|
||||
"reload '{}' aborted: host validation failed", name);
|
||||
"reload '{}' aborted: host validation failed",
|
||||
name);
|
||||
return std::unexpected{LoadError::ActivationFailed};
|
||||
}
|
||||
// Atomic swap. The old instance keeps living while any acquired
|
||||
@@ -421,29 +462,30 @@ namespace Void::Plugin
|
||||
return;
|
||||
}
|
||||
path = entry->path;
|
||||
entry->watched = true;
|
||||
}
|
||||
const std::string name_str{name};
|
||||
m_watcher.watch(
|
||||
path, [this, name_str](const FileSystem::FileWatcher::Event& e) {
|
||||
if (!e.exists) {
|
||||
m_watcher.watch(path, [this, name_str](
|
||||
const FileSystem::FileWatcher::Event& e) {
|
||||
if (!e.exists) {
|
||||
return;
|
||||
}
|
||||
std::vector<std::function<void(std::string_view)>> callbacks;
|
||||
{
|
||||
std::lock_guard lock(m_mutex);
|
||||
auto* entry = find(name_str);
|
||||
if (entry == nullptr) {
|
||||
return;
|
||||
}
|
||||
std::vector<std::function<void(std::string_view)>> callbacks;
|
||||
{
|
||||
std::lock_guard lock(m_mutex);
|
||||
auto* entry = find(name_str);
|
||||
if (entry == nullptr) {
|
||||
return;
|
||||
}
|
||||
if (!reload_locked(*entry, name_str).has_value()) {
|
||||
return;
|
||||
}
|
||||
callbacks = reload_callbacks_locked();
|
||||
if (!reload_locked(*entry, name_str, entry->path).has_value()) {
|
||||
return;
|
||||
}
|
||||
for (const auto& cb : callbacks) {
|
||||
cb(name_str);
|
||||
}
|
||||
});
|
||||
callbacks = reload_callbacks_locked();
|
||||
}
|
||||
for (const auto& cb : callbacks) {
|
||||
cb(name_str);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void PluginManager::unwatch(std::string_view name)
|
||||
@@ -452,6 +494,7 @@ namespace Void::Plugin
|
||||
auto* entry = find(name);
|
||||
if (entry != nullptr) {
|
||||
m_watcher.unwatch(entry->path);
|
||||
entry->watched = false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user