xref: /DragonReach/src/main.rs (revision 56797478d93fa8dbba9da9f520575e529b5f3326)
11dcc785bSGnoCiYeH #![no_std]
21dcc785bSGnoCiYeH #![no_main]
3e5a8055aSGnoCiYeH #![feature(slice_pattern)]
4e5a8055aSGnoCiYeH 
5f206f17aSGnoCiYeH use cfg_if::cfg_if;
621fc724cSGnoCiYeH 
7f206f17aSGnoCiYeH cfg_if! {
8f206f17aSGnoCiYeH     if #[cfg(target_os = "dragonos")]{
9f206f17aSGnoCiYeH         extern crate drstd;
10f206f17aSGnoCiYeH         use drstd as std;
11f206f17aSGnoCiYeH     }
12f206f17aSGnoCiYeH }
13f206f17aSGnoCiYeH 
14f206f17aSGnoCiYeH extern crate hashbrown;
154e851715SGnoCiYeH 
1621fc724cSGnoCiYeH mod contants;
174e851715SGnoCiYeH mod error;
1817ae4661SGnoCiYeH mod executor;
1917ae4661SGnoCiYeH mod manager;
204e851715SGnoCiYeH mod parse;
214e851715SGnoCiYeH mod task;
2221fc724cSGnoCiYeH mod unit;
2321fc724cSGnoCiYeH 
244e851715SGnoCiYeH use crate::unit::service;
2517ae4661SGnoCiYeH use std::eprint;
2617ae4661SGnoCiYeH use std::eprintln;
2717ae4661SGnoCiYeH use std::fs;
2817ae4661SGnoCiYeH use std::print;
2917ae4661SGnoCiYeH use std::println;
3017ae4661SGnoCiYeH use std::string::{String, ToString};
3117ae4661SGnoCiYeH use std::vec::Vec;
3217ae4661SGnoCiYeH use unit::service::ServiceUnit;
3321fc724cSGnoCiYeH 
344e851715SGnoCiYeH use self::unit::Unit;
3517ae4661SGnoCiYeH use error::ErrorFormat;
364e851715SGnoCiYeH 
374e851715SGnoCiYeH pub struct FileDescriptor(usize);
384e851715SGnoCiYeH 
3917ae4661SGnoCiYeH const DRAGON_REACH_UNIT_DIR: &'static str = "/etc/reach/system/";
4017ae4661SGnoCiYeH 
41f206f17aSGnoCiYeH #[cfg(target_os = "dragonos")]
42f206f17aSGnoCiYeH #[no_mangle]
434e851715SGnoCiYeH fn main() {
4417ae4661SGnoCiYeH     use parse::UnitParser;
4517ae4661SGnoCiYeH 
4617ae4661SGnoCiYeH     use crate::{
4717ae4661SGnoCiYeH         executor::Executor,
4817ae4661SGnoCiYeH         manager::{Manager, UnitManager},
4917ae4661SGnoCiYeH         parse::parse_util::UnitParseUtil,
5017ae4661SGnoCiYeH     };
5117ae4661SGnoCiYeH 
5217ae4661SGnoCiYeH     let mut units_file_name = Vec::new();
5317ae4661SGnoCiYeH     //读取目录里面的unit文件
5417ae4661SGnoCiYeH     if let Ok(entries) = fs::read_dir(DRAGON_REACH_UNIT_DIR) {
5517ae4661SGnoCiYeH         for entry in entries {
5617ae4661SGnoCiYeH             if let Ok(entry) = entry {
57*56797478SGnoCiYeH                 if let Ok(file_type) = entry.file_type() {
58*56797478SGnoCiYeH                     if file_type.is_file() {
5917ae4661SGnoCiYeH                         let filename = entry.file_name();
6017ae4661SGnoCiYeH                         let filename = filename.to_str().unwrap();
6117ae4661SGnoCiYeH                         units_file_name.push(filename.to_string());
6217ae4661SGnoCiYeH                     }
6317ae4661SGnoCiYeH                 }
6417ae4661SGnoCiYeH             }
65*56797478SGnoCiYeH         }
66*56797478SGnoCiYeH     }
6717ae4661SGnoCiYeH 
6817ae4661SGnoCiYeH     //启动服务
6917ae4661SGnoCiYeH     for path in units_file_name {
7017ae4661SGnoCiYeH         let id = match UnitParser::from_path(&path) {
7117ae4661SGnoCiYeH             Ok(id) => id,
72f206f17aSGnoCiYeH             Err(e) => {
7317ae4661SGnoCiYeH                 eprintln!("Err:{}", e.error_format());
7417ae4661SGnoCiYeH                 0
754e851715SGnoCiYeH             }
76f206f17aSGnoCiYeH         };
7717ae4661SGnoCiYeH 
7817ae4661SGnoCiYeH         if id != 0 {
7917ae4661SGnoCiYeH             let unit = UnitManager::get_unit_with_id(&id).unwrap();
8017ae4661SGnoCiYeH             if let Err(e) = Executor::exec(&unit) {
8117ae4661SGnoCiYeH                 eprintln!("Err:{}", e.error_format());
8217ae4661SGnoCiYeH             } else {
8317ae4661SGnoCiYeH                 println!("Service {} startup success...", id);
8417ae4661SGnoCiYeH             }
8517ae4661SGnoCiYeH         }
8617ae4661SGnoCiYeH     }
8717ae4661SGnoCiYeH 
8817ae4661SGnoCiYeH     // 启动完服务后进入主循环
8917ae4661SGnoCiYeH     loop {
9017ae4661SGnoCiYeH         // 检查各服务运行状态
9117ae4661SGnoCiYeH         //eprintln!(".");
9217ae4661SGnoCiYeH         Manager::check_running_status();
9317ae4661SGnoCiYeH     }
94f206f17aSGnoCiYeH }
95f206f17aSGnoCiYeH 
96f206f17aSGnoCiYeH #[cfg(not(target_os = "dragonos"))]
97f206f17aSGnoCiYeH fn main() {
981dcc785bSGnoCiYeH     use std::time::Instant;
991dcc785bSGnoCiYeH 
10017ae4661SGnoCiYeH     use parse::UnitParser;
101e5a8055aSGnoCiYeH 
10217ae4661SGnoCiYeH     use crate::{
10317ae4661SGnoCiYeH         executor::Executor,
10417ae4661SGnoCiYeH         manager::{Manager, UnitManager},
10517ae4661SGnoCiYeH         parse::parse_util::UnitParseUtil,
10617ae4661SGnoCiYeH     };
107f206f17aSGnoCiYeH 
10817ae4661SGnoCiYeH     let mut units_file_name = Vec::new();
109*56797478SGnoCiYeH     //读取目录里面的unit文件
110*56797478SGnoCiYeH     if let Ok(entries) = fs::read_dir("/bin") {
111*56797478SGnoCiYeH         for entry in entries {
112*56797478SGnoCiYeH             if let Ok(entry) = entry {
113*56797478SGnoCiYeH                 if let Ok(file_type) = entry.file_type() {
114*56797478SGnoCiYeH                     if file_type.is_file() {
115*56797478SGnoCiYeH                         let filename = entry.file_name();
116*56797478SGnoCiYeH                         let filename = filename.to_str().unwrap();
11717ae4661SGnoCiYeH                         //units_file_name.push(filename.to_string());
118*56797478SGnoCiYeH                     }
119*56797478SGnoCiYeH                 }
120*56797478SGnoCiYeH             }
121*56797478SGnoCiYeH         }
122*56797478SGnoCiYeH     }
123e5a8055aSGnoCiYeH 
124*56797478SGnoCiYeH     units_file_name.push("/home/heyicong/DragonReach/parse_test/test.service".to_string());
12517ae4661SGnoCiYeH 
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 {
13717ae4661SGnoCiYeH             let unit = UnitManager::get_unit_with_id(&id).unwrap();
138e5a8055aSGnoCiYeH             if let Err(e) = Executor::exec(&unit) {
13917ae4661SGnoCiYeH                 eprintln!("Err:{}", e.error_format());
14017ae4661SGnoCiYeH             }
14117ae4661SGnoCiYeH         }
14217ae4661SGnoCiYeH     }
14317ae4661SGnoCiYeH 
14417ae4661SGnoCiYeH     // 启动完服务后进入主循环
14517ae4661SGnoCiYeH     loop {
1461dcc785bSGnoCiYeH         let time = Instant::now();
1471dcc785bSGnoCiYeH 
14817ae4661SGnoCiYeH         // 检查各服务运行状态
14917ae4661SGnoCiYeH         Manager::check_running_status();
15017ae4661SGnoCiYeH         //println!(".");
1511dcc785bSGnoCiYeH 
152*56797478SGnoCiYeH         let t = time.elapsed().as_micros();
153*56797478SGnoCiYeH         //println!("{}",t);
154e5a8055aSGnoCiYeH     }
15521fc724cSGnoCiYeH }
156