Browse Source

添加systemctl查看系统服务状态

GnoCiYeH 1 year ago
parent
commit
893389d46c

+ 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", rev = "876bfc9744" }
+drstd = {git = "https://github.com/DragonOS-Community/drstd.git", revision = "8b1e314e01"}
+dragonos-dsc = { git = "https://git.mirrors.dragonos.org/DragonOS-Community/dragonos-dsc.git", rev = "aa61cb0109" }
 
 lazy_static = { version = "1.4.0", default-features = false, features = ["spin_no_std"] }
 

+ 2 - 2
parse_test/shell.service

@@ -1,8 +1,8 @@
 [Unit]
 Description=Shell
+After=http_server.service
 
 [Service]
 Type=simple
 ExecStart=/bin/shell.elf
-#Restart=always
-#RestartSec=500000
+Restart=always

+ 5 - 5
src/executor/mod.rs

@@ -53,11 +53,11 @@ impl Executor {
         match Self::exec_(unit_id) {
             Ok(_) => {
                 UnitManager::get_unit_with_id(&unit_id)
-                .unwrap()
-                .lock()
-                .unwrap()
-                .unit_base_mut()
-                .set_state(UnitState::Active);
+                    .unwrap()
+                    .lock()
+                    .unwrap()
+                    .unit_base_mut()
+                    .set_state(UnitState::Active);
                 Ok(())
             },
             Err(e) => {

+ 1 - 1
src/executor/service_executor/mod.rs

@@ -175,7 +175,7 @@ impl ServiceExecutor {
 
         //判断是否需要restart,需要则再次启动服务
         if service.service_part().restart().is_restart(&exit_status) {
-            Self::restart(service);
+            let _ = Self::restart(service);
             return;
         }
 

+ 7 - 84
src/main.rs

@@ -3,19 +3,9 @@
 #![feature(slice_pattern)]
 #![feature(fn_traits)]
 
-use cfg_if::cfg_if;
-
 #[cfg(target_os = "dragonos")]
 extern crate drstd as std;
 
-cfg_if! {
-    if #[cfg(target_os = "dragonos")] {
-        extern crate drstd as std;
-        #[macro_use]
-        extern crate dsc;
-    }
-}
-
 extern crate hashbrown;
 
 mod contants;
@@ -28,38 +18,30 @@ mod task;
 mod time;
 mod unit;
 
-use std::fs;
-use std::{eprint, eprintln, print, println};
+use std::{eprint, eprintln};
 
 use std::string::ToString;
 use std::vec::Vec;
 
 use error::ErrorFormat;
+use executor::Executor;
+use manager::timer_manager::TimerManager;
+use manager::Manager;
+use parse::UnitParser;
+use systemctl::listener::Systemctl;
 
 pub struct FileDescriptor(usize);
 
 const DRAGON_REACH_UNIT_DIR: &'static str = "/etc/reach/system/";
 
-extern "C" fn thread_function(_: *mut std::ffi::c_void) -> *mut std::ffi::c_void {
-    println!("Child thread");
-    // loop{}
-    core::ptr::null_mut() as *mut std::ffi::c_void
-}
-
-#[cfg(target_os = "dragonos")]
 #[no_mangle]
 fn main() {
-    use manager::timer_manager::TimerManager;
-    use parse::UnitParser;
-
-    use crate::{executor::Executor, manager::Manager, systemctl::listener::Systemctl};
-
     // 初始化
     Systemctl::init();
 
     let mut units_file_name = Vec::new();
     //读取目录里面的unit文件
-    if let Ok(entries) = fs::read_dir(DRAGON_REACH_UNIT_DIR) {
+    if let Ok(entries) = std::fs::read_dir(DRAGON_REACH_UNIT_DIR) {
         for entry in entries {
             if let Ok(entry) = entry {
                 if let Ok(file_type) = entry.file_type() {
@@ -102,62 +84,3 @@ fn main() {
         Systemctl::ctl_listen();
     }
 }
-
-#[cfg(not(target_os = "dragonos"))]
-fn main() {
-    use parse::UnitParser;
-
-    use crate::{
-        executor::Executor,
-        manager::{timer_manager::TimerManager, Manager, UnitManager},
-    };
-
-    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());
-
-    // 完善unit之间依赖关系
-    UnitManager::init_units_dependencies();
-
-    //启动服务
-    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(id) {
-                eprintln!("Err:{}", e.error_format());
-            }
-        }
-    }
-
-    // 启动完服务后进入主循环
-    loop {
-        // 检查各服务运行状态
-        Manager::check_running_status();
-
-        Manager::check_cmd_proc();
-
-        TimerManager::check_timer();
-    }
-}

+ 0 - 1
src/manager/ctl_manager/mod.rs

@@ -1,6 +1,5 @@
 use lazy_static::lazy_static;
 use std::fs::File;
-use std::io::Write;
 use std::os::fd::FromRawFd;
 use std::string::String;
 use std::string::ToString;

+ 6 - 5
src/manager/mod.rs

@@ -1,8 +1,8 @@
-use std::{eprint, eprintln, print, println, vec::Vec};
+use std::{eprint, eprintln, vec::Vec};
 
+pub mod ctl_manager;
 pub mod timer_manager;
 pub mod unit_manager;
-pub mod ctl_manager;
 
 pub use unit_manager::*;
 
@@ -24,9 +24,10 @@ impl Manager {
                 //进程正常退出
                 Ok(Some(status)) => {
                     //TODO:交付给相应类型的Unit类型去执行退出后的逻辑
-                    println!("Service exited success");
-
-                    exited_unit.push((*unit.0, ExitStatus::from_exit_code(status.code().unwrap())));
+                    exited_unit.push((
+                        *unit.0,
+                        ExitStatus::from_exit_code(status.code().unwrap_or(0)),
+                    ));
                 }
                 //进程错误退出(或启动失败)
                 Err(e) => {

+ 1 - 2
src/parse/mod.rs

@@ -1,6 +1,6 @@
 use crate::error::parse_error::ParseErrorType;
 use crate::manager::UnitManager;
-use crate::unit::timer::{TimerPart, TimerUnitAttr};
+use crate::unit::timer::TimerUnitAttr;
 use crate::unit::{BaseUnit, Unit};
 use crate::DRAGON_REACH_UNIT_DIR;
 use crate::{
@@ -148,7 +148,6 @@ lazy_static! {
         table.insert("ns", 1);
         table
     };
-
     pub static ref TIMER_UNIT_ATTR_TABLE: HashMap<&'static str, TimerUnitAttr> = {
         let mut map = HashMap::new();
         map.insert("OnActiveSec", TimerUnitAttr::OnActiveSec);

+ 1 - 1
src/parse/parse_service/mod.rs

@@ -15,7 +15,7 @@ impl ServiceParser {
     ///
     /// @param path 需解析的文件路径
     ///
-    /// @return 成功则返回Ok(Rc<ServiceUnit>),否则返回Err
+    /// @return 成功则返回Ok(id),否则返回Err
     pub fn parse(path: &str) -> Result<usize, ParseError> {
         //预先检查是否存在循环依赖
         let mut graph = Graph::construct_graph(path.to_string())?;

+ 1 - 1
src/parse/parse_util/mod.rs

@@ -693,7 +693,7 @@ impl UnitParseUtil {
 
     ///// ## 通过文件名解析该Unit的类型
     pub fn parse_type(path: &str) -> UnitType {
-        let mut ret: &str;
+        let ret: &str;
         if let Some(index) = path.rfind('.') {
             ret = &path[index + 1..];
         } else {

+ 2 - 0
src/systemctl/ctl_parser/mod.rs

@@ -10,6 +10,7 @@ use std::vec::Vec;
 
 use super::listener::Command;
 
+#[allow(dead_code)]
 #[derive(Debug, Clone)]
 pub enum CommandOperation {
     ListUnits,
@@ -76,6 +77,7 @@ pub enum CommandOperation {
     None,
 }
 
+#[allow(dead_code)]
 #[derive(Debug, Clone)]
 pub enum Pattern {
     Help,

+ 3 - 4
src/systemctl/listener/mod.rs

@@ -1,7 +1,6 @@
 use lazy_static::lazy_static;
 use std::fs::{self, File};
-use std::io::{self, BufRead, BufReader, Read};
-use std::libc::c_str::CString;
+use std::io::Read;
 use std::os::fd::FromRawFd;
 use std::string::String;
 use std::sync::{Arc, Mutex};
@@ -10,7 +9,6 @@ use std::{eprint, eprintln, libc};
 
 use crate::error::ErrorFormat;
 use crate::manager::ctl_manager::CtlManager;
-use crate::manager::UnitManager;
 
 use super::ctl_parser::{CommandOperation, CtlParser, Pattern};
 use super::{ctl_path, DRAGON_REACH_CTL_PIPE};
@@ -29,6 +27,7 @@ pub struct Command {
 }
 
 impl Command {
+    #[allow(dead_code)]
     fn new(op: CommandOperation, patterns: Vec<Pattern>, args: Option<Vec<String>>) -> Self {
         Command {
             operation: op,
@@ -72,7 +71,7 @@ impl Systemctl {
             }
             match CtlParser::parse_ctl(&s) {
                 Ok(cmd) => {
-                    CtlManager::exec_ctl(cmd);
+                    let _ = CtlManager::exec_ctl(cmd);
                 }
                 Err(err) => {
                     eprintln!("parse tcl command error: {}", err.error_format());

+ 1 - 1
src/systemctl/mod.rs

@@ -1,4 +1,4 @@
-use std::libc::c_str::CString;
+use std::ffi::CString;
 
 pub mod ctl_parser;
 pub mod listener;

+ 3 - 2
src/unit/timer/mod.rs

@@ -1,11 +1,12 @@
 use super::BaseUnit;
 
-
+#[allow(dead_code)]
 pub struct TimerUnit {
     unit_base: BaseUnit,
     timer_part: TimerPart,
 }
 
+#[allow(dead_code)]
 pub struct TimerPart {
     on_active_sec: u64,
     on_boot_sec: u64,
@@ -38,4 +39,4 @@ pub enum TimerUnitAttr {
     Persistent,
     WakeSystem,
     RemainAfterElapse,
-}
+}

+ 1 - 2
systemctl/Cargo.toml

@@ -13,5 +13,4 @@ path = "src/main.rs"
 cfg-if = { version = "1.0"}
 
 [target.'cfg(target_os = "dragonos")'.dependencies]
-#drstd = {git = "https://github.com/DragonOS-Community/drstd.git", revision = "af10011"}
-drstd = { path = "/home/heyicong/drstd" }
+drstd = {git = "https://github.com/DragonOS-Community/drstd.git", revision = "8b1e314e01"}

+ 3 - 19
systemctl/src/main.rs

@@ -8,18 +8,14 @@ use std::fs::File;
 use std::libc::c_str::CString;
 use std::os::fd::FromRawFd;
 use std::{env, libc};
-use std::{print, println, string::String, vec::Vec};
+use std::{string::String, vec::Vec};
 
 const REACH_PIPE_PATH: &str = "/etc/reach/ipc/ctl";
 
 #[cfg(target_os = "dragonos")]
 #[no_mangle]
 fn main() {
-    use std::{
-        eprint, eprintln, format,
-        io::{Read, Write},
-        libc,
-    };
+    use std::{eprint, eprintln, format, io::Write};
 
     let mut args: Vec<String> = env::args().collect();
 
@@ -39,19 +35,6 @@ fn main() {
     if let Err(err) = get_writer().write_all(msg.as_bytes()) {
         eprintln!("write error {}", err);
     }
-    // match get_reader().read_to_string(&mut buf) {
-    //     Ok(size) => {
-    //         println!("ctl read size: {}",size);
-    //     },
-    //     Err(err) => {
-    //         eprintln!("read error {}", err);
-    //     },
-    // }
-    // if let Err(err) = get_reader().read_to_string(&mut buf) {
-    //     eprintln!("read error {}", err);
-    // }
-
-    // println!("{}", buf);
 }
 
 #[cfg(not(target_os = "dragonos"))]
@@ -63,6 +46,7 @@ fn get_writer() -> File {
     unsafe { File::from_raw_fd(fd) }
 }
 
+#[allow(dead_code)]
 fn get_reader() -> File {
     let fd = unsafe { libc::open(ctl_path().as_ptr(), libc::O_RDONLY) };