fix(src/config/so): Ensure that the plugin keeps running

This commit is contained in:
Benedikt Peetz 2024-05-04 20:03:02 +02:00
parent d76f279a05
commit e8a3370dce
2 changed files with 20 additions and 13 deletions

View File

@ -1,22 +1,29 @@
use std::{ffi::c_int, path::Path};
use std::{
ffi::c_int,
path::PathBuf,
thread::{self},
};
use anyhow::{Context, Result};
use cli_log::info;
use libloading::{Library, Symbol};
pub fn load(plugin: &Path) -> Result<()> {
pub fn load(plugin: PathBuf) -> Result<()> {
info!("Loading a plugin from '{}'", plugin.display());
unsafe {
let lib = Library::new(plugin).context("Failed to load plugin")?;
let func: Symbol<unsafe fn() -> c_int> = lib
.get(b"plugin_main")
.context("Plugin does not have a 'plugin_main' symbol")?;
let _handle = thread::spawn(move || -> Result<()> {
unsafe {
let lib = Library::new(plugin).context("Failed to load plugin")?;
let func: Symbol<unsafe fn() -> c_int> = lib
.get(b"plugin_main")
.context("Plugin does not have a 'plugin_main' symbol")?;
info!("Starting plugin");
let out = func();
info!("Plugin finished with: {}", out);
}
info!("Starting plugin");
let out = func();
info!("Plugin finished with: {}", out);
}
Ok(())
});
Ok(())
}

View File

@ -6,7 +6,7 @@ pub mod status;
use std::{collections::HashMap, path::PathBuf, sync::OnceLock};
use anyhow::{Context, Result};
use cli_log::warn;
use cli_log::{debug, warn};
use directories::ProjectDirs;
use keymaps::trie::Node;
use tokio::sync::mpsc::{self, Sender};
@ -93,7 +93,7 @@ impl<U: TrinitrixUi> App<U> {
}
if let Some(plugin) = plugin_path {
config::shared_objects::load(&plugin)
config::shared_objects::load(plugin.clone())
.with_context(|| format!("Failed to load a pluging at '{}'", plugin.display()))?;
}