xref: /DragonReach/src/unit/mod.rs (revision dfd3fd9812f3584f9392934d1254e24d17661b2d)
1*dfd3fd98S裕依2439 use std::any::Any;
2*dfd3fd98S裕依2439 use std::fmt::Debug;
3*dfd3fd98S裕依2439 use std::sync::atomic::AtomicUsize;
4*dfd3fd98S裕依2439 use std::sync::atomic::Ordering;
5909e4d10SGnoCiYeH 
6e5a8055aSGnoCiYeH use crate::error::parse_error::ParseError;
7e5a8055aSGnoCiYeH use crate::error::parse_error::ParseErrorType;
8e5a8055aSGnoCiYeH use crate::error::runtime_error::RuntimeError;
9b40b6b4dSGnoCiYeH 
1017ae4661SGnoCiYeH use crate::executor::ExitStatus;
11f206f17aSGnoCiYeH use crate::parse::parse_util::UnitParseUtil;
124e851715SGnoCiYeH use crate::parse::Segment;
13f206f17aSGnoCiYeH 
144e851715SGnoCiYeH pub mod service;
154e851715SGnoCiYeH pub mod target;
16909e4d10SGnoCiYeH pub mod timer;
1721fc724cSGnoCiYeH 
1821fc724cSGnoCiYeH use self::target::TargetUnit;
19e5a8055aSGnoCiYeH 
generate_unit_id() -> usize20e5a8055aSGnoCiYeH pub fn generate_unit_id() -> usize {
21e5a8055aSGnoCiYeH     static UNIT_ID: AtomicUsize = AtomicUsize::new(1);
22e5a8055aSGnoCiYeH     return UNIT_ID.fetch_add(1, Ordering::SeqCst);
23e5a8055aSGnoCiYeH }
2421fc724cSGnoCiYeH 
2521fc724cSGnoCiYeH //所有可解析的Unit都应该实现该trait
26e5a8055aSGnoCiYeH pub trait Unit: Sync + Send + Debug {
274e851715SGnoCiYeH     /// @brief 从文件获取到Unit,该函数是解析Unit文件的入口函数
284e851715SGnoCiYeH     ///
294e851715SGnoCiYeH     /// 从path解析Unit属性
304e851715SGnoCiYeH     ///
314e851715SGnoCiYeH     /// @param path 需解析的文件
324e851715SGnoCiYeH     ///
3317ae4661SGnoCiYeH     /// @return 解析成功则返回对应Unit的id,否则返回Err
from_path(path: &str) -> Result<usize, ParseError> where Self: Sized3417ae4661SGnoCiYeH     fn from_path(path: &str) -> Result<usize, ParseError>
354e851715SGnoCiYeH     where
364e851715SGnoCiYeH         Self: Sized;
374e851715SGnoCiYeH 
as_any(&self) -> &dyn Any384e851715SGnoCiYeH     fn as_any(&self) -> &dyn Any;
394e851715SGnoCiYeH 
as_mut_any(&mut self) -> &mut dyn Any40b40b6b4dSGnoCiYeH     fn as_mut_any(&mut self) -> &mut dyn Any;
41b40b6b4dSGnoCiYeH 
424e851715SGnoCiYeH     /// @brief 设置Unit属性
434e851715SGnoCiYeH     ///
444e851715SGnoCiYeH     /// 设置对应Unit属性
454e851715SGnoCiYeH     ///
464e851715SGnoCiYeH     /// @param segment  属性段类型
474e851715SGnoCiYeH     ///
484e851715SGnoCiYeH     /// @param attr     属性名
494e851715SGnoCiYeH     ///
504e851715SGnoCiYeH     /// @param val      属性值
514e851715SGnoCiYeH     ///
524e851715SGnoCiYeH     /// @return 设置成功则返回Ok(()),否则返回Err
set_attr(&mut self, segment: Segment, attr: &str, val: &str) -> Result<(), ParseError>534e851715SGnoCiYeH     fn set_attr(&mut self, segment: Segment, attr: &str, val: &str) -> Result<(), ParseError>;
54f206f17aSGnoCiYeH 
55f206f17aSGnoCiYeH     /// # 设置每个Unit都应该有的属性
56f206f17aSGnoCiYeH     ///
57f206f17aSGnoCiYeH     /// 设置BaseUnit
58f206f17aSGnoCiYeH     ///
59f206f17aSGnoCiYeH     /// ## param unit_base  设置值
set_unit_base(&mut self, unit_base: BaseUnit)60f206f17aSGnoCiYeH     fn set_unit_base(&mut self, unit_base: BaseUnit);
61f206f17aSGnoCiYeH 
62f206f17aSGnoCiYeH     /// # 获取UnitType
63f206f17aSGnoCiYeH     ///
64f206f17aSGnoCiYeH     /// ## return UnitType
unit_type(&self) -> UnitType65f206f17aSGnoCiYeH     fn unit_type(&self) -> UnitType;
66e5a8055aSGnoCiYeH 
unit_base(&self) -> &BaseUnit67e5a8055aSGnoCiYeH     fn unit_base(&self) -> &BaseUnit;
68e5a8055aSGnoCiYeH 
unit_base_mut(&mut self) -> &mut BaseUnit69909e4d10SGnoCiYeH     fn unit_base_mut(&mut self) -> &mut BaseUnit;
70e5a8055aSGnoCiYeH 
unit_id(&self) -> usize71e5a8055aSGnoCiYeH     fn unit_id(&self) -> usize;
72e5a8055aSGnoCiYeH 
73e5a8055aSGnoCiYeH     /// ## Unit的工作逻辑
74e5a8055aSGnoCiYeH     ///
75e5a8055aSGnoCiYeH     /// ### return OK(())/Err
run(&mut self) -> Result<(), RuntimeError>7617ae4661SGnoCiYeH     fn run(&mut self) -> Result<(), RuntimeError>;
77e5a8055aSGnoCiYeH 
78e5a8055aSGnoCiYeH     /// ## 设置unit_id
79e5a8055aSGnoCiYeH     ///
80e5a8055aSGnoCiYeH     /// ### return OK(())/Err
set_unit_id(&mut self) -> usize8199ecc59fSGnoCiYeH     fn set_unit_id(&mut self) -> usize {
8299ecc59fSGnoCiYeH         let ret = generate_unit_id();
83909e4d10SGnoCiYeH         self.unit_base_mut().set_id(ret);
8499ecc59fSGnoCiYeH         ret
85e5a8055aSGnoCiYeH     }
86e5a8055aSGnoCiYeH 
8717ae4661SGnoCiYeH     /// ## Unit退出后逻辑
8817ae4661SGnoCiYeH     ///
8917ae4661SGnoCiYeH     /// 一般只有可运行的Unit(如Service)需要重写此函数
after_exit(&mut self, _exit_status: ExitStatus)90909e4d10SGnoCiYeH     fn after_exit(&mut self, _exit_status: ExitStatus) {
91909e4d10SGnoCiYeH         unimplemented!()
92909e4d10SGnoCiYeH     }
93b40b6b4dSGnoCiYeH 
94b40b6b4dSGnoCiYeH     /// ## 初始化Unit内任务的一些参数,各个Unit所需处理的不相同,故放在总的Unit trait
init(&mut self)95b40b6b4dSGnoCiYeH     fn init(&mut self) {}
96b40b6b4dSGnoCiYeH 
97b40b6b4dSGnoCiYeH     /// ## Unit的显式退出逻辑
exit(&mut self)98b40b6b4dSGnoCiYeH     fn exit(&mut self);
99909e4d10SGnoCiYeH 
set_unit_name(&mut self, name: String)100909e4d10SGnoCiYeH     fn set_unit_name(&mut self, name: String) {
101909e4d10SGnoCiYeH         self.unit_base_mut().unit_name = name;
102909e4d10SGnoCiYeH     }
103909e4d10SGnoCiYeH 
104909e4d10SGnoCiYeH     /// ## Unit重启逻辑
restart(&mut self) -> Result<(), RuntimeError>105909e4d10SGnoCiYeH     fn restart(&mut self) -> Result<(), RuntimeError> {
106909e4d10SGnoCiYeH         unimplemented!()
107909e4d10SGnoCiYeH     }
10821fc724cSGnoCiYeH }
10921fc724cSGnoCiYeH 
1104e851715SGnoCiYeH //Unit状态
11176992bdbSGnoCiYeH #[allow(dead_code)]
112e5a8055aSGnoCiYeH #[derive(Clone, Copy, Debug, PartialEq)]
113e5a8055aSGnoCiYeH pub enum UnitState {
114909e4d10SGnoCiYeH     Active,
115909e4d10SGnoCiYeH     Inactive,
116909e4d10SGnoCiYeH     Activating,
117909e4d10SGnoCiYeH     Deactivating,
118909e4d10SGnoCiYeH     Failed,
119909e4d10SGnoCiYeH     Reloading,
120909e4d10SGnoCiYeH     Maintenance,
121909e4d10SGnoCiYeH }
122909e4d10SGnoCiYeH 
123909e4d10SGnoCiYeH impl ToString for UnitState {
to_string(&self) -> String124909e4d10SGnoCiYeH     fn to_string(&self) -> String {
125909e4d10SGnoCiYeH         match *self {
126909e4d10SGnoCiYeH             UnitState::Active => "active".to_string(),
127909e4d10SGnoCiYeH             UnitState::Inactive => "inactive".to_string(),
128909e4d10SGnoCiYeH             UnitState::Activating => "activeting".to_string(),
129909e4d10SGnoCiYeH             UnitState::Deactivating => "deactivating".to_string(),
130909e4d10SGnoCiYeH             UnitState::Failed => "failed".to_string(),
131909e4d10SGnoCiYeH             UnitState::Reloading => "reloading".to_string(),
132909e4d10SGnoCiYeH             UnitState::Maintenance => "maintenance".to_string(),
133909e4d10SGnoCiYeH         }
134909e4d10SGnoCiYeH     }
135909e4d10SGnoCiYeH }
136909e4d10SGnoCiYeH 
137909e4d10SGnoCiYeH #[allow(dead_code)]
138909e4d10SGnoCiYeH #[derive(Clone, Copy, Debug, PartialEq)]
139909e4d10SGnoCiYeH pub enum UnitSubState {
140909e4d10SGnoCiYeH     Running,
141909e4d10SGnoCiYeH     Waiting,
142909e4d10SGnoCiYeH     StartPre,
143909e4d10SGnoCiYeH     StartPost,
144909e4d10SGnoCiYeH     StopSigterm,
145909e4d10SGnoCiYeH     StopSigkill,
146909e4d10SGnoCiYeH     StopFinalSigterm,
147909e4d10SGnoCiYeH     StopFinalSigkill,
148909e4d10SGnoCiYeH     Dead,
149909e4d10SGnoCiYeH     AutoRestart,
150909e4d10SGnoCiYeH     Failed,
151909e4d10SGnoCiYeH     Activating,
152909e4d10SGnoCiYeH     Deactivating,
153909e4d10SGnoCiYeH     Plugged,
154909e4d10SGnoCiYeH     Unknown,
155909e4d10SGnoCiYeH }
156909e4d10SGnoCiYeH 
157909e4d10SGnoCiYeH impl ToString for UnitSubState {
to_string(&self) -> String158909e4d10SGnoCiYeH     fn to_string(&self) -> String {
159909e4d10SGnoCiYeH         match *self {
160909e4d10SGnoCiYeH             UnitSubState::Running => "running".to_string(),
161909e4d10SGnoCiYeH             UnitSubState::Waiting => "waiting".to_string(),
162909e4d10SGnoCiYeH             UnitSubState::StartPre => "start-pre".to_string(),
163909e4d10SGnoCiYeH             UnitSubState::StartPost => "start-post".to_string(),
164909e4d10SGnoCiYeH             UnitSubState::StopSigterm => "stop-sigterm".to_string(),
165909e4d10SGnoCiYeH             UnitSubState::StopSigkill => "stop-sigkill".to_string(),
166909e4d10SGnoCiYeH             UnitSubState::StopFinalSigterm => "stop-final-sigterm".to_string(),
167909e4d10SGnoCiYeH             UnitSubState::StopFinalSigkill => "stop-final-sigkill".to_string(),
168909e4d10SGnoCiYeH             UnitSubState::Dead => "dead".to_string(),
169909e4d10SGnoCiYeH             UnitSubState::AutoRestart => "auto-restart".to_string(),
170909e4d10SGnoCiYeH             UnitSubState::Failed => "failed".to_string(),
171909e4d10SGnoCiYeH             UnitSubState::Activating => "activating".to_string(),
172909e4d10SGnoCiYeH             UnitSubState::Deactivating => "deactivating".to_string(),
173909e4d10SGnoCiYeH             UnitSubState::Plugged => "plugged".to_string(),
174909e4d10SGnoCiYeH             UnitSubState::Unknown => "unknown".to_string(),
175909e4d10SGnoCiYeH         }
176909e4d10SGnoCiYeH     }
177909e4d10SGnoCiYeH }
178909e4d10SGnoCiYeH 
179909e4d10SGnoCiYeH #[allow(dead_code)]
180909e4d10SGnoCiYeH #[derive(Clone, Copy, Debug, PartialEq)]
181909e4d10SGnoCiYeH pub enum LoadState {
182909e4d10SGnoCiYeH     Loaded,
183909e4d10SGnoCiYeH     NotFound,
184909e4d10SGnoCiYeH     Error,
1854e851715SGnoCiYeH     Masked,
18621fc724cSGnoCiYeH }
18721fc724cSGnoCiYeH 
188909e4d10SGnoCiYeH impl ToString for LoadState {
to_string(&self) -> String189909e4d10SGnoCiYeH     fn to_string(&self) -> String {
190909e4d10SGnoCiYeH         match *self {
191909e4d10SGnoCiYeH             LoadState::Loaded => "loaded".to_string(),
192909e4d10SGnoCiYeH             LoadState::NotFound => "not found".to_string(),
193909e4d10SGnoCiYeH             LoadState::Error => "error".to_string(),
194909e4d10SGnoCiYeH             LoadState::Masked => "maksed".to_string(),
195909e4d10SGnoCiYeH         }
196909e4d10SGnoCiYeH     }
197909e4d10SGnoCiYeH }
198909e4d10SGnoCiYeH 
1994e851715SGnoCiYeH //Unit类型
20076992bdbSGnoCiYeH #[allow(dead_code)]
2014e851715SGnoCiYeH #[derive(Clone, Copy, PartialEq, Debug)]
2024e851715SGnoCiYeH pub enum UnitType {
20321fc724cSGnoCiYeH     Automount,
20421fc724cSGnoCiYeH     Device,
20521fc724cSGnoCiYeH     Mount,
20621fc724cSGnoCiYeH     Path,
20721fc724cSGnoCiYeH     Scope,
20821fc724cSGnoCiYeH     Service,
20921fc724cSGnoCiYeH     Slice,
21021fc724cSGnoCiYeH     Snapshot,
21121fc724cSGnoCiYeH     Socket,
21221fc724cSGnoCiYeH     Swap,
21321fc724cSGnoCiYeH     Target,
21421fc724cSGnoCiYeH     Timer,
2154e851715SGnoCiYeH     Unknown,
21621fc724cSGnoCiYeH }
21721fc724cSGnoCiYeH 
2184e851715SGnoCiYeH //记录unit文件基本信息,这个结构体里面的信息是所有Unit文件都可以有的属性
219909e4d10SGnoCiYeH #[allow(dead_code)]
220e5a8055aSGnoCiYeH #[derive(Debug, Clone)]
2214e851715SGnoCiYeH pub struct BaseUnit {
222909e4d10SGnoCiYeH     unit_name: String,
223f206f17aSGnoCiYeH     unit_part: UnitPart,
224f206f17aSGnoCiYeH     install_part: InstallPart,
225f206f17aSGnoCiYeH     state: UnitState,
226909e4d10SGnoCiYeH     sub_state: UnitSubState,
227909e4d10SGnoCiYeH     load_state: LoadState,
228f206f17aSGnoCiYeH     unit_type: UnitType,
229e5a8055aSGnoCiYeH     unit_id: usize,
23021fc724cSGnoCiYeH }
23121fc724cSGnoCiYeH 
2324e851715SGnoCiYeH impl Default for BaseUnit {
default() -> Self2334e851715SGnoCiYeH     fn default() -> Self {
2344e851715SGnoCiYeH         BaseUnit {
235909e4d10SGnoCiYeH             unit_name: String::new(),
2364e851715SGnoCiYeH             unit_part: UnitPart::default(),
2374e851715SGnoCiYeH             install_part: InstallPart::default(),
238909e4d10SGnoCiYeH             state: UnitState::Inactive,
239909e4d10SGnoCiYeH             sub_state: UnitSubState::Unknown,
240909e4d10SGnoCiYeH             load_state: LoadState::Loaded,
2414e851715SGnoCiYeH             unit_type: UnitType::Unknown,
242e5a8055aSGnoCiYeH             unit_id: 0,
24321fc724cSGnoCiYeH         }
24421fc724cSGnoCiYeH     }
24521fc724cSGnoCiYeH }
24621fc724cSGnoCiYeH 
24776992bdbSGnoCiYeH #[allow(dead_code)]
2484e851715SGnoCiYeH impl BaseUnit {
set_state(&mut self, state: UnitState)249f206f17aSGnoCiYeH     pub fn set_state(&mut self, state: UnitState) {
250f206f17aSGnoCiYeH         self.state = state;
251f206f17aSGnoCiYeH     }
252f206f17aSGnoCiYeH 
set_unit_type(&mut self, utype: UnitType)253f206f17aSGnoCiYeH     pub fn set_unit_type(&mut self, utype: UnitType) {
254f206f17aSGnoCiYeH         self.unit_type = utype;
255f206f17aSGnoCiYeH     }
256f206f17aSGnoCiYeH 
set_unit_part_attr( &mut self, attr_type: &BaseUnitAttr, val: &str, ) -> Result<(), ParseError>257f206f17aSGnoCiYeH     pub fn set_unit_part_attr(
258f206f17aSGnoCiYeH         &mut self,
259f206f17aSGnoCiYeH         attr_type: &BaseUnitAttr,
260f206f17aSGnoCiYeH         val: &str,
261f206f17aSGnoCiYeH     ) -> Result<(), ParseError> {
262f206f17aSGnoCiYeH         return self.unit_part.set_attr(attr_type, val);
263f206f17aSGnoCiYeH     }
264f206f17aSGnoCiYeH 
set_install_part_attr( &mut self, attr_type: &InstallUnitAttr, val: &str, ) -> Result<(), ParseError>265f206f17aSGnoCiYeH     pub fn set_install_part_attr(
266f206f17aSGnoCiYeH         &mut self,
267f206f17aSGnoCiYeH         attr_type: &InstallUnitAttr,
268f206f17aSGnoCiYeH         val: &str,
269f206f17aSGnoCiYeH     ) -> Result<(), ParseError> {
270f206f17aSGnoCiYeH         return self.install_part.set_attr(attr_type, val);
271f206f17aSGnoCiYeH     }
272f206f17aSGnoCiYeH 
parse_and_set_attribute(&self) -> Result<(), ParseError>2734e851715SGnoCiYeH     pub fn parse_and_set_attribute(&self) -> Result<(), ParseError> {
2744e851715SGnoCiYeH         return Ok(());
2754e851715SGnoCiYeH     }
2764e851715SGnoCiYeH 
unit_part(&self) -> &UnitPart277f206f17aSGnoCiYeH     pub fn unit_part(&self) -> &UnitPart {
278f206f17aSGnoCiYeH         &self.unit_part
279f206f17aSGnoCiYeH     }
280f206f17aSGnoCiYeH 
mut_unit_part(&mut self) -> &mut UnitPart281b40b6b4dSGnoCiYeH     pub fn mut_unit_part(&mut self) -> &mut UnitPart {
282b40b6b4dSGnoCiYeH         &mut self.unit_part
283b40b6b4dSGnoCiYeH     }
284b40b6b4dSGnoCiYeH 
install_part(&self) -> &InstallPart285f206f17aSGnoCiYeH     pub fn install_part(&self) -> &InstallPart {
286f206f17aSGnoCiYeH         &self.install_part
287f206f17aSGnoCiYeH     }
288f206f17aSGnoCiYeH 
state(&self) -> &UnitState289f206f17aSGnoCiYeH     pub fn state(&self) -> &UnitState {
290f206f17aSGnoCiYeH         &self.state
291f206f17aSGnoCiYeH     }
292f206f17aSGnoCiYeH 
unit_type(&self) -> &UnitType293f206f17aSGnoCiYeH     pub fn unit_type(&self) -> &UnitType {
294f206f17aSGnoCiYeH         &self.unit_type
295f206f17aSGnoCiYeH     }
296e5a8055aSGnoCiYeH 
set_id(&mut self, id: usize)297e5a8055aSGnoCiYeH     pub fn set_id(&mut self, id: usize) {
298e5a8055aSGnoCiYeH         self.unit_id = id;
299e5a8055aSGnoCiYeH     }
300909e4d10SGnoCiYeH 
unit_name(&self) -> String301909e4d10SGnoCiYeH     pub fn unit_name(&self) -> String {
302909e4d10SGnoCiYeH         self.unit_name.clone()
303909e4d10SGnoCiYeH     }
304909e4d10SGnoCiYeH 
305909e4d10SGnoCiYeH     /// ## Unit基本格式化信息
unit_info(&self) -> String306909e4d10SGnoCiYeH     pub fn unit_info(&self) -> String {
307909e4d10SGnoCiYeH         format!(
308909e4d10SGnoCiYeH             "{}\t\t\t{}\t\t{}\t\t{}\t\t{}",
309909e4d10SGnoCiYeH             self.unit_name,
310909e4d10SGnoCiYeH             self.load_state.to_string(),
311909e4d10SGnoCiYeH             self.state.to_string(),
312909e4d10SGnoCiYeH             self.sub_state.to_string(),
313909e4d10SGnoCiYeH             self.unit_part.description
314909e4d10SGnoCiYeH         )
315909e4d10SGnoCiYeH     }
316f206f17aSGnoCiYeH }
317f206f17aSGnoCiYeH 
318e5a8055aSGnoCiYeH #[derive(Default, Debug, Clone)]
3194e851715SGnoCiYeH pub struct Url {
3204e851715SGnoCiYeH     pub url_string: String, // pub protocol: String,
3214e851715SGnoCiYeH                             // pub host: String,
3224e851715SGnoCiYeH                             // pub port: Option<u16>,
3234e851715SGnoCiYeH                             // pub path: String,
3244e851715SGnoCiYeH                             // pub query: Option<String>,
3254e851715SGnoCiYeH                             // pub fragment: Option<String>,
3264e851715SGnoCiYeH }
3274e851715SGnoCiYeH 
3284e851715SGnoCiYeH //对应Unit文件的Unit段
329e5a8055aSGnoCiYeH #[derive(Debug, Clone)]
3304e851715SGnoCiYeH pub struct UnitPart {
331b40b6b4dSGnoCiYeH     // Unit描述
332f206f17aSGnoCiYeH     description: String,
333b40b6b4dSGnoCiYeH     // Unit文档
334f206f17aSGnoCiYeH     documentation: Vec<Url>,
335b40b6b4dSGnoCiYeH     // 依赖项,同时与当前Unit启动,任意一个失败,终止该Unit的启动
33699ecc59fSGnoCiYeH     requires: Vec<usize>,
337b40b6b4dSGnoCiYeH     // 依赖项,同时与当前Unit启动,不需要考虑其成功与否
33899ecc59fSGnoCiYeH     wants: Vec<usize>,
339b40b6b4dSGnoCiYeH     // 该Unit在下列Units启动完成之后才能启动
34099ecc59fSGnoCiYeH     after: Vec<usize>,
341b40b6b4dSGnoCiYeH     // after相反的语义
34299ecc59fSGnoCiYeH     before: Vec<usize>,
343b40b6b4dSGnoCiYeH     // 与requires类似,但是这些模块任意一个意外结束或者重启时,该Unit也会终止或重启
34499ecc59fSGnoCiYeH     binds_to: Vec<usize>,
345b40b6b4dSGnoCiYeH     // 与binds_to类似,但是不会随着当前Unit启动而启动
34699ecc59fSGnoCiYeH     part_of: Vec<usize>,
347b40b6b4dSGnoCiYeH     // 启动失败时,需启动的项
34899ecc59fSGnoCiYeH     on_failure: Vec<usize>,
349b40b6b4dSGnoCiYeH     // 与当前Unit冲突项
35099ecc59fSGnoCiYeH     conflicts: Vec<usize>,
351b40b6b4dSGnoCiYeH     // 与当前Unit绑定的项,当前Unit失败或者重启时这些项也会跟着终止或重启
352b40b6b4dSGnoCiYeH     be_binded_by: Vec<usize>,
35321fc724cSGnoCiYeH }
35421fc724cSGnoCiYeH 
35521fc724cSGnoCiYeH impl Default for UnitPart {
default() -> Self35621fc724cSGnoCiYeH     fn default() -> Self {
35721fc724cSGnoCiYeH         UnitPart {
35821fc724cSGnoCiYeH             description: String::new(),
35921fc724cSGnoCiYeH             documentation: Vec::new(),
36021fc724cSGnoCiYeH             requires: Vec::new(),
36121fc724cSGnoCiYeH             wants: Vec::new(),
36221fc724cSGnoCiYeH             after: Vec::new(),
36321fc724cSGnoCiYeH             before: Vec::new(),
36421fc724cSGnoCiYeH             binds_to: Vec::new(),
36521fc724cSGnoCiYeH             part_of: Vec::new(),
36621fc724cSGnoCiYeH             on_failure: Vec::new(),
3674e851715SGnoCiYeH             conflicts: Vec::new(),
368b40b6b4dSGnoCiYeH             be_binded_by: Vec::new(),
36921fc724cSGnoCiYeH         }
37021fc724cSGnoCiYeH     }
37121fc724cSGnoCiYeH }
37221fc724cSGnoCiYeH 
37376992bdbSGnoCiYeH #[allow(dead_code)]
374f206f17aSGnoCiYeH impl UnitPart {
set_attr(&mut self, attr: &BaseUnitAttr, val: &str) -> Result<(), ParseError>375f206f17aSGnoCiYeH     pub fn set_attr(&mut self, attr: &BaseUnitAttr, val: &str) -> Result<(), ParseError> {
376f206f17aSGnoCiYeH         match attr {
377f206f17aSGnoCiYeH             BaseUnitAttr::None => {
378e5a8055aSGnoCiYeH                 return Err(ParseError::new(
379e5a8055aSGnoCiYeH                     ParseErrorType::ESyntaxError,
380e5a8055aSGnoCiYeH                     String::new(),
381e5a8055aSGnoCiYeH                     0,
382e5a8055aSGnoCiYeH                 ));
383f206f17aSGnoCiYeH             }
384f206f17aSGnoCiYeH             BaseUnitAttr::Description => self.description = String::from(val),
385f206f17aSGnoCiYeH             BaseUnitAttr::Documentation => {
386f206f17aSGnoCiYeH                 self.documentation.extend(UnitParseUtil::parse_url(val)?)
387f206f17aSGnoCiYeH             }
388f206f17aSGnoCiYeH             BaseUnitAttr::Requires => {
389f206f17aSGnoCiYeH                 let units = val.split_whitespace().collect::<Vec<&str>>();
390f206f17aSGnoCiYeH                 //TODO:目前先加入requires列表,可能会出现循环依赖问题,后续应解决循环依赖问题
391f206f17aSGnoCiYeH                 for unit_path in units {
392f206f17aSGnoCiYeH                     self.requires
393f206f17aSGnoCiYeH                         .push(UnitParseUtil::parse_unit_no_type(unit_path)?);
394f206f17aSGnoCiYeH                 }
395f206f17aSGnoCiYeH             }
396f206f17aSGnoCiYeH             BaseUnitAttr::Wants => {
397f206f17aSGnoCiYeH                 let units = val.split_whitespace().collect::<Vec<&str>>();
398f206f17aSGnoCiYeH                 //TODO:目前先加入列表,可能会出现循环依赖问题,后续应解决循环依赖问题
399f206f17aSGnoCiYeH                 for unit_path in units {
400f206f17aSGnoCiYeH                     self.wants
401f206f17aSGnoCiYeH                         .push(UnitParseUtil::parse_unit_no_type(unit_path)?);
402f206f17aSGnoCiYeH                 }
403f206f17aSGnoCiYeH             }
404f206f17aSGnoCiYeH             BaseUnitAttr::After => {
405f206f17aSGnoCiYeH                 let units = val.split_whitespace().collect::<Vec<&str>>();
406f206f17aSGnoCiYeH                 //TODO:目前先加入列表,可能会出现循环依赖问题,后续应解决循环依赖问题
407f206f17aSGnoCiYeH                 for unit_path in units {
408f206f17aSGnoCiYeH                     self.after
409f206f17aSGnoCiYeH                         .push(UnitParseUtil::parse_unit_no_type(unit_path)?);
410f206f17aSGnoCiYeH                 }
411f206f17aSGnoCiYeH             }
412f206f17aSGnoCiYeH             BaseUnitAttr::Before => {
413f206f17aSGnoCiYeH                 let units = val.split_whitespace().collect::<Vec<&str>>();
414f206f17aSGnoCiYeH                 //TODO:目前先加入列表,可能会出现循环依赖问题,后续应解决循环依赖问题
415f206f17aSGnoCiYeH                 for unit_path in units {
416f206f17aSGnoCiYeH                     self.before
417f206f17aSGnoCiYeH                         .push(UnitParseUtil::parse_unit_no_type(unit_path)?);
418f206f17aSGnoCiYeH                 }
419f206f17aSGnoCiYeH             }
420f206f17aSGnoCiYeH             BaseUnitAttr::BindsTo => {
421f206f17aSGnoCiYeH                 let units = val.split_whitespace().collect::<Vec<&str>>();
422f206f17aSGnoCiYeH                 //TODO:目前先加入列表,可能会出现循环依赖问题,后续应解决循环依赖问题
423f206f17aSGnoCiYeH                 for unit_path in units {
424f206f17aSGnoCiYeH                     self.binds_to
425f206f17aSGnoCiYeH                         .push(UnitParseUtil::parse_unit_no_type(unit_path)?);
426f206f17aSGnoCiYeH                 }
427f206f17aSGnoCiYeH             }
428f206f17aSGnoCiYeH             BaseUnitAttr::PartOf => {
429f206f17aSGnoCiYeH                 let units = val.split_whitespace().collect::<Vec<&str>>();
430f206f17aSGnoCiYeH                 //TODO:目前先加入列表,可能会出现循环依赖问题,后续应解决循环依赖问题
431f206f17aSGnoCiYeH                 for unit_path in units {
432f206f17aSGnoCiYeH                     self.part_of
433f206f17aSGnoCiYeH                         .push(UnitParseUtil::parse_unit_no_type(unit_path)?);
434f206f17aSGnoCiYeH                 }
435f206f17aSGnoCiYeH             }
436f206f17aSGnoCiYeH             BaseUnitAttr::OnFailure => {
437f206f17aSGnoCiYeH                 let units = val.split_whitespace().collect::<Vec<&str>>();
438f206f17aSGnoCiYeH                 //TODO:目前先加入列表,可能会出现循环依赖问题,后续应解决循环依赖问题
439f206f17aSGnoCiYeH                 for unit_path in units {
440f206f17aSGnoCiYeH                     self.on_failure
441f206f17aSGnoCiYeH                         .push(UnitParseUtil::parse_unit_no_type(unit_path)?);
442f206f17aSGnoCiYeH                 }
443f206f17aSGnoCiYeH             }
444f206f17aSGnoCiYeH             BaseUnitAttr::Conflicts => {
445f206f17aSGnoCiYeH                 let units = val.split_whitespace().collect::<Vec<&str>>();
446f206f17aSGnoCiYeH                 //TODO:目前先加入列表,可能会出现循环依赖问题,后续应解决循环依赖问题
447f206f17aSGnoCiYeH                 for unit_path in units {
448f206f17aSGnoCiYeH                     let unit = UnitParseUtil::parse_unit_no_type(unit_path)?;
449f206f17aSGnoCiYeH                     self.conflicts.push(unit);
450f206f17aSGnoCiYeH                 }
451f206f17aSGnoCiYeH             }
452f206f17aSGnoCiYeH         }
453f206f17aSGnoCiYeH         return Ok(());
454f206f17aSGnoCiYeH     }
455f206f17aSGnoCiYeH 
description(&self) -> &str456f206f17aSGnoCiYeH     pub fn description(&self) -> &str {
457f206f17aSGnoCiYeH         &self.description
458f206f17aSGnoCiYeH     }
459f206f17aSGnoCiYeH 
documentation(&self) -> &[Url]460f206f17aSGnoCiYeH     pub fn documentation(&self) -> &[Url] {
461f206f17aSGnoCiYeH         &self.documentation
462f206f17aSGnoCiYeH     }
463f206f17aSGnoCiYeH 
requires(&self) -> &[usize]46499ecc59fSGnoCiYeH     pub fn requires(&self) -> &[usize] {
465f206f17aSGnoCiYeH         &self.requires
466f206f17aSGnoCiYeH     }
467f206f17aSGnoCiYeH 
wants(&self) -> &[usize]46899ecc59fSGnoCiYeH     pub fn wants(&self) -> &[usize] {
469f206f17aSGnoCiYeH         &self.wants
470f206f17aSGnoCiYeH     }
471f206f17aSGnoCiYeH 
after(&self) -> &[usize]47299ecc59fSGnoCiYeH     pub fn after(&self) -> &[usize] {
473f206f17aSGnoCiYeH         &self.after
474f206f17aSGnoCiYeH     }
475f206f17aSGnoCiYeH 
before(&self) -> &[usize]47699ecc59fSGnoCiYeH     pub fn before(&self) -> &[usize] {
477f206f17aSGnoCiYeH         &self.before
478f206f17aSGnoCiYeH     }
479f206f17aSGnoCiYeH 
binds_to(&self) -> &[usize]48099ecc59fSGnoCiYeH     pub fn binds_to(&self) -> &[usize] {
481f206f17aSGnoCiYeH         &self.binds_to
482f206f17aSGnoCiYeH     }
483f206f17aSGnoCiYeH 
part_of(&self) -> &[usize]48499ecc59fSGnoCiYeH     pub fn part_of(&self) -> &[usize] {
485f206f17aSGnoCiYeH         &self.part_of
486f206f17aSGnoCiYeH     }
487f206f17aSGnoCiYeH 
on_failure(&self) -> &[usize]48899ecc59fSGnoCiYeH     pub fn on_failure(&self) -> &[usize] {
489f206f17aSGnoCiYeH         &self.on_failure
490f206f17aSGnoCiYeH     }
491f206f17aSGnoCiYeH 
conflicts(&self) -> &[usize]49299ecc59fSGnoCiYeH     pub fn conflicts(&self) -> &[usize] {
493f206f17aSGnoCiYeH         &self.conflicts
494f206f17aSGnoCiYeH     }
495b40b6b4dSGnoCiYeH 
be_binded_by(&self) -> &[usize]496b40b6b4dSGnoCiYeH     pub fn be_binded_by(&self) -> &[usize] {
497b40b6b4dSGnoCiYeH         &self.be_binded_by
498b40b6b4dSGnoCiYeH     }
499b40b6b4dSGnoCiYeH 
push_be_binded_by(&mut self, id: usize)500b40b6b4dSGnoCiYeH     pub fn push_be_binded_by(&mut self, id: usize) {
501b40b6b4dSGnoCiYeH         if !self.be_binded_by.contains(&id) {
502b40b6b4dSGnoCiYeH             self.be_binded_by.push(id);
503b40b6b4dSGnoCiYeH         }
504b40b6b4dSGnoCiYeH     }
505b40b6b4dSGnoCiYeH 
push_after_unit(&mut self, id: usize)506b40b6b4dSGnoCiYeH     pub fn push_after_unit(&mut self, id: usize) {
507b40b6b4dSGnoCiYeH         if !self.after.contains(&id) {
508b40b6b4dSGnoCiYeH             self.after.push(id);
509b40b6b4dSGnoCiYeH         }
510b40b6b4dSGnoCiYeH     }
511f206f17aSGnoCiYeH }
512f206f17aSGnoCiYeH 
5134e851715SGnoCiYeH //对应Unit文件的Install段
514e5a8055aSGnoCiYeH #[derive(Debug, Clone)]
5154e851715SGnoCiYeH pub struct InstallPart {
51617ae4661SGnoCiYeH     wanted_by: Vec<usize>,
51717ae4661SGnoCiYeH     requires_by: Vec<usize>,
51899ecc59fSGnoCiYeH     also: Vec<usize>,
519f206f17aSGnoCiYeH     alias: String,
52021fc724cSGnoCiYeH }
52121fc724cSGnoCiYeH 
52221fc724cSGnoCiYeH impl Default for InstallPart {
default() -> Self52321fc724cSGnoCiYeH     fn default() -> Self {
52421fc724cSGnoCiYeH         InstallPart {
52521fc724cSGnoCiYeH             wanted_by: Vec::new(),
52621fc724cSGnoCiYeH             requires_by: Vec::new(),
52721fc724cSGnoCiYeH             also: Vec::new(),
5284e851715SGnoCiYeH             alias: String::new(),
52921fc724cSGnoCiYeH         }
53021fc724cSGnoCiYeH     }
53121fc724cSGnoCiYeH }
53221fc724cSGnoCiYeH 
53376992bdbSGnoCiYeH #[allow(dead_code)]
534f206f17aSGnoCiYeH impl InstallPart {
set_attr(&mut self, attr: &InstallUnitAttr, val: &str) -> Result<(), ParseError>535f206f17aSGnoCiYeH     pub fn set_attr(&mut self, attr: &InstallUnitAttr, val: &str) -> Result<(), ParseError> {
536f206f17aSGnoCiYeH         match attr {
537f206f17aSGnoCiYeH             InstallUnitAttr::RequiredBy => {
538f206f17aSGnoCiYeH                 let units = val.split_whitespace().collect::<Vec<&str>>();
539f206f17aSGnoCiYeH                 //TODO:目前先加入列表,可能会出现循环依赖问题,后续应解决循环依赖问题
540f206f17aSGnoCiYeH                 for unit_path in units {
541f206f17aSGnoCiYeH                     let unit = UnitParseUtil::parse_unit::<TargetUnit>(unit_path)?;
542f206f17aSGnoCiYeH                     self.requires_by.push(unit);
543f206f17aSGnoCiYeH                 }
544f206f17aSGnoCiYeH             }
545f206f17aSGnoCiYeH             InstallUnitAttr::Also => {
546f206f17aSGnoCiYeH                 let units = val.split_whitespace().collect::<Vec<&str>>();
547f206f17aSGnoCiYeH                 //TODO:目前先加入列表,可能会出现循环依赖问题,后续应解决循环依赖问题
548f206f17aSGnoCiYeH                 for unit_path in units {
549f206f17aSGnoCiYeH                     let unit = UnitParseUtil::parse_unit_no_type(unit_path)?;
550f206f17aSGnoCiYeH                     self.also.push(unit);
551f206f17aSGnoCiYeH                 }
552f206f17aSGnoCiYeH             }
553f206f17aSGnoCiYeH             InstallUnitAttr::WantedBy => {
554f206f17aSGnoCiYeH                 let units = val.split_whitespace().collect::<Vec<&str>>();
555f206f17aSGnoCiYeH                 //TODO:目前先加入列表,可能会出现循环依赖问题,后续应解决循环依赖问题
556f206f17aSGnoCiYeH                 for unit_path in units {
557f206f17aSGnoCiYeH                     let unit = UnitParseUtil::parse_unit::<TargetUnit>(unit_path)?;
558f206f17aSGnoCiYeH                     self.wanted_by.push(unit);
559f206f17aSGnoCiYeH                 }
560f206f17aSGnoCiYeH             }
561f206f17aSGnoCiYeH             InstallUnitAttr::Alias => {
562f206f17aSGnoCiYeH                 self.alias = String::from(val);
563f206f17aSGnoCiYeH             }
564f206f17aSGnoCiYeH             InstallUnitAttr::None => {
565f206f17aSGnoCiYeH                 return Err(ParseError::new(ParseErrorType::EINVAL, String::new(), 0));
566f206f17aSGnoCiYeH             }
567f206f17aSGnoCiYeH         }
568f206f17aSGnoCiYeH         return Ok(());
569f206f17aSGnoCiYeH     }
570f206f17aSGnoCiYeH 
wanted_by(&self) -> &[usize]57117ae4661SGnoCiYeH     pub fn wanted_by(&self) -> &[usize] {
572f206f17aSGnoCiYeH         &self.wanted_by
573f206f17aSGnoCiYeH     }
574f206f17aSGnoCiYeH 
requires_by(&self) -> &[usize]57517ae4661SGnoCiYeH     pub fn requires_by(&self) -> &[usize] {
576f206f17aSGnoCiYeH         &self.requires_by
577f206f17aSGnoCiYeH     }
578f206f17aSGnoCiYeH 
also(&self) -> &[usize]57999ecc59fSGnoCiYeH     pub fn also(&self) -> &[usize] {
580f206f17aSGnoCiYeH         &self.also
581f206f17aSGnoCiYeH     }
582f206f17aSGnoCiYeH 
alias(&self) -> &str583f206f17aSGnoCiYeH     pub fn alias(&self) -> &str {
584f206f17aSGnoCiYeH         &self.alias
585f206f17aSGnoCiYeH     }
586f206f17aSGnoCiYeH }
5874e851715SGnoCiYeH //对应Unit文件的各种属性
58876992bdbSGnoCiYeH #[allow(dead_code)]
5894e851715SGnoCiYeH pub enum BaseUnitAttr {
5904e851715SGnoCiYeH     None,
5914e851715SGnoCiYeH 
5924e851715SGnoCiYeH     //Unit段
5934e851715SGnoCiYeH     //描述该Unit文件的信息
5944e851715SGnoCiYeH     Description,
5954e851715SGnoCiYeH     //指定服务文档
5964e851715SGnoCiYeH     Documentation,
5974e851715SGnoCiYeH     //依赖的其它 Unit 列表
5984e851715SGnoCiYeH     Requires,
5994e851715SGnoCiYeH     //这个 Unit 启动时,触发启动列出的每个 Unit 模块,而不去考虑这些模板启动是否成功
6004e851715SGnoCiYeH     Wants,
6014e851715SGnoCiYeH     //后面列出的所有模块全部启动完成以后,才会启动当前的服务
6024e851715SGnoCiYeH     After,
6034e851715SGnoCiYeH     //在启动指定的任务一个模块之间,都会首先确证当前服务已经运行
6044e851715SGnoCiYeH     Before,
6054e851715SGnoCiYeH     //这些Unit启动失败时该任务失败,都成功时该任务成功,在这些模板中有任意一个出现意外结束或重启时,这个服务也会跟着终止或重启
6064e851715SGnoCiYeH     BindsTo,
6074e851715SGnoCiYeH     //仅在列出的任务模块失败或重启时,终止或重启当前服务,而不会随列出模板的启动而启动
6084e851715SGnoCiYeH     PartOf,
6094e851715SGnoCiYeH     //当这个模板启动失败时,就会自动启动列出的每个模块
6104e851715SGnoCiYeH     OnFailure,
6114e851715SGnoCiYeH     //与这个模块有冲突的模块,如果列出的模块中有已经在运行的,这个服务就不能启动,反之亦然
6124e851715SGnoCiYeH     Conflicts,
6134e851715SGnoCiYeH }
6144e851715SGnoCiYeH 
61576992bdbSGnoCiYeH #[allow(dead_code)]
6164e851715SGnoCiYeH pub enum InstallUnitAttr {
6174e851715SGnoCiYeH     None,
6184e851715SGnoCiYeH     //Install段
6194e851715SGnoCiYeH     //依赖当前服务的模块。当前 Unit 激活时(enable)符号链接会放入 /etc/systemd/system 目录下面以 <Target 名> + .wants 后缀构成的子目录中
6204e851715SGnoCiYeH     WantedBy,
6214e851715SGnoCiYeH     //依赖当前服务的模块。当前 Unit 激活时(enable)符号链接会放入 /etc/systemd/system 目录下面以 <Target 名> + .required 后缀构成的子目录中
6224e851715SGnoCiYeH     RequiredBy,
6234e851715SGnoCiYeH     //当前 Unit enable/disable 时,同时 enable/disable 的其他 Unit
6244e851715SGnoCiYeH     Also,
6254e851715SGnoCiYeH     //别名
6264e851715SGnoCiYeH     Alias,
62721fc724cSGnoCiYeH }
628