xref: /DragonReach/src/main.rs (revision ea330323f68db57353801959d67cbd8e0031aa18)
11dcc785bSGnoCiYeH #![no_std]
21dcc785bSGnoCiYeH #![no_main]
3e5a8055aSGnoCiYeH #![feature(slice_pattern)]
4b40b6b4dSGnoCiYeH #![feature(fn_traits)]
5e5a8055aSGnoCiYeH 
6*ea330323SLoGin #[cfg(target_os = "dragonos")]
7*ea330323SLoGin 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;
164e851715SGnoCiYeH mod task;
17b40b6b4dSGnoCiYeH mod time;
1821fc724cSGnoCiYeH mod unit;
1921fc724cSGnoCiYeH 
2017ae4661SGnoCiYeH use std::fs;
21b40b6b4dSGnoCiYeH use std::{eprint, eprintln, print, println};
2221fc724cSGnoCiYeH 
23b40b6b4dSGnoCiYeH use std::string::ToString;
24b40b6b4dSGnoCiYeH use std::vec::Vec;
25b40b6b4dSGnoCiYeH 
2617ae4661SGnoCiYeH use error::ErrorFormat;
274e851715SGnoCiYeH 
284e851715SGnoCiYeH pub struct FileDescriptor(usize);
294e851715SGnoCiYeH 
3017ae4661SGnoCiYeH const DRAGON_REACH_UNIT_DIR: &'static str = "/etc/reach/system/";
3117ae4661SGnoCiYeH 
32f206f17aSGnoCiYeH #[cfg(target_os = "dragonos")]
33f206f17aSGnoCiYeH #[no_mangle]
344e851715SGnoCiYeH fn main() {
35b40b6b4dSGnoCiYeH     use manager::timer_manager::TimerManager;
3617ae4661SGnoCiYeH     use parse::UnitParser;
3717ae4661SGnoCiYeH 
38*ea330323SLoGin     use crate::{executor::Executor, manager::Manager};
3917ae4661SGnoCiYeH 
4017ae4661SGnoCiYeH     let mut units_file_name = Vec::new();
4117ae4661SGnoCiYeH     //读取目录里面的unit文件
4217ae4661SGnoCiYeH     if let Ok(entries) = fs::read_dir(DRAGON_REACH_UNIT_DIR) {
4317ae4661SGnoCiYeH         for entry in entries {
4417ae4661SGnoCiYeH             if let Ok(entry) = entry {
4556797478SGnoCiYeH                 if let Ok(file_type) = entry.file_type() {
4656797478SGnoCiYeH                     if file_type.is_file() {
4717ae4661SGnoCiYeH                         let filename = entry.file_name();
4817ae4661SGnoCiYeH                         let filename = filename.to_str().unwrap();
4917ae4661SGnoCiYeH                         units_file_name.push(filename.to_string());
5017ae4661SGnoCiYeH                     }
5117ae4661SGnoCiYeH                 }
5217ae4661SGnoCiYeH             }
5356797478SGnoCiYeH         }
5456797478SGnoCiYeH     }
5517ae4661SGnoCiYeH 
5617ae4661SGnoCiYeH     //启动服务
5717ae4661SGnoCiYeH     for path in units_file_name {
5817ae4661SGnoCiYeH         let id = match UnitParser::from_path(&path) {
5917ae4661SGnoCiYeH             Ok(id) => id,
60f206f17aSGnoCiYeH             Err(e) => {
6117ae4661SGnoCiYeH                 eprintln!("Err:{}", e.error_format());
6217ae4661SGnoCiYeH                 0
634e851715SGnoCiYeH             }
64f206f17aSGnoCiYeH         };
6517ae4661SGnoCiYeH 
6617ae4661SGnoCiYeH         if id != 0 {
67b40b6b4dSGnoCiYeH             if let Err(e) = Executor::exec(id) {
6817ae4661SGnoCiYeH                 eprintln!("Err:{}", e.error_format());
6917ae4661SGnoCiYeH             } else {
7017ae4661SGnoCiYeH                 println!("Service {} startup success...", id);
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();
8317ae4661SGnoCiYeH     }
84f206f17aSGnoCiYeH }
85f206f17aSGnoCiYeH 
86f206f17aSGnoCiYeH #[cfg(not(target_os = "dragonos"))]
87f206f17aSGnoCiYeH fn main() {
8817ae4661SGnoCiYeH     use parse::UnitParser;
89e5a8055aSGnoCiYeH 
9017ae4661SGnoCiYeH     use crate::{
9117ae4661SGnoCiYeH         executor::Executor,
92b40b6b4dSGnoCiYeH         manager::{timer_manager::TimerManager, Manager, UnitManager},
9317ae4661SGnoCiYeH     };
94f206f17aSGnoCiYeH 
9517ae4661SGnoCiYeH     let mut units_file_name = Vec::new();
9656797478SGnoCiYeH     //读取目录里面的unit文件
9756797478SGnoCiYeH     if let Ok(entries) = fs::read_dir("/bin") {
9856797478SGnoCiYeH         for entry in entries {
9956797478SGnoCiYeH             if let Ok(entry) = entry {
10056797478SGnoCiYeH                 if let Ok(file_type) = entry.file_type() {
10156797478SGnoCiYeH                     if file_type.is_file() {
10256797478SGnoCiYeH                         let filename = entry.file_name();
103b40b6b4dSGnoCiYeH                         let _filename = filename.to_str().unwrap();
10417ae4661SGnoCiYeH                         //units_file_name.push(filename.to_string());
10556797478SGnoCiYeH                     }
10656797478SGnoCiYeH                 }
10756797478SGnoCiYeH             }
10856797478SGnoCiYeH         }
10956797478SGnoCiYeH     }
110e5a8055aSGnoCiYeH 
11156797478SGnoCiYeH     units_file_name.push("/home/heyicong/DragonReach/parse_test/test.service".to_string());
11217ae4661SGnoCiYeH 
113b40b6b4dSGnoCiYeH     // 完善unit之间依赖关系
114b40b6b4dSGnoCiYeH     UnitManager::init_units_dependencies();
115b40b6b4dSGnoCiYeH 
11617ae4661SGnoCiYeH     //启动服务
11717ae4661SGnoCiYeH     for path in units_file_name {
11817ae4661SGnoCiYeH         let id = match UnitParser::from_path(&path) {
11917ae4661SGnoCiYeH             Ok(id) => id,
120f206f17aSGnoCiYeH             Err(e) => {
12117ae4661SGnoCiYeH                 eprintln!("Err:{}", e.error_format());
12217ae4661SGnoCiYeH                 0
123f206f17aSGnoCiYeH             }
124f206f17aSGnoCiYeH         };
125f206f17aSGnoCiYeH 
12617ae4661SGnoCiYeH         if id != 0 {
127b40b6b4dSGnoCiYeH             let _unit = UnitManager::get_unit_with_id(&id).unwrap();
128b40b6b4dSGnoCiYeH             if let Err(e) = Executor::exec(id) {
12917ae4661SGnoCiYeH                 eprintln!("Err:{}", e.error_format());
13017ae4661SGnoCiYeH             }
13117ae4661SGnoCiYeH         }
13217ae4661SGnoCiYeH     }
13317ae4661SGnoCiYeH 
13417ae4661SGnoCiYeH     // 启动完服务后进入主循环
13517ae4661SGnoCiYeH     loop {
13617ae4661SGnoCiYeH         // 检查各服务运行状态
13717ae4661SGnoCiYeH         Manager::check_running_status();
1381dcc785bSGnoCiYeH 
139b40b6b4dSGnoCiYeH         Manager::check_cmd_proc();
140b40b6b4dSGnoCiYeH 
141b40b6b4dSGnoCiYeH         TimerManager::check_timer();
142e5a8055aSGnoCiYeH     }
14321fc724cSGnoCiYeH }
144