1 parent
8583a479dd
commit
3851aeb703
42 files changed
+6948
-759
No files matched your search
+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