Selaa lähdekoodia

Merge branch 'master' of github.com:DragonOS-Community/DragonReach

GnoCiYeH 1 vuosi sitten
vanhempi
commit
7639fc16c2

+ 2 - 1
Cargo.toml

@@ -14,7 +14,8 @@ hashbrown = "0.11"
 cfg-if = { version = "1.0"}
 
 [target.'cfg(target_os = "dragonos")'.dependencies]
-drstd = {git = "https://git.mirrors.dragonos.org/DragonOS-Community/drstd.git", revision = "a4d693c682"}
+#drstd = {git = "https://github.com/DragonOS-Community/drstd.git", revision = "af10011"}
+drstd = {path = "/home/heyicong/drstd"}
 lazy_static = { version = "1.4.0", default-features = false, features = ["spin_no_std"] }
 
 [target.'cfg(not(target_os = "dragonos"))'.dependencies]

+ 10 - 3
Makefile

@@ -1,8 +1,15 @@
+OUTPUT_DIR = $(DADK_BUILD_CACHE_DIR_DRAGONREACH_0_1_0)
+
 build:
-	cargo -Z build-std=core,alloc,compiler_builtins build --target x86_64-unknown-dragonos
+	cargo -Z build-std=core,alloc,compiler_builtins build --target ./target.json
 
 install:
-	cargo -Z build-std=core,alloc,compiler_builtins install --target ./target.json --path .  --root ./DragonReach
+	cp ./parse_test/shell.service $(ROOT_PATH)/bin/sysroot/etc/reach/system/shell.service
+
+	mkdir -p $(OUTPUT_DIR)/tmp
+	cargo -Z build-std=core,alloc,compiler_builtins install --target $(TARGET) --path .  --root $(OUTPUT_DIR)/tmp
+	mv $(OUTPUT_DIR)/tmp/bin/DragonReach $(ROOT_PATH)/bin/user/DragonReach
+	rm -rf $(OUTPUT_DIR)/tmp
 
 build-linux:
-	cargo -Z build-std=core,alloc,compiler_builtins build --target x86_64-unknown-linux-gnu
+	cargo -Z build-std=core,alloc,compiler_builtins build --target x86_64-unknown-linux-gnu

+ 0 - 0
build.txt


+ 8 - 0
parse_test/shell.service

@@ -0,0 +1,8 @@
+[Unit]
+Description=Shell
+
+[Service]
+Type=simple
+ExecStart=/bin/shell.elf
+Restart=always
+RestartSec=5000

+ 7 - 3
parse_test/test.service

@@ -1,17 +1,21 @@
 [Unit]
 Description=My Service
 Documentation=https://example.com/docs/my-service.html
-#After=network.target
+#After=/home/heyicong/DragonReach/parse_test/test1.service
 
 [Service]
 Type=simple
-ExecStart=/home/heyicong/DragonOs/bin/sysroot/bin/DragonReach --option1=value1 --option2=value2
+ExecStart=/bin/gdb
+ExecStartPos=/bin/echo start_pos
+ExecStartPre=/bin/echo start_pre
+ExecStopPost=/bin/echo stop_pos
+ExecReload=/bin/echo reload
 WorkingDirectory=/home/heyicong/
 User=myuser
 Group=mygroup
 Environment=VAR1=value1
 Environment=VAR2=value2
-Restart=on-failure
+Restart=on-success
 RestartSec=5s
 TimeoutStartSec=10s
 TimeoutStopSec=10s

+ 20 - 0
parse_test/test1.service

@@ -0,0 +1,20 @@
+[Unit]
+Description=My Service1
+Documentation=https://example.com/docs/my-service.html
+#After=/home/heyicong/DragonReach/parse_test/test2.service
+
+[Service]
+Type=simple
+ExecStart=/bin/ls
+WorkingDirectory=/home/heyicong/
+User=myuser
+Group=mygroup
+Environment=VAR1=value1
+Environment=VAR2=value2
+Restart=on-failure
+RestartSec=5s
+TimeoutStartSec=10s
+TimeoutStopSec=10s
+
+[Install]
+#WantedBy=multi-user.target

+ 20 - 0
parse_test/test2.service

@@ -0,0 +1,20 @@
+[Unit]
+Description=My Service2
+Documentation=https://example.com/docs/my-service.html
+#After=/home/heyicong/DragonReach/parse_test/test.service
+
+[Service]
+Type=simple
+ExecStart=/bin/ls
+WorkingDirectory=/home/heyicong/
+User=myuser
+Group=mygroup
+Environment=VAR1=value1
+Environment=VAR2=value2
+Restart=on-failure
+RestartSec=5s
+TimeoutStartSec=10s
+TimeoutStopSec=10s
+
+[Install]
+#WantedBy=multi-user.target

+ 5 - 42
src/error/mod.rs

@@ -1,48 +1,11 @@
 #[cfg(target_os = "dragonos")]
 use drstd as std;
-use std::format;
-use std::string::String;
-use std::string::ToString;
-/// 解析错误,错误信息应该包括文件名以及行号
-#[repr(i32)]
-#[derive(Debug, PartialEq, Eq, Clone)]
-#[allow(dead_code, non_camel_case_types)]
-pub enum ParseErrorType {
-    /// 不合法参数
-    EINVAL,
-    /// 结果过大 Result too large.
-    ERANGE,
-    /// 重复定义
-    EREDEF,
-    /// 未预料到的空值
-    EUnexpectedEmpty,
-    /// 语法错误
-    ESyntaxError,
-    /// 非法文件描述符
-    EBADF,
-    /// 非法文件
-    EFILE,
-    /// 不是目录
-    ENODIR,
-}
-/// 错误信息应该包括错误类型ParseErrorType,当前解析的文件名,当前解析的行号
-#[derive(Debug, PartialEq, Eq, Clone)]
-pub struct ParseError(ParseErrorType,String,usize);
 
-impl ParseError {
-    pub fn new(error_type: ParseErrorType,file_name: String,line_number: usize) -> ParseError {
-        ParseError(error_type,file_name,line_number)
-    }
+pub mod parse_error;
+pub mod runtime_error;
 
-    pub fn set_file(&mut self,path: &str) {
-        self.1 = path.to_string();
-    }
-
-    pub fn set_linenum(&mut self,linenum: usize) {
-        self.2 = linenum;
-    }
+use std::string::String;
 
-    pub fn error_format(&self) -> String {
-        format!("Parse Error!,Error Type: {:?}, File: {}, Line: {}",self.0,self.1,self.2)
-    }
+pub trait ErrorFormat {
+    fn error_format(&self) -> String;
 }

+ 57 - 0
src/error/parse_error/mod.rs

@@ -0,0 +1,57 @@
+#[cfg(target_os = "dragonos")]
+use drstd as std;
+use std::format;
+use std::string::String;
+use std::string::ToString;
+
+use super::ErrorFormat;
+/// 解析错误,错误信息应该包括文件名以及行号
+#[repr(i32)]
+#[derive(Debug, PartialEq, Eq, Clone)]
+#[allow(dead_code, non_camel_case_types)]
+pub enum ParseErrorType {
+    /// 不合法参数
+    EINVAL,
+    /// 结果过大 Result too large.
+    ERANGE,
+    /// 重复定义
+    EREDEF,
+    /// 未预料到的空值
+    EUnexpectedEmpty,
+    /// 语法错误
+    ESyntaxError,
+    /// 非法文件描述符
+    EBADF,
+    /// 非法文件
+    EFILE,
+    /// 不是目录
+    ENODIR,
+    /// 循环依赖
+    ECircularDependency,
+}
+/// 错误信息应该包括错误类型ParseErrorType,当前解析的文件名,当前解析的行号
+#[derive(Debug, PartialEq, Eq, Clone)]
+pub struct ParseError(ParseErrorType, String, usize);
+
+impl ParseError {
+    pub fn new(error_type: ParseErrorType, file_name: String, line_number: usize) -> ParseError {
+        ParseError(error_type, file_name, line_number)
+    }
+
+    pub fn set_file(&mut self, path: &str) {
+        self.1 = path.to_string();
+    }
+
+    pub fn set_linenum(&mut self, linenum: usize) {
+        self.2 = linenum;
+    }
+}
+
+impl ErrorFormat for ParseError {
+    fn error_format(&self) -> String {
+        format!(
+            "Parse Error!,Error Type: {:?}, File: {}, Line: {}",
+            self.0, self.1, self.2
+        )
+    }
+}

+ 60 - 0
src/error/runtime_error/mod.rs

@@ -0,0 +1,60 @@
+use super::ErrorFormat;
+#[cfg(target_os = "dragonos")]
+use drstd as std;
+use std::format;
+use std::string::String;
+
+#[derive(Debug)]
+pub enum RuntimeErrorType {
+    //启动失败
+    ExecFailed,
+
+    // 文件相关错误
+    FileNotFound,
+    FileAccessDenied,
+    InvalidFileFormat,
+
+    // 循环依赖
+    CircularDependency,
+
+    // 转型错误
+    DowncastError,
+
+    // 网络相关错误
+    ConnectionError,
+    Timeout,
+
+    // 数据库相关错误
+    DatabaseError,
+    QueryError,
+
+    // 并发相关错误
+    Deadlock,
+    ThreadPanicked,
+
+    // 配置相关错误
+    ConfigError,
+    InvalidParameter,
+
+    // 其他通用错误
+    OutOfMemory,
+    InvalidInput,
+    UnsupportedOperation,
+
+    // 自定义错误
+    Custom(String),
+}
+
+pub struct RuntimeError(RuntimeErrorType);
+
+impl RuntimeError {
+    pub fn new(error_type: RuntimeErrorType) -> Self {
+        return RuntimeError(error_type);
+    }
+}
+
+impl ErrorFormat for RuntimeError {
+    fn error_format(&self) -> String {
+        format!("Runtime Error!,Error Type: {:?}", self.0)
+    }
+}

+ 113 - 0
src/executor/dep_graph/mod.rs

@@ -0,0 +1,113 @@
+#[cfg(target_os = "dragonos")]
+use drstd as std;
+
+use core::slice::SlicePattern;
+use std::sync::Arc;
+use std::vec::Vec;
+use std::{cmp::PartialEq, sync::Mutex};
+
+use crate::manager::{self, UnitManager};
+use crate::{
+    error::runtime_error::{RuntimeError, RuntimeErrorType},
+    unit::Unit,
+};
+
+pub struct DepGraphNode {
+    value: usize,
+    edges: Vec<usize>,
+    incoming_edges: Vec<usize>,
+}
+
+pub struct DepGraph {
+    nodes: Vec<DepGraphNode>,
+    value: Vec<usize>,
+}
+
+// 提供拓扑排序方法,在启动服务时确定先后顺序
+impl DepGraph {
+    fn new() -> Self {
+        return DepGraph {
+            nodes: Vec::new(),
+            value: Vec::new(),
+        };
+    }
+
+    pub fn add_node(&mut self, value: usize) -> usize {
+        let index = self.nodes.len();
+        //如果nodes中已经有了这个value则无需重复添加,直接返回nodes中的value对应的index
+        if let Some(idx) = self.value.iter().position(|x| *x == value) {
+            return idx;
+        }
+        //如果value在nodes中不存在,则添加value
+        self.nodes.push(DepGraphNode {
+            value: value,
+            edges: Vec::new(),
+            incoming_edges: Vec::new(),
+        });
+        self.value.push(value);
+        return index;
+    }
+    pub fn add_edge(&mut self, from: usize, to: usize) {
+        self.nodes[from].edges.push(to);
+        self.nodes[to].incoming_edges.push(from);
+    }
+    pub fn topological_sort(&mut self) -> Result<Vec<usize>, RuntimeError> {
+        let mut result = Vec::new();
+        let mut visited = Vec::new();
+        let mut stack = Vec::new();
+        for (i, node) in self.nodes.iter().enumerate() {
+            if node.incoming_edges.len() == 0 {
+                stack.push(i);
+            }
+        }
+        while stack.len() > 0 {
+            let index = stack.pop().unwrap();
+            if visited.contains(&index) {
+                continue;
+            }
+            visited.push(index);
+            result.push(self.nodes[index].value);
+            let len = self.nodes[index].edges.len();
+            for i in 0..len {
+                let edge = self.nodes[index].edges[i];
+                self.nodes[edge].incoming_edges.retain(|&x| x != index);
+                if self.nodes[edge].incoming_edges.len() == 0 {
+                    stack.push(edge);
+                }
+            }
+        }
+        if result.len() != self.nodes.len() {
+            return Err(RuntimeError::new(RuntimeErrorType::CircularDependency));
+        }
+        result.reverse();
+        return Ok(result);
+    }
+
+    fn add_edges(&mut self, unit: usize, after: &[usize]) {
+        //因为service的依赖关系规模不会很大,故先使用递归实现
+        //TODO:改递归
+        for target in after {
+            let s = self.add_node(unit);
+            let t = self.add_node(*target);
+            self.add_edge(s, t);
+
+            let arc_unit = UnitManager::get_unit_with_id(target).unwrap();
+            let unit = arc_unit.lock().unwrap();
+            let after = unit.unit_base().unit_part().after();
+
+            self.add_edges(*target, after);
+        }
+    }
+
+    pub fn construct_graph(unit: &Arc<Mutex<dyn Unit>>) -> DepGraph {
+        let mut graph: DepGraph = DepGraph::new();
+
+        let unit = unit.lock().unwrap();
+        let uid = unit.unit_id();
+        graph.add_node(uid);
+        let after = (&unit).unit_base().unit_part().after();
+        //递归添加边来构建图
+        graph.add_edges(uid, after);
+        return graph;
+    }
+}

+ 64 - 0
src/executor/mod.rs

@@ -0,0 +1,64 @@
+#[cfg(target_os = "dragonos")]
+use drstd as std;
+
+pub mod dep_graph;
+pub mod service_executor;
+
+use std::sync::Arc;
+use std::{process::Child, sync::Mutex};
+
+use crate::{
+    error::runtime_error::{RuntimeError, RuntimeErrorType},
+    manager::UnitManager,
+    unit::Unit,
+};
+
+use self::dep_graph::DepGraph;
+
+#[derive(Debug, Clone, Copy)]
+pub enum ExitStatus {
+    Success,
+    Failure,
+    Abnormal,
+    Abort,
+    Watchdog,
+}
+
+impl ExitStatus {
+    /// ## 从错误码获得退出状态
+    ///
+    /// 注意,该方法只会返回Success(exit_code == 0)和Abnormal(exit_code != 0)两种状态
+    /// 其他DragonReach定义的退出状态需要手动指定
+    ///
+    /// ### return Success(exit_code == 0)、Abnormal(exit_code != 0)
+    pub fn from_exit_code(exit_code: i32) -> Self {
+        match exit_code {
+            0 => return Self::Success,
+            _ => return Self::Abnormal,
+        }
+    }
+}
+
+//Unit的全局执行器
+pub struct Executor;
+
+impl Executor {
+    pub fn exec(unit: &Arc<Mutex<dyn Unit>>) -> Result<(), RuntimeError> {
+        //TODO: 优化此处,解析时也用到了拓扑排序,尝试使用那次拓扑排序的结果
+        let mut graph = DepGraph::construct_graph(unit);
+
+        let sort_ret = graph.topological_sort()?;
+        for u in sort_ret {
+            if UnitManager::is_running_unit(&u) {
+                continue;
+            }
+
+            let mutex = UnitManager::get_unit_with_id(&u).unwrap();
+            let mut unit = mutex.lock().unwrap();
+            if let Err(e) = unit.run() {
+                return Err(e);
+            }
+        }
+        return Ok(());
+    }
+}

+ 210 - 0
src/executor/service_executor/mod.rs

@@ -0,0 +1,210 @@
+#[cfg(target_os = "dragonos")]
+use drstd as std;
+
+use crate::{
+    error::{
+        runtime_error::{RuntimeError, RuntimeErrorType},
+        ErrorFormat,
+    },
+    manager::{RunningUnit, UnitManager},
+    parse::{parse_util::UnitParseUtil, Segment, UnitParser},
+    task::cmdtask::CmdTask,
+    unit::{
+        service::RestartOption,
+        service::{ServiceType, ServiceUnit},
+        Unit, UnitState, UnitType,
+    },
+};
+use std::os::unix::process::CommandExt;
+use std::sync::Mutex;
+use std::vec::Vec;
+use std::{
+    borrow::BorrowMut,
+    cell::RefCell,
+    eprint, eprintln,
+    io::{Error, ErrorKind},
+    print, println,
+};
+use std::{io::BufRead, process::Command, sync::Arc};
+use std::{process::Stdio, string::ToString};
+
+use super::ExitStatus;
+
+pub struct ServiceExecutor;
+
+impl ServiceExecutor {
+    pub fn exec(service: &mut ServiceUnit) -> Result<(), RuntimeError> {
+        match *service.service_part().service_type() {
+            ServiceType::Simple => {
+                return Self::exec_simple(service);
+            }
+            ServiceType::Forking => {
+                return Self::exec_forking(service);
+            }
+            ServiceType::Dbus => {
+                return Self::exec_dbus(service);
+            }
+            ServiceType::Notify => {
+                return Self::exec_notify(service);
+            }
+            ServiceType::Idle => {
+                return Self::exec_idle(service);
+            }
+            ServiceType::OneShot => {
+                return Self::exec_one_shot(service);
+            }
+        }
+    }
+    pub fn exec_simple(service: &mut ServiceUnit) -> Result<(), RuntimeError> {
+        //处理conflict
+        let conflicts = service.unit_base().unit_part().conflicts();
+        for u in conflicts {
+            // 如果有冲突项enable的时候,该unit不能启动
+            let mutex = UnitManager::get_unit_with_id(u).unwrap();
+            let unit = mutex.lock().unwrap();
+            if *unit.unit_base().state() == UnitState::Enabled {
+                eprintln!(
+                    "{}: Service startup failed: conflict unit",
+                    unit.unit_base().unit_part().description()
+                );
+                return Err(RuntimeError::new(RuntimeErrorType::ExecFailed));
+            }
+        }
+
+        //获取启动命令
+        let exec_start = service.service_part().exec_start();
+        println!("exec:{}", exec_start.path);
+
+        //TODO:设置uid与gid
+
+        //处理ExecStartsPre,准备在服务启动前执行的命令
+        Self::exec_start_pre(service)?;
+
+        //创建服务进程
+        //服务配置环境变量,配置工作目录
+        let proc = Command::new(&exec_start.path)
+            .args(&exec_start.cmd)
+            .current_dir(service.service_part().working_directory())
+            .envs(Vec::from(service.service_part().environment()))
+            .stderr(Stdio::inherit())
+            .stdout(Stdio::inherit())
+            .stdin(Stdio::inherit())
+            .spawn();
+
+        match proc {
+            Ok(p) => {
+                println!("Service running...");
+                //修改service状态
+                service.mut_unit_base().set_state(UnitState::Enabled);
+                //启动成功后将Child加入全局管理的进程表
+                UnitManager::push_running(RunningUnit::new(p, service.unit_id()));
+                //执行启动后命令
+                Self::exec_start_pos(service)?;
+            }
+            Err(err) => {
+                eprintln!("{}: Service startup failed: {}", exec_start.path, err);
+                return Err(RuntimeError::new(RuntimeErrorType::ExecFailed));
+            }
+        }
+        Ok(())
+    }
+
+    fn exec_dbus(service: &ServiceUnit) -> Result<(), RuntimeError> {
+        Ok(())
+    }
+
+    fn exec_forking(service: &ServiceUnit) -> Result<(), RuntimeError> {
+        Ok(())
+    }
+
+    // 此方法会改变service的启动模式为simple
+    fn exec_idle(service: &mut ServiceUnit) -> Result<(), RuntimeError> {
+        // 将该service加入等待运行队列
+        let _ = service.set_attr(Segment::Service, "Type", "simple");
+        UnitManager::push_a_idle_service(service.unit_id());
+        Ok(())
+    }
+
+    fn exec_notify(service: &ServiceUnit) -> Result<(), RuntimeError> {
+        Ok(())
+    }
+
+    fn exec_one_shot(service: &ServiceUnit) -> Result<(), RuntimeError> {
+        Ok(())
+    }
+
+    fn exec_start_pos(service: &ServiceUnit) -> Result<(), RuntimeError> {
+        let cmds = service.service_part().exec_start_pos();
+        for cmd in cmds {
+            cmd.spawn(
+                service.service_part().working_directory().to_string(),
+                service.service_part().environment(),
+            )?;
+        }
+        Ok(())
+    }
+
+    fn exec_start_pre(service: &ServiceUnit) -> Result<(), RuntimeError> {
+        let cmds = service.service_part().exec_start_pre();
+        for cmd in cmds {
+            cmd.spawn(
+                service.service_part().working_directory().to_string(),
+                service.service_part().environment(),
+            )?;
+        }
+        Ok(())
+    }
+
+    //显式停止时执行的命令
+    fn exec_stop(service: &mut ServiceUnit) -> Result<(), RuntimeError> {
+        let cmds = service.service_part().exec_stop();
+        for cmd in cmds {
+            cmd.no_spawn(
+                service.service_part().working_directory().to_string(),
+                service.service_part().environment(),
+            )?;
+        }
+        Ok(())
+    }
+
+    //停止后执行的命令
+    fn exec_stop_post(service: &mut ServiceUnit) -> Result<(), RuntimeError> {
+        let cmds = service.service_part().exec_stop_post();
+        for cmd in cmds {
+            cmd.no_spawn(
+                service.service_part().working_directory().to_string(),
+                service.service_part().environment(),
+            )?;
+        }
+        Ok(())
+    }
+
+    fn exec_reload(service: &mut ServiceUnit) -> Result<(), RuntimeError> {
+        let cmds = service.service_part().exec_reload();
+        for cmd in cmds {
+            cmd.no_spawn(
+                service.service_part().working_directory().to_string(),
+                service.service_part().environment(),
+            )?;
+        }
+        Ok(())
+    }
+
+    //服务退出执行的逻辑(包括自然退出及显式退出)
+    pub fn after_exit(service: &mut ServiceUnit, exit_status: ExitStatus) {
+        //TODO: 需要考虑是否需要在此处执行退出后代码,还是只需要显式退出时才执行
+        let _ = Self::exec_stop_post(service);
+
+        //判断是否需要restart,需要则再次启动服务
+        if service.service_part().restart().is_restart(&exit_status) {
+            let _ = Self::exec_reload(service);
+            let _ = service.run();
+            return;
+        }
+
+        //如果该进程标记了RemainAfterExit,则将其加入特殊标记表
+        if service.service_part().remain_after_exit() {
+            UnitManager::push_flag_running(service.unit_id());
+        }
+    }
+}

+ 118 - 48
src/main.rs

@@ -1,15 +1,13 @@
 #![no_std]
 #![no_main]
+#![feature(slice_pattern)]
+
 use cfg_if::cfg_if;
 
-cfg_if!{
+cfg_if! {
     if #[cfg(target_os = "dragonos")]{
         extern crate drstd;
         use drstd as std;
-        use std::print;
-        use std::println;
-        use std::rc::Rc;
-        use unit::service::ServiceUnit;
     }
 }
 
@@ -17,69 +15,141 @@ extern crate hashbrown;
 
 mod contants;
 mod error;
+mod executor;
+mod manager;
 mod parse;
 mod task;
 mod unit;
 
 use crate::unit::service;
+use std::eprint;
+use std::eprintln;
+use std::fs;
+use std::print;
+use std::println;
+use std::string::{String, ToString};
+use std::vec::Vec;
+use unit::service::ServiceUnit;
 
 use self::unit::Unit;
+use error::ErrorFormat;
 
 pub struct FileDescriptor(usize);
 
+const DRAGON_REACH_UNIT_DIR: &'static str = "/etc/reach/system/";
+
 #[cfg(target_os = "dragonos")]
 #[no_mangle]
 fn main() {
-    use unit::service::ServiceUnit;
+    use parse::UnitParser;
 
-    let service = match ServiceUnit::from_path("/bin/test.service"){
-        Ok(service) => service,
-        Err(e) => {
-            println!("Error:{}",e.error_format());
-            return;
-        }
+    use crate::{
+        executor::Executor,
+        manager::{Manager, UnitManager},
+        parse::parse_util::UnitParseUtil,
     };
-    let service = service.as_ref();
-    println!("parse_result:");
-    println!("Description:{:?}", service.unit_base().unit_part().description());
-    println!("Documentation:{:?}",service.unit_base().unit_part().documentation());
-    println!("ServiceType:{:?}",service.service_part().service_type());
-    println!("ExecStrat:{:?}",service.service_part().exec_start());
-    println!("WorkingDirectory:{:?}",service.service_part().working_directory());
-    println!("Environment:{:?}",service.service_part().environment());
-    println!("Restart:{:?}",service.service_part().restart());
-    println!("RestartSec:{:?}",service.service_part().restart_sec());
-    println!("User:{:?}",service.service_part().user());
-    println!("Group:{:?}",service.service_part().group());
-    println!("TimeoutStartSec:{:?}",service.service_part().timeout_start_sec());
-    println!("TimeoutStopSec:{:?}",service.service_part().timeout_stop_sec());
+
+    let mut units_file_name = Vec::new();
+    //读取目录里面的unit文件
+    if let Ok(entries) = fs::read_dir(DRAGON_REACH_UNIT_DIR) {
+        for entry in entries {
+            if let Ok(entry) = entry {
+                if let Ok(file_type) = entry.file_type() {
+                    if file_type.is_file() {
+                        let filename = entry.file_name();
+                        let filename = filename.to_str().unwrap();
+                        units_file_name.push(filename.to_string());
+                    }
+                }
+            }
+        }
+    }
+
+    //启动服务
+    for path in units_file_name {
+        let id = match UnitParser::from_path(&path) {
+            Ok(id) => id,
+            Err(e) => {
+                eprintln!("Err:{}", e.error_format());
+                0
+            }
+        };
+
+        if id != 0 {
+            let unit = UnitManager::get_unit_with_id(&id).unwrap();
+            if let Err(e) = Executor::exec(&unit) {
+                eprintln!("Err:{}", e.error_format());
+            } else {
+                println!("Service {} startup success...", id);
+            }
+        }
+    }
+
+    // 启动完服务后进入主循环
+    loop {
+        // 检查各服务运行状态
+        //eprintln!(".");
+        Manager::check_running_status();
+    }
 }
 
 #[cfg(not(target_os = "dragonos"))]
 fn main() {
-    use unit::service::ServiceUnit;
+    use std::time::Instant;
 
-    let service = match ServiceUnit::from_path("/home/heyicong/DragonReach/parse_test/test.service"){
-        Ok(service) => service,
-        Err(e) => {
-            println!("Error:{}",e.error_format());
-            return;
-        }
+    use parse::UnitParser;
+
+    use crate::{
+        executor::Executor,
+        manager::{Manager, UnitManager},
+        parse::parse_util::UnitParseUtil,
     };
 
-    
-    let service = service.as_ref();
-    println!("parse_result:");
-    println!("Description:{:?}", service.unit_base().unit_part().description());
-    println!("Documentation:{:?}",service.unit_base().unit_part().documentation());
-    println!("ServiceType:{:?}",service.service_part().service_type());
-    println!("ExecStrat:{:?}",service.service_part().exec_start());
-    println!("WorkingDirectory:{:?}",service.service_part().working_directory());
-    println!("Environment:{:?}",service.service_part().environment());
-    println!("Restart:{:?}",service.service_part().restart());
-    println!("RestartSec:{:?}",service.service_part().restart_sec());
-    println!("User:{:?}",service.service_part().user());
-    println!("Group:{:?}",service.service_part().group());
-    println!("TimeoutStartSec:{:?}",service.service_part().timeout_start_sec());
-    println!("TimeoutStopSec:{:?}",service.service_part().timeout_stop_sec());
+    let mut units_file_name = Vec::new();
+    //读取目录里面的unit文件
+    if let Ok(entries) = fs::read_dir("/bin") {
+        for entry in entries {
+            if let Ok(entry) = entry {
+                if let Ok(file_type) = entry.file_type() {
+                    if file_type.is_file() {
+                        let filename = entry.file_name();
+                        let filename = filename.to_str().unwrap();
+                        //units_file_name.push(filename.to_string());
+                    }
+                }
+            }
+        }
+    }
+  
+    units_file_name.push("/home/heyicong/DragonReach/parse_test/test.service".to_string());
+
+    //启动服务
+    for path in units_file_name {
+        let id = match UnitParser::from_path(&path) {
+            Ok(id) => id,
+            Err(e) => {
+                eprintln!("Err:{}", e.error_format());
+                0
+            }
+        };
+
+        if id != 0 {
+            let unit = UnitManager::get_unit_with_id(&id).unwrap();
+            if let Err(e) = Executor::exec(&unit) {
+                eprintln!("Err:{}", e.error_format());
+            }
+        }
+    }
+
+    // 启动完服务后进入主循环
+    loop {
+        let time = Instant::now();
+
+        // 检查各服务运行状态
+        Manager::check_running_status();
+        //println!(".");
+
+        let t = time.elapsed().as_micros();
+        //println!("{}",t);
+    }
 }

+ 75 - 0
src/manager/mod.rs

@@ -0,0 +1,75 @@
+#[cfg(target_os = "dragonos")]
+use drstd as std;
+use std::fs::File;
+use std::{eprint, eprintln, os::fd::AsFd, print, println, process::Child, vec::Vec};
+
+pub mod unit_manager;
+use std::io::Read;
+use std::os::unix::io::{AsRawFd, FromRawFd};
+pub use unit_manager::*;
+
+use crate::executor::ExitStatus;
+
+pub struct Manager;
+
+impl Manager {
+    /// ## 检查处于运行状态的unit状态
+    pub fn check_running_status() {
+        let mut running_manager = RUNNING_TABLE.write().unwrap();
+        let mut dead_unit: Vec<usize> = Vec::new();
+        let mut exited_unit: Vec<(usize, ExitStatus)> = Vec::new();
+        for unit in running_manager.into_iter() {
+            let proc = unit.child();
+            match proc.try_wait() {
+                //进程正常退出
+                Ok(Some(status)) => {
+                    //TODO:交付给相应类型的Unit类型去执行退出后的逻辑
+                    println!("Service exited success");
+
+                    exited_unit.push((
+                        *unit.id(),
+                        ExitStatus::from_exit_code(status.code().unwrap()),
+                    ));
+
+                    //退出后从表中去除该任务
+                    dead_unit.push(*unit.id());
+                }
+                //进程错误退出(或启动失败)
+                Err(e) => {
+                    eprintln!("unit error: {}", e);
+
+                    //test
+                    exited_unit.push((*unit.id(), ExitStatus::from_exit_code(!0)));
+
+                    //从表中去除该任务
+                    dead_unit.push(*unit.id());
+                }
+                //进程处于正常运行状态
+                _ => {}
+            }
+        }
+        //释放锁,以便后续删除操作能拿到锁
+        drop(running_manager);
+
+        //从表中清除数据
+        for id in dead_unit {
+            UnitManager::remove_running(id);
+        }
+
+        if UnitManager::running_count() == 0 {
+            let unit = UnitManager::pop_a_idle_service();
+            match unit {
+                Some(unit) => {
+                    let _ = unit.lock().unwrap().run();
+                }
+                None => {}
+            }
+        }
+
+        // 交付处理子进程退出逻辑
+        for tmp in exited_unit {
+            let unit = UnitManager::get_unit_with_id(&tmp.0).unwrap();
+            unit.lock().unwrap().after_exit(tmp.1);
+        }
+    }
+}

+ 197 - 0
src/manager/unit_manager/mod.rs

@@ -0,0 +1,197 @@
+#[cfg(target_os = "dragonos")]
+use drstd as std;
+use std::hash::{Hash, Hasher};
+use std::{
+    collections::hash_map::DefaultHasher,
+    collections::vec_deque::VecDeque,
+    process::Child,
+    sync::{Arc, Mutex, RwLock},
+    vec::Vec,
+};
+
+use crate::unit::service::ServiceUnit;
+use crate::unit::{service, Unit};
+use hashbrown::HashMap;
+use lazy_static::lazy_static;
+
+lazy_static! {
+    // 对于启动后即使退出亦认为其为运行状态的特殊注册类Service,对于这类进程做一个标记
+    static ref FLAG_RUNNING: RwLock<Vec<usize>> = RwLock::new(Vec::new());
+
+    // 任务等待队列,IDLE类型的service入队等待其它任务完成再执行
+    static ref IDLE_SERVIEC_DEQUE: Mutex<VecDeque<usize>> = Mutex::new(VecDeque::new());
+
+    // id到unit的映射表,全局的Unit管理表
+    static ref ID_TO_UNIT_MAP: RwLock<HashMap<usize,Arc<Mutex<dyn Unit>>>> = RwLock::new(HashMap::new());
+
+    // 辅助表,通过服务名映射其id
+    static ref PATH_TO_UNIT_MAP: RwLock<HashMap<u64,usize>> = RwLock::new(HashMap::new());
+
+    // 全局运行中的Unit表
+    pub(super) static ref RUNNING_TABLE: RwLock<RunningTableManager> = RwLock::new(RunningTableManager { running_table: Vec::new() });
+
+    // CMD进程表,用于处理Unit的CMD派生进程(ExecStartPre等命令派生进程)
+    pub(super) static ref CMD_PROCESS_TABLE: RwLock<HashMap<u32,Mutex<Child>>> = RwLock::new(HashMap::new());
+}
+
+pub struct RunningTableManager {
+    running_table: Vec<RunningUnit>,
+}
+
+impl<'a> IntoIterator for &'a mut RunningTableManager {
+    type Item = &'a mut RunningUnit;
+    type IntoIter = std::slice::IterMut<'a, RunningUnit>;
+
+    fn into_iter(self) -> Self::IntoIter {
+        self.running_table.iter_mut()
+    }
+}
+
+pub struct RunningUnit {
+    process: Child,
+    unit_id: usize,
+}
+impl RunningUnit {
+    pub fn new(p: Child, unit: usize) -> Self {
+        RunningUnit {
+            process: p,
+            unit_id: unit,
+        }
+    }
+
+    pub fn child(&mut self) -> &mut Child {
+        &mut self.process
+    }
+
+    pub fn id(&self) -> &usize {
+        &self.unit_id
+    }
+}
+
+pub struct UnitManager;
+
+unsafe impl Sync for UnitManager {}
+
+impl UnitManager {
+    pub fn insert_into_path_table(path: &str, unit: usize) {
+        let mut hasher = DefaultHasher::new();
+        path.hash(&mut hasher);
+        let hash = hasher.finish();
+        PATH_TO_UNIT_MAP.write().unwrap().insert(hash, unit);
+    }
+
+    pub fn contains_path(path: &str) -> bool {
+        let mut hasher = DefaultHasher::new();
+        path.hash(&mut hasher);
+        let hash = hasher.finish();
+        PATH_TO_UNIT_MAP.read().unwrap().contains_key(&hash)
+    }
+
+    pub fn get_unit_with_path(path: &str) -> Option<Arc<Mutex<dyn Unit>>> {
+        let mut hasher = DefaultHasher::new();
+        path.hash(&mut hasher);
+        let hash = hasher.finish();
+        let map = PATH_TO_UNIT_MAP.read().unwrap();
+        let id = match map.get(&hash) {
+            Some(id) => id,
+            None => {
+                return None;
+            }
+        };
+
+        let map = ID_TO_UNIT_MAP.read().unwrap();
+        let ret = map.get(id).cloned();
+        ret
+    }
+
+    pub fn get_unit_with_id(id: &usize) -> Option<Arc<Mutex<dyn Unit>>> {
+        let map = ID_TO_UNIT_MAP.read().unwrap();
+        let ret = map.get(&id).cloned();
+        ret
+    }
+
+    pub fn get_id_with_path(path: &str) -> Option<usize> {
+        let mut hasher = DefaultHasher::new();
+        path.hash(&mut hasher);
+        let hash = hasher.finish();
+        PATH_TO_UNIT_MAP.read().unwrap().get(&hash).cloned()
+    }
+
+    pub fn is_running_unit(id: &usize) -> bool {
+        !RUNNING_TABLE
+            .read()
+            .unwrap()
+            .running_table
+            .iter()
+            .filter(|x| x.unit_id == *id)
+            .collect::<Vec<_>>()
+            .is_empty()
+    }
+
+    pub fn push_running(unit: RunningUnit) {
+        RUNNING_TABLE.write().unwrap().running_table.push(unit);
+    }
+
+    pub fn remove_running(id: usize) {
+        let mut table = RUNNING_TABLE.write().unwrap();
+        match table.running_table.iter().position(|x| x.unit_id == id) {
+            Some(idx) => {
+                table.running_table.remove(idx);
+            }
+            _ => (),
+        }
+    }
+
+    pub fn insert_unit_with_id(id: usize, unit: Arc<Mutex<dyn Unit>>) {
+        let mut map = ID_TO_UNIT_MAP.write().unwrap();
+        if !map.contains_key(&id) {
+            map.insert(id, unit);
+        }
+    }
+
+    pub fn contains_id(id: &usize) -> bool {
+        ID_TO_UNIT_MAP.read().unwrap().contains_key(id)
+    }
+
+    pub fn pop_a_idle_service() -> Option<Arc<Mutex<dyn Unit>>> {
+        let id = IDLE_SERVIEC_DEQUE.lock().unwrap().pop_front();
+        match id {
+            Some(id) => {
+                return Self::get_unit_with_id(&id);
+            }
+            None => {
+                return None;
+            }
+        }
+    }
+
+    pub fn push_a_idle_service(id: usize) {
+        if !Self::contains_id(&id) {
+            return;
+        }
+        IDLE_SERVIEC_DEQUE.lock().unwrap().push_back(id);
+    }
+
+    pub fn push_flag_running(id: usize) {
+        let mut t = FLAG_RUNNING.write().unwrap();
+        if t.contains(&id) {
+            return;
+        }
+        t.push(id);
+    }
+
+    pub fn running_count() -> usize {
+        return RUNNING_TABLE.read().unwrap().running_table.len();
+    }
+
+    pub fn push_cmd_proc(proc: Child) {
+        CMD_PROCESS_TABLE
+            .write()
+            .unwrap()
+            .insert(proc.id(), Mutex::new(proc));
+    }
+
+    pub fn remove_cmd_proc(id: u32) {
+        CMD_PROCESS_TABLE.write().unwrap().remove(&id);
+    }
+}

+ 207 - 0
src/parse/graph/mod.rs

@@ -0,0 +1,207 @@
+use crate::{
+    error::parse_error::{ParseError, ParseErrorType},
+    unit::UnitType,
+};
+use core::slice::SlicePattern;
+#[cfg(target_os = "dragonos")]
+use drstd as std;
+use std::string::{String, ToString};
+use std::vec::Vec;
+use std::{
+    fs::File,
+    io::{BufRead, BufReader},
+};
+
+use super::{parse_util::UnitParseUtil, UnitParser};
+
+pub struct GraphNode {
+    value: String,
+    edges: Vec<usize>,
+    incoming_edges: Vec<usize>,
+}
+
+pub struct Graph {
+    total_edge: u32,
+    max_edge: u32,
+    nodes: Vec<GraphNode>,
+    value: Vec<String>,
+}
+
+impl Graph {
+    fn new() -> Self {
+        return Graph {
+            max_edge: 0,
+            total_edge: 0,
+            nodes: Vec::new(),
+            value: Vec::new(),
+        };
+    }
+
+    pub fn add_node(&mut self, value: &String) -> usize {
+        let index = self.nodes.len();
+        //如果nodes中已经有了这个value则无需重复添加,直接返回nodes中的value对应的index
+        if let Some(idx) = self.value.iter().position(|x| *x == *value) {
+            return idx;
+        }
+        //如果value在nodes中不存在,则添加value
+        self.nodes.push(GraphNode {
+            value: value.to_string(),
+            edges: Vec::new(),
+            incoming_edges: Vec::new(),
+        });
+        self.value.push(value.to_string());
+        return index;
+    }
+    pub fn add_edge(&mut self, from: usize, to: usize) {
+        self.total_edge += 1;
+        self.nodes[from].edges.push(to);
+        self.nodes[to].incoming_edges.push(from);
+    }
+    pub fn topological_sort(&mut self) -> Result<Vec<String>, ParseError> {
+        let mut result = Vec::new();
+        let mut visited = Vec::new();
+        let mut stack = Vec::new();
+        for (i, node) in self.nodes.iter().enumerate() {
+            if node.incoming_edges.len() == 0 {
+                stack.push(i);
+            }
+        }
+        while stack.len() > 0 {
+            let index = stack.pop().unwrap();
+            if visited.contains(&index) {
+                continue;
+            }
+            visited.push(index);
+            result.push(self.nodes[index].value.clone());
+            let len = self.nodes[index].edges.len();
+            for i in 0..len {
+                let edge = self.nodes[index].edges[i];
+                self.nodes[edge].incoming_edges.retain(|&x| x != index);
+                if self.nodes[edge].incoming_edges.len() == 0 {
+                    stack.push(edge);
+                }
+            }
+        }
+        if result.len() != self.nodes.len() {
+            return Err(ParseError::new(
+                ParseErrorType::ECircularDependency,
+                "".to_string(),
+                0,
+            ));
+        }
+        result.reverse();
+        return Ok(result);
+    }
+
+    fn add_edges(&mut self, path: &String, after: Vec<String>) {
+        //因为service的依赖关系规模不会很大,故先使用递归实现
+        //TODO:改递归
+        for target in after {
+            let s = self.add_node(&path);
+            let t = self.add_node(&target);
+            self.add_edge(s, t);
+            if self.total_edge > self.max_edge {
+                return;
+            }
+            self.add_edges(&target, Self::parse_after(&target));
+        }
+    }
+
+    pub fn construct_graph(unit: String) -> Result<Graph, ParseError> {
+        //计算整个依赖图中的节点数
+        let mut node_num = 1;
+        let mut dep = Vec::new();
+        Self::get_node_num(&unit, &mut dep, &mut node_num);
+
+        let mut graph: Graph = Graph::new();
+        graph.max_edge = node_num * (node_num - 1);
+
+        graph.add_node(&unit);
+        let after = Self::parse_after(&unit);
+        //递归添加边来构建图
+        graph.add_edges(&unit, after);
+
+        if graph.max_edge < graph.total_edge {
+            return Err(ParseError::new(
+                ParseErrorType::ECircularDependency,
+                unit,
+                0,
+            ));
+        }
+
+        return Ok(graph);
+    }
+
+    pub fn parse_after(path: &String) -> Vec<String> {
+        let mut ret = Vec::new();
+
+        let reader = UnitParser::get_reader(path, UnitType::Unknown).unwrap();
+
+        let mut lines_with_after = Vec::new();
+
+        for line_result in reader.lines() {
+            if let Ok(line) = line_result {
+                if line.starts_with("After") {
+                    lines_with_after.push(line);
+                }
+            }
+        }
+
+        for line in lines_with_after {
+            let after = &line.split('=').collect::<Vec<&str>>()[1];
+            let units = after.split_whitespace().collect::<Vec<&str>>();
+
+            for unit in units {
+                ret.push(String::from(unit));
+            }
+        }
+
+        ret
+    }
+
+    /// ## 获取到unit文件依赖图节点数
+    ///
+    /// ### param file_path unit文件路径
+    ///
+    /// ### dependencies 缓存after依赖的容器
+    ///
+    /// ### total_after_count 返回节点数
+    ///
+    /// ### return
+    fn get_node_num(
+        file_path: &str,
+        dependencies: &mut Vec<String>,
+        total_after_count: &mut u32,
+    ) -> Result<(), ParseError> {
+        let reader = UnitParser::get_reader(file_path, UnitType::Unknown)?;
+
+        let mut current_after_count = 0;
+
+        for line_result in reader.lines() {
+            if let Ok(line) = line_result {
+                if line.starts_with("After=") {
+                    let dependencies_str = &line[6..];
+                    let dependency_list: Vec<&str> = dependencies_str.split_whitespace().collect();
+
+                    for dependency in dependency_list {
+                        if dependencies.contains(&dependency.to_string()) {
+                            // 循环依赖检查
+                            continue;
+                        }
+
+                        dependencies.push(dependency.to_string());
+
+                        // 递归解析依赖链
+                        Self::get_node_num(dependency, dependencies, total_after_count)?;
+                    }
+
+                    current_after_count += 1;
+                }
+            }
+        }
+
+        *total_after_count += current_after_count;
+
+        Ok(())
+    }
+}

+ 92 - 41
src/parse/mod.rs

@@ -1,7 +1,9 @@
-use crate::error::ParseErrorType;
+use crate::error::parse_error::ParseErrorType;
+use crate::manager::{self, UnitManager};
 use crate::unit::{BaseUnit, Unit};
+use crate::DRAGON_REACH_UNIT_DIR;
 use crate::{
-    error::ParseError,
+    error::parse_error::ParseError,
     unit::{service::ServiceUnitAttr, BaseUnitAttr, InstallUnitAttr, UnitType},
 };
 
@@ -13,13 +15,18 @@ use lazy_static::lazy_static;
 use std::format;
 use std::fs::File;
 use std::io::{self, BufRead};
-use std::rc::Rc;
 use std::string::String;
-use std::vec::Vec;
 use std::string::ToString;
+use std::sync::{Arc, Mutex};
+use std::vec::Vec;
 
-pub mod parse_target;
+use self::parse_service::ServiceParser;
+use self::parse_target::TargetParser;
+use self::parse_util::UnitParseUtil;
+
+pub mod graph;
 pub mod parse_service;
+pub mod parse_target;
 pub mod parse_util;
 
 //对应Unit段类型
@@ -151,37 +158,54 @@ pub struct UnitParser;
 impl UnitParser {
     /// @brief 从path获取到BufReader,此方法将会检验文件类型
     ///
-    /// 从path获取到BufReader,此方法将会检验文件类型
+    /// 如果指定UnitType,则进行文件名检查
     ///
     /// @param path 需解析的文件路径
     ///
     /// @param unit_type 指定Unit类型
     ///
     /// @return 成功则返回对应BufReader,否则返回Err
-    fn get_unit_reader(path: &str, unit_type: UnitType) -> Result<io::BufReader<File>, ParseError> {
-        let suffix = match path.rfind('.') {
-            Some(idx) => &path[idx + 1..],
-            None => {
-                return Err(ParseError::new(ParseErrorType::EFILE, path.to_string(),0));
-            }
-        };
-        let u_type = UNIT_SUFFIX.get(suffix);
-        if u_type.is_none() {
-            return Err(ParseError::new(ParseErrorType::EFILE, path.to_string(),0));
+    pub fn get_reader(path: &str, unit_type: UnitType) -> Result<io::BufReader<File>, ParseError> {
+        //判断是否为路径,若不为路径则到定向到默认unit文件夹
+        let mut realpath = path.to_string();
+        if !path.contains('/') {
+            realpath = format!("{}{}", DRAGON_REACH_UNIT_DIR, &path).to_string();
         }
-        if *(u_type.unwrap()) != unit_type {
-            return Err(ParseError::new(ParseErrorType::EFILE, path.to_string(),0));
+        let path = realpath.as_str();
+        // 如果指定UnitType,则进行文件名检查,不然直接返回reader
+        if unit_type != UnitType::Unknown {
+            let suffix = match path.rfind('.') {
+                Some(idx) => &path[idx + 1..],
+                None => {
+                    return Err(ParseError::new(ParseErrorType::EFILE, path.to_string(), 0));
+                }
+            };
+            let u_type = UNIT_SUFFIX.get(suffix);
+            if u_type.is_none() {
+                return Err(ParseError::new(ParseErrorType::EFILE, path.to_string(), 0));
+            }
+            if *(u_type.unwrap()) != unit_type {
+                return Err(ParseError::new(ParseErrorType::EFILE, path.to_string(), 0));
+            }
         }
-
         let file = match File::open(path) {
             Ok(file) => file,
             Err(_) => {
-                return Err(ParseError::new(ParseErrorType::EFILE, path.to_string(),0));
+                return Err(ParseError::new(ParseErrorType::EFILE, path.to_string(), 0));
             }
         };
         return Ok(io::BufReader::new(file));
     }
 
+    pub fn from_path(path: &str) -> Result<usize, ParseError> {
+        let unit_type = UnitParseUtil::parse_type(&path);
+        match unit_type {
+            UnitType::Service => ServiceParser::parse(path),
+            UnitType::Target => TargetParser::parse(path),
+            _ => Err(ParseError::new(ParseErrorType::EFILE, path.to_string(), 0)),
+        }
+    }
+
     /// @brief 将path路径的文件解析为unit_type类型的Unit
     ///
     /// 该方法解析每个Unit共有的段(Unit,Install),其余独有的段属性将会交付T类型的Unit去解析
@@ -190,14 +214,24 @@ impl UnitParser {
     ///
     /// @param unit_type 指定Unit类型
     ///
-    /// @return 解析成功则返回Ok(Rc<T>),否则返回Err
-    pub fn parse<T: Unit + Default>(path: &str, unit_type: UnitType) -> Result<Rc<T>, ParseError> {
+    /// @return 解析成功则返回Ok(Arc<T>),否则返回Err
+    pub fn parse<T: Unit + Default + Clone + 'static>(
+        path: &str,
+        unit_type: UnitType,
+    ) -> Result<usize, ParseError> {
+        // 如果该文件已解析过,则直接返回id
+        if UnitManager::contains_path(path) {
+            let unit = UnitManager::get_unit_with_path(path).unwrap();
+            let unit = unit.lock().unwrap();
+            return Ok(unit.unit_id());
+        }
+
         let mut unit: T = T::default();
         let mut unit_base = BaseUnit::default();
         //设置unit类型标记
         unit_base.set_unit_type(unit_type);
 
-        let reader = UnitParser::get_unit_reader(path, unit_type)?;
+        let reader = UnitParser::get_reader(path, unit_type)?;
 
         //用于记录当前段的类型
         let mut segment = Segment::None;
@@ -232,7 +266,11 @@ impl UnitParser {
             }
             if segment == Segment::None {
                 //未找到段名则不能继续匹配
-                return Err(ParseError::new(ParseErrorType::ESyntaxError, path.to_string(),i + 1));
+                return Err(ParseError::new(
+                    ParseErrorType::ESyntaxError,
+                    path.to_string(),
+                    i + 1,
+                ));
             }
 
             //下面进行属性匹配
@@ -251,23 +289,28 @@ impl UnitParser {
                 break;
             }
             //=号分割后第一个元素为属性,后面的均为值,若一行出现两个等号则是语法错误
-            let (attr_str,val_str) = match line.find('=') {
-                Some(idx) => {
-                    (line[..idx].trim(), line[idx+1..].trim())
-                }
+            let (attr_str, val_str) = match line.find('=') {
+                Some(idx) => (line[..idx].trim(), line[idx + 1..].trim()),
                 None => {
-                    return Err(ParseError::new(ParseErrorType::ESyntaxError, path.to_string(),i + 1));
+                    return Err(ParseError::new(
+                        ParseErrorType::ESyntaxError,
+                        path.to_string(),
+                        i + 1,
+                    ));
                 }
             };
             //首先匹配所有unit文件都有的unit段和install段
             if BASE_UNIT_ATTR_TABLE.get(attr_str).is_some() {
                 if segment != Segment::Unit {
-                    return Err(ParseError::new(ParseErrorType::EINVAL, path.to_string(),i + 1));
+                    return Err(ParseError::new(
+                        ParseErrorType::EINVAL,
+                        path.to_string(),
+                        i + 1,
+                    ));
                 }
-                if let Err(e) = unit_base.set_unit_part_attr(
-                    BASE_UNIT_ATTR_TABLE.get(attr_str).unwrap(),
-                    val_str,
-                ){
+                if let Err(e) = unit_base
+                    .set_unit_part_attr(BASE_UNIT_ATTR_TABLE.get(attr_str).unwrap(), val_str)
+                {
                     let mut e = e.clone();
                     e.set_file(path);
                     e.set_linenum(i + 1);
@@ -275,19 +318,22 @@ impl UnitParser {
                 }
             } else if INSTALL_UNIT_ATTR_TABLE.get(attr_str).is_some() {
                 if segment != Segment::Install {
-                    return Err(ParseError::new(ParseErrorType::EINVAL, path.to_string(),i + 1));
+                    return Err(ParseError::new(
+                        ParseErrorType::EINVAL,
+                        path.to_string(),
+                        i + 1,
+                    ));
                 }
-                if let Err(e) = unit_base.set_install_part_attr(
-                    INSTALL_UNIT_ATTR_TABLE.get(attr_str).unwrap(),
-                    val_str,
-                ){
+                if let Err(e) = unit_base
+                    .set_install_part_attr(INSTALL_UNIT_ATTR_TABLE.get(attr_str).unwrap(), val_str)
+                {
                     let mut e = e.clone();
                     e.set_file(path);
                     e.set_linenum(i + 1);
                     return Err(e);
                 }
             } else {
-                if let Err(e) = unit.set_attr(segment, attr_str, val_str){
+                if let Err(e) = unit.set_attr(segment, attr_str, val_str) {
                     let mut e = e.clone();
                     e.set_file(path);
                     e.set_linenum(i + 1);
@@ -297,6 +343,11 @@ impl UnitParser {
             i += 1;
         }
         unit.set_unit_base(unit_base);
-        return Ok(Rc::new(unit));
+        let id = unit.set_unit_id();
+        let dret: Arc<Mutex<dyn Unit>> = Arc::new(Mutex::new(unit));
+        UnitManager::insert_unit_with_id(id, dret);
+        UnitManager::insert_into_path_table(path, id);
+
+        return Ok(id);
     }
 }

+ 19 - 6
src/parse/parse_service/mod.rs

@@ -1,11 +1,17 @@
+use super::graph::Graph;
+use super::parse_util::UnitParseUtil;
 use super::UnitParser;
-use crate::error::ParseError;
-use crate::unit::service::ServiceUnit;
+use crate::error::parse_error::ParseError;
+use crate::manager::UnitManager;
+use crate::unit::service::{self, ServiceUnit};
 
 #[cfg(target_os = "dragonos")]
 use drstd as std;
 
+use std::any::Any;
 use std::rc::Rc;
+use std::string::ToString;
+use std::sync::Arc;
 pub struct ServiceParser;
 
 impl ServiceParser {
@@ -16,9 +22,16 @@ impl ServiceParser {
     /// @param path 需解析的文件路径
     ///
     /// @return 成功则返回Ok(Rc<ServiceUnit>),否则返回Err
-    pub fn parse(path: &str) -> Result<Rc<ServiceUnit>, ParseError> {
-        //交付总解析器
-        let service = UnitParser::parse::<ServiceUnit>(path, crate::unit::UnitType::Service)?;
-        return Ok(service);
+    pub fn parse(path: &str) -> Result<usize, ParseError> {
+        //预先检查是否存在循环依赖
+        let mut graph = Graph::construct_graph(path.to_string())?;
+        let ret = graph.topological_sort()?;
+        for p in ret {
+            let temp_unit = UnitParseUtil::parse_unit_no_type(&p)?;
+        }
+
+        let result = UnitManager::get_id_with_path(path).unwrap();
+
+        return Ok(result);
     }
 }

+ 17 - 6
src/parse/parse_target/mod.rs

@@ -1,11 +1,16 @@
+use super::graph::Graph;
+use super::parse_util::UnitParseUtil;
 use super::UnitParser;
-use crate::error::ParseError;
+use crate::error::parse_error::ParseError;
+use crate::manager::UnitManager;
 use crate::unit::target::TargetUnit;
 
 #[cfg(target_os = "dragonos")]
 use drstd as std;
 
 use std::rc::Rc;
+use std::string::ToString;
+use std::sync::Arc;
 
 pub struct TargetParser;
 
@@ -17,9 +22,15 @@ impl TargetParser {
     /// @param path 需解析的文件路径
     ///
     /// @return 成功则返回Ok(Rc<ServiceUnit>),否则返回Err
-    pub fn parse(path: &str) -> Result<Rc<TargetUnit>, ParseError> {
-        //交付总解析器
-        let service = UnitParser::parse::<TargetUnit>(path, crate::unit::UnitType::Service)?;
-        return Ok(service);
+    pub fn parse(path: &str) -> Result<usize, ParseError> {
+        //预先检查是否存在循环依赖
+        let mut graph = Graph::construct_graph(path.to_string())?;
+        let ret = graph.topological_sort()?;
+        for p in ret {
+            let temp_unit = UnitParseUtil::parse_unit_no_type(&p)?;
+        }
+
+        let result = UnitManager::get_id_with_path(path).unwrap();
+        return Ok(result);
     }
-}
+}

+ 66 - 32
src/parse/parse_util/mod.rs

@@ -1,8 +1,8 @@
 use crate::{
     contants::{AF_INET, AF_INET6, IPV4_MIN_MTU, IPV6_MIN_MTU, PRIO_MAX, PRIO_MIN},
-    error::{ParseError, ParseErrorType},
+    error::{parse_error::ParseError, parse_error::ParseErrorType},
     task::cmdtask::CmdTask,
-    unit::{Unit, Url},
+    unit::{service::ServiceUnit, target::TargetUnit, Unit, UnitType, Url},
     FileDescriptor,
 };
 
@@ -10,13 +10,13 @@ use crate::{
 use drstd as std;
 
 use std::{
-    format, fs, path::Path, print, println, rc::Rc, string::String, string::ToString, vec, vec::Vec, os::unix::prelude::PermissionsExt,
+    any::Any, format, fs, io::BufRead, os::unix::prelude::PermissionsExt, path::Path, print,
+    println, string::String, string::ToString, sync::Arc, vec, vec::Vec,
 };
 
-use std::os::unix::fs::MetadataExt;
-
 use super::{
-    parse_service::ServiceParser, parse_target::TargetParser, BASE_IEC, BASE_SI, SEC_UNIT_TABLE,
+    parse_service::ServiceParser, parse_target::TargetParser, UnitParser, BASE_IEC, BASE_SI,
+    SEC_UNIT_TABLE,
 };
 
 #[derive(PartialEq)]
@@ -430,19 +430,19 @@ impl UnitParseUtil {
     ///
     /// @param path 需解析的文件
     ///
-    /// @return 解析成功则返回Ok(Rc<dyn Unit>),否则返回Err
-    pub fn parse_unit<T: Unit>(path: &str) -> Result<Rc<T>, ParseError> {
+    /// @return 解析成功则返回Ok(Arc<dyn Unit>),否则返回Err
+    pub fn parse_unit<T: Unit>(path: &str) -> Result<usize, ParseError> {
         return T::from_path(path);
     }
 
-    /// @brief 将对应的str解析为Rc<dyn Unit>
+    /// @brief 将对应的str解析为Arc<dyn Unit>
     ///
-    /// 将传入的字符串解析为Rc<dyn Unit>,解析失败返回错误
+    /// 将传入的字符串解析为Arc<dyn Unit>,解析失败返回错误
     ///
     /// @param path 需解析的文件
     ///
-    /// @return 解析成功则返回Ok(Rc<dyn Unit>),否则返回Err
-    pub fn parse_unit_no_type(path: &str) -> Result<Rc<dyn Unit>, ParseError> {
+    /// @return 解析成功则返回Ok(Arc<dyn Unit>),否则返回Err
+    pub fn parse_unit_no_type(path: &str) -> Result<usize, ParseError> {
         let idx = match path.rfind('.') {
             Some(val) => val,
             None => {
@@ -458,10 +458,10 @@ impl UnitParseUtil {
         let suffix = &path[idx + 1..];
 
         //通过文件后缀分发给不同类型的Unit解析器解析
-        let unit: Rc<dyn Unit> = match suffix {
+        let unit = match suffix {
             //TODO: 目前为递归,后续应考虑从DragonReach管理的Unit表中寻找是否有该Unit,并且通过记录消除递归
-            "service" => ServiceParser::parse(path)?,
-            "target" => TargetParser::parse(path)?,
+            "service" => UnitParser::parse::<ServiceUnit>(path, UnitType::Service)?,
+            "target" => UnitParser::parse::<TargetUnit>(path, UnitType::Target)?,
             _ => {
                 return Err(ParseError::new(ParseErrorType::EFILE, path.to_string(), 0));
             }
@@ -470,6 +470,15 @@ impl UnitParseUtil {
         return Ok(unit);
     }
 
+    pub fn parse_env(s: &str) -> Result<(String, String), ParseError> {
+        let s = s.trim().split('=').collect::<Vec<&str>>();
+        if s.len() != 2 {
+            return Err(ParseError::new(ParseErrorType::EINVAL, "".to_string(), 0));
+        }
+
+        return Ok((s[0].to_string(), s[1].to_string()));
+    }
+
     /// @brief 将对应的str解析为对应CmdTask
     ///
     /// 将传入的字符串解析为CmdTask组,解析失败返回错误
@@ -485,37 +494,37 @@ impl UnitParseUtil {
         while i < cmds.len() {
             let mut cmd_task = CmdTask {
                 path: String::new(),
-                cmd: String::new(),
+                cmd: Vec::new(),
                 ignore: false,
             };
             //匹配到这里时,这个单词肯定是路径,若路径以-开头则设置ignore
             cmd_task.ignore = cmds[i].starts_with('-');
 
             //获取到一个CmdTask的路径部分
-            let mut path = "";
+            let mut path = String::new();
             if cmd_task.ignore {
-                path = &cmds[i][1..];
+                path = String::from(&cmds[i][1..]);
             } else {
-                path = &cmds[i];
+                path = String::from(cmds[i]);
             }
 
             //得到的非绝对路径则不符合语法要求,报错
-            if !UnitParseUtil::is_valid_exec_path(path) {
+            if !UnitParseUtil::is_valid_exec_path(path.as_str()) {
                 return Err(ParseError::new(ParseErrorType::EINVAL, String::new(), 0));
             }
 
-            cmd_task.path = String::from(path);
+            cmd_task.path = path;
 
             //i += 1,继续匹配下一个单词
             i += 1;
-            let mut cmd_str = String::new();
+            let mut cmd_vec = Vec::new();
             while i < cmds.len() && !UnitParseUtil::is_valid_exec_path(cmds[i]) {
                 //命令可能会有多个单词,将多个命令整理成一个
                 let cmd = cmds[i];
-                cmd_str = format!("{} {}", cmd_str, cmd);
+                cmd_vec.push(String::from(cmd));
                 i += 1;
             }
-            cmd_task.cmd = cmd_str;
+            cmd_task.cmd = cmd_vec;
             tasks.push(cmd_task);
             //经过while到这里之后,cmds[i]对应的单词一点是路径,i不需要加一
         }
@@ -525,15 +534,13 @@ impl UnitParseUtil {
     /// @brief 判断是否为绝对路径,以及指向是否为可执行文件或者sh脚本
     ///
     /// 目前该方法仅判断是否为绝对路径
-    /// 
+    ///
     /// @param path 路径
     ///
     /// @return 解析成功则返回true,否则返回false
     pub fn is_valid_exec_path(path: &str) -> bool {
-        if !path.starts_with("/"){
-            return false;
-        }
-        return true;
+        let path = Path::new(path);
+        return path.is_absolute();
 
         //TODO: 后续应判断该文件是否为合法文件
         //let path = Path::new(path);
@@ -541,10 +548,10 @@ impl UnitParseUtil {
     }
 
     pub fn is_valid_file(path: &str) -> bool {
-        if !path.starts_with("/"){
+        if !path.starts_with("/") {
             return false;
         }
-        
+
         let path = Path::new(path);
         if let Ok(matadata) = fs::metadata(path) {
             return matadata.is_file();
@@ -556,7 +563,7 @@ impl UnitParseUtil {
     fn is_executable_file(path: &Path) -> bool {
         if let Ok(metadata) = fs::metadata(path) {
             // 检查文件类型是否是普通文件并且具有可执行权限
-            if metadata.is_file(){
+            if metadata.is_file() {
                 let permissions = metadata.permissions().mode();
                 return permissions & 0o111 != 0;
             }
@@ -687,4 +694,31 @@ impl UnitParseUtil {
         }
         return false;
     }
+
+    pub fn parse_type(path: &str) -> UnitType {
+        if let Some(index) = path.rfind('.') {
+            let result = &path[index + 1..];
+            match result {
+                "service" => return UnitType::Service,
+                "target" => return UnitType::Target,
+                //TODO: 添加文件类型
+                _ => return UnitType::Unknown,
+            }
+        }
+        UnitType::Unknown
+    }
+
+    pub fn parse_environment_file(env_file: &str) -> Result<Vec<(String, String)>, ParseError> {
+        let mut envs = Vec::new();
+        if env_file.len() > 0 {
+            let env_reader = UnitParser::get_reader(env_file, UnitType::Unknown)?;
+            for line in env_reader.lines() {
+                if let Ok(line) = line {
+                    let x = UnitParseUtil::parse_env(line.as_str())?;
+                    envs.push(x);
+                }
+            }
+        }
+        return Ok(envs);
+    }
 }

+ 59 - 3
src/task/cmdtask/mod.rs

@@ -1,11 +1,67 @@
 #[cfg(target_os = "dragonos")]
 use drstd as std;
 
-use std::string::String;
+use std::{
+    eprint, eprintln, print, println,
+    process::{Child, Command},
+    string::String,
+    vec::Vec,
+};
 
-#[derive(Debug)]
+use crate::{
+    error::runtime_error::{RuntimeError, RuntimeErrorType},
+    manager::UnitManager,
+};
+
+#[derive(Debug, Clone, Default)]
 pub struct CmdTask {
     pub path: String,
-    pub cmd: String,
+    pub cmd: Vec<String>,
     pub ignore: bool, //表示忽略这个命令的错误,即使它运行失败也不影响unit正常运作
 }
+
+impl CmdTask {
+    pub fn spawn(&self, dir: String, envs: &[(String, String)]) -> Result<(), RuntimeError> {
+        let result = Command::new(&self.path)
+            .args(&self.cmd)
+            .current_dir(dir)
+            .envs(Vec::from(envs))
+            .spawn();
+        match result {
+            Ok(proc) => {
+                UnitManager::push_cmd_proc(proc);
+            }
+            Err(err) => {
+                if !self.ignore {
+                    eprintln!("{}: Command failed: {}", self.path, err);
+                    return Err(RuntimeError::new(RuntimeErrorType::ExecFailed));
+                }
+            }
+        }
+        Ok(())
+    }
+
+    pub fn no_spawn(&self, dir: String, envs: &[(String, String)]) -> Result<(), RuntimeError> {
+        let result = Command::new(&self.path)
+            .args(&self.cmd)
+            .current_dir(dir)
+            .envs(Vec::from(envs))
+            .output();
+        match result {
+            Ok(output) => {
+                print!("{}", String::from_utf8_lossy(&output.stdout));
+                eprint!("{}", String::from_utf8_lossy(&output.stderr));
+                if !output.status.success() {
+                    return Err(RuntimeError::new(RuntimeErrorType::ExecFailed));
+                }
+            }
+            Err(err) => {
+                if !self.ignore {
+                    eprintln!("{}: Command failed: {}", self.path, err);
+                    return Err(RuntimeError::new(RuntimeErrorType::ExecFailed));
+                }
+            }
+        }
+        Ok(())
+    }
+}

+ 86 - 34
src/unit/mod.rs

@@ -1,36 +1,50 @@
-use crate::error::ParseError;
-use crate::error::ParseErrorType;
+use crate::error::parse_error::ParseError;
+use crate::error::parse_error::ParseErrorType;
+use crate::error::runtime_error::RuntimeError;
+use crate::error::runtime_error::RuntimeErrorType;
+use crate::executor::ExitStatus;
 use crate::parse::parse_util::UnitParseUtil;
 use crate::parse::Segment;
 
 #[cfg(target_os = "dragonos")]
 use drstd as std;
+use hashbrown::HashMap;
 
 use std::any::Any;
-use std::boxed::Box;
 use std::default::Default;
-use std::rc::Rc;
+use std::fmt::Debug;
+use std::marker::{Send, Sized, Sync};
+use std::option::Option::Some;
 use std::result::Result;
 use std::result::Result::Err;
 use std::result::Result::Ok;
 use std::string::String;
+use std::sync::atomic::AtomicUsize;
+use std::sync::atomic::Ordering;
+use std::sync::Arc;
 use std::vec::Vec;
 
 pub mod service;
 pub mod target;
 
 use self::target::TargetUnit;
+use lazy_static::lazy_static;
+
+pub fn generate_unit_id() -> usize {
+    static UNIT_ID: AtomicUsize = AtomicUsize::new(1);
+    return UNIT_ID.fetch_add(1, Ordering::SeqCst);
+}
 
 //所有可解析的Unit都应该实现该trait
-pub trait Unit {
+pub trait Unit: Sync + Send + Debug {
     /// @brief 从文件获取到Unit,该函数是解析Unit文件的入口函数
     ///
     /// 从path解析Unit属性
     ///
     /// @param path 需解析的文件
     ///
-    /// @return 解析成功则返回对应Unit的Rc指针,否则返回Err
-    fn from_path(path: &str) -> Result<Rc<Self>, ParseError>
+    /// @return 解析成功则返回对应Unit的id,否则返回Err
+    fn from_path(path: &str) -> Result<usize, ParseError>
     where
         Self: Sized;
 
@@ -60,11 +74,36 @@ pub trait Unit {
     ///
     /// ## return UnitType
     fn unit_type(&self) -> UnitType;
+
+    fn unit_base(&self) -> &BaseUnit;
+
+    fn mut_unit_base(&mut self) -> &mut BaseUnit;
+
+    fn unit_id(&self) -> usize;
+
+    /// ## Unit的工作逻辑
+    ///
+    /// ### return OK(())/Err
+    fn run(&mut self) -> Result<(), RuntimeError>;
+
+    /// ## 设置unit_id
+    ///
+    /// ### return OK(())/Err
+    fn set_unit_id(&mut self) -> usize {
+        let ret = generate_unit_id();
+        self.mut_unit_base().set_id(ret);
+        ret
+    }
+
+    /// ## Unit退出后逻辑
+    ///
+    /// 一般只有可运行的Unit(如Service)需要重写此函数
+    fn after_exit(&mut self, exit_status: ExitStatus) {}
 }
 
 //Unit状态
-#[derive(Clone, Copy, Debug)]
-enum UnitState {
+#[derive(Clone, Copy, Debug, PartialEq)]
+pub enum UnitState {
     Enabled,
     Disabled,
     Static,
@@ -90,11 +129,13 @@ pub enum UnitType {
 }
 
 //记录unit文件基本信息,这个结构体里面的信息是所有Unit文件都可以有的属性
+#[derive(Debug, Clone)]
 pub struct BaseUnit {
     unit_part: UnitPart,
     install_part: InstallPart,
     state: UnitState,
     unit_type: UnitType,
+    unit_id: usize,
 }
 
 impl Default for BaseUnit {
@@ -104,6 +145,7 @@ impl Default for BaseUnit {
             install_part: InstallPart::default(),
             state: UnitState::Disabled,
             unit_type: UnitType::Unknown,
+            unit_id: 0,
         }
     }
 }
@@ -152,9 +194,13 @@ impl BaseUnit {
     pub fn unit_type(&self) -> &UnitType {
         &self.unit_type
     }
+
+    pub fn set_id(&mut self, id: usize) {
+        self.unit_id = id;
+    }
 }
 
-#[derive(Default,Debug)]
+#[derive(Default, Debug, Clone)]
 pub struct Url {
     pub url_string: String, // pub protocol: String,
                             // pub host: String,
@@ -165,17 +211,18 @@ pub struct Url {
 }
 
 //对应Unit文件的Unit段
+#[derive(Debug, Clone)]
 pub struct UnitPart {
     description: String,
     documentation: Vec<Url>,
-    requires: Vec<Rc<dyn Unit>>,
-    wants: Vec<Rc<dyn Unit>>,
-    after: Vec<Rc<dyn Unit>>,
-    before: Vec<Rc<dyn Unit>>,
-    binds_to: Vec<Rc<dyn Unit>>,
-    part_of: Vec<Rc<dyn Unit>>,
-    on_failure: Vec<Rc<dyn Unit>>,
-    conflicts: Vec<Rc<dyn Unit>>,
+    requires: Vec<usize>,
+    wants: Vec<usize>,
+    after: Vec<usize>,
+    before: Vec<usize>,
+    binds_to: Vec<usize>,
+    part_of: Vec<usize>,
+    on_failure: Vec<usize>,
+    conflicts: Vec<usize>,
 }
 
 impl Default for UnitPart {
@@ -199,7 +246,11 @@ impl UnitPart {
     pub fn set_attr(&mut self, attr: &BaseUnitAttr, val: &str) -> Result<(), ParseError> {
         match attr {
             BaseUnitAttr::None => {
-                return Err(ParseError::new(ParseErrorType::ESyntaxError,String::new(),0));
+                return Err(ParseError::new(
+                    ParseErrorType::ESyntaxError,
+                    String::new(),
+                    0,
+                ));
             }
             BaseUnitAttr::Description => self.description = String::from(val),
             BaseUnitAttr::Documentation => {
@@ -281,44 +332,45 @@ impl UnitPart {
         &self.documentation
     }
 
-    pub fn requires(&self) -> &[Rc<dyn Unit>] {
+    pub fn requires(&self) -> &[usize] {
         &self.requires
     }
 
-    pub fn wants(&self) -> &[Rc<dyn Unit>] {
+    pub fn wants(&self) -> &[usize] {
         &self.wants
     }
 
-    pub fn after(&self) -> &[Rc<dyn Unit>] {
+    pub fn after(&self) -> &[usize] {
         &self.after
     }
 
-    pub fn before(&self) -> &[Rc<dyn Unit>] {
+    pub fn before(&self) -> &[usize] {
         &self.before
     }
 
-    pub fn binds_to(&self) -> &[Rc<dyn Unit>] {
+    pub fn binds_to(&self) -> &[usize] {
         &self.binds_to
     }
 
-    pub fn part_of(&self) -> &[Rc<dyn Unit>] {
+    pub fn part_of(&self) -> &[usize] {
         &self.part_of
     }
 
-    pub fn on_failure(&self) -> &[Rc<dyn Unit>] {
+    pub fn on_failure(&self) -> &[usize] {
         &self.on_failure
     }
 
-    pub fn conflicts(&self) -> &[Rc<dyn Unit>] {
+    pub fn conflicts(&self) -> &[usize] {
         &self.conflicts
     }
 }
 
 //对应Unit文件的Install段
+#[derive(Debug, Clone)]
 pub struct InstallPart {
-    wanted_by: Vec<Rc<TargetUnit>>,
-    requires_by: Vec<Rc<TargetUnit>>,
-    also: Vec<Rc<dyn Unit>>,
+    wanted_by: Vec<usize>,
+    requires_by: Vec<usize>,
+    also: Vec<usize>,
     alias: String,
 }
 
@@ -364,21 +416,21 @@ impl InstallPart {
                 self.alias = String::from(val);
             }
             InstallUnitAttr::None => {
-                return Err(ParseError::new(ParseErrorType::EINVAL,String::new(),0));
+                return Err(ParseError::new(ParseErrorType::EINVAL, String::new(), 0));
             }
         }
         return Ok(());
     }
 
-    pub fn wanted_by(&self) -> &[Rc<TargetUnit>] {
+    pub fn wanted_by(&self) -> &[usize] {
         &self.wanted_by
     }
 
-    pub fn requires_by(&self) -> &[Rc<TargetUnit>] {
+    pub fn requires_by(&self) -> &[usize] {
         &self.requires_by
     }
 
-    pub fn also(&self) -> &[Rc<dyn Unit>] {
+    pub fn also(&self) -> &[usize] {
         &self.also
     }
 

+ 113 - 36
src/unit/service/mod.rs

@@ -1,22 +1,42 @@
 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::ExitStatus;
+use crate::executor::service_executor::ServiceExecutor;
+use crate::executor::ExitStatus;
+use crate::manager::UnitManager;
+use crate::parse::graph::Graph;
 use crate::parse::parse_service::ServiceParser;
 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;
 
 #[cfg(target_os = "dragonos")]
 use drstd as std;
+use std::mem::MaybeUninit;
+use std::process::{Child, Command};
 use std::rc::Rc;
 use std::string::String;
+use std::sync::Arc;
 use std::vec::Vec;
-#[derive(Default)]
+#[derive(Clone, Debug)]
 pub struct ServiceUnit {
     unit_base: BaseUnit,
     service_part: ServicePart,
 }
 
-#[derive(Debug)]
+impl Default for ServiceUnit {
+    fn default() -> Self {
+        let mut sp = ServicePart::default();
+        sp.working_directory = String::from("/");
+        Self {
+            unit_base: BaseUnit::default(),
+            service_part: sp,
+        }
+    }
+}
+
+#[derive(Debug, Clone, Copy)]
 pub enum ServiceType {
     Simple,
     Forking,
@@ -32,15 +52,15 @@ impl Default for ServiceType {
     }
 }
 
-#[derive(Debug)]
+#[derive(Debug, Clone, Copy, PartialEq)]
 pub enum RestartOption {
-    AlwaysRestart,
-    OnSuccess,
-    OnFailure,
-    OnAbnormal,
-    OnAbort,
-    OnWatchdog,
-    None,
+    AlwaysRestart, //总是重启
+    OnSuccess,     //在该服务正常退出时
+    OnFailure,     //在该服务启动失败时
+    OnAbnormal,    //在该服务以非0错误码退出时
+    OnAbort,       //在该服务显示退出时(通过DragonReach手动退出)
+    OnWatchdog,    //定时观测进程无响应时(当前未实现)
+    None,          //不重启
 }
 
 impl Default for RestartOption {
@@ -49,7 +69,36 @@ impl Default for RestartOption {
     }
 }
 
-#[derive(Debug)]
+impl RestartOption {
+    pub fn is_restart(&self, exit_status: &ExitStatus) -> bool {
+        if *self == Self::AlwaysRestart {
+            return true;
+        }
+
+        match (*self, *exit_status) {
+            (Self::OnSuccess, ExitStatus::Success) => {
+                return true;
+            }
+            (Self::OnAbnormal, ExitStatus::Abnormal) => {
+                return true;
+            }
+            (Self::OnAbort, ExitStatus::Abort) => {
+                return true;
+            }
+            (Self::OnFailure, ExitStatus::Failure) => {
+                return true;
+            }
+            (Self::OnWatchdog, ExitStatus::Watchdog) => {
+                return true;
+            }
+            _ => {
+                return false;
+            }
+        }
+    }
+}
+
+#[derive(Debug, Clone, Copy)]
 pub enum MountFlag {
     Shared,
     Slave,
@@ -62,13 +111,13 @@ impl Default for MountFlag {
     }
 }
 
-#[derive(Default, Debug)]
+#[derive(Default, Debug, Clone)]
 pub struct ServicePart {
     //生命周期相关
     service_type: ServiceType,
     ///
     remain_after_exit: bool,
-    exec_start: Vec<CmdTask>,
+    exec_start: CmdTask,
     exec_start_pre: Vec<CmdTask>,
     exec_start_pos: Vec<CmdTask>,
     exec_reload: Vec<CmdTask>,
@@ -79,8 +128,7 @@ pub struct ServicePart {
     timeout_start_sec: u64,
     timeout_stop_sec: u64,
     //上下文配置相关
-    environment: Vec<String>,
-    environment_file: String,
+    environment: Vec<(String, String)>,
     nice: i8,
     working_directory: String,
     root_directory: String,
@@ -95,7 +143,7 @@ impl Unit for ServiceUnit {
         self
     }
 
-    fn from_path(path: &str) -> Result<Rc<Self>, ParseError>
+    fn from_path(path: &str) -> Result<usize, ParseError>
     where
         Self: Sized,
     {
@@ -104,9 +152,13 @@ impl Unit for ServiceUnit {
 
     fn set_attr(&mut self, segment: Segment, attr: &str, val: &str) -> Result<(), ParseError> {
         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);
     }
 
@@ -117,6 +169,26 @@ impl Unit for ServiceUnit {
     fn unit_type(&self) -> super::UnitType {
         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(&mut self) -> Result<(), RuntimeError> {
+        self.exec()
+    }
+
+    fn mut_unit_base(&mut self) -> &mut BaseUnit {
+        return &mut self.unit_base;
+    }
+
+    fn after_exit(&mut self, exit_status: ExitStatus) {
+        ServiceExecutor::after_exit(self, exit_status);
+    }
 }
 
 impl ServiceUnit {
@@ -127,8 +199,16 @@ impl ServiceUnit {
     pub fn service_part(&self) -> &ServicePart {
         return &self.service_part;
     }
+
+    fn exec(&mut self) -> Result<(), RuntimeError> {
+        ServiceExecutor::exec(self)
+    }
 }
 
+unsafe impl Sync for ServiceUnit {}
+
+unsafe impl Send for ServiceUnit {}
+
 pub enum ServiceUnitAttr {
     None,
     //Service段
@@ -175,7 +255,7 @@ pub enum ServiceUnitAttr {
 }
 
 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 {
             ServiceUnitAttr::Type => match val {
                 "simple" => self.service_type = ServiceType::Simple,
@@ -185,14 +265,14 @@ impl ServicePart {
                 "notify" => self.service_type = ServiceType::Notify,
                 "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 => {
                 self.remain_after_exit = UnitParseUtil::parse_boolean(val)?
             }
             ServiceUnitAttr::ExecStart => {
-                self.exec_start.extend(UnitParseUtil::parse_cmd_task(val)?);
+                self.exec_start = UnitParseUtil::parse_cmd_task(val)?[0].clone();
             }
             ServiceUnitAttr::ExecStartPre => {
                 self.exec_start_pre
@@ -221,7 +301,7 @@ impl ServicePart {
                 "on-abort" => self.restart = RestartOption::OnAbort,
                 "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 => {
@@ -231,20 +311,21 @@ impl ServicePart {
                 self.timeout_stop_sec = UnitParseUtil::parse_sec(val)?
             }
             ServiceUnitAttr::Environment => {
-                self.environment.push(String::from(val));
+                self.environment.push(UnitParseUtil::parse_env(val)?);
             }
             ServiceUnitAttr::EnvironmentFile => {
                 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
+                    .extend(UnitParseUtil::parse_environment_file(val)?);
             }
             ServiceUnitAttr::Nice => {
                 self.nice = UnitParseUtil::parse_nice(val)?;
             }
             ServiceUnitAttr::WorkingDirectory => {
                 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);
             }
@@ -261,11 +342,11 @@ impl ServicePart {
                 "slave" => self.mount_flags = MountFlag::Slave,
                 "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(());
@@ -280,7 +361,7 @@ impl ServicePart {
         self.remain_after_exit
     }
 
-    pub fn exec_start(&self) -> &Vec<CmdTask> {
+    pub fn exec_start(&self) -> &CmdTask {
         &self.exec_start
     }
 
@@ -321,14 +402,10 @@ impl ServicePart {
     }
 
     // 上下文配置相关
-    pub fn environment(&self) -> &[String] {
+    pub fn environment(&self) -> &[(String, String)] {
         &self.environment
     }
 
-    pub fn environment_file(&self) -> &str {
-        &self.environment_file
-    }
-
     pub fn nice(&self) -> i8 {
         self.nice
     }

+ 32 - 23
src/unit/target/mod.rs

@@ -1,40 +1,29 @@
 use super::{BaseUnit, Unit};
-use crate::error::ParseError;
-use crate::parse::Segment;
+use crate::error::parse_error::ParseError;
 use crate::parse::parse_target::TargetParser;
-use core::ops::Deref;
+use crate::parse::Segment;
 use cfg_if::cfg_if;
+use core::ops::Deref;
+use core::result::Result::{self, Err, Ok};
+#[cfg(target_os = "dragonos")]
+use drstd as std;
+use std::marker::{Send, Sized, Sync};
+use std::rc::Rc;
+use std::sync::Arc;
+use std::vec::Vec;
 
-cfg_if!{
-    if #[cfg(target_os = "dragonos")]{
-        use drstd as std;
-        use std::rc::Rc;
-        use std::vec::Vec;
-    }else{
-        use std::rc::Rc;
-        use std::vec::Vec;
-    }
-}
-
-#[derive(Default)]
+#[derive(Debug, Clone, Default)]
 pub struct TargetUnit {
     unit_base: BaseUnit,
     //targets: Vec<Rc<dyn Unit>>,
 }
 
-impl Deref for TargetUnit {
-    type Target = TargetUnit;
-    fn deref(&self) -> &Self::Target {
-        &self
-    }
-}
-
 impl Unit for TargetUnit {
     fn as_any(&self) -> &dyn core::any::Any {
         self
     }
 
-    fn from_path(path: &str) -> Result<Rc<Self>, ParseError>
+    fn from_path(path: &str) -> Result<usize, ParseError>
     where
         Self: Sized,
     {
@@ -52,4 +41,24 @@ impl Unit for TargetUnit {
     fn unit_type(&self) -> super::UnitType {
         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(&mut self) -> Result<(), crate::error::runtime_error::RuntimeError> {
+        Ok(())
+    }
+
+    fn mut_unit_base(&mut self) -> &mut BaseUnit {
+        return &mut self.unit_base;
+    }
 }
+
+unsafe impl Sync for TargetUnit {}
+
+unsafe impl Send for TargetUnit {}

+ 4 - 2
target.json

@@ -5,6 +5,9 @@
     "os": "dragonos",
     "target-endian": "little",
     "target-pointer-width": "64",
+    "target-family": [
+        "unix"
+    ],
     "target-c-int-width": "32",
     "data-layout": "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128",
     "disable-redzone": true,
@@ -27,6 +30,5 @@
     "static-position-independent-executables": true,
     "supported-sanitizers": [
         "kcfi"
-    ],
-    "target-pointer-width": "64"
+    ]
 }