1e5a8055aSGnoCiYeH use crate::error::parse_error::ParseError; 2e5a8055aSGnoCiYeH use crate::error::parse_error::ParseErrorType; 3e5a8055aSGnoCiYeH use crate::error::runtime_error::RuntimeError; 4e5a8055aSGnoCiYeH use crate::error::runtime_error::RuntimeErrorType; 5f206f17aSGnoCiYeH use crate::parse::parse_util::UnitParseUtil; 64e851715SGnoCiYeH use crate::parse::Segment; 7f206f17aSGnoCiYeH 8f206f17aSGnoCiYeH #[cfg(target_os = "dragonos")] 9f206f17aSGnoCiYeH use drstd as std; 10e5a8055aSGnoCiYeH use hashbrown::HashMap; 11f206f17aSGnoCiYeH 124e851715SGnoCiYeH use std::any::Any; 13f206f17aSGnoCiYeH use std::default::Default; 14e5a8055aSGnoCiYeH use std::fmt::Debug; 15f206f17aSGnoCiYeH use std::result::Result; 16f206f17aSGnoCiYeH use std::result::Result::Err; 17f206f17aSGnoCiYeH use std::result::Result::Ok; 1821fc724cSGnoCiYeH use std::string::String; 19e5a8055aSGnoCiYeH use std::sync::atomic::AtomicUsize; 20e5a8055aSGnoCiYeH use std::sync::atomic::Ordering; 21e5a8055aSGnoCiYeH use std::sync::Arc; 2221fc724cSGnoCiYeH use std::vec::Vec; 2321fc724cSGnoCiYeH 244e851715SGnoCiYeH pub mod service; 254e851715SGnoCiYeH pub mod target; 2621fc724cSGnoCiYeH 2721fc724cSGnoCiYeH use self::target::TargetUnit; 28e5a8055aSGnoCiYeH use lazy_static::lazy_static; 29e5a8055aSGnoCiYeH 30e5a8055aSGnoCiYeH pub fn generate_unit_id() -> usize { 31e5a8055aSGnoCiYeH static UNIT_ID: AtomicUsize = AtomicUsize::new(1); 32e5a8055aSGnoCiYeH return UNIT_ID.fetch_add(1, Ordering::SeqCst); 33e5a8055aSGnoCiYeH } 3421fc724cSGnoCiYeH 3521fc724cSGnoCiYeH //所有可解析的Unit都应该实现该trait 36e5a8055aSGnoCiYeH pub trait Unit: Sync + Send + Debug { 374e851715SGnoCiYeH /// @brief 从文件获取到Unit,该函数是解析Unit文件的入口函数 384e851715SGnoCiYeH /// 394e851715SGnoCiYeH /// 从path解析Unit属性 404e851715SGnoCiYeH /// 414e851715SGnoCiYeH /// @param path 需解析的文件 424e851715SGnoCiYeH /// 43e5a8055aSGnoCiYeH /// @return 解析成功则返回对应Unit的Arc指针,否则返回Err 44e5a8055aSGnoCiYeH fn from_path(path: &str) -> Result<Arc<Self>, ParseError> 454e851715SGnoCiYeH where 464e851715SGnoCiYeH Self: Sized; 474e851715SGnoCiYeH 484e851715SGnoCiYeH fn as_any(&self) -> &dyn Any; 494e851715SGnoCiYeH 504e851715SGnoCiYeH /// @brief 设置Unit属性 514e851715SGnoCiYeH /// 524e851715SGnoCiYeH /// 设置对应Unit属性 534e851715SGnoCiYeH /// 544e851715SGnoCiYeH /// @param segment 属性段类型 554e851715SGnoCiYeH /// 564e851715SGnoCiYeH /// @param attr 属性名 574e851715SGnoCiYeH /// 584e851715SGnoCiYeH /// @param val 属性值 594e851715SGnoCiYeH /// 604e851715SGnoCiYeH /// @return 设置成功则返回Ok(()),否则返回Err 614e851715SGnoCiYeH fn set_attr(&mut self, segment: Segment, attr: &str, val: &str) -> Result<(), ParseError>; 62f206f17aSGnoCiYeH 63f206f17aSGnoCiYeH /// # 设置每个Unit都应该有的属性 64f206f17aSGnoCiYeH /// 65f206f17aSGnoCiYeH /// 设置BaseUnit 66f206f17aSGnoCiYeH /// 67f206f17aSGnoCiYeH /// ## param unit_base 设置值 68f206f17aSGnoCiYeH fn set_unit_base(&mut self, unit_base: BaseUnit); 69f206f17aSGnoCiYeH 70f206f17aSGnoCiYeH /// # 获取UnitType 71f206f17aSGnoCiYeH /// 72f206f17aSGnoCiYeH /// ## return UnitType 73f206f17aSGnoCiYeH fn unit_type(&self) -> UnitType; 74e5a8055aSGnoCiYeH 75e5a8055aSGnoCiYeH fn unit_base(&self) -> &BaseUnit; 76e5a8055aSGnoCiYeH 77e5a8055aSGnoCiYeH fn mut_unit_base(&mut self) -> &mut BaseUnit; 78e5a8055aSGnoCiYeH 79e5a8055aSGnoCiYeH fn unit_id(&self) -> usize; 80e5a8055aSGnoCiYeH 81e5a8055aSGnoCiYeH /// ## Unit的工作逻辑 82e5a8055aSGnoCiYeH /// 83e5a8055aSGnoCiYeH /// ### return OK(())/Err 84e5a8055aSGnoCiYeH fn run(&self) -> Result<(), RuntimeError>; 85e5a8055aSGnoCiYeH 86e5a8055aSGnoCiYeH /// ## 设置unit_id 87e5a8055aSGnoCiYeH /// 88e5a8055aSGnoCiYeH /// ### return OK(())/Err 89*99ecc59fSGnoCiYeH fn set_unit_id(&mut self) -> usize { 90*99ecc59fSGnoCiYeH let ret = generate_unit_id(); 91*99ecc59fSGnoCiYeH self.mut_unit_base().set_id(ret); 92*99ecc59fSGnoCiYeH ret 93e5a8055aSGnoCiYeH } 94e5a8055aSGnoCiYeH } 95e5a8055aSGnoCiYeH 96e5a8055aSGnoCiYeH pub struct Downcast; 97e5a8055aSGnoCiYeH impl Downcast { 98e5a8055aSGnoCiYeH fn downcast<T: Unit + Clone + 'static>(unit: Arc<dyn Unit>) -> Result<Arc<T>,RuntimeError> { 99e5a8055aSGnoCiYeH let any = unit.as_any(); 100e5a8055aSGnoCiYeH let unit = match any.downcast_ref::<T>(){ 101e5a8055aSGnoCiYeH Some(v) => v, 102e5a8055aSGnoCiYeH None => { 103e5a8055aSGnoCiYeH return Err(RuntimeError::new(RuntimeErrorType::DatabaseError)); 104e5a8055aSGnoCiYeH } 105e5a8055aSGnoCiYeH }; 106e5a8055aSGnoCiYeH 107e5a8055aSGnoCiYeH return Ok(Arc::new(unit.clone())); 108e5a8055aSGnoCiYeH } 10921fc724cSGnoCiYeH } 11021fc724cSGnoCiYeH 1114e851715SGnoCiYeH //Unit状态 112e5a8055aSGnoCiYeH #[derive(Clone, Copy, Debug,PartialEq)] 113e5a8055aSGnoCiYeH pub enum UnitState { 11421fc724cSGnoCiYeH Enabled, 11521fc724cSGnoCiYeH Disabled, 11621fc724cSGnoCiYeH Static, 1174e851715SGnoCiYeH Masked, 11821fc724cSGnoCiYeH } 11921fc724cSGnoCiYeH 1204e851715SGnoCiYeH //Unit类型 1214e851715SGnoCiYeH #[derive(Clone, Copy, PartialEq, Debug)] 1224e851715SGnoCiYeH pub enum UnitType { 12321fc724cSGnoCiYeH Automount, 12421fc724cSGnoCiYeH Device, 12521fc724cSGnoCiYeH Mount, 12621fc724cSGnoCiYeH Path, 12721fc724cSGnoCiYeH Scope, 12821fc724cSGnoCiYeH Service, 12921fc724cSGnoCiYeH Slice, 13021fc724cSGnoCiYeH Snapshot, 13121fc724cSGnoCiYeH Socket, 13221fc724cSGnoCiYeH Swap, 13321fc724cSGnoCiYeH Target, 13421fc724cSGnoCiYeH Timer, 1354e851715SGnoCiYeH Unknown, 13621fc724cSGnoCiYeH } 13721fc724cSGnoCiYeH 1384e851715SGnoCiYeH //记录unit文件基本信息,这个结构体里面的信息是所有Unit文件都可以有的属性 139e5a8055aSGnoCiYeH #[derive(Debug, Clone)] 1404e851715SGnoCiYeH pub struct BaseUnit { 141f206f17aSGnoCiYeH unit_part: UnitPart, 142f206f17aSGnoCiYeH install_part: InstallPart, 143f206f17aSGnoCiYeH state: UnitState, 144f206f17aSGnoCiYeH unit_type: UnitType, 145e5a8055aSGnoCiYeH unit_id: usize, 14621fc724cSGnoCiYeH } 14721fc724cSGnoCiYeH 1484e851715SGnoCiYeH impl Default for BaseUnit { 1494e851715SGnoCiYeH fn default() -> Self { 1504e851715SGnoCiYeH BaseUnit { 1514e851715SGnoCiYeH unit_part: UnitPart::default(), 1524e851715SGnoCiYeH install_part: InstallPart::default(), 1534e851715SGnoCiYeH state: UnitState::Disabled, 1544e851715SGnoCiYeH unit_type: UnitType::Unknown, 155e5a8055aSGnoCiYeH unit_id: 0, 15621fc724cSGnoCiYeH } 15721fc724cSGnoCiYeH } 15821fc724cSGnoCiYeH } 15921fc724cSGnoCiYeH 1604e851715SGnoCiYeH impl BaseUnit { 161f206f17aSGnoCiYeH pub fn set_state(&mut self, state: UnitState) { 162f206f17aSGnoCiYeH self.state = state; 163f206f17aSGnoCiYeH } 164f206f17aSGnoCiYeH 165f206f17aSGnoCiYeH pub fn set_unit_type(&mut self, utype: UnitType) { 166f206f17aSGnoCiYeH self.unit_type = utype; 167f206f17aSGnoCiYeH } 168f206f17aSGnoCiYeH 169f206f17aSGnoCiYeH pub fn set_unit_part_attr( 170f206f17aSGnoCiYeH &mut self, 171f206f17aSGnoCiYeH attr_type: &BaseUnitAttr, 172f206f17aSGnoCiYeH val: &str, 173f206f17aSGnoCiYeH ) -> Result<(), ParseError> { 174f206f17aSGnoCiYeH return self.unit_part.set_attr(attr_type, val); 175f206f17aSGnoCiYeH } 176f206f17aSGnoCiYeH 177f206f17aSGnoCiYeH pub fn set_install_part_attr( 178f206f17aSGnoCiYeH &mut self, 179f206f17aSGnoCiYeH attr_type: &InstallUnitAttr, 180f206f17aSGnoCiYeH val: &str, 181f206f17aSGnoCiYeH ) -> Result<(), ParseError> { 182f206f17aSGnoCiYeH return self.install_part.set_attr(attr_type, val); 183f206f17aSGnoCiYeH } 184f206f17aSGnoCiYeH 1854e851715SGnoCiYeH pub fn parse_and_set_attribute(&self) -> Result<(), ParseError> { 1864e851715SGnoCiYeH return Ok(()); 1874e851715SGnoCiYeH } 1884e851715SGnoCiYeH 189f206f17aSGnoCiYeH pub fn unit_part(&self) -> &UnitPart { 190f206f17aSGnoCiYeH &self.unit_part 191f206f17aSGnoCiYeH } 192f206f17aSGnoCiYeH 193f206f17aSGnoCiYeH pub fn install_part(&self) -> &InstallPart { 194f206f17aSGnoCiYeH &self.install_part 195f206f17aSGnoCiYeH } 196f206f17aSGnoCiYeH 197f206f17aSGnoCiYeH pub fn state(&self) -> &UnitState { 198f206f17aSGnoCiYeH &self.state 199f206f17aSGnoCiYeH } 200f206f17aSGnoCiYeH 201f206f17aSGnoCiYeH pub fn unit_type(&self) -> &UnitType { 202f206f17aSGnoCiYeH &self.unit_type 203f206f17aSGnoCiYeH } 204e5a8055aSGnoCiYeH 205e5a8055aSGnoCiYeH pub fn set_id(&mut self, id: usize) { 206e5a8055aSGnoCiYeH self.unit_id = id; 207e5a8055aSGnoCiYeH } 208f206f17aSGnoCiYeH } 209f206f17aSGnoCiYeH 210e5a8055aSGnoCiYeH #[derive(Default, Debug, Clone)] 2114e851715SGnoCiYeH pub struct Url { 2124e851715SGnoCiYeH pub url_string: String, // pub protocol: String, 2134e851715SGnoCiYeH // pub host: String, 2144e851715SGnoCiYeH // pub port: Option<u16>, 2154e851715SGnoCiYeH // pub path: String, 2164e851715SGnoCiYeH // pub query: Option<String>, 2174e851715SGnoCiYeH // pub fragment: Option<String>, 2184e851715SGnoCiYeH } 2194e851715SGnoCiYeH 2204e851715SGnoCiYeH //对应Unit文件的Unit段 221e5a8055aSGnoCiYeH #[derive(Debug, Clone)] 2224e851715SGnoCiYeH pub struct UnitPart { 223f206f17aSGnoCiYeH description: String, 224f206f17aSGnoCiYeH documentation: Vec<Url>, 225*99ecc59fSGnoCiYeH requires: Vec<usize>, 226*99ecc59fSGnoCiYeH wants: Vec<usize>, 227*99ecc59fSGnoCiYeH after: Vec<usize>, 228*99ecc59fSGnoCiYeH before: Vec<usize>, 229*99ecc59fSGnoCiYeH binds_to: Vec<usize>, 230*99ecc59fSGnoCiYeH part_of: Vec<usize>, 231*99ecc59fSGnoCiYeH on_failure: Vec<usize>, 232*99ecc59fSGnoCiYeH conflicts: Vec<usize>, 23321fc724cSGnoCiYeH } 23421fc724cSGnoCiYeH 23521fc724cSGnoCiYeH impl Default for UnitPart { 23621fc724cSGnoCiYeH fn default() -> Self { 23721fc724cSGnoCiYeH UnitPart { 23821fc724cSGnoCiYeH description: String::new(), 23921fc724cSGnoCiYeH documentation: Vec::new(), 24021fc724cSGnoCiYeH requires: Vec::new(), 24121fc724cSGnoCiYeH wants: Vec::new(), 24221fc724cSGnoCiYeH after: Vec::new(), 24321fc724cSGnoCiYeH before: Vec::new(), 24421fc724cSGnoCiYeH binds_to: Vec::new(), 24521fc724cSGnoCiYeH part_of: Vec::new(), 24621fc724cSGnoCiYeH on_failure: Vec::new(), 2474e851715SGnoCiYeH conflicts: Vec::new(), 24821fc724cSGnoCiYeH } 24921fc724cSGnoCiYeH } 25021fc724cSGnoCiYeH } 25121fc724cSGnoCiYeH 252f206f17aSGnoCiYeH impl UnitPart { 253f206f17aSGnoCiYeH pub fn set_attr(&mut self, attr: &BaseUnitAttr, val: &str) -> Result<(), ParseError> { 254f206f17aSGnoCiYeH match attr { 255f206f17aSGnoCiYeH BaseUnitAttr::None => { 256e5a8055aSGnoCiYeH return Err(ParseError::new( 257e5a8055aSGnoCiYeH ParseErrorType::ESyntaxError, 258e5a8055aSGnoCiYeH String::new(), 259e5a8055aSGnoCiYeH 0, 260e5a8055aSGnoCiYeH )); 261f206f17aSGnoCiYeH } 262f206f17aSGnoCiYeH BaseUnitAttr::Description => self.description = String::from(val), 263f206f17aSGnoCiYeH BaseUnitAttr::Documentation => { 264f206f17aSGnoCiYeH self.documentation.extend(UnitParseUtil::parse_url(val)?) 265f206f17aSGnoCiYeH } 266f206f17aSGnoCiYeH BaseUnitAttr::Requires => { 267f206f17aSGnoCiYeH let units = val.split_whitespace().collect::<Vec<&str>>(); 268f206f17aSGnoCiYeH //TODO:目前先加入requires列表,可能会出现循环依赖问题,后续应解决循环依赖问题 269f206f17aSGnoCiYeH for unit_path in units { 270f206f17aSGnoCiYeH self.requires 271f206f17aSGnoCiYeH .push(UnitParseUtil::parse_unit_no_type(unit_path)?); 272f206f17aSGnoCiYeH } 273f206f17aSGnoCiYeH } 274f206f17aSGnoCiYeH BaseUnitAttr::Wants => { 275f206f17aSGnoCiYeH let units = val.split_whitespace().collect::<Vec<&str>>(); 276f206f17aSGnoCiYeH //TODO:目前先加入列表,可能会出现循环依赖问题,后续应解决循环依赖问题 277f206f17aSGnoCiYeH for unit_path in units { 278f206f17aSGnoCiYeH self.wants 279f206f17aSGnoCiYeH .push(UnitParseUtil::parse_unit_no_type(unit_path)?); 280f206f17aSGnoCiYeH } 281f206f17aSGnoCiYeH } 282f206f17aSGnoCiYeH BaseUnitAttr::After => { 283f206f17aSGnoCiYeH let units = val.split_whitespace().collect::<Vec<&str>>(); 284f206f17aSGnoCiYeH //TODO:目前先加入列表,可能会出现循环依赖问题,后续应解决循环依赖问题 285f206f17aSGnoCiYeH for unit_path in units { 286f206f17aSGnoCiYeH self.after 287f206f17aSGnoCiYeH .push(UnitParseUtil::parse_unit_no_type(unit_path)?); 288f206f17aSGnoCiYeH } 289f206f17aSGnoCiYeH } 290f206f17aSGnoCiYeH BaseUnitAttr::Before => { 291f206f17aSGnoCiYeH let units = val.split_whitespace().collect::<Vec<&str>>(); 292f206f17aSGnoCiYeH //TODO:目前先加入列表,可能会出现循环依赖问题,后续应解决循环依赖问题 293f206f17aSGnoCiYeH for unit_path in units { 294f206f17aSGnoCiYeH self.before 295f206f17aSGnoCiYeH .push(UnitParseUtil::parse_unit_no_type(unit_path)?); 296f206f17aSGnoCiYeH } 297f206f17aSGnoCiYeH } 298f206f17aSGnoCiYeH BaseUnitAttr::BindsTo => { 299f206f17aSGnoCiYeH let units = val.split_whitespace().collect::<Vec<&str>>(); 300f206f17aSGnoCiYeH //TODO:目前先加入列表,可能会出现循环依赖问题,后续应解决循环依赖问题 301f206f17aSGnoCiYeH for unit_path in units { 302f206f17aSGnoCiYeH self.binds_to 303f206f17aSGnoCiYeH .push(UnitParseUtil::parse_unit_no_type(unit_path)?); 304f206f17aSGnoCiYeH } 305f206f17aSGnoCiYeH } 306f206f17aSGnoCiYeH BaseUnitAttr::PartOf => { 307f206f17aSGnoCiYeH let units = val.split_whitespace().collect::<Vec<&str>>(); 308f206f17aSGnoCiYeH //TODO:目前先加入列表,可能会出现循环依赖问题,后续应解决循环依赖问题 309f206f17aSGnoCiYeH for unit_path in units { 310f206f17aSGnoCiYeH self.part_of 311f206f17aSGnoCiYeH .push(UnitParseUtil::parse_unit_no_type(unit_path)?); 312f206f17aSGnoCiYeH } 313f206f17aSGnoCiYeH } 314f206f17aSGnoCiYeH BaseUnitAttr::OnFailure => { 315f206f17aSGnoCiYeH let units = val.split_whitespace().collect::<Vec<&str>>(); 316f206f17aSGnoCiYeH //TODO:目前先加入列表,可能会出现循环依赖问题,后续应解决循环依赖问题 317f206f17aSGnoCiYeH for unit_path in units { 318f206f17aSGnoCiYeH self.on_failure 319f206f17aSGnoCiYeH .push(UnitParseUtil::parse_unit_no_type(unit_path)?); 320f206f17aSGnoCiYeH } 321f206f17aSGnoCiYeH } 322f206f17aSGnoCiYeH BaseUnitAttr::Conflicts => { 323f206f17aSGnoCiYeH let units = val.split_whitespace().collect::<Vec<&str>>(); 324f206f17aSGnoCiYeH //TODO:目前先加入列表,可能会出现循环依赖问题,后续应解决循环依赖问题 325f206f17aSGnoCiYeH for unit_path in units { 326f206f17aSGnoCiYeH let unit = UnitParseUtil::parse_unit_no_type(unit_path)?; 327f206f17aSGnoCiYeH self.conflicts.push(unit); 328f206f17aSGnoCiYeH } 329f206f17aSGnoCiYeH } 330f206f17aSGnoCiYeH } 331f206f17aSGnoCiYeH return Ok(()); 332f206f17aSGnoCiYeH } 333f206f17aSGnoCiYeH 334f206f17aSGnoCiYeH pub fn description(&self) -> &str { 335f206f17aSGnoCiYeH &self.description 336f206f17aSGnoCiYeH } 337f206f17aSGnoCiYeH 338f206f17aSGnoCiYeH pub fn documentation(&self) -> &[Url] { 339f206f17aSGnoCiYeH &self.documentation 340f206f17aSGnoCiYeH } 341f206f17aSGnoCiYeH 342*99ecc59fSGnoCiYeH pub fn requires(&self) -> &[usize] { 343f206f17aSGnoCiYeH &self.requires 344f206f17aSGnoCiYeH } 345f206f17aSGnoCiYeH 346*99ecc59fSGnoCiYeH pub fn wants(&self) -> &[usize] { 347f206f17aSGnoCiYeH &self.wants 348f206f17aSGnoCiYeH } 349f206f17aSGnoCiYeH 350*99ecc59fSGnoCiYeH pub fn after(&self) -> &[usize] { 351f206f17aSGnoCiYeH &self.after 352f206f17aSGnoCiYeH } 353f206f17aSGnoCiYeH 354*99ecc59fSGnoCiYeH pub fn before(&self) -> &[usize] { 355f206f17aSGnoCiYeH &self.before 356f206f17aSGnoCiYeH } 357f206f17aSGnoCiYeH 358*99ecc59fSGnoCiYeH pub fn binds_to(&self) -> &[usize] { 359f206f17aSGnoCiYeH &self.binds_to 360f206f17aSGnoCiYeH } 361f206f17aSGnoCiYeH 362*99ecc59fSGnoCiYeH pub fn part_of(&self) -> &[usize] { 363f206f17aSGnoCiYeH &self.part_of 364f206f17aSGnoCiYeH } 365f206f17aSGnoCiYeH 366*99ecc59fSGnoCiYeH pub fn on_failure(&self) -> &[usize] { 367f206f17aSGnoCiYeH &self.on_failure 368f206f17aSGnoCiYeH } 369f206f17aSGnoCiYeH 370*99ecc59fSGnoCiYeH pub fn conflicts(&self) -> &[usize] { 371f206f17aSGnoCiYeH &self.conflicts 372f206f17aSGnoCiYeH } 373f206f17aSGnoCiYeH } 374f206f17aSGnoCiYeH 3754e851715SGnoCiYeH //对应Unit文件的Install段 376e5a8055aSGnoCiYeH #[derive(Debug, Clone)] 3774e851715SGnoCiYeH pub struct InstallPart { 378e5a8055aSGnoCiYeH wanted_by: Vec<Arc<TargetUnit>>, 379e5a8055aSGnoCiYeH requires_by: Vec<Arc<TargetUnit>>, 380*99ecc59fSGnoCiYeH also: Vec<usize>, 381f206f17aSGnoCiYeH alias: String, 38221fc724cSGnoCiYeH } 38321fc724cSGnoCiYeH 38421fc724cSGnoCiYeH impl Default for InstallPart { 38521fc724cSGnoCiYeH fn default() -> Self { 38621fc724cSGnoCiYeH InstallPart { 38721fc724cSGnoCiYeH wanted_by: Vec::new(), 38821fc724cSGnoCiYeH requires_by: Vec::new(), 38921fc724cSGnoCiYeH also: Vec::new(), 3904e851715SGnoCiYeH alias: String::new(), 39121fc724cSGnoCiYeH } 39221fc724cSGnoCiYeH } 39321fc724cSGnoCiYeH } 39421fc724cSGnoCiYeH 395f206f17aSGnoCiYeH impl InstallPart { 396f206f17aSGnoCiYeH pub fn set_attr(&mut self, attr: &InstallUnitAttr, val: &str) -> Result<(), ParseError> { 397f206f17aSGnoCiYeH match attr { 398f206f17aSGnoCiYeH InstallUnitAttr::RequiredBy => { 399f206f17aSGnoCiYeH let units = val.split_whitespace().collect::<Vec<&str>>(); 400f206f17aSGnoCiYeH //TODO:目前先加入列表,可能会出现循环依赖问题,后续应解决循环依赖问题 401f206f17aSGnoCiYeH for unit_path in units { 402f206f17aSGnoCiYeH let unit = UnitParseUtil::parse_unit::<TargetUnit>(unit_path)?; 403f206f17aSGnoCiYeH self.requires_by.push(unit); 404f206f17aSGnoCiYeH } 405f206f17aSGnoCiYeH } 406f206f17aSGnoCiYeH InstallUnitAttr::Also => { 407f206f17aSGnoCiYeH let units = val.split_whitespace().collect::<Vec<&str>>(); 408f206f17aSGnoCiYeH //TODO:目前先加入列表,可能会出现循环依赖问题,后续应解决循环依赖问题 409f206f17aSGnoCiYeH for unit_path in units { 410f206f17aSGnoCiYeH let unit = UnitParseUtil::parse_unit_no_type(unit_path)?; 411f206f17aSGnoCiYeH self.also.push(unit); 412f206f17aSGnoCiYeH } 413f206f17aSGnoCiYeH } 414f206f17aSGnoCiYeH InstallUnitAttr::WantedBy => { 415f206f17aSGnoCiYeH let units = val.split_whitespace().collect::<Vec<&str>>(); 416f206f17aSGnoCiYeH //TODO:目前先加入列表,可能会出现循环依赖问题,后续应解决循环依赖问题 417f206f17aSGnoCiYeH for unit_path in units { 418f206f17aSGnoCiYeH let unit = UnitParseUtil::parse_unit::<TargetUnit>(unit_path)?; 419f206f17aSGnoCiYeH self.wanted_by.push(unit); 420f206f17aSGnoCiYeH } 421f206f17aSGnoCiYeH } 422f206f17aSGnoCiYeH InstallUnitAttr::Alias => { 423f206f17aSGnoCiYeH self.alias = String::from(val); 424f206f17aSGnoCiYeH } 425f206f17aSGnoCiYeH InstallUnitAttr::None => { 426f206f17aSGnoCiYeH return Err(ParseError::new(ParseErrorType::EINVAL, String::new(), 0)); 427f206f17aSGnoCiYeH } 428f206f17aSGnoCiYeH } 429f206f17aSGnoCiYeH return Ok(()); 430f206f17aSGnoCiYeH } 431f206f17aSGnoCiYeH 432e5a8055aSGnoCiYeH pub fn wanted_by(&self) -> &[Arc<TargetUnit>] { 433f206f17aSGnoCiYeH &self.wanted_by 434f206f17aSGnoCiYeH } 435f206f17aSGnoCiYeH 436e5a8055aSGnoCiYeH pub fn requires_by(&self) -> &[Arc<TargetUnit>] { 437f206f17aSGnoCiYeH &self.requires_by 438f206f17aSGnoCiYeH } 439f206f17aSGnoCiYeH 440*99ecc59fSGnoCiYeH pub fn also(&self) -> &[usize] { 441f206f17aSGnoCiYeH &self.also 442f206f17aSGnoCiYeH } 443f206f17aSGnoCiYeH 444f206f17aSGnoCiYeH pub fn alias(&self) -> &str { 445f206f17aSGnoCiYeH &self.alias 446f206f17aSGnoCiYeH } 447f206f17aSGnoCiYeH } 4484e851715SGnoCiYeH //对应Unit文件的各种属性 4494e851715SGnoCiYeH pub enum BaseUnitAttr { 4504e851715SGnoCiYeH None, 4514e851715SGnoCiYeH 4524e851715SGnoCiYeH //Unit段 4534e851715SGnoCiYeH //描述该Unit文件的信息 4544e851715SGnoCiYeH Description, 4554e851715SGnoCiYeH //指定服务文档 4564e851715SGnoCiYeH Documentation, 4574e851715SGnoCiYeH //依赖的其它 Unit 列表 4584e851715SGnoCiYeH Requires, 4594e851715SGnoCiYeH //这个 Unit 启动时,触发启动列出的每个 Unit 模块,而不去考虑这些模板启动是否成功 4604e851715SGnoCiYeH Wants, 4614e851715SGnoCiYeH //后面列出的所有模块全部启动完成以后,才会启动当前的服务 4624e851715SGnoCiYeH After, 4634e851715SGnoCiYeH //在启动指定的任务一个模块之间,都会首先确证当前服务已经运行 4644e851715SGnoCiYeH Before, 4654e851715SGnoCiYeH //这些Unit启动失败时该任务失败,都成功时该任务成功,在这些模板中有任意一个出现意外结束或重启时,这个服务也会跟着终止或重启 4664e851715SGnoCiYeH BindsTo, 4674e851715SGnoCiYeH //仅在列出的任务模块失败或重启时,终止或重启当前服务,而不会随列出模板的启动而启动 4684e851715SGnoCiYeH PartOf, 4694e851715SGnoCiYeH //当这个模板启动失败时,就会自动启动列出的每个模块 4704e851715SGnoCiYeH OnFailure, 4714e851715SGnoCiYeH //与这个模块有冲突的模块,如果列出的模块中有已经在运行的,这个服务就不能启动,反之亦然 4724e851715SGnoCiYeH Conflicts, 4734e851715SGnoCiYeH } 4744e851715SGnoCiYeH 4754e851715SGnoCiYeH pub enum InstallUnitAttr { 4764e851715SGnoCiYeH None, 4774e851715SGnoCiYeH //Install段 4784e851715SGnoCiYeH //依赖当前服务的模块。当前 Unit 激活时(enable)符号链接会放入 /etc/systemd/system 目录下面以 <Target 名> + .wants 后缀构成的子目录中 4794e851715SGnoCiYeH WantedBy, 4804e851715SGnoCiYeH //依赖当前服务的模块。当前 Unit 激活时(enable)符号链接会放入 /etc/systemd/system 目录下面以 <Target 名> + .required 后缀构成的子目录中 4814e851715SGnoCiYeH RequiredBy, 4824e851715SGnoCiYeH //当前 Unit enable/disable 时,同时 enable/disable 的其他 Unit 4834e851715SGnoCiYeH Also, 4844e851715SGnoCiYeH //别名 4854e851715SGnoCiYeH Alias, 48621fc724cSGnoCiYeH } 487