|
@@ -1,22 +1,29 @@
|
|
use super::{BaseUnit, Unit};
|
|
use super::{BaseUnit, Unit};
|
|
-use crate::error::{ParseError, ParseErrorType};
|
|
|
|
|
|
+use crate::error::runtime_error::{RuntimeError, RuntimeErrorType};
|
|
|
|
+use crate::error::{parse_error::ParseError, parse_error::ParseErrorType};
|
|
|
|
+use crate::executor::service_executor::ServiceExecutor;
|
|
|
|
+use crate::manager::GLOBAL_UNIT_MANAGER;
|
|
|
|
+use crate::parse::graph::Graph;
|
|
use crate::parse::parse_service::ServiceParser;
|
|
use crate::parse::parse_service::ServiceParser;
|
|
use crate::parse::parse_util::UnitParseUtil;
|
|
use crate::parse::parse_util::UnitParseUtil;
|
|
-use crate::parse::{Segment, SERVICE_UNIT_ATTR_TABLE};
|
|
|
|
|
|
+use crate::parse::{Segment, UnitParser, SERVICE_UNIT_ATTR_TABLE};
|
|
use crate::task::cmdtask::CmdTask;
|
|
use crate::task::cmdtask::CmdTask;
|
|
|
|
|
|
#[cfg(target_os = "dragonos")]
|
|
#[cfg(target_os = "dragonos")]
|
|
use drstd as std;
|
|
use drstd as std;
|
|
|
|
+use std::mem::MaybeUninit;
|
|
|
|
+use std::process::{Child, Command};
|
|
use std::rc::Rc;
|
|
use std::rc::Rc;
|
|
use std::string::String;
|
|
use std::string::String;
|
|
|
|
+use std::sync::Arc;
|
|
use std::vec::Vec;
|
|
use std::vec::Vec;
|
|
-#[derive(Default)]
|
|
|
|
|
|
+#[derive(Clone, Debug,Default)]
|
|
pub struct ServiceUnit {
|
|
pub struct ServiceUnit {
|
|
unit_base: BaseUnit,
|
|
unit_base: BaseUnit,
|
|
service_part: ServicePart,
|
|
service_part: ServicePart,
|
|
}
|
|
}
|
|
|
|
|
|
-#[derive(Debug)]
|
|
|
|
|
|
+#[derive(Debug, Clone, Copy)]
|
|
pub enum ServiceType {
|
|
pub enum ServiceType {
|
|
Simple,
|
|
Simple,
|
|
Forking,
|
|
Forking,
|
|
@@ -32,7 +39,7 @@ impl Default for ServiceType {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-#[derive(Debug)]
|
|
|
|
|
|
+#[derive(Debug, Clone, Copy)]
|
|
pub enum RestartOption {
|
|
pub enum RestartOption {
|
|
AlwaysRestart,
|
|
AlwaysRestart,
|
|
OnSuccess,
|
|
OnSuccess,
|
|
@@ -49,7 +56,7 @@ impl Default for RestartOption {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-#[derive(Debug)]
|
|
|
|
|
|
+#[derive(Debug, Clone, Copy)]
|
|
pub enum MountFlag {
|
|
pub enum MountFlag {
|
|
Shared,
|
|
Shared,
|
|
Slave,
|
|
Slave,
|
|
@@ -62,13 +69,13 @@ impl Default for MountFlag {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-#[derive(Default, Debug)]
|
|
|
|
|
|
+#[derive(Default, Debug, Clone)]
|
|
pub struct ServicePart {
|
|
pub struct ServicePart {
|
|
//生命周期相关
|
|
//生命周期相关
|
|
service_type: ServiceType,
|
|
service_type: ServiceType,
|
|
///
|
|
///
|
|
remain_after_exit: bool,
|
|
remain_after_exit: bool,
|
|
- exec_start: Vec<CmdTask>,
|
|
|
|
|
|
+ exec_start: CmdTask,
|
|
exec_start_pre: Vec<CmdTask>,
|
|
exec_start_pre: Vec<CmdTask>,
|
|
exec_start_pos: Vec<CmdTask>,
|
|
exec_start_pos: Vec<CmdTask>,
|
|
exec_reload: Vec<CmdTask>,
|
|
exec_reload: Vec<CmdTask>,
|
|
@@ -95,7 +102,7 @@ impl Unit for ServiceUnit {
|
|
self
|
|
self
|
|
}
|
|
}
|
|
|
|
|
|
- fn from_path(path: &str) -> Result<Rc<Self>, ParseError>
|
|
|
|
|
|
+ fn from_path(path: &str) -> Result<Arc<Self>, ParseError>
|
|
where
|
|
where
|
|
Self: Sized,
|
|
Self: Sized,
|
|
{
|
|
{
|
|
@@ -104,9 +111,13 @@ impl Unit for ServiceUnit {
|
|
|
|
|
|
fn set_attr(&mut self, segment: Segment, attr: &str, val: &str) -> Result<(), ParseError> {
|
|
fn set_attr(&mut self, segment: Segment, attr: &str, val: &str) -> Result<(), ParseError> {
|
|
if segment != Segment::Service {
|
|
if segment != Segment::Service {
|
|
- return Err(ParseError::new(ParseErrorType::EINVAL, String::new(),0));
|
|
|
|
|
|
+ return Err(ParseError::new(ParseErrorType::EINVAL, String::new(), 0));
|
|
}
|
|
}
|
|
- let attr_type = SERVICE_UNIT_ATTR_TABLE.get(attr).ok_or(ParseError::new(ParseErrorType::EINVAL, String::new(),0));
|
|
|
|
|
|
+ let attr_type = SERVICE_UNIT_ATTR_TABLE.get(attr).ok_or(ParseError::new(
|
|
|
|
+ ParseErrorType::EINVAL,
|
|
|
|
+ String::new(),
|
|
|
|
+ 0,
|
|
|
|
+ ));
|
|
return self.service_part.set_attr(attr_type.unwrap(), val);
|
|
return self.service_part.set_attr(attr_type.unwrap(), val);
|
|
}
|
|
}
|
|
|
|
|
|
@@ -117,6 +128,22 @@ impl Unit for ServiceUnit {
|
|
fn unit_type(&self) -> super::UnitType {
|
|
fn unit_type(&self) -> super::UnitType {
|
|
return self.unit_base.unit_type;
|
|
return self.unit_base.unit_type;
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ fn unit_base(&self) -> &BaseUnit {
|
|
|
|
+ return &self.unit_base;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ fn unit_id(&self) -> usize {
|
|
|
|
+ return self.unit_base.unit_id;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ fn run(&self) -> Result<(), RuntimeError> {
|
|
|
|
+ self.exec()
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ fn mut_unit_base(&mut self) -> &mut BaseUnit {
|
|
|
|
+ return &mut self.unit_base;
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
impl ServiceUnit {
|
|
impl ServiceUnit {
|
|
@@ -127,8 +154,16 @@ impl ServiceUnit {
|
|
pub fn service_part(&self) -> &ServicePart {
|
|
pub fn service_part(&self) -> &ServicePart {
|
|
return &self.service_part;
|
|
return &self.service_part;
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ fn exec(&self) -> Result<(), RuntimeError> {
|
|
|
|
+ ServiceExecutor::exec(self)
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+unsafe impl Sync for ServiceUnit {}
|
|
|
|
+
|
|
|
|
+unsafe impl Send for ServiceUnit {}
|
|
|
|
+
|
|
pub enum ServiceUnitAttr {
|
|
pub enum ServiceUnitAttr {
|
|
None,
|
|
None,
|
|
//Service段
|
|
//Service段
|
|
@@ -175,7 +210,7 @@ pub enum ServiceUnitAttr {
|
|
}
|
|
}
|
|
|
|
|
|
impl ServicePart {
|
|
impl ServicePart {
|
|
- pub fn set_attr(&mut self, attr: &ServiceUnitAttr, val: &str) -> Result<(), ParseError> {
|
|
|
|
|
|
+ pub fn set_attr(&'_ mut self, attr: &ServiceUnitAttr, val: &str) -> Result<(), ParseError> {
|
|
match attr {
|
|
match attr {
|
|
ServiceUnitAttr::Type => match val {
|
|
ServiceUnitAttr::Type => match val {
|
|
"simple" => self.service_type = ServiceType::Simple,
|
|
"simple" => self.service_type = ServiceType::Simple,
|
|
@@ -185,14 +220,14 @@ impl ServicePart {
|
|
"notify" => self.service_type = ServiceType::Notify,
|
|
"notify" => self.service_type = ServiceType::Notify,
|
|
"idle" => self.service_type = ServiceType::Idle,
|
|
"idle" => self.service_type = ServiceType::Idle,
|
|
_ => {
|
|
_ => {
|
|
- return Err(ParseError::new(ParseErrorType::EINVAL, String::new(),0));
|
|
|
|
|
|
+ return Err(ParseError::new(ParseErrorType::EINVAL, String::new(), 0));
|
|
}
|
|
}
|
|
},
|
|
},
|
|
ServiceUnitAttr::RemainAfterExit => {
|
|
ServiceUnitAttr::RemainAfterExit => {
|
|
self.remain_after_exit = UnitParseUtil::parse_boolean(val)?
|
|
self.remain_after_exit = UnitParseUtil::parse_boolean(val)?
|
|
}
|
|
}
|
|
ServiceUnitAttr::ExecStart => {
|
|
ServiceUnitAttr::ExecStart => {
|
|
- self.exec_start.extend(UnitParseUtil::parse_cmd_task(val)?);
|
|
|
|
|
|
+ self.exec_start = UnitParseUtil::parse_cmd_task(val)?[0].clone();
|
|
}
|
|
}
|
|
ServiceUnitAttr::ExecStartPre => {
|
|
ServiceUnitAttr::ExecStartPre => {
|
|
self.exec_start_pre
|
|
self.exec_start_pre
|
|
@@ -221,7 +256,7 @@ impl ServicePart {
|
|
"on-abort" => self.restart = RestartOption::OnAbort,
|
|
"on-abort" => self.restart = RestartOption::OnAbort,
|
|
"on-watchdog" => self.restart = RestartOption::OnWatchdog,
|
|
"on-watchdog" => self.restart = RestartOption::OnWatchdog,
|
|
_ => {
|
|
_ => {
|
|
- return Err(ParseError::new(ParseErrorType::EINVAL,String::new(),0));
|
|
|
|
|
|
+ return Err(ParseError::new(ParseErrorType::EINVAL, String::new(), 0));
|
|
}
|
|
}
|
|
},
|
|
},
|
|
ServiceUnitAttr::TimeoutStartSec => {
|
|
ServiceUnitAttr::TimeoutStartSec => {
|
|
@@ -235,7 +270,7 @@ impl ServicePart {
|
|
}
|
|
}
|
|
ServiceUnitAttr::EnvironmentFile => {
|
|
ServiceUnitAttr::EnvironmentFile => {
|
|
if !UnitParseUtil::is_valid_file(val) {
|
|
if !UnitParseUtil::is_valid_file(val) {
|
|
- return Err(ParseError::new(ParseErrorType::EFILE,String::new(),0));
|
|
|
|
|
|
+ return Err(ParseError::new(ParseErrorType::EFILE, String::new(), 0));
|
|
}
|
|
}
|
|
self.environment_file = String::from(val);
|
|
self.environment_file = String::from(val);
|
|
}
|
|
}
|
|
@@ -244,7 +279,7 @@ impl ServicePart {
|
|
}
|
|
}
|
|
ServiceUnitAttr::WorkingDirectory => {
|
|
ServiceUnitAttr::WorkingDirectory => {
|
|
if !UnitParseUtil::is_dir(val) {
|
|
if !UnitParseUtil::is_dir(val) {
|
|
- return Err(ParseError::new(ParseErrorType::ENODIR,String::new(),0));
|
|
|
|
|
|
+ return Err(ParseError::new(ParseErrorType::ENODIR, String::new(), 0));
|
|
}
|
|
}
|
|
self.working_directory = String::from(val);
|
|
self.working_directory = String::from(val);
|
|
}
|
|
}
|
|
@@ -261,11 +296,11 @@ impl ServicePart {
|
|
"slave" => self.mount_flags = MountFlag::Slave,
|
|
"slave" => self.mount_flags = MountFlag::Slave,
|
|
"private" => self.mount_flags = MountFlag::Private,
|
|
"private" => self.mount_flags = MountFlag::Private,
|
|
_ => {
|
|
_ => {
|
|
- return Err(ParseError::new(ParseErrorType::EINVAL,String::new(),0));
|
|
|
|
|
|
+ return Err(ParseError::new(ParseErrorType::EINVAL, String::new(), 0));
|
|
}
|
|
}
|
|
},
|
|
},
|
|
_ => {
|
|
_ => {
|
|
- return Err(ParseError::new(ParseErrorType::EINVAL,String::new(),0));
|
|
|
|
|
|
+ return Err(ParseError::new(ParseErrorType::EINVAL, String::new(), 0));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return Ok(());
|
|
return Ok(());
|
|
@@ -280,7 +315,7 @@ impl ServicePart {
|
|
self.remain_after_exit
|
|
self.remain_after_exit
|
|
}
|
|
}
|
|
|
|
|
|
- pub fn exec_start(&self) -> &Vec<CmdTask> {
|
|
|
|
|
|
+ pub fn exec_start(&self) -> &CmdTask {
|
|
&self.exec_start
|
|
&self.exec_start
|
|
}
|
|
}
|
|
|
|
|