xref: /DragonReach/src/main.rs (revision 1dcc785b2b7c7204ef36a061f07bfaaaee27ec8e)
1*1dcc785bSGnoCiYeH #![no_std]
2*1dcc785bSGnoCiYeH #![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;
45f206f17aSGnoCiYeH 
4617ae4661SGnoCiYeH     // use crate::{
4717ae4661SGnoCiYeH     //     executor::Executor,
4817ae4661SGnoCiYeH     //     manager::{Manager, UnitManager},
4917ae4661SGnoCiYeH     //     parse::parse_util::UnitParseUtil,
5017ae4661SGnoCiYeH     // };
5117ae4661SGnoCiYeH     // let id = ServiceUnit::from_path("/etc/reach/system/shell.service").unwrap();
5217ae4661SGnoCiYeH 
5317ae4661SGnoCiYeH     // if id != 0 {
5417ae4661SGnoCiYeH     //     let unit = UnitManager::get_unit_with_id(&id).unwrap();
5517ae4661SGnoCiYeH     //     if let Err(e) = Executor::exec(&unit) {
5617ae4661SGnoCiYeH     //         eprintln!("Err:{}", e.error_format());
5717ae4661SGnoCiYeH     //     }
5817ae4661SGnoCiYeH     // }
5917ae4661SGnoCiYeH 
6017ae4661SGnoCiYeH     // loop{
6117ae4661SGnoCiYeH 
6217ae4661SGnoCiYeH     // }
6317ae4661SGnoCiYeH 
6417ae4661SGnoCiYeH     //================================
6517ae4661SGnoCiYeH     // use std::process::Command;
6617ae4661SGnoCiYeH     // use std::process::Stdio;
6717ae4661SGnoCiYeH     // let proc = unsafe {
6817ae4661SGnoCiYeH     //     Command::new("/bin/shell.elf")
6917ae4661SGnoCiYeH     //         .stderr(Stdio::inherit())
7017ae4661SGnoCiYeH     //         .stdout(Stdio::inherit())
7117ae4661SGnoCiYeH     //         .stdin(Stdio::inherit())
7217ae4661SGnoCiYeH     //         .spawn()
7317ae4661SGnoCiYeH     // };
7417ae4661SGnoCiYeH 
7517ae4661SGnoCiYeH     // match proc {
7617ae4661SGnoCiYeH     //     Ok(p) => {
7717ae4661SGnoCiYeH     //         println!("Service running...");
7817ae4661SGnoCiYeH     //     }
7917ae4661SGnoCiYeH     //     Err(err) => {
8017ae4661SGnoCiYeH     //         eprintln!(": Service startup failed: {}",err);
8117ae4661SGnoCiYeH     //     }
8217ae4661SGnoCiYeH     // }
8317ae4661SGnoCiYeH 
8417ae4661SGnoCiYeH     // loop {
8517ae4661SGnoCiYeH 
8617ae4661SGnoCiYeH     // }
8717ae4661SGnoCiYeH 
8817ae4661SGnoCiYeH     //========================================
8917ae4661SGnoCiYeH 
9017ae4661SGnoCiYeH     use parse::UnitParser;
9117ae4661SGnoCiYeH 
9217ae4661SGnoCiYeH     use crate::{
9317ae4661SGnoCiYeH         executor::Executor,
9417ae4661SGnoCiYeH         manager::{Manager, UnitManager},
9517ae4661SGnoCiYeH         parse::parse_util::UnitParseUtil,
9617ae4661SGnoCiYeH     };
9717ae4661SGnoCiYeH 
9817ae4661SGnoCiYeH     let mut units_file_name = Vec::new();
9917ae4661SGnoCiYeH     //读取目录里面的unit文件
10017ae4661SGnoCiYeH     if let Ok(entries) = fs::read_dir(DRAGON_REACH_UNIT_DIR) {
10117ae4661SGnoCiYeH         for entry in entries {
10217ae4661SGnoCiYeH             if let Ok(entry) = entry {
10317ae4661SGnoCiYeH                 let filename = entry.file_name();
10417ae4661SGnoCiYeH                 let filename = filename.to_str().unwrap();
10517ae4661SGnoCiYeH                 units_file_name.push(filename.to_string());
10617ae4661SGnoCiYeH             }
10717ae4661SGnoCiYeH         }
10817ae4661SGnoCiYeH     }
10917ae4661SGnoCiYeH 
11017ae4661SGnoCiYeH     units_file_name.push(String::from("shell.service"));
11117ae4661SGnoCiYeH 
11217ae4661SGnoCiYeH     println!("files: {:?}", units_file_name);
11317ae4661SGnoCiYeH 
11417ae4661SGnoCiYeH     //启动服务
11517ae4661SGnoCiYeH     for path in units_file_name {
11617ae4661SGnoCiYeH         let id = match UnitParser::from_path(&path) {
11717ae4661SGnoCiYeH             Ok(id) => id,
118f206f17aSGnoCiYeH             Err(e) => {
11917ae4661SGnoCiYeH                 eprintln!("Err:{}", e.error_format());
12017ae4661SGnoCiYeH                 0
1214e851715SGnoCiYeH             }
122f206f17aSGnoCiYeH         };
12317ae4661SGnoCiYeH 
12417ae4661SGnoCiYeH         if id != 0 {
12517ae4661SGnoCiYeH             let unit = UnitManager::get_unit_with_id(&id).unwrap();
12617ae4661SGnoCiYeH             if let Err(e) = Executor::exec(&unit) {
12717ae4661SGnoCiYeH                 eprintln!("Err:{}", e.error_format());
12817ae4661SGnoCiYeH             } else {
12917ae4661SGnoCiYeH                 println!("Service {} startup success...", id);
13017ae4661SGnoCiYeH             }
13117ae4661SGnoCiYeH         }
13217ae4661SGnoCiYeH     }
13317ae4661SGnoCiYeH 
13417ae4661SGnoCiYeH     // 启动完服务后进入主循环
13517ae4661SGnoCiYeH     loop {
13617ae4661SGnoCiYeH         // 检查各服务运行状态
13717ae4661SGnoCiYeH         //eprintln!(".");
13817ae4661SGnoCiYeH         Manager::check_running_status();
13917ae4661SGnoCiYeH     }
140f206f17aSGnoCiYeH }
141f206f17aSGnoCiYeH 
142f206f17aSGnoCiYeH #[cfg(not(target_os = "dragonos"))]
143f206f17aSGnoCiYeH fn main() {
144*1dcc785bSGnoCiYeH     use std::time::Instant;
145*1dcc785bSGnoCiYeH 
14617ae4661SGnoCiYeH     use parse::UnitParser;
147e5a8055aSGnoCiYeH 
14817ae4661SGnoCiYeH     use crate::{
14917ae4661SGnoCiYeH         executor::Executor,
15017ae4661SGnoCiYeH         manager::{Manager, UnitManager},
15117ae4661SGnoCiYeH         parse::parse_util::UnitParseUtil,
15217ae4661SGnoCiYeH     };
153f206f17aSGnoCiYeH 
15417ae4661SGnoCiYeH     let mut units_file_name = Vec::new();
15517ae4661SGnoCiYeH     // //读取目录里面的unit文件
15617ae4661SGnoCiYeH     // if let Ok(entries) = fs::read_dir(DRAGON_REACH_UNIT_DIR) {
15717ae4661SGnoCiYeH     //     for entry in entries {
15817ae4661SGnoCiYeH     //         if let Ok(entry) = entry {
15917ae4661SGnoCiYeH     //             if let Ok(file_type) = entry.file_type() {
16017ae4661SGnoCiYeH     //                 if file_type.is_file() {
16117ae4661SGnoCiYeH     //                     let filename = entry.file_name();
16217ae4661SGnoCiYeH     //                     let filename = filename.to_str().unwrap();
16317ae4661SGnoCiYeH     //                     units_file_name.push(filename.to_string());
16417ae4661SGnoCiYeH     //                 }
16517ae4661SGnoCiYeH     //             }
16617ae4661SGnoCiYeH     //         }
16717ae4661SGnoCiYeH     //     }
16817ae4661SGnoCiYeH     // }
169e5a8055aSGnoCiYeH 
17017ae4661SGnoCiYeH     units_file_name.push("/home/heyicong/DragonReach/parse_test/test.service");
17117ae4661SGnoCiYeH 
17217ae4661SGnoCiYeH     //启动服务
17317ae4661SGnoCiYeH     for path in units_file_name {
17417ae4661SGnoCiYeH         let id = match UnitParser::from_path(&path) {
17517ae4661SGnoCiYeH             Ok(id) => id,
176f206f17aSGnoCiYeH             Err(e) => {
17717ae4661SGnoCiYeH                 eprintln!("Err:{}", e.error_format());
17817ae4661SGnoCiYeH                 0
179f206f17aSGnoCiYeH             }
180f206f17aSGnoCiYeH         };
181f206f17aSGnoCiYeH 
18217ae4661SGnoCiYeH         if id != 0 {
18317ae4661SGnoCiYeH             let unit = UnitManager::get_unit_with_id(&id).unwrap();
184e5a8055aSGnoCiYeH             if let Err(e) = Executor::exec(&unit) {
18517ae4661SGnoCiYeH                 eprintln!("Err:{}", e.error_format());
18617ae4661SGnoCiYeH             }
18717ae4661SGnoCiYeH         }
18817ae4661SGnoCiYeH     }
18917ae4661SGnoCiYeH 
19017ae4661SGnoCiYeH     // 启动完服务后进入主循环
19117ae4661SGnoCiYeH     loop {
192*1dcc785bSGnoCiYeH         let time = Instant::now();
193*1dcc785bSGnoCiYeH 
19417ae4661SGnoCiYeH         // 检查各服务运行状态
19517ae4661SGnoCiYeH         Manager::check_running_status();
19617ae4661SGnoCiYeH         //println!(".");
197*1dcc785bSGnoCiYeH 
198*1dcc785bSGnoCiYeH         let t = time.elapsed().as_secs_f64();
199*1dcc785bSGnoCiYeH         println!("{}",t);
200e5a8055aSGnoCiYeH     }
20121fc724cSGnoCiYeH }
202