1e5a8055aSGnoCiYeH use crate::error::parse_error::ParseError; 2e5a8055aSGnoCiYeH use crate::error::parse_error::ParseErrorType; 3e5a8055aSGnoCiYeH use crate::error::runtime_error::RuntimeError; 4*b40b6b4dSGnoCiYeH 517ae4661SGnoCiYeH use crate::executor::ExitStatus; 6f206f17aSGnoCiYeH use crate::parse::parse_util::UnitParseUtil; 74e851715SGnoCiYeH use crate::parse::Segment; 8f206f17aSGnoCiYeH 9f206f17aSGnoCiYeH #[cfg(target_os = "dragonos")] 10f206f17aSGnoCiYeH use drstd as std; 11f206f17aSGnoCiYeH 124e851715SGnoCiYeH use std::any::Any; 13f206f17aSGnoCiYeH use std::default::Default; 14e5a8055aSGnoCiYeH use std::fmt::Debug; 1517ae4661SGnoCiYeH use std::marker::{Send, Sized, Sync}; 16*b40b6b4dSGnoCiYeH 17f206f17aSGnoCiYeH use std::result::Result; 18f206f17aSGnoCiYeH use std::result::Result::Err; 19f206f17aSGnoCiYeH use std::result::Result::Ok; 2021fc724cSGnoCiYeH use std::string::String; 21e5a8055aSGnoCiYeH use std::sync::atomic::AtomicUsize; 22e5a8055aSGnoCiYeH use std::sync::atomic::Ordering; 23*b40b6b4dSGnoCiYeH 2421fc724cSGnoCiYeH use std::vec::Vec; 2521fc724cSGnoCiYeH 264e851715SGnoCiYeH pub mod service; 274e851715SGnoCiYeH pub mod target; 2821fc724cSGnoCiYeH 2921fc724cSGnoCiYeH use self::target::TargetUnit; 30e5a8055aSGnoCiYeH 31e5a8055aSGnoCiYeH pub fn generate_unit_id() -> usize { 32e5a8055aSGnoCiYeH static UNIT_ID: AtomicUsize = AtomicUsize::new(1); 33e5a8055aSGnoCiYeH return UNIT_ID.fetch_add(1, Ordering::SeqCst); 34e5a8055aSGnoCiYeH } 3521fc724cSGnoCiYeH 3621fc724cSGnoCiYeH //所有可解析的Unit都应该实现该trait 37e5a8055aSGnoCiYeH pub trait Unit: Sync + Send + Debug { 384e851715SGnoCiYeH /// @brief 从文件获取到Unit,该函数是解析Unit文件的入口函数 394e851715SGnoCiYeH /// 404e851715SGnoCiYeH /// 从path解析Unit属性 414e851715SGnoCiYeH /// 424e851715SGnoCiYeH /// @param path 需解析的文件 434e851715SGnoCiYeH /// 4417ae4661SGnoCiYeH /// @return 解析成功则返回对应Unit的id,否则返回Err 4517ae4661SGnoCiYeH fn from_path(path: &str) -> Result<usize, ParseError> 464e851715SGnoCiYeH where 474e851715SGnoCiYeH Self: Sized; 484e851715SGnoCiYeH 494e851715SGnoCiYeH fn as_any(&self) -> &dyn Any; 504e851715SGnoCiYeH 51*b40b6b4dSGnoCiYeH fn as_mut_any(&mut self) -> &mut dyn Any; 52*b40b6b4dSGnoCiYeH 534e851715SGnoCiYeH /// @brief 设置Unit属性 544e851715SGnoCiYeH /// 554e851715SGnoCiYeH /// 设置对应Unit属性 564e851715SGnoCiYeH /// 574e851715SGnoCiYeH /// @param segment 属性段类型 584e851715SGnoCiYeH /// 594e851715SGnoCiYeH /// @param attr 属性名 604e851715SGnoCiYeH /// 614e851715SGnoCiYeH /// @param val 属性值 624e851715SGnoCiYeH /// 634e851715SGnoCiYeH /// @return 设置成功则返回Ok(()),否则返回Err 644e851715SGnoCiYeH fn set_attr(&mut self, segment: Segment, attr: &str, val: &str) -> Result<(), ParseError>; 65f206f17aSGnoCiYeH 66f206f17aSGnoCiYeH /// # 设置每个Unit都应该有的属性 67f206f17aSGnoCiYeH /// 68f206f17aSGnoCiYeH /// 设置BaseUnit 69f206f17aSGnoCiYeH /// 70f206f17aSGnoCiYeH /// ## param unit_base 设置值 71f206f17aSGnoCiYeH fn set_unit_base(&mut self, unit_base: BaseUnit); 72f206f17aSGnoCiYeH 73f206f17aSGnoCiYeH /// # 获取UnitType 74f206f17aSGnoCiYeH /// 75f206f17aSGnoCiYeH /// ## return UnitType 76f206f17aSGnoCiYeH fn unit_type(&self) -> UnitType; 77e5a8055aSGnoCiYeH 78e5a8055aSGnoCiYeH fn unit_base(&self) -> &BaseUnit; 79e5a8055aSGnoCiYeH 80e5a8055aSGnoCiYeH fn mut_unit_base(&mut self) -> &mut BaseUnit; 81e5a8055aSGnoCiYeH 82e5a8055aSGnoCiYeH fn unit_id(&self) -> usize; 83e5a8055aSGnoCiYeH 84e5a8055aSGnoCiYeH /// ## Unit的工作逻辑 85e5a8055aSGnoCiYeH /// 86e5a8055aSGnoCiYeH /// ### return OK(())/Err 8717ae4661SGnoCiYeH fn run(&mut self) -> Result<(), RuntimeError>; 88e5a8055aSGnoCiYeH 89e5a8055aSGnoCiYeH /// ## 设置unit_id 90e5a8055aSGnoCiYeH /// 91e5a8055aSGnoCiYeH /// ### return OK(())/Err 9299ecc59fSGnoCiYeH fn set_unit_id(&mut self) -> usize { 9399ecc59fSGnoCiYeH let ret = generate_unit_id(); 9499ecc59fSGnoCiYeH self.mut_unit_base().set_id(ret); 9599ecc59fSGnoCiYeH ret 96e5a8055aSGnoCiYeH } 97e5a8055aSGnoCiYeH 9817ae4661SGnoCiYeH /// ## Unit退出后逻辑 9917ae4661SGnoCiYeH /// 10017ae4661SGnoCiYeH /// 一般只有可运行的Unit(如Service)需要重写此函数 101*b40b6b4dSGnoCiYeH fn after_exit(&mut self, _exit_status: ExitStatus) {} 102*b40b6b4dSGnoCiYeH 103*b40b6b4dSGnoCiYeH /// ## 初始化Unit内任务的一些参数,各个Unit所需处理的不相同,故放在总的Unit trait 104*b40b6b4dSGnoCiYeH fn init(&mut self) {} 105*b40b6b4dSGnoCiYeH 106*b40b6b4dSGnoCiYeH /// ## Unit的显式退出逻辑 107*b40b6b4dSGnoCiYeH fn exit(&mut self); 10821fc724cSGnoCiYeH } 10921fc724cSGnoCiYeH 1104e851715SGnoCiYeH //Unit状态 111e5a8055aSGnoCiYeH #[derive(Clone, Copy, Debug, PartialEq)] 112e5a8055aSGnoCiYeH pub enum UnitState { 11321fc724cSGnoCiYeH Enabled, 11421fc724cSGnoCiYeH Disabled, 11521fc724cSGnoCiYeH Static, 1164e851715SGnoCiYeH Masked, 11721fc724cSGnoCiYeH } 11821fc724cSGnoCiYeH 1194e851715SGnoCiYeH //Unit类型 1204e851715SGnoCiYeH #[derive(Clone, Copy, PartialEq, Debug)] 1214e851715SGnoCiYeH pub enum UnitType { 12221fc724cSGnoCiYeH Automount, 12321fc724cSGnoCiYeH Device, 12421fc724cSGnoCiYeH Mount, 12521fc724cSGnoCiYeH Path, 12621fc724cSGnoCiYeH Scope, 12721fc724cSGnoCiYeH Service, 12821fc724cSGnoCiYeH Slice, 12921fc724cSGnoCiYeH Snapshot, 13021fc724cSGnoCiYeH Socket, 13121fc724cSGnoCiYeH Swap, 13221fc724cSGnoCiYeH Target, 13321fc724cSGnoCiYeH Timer, 1344e851715SGnoCiYeH Unknown, 13521fc724cSGnoCiYeH } 13621fc724cSGnoCiYeH 1374e851715SGnoCiYeH //记录unit文件基本信息,这个结构体里面的信息是所有Unit文件都可以有的属性 138e5a8055aSGnoCiYeH #[derive(Debug, Clone)] 1394e851715SGnoCiYeH pub struct BaseUnit { 140f206f17aSGnoCiYeH unit_part: UnitPart, 141f206f17aSGnoCiYeH install_part: InstallPart, 142f206f17aSGnoCiYeH state: UnitState, 143f206f17aSGnoCiYeH unit_type: UnitType, 144e5a8055aSGnoCiYeH unit_id: usize, 14521fc724cSGnoCiYeH } 14621fc724cSGnoCiYeH 1474e851715SGnoCiYeH impl Default for BaseUnit { 1484e851715SGnoCiYeH fn default() -> Self { 1494e851715SGnoCiYeH BaseUnit { 1504e851715SGnoCiYeH unit_part: UnitPart::default(), 1514e851715SGnoCiYeH install_part: InstallPart::default(), 1524e851715SGnoCiYeH state: UnitState::Disabled, 1534e851715SGnoCiYeH unit_type: UnitType::Unknown, 154e5a8055aSGnoCiYeH unit_id: 0, 15521fc724cSGnoCiYeH } 15621fc724cSGnoCiYeH } 15721fc724cSGnoCiYeH } 15821fc724cSGnoCiYeH 1594e851715SGnoCiYeH impl BaseUnit { 160f206f17aSGnoCiYeH pub fn set_state(&mut self, state: UnitState) { 161f206f17aSGnoCiYeH self.state = state; 162f206f17aSGnoCiYeH } 163f206f17aSGnoCiYeH 164f206f17aSGnoCiYeH pub fn set_unit_type(&mut self, utype: UnitType) { 165f206f17aSGnoCiYeH self.unit_type = utype; 166f206f17aSGnoCiYeH } 167f206f17aSGnoCiYeH 168f206f17aSGnoCiYeH pub fn set_unit_part_attr( 169f206f17aSGnoCiYeH &mut self, 170f206f17aSGnoCiYeH attr_type: &BaseUnitAttr, 171f206f17aSGnoCiYeH val: &str, 172f206f17aSGnoCiYeH ) -> Result<(), ParseError> { 173f206f17aSGnoCiYeH return self.unit_part.set_attr(attr_type, val); 174f206f17aSGnoCiYeH } 175f206f17aSGnoCiYeH 176f206f17aSGnoCiYeH pub fn set_install_part_attr( 177f206f17aSGnoCiYeH &mut self, 178f206f17aSGnoCiYeH attr_type: &InstallUnitAttr, 179f206f17aSGnoCiYeH val: &str, 180f206f17aSGnoCiYeH ) -> Result<(), ParseError> { 181f206f17aSGnoCiYeH return self.install_part.set_attr(attr_type, val); 182f206f17aSGnoCiYeH } 183f206f17aSGnoCiYeH 1844e851715SGnoCiYeH pub fn parse_and_set_attribute(&self) -> Result<(), ParseError> { 1854e851715SGnoCiYeH return Ok(()); 1864e851715SGnoCiYeH } 1874e851715SGnoCiYeH 188f206f17aSGnoCiYeH pub fn unit_part(&self) -> &UnitPart { 189f206f17aSGnoCiYeH &self.unit_part 190f206f17aSGnoCiYeH } 191f206f17aSGnoCiYeH 192*b40b6b4dSGnoCiYeH pub fn mut_unit_part(&mut self) -> &mut UnitPart { 193*b40b6b4dSGnoCiYeH &mut self.unit_part 194*b40b6b4dSGnoCiYeH } 195*b40b6b4dSGnoCiYeH 196f206f17aSGnoCiYeH pub fn install_part(&self) -> &InstallPart { 197f206f17aSGnoCiYeH &self.install_part 198f206f17aSGnoCiYeH } 199f206f17aSGnoCiYeH 200f206f17aSGnoCiYeH pub fn state(&self) -> &UnitState { 201f206f17aSGnoCiYeH &self.state 202f206f17aSGnoCiYeH } 203f206f17aSGnoCiYeH 204f206f17aSGnoCiYeH pub fn unit_type(&self) -> &UnitType { 205f206f17aSGnoCiYeH &self.unit_type 206f206f17aSGnoCiYeH } 207e5a8055aSGnoCiYeH 208e5a8055aSGnoCiYeH pub fn set_id(&mut self, id: usize) { 209e5a8055aSGnoCiYeH self.unit_id = id; 210e5a8055aSGnoCiYeH } 211f206f17aSGnoCiYeH } 212f206f17aSGnoCiYeH 213e5a8055aSGnoCiYeH #[derive(Default, Debug, Clone)] 2144e851715SGnoCiYeH pub struct Url { 2154e851715SGnoCiYeH pub url_string: String, // pub protocol: String, 2164e851715SGnoCiYeH // pub host: String, 2174e851715SGnoCiYeH // pub port: Option<u16>, 2184e851715SGnoCiYeH // pub path: String, 2194e851715SGnoCiYeH // pub query: Option<String>, 2204e851715SGnoCiYeH // pub fragment: Option<String>, 2214e851715SGnoCiYeH } 2224e851715SGnoCiYeH 2234e851715SGnoCiYeH //对应Unit文件的Unit段 224e5a8055aSGnoCiYeH #[derive(Debug, Clone)] 2254e851715SGnoCiYeH pub struct UnitPart { 226*b40b6b4dSGnoCiYeH // Unit描述 227f206f17aSGnoCiYeH description: String, 228*b40b6b4dSGnoCiYeH // Unit文档 229f206f17aSGnoCiYeH documentation: Vec<Url>, 230*b40b6b4dSGnoCiYeH // 依赖项,同时与当前Unit启动,任意一个失败,终止该Unit的启动 23199ecc59fSGnoCiYeH requires: Vec<usize>, 232*b40b6b4dSGnoCiYeH // 依赖项,同时与当前Unit启动,不需要考虑其成功与否 23399ecc59fSGnoCiYeH wants: Vec<usize>, 234*b40b6b4dSGnoCiYeH // 该Unit在下列Units启动完成之后才能启动 23599ecc59fSGnoCiYeH after: Vec<usize>, 236*b40b6b4dSGnoCiYeH // after相反的语义 23799ecc59fSGnoCiYeH before: Vec<usize>, 238*b40b6b4dSGnoCiYeH // 与requires类似,但是这些模块任意一个意外结束或者重启时,该Unit也会终止或重启 23999ecc59fSGnoCiYeH binds_to: Vec<usize>, 240*b40b6b4dSGnoCiYeH // 与binds_to类似,但是不会随着当前Unit启动而启动 24199ecc59fSGnoCiYeH part_of: Vec<usize>, 242*b40b6b4dSGnoCiYeH // 启动失败时,需启动的项 24399ecc59fSGnoCiYeH on_failure: Vec<usize>, 244*b40b6b4dSGnoCiYeH // 与当前Unit冲突项 24599ecc59fSGnoCiYeH conflicts: Vec<usize>, 246*b40b6b4dSGnoCiYeH // 与当前Unit绑定的项,当前Unit失败或者重启时这些项也会跟着终止或重启 247*b40b6b4dSGnoCiYeH be_binded_by: Vec<usize>, 24821fc724cSGnoCiYeH } 24921fc724cSGnoCiYeH 25021fc724cSGnoCiYeH impl Default for UnitPart { 25121fc724cSGnoCiYeH fn default() -> Self { 25221fc724cSGnoCiYeH UnitPart { 25321fc724cSGnoCiYeH description: String::new(), 25421fc724cSGnoCiYeH documentation: Vec::new(), 25521fc724cSGnoCiYeH requires: Vec::new(), 25621fc724cSGnoCiYeH wants: Vec::new(), 25721fc724cSGnoCiYeH after: Vec::new(), 25821fc724cSGnoCiYeH before: Vec::new(), 25921fc724cSGnoCiYeH binds_to: Vec::new(), 26021fc724cSGnoCiYeH part_of: Vec::new(), 26121fc724cSGnoCiYeH on_failure: Vec::new(), 2624e851715SGnoCiYeH conflicts: Vec::new(), 263*b40b6b4dSGnoCiYeH be_binded_by: Vec::new(), 26421fc724cSGnoCiYeH } 26521fc724cSGnoCiYeH } 26621fc724cSGnoCiYeH } 26721fc724cSGnoCiYeH 268f206f17aSGnoCiYeH impl UnitPart { 269f206f17aSGnoCiYeH pub fn set_attr(&mut self, attr: &BaseUnitAttr, val: &str) -> Result<(), ParseError> { 270f206f17aSGnoCiYeH match attr { 271f206f17aSGnoCiYeH BaseUnitAttr::None => { 272e5a8055aSGnoCiYeH return Err(ParseError::new( 273e5a8055aSGnoCiYeH ParseErrorType::ESyntaxError, 274e5a8055aSGnoCiYeH String::new(), 275e5a8055aSGnoCiYeH 0, 276e5a8055aSGnoCiYeH )); 277f206f17aSGnoCiYeH } 278f206f17aSGnoCiYeH BaseUnitAttr::Description => self.description = String::from(val), 279f206f17aSGnoCiYeH BaseUnitAttr::Documentation => { 280f206f17aSGnoCiYeH self.documentation.extend(UnitParseUtil::parse_url(val)?) 281f206f17aSGnoCiYeH } 282f206f17aSGnoCiYeH BaseUnitAttr::Requires => { 283f206f17aSGnoCiYeH let units = val.split_whitespace().collect::<Vec<&str>>(); 284f206f17aSGnoCiYeH //TODO:目前先加入requires列表,可能会出现循环依赖问题,后续应解决循环依赖问题 285f206f17aSGnoCiYeH for unit_path in units { 286f206f17aSGnoCiYeH self.requires 287f206f17aSGnoCiYeH .push(UnitParseUtil::parse_unit_no_type(unit_path)?); 288f206f17aSGnoCiYeH } 289f206f17aSGnoCiYeH } 290f206f17aSGnoCiYeH BaseUnitAttr::Wants => { 291f206f17aSGnoCiYeH let units = val.split_whitespace().collect::<Vec<&str>>(); 292f206f17aSGnoCiYeH //TODO:目前先加入列表,可能会出现循环依赖问题,后续应解决循环依赖问题 293f206f17aSGnoCiYeH for unit_path in units { 294f206f17aSGnoCiYeH self.wants 295f206f17aSGnoCiYeH .push(UnitParseUtil::parse_unit_no_type(unit_path)?); 296f206f17aSGnoCiYeH } 297f206f17aSGnoCiYeH } 298f206f17aSGnoCiYeH BaseUnitAttr::After => { 299f206f17aSGnoCiYeH let units = val.split_whitespace().collect::<Vec<&str>>(); 300f206f17aSGnoCiYeH //TODO:目前先加入列表,可能会出现循环依赖问题,后续应解决循环依赖问题 301f206f17aSGnoCiYeH for unit_path in units { 302f206f17aSGnoCiYeH self.after 303f206f17aSGnoCiYeH .push(UnitParseUtil::parse_unit_no_type(unit_path)?); 304f206f17aSGnoCiYeH } 305f206f17aSGnoCiYeH } 306f206f17aSGnoCiYeH BaseUnitAttr::Before => { 307f206f17aSGnoCiYeH let units = val.split_whitespace().collect::<Vec<&str>>(); 308f206f17aSGnoCiYeH //TODO:目前先加入列表,可能会出现循环依赖问题,后续应解决循环依赖问题 309f206f17aSGnoCiYeH for unit_path in units { 310f206f17aSGnoCiYeH self.before 311f206f17aSGnoCiYeH .push(UnitParseUtil::parse_unit_no_type(unit_path)?); 312f206f17aSGnoCiYeH } 313f206f17aSGnoCiYeH } 314f206f17aSGnoCiYeH BaseUnitAttr::BindsTo => { 315f206f17aSGnoCiYeH let units = val.split_whitespace().collect::<Vec<&str>>(); 316f206f17aSGnoCiYeH //TODO:目前先加入列表,可能会出现循环依赖问题,后续应解决循环依赖问题 317f206f17aSGnoCiYeH for unit_path in units { 318f206f17aSGnoCiYeH self.binds_to 319f206f17aSGnoCiYeH .push(UnitParseUtil::parse_unit_no_type(unit_path)?); 320f206f17aSGnoCiYeH } 321f206f17aSGnoCiYeH } 322f206f17aSGnoCiYeH BaseUnitAttr::PartOf => { 323f206f17aSGnoCiYeH let units = val.split_whitespace().collect::<Vec<&str>>(); 324f206f17aSGnoCiYeH //TODO:目前先加入列表,可能会出现循环依赖问题,后续应解决循环依赖问题 325f206f17aSGnoCiYeH for unit_path in units { 326f206f17aSGnoCiYeH self.part_of 327f206f17aSGnoCiYeH .push(UnitParseUtil::parse_unit_no_type(unit_path)?); 328f206f17aSGnoCiYeH } 329f206f17aSGnoCiYeH } 330f206f17aSGnoCiYeH BaseUnitAttr::OnFailure => { 331f206f17aSGnoCiYeH let units = val.split_whitespace().collect::<Vec<&str>>(); 332f206f17aSGnoCiYeH //TODO:目前先加入列表,可能会出现循环依赖问题,后续应解决循环依赖问题 333f206f17aSGnoCiYeH for unit_path in units { 334f206f17aSGnoCiYeH self.on_failure 335f206f17aSGnoCiYeH .push(UnitParseUtil::parse_unit_no_type(unit_path)?); 336f206f17aSGnoCiYeH } 337f206f17aSGnoCiYeH } 338f206f17aSGnoCiYeH BaseUnitAttr::Conflicts => { 339f206f17aSGnoCiYeH let units = val.split_whitespace().collect::<Vec<&str>>(); 340f206f17aSGnoCiYeH //TODO:目前先加入列表,可能会出现循环依赖问题,后续应解决循环依赖问题 341f206f17aSGnoCiYeH for unit_path in units { 342f206f17aSGnoCiYeH let unit = UnitParseUtil::parse_unit_no_type(unit_path)?; 343f206f17aSGnoCiYeH self.conflicts.push(unit); 344f206f17aSGnoCiYeH } 345f206f17aSGnoCiYeH } 346f206f17aSGnoCiYeH } 347f206f17aSGnoCiYeH return Ok(()); 348f206f17aSGnoCiYeH } 349f206f17aSGnoCiYeH 350f206f17aSGnoCiYeH pub fn description(&self) -> &str { 351f206f17aSGnoCiYeH &self.description 352f206f17aSGnoCiYeH } 353f206f17aSGnoCiYeH 354f206f17aSGnoCiYeH pub fn documentation(&self) -> &[Url] { 355f206f17aSGnoCiYeH &self.documentation 356f206f17aSGnoCiYeH } 357f206f17aSGnoCiYeH 35899ecc59fSGnoCiYeH pub fn requires(&self) -> &[usize] { 359f206f17aSGnoCiYeH &self.requires 360f206f17aSGnoCiYeH } 361f206f17aSGnoCiYeH 36299ecc59fSGnoCiYeH pub fn wants(&self) -> &[usize] { 363f206f17aSGnoCiYeH &self.wants 364f206f17aSGnoCiYeH } 365f206f17aSGnoCiYeH 36699ecc59fSGnoCiYeH pub fn after(&self) -> &[usize] { 367f206f17aSGnoCiYeH &self.after 368f206f17aSGnoCiYeH } 369f206f17aSGnoCiYeH 37099ecc59fSGnoCiYeH pub fn before(&self) -> &[usize] { 371f206f17aSGnoCiYeH &self.before 372f206f17aSGnoCiYeH } 373f206f17aSGnoCiYeH 37499ecc59fSGnoCiYeH pub fn binds_to(&self) -> &[usize] { 375f206f17aSGnoCiYeH &self.binds_to 376f206f17aSGnoCiYeH } 377f206f17aSGnoCiYeH 37899ecc59fSGnoCiYeH pub fn part_of(&self) -> &[usize] { 379f206f17aSGnoCiYeH &self.part_of 380f206f17aSGnoCiYeH } 381f206f17aSGnoCiYeH 38299ecc59fSGnoCiYeH pub fn on_failure(&self) -> &[usize] { 383f206f17aSGnoCiYeH &self.on_failure 384f206f17aSGnoCiYeH } 385f206f17aSGnoCiYeH 38699ecc59fSGnoCiYeH pub fn conflicts(&self) -> &[usize] { 387f206f17aSGnoCiYeH &self.conflicts 388f206f17aSGnoCiYeH } 389*b40b6b4dSGnoCiYeH 390*b40b6b4dSGnoCiYeH pub fn be_binded_by(&self) -> &[usize] { 391*b40b6b4dSGnoCiYeH &self.be_binded_by 392*b40b6b4dSGnoCiYeH } 393*b40b6b4dSGnoCiYeH 394*b40b6b4dSGnoCiYeH pub fn push_be_binded_by(&mut self, id: usize) { 395*b40b6b4dSGnoCiYeH if !self.be_binded_by.contains(&id) { 396*b40b6b4dSGnoCiYeH self.be_binded_by.push(id); 397*b40b6b4dSGnoCiYeH } 398*b40b6b4dSGnoCiYeH } 399*b40b6b4dSGnoCiYeH 400*b40b6b4dSGnoCiYeH pub fn push_after_unit(&mut self, id: usize) { 401*b40b6b4dSGnoCiYeH if !self.after.contains(&id) { 402*b40b6b4dSGnoCiYeH self.after.push(id); 403*b40b6b4dSGnoCiYeH } 404*b40b6b4dSGnoCiYeH } 405f206f17aSGnoCiYeH } 406f206f17aSGnoCiYeH 4074e851715SGnoCiYeH //对应Unit文件的Install段 408e5a8055aSGnoCiYeH #[derive(Debug, Clone)] 4094e851715SGnoCiYeH pub struct InstallPart { 41017ae4661SGnoCiYeH wanted_by: Vec<usize>, 41117ae4661SGnoCiYeH requires_by: Vec<usize>, 41299ecc59fSGnoCiYeH also: Vec<usize>, 413f206f17aSGnoCiYeH alias: String, 41421fc724cSGnoCiYeH } 41521fc724cSGnoCiYeH 41621fc724cSGnoCiYeH impl Default for InstallPart { 41721fc724cSGnoCiYeH fn default() -> Self { 41821fc724cSGnoCiYeH InstallPart { 41921fc724cSGnoCiYeH wanted_by: Vec::new(), 42021fc724cSGnoCiYeH requires_by: Vec::new(), 42121fc724cSGnoCiYeH also: Vec::new(), 4224e851715SGnoCiYeH alias: String::new(), 42321fc724cSGnoCiYeH } 42421fc724cSGnoCiYeH } 42521fc724cSGnoCiYeH } 42621fc724cSGnoCiYeH 427f206f17aSGnoCiYeH impl InstallPart { 428f206f17aSGnoCiYeH pub fn set_attr(&mut self, attr: &InstallUnitAttr, val: &str) -> Result<(), ParseError> { 429f206f17aSGnoCiYeH match attr { 430f206f17aSGnoCiYeH InstallUnitAttr::RequiredBy => { 431f206f17aSGnoCiYeH let units = val.split_whitespace().collect::<Vec<&str>>(); 432f206f17aSGnoCiYeH //TODO:目前先加入列表,可能会出现循环依赖问题,后续应解决循环依赖问题 433f206f17aSGnoCiYeH for unit_path in units { 434f206f17aSGnoCiYeH let unit = UnitParseUtil::parse_unit::<TargetUnit>(unit_path)?; 435f206f17aSGnoCiYeH self.requires_by.push(unit); 436f206f17aSGnoCiYeH } 437f206f17aSGnoCiYeH } 438f206f17aSGnoCiYeH InstallUnitAttr::Also => { 439f206f17aSGnoCiYeH let units = val.split_whitespace().collect::<Vec<&str>>(); 440f206f17aSGnoCiYeH //TODO:目前先加入列表,可能会出现循环依赖问题,后续应解决循环依赖问题 441f206f17aSGnoCiYeH for unit_path in units { 442f206f17aSGnoCiYeH let unit = UnitParseUtil::parse_unit_no_type(unit_path)?; 443f206f17aSGnoCiYeH self.also.push(unit); 444f206f17aSGnoCiYeH } 445f206f17aSGnoCiYeH } 446f206f17aSGnoCiYeH InstallUnitAttr::WantedBy => { 447f206f17aSGnoCiYeH let units = val.split_whitespace().collect::<Vec<&str>>(); 448f206f17aSGnoCiYeH //TODO:目前先加入列表,可能会出现循环依赖问题,后续应解决循环依赖问题 449f206f17aSGnoCiYeH for unit_path in units { 450f206f17aSGnoCiYeH let unit = UnitParseUtil::parse_unit::<TargetUnit>(unit_path)?; 451f206f17aSGnoCiYeH self.wanted_by.push(unit); 452f206f17aSGnoCiYeH } 453f206f17aSGnoCiYeH } 454f206f17aSGnoCiYeH InstallUnitAttr::Alias => { 455f206f17aSGnoCiYeH self.alias = String::from(val); 456f206f17aSGnoCiYeH } 457f206f17aSGnoCiYeH InstallUnitAttr::None => { 458f206f17aSGnoCiYeH return Err(ParseError::new(ParseErrorType::EINVAL, String::new(), 0)); 459f206f17aSGnoCiYeH } 460f206f17aSGnoCiYeH } 461f206f17aSGnoCiYeH return Ok(()); 462f206f17aSGnoCiYeH } 463f206f17aSGnoCiYeH 46417ae4661SGnoCiYeH pub fn wanted_by(&self) -> &[usize] { 465f206f17aSGnoCiYeH &self.wanted_by 466f206f17aSGnoCiYeH } 467f206f17aSGnoCiYeH 46817ae4661SGnoCiYeH pub fn requires_by(&self) -> &[usize] { 469f206f17aSGnoCiYeH &self.requires_by 470f206f17aSGnoCiYeH } 471f206f17aSGnoCiYeH 47299ecc59fSGnoCiYeH pub fn also(&self) -> &[usize] { 473f206f17aSGnoCiYeH &self.also 474f206f17aSGnoCiYeH } 475f206f17aSGnoCiYeH 476f206f17aSGnoCiYeH pub fn alias(&self) -> &str { 477f206f17aSGnoCiYeH &self.alias 478f206f17aSGnoCiYeH } 479f206f17aSGnoCiYeH } 4804e851715SGnoCiYeH //对应Unit文件的各种属性 4814e851715SGnoCiYeH pub enum BaseUnitAttr { 4824e851715SGnoCiYeH None, 4834e851715SGnoCiYeH 4844e851715SGnoCiYeH //Unit段 4854e851715SGnoCiYeH //描述该Unit文件的信息 4864e851715SGnoCiYeH Description, 4874e851715SGnoCiYeH //指定服务文档 4884e851715SGnoCiYeH Documentation, 4894e851715SGnoCiYeH //依赖的其它 Unit 列表 4904e851715SGnoCiYeH Requires, 4914e851715SGnoCiYeH //这个 Unit 启动时,触发启动列出的每个 Unit 模块,而不去考虑这些模板启动是否成功 4924e851715SGnoCiYeH Wants, 4934e851715SGnoCiYeH //后面列出的所有模块全部启动完成以后,才会启动当前的服务 4944e851715SGnoCiYeH After, 4954e851715SGnoCiYeH //在启动指定的任务一个模块之间,都会首先确证当前服务已经运行 4964e851715SGnoCiYeH Before, 4974e851715SGnoCiYeH //这些Unit启动失败时该任务失败,都成功时该任务成功,在这些模板中有任意一个出现意外结束或重启时,这个服务也会跟着终止或重启 4984e851715SGnoCiYeH BindsTo, 4994e851715SGnoCiYeH //仅在列出的任务模块失败或重启时,终止或重启当前服务,而不会随列出模板的启动而启动 5004e851715SGnoCiYeH PartOf, 5014e851715SGnoCiYeH //当这个模板启动失败时,就会自动启动列出的每个模块 5024e851715SGnoCiYeH OnFailure, 5034e851715SGnoCiYeH //与这个模块有冲突的模块,如果列出的模块中有已经在运行的,这个服务就不能启动,反之亦然 5044e851715SGnoCiYeH Conflicts, 5054e851715SGnoCiYeH } 5064e851715SGnoCiYeH 5074e851715SGnoCiYeH pub enum InstallUnitAttr { 5084e851715SGnoCiYeH None, 5094e851715SGnoCiYeH //Install段 5104e851715SGnoCiYeH //依赖当前服务的模块。当前 Unit 激活时(enable)符号链接会放入 /etc/systemd/system 目录下面以 <Target 名> + .wants 后缀构成的子目录中 5114e851715SGnoCiYeH WantedBy, 5124e851715SGnoCiYeH //依赖当前服务的模块。当前 Unit 激活时(enable)符号链接会放入 /etc/systemd/system 目录下面以 <Target 名> + .required 后缀构成的子目录中 5134e851715SGnoCiYeH RequiredBy, 5144e851715SGnoCiYeH //当前 Unit enable/disable 时,同时 enable/disable 的其他 Unit 5154e851715SGnoCiYeH Also, 5164e851715SGnoCiYeH //别名 5174e851715SGnoCiYeH Alias, 51821fc724cSGnoCiYeH } 519