diff --git a/src/process1/src/manager/mod.rs b/src/process1/src/manager/mod.rs index ddcec03e6a64c0622df63f0b3068fa53f0d22139..89f6c809c943eb6e1b45732d61c936ac59ece831 100644 --- a/src/process1/src/manager/mod.rs +++ b/src/process1/src/manager/mod.rs @@ -1,2 +1,3 @@ pub mod manager; -pub mod unit; \ No newline at end of file +pub mod unit; +pub mod service; \ No newline at end of file diff --git a/src/process1/src/manager/service.rs b/src/process1/src/manager/service.rs new file mode 100644 index 0000000000000000000000000000000000000000..c26707bc98f8d85b3a6a6f7a008af5602ef317c5 --- /dev/null +++ b/src/process1/src/manager/service.rs @@ -0,0 +1,151 @@ +use super::unit; +use std::io; +use std::ops::{Deref, DerefMut}; + +struct ExitStatusSet { + +} + +enum ServiceTimeoutFailureMode { + ServiceTimeoutTerminate, + ServiceTimeoutAbort, + ServiceTimeoutKill, + ServiceTimeoutFailureModeMax, + ServiceTimeoutFailureModeInvalid = -1, +} + +enum ServiceRestart { + ServiceRestartNo, + ServiceRestartOnSuccess, + ServiceRestartOnFailure, + ServiceRestartOnAbnormal, + ServiceRestartOnAbort, + ServiceRestartAlways, + ServiceRestartMax, + ServiceRestartInvalid = -1 +} + +#[derive(PartialEq, Debug)] +enum ServiceType { + ServiceSimple, + SserviceForking, + ServiceOneshot, + ServiceDbus, + ServiceNotify, + SserviceIdle, + ServiceExec, + ServiceTypeMax, + ServiceTypeInvalid = -1, +} + +/* +impl Default for ServiceType { + fn default() => Self {ServiceType::ServiceTypeInvalid} +} +*/ +struct DualTimestamp { + +} + +pub struct ServiceUnit { + service_unit: unit::Unit, + service_type: ServiceType, + restart: ServiceRestart, + restart_prevent_status: ExitStatusSet, + restart_force_status: ExitStatusSet, + success_status: ExitStatusSet, + pid_file: String, + restart_usec:u64, + timeout_start_usec:u64, + timeout_stop_usec:u64, + timeout_abort_usec:u64, + timeout_abort_set:bool, + runtime_max_usec:u64, + timeout_start_failure_mode: ServiceTimeoutFailureMode, + timeout_stop_failure_mode: ServiceTimeoutFailureMode, + watchdog_timestamp: DualTimestamp, + watchdog_usec: u64, + watchdog_original_usec:u64, + watchdog_override_usec:u64, + watchdog_override_enable:bool, + socket_fd: isize, + bus_name:String, + // TODO + +} + +impl ServiceUnit { + pub fn new(unit: unit::Unit) -> Self { + Self { + service_unit: unit, + service_type: ServiceType::ServiceTypeInvalid, + restart: ServiceRestart::ServiceRestartInvalid, + restart_prevent_status: ExitStatusSet{}, + restart_force_status:ExitStatusSet{}, + success_status:ExitStatusSet{}, + pid_file: String::from(""), + restart_usec: 0, + timeout_start_usec:0, + timeout_stop_usec:0, + timeout_abort_usec:0, + timeout_abort_set:false, + runtime_max_usec:u64::MAX, + timeout_start_failure_mode:ServiceTimeoutFailureMode::ServiceTimeoutFailureModeInvalid, + timeout_stop_failure_mode:ServiceTimeoutFailureMode::ServiceTimeoutFailureModeInvalid, + watchdog_timestamp:DualTimestamp{}, + watchdog_usec:0, + watchdog_original_usec:u64::MAX, + watchdog_override_usec:0, + watchdog_override_enable:false, + socket_fd:-1, + bus_name:String::from(""), + } + } + + pub fn unit_load_fragment_and_dropin(&self) -> Result { + println!("load frament and dropin"); + Ok(0) + } + + pub fn service_add_extras(&mut self) -> Result { + if self.service_type == ServiceType::ServiceTypeInvalid { + if !self.bus_name.is_empty() { + self.service_type = ServiceType::ServiceDbus; + } + } + Ok(0) + } + + pub fn service_verify(&self) -> bool { + true + } +} + +impl unit::UnitObj for ServiceUnit { + fn init(&self) { + todo!() + } + fn done(&self) { todo!() } + fn load(&mut self) -> bool { + match self.unit_load_fragment_and_dropin() { + Err(e) => return false, + Ok(v) => (), + } + + self.service_add_extras(); + + return self.service_verify() + + } + 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!() } +} \ No newline at end of file diff --git a/src/process1/src/manager/unit.rs b/src/process1/src/manager/unit.rs index c382bc8878cb939963567c65e5942f1e32f7f628..2cb69d1313c14a3cef3d00137769829ee4f78eef 100644 --- a/src/process1/src/manager/unit.rs +++ b/src/process1/src/manager/unit.rs @@ -1,4 +1,5 @@ use super::manager; +use super::service; use std::collections::HashSet; use std::cell::RefCell; use std::sync::Arc; @@ -12,7 +13,7 @@ enum UnitType { UNIT_DEVICE, } -enum UnitLoadState { +pub enum UnitLoadState { UNIT_STUB = 0, UNIT_LOADED, UNIT_NOT_FOUND, @@ -37,7 +38,7 @@ enum UnitNameFlags { UNIT_NAME_ANY = 1|2|4, } -struct Unit { +pub struct Unit { unit_type: UnitType, load_state: UnitLoadState, id: String, @@ -82,7 +83,7 @@ struct Unit { pub trait UnitObj { fn init(&self){} fn done(&self){} - fn load(&self){} + fn load(&mut self) -> bool {false} fn coldplug(&self){} fn dump(&self){} fn start(&self){} @@ -152,6 +153,10 @@ impl Unit { pub fn set_manager(&mut self,manger: Option>){ self.manager = manger; } + + pub fn set_load_state(&mut self, load_state: UnitLoadState){ + self.load_state = load_state; + } } /* impl <'l> Default for Unit<'l> { @@ -211,23 +216,33 @@ impl UnitObj for Unit{ } impl UnitObj for MountUnit{ + fn init(&self) { todo!() } + fn done(&self) { todo!() } + fn load(&mut self) -> bool { 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!() } +} -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!() } +fn unit_new(unit_type: UnitType) -> Box { + match unit_type { + UnitType::UNIT_SERVICE => Box::new(service::ServiceUnit::new(Unit::new())), + UnitType::UNIT_SOCKET => Box::new(service::ServiceUnit::new(Unit::new())), + UnitType::UNIT_BUSNAME => Box::new(service::ServiceUnit::new(Unit::new())), + UnitType::UNIT_TARGET => Box::new(service::ServiceUnit::new(Unit::new())), + UnitType::UNIT_SNAPSHOT => Box::new(service::ServiceUnit::new(Unit::new())), + UnitType::UNIT_DEVICE => Box::new(service::ServiceUnit::new(Unit::new())), + //TODO + } } - pub struct UnitManager { units: RefCell>>>, @@ -246,9 +261,9 @@ impl manager::Mangerobj for UnitManager { fn load(&self){ let mut units_vec = self.units.borrow_mut(); - let unit: Unit = Unit::new(); + let mut unit = unit_new(UnitType::UNIT_SERVICE); - units_vec.push(RefCell::new(Box::new(unit))); + units_vec.push(RefCell::new(unit)); } fn dispatch(&self) -> i32 { @@ -281,4 +296,10 @@ mod tests { mp.load_plugins(Box::new(unit_manger)); assert_eq!(mp.run(),0); } + + #[test] + fn test_unit_load(){ + let mut unit = unit_new(UnitType::UNIT_SERVICE); + assert_eq!(unit.load(), true) + } } \ No newline at end of file