diff --git a/unshell-test-module/.cargo/config.toml b/unshell-breakout-module/.cargo/config.toml similarity index 100% rename from unshell-test-module/.cargo/config.toml rename to unshell-breakout-module/.cargo/config.toml diff --git a/unshell-test-module/Cargo.lock b/unshell-breakout-module/Cargo.lock similarity index 86% rename from unshell-test-module/Cargo.lock rename to unshell-breakout-module/Cargo.lock index aa9bc06..30ea2a8 100644 --- a/unshell-test-module/Cargo.lock +++ b/unshell-breakout-module/Cargo.lock @@ -28,21 +28,6 @@ version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801" -[[package]] -name = "crossbeam-channel" -version = "0.5.15" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "82b8f8f868b36967f9606790d1903570de9ceaf870a7bf9fbbd3016d636a2cb2" -dependencies = [ - "crossbeam-utils", -] - -[[package]] -name = "crossbeam-utils" -version = "0.8.21" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d0a5c400df2834b80a4c3327b3aad3a4c4cd4de0629063962b03235697506a28" - [[package]] name = "libloading" version = "0.8.9" @@ -124,21 +109,19 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9312f7c4f6ff9069b165498234ce8be658059c6728633667c526e27dc2cf1df5" [[package]] -name = "unshell-modules" +name = "unshell-breakout-module" version = "0.1.0" dependencies = [ - "libloading", - "log", + "unshell-lib", ] [[package]] -name = "unshell-test-module" +name = "unshell-lib" version = "0.1.0" dependencies = [ "bincode", - "crossbeam-channel", + "libloading", "log", - "unshell-modules", ] [[package]] diff --git a/unshell-test-module/Cargo.toml b/unshell-breakout-module/Cargo.toml similarity index 59% rename from unshell-test-module/Cargo.toml rename to unshell-breakout-module/Cargo.toml index c6bdc5d..63238d6 100644 --- a/unshell-test-module/Cargo.toml +++ b/unshell-breakout-module/Cargo.toml @@ -1,19 +1,21 @@ cargo-features = ["trim-paths"] [package] -name = "unshell-test-module" +name = "unshell-breakout-module" version = "0.1.0" edition = "2024" +[dependencies] +# unshell-modules = {path = "../unshell-modules"} +unshell-lib = {path = "../unshell-lib"} [lib] crate-type = ["cdylib"] [features] -default = ["client-component", "server-component"] -client-component = [] -server-component = [] -# server = [] +default = ["client", "server"] +client = [] +server = [] [profile.release] strip = true # Strip symbols from the binary @@ -26,10 +28,3 @@ debug = false trim-paths = "all" # crate-type = ["cdylib"] - - -[dependencies] -bincode = "2.0.1" -crossbeam-channel = "0.5.15" -log = "0.4.28" -unshell-modules = {path = "../unshell-modules"} diff --git a/unshell-test-module/build.sh b/unshell-breakout-module/build.sh similarity index 100% rename from unshell-test-module/build.sh rename to unshell-breakout-module/build.sh diff --git a/unshell-breakout-module/src/lib.rs b/unshell-breakout-module/src/lib.rs new file mode 100644 index 0000000..b5466e7 --- /dev/null +++ b/unshell-breakout-module/src/lib.rs @@ -0,0 +1,28 @@ +#![no_main] + +use std::collections::HashMap; +use unshell_lib::Component; + +#[unsafe(no_mangle)] +pub fn get_components() -> HashMap<&'static str, Box> { + let mut components: HashMap<&'static str, Box> = HashMap::new(); + + #[cfg(feature = "client")] + components.insert( + unshell_lib::client::MODULE_NAME, + Box::new(unshell_lib::client::ClientComponent::new()), + ); + + components + + // vec![ + // Feature::Client, + // #[cfg(feature = "server")] + // Feature::Server, + // ] +} + +#[cfg(feature = "client")] +pub use unshell_lib::client::*; +#[cfg(feature = "server")] +pub use unshell_lib::server::*; diff --git a/unshell-cli/Cargo.lock b/unshell-cli/Cargo.lock index 872aa97..35ff371 100644 --- a/unshell-cli/Cargo.lock +++ b/unshell-cli/Cargo.lock @@ -28,21 +28,6 @@ version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801" -[[package]] -name = "crossbeam-channel" -version = "0.5.15" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "82b8f8f868b36967f9606790d1903570de9ceaf870a7bf9fbbd3016d636a2cb2" -dependencies = [ - "crossbeam-utils", -] - -[[package]] -name = "crossbeam-utils" -version = "0.8.21" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d0a5c400df2834b80a4c3327b3aad3a4c4cd4de0629063962b03235697506a28" - [[package]] name = "libloading" version = "0.8.9" @@ -127,25 +112,16 @@ checksum = "9312f7c4f6ff9069b165498234ce8be658059c6728633667c526e27dc2cf1df5" name = "unshell-cli" version = "0.1.0" dependencies = [ - "unshell-test-module", + "unshell-lib", ] [[package]] -name = "unshell-modules" -version = "0.1.0" -dependencies = [ - "libloading", - "log", -] - -[[package]] -name = "unshell-test-module" +name = "unshell-lib" version = "0.1.0" dependencies = [ "bincode", - "crossbeam-channel", + "libloading", "log", - "unshell-modules", ] [[package]] diff --git a/unshell-cli/Cargo.toml b/unshell-cli/Cargo.toml index 7237dbc..14b48e1 100644 --- a/unshell-cli/Cargo.toml +++ b/unshell-cli/Cargo.toml @@ -4,4 +4,4 @@ version = "0.1.0" edition = "2024" [dependencies] -unshell-test-module = {path = "../unshell-test-module"}#, default-features = false, features = ["server-component"]} +unshell-lib = {path = "../unshell-lib"}#, default-features = false, features = ["server-component"]} diff --git a/unshell-cli/src/main.rs b/unshell-cli/src/main.rs index bf9ee0f..41b710f 100644 --- a/unshell-cli/src/main.rs +++ b/unshell-cli/src/main.rs @@ -1,6 +1,6 @@ use std::io::{Write, stdin, stdout}; -use unshell_test_module::Announcement; +use unshell_lib::Announcement; // use unshell_test_module::ListenerRuntime; @@ -9,7 +9,7 @@ use unshell_test_module::Announcement; // } fn main() { - let mut serverruntime = unshell_test_module::ListenerRuntime::new(); + let mut serverruntime = unshell_lib::server::ListenerRuntime::new(); loop { print!("> "); diff --git a/unshell-lib/Cargo.lock b/unshell-lib/Cargo.lock new file mode 100644 index 0000000..a10b348 --- /dev/null +++ b/unshell-lib/Cargo.lock @@ -0,0 +1,136 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "bincode" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "36eaf5d7b090263e8150820482d5d93cd964a81e4019913c972f4edcc6edb740" +dependencies = [ + "bincode_derive", + "serde", + "unty", +] + +[[package]] +name = "bincode_derive" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bf95709a440f45e986983918d0e8a1f30a9b1df04918fc828670606804ac3c09" +dependencies = [ + "virtue", +] + +[[package]] +name = "cfg-if" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801" + +[[package]] +name = "libloading" +version = "0.8.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d7c4b02199fee7c5d21a5ae7d8cfa79a6ef5bb2fc834d6e9058e89c825efdc55" +dependencies = [ + "cfg-if", + "windows-link", +] + +[[package]] +name = "log" +version = "0.4.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34080505efa8e45a4b816c349525ebe327ceaa8559756f0356cba97ef3bf7432" + +[[package]] +name = "proc-macro2" +version = "1.0.103" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5ee95bc4ef87b8d5ba32e8b7714ccc834865276eab0aed5c9958d00ec45f49e8" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.42" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a338cc41d27e6cc6dce6cefc13a0729dfbb81c262b1f519331575dd80ef3067f" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "serde" +version = "1.0.228" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a8e94ea7f378bd32cbbd37198a4a91436180c5bb472411e48b5ec2e2124ae9e" +dependencies = [ + "serde_core", +] + +[[package]] +name = "serde_core" +version = "1.0.228" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41d385c7d4ca58e59fc732af25c3983b67ac852c1a25000afe1175de458b67ad" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.228" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d540f220d3187173da220f885ab66608367b6574e925011a9353e4badda91d79" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "syn" +version = "2.0.109" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2f17c7e013e88258aa9543dcbe81aca68a667a9ac37cd69c9fbc07858bfe0e2f" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9312f7c4f6ff9069b165498234ce8be658059c6728633667c526e27dc2cf1df5" + +[[package]] +name = "unshell-lib" +version = "0.1.0" +dependencies = [ + "bincode", + "libloading", + "log", +] + +[[package]] +name = "unty" +version = "0.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6d49784317cd0d1ee7ec5c716dd598ec5b4483ea832a2dced265471cc0f690ae" + +[[package]] +name = "virtue" +version = "0.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "051eb1abcf10076295e815102942cc58f9d5e3b4560e46e53c21e8ff6f3af7b1" + +[[package]] +name = "windows-link" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5" diff --git a/unshell-lib/Cargo.toml b/unshell-lib/Cargo.toml new file mode 100644 index 0000000..c192892 --- /dev/null +++ b/unshell-lib/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "unshell-lib" +version = "0.1.0" +edition = "2024" + +[dependencies] +libloading = {version = "0.8.9"} +bincode = "2.0.1" +log = "0.4.28" +# unshell-modules = {path = "../unshell-modules"} diff --git a/unshell-test-module/src/lib.rs b/unshell-lib/src/announcement.rs similarity index 67% rename from unshell-test-module/src/lib.rs rename to unshell-lib/src/announcement.rs index d061d6a..558c6ce 100644 --- a/unshell-test-module/src/lib.rs +++ b/unshell-lib/src/announcement.rs @@ -1,19 +1,5 @@ -#![no_main] -#[macro_use] -extern crate log; - use bincode::{Decode, Encode}; -#[cfg(feature = "client-component")] -mod client; -#[cfg(feature = "client-component")] -pub use client::*; - -#[cfg(feature = "server-component")] -mod server; -#[cfg(feature = "server-component")] -pub use server::ListenerRuntime; - #[derive(Debug, Encode, Decode)] pub enum Announcement { TestAnnouncement(String), diff --git a/unshell-test-module/src/client/client_runtime.rs b/unshell-lib/src/client/client_runtime.rs similarity index 95% rename from unshell-test-module/src/client/client_runtime.rs rename to unshell-lib/src/client/client_runtime.rs index 49e947d..06341e5 100644 --- a/unshell-test-module/src/client/client_runtime.rs +++ b/unshell-lib/src/client/client_runtime.rs @@ -8,9 +8,9 @@ use std::{ thread::{self, JoinHandle}, }; -use unshell_modules::{Manager, ModuleRuntime}; +// use unshell_modules::{Manager, ModuleRuntime}; -use crate::Announcement; +use crate::{Announcement, ModuleRuntime, module::Manager}; pub struct RuntimeTest { thread_handle: JoinHandle<()>, diff --git a/unshell-lib/src/client/mod.rs b/unshell-lib/src/client/mod.rs new file mode 100644 index 0000000..1eeea46 --- /dev/null +++ b/unshell-lib/src/client/mod.rs @@ -0,0 +1,74 @@ +mod client_runtime; + +// use crate::module::Interface; +use std::sync::{Arc, Mutex}; + +pub const MODULE_NAME: &'static str = "client"; + +// pub use unshell_modules::setup_logger; + +// use unshell_modules::{Manager, ModuleRuntime, module_interface}; + +use crate::{ + Component, + ModuleRuntime, + client::client_runtime::RuntimeTest, + module::{Interface, Manager}, + module_interface, + // module_interface, +}; + +pub extern "C" fn test1() { + warn!("Test1 called xxxxxxxxxxx"); +} +pub extern "C" fn test2() { + warn!("Test2 called"); +} +pub extern "C" fn test3() { + warn!("Test3 called"); +} + +module_interface! { + ClientInterface { + fn test1(); + fn test2(); + fn test3(); + } +} + +// #[unsafe(no_mangle)] +// pub fn interface() -> Interface { +// Interface::from_raw(test1, test2, test3) +// } + +// #[unsafe(no_mangle)] +// pub fn init(manager: Arc>) -> Box { +// info!("Initializing client module"); +// } + +#[derive(Clone)] +pub struct ClientComponent; + +impl ClientComponent { + pub fn new() -> Self { + ClientComponent + } +} + +impl Component for ClientComponent { + fn name(&self) -> &'static str { + MODULE_NAME + } + + fn start_runtime(&self, manager: Arc>) -> Option> { + Some(Box::new(RuntimeTest::new(manager))) + } + + fn clone_box(&self) -> Box { + Box::new(self.clone()) + } + + fn get_interface(&self) -> Box { + Box::new(ClientInterface::from_raw(test1, test2, test3)) + } +} diff --git a/unshell-lib/src/lib.rs b/unshell-lib/src/lib.rs new file mode 100644 index 0000000..3c047ee --- /dev/null +++ b/unshell-lib/src/lib.rs @@ -0,0 +1,45 @@ +#![no_main] +#[macro_use] +extern crate log; + +pub mod client; +pub mod module; +pub mod server; + +mod announcement; +use std::sync::{Arc, Mutex}; + +pub use announcement::Announcement; + +use crate::module::{Interface, Manager}; + +///Generic error type for module-related operations. +#[derive(Debug)] +pub enum ModuleError { + LibLoadingError(libloading::Error), + LogError(log::SetLoggerError), + LinkError(String), + Error(String), +} + +/// Trait for defining modules that have a runtime. +pub trait ModuleRuntime: Send { + /// Returns true if the module is running. + /// After returning false, the module will be dropped. + fn is_running(&self) -> bool; + /// Consumes the module, implementation should kill whatever is running. + fn kill(self: Box); +} + +pub trait Component { + fn name(&self) -> &'static str; + fn start_runtime(&self, manager: Arc>) -> Option>; + fn get_interface(&self) -> Box; + fn clone_box(&self) -> Box; +} + +impl Clone for Box { + fn clone(&self) -> Box { + self.clone_box() + } +} diff --git a/unshell-modules/src/logger.rs b/unshell-lib/src/module/logger.rs similarity index 100% rename from unshell-modules/src/logger.rs rename to unshell-lib/src/module/logger.rs diff --git a/unshell-lib/src/module/manager.rs b/unshell-lib/src/module/manager.rs new file mode 100644 index 0000000..bafc657 --- /dev/null +++ b/unshell-lib/src/module/manager.rs @@ -0,0 +1,88 @@ +use std::{ + collections::HashMap, + sync::{Arc, Mutex}, + thread, + time::Duration, +}; + +use crate::{Component, ModuleRuntime, module::Module}; + +pub struct Manager { + modules: Vec, + components: HashMap<&'static str, Box>, +} + +// static mut MANAGER_RUNTIME: Option>> = None; + +impl Manager { + /// Create Manager, and run initilization for each Module + #[allow(static_mut_refs)] + pub fn run<'a>(modules: Vec) { + let this: Self = Self::load_modules(modules); + let components = this.components.clone(); + + let this = Arc::new(Mutex::new(this)); + + let mut runtimes: Vec> = Vec::new(); + + for (name, component) in components { + let module_runtime = component.start_runtime(this.clone()); + if let Some(module_runtime) = module_runtime { + info!("Initialized {}", name); + runtimes.push(module_runtime); + } + } + + Self::join(&mut runtimes); + } + + pub fn load_modules<'a>(modules: Vec) -> Self { + let module_count = modules.len(); + let mut this = Self { + modules, + components: HashMap::new(), + }; + + // let mut runtimes = Vec::new(); + + for i in 0..module_count { + info!("Importing module {}", i); + // let this_lock = .unwrap(); + let component_func = if let Ok(component_func) = this.modules[i] + .get_symbol:: HashMap<&'static str, Box>>(b"get_components") + { + component_func + } else { + warn!("get_components function not found"); + continue; + }; + + let components = component_func(); + + info!("[{i}] Loaded {} components", components.len()); + + this.components.extend(components); + } + + this + } + + /// Iterateratively loop through all runtimes, until all are finished executing + pub fn join(runtimes: &mut Vec>) { + // let mut len = runtimes.len().clone(); + while runtimes.len() > 0 { + runtimes.retain(|runtime| runtime.is_running()); + + thread::sleep(Duration::from_micros(100)); + } + } + + // pub extern "C" fn test1234(&self, float: f32) { + // info!("Manager Test Sucsessfull! {}", float.powf(2.)); + // } + + // #[allow(static_mut_refs, improper_ctypes_definitions)] + // pub extern "C" fn get_manager() -> Arc> { + // unsafe { MANAGER_RUNTIME.clone().unwrap() } + // } +} diff --git a/unshell-modules/src/lib.rs b/unshell-lib/src/module/mod.rs similarity index 77% rename from unshell-modules/src/lib.rs rename to unshell-lib/src/module/mod.rs index 7fcd0cb..e2ff5d5 100644 --- a/unshell-modules/src/lib.rs +++ b/unshell-lib/src/module/mod.rs @@ -1,30 +1,15 @@ -#[macro_use] -extern crate log; - mod logger; mod manager; mod module; +// use std::any::Any; + pub use logger::setup_logger; pub use manager::Manager; pub use module::Module; -///Generic error type for module-related operations. -#[derive(Debug)] -pub enum ModuleError { - LibLoadingError(libloading::Error), - LogError(log::SetLoggerError), - LinkError(String), - Error(String), -} - -/// Trait for defining modules that have a runtime. -pub trait ModuleRuntime: Send { - /// Returns true if the module is running. - /// After returning false, the module will be dropped. - fn is_running(&self) -> bool; - /// Consumes the module, implementation should kill whatever is running. - fn kill(self: Box); +pub trait Interface { + fn as_any(self: Box) -> Box; } /// "Module Interface" helper macro that creates a struct with function pointers @@ -73,5 +58,11 @@ macro_rules! module_interface { } } } + + impl crate::module::Interface for $interface_name { + fn as_any(self: Box) -> Box { + self + } + } }; } diff --git a/unshell-modules/src/module.rs b/unshell-lib/src/module/module.rs similarity index 70% rename from unshell-modules/src/module.rs rename to unshell-lib/src/module/module.rs index 24f01d2..0b8af76 100644 --- a/unshell-modules/src/module.rs +++ b/unshell-lib/src/module/module.rs @@ -1,8 +1,7 @@ use libloading::{Library, Symbol}; -use crate::{ModuleError, logger::SetupLogger}; +use crate::{ModuleError, module::logger::SetupLogger}; -// #[derive(Clone, Copy)] pub struct Module { // name: String, lib: Library, @@ -31,15 +30,15 @@ impl Module { Ok(symbol) } - pub fn get_interface(&self) -> Result { - if let Ok(interface_function) = self.get_symbol:: T>(b"interface") { - Ok(interface_function()) - } else { - Err(ModuleError::LinkError(format!( - "Interface function not found!" - ))) - } - } + // pub fn get_interface(&self) -> Result { + // if let Ok(interface_function) = self.get_symbol:: T>(b"interface") { + // Ok(interface_function()) + // } else { + // Err(ModuleError::LinkError(format!( + // "Interface function not found!" + // ))) + // } + // } } // extern "C" fn test1234() { diff --git a/unshell-test-module/src/server/mod.rs b/unshell-lib/src/server/mod.rs similarity index 100% rename from unshell-test-module/src/server/mod.rs rename to unshell-lib/src/server/mod.rs diff --git a/unshell-test-module/src/server/server_runtime.rs b/unshell-lib/src/server/server_runtime.rs similarity index 64% rename from unshell-test-module/src/server/server_runtime.rs rename to unshell-lib/src/server/server_runtime.rs index 9217ca4..ebef55b 100644 --- a/unshell-test-module/src/server/server_runtime.rs +++ b/unshell-lib/src/server/server_runtime.rs @@ -5,7 +5,7 @@ use std::{ thread::{self, JoinHandle}, }; -use unshell_modules::ModuleRuntime; +use crate::ModuleRuntime; use crate::Announcement; @@ -31,39 +31,11 @@ impl ListenerRuntime { let stream = stream.unwrap(); println!("New connection from {}", stream.peer_addr().unwrap()); streams.lock().unwrap().push(stream); - - // thread::spawn(move || { - // let _ = handle_connection(&mut stream); - // info!("Connection from {} closed", stream.peer_addr().unwrap()); - // }); } }); - - // let reader = BufReader::new(stream.try_clone().unwrap()); - // let writer = BufWriter::new(stream.try_clone().unwrap()); - Self { thread_handle, streams, - // stream, - // reader, - // writer, - // thread_handle: thread::spawn(move || { - - // for stream in listener.incoming() { - // let mut stream = stream.unwrap(); - - // thread::spawn(move || { - // let _ = handle_connection(&mut stream); - // info!("Connection from {} closed", stream.peer_addr().unwrap()); - // }); - // } - // // thread::sleep(Duration::from_secs(2)); - - // // let manager_lock = manager.lock().unwrap(); - // // manager_lock.test1234(111.1111); - // // drop(manager_lock); - // }), } } diff --git a/unshell-modules/Cargo.lock b/unshell-modules/Cargo.lock deleted file mode 100644 index b5085a9..0000000 --- a/unshell-modules/Cargo.lock +++ /dev/null @@ -1,39 +0,0 @@ -# This file is automatically @generated by Cargo. -# It is not intended for manual editing. -version = 4 - -[[package]] -name = "cfg-if" -version = "1.0.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801" - -[[package]] -name = "libloading" -version = "0.8.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d7c4b02199fee7c5d21a5ae7d8cfa79a6ef5bb2fc834d6e9058e89c825efdc55" -dependencies = [ - "cfg-if", - "windows-link", -] - -[[package]] -name = "log" -version = "0.4.28" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "34080505efa8e45a4b816c349525ebe327ceaa8559756f0356cba97ef3bf7432" - -[[package]] -name = "unshell-modules" -version = "0.1.0" -dependencies = [ - "libloading", - "log", -] - -[[package]] -name = "windows-link" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5" diff --git a/unshell-modules/Cargo.toml b/unshell-modules/Cargo.toml deleted file mode 100644 index d22673a..0000000 --- a/unshell-modules/Cargo.toml +++ /dev/null @@ -1,12 +0,0 @@ -[package] -name = "unshell-modules" -version = "0.1.0" -edition = "2024" - -[features] -default = ["exec"] -"exec" = ["libloading"] - -[dependencies] -libloading = {version = "0.8.9", optional = true} -log = "0.4.28" diff --git a/unshell-modules/src/manager.rs b/unshell-modules/src/manager.rs deleted file mode 100644 index 317d69c..0000000 --- a/unshell-modules/src/manager.rs +++ /dev/null @@ -1,71 +0,0 @@ -use std::{ - sync::{Arc, Mutex}, - thread, - time::Duration, -}; - -use crate::{Module, ModuleRuntime}; - -pub struct Manager { - modules: Vec, -} - -static mut MANAGER_RUNTIME: Option>> = None; - -impl Manager { - /// Create Manager, and run initilization for each Module - #[allow(static_mut_refs)] - pub fn run<'a>(modules: Vec) { - let module_count = modules.len(); - let this = Self { modules }; - - let this = Arc::new(Mutex::new(this)); - - let mut runtimes = Vec::new(); - - for i in 0..module_count { - info!("Initializing {}", i); - let this_lock = this.lock().unwrap(); - let init = if let Ok(init) = - this_lock.modules[i] - .get_symbol::>) -> Box>(b"init") - { - init.to_owned() - } else { - warn!("init not found"); - continue; - }; - - let runtime = init(this.clone()); - - info!("Initialized {}", i); - - runtimes.push(runtime); - } - - Self::join(&mut runtimes); - } - - /// Iterateratively loop through all runtimes, until all are finished executing - pub fn join(runtimes: &mut Vec>) { - // let mut len = runtimes.len().clone(); - while runtimes.len() > 0 { - runtimes.retain(|runtime| runtime.is_running()); - - thread::sleep(Duration::from_micros(100)); - } - } - - // pub extern "C" fn test123() { - // info!("Manager Test Sucsessfull!"); - // } - - pub extern "C" fn test1234(&self, float: f32) { - info!("Manager Test Sucsessfull! {}", float.powf(2.)); - } - - #[allow(static_mut_refs, improper_ctypes_definitions)] - pub extern "C" fn get_manager() -> Arc> { - unsafe { MANAGER_RUNTIME.clone().unwrap() } - } -} diff --git a/unshell-payload/Cargo.lock b/unshell-payload/Cargo.lock index b83733b..262a0ea 100644 --- a/unshell-payload/Cargo.lock +++ b/unshell-payload/Cargo.lock @@ -61,6 +61,26 @@ dependencies = [ "windows-sys 0.60.2", ] +[[package]] +name = "bincode" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "36eaf5d7b090263e8150820482d5d93cd964a81e4019913c972f4edcc6edb740" +dependencies = [ + "bincode_derive", + "serde", + "unty", +] + +[[package]] +name = "bincode_derive" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bf95709a440f45e986983918d0e8a1f30a9b1df04918fc828670606804ac3c09" +dependencies = [ + "virtue", +] + [[package]] name = "cfg-if" version = "1.0.4" @@ -324,9 +344,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9312f7c4f6ff9069b165498234ce8be658059c6728633667c526e27dc2cf1df5" [[package]] -name = "unshell-modules" +name = "unshell-lib" version = "0.1.0" dependencies = [ + "bincode", "libloading", "log", ] @@ -339,15 +360,27 @@ dependencies = [ "libloading", "log", "pretty_env_logger", - "unshell-modules", + "unshell-lib", ] +[[package]] +name = "unty" +version = "0.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6d49784317cd0d1ee7ec5c716dd598ec5b4483ea832a2dced265471cc0f690ae" + [[package]] name = "utf8parse" version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" +[[package]] +name = "virtue" +version = "0.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "051eb1abcf10076295e815102942cc58f9d5e3b4560e46e53c21e8ff6f3af7b1" + [[package]] name = "winapi-util" version = "0.1.11" diff --git a/unshell-payload/Cargo.toml b/unshell-payload/Cargo.toml index 6936611..44117ae 100644 --- a/unshell-payload/Cargo.toml +++ b/unshell-payload/Cargo.toml @@ -11,7 +11,7 @@ libloading = "0.8.9" log = "0.4.28" pretty_env_logger = "0.5.0" # unshell-logger = {path = "../unshell-logger"} -unshell-modules = {path = "../unshell-modules"} +unshell-lib = {path = "../unshell-lib"} [profile.release] strip = true # Strip symbols from the binary diff --git a/unshell-payload/src/main.rs b/unshell-payload/src/main.rs index b19000c..36397ec 100644 --- a/unshell-payload/src/main.rs +++ b/unshell-payload/src/main.rs @@ -1,16 +1,11 @@ -use unshell_modules::{Manager, Module, ModuleError, module_interface}; +use unshell_lib::{ + ModuleError, + module::{Manager, Module}, +}; #[macro_use] extern crate log; -module_interface! { - Interface { - fn test1(); - fn test2(); - fn test3(); - } -} - fn main() { // Init the logger (This uses like 600MB of storage) pretty_env_logger::init(); @@ -26,7 +21,6 @@ fn main() { modules.push(Module::new(&arg)?) } Manager::run(modules); - // manager.join(); Ok(()) }() { diff --git a/unshell-test-module/src/client/mod.rs b/unshell-test-module/src/client/mod.rs deleted file mode 100644 index c8dce3c..0000000 --- a/unshell-test-module/src/client/mod.rs +++ /dev/null @@ -1,38 +0,0 @@ -mod client_runtime; - -use std::sync::{Arc, Mutex}; - -pub use unshell_modules::setup_logger; - -use unshell_modules::{Manager, ModuleRuntime, module_interface}; - -use crate::client::client_runtime::RuntimeTest; - -pub extern "C" fn test1() { - warn!("Test1 called"); -} -pub extern "C" fn test2() { - warn!("Test2 called"); -} -pub extern "C" fn test3() { - warn!("Test3 called"); -} - -module_interface! { - Interface { - fn test1(); - fn test2(); - fn test3(); - } -} - -#[unsafe(no_mangle)] -pub fn interface() -> Interface { - Interface::from_raw(test1, test2, test3) -} - -#[unsafe(no_mangle)] -pub fn init(manager: Arc>) -> Box { - info!("Initializing client module"); - Box::new(RuntimeTest::new(manager)) -}