xref: /DragonReach/src/main.rs (revision 909e4d102ffeb8646e7346c10b007cb0861226a4)
11dcc785bSGnoCiYeH #![no_std]
21dcc785bSGnoCiYeH #![no_main]
3e5a8055aSGnoCiYeH #![feature(slice_pattern)]
4b40b6b4dSGnoCiYeH #![feature(fn_traits)]
5e5a8055aSGnoCiYeH 
6ea330323SLoGin #[cfg(target_os = "dragonos")]
7ea330323SLoGin extern crate drstd as std;
8f206f17aSGnoCiYeH 
9f206f17aSGnoCiYeH extern crate hashbrown;
104e851715SGnoCiYeH 
1121fc724cSGnoCiYeH mod contants;
124e851715SGnoCiYeH mod error;
1317ae4661SGnoCiYeH mod executor;
1417ae4661SGnoCiYeH mod manager;
154e851715SGnoCiYeH mod parse;
16*909e4d10SGnoCiYeH mod systemctl;
174e851715SGnoCiYeH mod task;
18b40b6b4dSGnoCiYeH mod time;
1921fc724cSGnoCiYeH mod unit;
2021fc724cSGnoCiYeH 
21*909e4d10SGnoCiYeH use std::{eprint, eprintln};
2221fc724cSGnoCiYeH 
23b40b6b4dSGnoCiYeH use std::string::ToString;
24b40b6b4dSGnoCiYeH use std::vec::Vec;
25b40b6b4dSGnoCiYeH 
2617ae4661SGnoCiYeH use error::ErrorFormat;
27*909e4d10SGnoCiYeH use executor::Executor;
28*909e4d10SGnoCiYeH use manager::timer_manager::TimerManager;
29*909e4d10SGnoCiYeH use manager::Manager;
30*909e4d10SGnoCiYeH use parse::UnitParser;
31*909e4d10SGnoCiYeH use systemctl::listener::Systemctl;
324e851715SGnoCiYeH 
334e851715SGnoCiYeH pub struct FileDescriptor(usize);
344e851715SGnoCiYeH 
3517ae4661SGnoCiYeH const DRAGON_REACH_UNIT_DIR: &'static str = "/etc/reach/system/";
3617ae4661SGnoCiYeH 
37f206f17aSGnoCiYeH #[no_mangle]
384e851715SGnoCiYeH fn main() {
39*909e4d10SGnoCiYeH     // 初始化
40*909e4d10SGnoCiYeH     Systemctl::init();
4117ae4661SGnoCiYeH 
4217ae4661SGnoCiYeH     let mut units_file_name = Vec::new();
4317ae4661SGnoCiYeH     //读取目录里面的unit文件
44*909e4d10SGnoCiYeH     if let Ok(entries) = std::fs::read_dir(DRAGON_REACH_UNIT_DIR) {
4517ae4661SGnoCiYeH         for entry in entries {
4617ae4661SGnoCiYeH             if let Ok(entry) = entry {
4756797478SGnoCiYeH                 if let Ok(file_type) = entry.file_type() {
4856797478SGnoCiYeH                     if file_type.is_file() {
4917ae4661SGnoCiYeH                         let filename = entry.file_name();
5017ae4661SGnoCiYeH                         let filename = filename.to_str().unwrap();
5117ae4661SGnoCiYeH                         units_file_name.push(filename.to_string());
5217ae4661SGnoCiYeH                     }
5317ae4661SGnoCiYeH                 }
5417ae4661SGnoCiYeH             }
5556797478SGnoCiYeH         }
5656797478SGnoCiYeH     }
5717ae4661SGnoCiYeH 
5817ae4661SGnoCiYeH     //启动服务
5917ae4661SGnoCiYeH     for path in units_file_name {
6017ae4661SGnoCiYeH         let id = match UnitParser::from_path(&path) {
6117ae4661SGnoCiYeH             Ok(id) => id,
62f206f17aSGnoCiYeH             Err(e) => {
6317ae4661SGnoCiYeH                 eprintln!("Err:{}", e.error_format());
6417ae4661SGnoCiYeH                 0
654e851715SGnoCiYeH             }
66f206f17aSGnoCiYeH         };
6717ae4661SGnoCiYeH 
6817ae4661SGnoCiYeH         if id != 0 {
69b40b6b4dSGnoCiYeH             if let Err(e) = Executor::exec(id) {
7017ae4661SGnoCiYeH                 eprintln!("Err:{}", e.error_format());
7117ae4661SGnoCiYeH             }
7217ae4661SGnoCiYeH         }
7317ae4661SGnoCiYeH     }
7417ae4661SGnoCiYeH 
7517ae4661SGnoCiYeH     // 启动完服务后进入主循环
7617ae4661SGnoCiYeH     loop {
7717ae4661SGnoCiYeH         // 检查各服务运行状态
7817ae4661SGnoCiYeH         Manager::check_running_status();
79b40b6b4dSGnoCiYeH         // 检查cmd进程状态
80b40b6b4dSGnoCiYeH         Manager::check_cmd_proc();
81b40b6b4dSGnoCiYeH         // 检查计时器任务
82b40b6b4dSGnoCiYeH         TimerManager::check_timer();
83*909e4d10SGnoCiYeH         // 监听systemctl
84*909e4d10SGnoCiYeH         Systemctl::ctl_listen();
85e5a8055aSGnoCiYeH     }
8621fc724cSGnoCiYeH }
87