diff --git a/src/process1/src/manager/unit.rs b/src/process1/src/manager/unit.rs index 60691f346ca997fa9e1f9b31fbfc13575df2895c..27dff5e272574732d3dc84f6bc93776e24e38ddf 100644 --- a/src/process1/src/manager/unit.rs +++ b/src/process1/src/manager/unit.rs @@ -9,20 +9,11 @@ use std::fs::File; use std::time::SystemTime; use std::os::unix::fs::FileTypeExt; -use utils:: {time_util, path_lookup, unit_load}; +use utils:: {time_util, path_lookup, unit_config_parser}; enum UnitType { UnitService = 0, - UnitMount, - UnitSwap, - UnitSocket, UnitTarget, - UnitDevice, - UnitAutomount, - UnitTimer, - UnitPath, - UnitSlice, - UnitScope, UnitTypeMax, UnitTypeInvalid, UnitTypeErrnoMax, @@ -75,46 +66,26 @@ enum UnitFileState { pub struct Unit { unit_type: UnitType, load_state: UnitLoadState, - unit_file_state: UnitFileState, id: String, instance: Option, name: String, depencies: Vec, desc: String, documnetation: String, - fragment_path: String, - source_path: String, - fragment_mtine: u128, - source_mtime: u128, - dropin_mtime: u64, + config_file_path: String, + config_file_mtime: u128, - 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, default_dependencies: bool, - perpetual: bool, - conf: Option, + conf: Option, manager: Option>, } @@ -156,38 +127,18 @@ impl Unit { 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(), + config_file_path: null_str!(""), + config_file_mtime: 0, load_queue: Vec::::new(), - dbus_queue: Vec::::new(), cleanup_queue: Vec::::new(), - gc_queue: Vec::::new(), - cgroup_queue: Vec::::new(), + deseialize_job:0, 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, default_dependencies: true, - perpetual: false, - unit_file_state: UnitFileState::UnitFileStateInvalid, manager: None, conf: None, } @@ -215,31 +166,12 @@ impl Unit { if !self.unit_load_dropin() { return false; } - - if !self.source_path.is_empty() { - match fs::metadata(&self.source_path) { - Ok(metadata) => match metadata.modified() { - Ok(time) => { - self.source_mtime = time_util::timespec_load(time); - }, - _ => { - self.source_mtime = 0; - }, - } - - _ => { - self.source_mtime = 0; - } - } - } - return true; } fn unit_load_dropin(&mut self) -> bool { todo!(); - true } fn unit_load_fragment(&mut self) -> bool { @@ -251,12 +183,12 @@ impl Unit { return true; } - if self.fragment_path != fragment { - self.fragment_path = fragment; + if self.config_file_path!= fragment { + self.config_file_path = fragment; } - if !self.fragment_path.is_empty() { - let file = File::open(&self.fragment_path); + if !self.config_file_path.is_empty() { + let file = File::open(&self.config_file_path); let time: SystemTime; match file { @@ -265,27 +197,23 @@ impl Unit { match f.metadata(){ Err(e) => return false, Ok(m) => - - if ((m.is_file() && m.len() <=0) || m.file_type().is_char_device()) { - self.load_state = UnitLoadState::UNIT_MASKED; - if self.perpetual { - self.load_state = UnitLoadState::UNIT_LOADED; - } - self.fragment_mtine = 0; + if (m.is_file() && m.len() <=0) || m.file_type().is_char_device() { + self.load_state = UnitLoadState::UNIT_LOADED; + self.config_file_mtime = 0; } else { self.load_state = UnitLoadState::UNIT_LOADED; - // self.fragment_mtine = time_util::timespec_load(time); - match unit_load::unit_file_load(self.fragment_path.to_string()) { + match unit_config_parser::unit_file_load(self.config_file_path.to_string()) { Ok(conf) => self.conf = Some(conf), Err(e) => { - return false;}, + return false; + }, } } }, } - println!("fragmeng_mtime is: {}", self.fragment_mtine); + println!("config file _mtime is: {}", self.config_file_mtime); } @@ -310,9 +238,9 @@ impl <'l> Default for Unit<'l> { depencies: Vec::::new(), desc: String::from(""), documnetation: null_str!(""), - fragment_path: null_str!(""), + config_file_path: null_str!(""), source_path: null_str!(""), - fragment_mtine: 0, + config_file_mtime: 0, source_mtime: 0, dropin_mtime: 0, units_by_type: Vec::::new(), @@ -379,16 +307,12 @@ fn unit_new(manager: Arc, unit_type: UnitType) -> Box UnitType::UnitService => { return Box::new(service::ServiceUnit::new(unit)) }, - UnitType::UnitSocket => { + UnitType::UnitTarget => { return Box::new(service::ServiceUnit::new(unit)) - } - UnitType::UnitTarget => Box::new(service::ServiceUnit::new(unit)), - UnitType::UnitDevice => Box::new(service::ServiceUnit::new(unit)), - UnitType::UnitDevice => Box::new(service::ServiceUnit::new(unit)), - UnitType::UnitTimer => Box::new(service::ServiceUnit::new(unit)), - (_) => Box::new(service::ServiceUnit::new(unit)), - //TODO - + }, + _ => { + return Box::new(service::ServiceUnit::new(unit)) + }, /* UnitPath, UnitSlice, diff --git a/src/utils/src/unit_config_parser.rs b/src/utils/src/unit_config_parser.rs new file mode 100644 index 0000000000000000000000000000000000000000..59069f156015387e23e227a17418d7eb32957131 --- /dev/null +++ b/src/utils/src/unit_config_parser.rs @@ -0,0 +1,122 @@ +extern crate toml; + +use std::fs::File; +use std::io::prelude::*; +use std::io::{Error, ErrorKind}; + +#[derive(Deserialize)] +#[derive(Debug)] +struct ConfUnit { + #[serde(alias = "Description")] + description: Option, + #[serde(alias = "Documentation")] + documentation: Option, + #[serde(alias = "Requires")] + requires: Option, + #[serde(alias = "Wants")] + wants: Option, +} + +#[derive(Deserialize)] +#[derive(Debug)] +struct ConfService { + #[serde(alias = "ExecStart")] + exec_start: Option, + #[serde(alias = "Sockets")] + sockets: Option, + #[serde(alias = "Restart")] + restart: Option, + #[serde(alias = "RestrictRealtime")] + restrict_realtime: Option, + #[serde(alias = "RebootArgument")] + reboot_argument: Option, + #[serde(alias = "ExecReload")] + exec_reload: Option, + #[serde(alias = "OOMScoreAdjust")] + oom_score_adjust: Option, + #[serde(alias = "RestartSec")] + restart_sec: Option, + #[serde(alias = "Slice")] + slice: Option, + #[serde(alias = "MemoryLimit")] + memory_limit: Option, + #[serde(alias = "MemoryLow")] + memory_low: Option, + #[serde(alias = "MemoryMin")] + memory_min: Option, + #[serde(alias = "MemoryMax")] + memory_max: Option, + #[serde(alias = "MemoryHigh")] + memory_high: Option, + #[serde(alias = "MemorySwapMax")] + memory_swap_max: Option, +} + +#[derive(Deserialize)] +#[derive(Debug)] +struct ConfInstall { + #[serde(alias = "WantedBy")] + wanted_by: Option, + #[serde(alias = "Alias")] + alias: Option, + #[serde(alias = "RequiredBy")] + required_by: Option, + #[serde(alias = "DefaultInstance")] + default_instance: Option, +} + +#[derive(Deserialize)] +#[derive(Debug)] +pub struct Conf { + Unit: Option, + Service: Option, + Install: Option, +} + +pub fn unit_file_load(file_path: String) -> Result { + let mut file = match File::open(file_path) { + Ok(f) => f, + Err(_e) => { return Err(Error::new(ErrorKind::Other, + "Error: Open file failed"));} + }; + + let mut buf = String::new(); + match file.read_to_string(&mut buf) { + Ok(s) => s, + Err(_e) => {return Err(Error::new(ErrorKind::Other, + "read file content failed"));} + }; + + let conf: Conf = match toml::from_str(&buf) { + Ok(conf) => conf, + Err(_e) => {return Err(Error::new(ErrorKind::Other, + "translate string to struct failed"));} + }; + + return Ok(conf); +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_unit_file_load() -> Result<(), Error>{ + let file: String = String::from("config.toml"); + let conf = match unit_file_load(file) { + Ok(conf) => { + match conf.Install { + Some(c) => assert_eq!(c.wanted_by, Some("dbus".to_string())), + None => { + return Err(Error::new(ErrorKind::Other, + "no install field")); + } + } + } + Err(e) => { return Err(Error::new(ErrorKind::Other, + e.to_string()));} + }; + + Ok(()) + } +}