From 1f76d0ee3f9405b24c498d79dc1b17755ec4b4be Mon Sep 17 00:00:00 2001 From: love_hangzhou Date: Wed, 27 Oct 2021 02:22:21 +0800 Subject: [PATCH 1/2] update dynamic lib loader --- src/process1/Cargo.toml | 1 + src/process1/src/plugin/manager.rs | 60 ------ src/process1/src/plugin/mod.rs | 78 +++++++- src/process1/src/plugin/unit.rs | 285 ----------------------------- 4 files changed, 74 insertions(+), 350 deletions(-) delete mode 100644 src/process1/src/plugin/manager.rs delete mode 100644 src/process1/src/plugin/unit.rs diff --git a/src/process1/Cargo.toml b/src/process1/Cargo.toml index 03b321d1..01451231 100644 --- a/src/process1/Cargo.toml +++ b/src/process1/Cargo.toml @@ -11,3 +11,4 @@ path = "src/lib.rs" [dependencies] dynamic_reload = "0.4.0" +walkdir = "2" diff --git a/src/process1/src/plugin/manager.rs b/src/process1/src/plugin/manager.rs deleted file mode 100644 index d4c74b0f..00000000 --- a/src/process1/src/plugin/manager.rs +++ /dev/null @@ -1,60 +0,0 @@ - -use std::sync::Arc; -pub trait Mangerobj { - fn init(&self){ - - } - - fn load(&self); - - fn dispatch(&self) -> i32; - - fn reload(&self) -> Option; - - fn destroy(&self); - - // reserved for sd event - fn event_dispatch(&self) -> Option; -} - - - -pub struct MangerPlugins { - pub managers: Vec>, -} - - -impl MangerPlugins{ - pub fn new() -> Self{ - MangerPlugins{ - managers: Vec::new() - } - } - pub fn load_plugins(&mut self, d: Box) { - self.managers.push(d); - } - - pub fn run(&mut self) -> i32{ - let mut ret:i32 = 0; - for m in self.managers.iter(){ - m.init(); - m.load(); - ret =m.dispatch(); - } - ret - } - - pub fn destroy(&self) { - for m in self.managers.iter(){ - m.destroy(); - } - } - - pub fn reload(&self){ - for m in self.managers.iter(){ - m.reload(); - } - } -} - - diff --git a/src/process1/src/plugin/mod.rs b/src/process1/src/plugin/mod.rs index 67753e32..9b5bde3c 100644 --- a/src/process1/src/plugin/mod.rs +++ b/src/process1/src/plugin/mod.rs @@ -2,7 +2,8 @@ use dynamic_reload; use std::sync::Arc; use std::time::Duration; -use std::thread; +use walkdir::{DirEntry,WalkDir}; +use std::path::Path; struct LibLoaders{ libs: Vec>, @@ -36,12 +37,79 @@ impl LibLoaders { pub struct Plugin { plugin_lists: Vec>, - loader_dir: Option + library_dir: String, + lib_loader: LibLoaders } - +#[allow(dead_code)] impl Plugin { - fn init() { + fn new() -> Self { + Self{ + plugin_lists: Vec::new(), + library_dir: String::new(), + lib_loader: LibLoaders{libs:Vec::new()}, + + } + } + pub fn set_library_dir(&mut self,library_dir:&str){ + self.library_dir.push_str(library_dir); + } + + pub fn is_dynamic_lib(entry: &DirEntry) -> bool{ + let file_type = entry.file_type(); + let file_name = entry.file_name(); + if file_type.is_file() && file_name.to_str().map(|s| s.ends_with(".so.*")).unwrap_or(false) { + true + } else { + false + } } -} \ No newline at end of file + + pub fn load_lib(&mut self){ + let file_exist = || { + if self.library_dir.is_empty() { + return false + } + if !Path::new(&self.library_dir).is_dir(){ + return false + } + true + }; + if !file_exist() { + return + }else{ + let mut reload_handler = dynamic_reload::DynamicReload::new(Some(vec![self.library_dir.as_str()]), Some(self.library_dir.as_str()), dynamic_reload::Search::Default); + for entry in WalkDir::new(&self.library_dir) + .follow_links(true) + .into_iter() + .filter_entry(|e| Self::is_dynamic_lib(e)){ + let entry = entry.unwrap(); + let path = entry.path(); + if path.is_dir(){ + continue; + }else{ + let file_name = path.file_name(); + match file_name { + Some(v) => { + let str_name = v.to_str().unwrap(); + match reload_handler.add_library(&str_name, dynamic_reload::PlatformName::Yes){ + Ok(lib) =>{ + println!("loader dynamic lib in to lib_loader"); + self.lib_loader.add_lib(&lib); + } + Err(e) =>{ + println!("Unable to load dynamic lib, err {:?}", e); + } + } + } + None =>{ + println!("file name is None"); + } + } + } + + } + } + } +} diff --git a/src/process1/src/plugin/unit.rs b/src/process1/src/plugin/unit.rs deleted file mode 100644 index a226c520..00000000 --- a/src/process1/src/plugin/unit.rs +++ /dev/null @@ -1,285 +0,0 @@ -use crate::manager; -use std::collections::HashSet; -use std::cell::RefCell; -use std::sync::Arc; - -enum UnitType { - UNIT_SERVICE = 0, - UNIT_SOCKET, - UNIT_BUSNAME, - UNIT_TARGET, - UNIT_SNAPSHOT, - UNIT_DEVICE, -} - -enum UnitLoadState { - UNIT_STUB = 0, - UNIT_LOADED, - UNIT_NOT_FOUND, - UNIT_ERROR, - UNIT_MERGED, - UNIT_MASKED, - _UNIT_LOAD_STATE_MAX, - _UNIT_LOAD_STATE_INVALID = -1, -} - -enum UnitDependency { - UNIT_REQUIRES, - UNIT_REQUIRES_OVERRIDABLE, - UNIT_REQUISITE, - UNIT_REQUISITE_OVERRIDABLE, -} - -enum UnitNameFlags { - UNIT_NAME_PLAIN =1, - UNIT_NAME_INSTANCE = 2, - UNIT_NAME_TEMPLATE = 4, - UNIT_NAME_ANY = 1|2|4, -} - -struct Unit { - unit_type: UnitType, - load_state: UnitLoadState, - id: String, - instance: String, - name: String, - depencies: Vec, - desc: String, - documnetation: String, - fragment_path: String, - source_path: String, - fragment_mtine: u64, - source_mtime: u64, - dropin_mtime: u64, - - units_by_type: Vec, - has_requires_mounts_for: Vec, - load_queue: Vec, - dbus_queue: Vec, - cleanup_queue: Vec, - gc_queue: Vec, - cgroup_queue: Vec, - pids: HashSet, - sigchldgen: u64, - gc_marker: u64, - deseialize_job: i32, - load_error: i32, - stop_when_unneeded: bool, - refuse_manual_start: bool, - allow_isolate: bool, - ignore_on_isolate: bool, - ignore_on_snapshot: bool, - condition_result: bool, - assert_result: bool, - transient: bool, - in_load_queue: bool, - in_dubs_queue: bool, - in_cleanup_queue: bool, - in_gc_queue: bool, - manager: Option>, -} - -pub trait UnitObj { - fn init(&self){} - fn done(&self){} - fn load(&self){} - fn coldplug(&self){} - fn dump(&self){} - fn start(&self){} - fn stop(&self){} - fn reload(&self){} - fn kill(&self){} - fn check_gc(&self)->bool; - fn release_resources(&self){} - fn check_snapshot(&self){} - fn sigchld_events(&self, pid:u64,code:i32, status:i32){} - fn reset_failed(&self){} -} -struct MountUnit { - mount_unit:Unit, -} - -#[macro_export] -macro_rules! null_str{ - ($name:expr) => { - String::from($name) - } -} - -impl Unit { - pub fn new() -> Self { - Unit{ - unit_type: UnitType::UNIT_SERVICE, - load_state: UnitLoadState::UNIT_STUB, - id: String::from(""), - instance: String::from(""), - name: String::from(""), - depencies: Vec::::new(), - desc: String::from(""), - documnetation: null_str!(""), - fragment_path: null_str!(""), - source_path: null_str!(""), - fragment_mtine: 0, - source_mtime: 0, - dropin_mtime: 0, - units_by_type: Vec::::new(), - has_requires_mounts_for: Vec::::new(), - load_queue: Vec::::new(), - dbus_queue: Vec::::new(), - cleanup_queue: Vec::::new(), - gc_queue: Vec::::new(), - cgroup_queue: Vec::::new(), - pids: HashSet::::new(), - sigchldgen: 0, - gc_marker: 0, - deseialize_job: 0, - load_error: 0, - stop_when_unneeded: false, - refuse_manual_start: false, - allow_isolate: false, - ignore_on_isolate: false, - ignore_on_snapshot: false, - condition_result: false, - assert_result: false, - transient: false, - in_load_queue: false, - in_dubs_queue: false, - in_cleanup_queue: false, - in_gc_queue: false, - manager:None, - } - } - pub fn set_manager(&mut self,manger: Option>){ - self.manager = manger; - } -} -/* -impl <'l> Default for Unit<'l> { - fn default() -> Self { - Self{ - unitType: UnitType::UNIT_SERVICE, - load_state: UnitLoadState::UNIT_STUB, - id: String::from(""), - instance: String::from(""), - name: String::from(""), - depencies: Vec::::new(), - desc: String::from(""), - documnetation: null_str!(""), - fragment_path: null_str!(""), - source_path: null_str!(""), - fragment_mtine: 0, - source_mtime: 0, - dropin_mtime: 0, - units_by_type: Vec::::new(), - has_requires_mounts_for: Vec::::new(), - load_queue: Vec::::new(), - dbus_queue: Vec::::new(), - cleanup_queue: Vec::::new(), - gc_queue: Vec::::new(), - cgroup_queue: Vec::::new(), - pids: HashSet::::new(), - sigchldgen: 0, - gc_marker: 0, - deseialize_job: 0, - load_error: 0, - stop_when_unneeded: false, - refuse_manual_start: false, - allow_isolate: false, - ignore_on_isolate: false, - ignore_on_snapshot: false, - condition_result: false, - assert_result: false, - transient: false, - in_load_queue: false, - in_dubs_queue: false, - in_cleanup_queue: false, - in_gc_queue: false, - manager: None, - } - } -} -*/ -impl UnitObj for Unit{ - - fn init(&self){ - - } - fn done(&self){ - - } - fn check_gc(&self) -> bool { todo!() } -} - -impl UnitObj for MountUnit{ - -fn init(&self) { todo!() } -fn done(&self) { todo!() } -fn load(&self) { todo!() } -fn coldplug(&self) { todo!() } -fn start(&self) { todo!() } -fn dump(&self) { todo!() } -fn stop(&self) { todo!() } -fn reload(&self) { todo!() } -fn kill(&self) { todo!() } -fn check_gc(&self) -> bool { todo!() } -fn release_resources(&self) { todo!() } -fn check_snapshot(&self) { todo!() } -fn sigchld_events(&self, _: u64, _: i32, _: i32) { todo!() } -fn reset_failed(&self) { todo!() } -} - - -pub struct UnitManager { - units: RefCell>>>, -} - -impl UnitManager{ - pub fn new() -> Self{ - UnitManager {units: RefCell::new(Vec::new())} - } -} - -impl manager::Mangerobj for UnitManager { - - fn init(&self){ - } - - fn load(&self){ - let mut units_vec = self.units.borrow_mut(); - - let mut unit: Unit = Unit::new(); - - units_vec.push(RefCell::new(Box::new(unit))); - } - - fn dispatch(&self) -> i32 { - 0 - } - - fn reload(&self) -> Option{ - None - } - - fn destroy(&self){ - - } - - // reserved for sd event - fn event_dispatch(&self) -> Option{ - None - } -} - - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn test_mangerplugin(){ - let unit_manger = UnitManager::new(); - let mut mp = manager::MangerPlugins::new(); - mp.load_plugins(Box::new(unit_manger)); - assert_eq!(mp.run(),0); - } -} \ No newline at end of file -- Gitee From d41d48b8744bef236d28c862e2a613e9d66a5615 Mon Sep 17 00:00:00 2001 From: love_hangzhou Date: Tue, 2 Nov 2021 08:56:13 +0800 Subject: [PATCH 2/2] remove dead_code --- src/process1/src/lib.rs | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-) diff --git a/src/process1/src/lib.rs b/src/process1/src/lib.rs index eca1b3d4..200eb8a0 100644 --- a/src/process1/src/lib.rs +++ b/src/process1/src/lib.rs @@ -6,18 +6,6 @@ mod tests { } } - - -pub mod front_of_hourse { - pub mod hosting { - pub fn add_to_waitlist(name:String) -> Vec { - let mut result = Vec::new(); - result.push(name); - return result; - } - } -} - pub mod manager; -pub mod plugin; \ No newline at end of file +pub mod plugin; -- Gitee