xref: /DragonReach/src/main.rs (revision b40b6b4d2f72c30a382f9f18740fa73d7fc90721)
11dcc785bSGnoCiYeH #![no_std]
21dcc785bSGnoCiYeH #![no_main]
3e5a8055aSGnoCiYeH #![feature(slice_pattern)]
4*b40b6b4dSGnoCiYeH #![feature(fn_traits)]
5e5a8055aSGnoCiYeH 
6f206f17aSGnoCiYeH use cfg_if::cfg_if;
721fc724cSGnoCiYeH 
8f206f17aSGnoCiYeH cfg_if! {
9f206f17aSGnoCiYeH     if #[cfg(target_os = "dragonos")]{
10f206f17aSGnoCiYeH         extern crate drstd;
11f206f17aSGnoCiYeH         use drstd as std;
12f206f17aSGnoCiYeH     }
13f206f17aSGnoCiYeH }
14f206f17aSGnoCiYeH 
15f206f17aSGnoCiYeH extern crate hashbrown;
164e851715SGnoCiYeH 
1721fc724cSGnoCiYeH mod contants;
184e851715SGnoCiYeH mod error;
1917ae4661SGnoCiYeH mod executor;
2017ae4661SGnoCiYeH mod manager;
214e851715SGnoCiYeH mod parse;
224e851715SGnoCiYeH mod task;
23*b40b6b4dSGnoCiYeH mod time;
2421fc724cSGnoCiYeH mod unit;
2521fc724cSGnoCiYeH 
2617ae4661SGnoCiYeH use std::fs;
27*b40b6b4dSGnoCiYeH use std::{eprint, eprintln, print, println};
2821fc724cSGnoCiYeH 
29*b40b6b4dSGnoCiYeH use std::string::ToString;
30*b40b6b4dSGnoCiYeH use std::vec::Vec;
31*b40b6b4dSGnoCiYeH 
3217ae4661SGnoCiYeH use error::ErrorFormat;
334e851715SGnoCiYeH 
344e851715SGnoCiYeH pub struct FileDescriptor(usize);
354e851715SGnoCiYeH 
3617ae4661SGnoCiYeH const DRAGON_REACH_UNIT_DIR: &'static str = "/etc/reach/system/";
3717ae4661SGnoCiYeH 
38f206f17aSGnoCiYeH #[cfg(target_os = "dragonos")]
39f206f17aSGnoCiYeH #[no_mangle]
404e851715SGnoCiYeH fn main() {
41*b40b6b4dSGnoCiYeH     use manager::timer_manager::TimerManager;
4217ae4661SGnoCiYeH     use parse::UnitParser;
4317ae4661SGnoCiYeH 
4417ae4661SGnoCiYeH     use crate::{
4517ae4661SGnoCiYeH         executor::Executor,
4617ae4661SGnoCiYeH         manager::{Manager, UnitManager},
4717ae4661SGnoCiYeH         parse::parse_util::UnitParseUtil,
4817ae4661SGnoCiYeH     };
4917ae4661SGnoCiYeH 
5017ae4661SGnoCiYeH     let mut units_file_name = Vec::new();
5117ae4661SGnoCiYeH     //读取目录里面的unit文件
5217ae4661SGnoCiYeH     if let Ok(entries) = fs::read_dir(DRAGON_REACH_UNIT_DIR) {
5317ae4661SGnoCiYeH         for entry in entries {
5417ae4661SGnoCiYeH             if let Ok(entry) = entry {
5556797478SGnoCiYeH                 if let Ok(file_type) = entry.file_type() {
5656797478SGnoCiYeH                     if file_type.is_file() {
5717ae4661SGnoCiYeH                         let filename = entry.file_name();
5817ae4661SGnoCiYeH                         let filename = filename.to_str().unwrap();
5917ae4661SGnoCiYeH                         units_file_name.push(filename.to_string());
6017ae4661SGnoCiYeH                     }
6117ae4661SGnoCiYeH                 }
6217ae4661SGnoCiYeH             }
6356797478SGnoCiYeH         }
6456797478SGnoCiYeH     }
6517ae4661SGnoCiYeH 
6617ae4661SGnoCiYeH     //启动服务
6717ae4661SGnoCiYeH     for path in units_file_name {
6817ae4661SGnoCiYeH         let id = match UnitParser::from_path(&path) {
6917ae4661SGnoCiYeH             Ok(id) => id,
70f206f17aSGnoCiYeH             Err(e) => {
7117ae4661SGnoCiYeH                 eprintln!("Err:{}", e.error_format());
7217ae4661SGnoCiYeH                 0
734e851715SGnoCiYeH             }
74f206f17aSGnoCiYeH         };
7517ae4661SGnoCiYeH 
7617ae4661SGnoCiYeH         if id != 0 {
77*b40b6b4dSGnoCiYeH             if let Err(e) = Executor::exec(id) {
7817ae4661SGnoCiYeH                 eprintln!("Err:{}", e.error_format());
7917ae4661SGnoCiYeH             } else {
8017ae4661SGnoCiYeH                 println!("Service {} startup success...", id);
8117ae4661SGnoCiYeH             }
8217ae4661SGnoCiYeH         }
8317ae4661SGnoCiYeH     }
8417ae4661SGnoCiYeH 
8517ae4661SGnoCiYeH     // 启动完服务后进入主循环
8617ae4661SGnoCiYeH     loop {
8717ae4661SGnoCiYeH         // 检查各服务运行状态
8817ae4661SGnoCiYeH         Manager::check_running_status();
89*b40b6b4dSGnoCiYeH         // 检查cmd进程状态
90*b40b6b4dSGnoCiYeH         Manager::check_cmd_proc();
91*b40b6b4dSGnoCiYeH         // 检查计时器任务
92*b40b6b4dSGnoCiYeH         TimerManager::check_timer();
9317ae4661SGnoCiYeH     }
94f206f17aSGnoCiYeH }
95f206f17aSGnoCiYeH 
96f206f17aSGnoCiYeH #[cfg(not(target_os = "dragonos"))]
97f206f17aSGnoCiYeH fn main() {
9817ae4661SGnoCiYeH     use parse::UnitParser;
99e5a8055aSGnoCiYeH 
10017ae4661SGnoCiYeH     use crate::{
10117ae4661SGnoCiYeH         executor::Executor,
102*b40b6b4dSGnoCiYeH         manager::{timer_manager::TimerManager, Manager, UnitManager},
10317ae4661SGnoCiYeH     };
104f206f17aSGnoCiYeH 
10517ae4661SGnoCiYeH     let mut units_file_name = Vec::new();
10656797478SGnoCiYeH     //读取目录里面的unit文件
10756797478SGnoCiYeH     if let Ok(entries) = fs::read_dir("/bin") {
10856797478SGnoCiYeH         for entry in entries {
10956797478SGnoCiYeH             if let Ok(entry) = entry {
11056797478SGnoCiYeH                 if let Ok(file_type) = entry.file_type() {
11156797478SGnoCiYeH                     if file_type.is_file() {
11256797478SGnoCiYeH                         let filename = entry.file_name();
113*b40b6b4dSGnoCiYeH                         let _filename = filename.to_str().unwrap();
11417ae4661SGnoCiYeH                         //units_file_name.push(filename.to_string());
11556797478SGnoCiYeH                     }
11656797478SGnoCiYeH                 }
11756797478SGnoCiYeH             }
11856797478SGnoCiYeH         }
11956797478SGnoCiYeH     }
120e5a8055aSGnoCiYeH 
12156797478SGnoCiYeH     units_file_name.push("/home/heyicong/DragonReach/parse_test/test.service".to_string());
12217ae4661SGnoCiYeH 
123*b40b6b4dSGnoCiYeH     // 完善unit之间依赖关系
124*b40b6b4dSGnoCiYeH     UnitManager::init_units_dependencies();
125*b40b6b4dSGnoCiYeH 
12617ae4661SGnoCiYeH     //启动服务
12717ae4661SGnoCiYeH     for path in units_file_name {
12817ae4661SGnoCiYeH         let id = match UnitParser::from_path(&path) {
12917ae4661SGnoCiYeH             Ok(id) => id,
130f206f17aSGnoCiYeH             Err(e) => {
13117ae4661SGnoCiYeH                 eprintln!("Err:{}", e.error_format());
13217ae4661SGnoCiYeH                 0
133f206f17aSGnoCiYeH             }
134f206f17aSGnoCiYeH         };
135f206f17aSGnoCiYeH 
13617ae4661SGnoCiYeH         if id != 0 {
137*b40b6b4dSGnoCiYeH             let _unit = UnitManager::get_unit_with_id(&id).unwrap();
138*b40b6b4dSGnoCiYeH             if let Err(e) = Executor::exec(id) {
13917ae4661SGnoCiYeH                 eprintln!("Err:{}", e.error_format());
14017ae4661SGnoCiYeH             }
14117ae4661SGnoCiYeH         }
14217ae4661SGnoCiYeH     }
14317ae4661SGnoCiYeH 
14417ae4661SGnoCiYeH     // 启动完服务后进入主循环
14517ae4661SGnoCiYeH     loop {
14617ae4661SGnoCiYeH         // 检查各服务运行状态
14717ae4661SGnoCiYeH         Manager::check_running_status();
1481dcc785bSGnoCiYeH 
149*b40b6b4dSGnoCiYeH         Manager::check_cmd_proc();
150*b40b6b4dSGnoCiYeH 
151*b40b6b4dSGnoCiYeH         TimerManager::check_timer();
152e5a8055aSGnoCiYeH     }
15321fc724cSGnoCiYeH }
154