xref: /DragonReach/src/parse/parse_service/mod.rs (revision 4e851715e713d4b9f10926924b8a727bd7cc6cb3)
1 use super::{AttrParse, ServiceUnitAttr, UnitParser, SERVICE_UNIT_ATTR_TABLE};
2 use crate::error::ParseError;
3 use crate::parse::parse_util::UnitParseUtil;
4 use crate::unit::service::{MountFlag, RestartOption, ServiceType, ServiceUnit};
5 use crate::unit::{BaseUnit, Unit};
6 use core::borrow::BorrowMut;
7 use core::cell::RefCell;
8 //use drstd as std;
9 use std::boxed::Box;
10 use std::fs;
11 use std::rc::Rc;
12 use std::string::String;
13 pub struct ServiceParser;
14 
15 impl ServiceParser {
16     /// @brief 解析Service类型Unit的
17     ///
18     /// 从path解析Service类型Unit
19     ///
20     /// @param path 需解析的文件路径
21     ///
22     /// @return 成功则返回Ok(Rc<ServiceUnit>),否则返回Err
23     pub fn parse(path: &str) -> Result<Rc<ServiceUnit>, ParseError> {
24         let mut service = ServiceUnit::default();
25         let mut unit_base = BaseUnit::default();
26 
27         //交付总解析器
28         UnitParser::parse::<ServiceUnit>(
29             path,
30             crate::unit::UnitType::Service,
31             &mut service,
32             &mut unit_base,
33         )?;
34 
35         //设置
36         service.unit_base = unit_base;
37         let mut service = Rc::new(service);
38         return Ok(service);
39     }
40 }
41 
42 impl AttrParse<ServiceUnit> for ServiceParser {
43     /// @brief 为Service类型Unit解析并添加属性
44     ///
45     /// 为Service类型Unit解析并添加属性
46     ///
47     /// @param service ServiceUnit对象
48     ///
49     /// @param attr_str 属性名
50     ///
51     /// @param val 属性值
52     ///
53     /// @return 成功则返回Ok(()),否则返回Err
54     fn parse_and_set_attribute(
55         service: &mut ServiceUnit,
56         attr_str: &str,
57         val: &str,
58     ) -> Result<(), ParseError> {
59         //let mut service = *unit;
60 
61         let attr = match SERVICE_UNIT_ATTR_TABLE.get(attr_str) {
62             Some(val) => val,
63             None => {
64                 return Err(ParseError::EINVAL);
65             }
66         };
67         match attr {
68             ServiceUnitAttr::Type => match val {
69                 "simple" => service.service_part.service_type = ServiceType::Simple,
70                 "forking" => service.service_part.service_type = ServiceType::Forking,
71                 "oneshot" => service.service_part.service_type = ServiceType::OneShot,
72                 "dbus" => service.service_part.service_type = ServiceType::Dbus,
73                 "notify" => service.service_part.service_type = ServiceType::Notify,
74                 "idle" => service.service_part.service_type = ServiceType::Idle,
75                 _ => {
76                     return Err(ParseError::EINVAL);
77                 }
78             },
79             ServiceUnitAttr::RemainAfterExit => {
80                 service.service_part.remain_after_exit = UnitParseUtil::parse_boolean(val)?
81             }
82             ServiceUnitAttr::ExecStart => {
83                 service
84                     .service_part
85                     .exec_start
86                     .extend(UnitParseUtil::parse_cmd_task(val)?);
87             }
88             ServiceUnitAttr::ExecStartPre => {
89                 service
90                     .service_part
91                     .exec_start_pre
92                     .extend(UnitParseUtil::parse_cmd_task(val)?);
93             }
94             ServiceUnitAttr::ExecStartPos => {
95                 service
96                     .service_part
97                     .exec_start_pos
98                     .extend(UnitParseUtil::parse_cmd_task(val)?);
99             }
100             ServiceUnitAttr::ExecReload => {
101                 service
102                     .service_part
103                     .exec_reload
104                     .extend(UnitParseUtil::parse_cmd_task(val)?);
105             }
106             ServiceUnitAttr::ExecStopPost => {
107                 service
108                     .service_part
109                     .exec_stop_post
110                     .extend(UnitParseUtil::parse_cmd_task(val)?);
111             }
112             ServiceUnitAttr::ExecStop => {
113                 service
114                     .service_part
115                     .exec_stop
116                     .extend(UnitParseUtil::parse_cmd_task(val)?);
117             }
118             ServiceUnitAttr::RestartSec => {
119                 service.service_part.restart_sec = UnitParseUtil::parse_sec(val)?
120             }
121             ServiceUnitAttr::Restart => match val {
122                 "always" => service.service_part.restart = RestartOption::AlwaysRestart,
123                 "on-success" => service.service_part.restart = RestartOption::OnSuccess,
124                 "on-failure" => service.service_part.restart = RestartOption::OnFailure,
125                 "on-abnormal" => service.service_part.restart = RestartOption::OnAbnormal,
126                 "on-abort" => service.service_part.restart = RestartOption::OnAbort,
127                 "on-watchdog" => service.service_part.restart = RestartOption::OnWatchdog,
128                 _ => {
129                     return Err(ParseError::EINVAL);
130                 }
131             },
132             ServiceUnitAttr::TimeoutStartSec => {
133                 service.service_part.timeout_start_sec = UnitParseUtil::parse_sec(val)?
134             }
135             ServiceUnitAttr::TimeoutStopSec => {
136                 service.service_part.timeout_stop_sec = UnitParseUtil::parse_sec(val)?
137             }
138             ServiceUnitAttr::Environment => {
139                 service.service_part.environment = String::from(val);
140             }
141             ServiceUnitAttr::EnvironmentFile => {
142                 if !UnitParseUtil::is_absolute_path(val) {
143                     return Err(ParseError::EFILE);
144                 }
145                 service.service_part.environment_file = String::from(val);
146             }
147             ServiceUnitAttr::Nice => {
148                 service.service_part.nice = UnitParseUtil::parse_nice(val)?;
149             }
150             ServiceUnitAttr::WorkingDirectory => {
151                 if !UnitParseUtil::is_dir(val) {
152                     return Err(ParseError::ENODIR);
153                 }
154                 service.service_part.working_directory = String::from(val);
155             }
156             ServiceUnitAttr::User => {
157                 //TODO: 检查系统是否存在这个用户
158                 service.service_part.user = String::from(val);
159             }
160             ServiceUnitAttr::Group => {
161                 //TODO: 检查系统是否存在该用户组
162                 service.service_part.group = String::from(val);
163             }
164             ServiceUnitAttr::MountFlags => match val {
165                 "shared" => service.service_part.mount_flags = MountFlag::Shared,
166                 "slave" => service.service_part.mount_flags = MountFlag::Slave,
167                 "private" => service.service_part.mount_flags = MountFlag::Private,
168                 _ => {
169                     return Err(ParseError::EINVAL);
170                 }
171             },
172             _ => {
173                 return Err(ParseError::EINVAL);
174             }
175         }
176 
177         return Ok(());
178     }
179 }
180