xref: /DragonReach/src/main.rs (revision 2069cc0dc0984a2981454b00316ba607f88ac512)
121fc724cSGnoCiYeH mod contants;
24e851715SGnoCiYeH mod error;
317ae4661SGnoCiYeH mod executor;
417ae4661SGnoCiYeH mod manager;
54e851715SGnoCiYeH mod parse;
6909e4d10SGnoCiYeH mod systemctl;
74e851715SGnoCiYeH mod task;
8b40b6b4dSGnoCiYeH mod time;
921fc724cSGnoCiYeH mod unit;
1021fc724cSGnoCiYeH 
1117ae4661SGnoCiYeH use error::ErrorFormat;
12dfd3fd98S裕依2439 use manager::{timer_manager::TimerManager, Manager};
13909e4d10SGnoCiYeH use parse::UnitParser;
14909e4d10SGnoCiYeH use systemctl::listener::Systemctl;
154e851715SGnoCiYeH 
16*2069cc0dSBrahmaMantra use crate::executor::Executor;
17*2069cc0dSBrahmaMantra 
184e851715SGnoCiYeH pub struct FileDescriptor(usize);
194e851715SGnoCiYeH 
2017ae4661SGnoCiYeH const DRAGON_REACH_UNIT_DIR: &'static str = "/etc/reach/system/";
main()214e851715SGnoCiYeH fn main() {
22909e4d10SGnoCiYeH     // 初始化
23909e4d10SGnoCiYeH     Systemctl::init();
2417ae4661SGnoCiYeH 
2517ae4661SGnoCiYeH     let mut units_file_name = Vec::new();
2617ae4661SGnoCiYeH     //读取目录里面的unit文件
27909e4d10SGnoCiYeH     if let Ok(entries) = std::fs::read_dir(DRAGON_REACH_UNIT_DIR) {
2817ae4661SGnoCiYeH         for entry in entries {
2917ae4661SGnoCiYeH             if let Ok(entry) = entry {
3056797478SGnoCiYeH                 if let Ok(file_type) = entry.file_type() {
3156797478SGnoCiYeH                     if file_type.is_file() {
32dfd3fd98S裕依2439                         let filename = entry.file_name().to_str().unwrap().to_string();
33dfd3fd98S裕依2439                         units_file_name.push(filename);
3417ae4661SGnoCiYeH                     }
3517ae4661SGnoCiYeH                 }
3617ae4661SGnoCiYeH             }
3756797478SGnoCiYeH         }
3856797478SGnoCiYeH     }
3917ae4661SGnoCiYeH 
4017ae4661SGnoCiYeH     //启动服务
4117ae4661SGnoCiYeH     for path in units_file_name {
4217ae4661SGnoCiYeH         let id = match UnitParser::from_path(&path) {
4317ae4661SGnoCiYeH             Ok(id) => id,
44f206f17aSGnoCiYeH             Err(e) => {
4517ae4661SGnoCiYeH                 eprintln!("Err:{}", e.error_format());
4617ae4661SGnoCiYeH                 0
474e851715SGnoCiYeH             }
48f206f17aSGnoCiYeH         };
4917ae4661SGnoCiYeH         if id != 0 {
50b40b6b4dSGnoCiYeH             if let Err(e) = Executor::exec(id) {
5117ae4661SGnoCiYeH                 eprintln!("Err:{}", e.error_format());
5217ae4661SGnoCiYeH             }
5317ae4661SGnoCiYeH         }
54*2069cc0dSBrahmaMantra         println!("Parse {} success!", path);
5517ae4661SGnoCiYeH     }
5617ae4661SGnoCiYeH 
5717ae4661SGnoCiYeH     // 启动完服务后进入主循环
5817ae4661SGnoCiYeH     loop {
5917ae4661SGnoCiYeH         // 检查各服务运行状态
6017ae4661SGnoCiYeH         Manager::check_running_status();
61b40b6b4dSGnoCiYeH         // 检查cmd进程状态
62b40b6b4dSGnoCiYeH         Manager::check_cmd_proc();
63b40b6b4dSGnoCiYeH         // 检查计时器任务
64b40b6b4dSGnoCiYeH         TimerManager::check_timer();
65909e4d10SGnoCiYeH         // 监听systemctl
66909e4d10SGnoCiYeH         Systemctl::ctl_listen();
67e5a8055aSGnoCiYeH     }
6821fc724cSGnoCiYeH }
69